Try utf-16 if utf-8 fails

This commit is contained in:
Dustin Pianalto 2018-05-09 15:18:02 -08:00
parent 0b29d1a3e1
commit 1c3e207f5d
2 changed files with 16 additions and 1 deletions

View File

@ -49,7 +49,14 @@ def process_file(in_file, file_type) -> ConfigParser:
bot_config = json.load(f)
ignore_strings = bot_config['ignore_strings'][file_type]
keep_blocks = bot_config['keep_blocks'][file_type]
data = list(map(bytes.decode, in_file.readlines()))
lines = in_file.readlines()
try:
data = [line.decode('utf-8') for line in lines]
except UnicodeDecodeError:
try:
data = [line.decode('utf-16') for line in lines]
except UnicodeDecodeError:
return 0
clean_data = list()
if ignore_strings:
@ -88,10 +95,14 @@ def process_files(z) -> (ConfigParser, ConfigParser, list):
if filename.lower() == 'game.ini':
# Clean the Game.ini file, removing unnecessary lines
game_config = process_file(z.open(filename), 'game.ini')
if not game_config:
return 0, 0, 0
mods = check_for_mods(z.open(filename))
elif 'DinoExport' in filename:
# Get the contents of all DinoExport_*.ini files loaded into a dict
dino_data[filename] = process_file(z.open(filename), 'dino.ini')
if not dino_data[filename]:
return 0, 0, 0
if not mods:
mods = check_for_modded_dinos(dino_data, mods)
return game_config, dino_data, mods

View File

@ -24,6 +24,10 @@ class Uploader:
await attachment.save(file)
unzipped = process_files.load_zip(file)
game_ini, dinos_data, mods = process_files.process_files(unzipped)
if not game_ini and not dinos_data and not mods:
await msg.edit(content='There was an encoding error with one of the files provided'
'and they cannot be processed')
return
if dinos_data == dict():
await msg.edit(content='There aren\'t any DinoExport files in the zip file attached.\n'
'Please make sure the files have not been renamed.')