Rewrote serializer type length
This commit is contained in:
parent
12ea8c4d1b
commit
ba738e395d
|
@ -15,7 +15,7 @@ TYPE_FLOAT = b"f", 4
|
||||||
TYPE_DOUBLE = b"d", 8
|
TYPE_DOUBLE = b"d", 8
|
||||||
TYPE_STRING = b"s", 1
|
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_NATIVE = b"@"
|
||||||
BYTEORDER_LITTLE = b"<"
|
BYTEORDER_LITTLE = b"<"
|
||||||
|
@ -80,11 +80,11 @@ def read(value_type, stream):
|
||||||
struct.unpack(value_type[0], stream[:value_type[1]])
|
struct.unpack(value_type[0], stream[:value_type[1]])
|
||||||
|
|
||||||
class Serializable:
|
class Serializable:
|
||||||
length = None
|
length = None
|
||||||
prefix = b""
|
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
|
self.value = value
|
||||||
|
|
||||||
if type is not None:
|
if type is not None:
|
||||||
|
@ -94,34 +94,44 @@ class Serializable:
|
||||||
|
|
||||||
self.length = length
|
self.length = length
|
||||||
self.prefix = prefix
|
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:
|
if self.length is not None:
|
||||||
return b"%d%b" % (self.length.value, self.type[0])
|
if self.length == TYPE_ULEB:
|
||||||
return self.type[0]
|
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):
|
def get_value(self):
|
||||||
val = self.value
|
val = [self.value]
|
||||||
if self.pre_serialized is not None:
|
if self.filter is not None:
|
||||||
val[-1] = self.pre_serialized(val[-1])
|
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
|
return val
|
||||||
|
|
||||||
def pack(self, byte_order = BYTEORDER_LITTLE):
|
def pack(self, byte_order = BYTEORDER_LITTLE):
|
||||||
if self.type == TYPE_ULEB:
|
length = 1
|
||||||
return encode_uleb(self.value)
|
if self.type == TYPE_STRING:
|
||||||
struct_fmt = byte_order + self.get_struct_fmt()
|
length = len(self.get_value()[-1])
|
||||||
packed = struct.pack(struct_fmt, self.get_value())
|
struct_fmt = byte_order + self.get_struct_fmt(length)
|
||||||
|
packed = struct.pack(struct_fmt, *self.get_value())
|
||||||
if self.prefix is not None:
|
if self.prefix is not None:
|
||||||
packed = self.prefix + packed
|
packed = self.prefix + packed
|
||||||
return packed
|
return packed
|
||||||
|
|
||||||
def unpack(self, length = -1, byte_order = BYTEORDER_LITTLE):
|
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:
|
if self.type == TYPE_STRING:
|
||||||
length =
|
length = len(self.value)
|
||||||
struct_fmt = byte_order + self.get_struct_fmt(length)
|
struct_fmt = byte_order + self.get_struct_fmt(length)
|
||||||
|
|
||||||
class Serializer:
|
class Serializer:
|
||||||
|
|
|
@ -18,10 +18,8 @@ from hashlib import md5 as _md5
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
def md5_str(data):
|
def md5_str(data):
|
||||||
print(data)
|
|
||||||
if type(data) is str:
|
if type(data) is str:
|
||||||
data = data.encode("ascii")
|
data = data.encode("ascii")
|
||||||
print(_md5(data).hexdigest().encode())
|
|
||||||
return _md5(data).hexdigest().encode()
|
return _md5(data).hexdigest().encode()
|
||||||
|
|
||||||
def md5_file(file):
|
def md5_file(file):
|
||||||
|
@ -42,17 +40,17 @@ def append_and_compress(data):
|
||||||
def lzma_compress(data):
|
def lzma_compress(data):
|
||||||
comp = pylzma.compress(data, dictionary = 21, fastBytes = 255, eos = False)
|
comp = pylzma.compress(data, dictionary = 21, fastBytes = 255, eos = False)
|
||||||
comp = comp[:5] + struct.pack(b"<Q", len(data)) + comp[5:] # Append uncompressed length
|
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:
|
class Replay:
|
||||||
_mode = Serializable(osuModes.STANDARD, TYPE_BYTE)
|
_mode = Serializable(osuModes.STANDARD, TYPE_BYTE)
|
||||||
_osu_version = Serializable(20181216, TYPE_INT)
|
_osu_version = Serializable(20181216, TYPE_INT)
|
||||||
_beatmap_hash_length = Serializable(32, TYPE_ULEB)
|
#_beatmap_hash_length = Serializable(32, TYPE_ULEB)
|
||||||
_beatmap_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = _beatmap_hash_length, prefix = b"\x0b")
|
_beatmap_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
|
||||||
_player_name_length = Serializable(4, TYPE_ULEB)
|
#_player_name_length = Serializable(4, TYPE_ULEB)
|
||||||
_player_name = Serializable(b"osu!", TYPE_STRING, length = _player_name_length, prefix = b"\x0b")
|
_player_name = Serializable(b"osu!", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
|
||||||
_score_hash_length = Serializable(32, TYPE_ULEB)
|
#_score_hash_length = Serializable(32, TYPE_ULEB)
|
||||||
_score_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING)
|
_score_hash = Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
|
||||||
|
|
||||||
_score_300s = Serializable(0, TYPE_USHORT)
|
_score_300s = Serializable(0, TYPE_USHORT)
|
||||||
_score_100s = Serializable(0, TYPE_USHORT)
|
_score_100s = Serializable(0, TYPE_USHORT)
|
||||||
|
@ -66,12 +64,12 @@ class Replay:
|
||||||
|
|
||||||
_mods = Serializable(osuMods.NOMOD, TYPE_INT)
|
_mods = Serializable(osuMods.NOMOD, TYPE_INT)
|
||||||
|
|
||||||
_lifebar_graph_length = Serializable(4, TYPE_ULEB)
|
#_lifebar_graph_length = Serializable(4, TYPE_ULEB)
|
||||||
_lifebar_graph = Serializable(b"0|1,", TYPE_STRING, length = _lifebar_graph_length, prefix = b"\x0b")
|
_lifebar_graph = Serializable(b"0|1,", TYPE_STRING, length = TYPE_ULEB, prefix = b"\x0b")
|
||||||
_timestamp = Serializable(0, TYPE_ULLONG)
|
_timestamp = Serializable(0, TYPE_ULLONG)
|
||||||
|
|
||||||
#_replay_data_length = Serializable(0, TYPE_UINT)
|
#_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)
|
_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):
|
def update(self):
|
||||||
self.update_lengths()
|
|
||||||
self.update_perfect()
|
self.update_perfect()
|
||||||
self.update_score_hash()
|
self.update_score_hash()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user