fix: parallelization effort, progressbar w/ bit-identitical file write compared to sequential path
This commit is contained in:
parent
35e49044f1
commit
8ca5371a98
9 changed files with 407 additions and 387 deletions
116
libray/ird.py
116
libray/ird.py
|
|
@ -23,6 +23,7 @@ import os
|
|||
import sys
|
||||
import zlib
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
|
||||
try:
|
||||
|
|
@ -47,84 +48,91 @@ class IRD:
|
|||
"""
|
||||
|
||||
ORDER = 'little'
|
||||
TEMP_FILE = 'ird'
|
||||
MAGIC_STRING = b'3IRD'
|
||||
|
||||
def __init__(self, ird_path, verbose=False):
|
||||
"""IRD constructor using args from argparse."""
|
||||
|
||||
self.uncompress(ird_path) # TODO: Try/Except?
|
||||
temp_path = self._prepare_temp(ird_path)
|
||||
|
||||
self.size = core.size(self.TEMP_FILE)
|
||||
try:
|
||||
self.size = core.size(temp_path)
|
||||
|
||||
if not self.size:
|
||||
core.error('IRD file is empty!')
|
||||
if not self.size:
|
||||
core.error('IRD file is empty!')
|
||||
|
||||
with open(self.TEMP_FILE, 'rb') as input_ird:
|
||||
if input_ird.read(4) != self.MAGIC_STRING:
|
||||
core.error('Either not an IRD file, corruped IRD file, or unknown IRD format')
|
||||
with open(temp_path, 'rb') as input_ird:
|
||||
if input_ird.read(4) != self.MAGIC_STRING:
|
||||
core.error('Either not an IRD file, corruped IRD file, or unknown IRD format')
|
||||
|
||||
self.version = core.to_int(input_ird.read(1), self.ORDER)
|
||||
self.game_id = input_ird.read(9)
|
||||
name_length = core.read_seven_bit_encoded_int(input_ird, self.ORDER)
|
||||
self.game_name = input_ird.read(name_length).decode('utf8')
|
||||
self.update_version = input_ird.read(4)
|
||||
self.game_version = input_ird.read(5)
|
||||
self.app_version = input_ird.read(5)
|
||||
self.version = core.to_int(input_ird.read(1), self.ORDER)
|
||||
self.game_id = input_ird.read(9)
|
||||
name_length = core.read_seven_bit_encoded_int(input_ird, self.ORDER)
|
||||
self.game_name = input_ird.read(name_length).decode('utf8')
|
||||
self.update_version = input_ird.read(4)
|
||||
self.game_version = input_ird.read(5)
|
||||
self.app_version = input_ird.read(5)
|
||||
|
||||
if self.version == 7:
|
||||
self.identifier = input_ird.read(4)
|
||||
if self.version == 7:
|
||||
self.identifier = input_ird.read(4)
|
||||
|
||||
header_length = (core.to_int(input_ird.read(4), self.ORDER))
|
||||
self.header = input_ird.read(header_length)
|
||||
footer_length = (core.to_int(input_ird.read(4), self.ORDER))
|
||||
self.footer = input_ird.read(footer_length)
|
||||
header_length = (core.to_int(input_ird.read(4), self.ORDER))
|
||||
self.header = input_ird.read(header_length)
|
||||
footer_length = (core.to_int(input_ird.read(4), self.ORDER))
|
||||
self.footer = input_ird.read(footer_length)
|
||||
|
||||
self.region_count = core.to_int(input_ird.read(1), self.ORDER)
|
||||
self.region_hashes = []
|
||||
for _ in range(0, self.region_count):
|
||||
self.region_hashes.append(input_ird.read(16))
|
||||
self.region_count = core.to_int(input_ird.read(1), self.ORDER)
|
||||
self.region_hashes = []
|
||||
for _ in range(0, self.region_count):
|
||||
self.region_hashes.append(input_ird.read(16))
|
||||
|
||||
self.file_count = core.to_int(input_ird.read(4), self.ORDER)
|
||||
self.file_hashes = []
|
||||
for _ in range(0, self.file_count):
|
||||
key = core.to_int(input_ird.read(8), self.ORDER)
|
||||
val = input_ird.read(16)
|
||||
self.file_hashes.append({'key': key, 'val': val})
|
||||
self.file_count = core.to_int(input_ird.read(4), self.ORDER)
|
||||
self.file_hashes = []
|
||||
for _ in range(0, self.file_count):
|
||||
key = core.to_int(input_ird.read(8), self.ORDER)
|
||||
val = input_ird.read(16)
|
||||
self.file_hashes.append({'key': key, 'val': val})
|
||||
|
||||
if self.version >= 9:
|
||||
self.pic = input_ird.read(115)
|
||||
if self.version >= 9:
|
||||
self.pic = input_ird.read(115)
|
||||
|
||||
input_ird.seek(input_ird.tell() + 4) # ?
|
||||
input_ird.seek(input_ird.tell() + 4) # ?
|
||||
|
||||
self.data1 = input_ird.read(16)
|
||||
self.data2 = input_ird.read(16)
|
||||
self.data1 = input_ird.read(16)
|
||||
self.data2 = input_ird.read(16)
|
||||
|
||||
if self.version < 9:
|
||||
self.pic = input_ird.read(115)
|
||||
if self.version < 9:
|
||||
self.pic = input_ird.read(115)
|
||||
|
||||
if self.version < 7:
|
||||
self.uid = core.to_int(input_ird.read(4), self.ORDER)
|
||||
if self.version < 7:
|
||||
self.uid = core.to_int(input_ird.read(4), self.ORDER)
|
||||
|
||||
if verbose:
|
||||
self.print_info()
|
||||
if verbose:
|
||||
self.print_info()
|
||||
finally:
|
||||
os.remove(temp_path)
|
||||
|
||||
os.remove(self.TEMP_FILE)
|
||||
def _prepare_temp(self, filename):
|
||||
"""Decompress (if needed) and copy an IRD file to a unique temp file.
|
||||
|
||||
def uncompress(self, filename):
|
||||
"""Uncompress IRD. Assumes given .ird file is not compressed, but then tries to decompress it with zlib/gzfile if it was not uncompressed."""
|
||||
Returns the path to the temp file. The caller is responsible for
|
||||
deleting it (use a ``try / finally`` block).
|
||||
"""
|
||||
with open(filename, 'rb') as f:
|
||||
magic = f.read(4)
|
||||
|
||||
uncompress = False
|
||||
with open(filename, 'rb') as input_ird:
|
||||
if input_ird.read(4) != self.MAGIC_STRING:
|
||||
uncompress = True
|
||||
fd, tmp_path = tempfile.mkstemp(suffix='.ird')
|
||||
os.close(fd)
|
||||
|
||||
if uncompress:
|
||||
with open(filename, 'rb') as gzfile:
|
||||
with open(self.TEMP_FILE, 'wb') as tmpfile:
|
||||
tmpfile.write(zlib.decompress(gzfile.read(), zlib.MAX_WBITS | 16))
|
||||
if magic != self.MAGIC_STRING:
|
||||
# Compressed — decompress to the temp file
|
||||
with open(filename, 'rb') as gzfile, open(tmp_path, 'wb') as tmpfile:
|
||||
tmpfile.write(zlib.decompress(gzfile.read(), zlib.MAX_WBITS | 16))
|
||||
else:
|
||||
shutil.copyfile(filename, self.TEMP_FILE)
|
||||
# Uncompressed — copy to the temp file
|
||||
shutil.copyfile(filename, tmp_path)
|
||||
|
||||
return tmp_path
|
||||
|
||||
def print_info(self, regions=False):
|
||||
# TODO: This could probably have been a __str__? Who cares?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue