Rewrote serializer type length

This commit is contained in:
Emily 2019-03-19 14:10:14 +01:00
parent 12ea8c4d1b
commit ba738e395d
2 changed files with 41 additions and 42 deletions

View File

@ -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"<"
@ -82,9 +82,9 @@ def read(value_type, stream):
class Serializable:
length = None
prefix = b""
pre_serialized = None
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:

View File

@ -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"<Q", len(data)) + comp[5:] # Append uncompressed length
return struct.pack("<I", len(comp)) + comp # For some odd ass reason this doesnt use ULEB
return comp
class Replay:
_mode = Serializable(osuModes.STANDARD, TYPE_BYTE)
_osu_version = Serializable(20181216, TYPE_INT)
_beatmap_hash_length = Serializable(32, TYPE_ULEB)
_beatmap_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = _beatmap_hash_length, prefix = b"\x0b")
_player_name_length = Serializable(4, TYPE_ULEB)
_player_name = Serializable(b"osu!", TYPE_STRING, length = _player_name_length, prefix = b"\x0b")
_score_hash_length = Serializable(32, TYPE_ULEB)
_score_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING)
#_beatmap_hash_length = Serializable(32, TYPE_ULEB)
_beatmap_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
#_player_name_length = Serializable(4, TYPE_ULEB)
_player_name = Serializable(b"osu!", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
#_score_hash_length = Serializable(32, TYPE_ULEB)
_score_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
_score_300s = Serializable(0, TYPE_USHORT)
_score_100s = Serializable(0, TYPE_USHORT)
@ -66,12 +64,12 @@ class Replay:
_mods = Serializable(osuMods.NOMOD, TYPE_INT)
_lifebar_graph_length = Serializable(4, TYPE_ULEB)
_lifebar_graph = Serializable(b"0|1,", TYPE_STRING, length = _lifebar_graph_length, prefix = b"\x0b")
#_lifebar_graph_length = Serializable(4, TYPE_ULEB)
_lifebar_graph = Serializable(b"0|1,", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
_timestamp = Serializable(0, TYPE_ULLONG)
#_replay_data_length = Serializable(0, TYPE_UINT)
_replay_data = Serializable(b"", TYPE_STRING, pre = append_and_compress)
_replay_data = Serializable(b"", TYPE_STRING, length = TYPE_UINT, filter = append_and_compress)
_online_score_id = Serializable(0, TYPE_ULLONG)
@ -181,16 +179,7 @@ class Replay:
))
)
def update_lengths(self):
self._beatmap_hash_length.value = len(self._beatmap_hash.value)
self._player_name_length.value = len(self._player_name.value)
self._score_hash_length.value = len(self._score_hash.value)
self._lifebar_graph_length.value = len(self._lifebar_graph.value)
#self._replay_data_length.value = len(self._replay_data.get_value())
def update(self):
self.update_lengths()
self.update_perfect()
self.update_score_hash()