-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.py
99 lines (78 loc) · 2.81 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import struct
def read_bool(buffer) -> bool:
return struct.unpack("<?", buffer.read(1))[0]
def read_ubyte(buffer) -> int:
return struct.unpack("<B", buffer.read(1))[0]
def read_ushort(buffer) -> int:
return struct.unpack("<H", buffer.read(2))[0]
def read_uint(buffer) -> int:
return struct.unpack("<I", buffer.read(4))[0]
def read_float(buffer) -> float:
return struct.unpack("<f", buffer.read(4))[0]
def read_double(buffer) -> float:
return struct.unpack("<d", buffer.read(8))[0]
def read_ulong(buffer) -> int:
return struct.unpack("<Q", buffer.read(8))[0]
# osu specific
def read_int_double(buffer):
read_ubyte(buffer)
integer = read_uint(buffer)
read_ubyte(buffer)
double = read_double(buffer)
return (integer, double)
def read_timing_point(buffer):
bpm = read_double(buffer)
offset = read_double(buffer)
inherited = read_bool(buffer)
return (bpm, offset, inherited)
def read_string(buffer) -> str:
strlen = 0
strflag = read_ubyte(buffer)
if (strflag == 0x0b):
strlen = 0
shift = 0
# uleb128
# https://en.wikipedia.org/wiki/LEB128
while True:
byte = read_ubyte(buffer)
strlen |= ((byte & 0x7F) << shift)
if (byte & (1 << 7)) == 0:
break
shift += 7
return (struct.unpack("<" + str(strlen) + "s", buffer.read(strlen))[0]).decode("utf-8")
class WriteBuffer:
def __init__(self):
self.offset = 0
self.data = b""
def write_bool(self, data: bool):
self.data += struct.pack("<?", data)
def write_ubyte(self, data: int):
self.data += struct.pack("<B", data)
def write_ushort(self, data: int):
self.data += struct.pack("<H", data)
def write_uint(self, data: int):
self.data += struct.pack("<I", data)
def write_float(self, data: float):
self.data += struct.pack("<f", data)
def write_double(self, data: float):
self.data += struct.pack("<d", data)
def write_ulong(self, data: int):
self.data += struct.pack("<Q", data)
def write_string(self, data: str):
if len(data) > 0:
self.write_ubyte(0x0b)
strlen = b""
value = len(data.encode('utf-8'))
while value != 0:
byte = (value & 0x7F)
value >>= 7
if value != 0:
byte |= 0x80
strlen += struct.pack("<B", byte)
self.data += strlen
self.data += struct.pack("<" + str(len(data.encode("utf-8"))) +
"s", data.encode("utf-8"))
else:
self.write_ubyte(0x0)
def clear_buffer(self):
self.data = b""