osu-repy/osuRepy/replay.py

243 lines
8.3 KiB
Python
Raw Normal View History

2019-03-13 05:43:26 +01:00
import struct
import string
2019-03-17 02:57:48 +01:00
#import lzma
import pylzma # Required cause pythons standard library lzma has missing support
2019-03-13 05:43:26 +01:00
2019-03-13 10:59:06 +01:00
from .frame import ReplayFrame
2019-03-13 05:43:26 +01:00
from .helpers import osuModes
from .helpers import osuMods
from .helpers import osuRanks
2019-03-13 09:07:46 +01:00
from .helpers.typeSerializer import *
2019-03-13 06:32:21 +01:00
from io import BufferedReader
2019-03-13 06:04:29 +01:00
from os.path import isfile
2019-03-13 05:43:26 +01:00
from hashlib import md5 as _md5
2019-03-17 02:57:48 +01:00
import traceback
2019-03-13 06:32:21 +01:00
def md5_str(data):
2019-03-17 02:57:48 +01:00
print(data)
2019-03-13 06:32:21 +01:00
if type(data) is str:
data = data.encode("ascii")
2019-03-17 02:57:48 +01:00
print(_md5(data).hexdigest().encode())
2019-03-13 06:32:21 +01:00
return _md5(data).hexdigest().encode()
def md5_file(file):
if type(file) is not BufferedReader:
raise Exception("File is not a BufferedReader")
if file.mode != "rb":
raise Exception("File is not in ReadBytes mode")
hash = _md5()
for chunk in iter(lambda: file.read(4096), b""):
hash.update(chunk)
2019-09-01 15:55:19 +02:00
return hash.hexdigest().encode()
2019-03-13 05:43:26 +01:00
2019-03-17 02:57:48 +01:00
def append_and_compress(data):
return lzma_compress(data + b"-12345|0|0|1337,")
2019-03-13 09:07:46 +01:00
def lzma_compress(data):
2019-03-17 02:57:48 +01:00
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
2019-03-13 09:07:46 +01:00
2019-03-13 05:43:26 +01:00
class Replay:
structure = {
2019-09-01 15:47:38 +02:00
"mode": Serializable(osuModes.STANDARD, TYPE_BYTE),
"osu_version": Serializable(20181216, TYPE_INT),
2019-09-01 15:47:38 +02:00
"beatmap_hash": Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING),
"player_name": Serializable(b"osu!", TYPE_STRING),
"score_hash": Serializable(b"d41d8cd98f00b204e9800998ecf8427e", TYPE_STRING),
2019-03-13 05:43:26 +01:00
"score_300s": Serializable(0, TYPE_USHORT),
"score_100s": Serializable(0, TYPE_USHORT),
"score_50s": Serializable(0, TYPE_USHORT),
"score_gekis": Serializable(0, TYPE_USHORT),
"score_katus": Serializable(0, TYPE_USHORT),
"score_miss": Serializable(0, TYPE_USHORT),
"score": Serializable(0, TYPE_INT),
"combo": Serializable(0, TYPE_USHORT),
"perfect": Serializable(True, TYPE_BOOL),
2019-03-13 05:43:26 +01:00
2019-09-01 15:47:38 +02:00
"mods": Serializable(osuMods.NOMOD, TYPE_INT),
2019-03-13 06:32:21 +01:00
"lifebar_graph": Serializable(b"0|1,", TYPE_STRING),
"timestamp": Serializable(0, TYPE_ULLONG),
2019-03-13 05:43:26 +01:00
"replay_data": Serializable(b"", TYPE_BYTESTREAM, post = append_and_compress),
2019-03-17 02:57:48 +01:00
"online_score_id": Serializable(0, TYPE_ULLONG)
}
2019-03-13 05:43:26 +01:00
def __init__(self, **kwargs):
allowed_kwargs = {
"mode": self.set_mode,
"mods": self.set_mods,
"osu_version": self.set_osu_version,
2019-03-13 11:13:55 +01:00
"beatmap_hash": self.set_beatmap_hash,
2019-03-13 05:43:26 +01:00
"player_name": self.set_player_name,
"replay_data": self.write
}
for k, v in kwargs.items():
if k in allowed_kwargs.keys():
allowed_kwargs[k](v) # Run callback func
2019-03-13 06:32:21 +01:00
2019-03-13 09:55:17 +01:00
# --------------------------------------------------------------------------
2019-03-13 05:43:26 +01:00
# Set validators -----------------------------------------------------------
2019-03-13 09:07:46 +01:00
def set_mode(self, mode):
if mode > 3 or mode < 0:
2019-03-13 05:43:26 +01:00
raise Exception("Invalid mode")
self.structure["mode"].value = mode
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def set_osu_version(self, osu_version):
if type(osu_version) is not int:
raise Exception("osu! version must be an int")
self.structure["osu_version"].value = osu_version
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def set_beatmap_hash(self, md5_hash):
2019-03-13 11:13:55 +01:00
if type(md5_hash) is bytes:
md5_hash = md5_hash.decode()
2019-03-13 05:43:26 +01:00
if len(md5_hash) != 32 or len([x for x in md5_hash if x in string.hexdigits]) != 32:
raise Exception("Invalid beatmap hash")
self.structure["beatmap_hash"].value = md5_hash.encode()
2019-03-13 06:32:21 +01:00
2019-03-13 09:55:17 +01:00
def set_beatmap_file(self, filepath):
2019-03-13 06:32:21 +01:00
if not isfile(filepath):
raise Exception("Beatmap file not found")
with open(filepath, "rb") as f:
self.structure["beatmap_hash"].value = md5_file(f)
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def set_player_name(self, player_name):
2019-03-17 02:57:48 +01:00
if type(player_name) is str:
player_name = player_name.encode()
self.structure["player_name"].value = player_name
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def set_score_hash(self, md5_hash):
if type(md5_hash) is bytes:
md5_hash = md5_hash.decode()
if len(md5_hash) != 32 or len([x for x in md5_hash if x in string.hexdigits]) != 32:
raise Exception("Invalid replay hash")
self.structure["score_hash"].value = md5_hash.encode()
2019-03-13 09:55:17 +01:00
def set_score(self, score = None, combo = None, s300 = None, s100 = None, s50 = None, sgekis = None, skatus = None, miss = None):
if type(score) is int: self.structure["score"].value = score
if type(combo) is int: self.structure["combo"].value = combo
if type(s300) is int: self.structure["score_300s"].value = s300
if type(s100) is int: self.structure["score_100s"].value = s100
if type(s50) is int: self.structure["score_50s"].value = s50
if type(sgekis) is int: self.structure["score_gekis"].value = sgekis
if type(skatus) is int: self.structure["score_katus"].value = skatus
if type(miss) is int: self.structure["score_miss"].value = miss
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def set_mods(self, mods):
if mods < 0 or mods > (1 << 30) - 1:
raise Exception("Mods are out of range")
self.structure["mods"].value = mods
2019-03-13 09:55:17 +01:00
def set_lifebar_graph(self, graph):
t_graph = type(graph)
if t_graph is list:
self.structure["lifebar_graph"].value = b"".join(graph)
2019-03-13 09:55:17 +01:00
elif t_graph is str:
self.structure["lifebar_graph"].value = graph.encode()
2019-03-13 09:55:17 +01:00
elif t_graph is bytes:
self.structure["lifebar_graph"].value = graph
2019-03-13 09:55:17 +01:00
else:
raise Exception("Invalid lifebar data")
def set_timestamp(self, timestamp, use_unix = True):
if use_unix:
timestamp += 62135599380000 # offset
timestamp *= 10 ** 4
self.structure["timestamp"].value = timestamp
2019-03-13 06:32:21 +01:00
2019-03-17 02:57:48 +01:00
def set_online_score_id(self, online_score_id):
self.structure["online_score_id"].value = online_score_id
2019-03-17 02:57:48 +01:00
2019-03-13 05:43:26 +01:00
# --------------------------------------------------------------------------
# Update variables ---------------------------------------------------------
def update_perfect(self):
m = [self.structure["score_100s"].value, self.structure["score_50s"].value, self.structure["score_katus"].value, self.structure["score_miss"].value]
self.structure["perfect"].value = sum(m) == 0
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def update_score_hash(self):
self.set_score_hash(
2019-03-17 02:57:48 +01:00
md5_str(b"%d%b%b%b%d%b" % (
self.structure["combo"].value, b"osu", self.structure["player_name"].value,
self.structure["beatmap_hash"].value, self.structure["score"].value, self.get_rank()
2019-03-13 05:43:26 +01:00
))
)
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
def update(self):
self.update_perfect()
self.update_score_hash()
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
# --------------------------------------------------------------------------
# Get / Helpers ------------------------------------------------------------
def get_hits(self):
return sum([self.structure["score_300s"].value, self.structure["score_100s"].value, self.structure["score_50s"].value])
2019-03-13 05:43:26 +01:00
def get_possible_hits(self):
return self.get_hits() + self.structure["score_miss"].value
2019-03-13 05:43:26 +01:00
2019-03-13 11:01:54 +01:00
def get_rank(self):
2019-03-13 05:43:26 +01:00
hits = self.get_possible_hits()
if hits == 0:
2019-03-13 11:01:54 +01:00
print("Can not calculate rank without any score data (Defaulting to Fail)")
return osuRanks.F
2019-03-13 05:43:26 +01:00
r300 = self.structure["score_300s"].value / hits
r50 = self.structure["score_50s"].value / hits
h = osuMods.any_enabled(self.structure["mods"].value, osuMods.HIDDEN | osuMods.FLASHLIGHT)
2019-03-13 05:43:26 +01:00
if r300 == 1:
return osuRanks.SSH if h else osuRanks.SS
if r300 > .9 and r50 <= .01 and self.structure["score_miss"].value == 0:
2019-03-13 05:43:26 +01:00
return osuRanks.SH if h else osuRanks.S
if r300 > .8 and self.structure["score_miss"].value == 0 or r300 > .9:
2019-03-13 05:43:26 +01:00
return osuRanks.A
if r300 > .7 and self.structure["score_miss"].value == 0 or r300 > .8:
2019-03-13 05:43:26 +01:00
return osuRanks.B
if r300 > .6:
return osuRanks.C
return osuRanks.D
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
# --------------------------------------------------------------------------
# Write replay data --------------------------------------------------------
def write(self, frame):
2019-03-13 10:59:06 +01:00
t_frame = type(frame)
if t_frame is list:
2019-03-13 05:43:26 +01:00
for _frame in frame: # (frames)
self.write(_frame)
return
2019-03-13 10:59:06 +01:00
if t_frame is ReplayFrame:
self.structure["replay_data"].value += bytes(frame)
2019-03-13 10:59:06 +01:00
elif t_frame is bytes:
self.structure["replay_data"].value += frame
2019-03-13 10:59:06 +01:00
elif t_frame is str:
self.structure["replay_data"].value += frame.encode()
2019-03-13 10:59:06 +01:00
else:
raise Exception("Invalid frame data")
2019-03-13 06:32:21 +01:00
2019-03-13 05:43:26 +01:00
# --------------------------------------------------------------------------
# IO replay data -----------------------------------------------------------
def save(self, filename):
self.update()
2019-03-13 09:07:46 +01:00
serializer = Serializer()
2019-09-01 15:47:38 +02:00
[ serializer.add( struc ) for struc in self.structure.values() ] # Add values to serializer
2019-03-13 05:43:26 +01:00
with open(filename, "wb") as f:
2019-03-13 09:07:46 +01:00
f.write( serializer.flush() )