From d10fa6c65cd3d6ee3edb9ca2884acd2db74cc4bc Mon Sep 17 00:00:00 2001 From: derrod Date: Wed, 6 May 2020 18:21:26 +0200 Subject: [PATCH] [models] Support manifest serialization --- legendary/models/json_manifest.py | 7 +- legendary/models/manifest.py | 156 +++++++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 5 deletions(-) diff --git a/legendary/models/json_manifest.py b/legendary/models/json_manifest.py index ac88137..d6ef316 100644 --- a/legendary/models/json_manifest.py +++ b/legendary/models/json_manifest.py @@ -6,7 +6,9 @@ import struct from copy import deepcopy -from legendary.models.manifest import Manifest, ManifestMeta, CDL, ChunkPart, ChunkInfo, FML, FileManifest +from legendary.models.manifest import ( + Manifest, ManifestMeta, CDL, ChunkPart, ChunkInfo, FML, FileManifest, CustomFields +) def blob_to_num(in_str): @@ -48,7 +50,8 @@ class JSONManifest(Manifest): _m.meta = JSONManifestMeta.read(_tmp) _m.chunk_data_list = JSONCDL.read(_tmp, manifest_version=_m.version) _m.file_manifest_list = JSONFML.read(_tmp) - _m.custom_fields = _tmp.pop('CustomFields', dict()) + _m.custom_fields = CustomFields() + _m.custom_fields._dict = _tmp.pop('CustomFields', dict()) if _tmp.keys(): print(f'Did not read JSON keys: {_tmp.keys()}!') diff --git a/legendary/models/manifest.py b/legendary/models/manifest.py index 7cc6ca9..b2cde90 100644 --- a/legendary/models/manifest.py +++ b/legendary/models/manifest.py @@ -31,6 +31,23 @@ def read_fstring(bio): return s +def write_fstring(bio, string): + if not string: + bio.write(struct.pack('= 15: @@ -47,7 +64,7 @@ class Manifest: header_magic = 0x44BEC00C def __init__(self): - self.header_size = 0 + self.header_size = 41 self.size_compressed = 0 self.size_uncompressed = 0 self.sha_hash = '' @@ -95,8 +112,8 @@ class Manifest: _manifest = cls() _manifest.header_size = struct.unpack(' 0: _meta._build_id = read_fstring(bio) - + if bio.tell() != _meta.meta_size: raise ValueError('Did not read entire meta!') @@ -187,6 +232,35 @@ class ManifestMeta: return _meta + def write(self, bio): + meta_start = bio.tell() + + bio.write(struct.pack(' 0: + write_fstring(bio, self.build_id) + + meta_end = bio.tell() + bio.seek(meta_start) + bio.write(struct.pack('