From ba738e395d6a76d9aef159ca65e2d0877e086717 Mon Sep 17 00:00:00 2001 From: Sunpy Date: Tue, 19 Mar 2019 14:10:14 +0100 Subject: [PATCH] Rewrote serializer type length --- osuRepy/helpers/typeSerializer.py | 50 ++++++++++++++++++------------- osuRepy/replay.py | 33 +++++++------------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/osuRepy/helpers/typeSerializer.py b/osuRepy/helpers/typeSerializer.py index 709ab08..81b1aed 100644 --- a/osuRepy/helpers/typeSerializer.py +++ b/osuRepy/helpers/typeSerializer.py @@ -15,7 +15,7 @@ TYPE_FLOAT = b"f", 4 TYPE_DOUBLE = b"d", 8 TYPE_STRING = b"s", 1 -TYPE_ULEB = 1 # Just so we have the enum type~ish +TYPE_ULEB = b"s", 1, True # True just so they are different tuples BYTEORDER_NATIVE = b"@" BYTEORDER_LITTLE = b"<" @@ -80,11 +80,11 @@ def read(value_type, stream): struct.unpack(value_type[0], stream[:value_type[1]]) class Serializable: - length = None - prefix = b"" - pre_serialized = None + length = None + prefix = b"" + filter = None - def __init__(self, value, type = None, length = None, prefix = b"", pre = None): + def __init__(self, value, type = None, length = None, prefix = b"", filter = None): self.value = value if type is not None: @@ -94,34 +94,44 @@ class Serializable: self.length = length self.prefix = prefix - self.pre_serialized = pre + self.filter = filter - def get_struct_fmt(self): + def get_struct_fmt(self, length = 1): + struct_fmt = self.type[0] + if length > 1: + struct_fmt = b"%d%b" % (length, self.type[0]) if self.length is not None: - return b"%d%b" % (self.length.value, self.type[0]) - return self.type[0] + if self.length == TYPE_ULEB: + val = self.get_value() + struct_fmt = b"%d%b%b" % (len(val[0]), self.length[0], struct_fmt) + else: + struct_fmt = b"%b%b" % (self.length[0], struct_fmt) + return struct_fmt def get_value(self): - val = self.value - if self.pre_serialized is not None: - val[-1] = self.pre_serialized(val[-1]) + val = [self.value] + if self.filter is not None: + val[-1] = self.filter(val[-1]) + if self.length is not None: + if self.length == TYPE_ULEB: + val.insert(0, encode_uleb(len(val[-1]))) + else: + val.insert(0, len(val[-1])) return val def pack(self, byte_order = BYTEORDER_LITTLE): - if self.type == TYPE_ULEB: - return encode_uleb(self.value) - struct_fmt = byte_order + self.get_struct_fmt() - packed = struct.pack(struct_fmt, self.get_value()) + length = 1 + if self.type == TYPE_STRING: + length = len(self.get_value()[-1]) + struct_fmt = byte_order + self.get_struct_fmt(length) + packed = struct.pack(struct_fmt, *self.get_value()) if self.prefix is not None: packed = self.prefix + packed return packed def unpack(self, length = -1, byte_order = BYTEORDER_LITTLE): - if self.type == TYPE_ULEB: - self.value = decode_uleb(2) - return if self.type == TYPE_STRING: - length = + length = len(self.value) struct_fmt = byte_order + self.get_struct_fmt(length) class Serializer: diff --git a/osuRepy/replay.py b/osuRepy/replay.py index c82317d..739cf3c 100644 --- a/osuRepy/replay.py +++ b/osuRepy/replay.py @@ -18,10 +18,8 @@ from hashlib import md5 as _md5 import traceback def md5_str(data): - print(data) if type(data) is str: data = data.encode("ascii") - print(_md5(data).hexdigest().encode()) return _md5(data).hexdigest().encode() def md5_file(file): @@ -42,17 +40,17 @@ def append_and_compress(data): def lzma_compress(data): comp = pylzma.compress(data, dictionary = 21, fastBytes = 255, eos = False) comp = comp[:5] + struct.pack(b"