Skip to content

Commit

Permalink
Update th07-09 to allow pristine Kaitai generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rook committed Dec 25, 2023
1 parent 5e00879 commit 3cb5eb8
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 176 deletions.
12 changes: 0 additions & 12 deletions project/thscoreboard/replays/kaitai_parsers/th06.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ def __init__(self, _io, _parent=None, _root=None):
def _read(self):
self.file_header = Th06.FileHeader(self._io, self, self._root)

class Dummy(KaitaiStruct):
"""blank type."""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
pass


class FileHeader(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
Expand Down
61 changes: 25 additions & 36 deletions project/thscoreboard/replays/kaitai_parsers/th07.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ def _read(self):
self.file_header = Th07.FileHeader(self._io, self, self._root)
self.header = Th07.Header(self._io, self, self._root)

class Dummy(KaitaiStruct):
"""blank type."""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
pass


class FileHeader(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
Expand All @@ -50,7 +38,7 @@ def _read(self):
self.size = self._io.read_u4le()
self.stage_offsets = []
for i in range(7):
self.stage_offsets.append(self._io.read_u4le())
self.stage_offsets.append(Th07.StagePointer(self._io, self, self._root))

self.unknown_4 = []
for i in range(7):
Expand Down Expand Up @@ -80,6 +68,30 @@ def _read(self):
self.slowdown = self._io.read_f4le()


class StagePointer(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
self.offset = self._io.read_u4le()

@property
def body(self):
if hasattr(self, '_m_body'):
return self._m_body

if self.offset != 0:
_pos = self._io.pos()
self._io.seek(self.offset)
self._m_body = Th07.Stage(self._io, self, self._root)
self._io.seek(_pos)

return getattr(self, '_m_body', None)


class Stage(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
Expand All @@ -103,27 +115,4 @@ def _read(self):
self.unknown_4 = self._io.read_u1()


@property
def stages(self):
if hasattr(self, '_m_stages'):
return self._m_stages

_pos = self._io.pos()
self._raw__m_stages = []
self._m_stages = []
for i in range(7):
_on = self.file_header.stage_offsets[i]
if _on == 0:
self._raw__m_stages.append(self._io.read_bytes(40))
_io__raw__m_stages = KaitaiStream(BytesIO(self._raw__m_stages[i]))
self._m_stages.append(Th07.Dummy(_io__raw__m_stages, self, self._root))
else:
self._io.seek(self.file_header.stage_offsets[i])
self._raw__m_stages.append(self._io.read_bytes(40))
_io__raw__m_stages = KaitaiStream(BytesIO(self._raw__m_stages[i]))
self._m_stages.append(Th07.Stage(_io__raw__m_stages, self, self._root))

self._io.seek(_pos)
return getattr(self, '_m_stages', None)


56 changes: 25 additions & 31 deletions project/thscoreboard/replays/kaitai_parsers/th08.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ def _read(self):
self.file_header = Th08.FileHeader(self._io, self, self._root)
self.header = Th08.Header(self._io, self, self._root)

class Dummy(KaitaiStruct):
"""blank type."""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
pass


class FileHeader(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
Expand All @@ -51,7 +39,7 @@ def _read(self):
self.decomp_size = self._io.read_u4le()
self.stage_offsets = []
for i in range(9):
self.stage_offsets.append(self._io.read_u4le())
self.stage_offsets.append(Th08.StagePointer(self._io, self, self._root))

self.potential_stage_size = []
for i in range(9):
Expand Down Expand Up @@ -82,6 +70,30 @@ def _read(self):
self.slowdown = self._io.read_f4le()


class StagePointer(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
self.offset = self._io.read_u4le()

@property
def body(self):
if hasattr(self, '_m_body'):
return self._m_body

if self.offset != 0:
_pos = self._io.pos()
self._io.seek(self.offset)
self._m_body = Th08.Stage(self._io, self, self._root)
self._io.seek(_pos)

return getattr(self, '_m_body', None)


class Stage(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
Expand All @@ -103,22 +115,4 @@ def _read(self):
self.unknown_2 = self._io.read_u1()


@property
def stages(self):
if hasattr(self, '_m_stages'):
return self._m_stages

_pos = self._io.pos()
self._m_stages = []
for i in range(9):
_on = self.file_header.stage_offsets[i]
if _on == 0:
self._m_stages.append(Th08.Dummy(self._io, self, self._root))
else:
self._io.seek(self.file_header.stage_offsets[i])
self._m_stages.append(Th08.Stage(self._io, self, self._root))

self._io.seek(_pos)
return getattr(self, '_m_stages', None)


94 changes: 50 additions & 44 deletions project/thscoreboard/replays/kaitai_parsers/th09.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,57 @@ def _read(self):
self.file_header = Th09.FileHeader(self._io, self, self._root)
self.header = Th09.Header(self._io, self, self._root)

class Dummy(KaitaiStruct):
"""blank type."""
class Stage(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
pass
self.score = self._io.read_u4le()
self.pair = self._io.read_u2le()
self.shot = self._io.read_u1()
self.ai = self._io.read_bits_int_be(1) != 0
self._io.align_to_byte()
self.lives = self._io.read_u1()
self.unknown = self._io.read_u1()


class FileHeader(KaitaiStruct):
class StagePointer(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
self.magic = self._io.read_bytes(4)
if not self.magic == b"\x54\x39\x52\x50":
raise kaitaistruct.ValidationNotEqualError(b"\x54\x39\x52\x50", self.magic, self._io, u"/types/file_header/seq/0")
self.version = self._io.read_u2le()
self.unknown_1 = self._io.read_bytes(6)
self.comp_size = self._io.read_u4le()
self.unknown_2 = self._io.read_u4le()
self.key = self._io.read_u1()
self.unknown_3 = self._io.read_bytes(7)
self.decomp_size = self._io.read_u4le()
self.stage_offsets = []
for i in range(20):
self.stage_offsets.append(self._io.read_u4le())
self.offset = self._io.read_u4le()

self.unknown_offsets = []
for i in range(20):
self.unknown_offsets.append(self._io.read_u4le())
@property
def body(self):
if hasattr(self, '_m_body'):
return self._m_body

if self.offset != 0:
_pos = self._io.pos()
self._io.seek(self.offset)
self._m_body = Th09.Stage(self._io, self, self._root)
self._io.seek(_pos)

return getattr(self, '_m_body', None)


class Dummy(KaitaiStruct):
"""blank type."""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
pass


class Header(KaitaiStruct):
Expand All @@ -72,39 +85,32 @@ def _read(self):
self.difficulty = self._io.read_u1()


class Stage(KaitaiStruct):
class FileHeader(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()

def _read(self):
self.score = self._io.read_u4le()
self.pair = self._io.read_u2le()
self.shot = self._io.read_u1()
self.ai = self._io.read_bits_int_be(1) != 0
self._io.align_to_byte()
self.lives = self._io.read_u1()
self.unknown = self._io.read_u1()

self.magic = self._io.read_bytes(4)
if not self.magic == b"\x54\x39\x52\x50":
raise kaitaistruct.ValidationNotEqualError(b"\x54\x39\x52\x50", self.magic, self._io, u"/types/file_header/seq/0")
self.version = self._io.read_u2le()
self.unknown_1 = self._io.read_bytes(6)
self.comp_size = self._io.read_u4le()
self.unknown_2 = self._io.read_u4le()
self.key = self._io.read_u1()
self.unknown_3 = self._io.read_bytes(7)
self.decomp_size = self._io.read_u4le()
self.stage_offsets = []
for i in range(20):
self.stage_offsets.append(Th09.StagePointer(self._io, self, self._root))

@property
def stages(self):
if hasattr(self, '_m_stages'):
return self._m_stages
self.unknown_offsets = []
for i in range(20):
self.unknown_offsets.append(self._io.read_u4le())

_pos = self._io.pos()
self._m_stages = []
for i in range(20):
_on = self.file_header.stage_offsets[i]
if _on == 0:
self._m_stages.append(Th09.Dummy(self._io, self, self._root))
else:
self._io.seek(self.file_header.stage_offsets[i])
self._m_stages.append(Th09.Stage(self._io, self, self._root))

self._io.seek(_pos)
return getattr(self, '_m_stages', None)


29 changes: 16 additions & 13 deletions project/thscoreboard/replays/replay_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def _Parse07(rep_raw):
rep_stages = []

enumerated_non_dummy_stages = [
(i, _stage)
for i, _stage in enumerate(replay.stages)
if replay.file_header.stage_offsets[i] != 0
(i, _pointer.body)
for i, _pointer in enumerate(replay.file_header.stage_offsets)
if _pointer.body
]

def is_phantasm(difficulty_code: int) -> bool:
Expand Down Expand Up @@ -282,11 +282,13 @@ def _Parse08(rep_raw):

# TH08 stores stage data values from the start of the stage but score from the end
route = None

enumerated_non_dummy_stages = [
(i, _stage)
for i, _stage in enumerate(replay.stages)
if replay.file_header.stage_offsets[i] != 0
(i, _pointer.body)
for i, _pointer in enumerate(replay.file_header.stage_offsets)
if _pointer.body
]

for (i, current_stage), (j, next_stage) in zip(
enumerated_non_dummy_stages, enumerated_non_dummy_stages[1:] + [(None, None)]
):
Expand Down Expand Up @@ -337,6 +339,7 @@ def _Parse09(rep_raw):
replay = th09.Th09.from_bytes(
bytearray(rep_raw[0:24]) + comp_data[0:168] + td.unlzss(comp_data[168:])
)
stage_pointers = replay.file_header.stage_offsets
shots = [
"Reimu",
"Marisa",
Expand All @@ -362,14 +365,14 @@ def _Parse09(rep_raw):
r_type = game_ids.ReplayTypes.FULL_GAME

highest_stage = 0
if replay.file_header.stage_offsets[9] == 0:
if not stage_pointers[9].body:
# story mode
# collect start-of-stage data
for i in range(9):
if replay.file_header.stage_offsets[i] != 0:
if stage_pointers[i].body:
# real stage
p1 = replay.stages[i]
p2 = replay.stages[i + 10]
p1 = stage_pointers[i].body
p2 = stage_pointers[i + 10].body

s = ReplayStage()
s.stage = i + 1
Expand All @@ -385,7 +388,7 @@ def _Parse09(rep_raw):
rep_stages.append(s)

# fill in replayinfo
p1 = replay.stages[highest_stage]
p1 = stage_pointers[highest_stage].body
r_shot = shots[p1.shot]
r_score = p1.score * 10

Expand All @@ -406,8 +409,8 @@ def _Parse09(rep_raw):

else:
# vs mode
p1 = replay.stages[9]
p2 = replay.stages[19]
p1 = stage_pointers[9].body
p2 = stage_pointers[19].body

r_shot = shots[p1.shot]
r_score = p1.score * 10
Expand Down
Loading

0 comments on commit 3cb5eb8

Please sign in to comment.