diff --git a/config/bot_config.json b/config/bot_config.json index 7e8f2b5..a0734d0 100644 --- a/config/bot_config.json +++ b/config/bot_config.json @@ -27,7 +27,9 @@ "Dino Data", "Colorization", "Max Character Status Values", - "Dino Ancestry" + "Dino Ancestry", + "DinoAncestors", + "DinoAncestorsMale" ] }, "mods": { diff --git a/exts/imports/process_files.py b/exts/imports/process_files.py index c128a4f..081127b 100644 --- a/exts/imports/process_files.py +++ b/exts/imports/process_files.py @@ -12,11 +12,8 @@ class MissingFile(Exception): pass -def open_zip(file: str) -> zipfile.ZipFile: - if file.endswith('.zip'): - return zipfile.ZipFile(file) - else: - raise FileNotFoundError('File name must end in .zip') +def load_zip(file) -> zipfile.ZipFile: + return zipfile.ZipFile(file) def check_for_mods(game_file) -> list: @@ -104,7 +101,7 @@ def generate_game_ini(game_config, mods, directory): print(game_config.sections()) if mods: game_config['/script/shootergame.shootergamemode']['ModIDS'] = ', '.join(mods) - with open(f'{directory}Game.ini', 'w') as f: + with open(f'{directory}/Game.ini', 'w') as f: game_config.write(f, space_around_delimiters=False) @@ -113,5 +110,5 @@ def generate_dino_files(dino_data, directory): print(filename) dino['Dino Data']['Guid'] = guid.get_guid_string(int(dino['Dino Data']['DinoID1']), int(dino['Dino Data']['DinoID2'])) - with open(f'{directory}{filename}', 'w') as f: + with open(f'{directory}/{filename}', 'w') as f: dino.write(f, space_around_delimiters=False) diff --git a/exts/uploader.py b/exts/uploader.py index 671a180..aa892f4 100644 --- a/exts/uploader.py +++ b/exts/uploader.py @@ -1,20 +1,53 @@ import discord from discord.ext import commands +from io import BytesIO +from .imports import process_files +from configparser import ConfigParser +import os + +storage_dir = '../ASB_dino_submissions' class Uploader: def __init__(self, bot): self.bot = bot - @commands.command(name='submit', aliases=['upload']) - async def upload_dino(self, ctx, official: str='unofficial'): - if official == 'unofficial': - pass - elif official == 'official': - pass + @commands.command(name='upload', aliases=['submit']) + async def upload_dino(self, ctx, official: str='unofficial', singleplayer: bool=False): + if ctx.message.attachments: + attachment = ctx.message.attachements[0] + if attachment.filename.endswith('.zip'): + with BytesIO() as file: + await attachment.save(file) + unzipped = process_files.load_zip(file) + game_ini, dinos_data, mods = process_files.process_files(unzipped) + if dinos_data == dict(): + await ctx.send('There aren\'t any DinoExport files in the zip file attached.\n' + 'Please make sure the files have not been renamed.') + else: + if official == 'unofficial' and game_ini == ConfigParser(): + await ctx.send('Game.ini is missing or is not valid.') + return + elif official == 'official' and game_ini == ConfigParser(): + if singleplayer: + game_ini.add_section('/script/shootergame.shootergamemode') + game_ini.set('/script/shootergame.shootergamemode', 'bUseSingleplayerSettings', True) + elif official not in ['official', 'unofficial']: + await ctx.send(f'{official} is not a valid option. Please specify "official" or ' + f'"unofficial" or leave it blank to default to "unofficial"') + return + if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'): + os.mkdir(f'{storage_dir}/{ctx.author.id}') + directory = f'{storage_dir}/{ctx.author.id}/{attachment.filename}_' \ + f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}' + os.mkdir(directory) + process_files.generate_game_ini(game_ini, directory, mods) + process_files.generate_dino_files(dinos_data, directory) + await ctx.send('Upload complete.') + else: + await ctx.send('Please attach a zip file to the command.') else: - await ctx.send(f'{official} is not a valid option. Please specify "official" or "unofficial" or leave it ' - f'blank to default to "unofficial"') + await ctx.send('Please attach a zip file to the command.') def setup(bot):