-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathosr.py
208 lines (184 loc) · 4.02 KB
/
osr.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import lzma
import struct
from collections import deque, namedtuple
MODES = ["osu!", "Taiko", "Catch the Beat", "osu!mania"]
SHORTMODS = [
None,
"NF",
"EZ",
None,
"HD",
"HR",
"SD",
"DT",
"RX",
"HT",
"NC",
"FL",
"AO",
"SO",
"AP",
"PF",
"4K",
"5K",
"6K",
"7K",
"8K",
"FI",
"RD",
None,
"TP",
"9K",
"CO",
"1K",
"3K",
"2K",
]
MODS = [
"None",
"NoFail",
"Easy",
"NoVideo",
"Hidden",
"HardRock",
"SuddenDeath",
"DoubleTime",
"Relax",
"HalfTime",
"NightCore",
"Flashlight",
"Autoplay",
"SpunOut",
"Autopilot",
"Perfect",
"Key4",
"Key5",
"Key6",
"Key7",
"Key8",
"FadeIn",
"Random",
"Cinema",
"TargetPractice",
"Key9",
"Co-op",
"Key1",
"Key3",
"Key2",
]
def shortmods(n):
i = 1
s = ""
while n:
if n & 1:
s += SHORTMODS[i]
i += 1
n >>= 1
return s
def parse_uleb128(f):
result = 0
shift = 0
while True:
byte = struct.unpack("<B", f.read(1))[0]
result |= (byte & 0x7F) << shift
if (byte & 0x80) == 0:
break
shift += 7
return result
def parse_string(f):
head = struct.unpack("<B", f.read(1))[0]
if head == 0x00:
return ""
elif head == 0x0B:
length = parse_uleb128(f)
return f.read(length).decode()
def each_bit(n, count):
for x in range(count):
yield n & (1 << x)
def keys(z):
k1 = z & 5 == 5
k2 = z & 10 == 10
yield k1
yield k2
yield not k1 and z & 1 == 1
yield not k2 and z & 2 == 2
yield z & 16 == 16
ReplayPoint = namedtuple("ReplayPoint", "t x y z")
class Replay:
__slots__ = [
"mode",
"version",
"beatmap_hash",
"player",
"replay_hash",
"n300",
"n100",
"n50",
"ngeki",
"nkatu",
"nmiss",
"score",
"combo",
"perfect",
"mods",
"life_events",
"timestamp",
"length",
"replay",
"color",
]
def read_file(self, f, flip_hr=False):
self.mode, self.version = struct.unpack("<BI", f.read(5))
assert self.mode == 0, "%s support not added yet" % MODES[self.mode]
self.beatmap_hash = parse_string(f)
self.player = parse_string(f)
self.replay_hash = parse_string(f)
(
self.n300,
self.n100,
self.n50,
self.ngeki,
self.nkatu,
self.nmiss,
self.score,
self.combo,
self.perfect,
self.mods,
) = struct.unpack("<HHHHHHIH?I", f.read(23))
self.life_events = deque()
for rec in parse_string(f).split(","):
if rec:
u, v = rec.split("|")
self.life_events.append((int(u), float(v)))
self.timestamp, self.length = struct.unpack("<QI", f.read(12))
self.replay = replay = deque()
flip = flip_hr and self.has_mod(16)
t = 0
for rec in lzma.decompress(f.read(self.length)).decode().split(","):
if rec:
w, x, y, z = rec.split("|")
w, x, y, z = int(w), float(x), float(y), int(z)
t += w
if flip:
y = 384 - y
p = ReplayPoint(t, x, y, z)
replay.append(p)
def has_mod(self, mod):
return self.mods & mod == mod
def __len__(self):
return len(self.replay)
def __getitem__(self, t):
if t < len(self):
return self.replay[t]
return self.replay[-1]
def _key(self):
return (self.score, -self.timestamp, self.player)
def __lt__(self, other):
return self._key() < other._key()
def read_file(f, flip_hr=False):
if isinstance(f, str):
with open(f, "rb") as ff:
return read_file(ff, flip_hr)
r = Replay()
r.read_file(f, flip_hr)
return r