Created
November 11, 2019 20:58
-
-
Save shchmue/107b58e35376cd61e9b12a26b10edab2 to your computer and use it in GitHub Desktop.
Decompresses SystemPlayReport .msgpack.lz4 files if needed and converts msgpack to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import struct | |
import sys | |
import json | |
import base64 | |
import os | |
from pathlib import Path | |
import msgpack | |
import lz4.block | |
if len(sys.argv) != 2: | |
sys.exit('Usage: ' + sys.argv[0] + ' [dirname]') | |
MSGPACK_PATH = Path(sys.argv[1]) | |
if not MSGPACK_PATH.exists() or not MSGPACK_PATH.is_dir(): | |
sys.exit('Path "' + str(MSGPACK_PATH) + '" does not exist or is not a directory.') | |
for filepath in MSGPACK_PATH.iterdir(): | |
if not filepath.is_file(): | |
continue | |
decomp_filepath = None | |
decomp = bytes() | |
with filepath.open(mode='rb') as f: | |
data = f.read() | |
file_size = len(data) | |
print('File ' + filepath.name + ' size: ', file_size) | |
if filepath.suffix == '.lz4': | |
i = 0 | |
while i < file_size: | |
chunk_size = struct.unpack('<I', data[i:i+4])[0] | |
i += 4 | |
decomp += lz4.block.decompress(data[i:i+chunk_size], uncompressed_size=0x400000) | |
i += chunk_size | |
decomp_filepath = filepath.with_suffix('') | |
print('Decompressed size: ', len(decomp)) | |
with decomp_filepath.open(mode='wb') as f: | |
f.write(decomp) | |
if len(decomp) == 0: | |
decomp = data | |
if decomp_filepath or filepath.suffix == '.msgpack': | |
def ext_hook(_, hook_data): | |
return hook_data.hex() | |
class Base64Encoder(json.JSONEncoder): | |
# pylint: disable=method-hidden | |
def default(self, o): | |
if isinstance(o, bytes): | |
return base64.b64encode(o).decode() | |
return json.JSONEncoder.default(self, o) | |
with filepath.with_suffix('').with_suffix('.json').open(mode='w') as f: | |
decomp = msgpack.unpackb(decomp, raw=False, ext_hook=ext_hook) | |
json.dump(decomp, f, cls=Base64Encoder) | |
f.flush() | |
print('Final JSON size: ', os.fstat(f.fileno()).st_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment