-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from bemxio/dumping
dumping features for all gamemodes
- Loading branch information
Showing
6 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import lzma | ||
import struct | ||
|
||
from osrparse.utils import (ReplayEventOsu, ReplayEventTaiko, ReplayEventCatch, | ||
ReplayEventMania) | ||
|
||
def pack_byte(data: int): | ||
return struct.pack("<B", data) | ||
|
||
def pack_short(data: int): | ||
return struct.pack("<H", data) | ||
|
||
def pack_int(data: int): | ||
return struct.pack("<I", data) | ||
|
||
def pack_long(data: int): | ||
return struct.pack("<Q", data) | ||
|
||
def pack_ULEB128(data): | ||
# taken from https://github.com/mohanson/leb128 | ||
r, i = [], len(data) | ||
|
||
while True: | ||
byte = i & 0x7f | ||
i = i >> 7 | ||
|
||
if (i == 0 and byte & 0x40 == 0) or (i == -1 and byte & 0x40 != 0): | ||
r.append(byte) | ||
return b"".join(map(pack_byte, r)) | ||
|
||
r.append(0x80 | byte) | ||
|
||
def pack_string(data: str): | ||
if data: | ||
return pack_byte(11) + pack_ULEB128(data) + data.encode("utf-8") | ||
return pack_byte(11) + pack_byte(0) | ||
|
||
def dump_timestamp(replay): | ||
# windows ticks starts at year 0001, in contrast to unix time (1970). | ||
# 62135596800 is the number of seconds between these two years and is added | ||
# to account for this difference. | ||
# The factor of 10000000 converts seconds to ticks. | ||
ticks = (62135596800 + replay.timestamp.timestamp()) * 10000000 | ||
ticks = int(ticks) | ||
return pack_long(ticks) | ||
|
||
|
||
def dump_replay_data(replay): | ||
replay_data = "" | ||
for event in replay.play_data: | ||
if isinstance(event, ReplayEventOsu): | ||
replay_data += f"{event.time_delta}|{event.x}|{event.y}|{event.keys.value}," | ||
elif isinstance(event, ReplayEventTaiko): | ||
replay_data += f"{event.time_delta}|{event.x}|0|{event.keys.value}," | ||
elif isinstance(event, ReplayEventCatch): | ||
replay_data += f"{event.time_delta}|{event.x}|0|{int(event.dashing)}," | ||
elif isinstance(event, ReplayEventMania): | ||
replay_data += f"{event.time_delta}|{event.keys}|0|0," | ||
|
||
filters = [{"id": lzma.FILTER_LZMA1, "dict_size": 1 << 21, "mode": lzma.MODE_FAST}] | ||
compressed = lzma.compress(replay_data.encode("ascii"), format=lzma.FORMAT_ALONE, filters=filters) | ||
|
||
return pack_int(len(compressed)) + compressed | ||
|
||
|
||
def dump_replay(replay): | ||
data = b"" | ||
|
||
data += pack_byte(replay.game_mode.value) | ||
data += pack_int(replay.game_version) | ||
data += pack_string(replay.beatmap_hash) | ||
|
||
data += pack_string(replay.player_name) | ||
data += pack_string(replay.replay_hash) | ||
|
||
data += pack_short(replay.number_300s) | ||
data += pack_short(replay.number_100s) | ||
data += pack_short(replay.number_50s) | ||
data += pack_short(replay.gekis) | ||
data += pack_short(replay.katus) | ||
data += pack_short(replay.misses) | ||
|
||
data += pack_int(replay.score) | ||
data += pack_short(replay.max_combo) | ||
data += pack_byte(replay.is_perfect_combo) | ||
|
||
data += pack_int(replay.mod_combination.value) | ||
data += pack_string(replay.life_bar_graph) | ||
data += dump_timestamp(replay) | ||
|
||
data += dump_replay_data(replay) | ||
data += pack_long(replay.replay_id) | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pathlib import Path | ||
from unittest import TestCase | ||
from tempfile import TemporaryDirectory | ||
|
||
from osrparse import parse_replay_file | ||
|
||
|
||
RES = Path(__file__).parent / "resources" | ||
|
||
|
||
class TestDumping(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.replay = parse_replay_file(RES / "replay.osr") | ||
|
||
def test_dumping(self): | ||
with TemporaryDirectory() as tempdir: | ||
r2_path = Path(tempdir) / "dumped.osr" | ||
self.replay.dump(r2_path) | ||
r2 = parse_replay_file(r2_path) | ||
|
||
# `replay_length` is intentionally not tested for equality here, as the | ||
# length of the compressed replay data may change after dumping due to | ||
# varying lzma settings. | ||
attrs = ["game_mode", "game_version", "beatmap_hash", "player_name", | ||
"replay_hash", "number_300s", "number_100s", "number_50s", "gekis", | ||
"katus", "misses", "score", "max_combo", "is_perfect_combo", | ||
"mod_combination", "life_bar_graph", "timestamp", "play_data", | ||
"replay_id"] | ||
for attr in attrs: | ||
self.assertEqual(getattr(self.replay, attr), getattr(r2, attr), | ||
f"{attr} is wrong") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters