Skip to content

Commit ef64039

Browse files
committed
Clean up logic for #102 and document the reasoning
1 parent f228bd7 commit ef64039

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

sc2reader/utils.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,23 +452,28 @@ def open_archive(replay_file):
452452
raise exceptions.MPQError("Unable to construct the MPQArchive",e), None, trace
453453

454454
def extract_data_file(data_file, archive):
455-
# To wrap mpyq exceptions we have to do this try hack again.
455+
# Wrap all mpyq related exceptions so they can be distinguished
456+
# from other sc2reader issues later on.
456457
try:
457-
# Some sites tamper with the replay archive files so
458-
# Catch decompression errors and try again before giving up
459-
if data_file == 'replay.message.events':
458+
# Some replays tampered with by 3rd party software report
459+
# block sizes wrong. They can either over report or under
460+
# report. If they over report a non-compressed file might
461+
# attempt decompression. If they under report a compressed
462+
# file might bypass decompression. So do this:
463+
#
464+
# * Attempt to do things normally.
465+
# * Force Decompression and fall back to original exception
466+
# * mpyq doesn't allow you to ignore decompression, it has
467+
# not been a problem yet though.
468+
try:
469+
file_data = archive.read_file(data_file)
470+
except Exception as e:
471+
exc_info = sys.exc_info()
460472
try:
461473
file_data = archive.read_file(data_file, force_decompress=True)
462474
except Exception as e:
463-
# If it doesn't work with forced decompression, try again without it
464-
exc_info = sys.exc_info()
465-
try:
466-
file_data = archive.read_file(data_file, force_decompress=False)
467-
except Exception:
468-
# raise the original exception
469-
raise exc_info[1], None, exc_info[2]
470-
else:
471-
file_data = archive.read_file(data_file)
475+
# raise the original exception
476+
raise exc_info[1], None, exc_info[2]
472477

473478
return file_data
474479

0 commit comments

Comments
 (0)