hash of beatmap file

This commit is contained in:
Emily 2019-03-13 06:32:21 +01:00
parent a94baeac62
commit fddb66fb86

View File

@ -7,11 +7,26 @@ from .helpers import osuMods
from .helpers import osuRanks from .helpers import osuRanks
from .helpers import typeSerializer from .helpers import typeSerializer
from io import BufferedReader
from os.path import isfile from os.path import isfile
from hashlib import md5 as _md5 from hashlib import md5 as _md5
def md5(str): def md5_str(data):
return _md5(str.encode("ascii")).hexdigest().encode() if type(data) is str:
data = data.encode("ascii")
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)
return hash.hexdigest()
class Replay: class Replay:
mode = osuModes.STANDARD # Byte mode = osuModes.STANDARD # Byte
@ -68,6 +83,12 @@ class Replay:
raise Exception("Invalid beatmap hash") raise Exception("Invalid beatmap hash")
self.beatmap_hash = md5_hash self.beatmap_hash = md5_hash
def set_beatmap(self, filepath):
if not isfile(filepath):
raise Exception("Beatmap file not found")
with open(filepath, "rb") as f:
self.beatmap_hash = md5_file(f)
def set_player_name(self, player_name): def set_player_name(self, player_name):
self.player_name = player_name self.player_name = player_name
@ -92,7 +113,7 @@ class Replay:
def update_score_hash(self): def update_score_hash(self):
self.set_score_hash( self.set_score_hash(
md5("%d%s%s%s%d%d" % ( md5_str("%d%s%s%s%d%d" % (
self.combo, "osu", self.player_name, self.combo, "osu", self.player_name,
self.beatmap_hash, self.score, self.get_rank() self.beatmap_hash, self.score, self.get_rank()
)) ))