Skip to content

Commit 9a49fc8

Browse files
committed
Start cleaning up the GlobalParameters structure:
- Abstract the file number section into its own class, rather than a method. This is more consistent with how I am using structures elsewhere. - Add datum assertions.
1 parent add70e9 commit 9a49fc8

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

src/MediaStation/Context.py

+35-23
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,47 @@ def __init__(self, stream):
4848
self.file_number: int = Datum(stream).d
4949

5050
# READ THE SECTIONS.
51-
section_type: int = Datum(stream).d
51+
section_type: int = Datum(stream, Datum.Type.UINT16_1).d
5252
if section_type != GlobalParameters.SectionType.EMPTY:
5353
if section_type == GlobalParameters.SectionType.NAME:
54-
assert_equal(Datum(stream).d, self.file_number)
55-
self.name = Datum(stream).d
56-
assert_equal(Datum(stream).d, 0x0000)
54+
repeated_file_number = Datum(stream, Datum.Type.UINT16_1).d
55+
assert_equal(repeated_file_number, self.file_number)
56+
self.name = Datum(stream, Datum.Type.STRING).d
57+
unk1 = Datum(stream, Datum.Type.UINT16_1).d # Always 0x0000
5758

5859
elif section_type == GlobalParameters.SectionType.FILE_NUMBER:
59-
self.read_file_number_section(stream)
60+
unk1 = UnknownFileNumberSection(stream)
61+
assert_equal(unk1.file_number, self.file_number)
6062

6163
else:
6264
# TODO: Document what this stuff is. My original code had zero documentation.
63-
section_type = Datum(stream)
65+
section_type = Datum(stream, Datum.Type.UINT16_1)
6466
while section_type.d != GlobalParameters.SectionType.NULL:
6567
assert_equal(section_type.d, self.file_number, "file ID")
6668

67-
id = Datum(stream)
68-
self.entries.update({id.d: self.entity(Datum(stream), stream)})
69+
id = Datum(stream).d
70+
entity = self.entity(Datum(stream), stream)
71+
self.entries.update({id: entity})
6972

7073
check = Datum(stream)
7174
if check.d != GlobalParameters.SectionType.EMPTY:
7275
break
7376

74-
section_type = Datum(stream)
77+
section_type = Datum(stream, Datum.Type.UINT16_1)
7578

7679
if check.d == GlobalParameters.SectionType.FILE_NUMBER:
77-
self.read_file_number_section(stream)
80+
unk1 = UnknownFileNumberSection(stream)
81+
assert_equal(unk1.file_number, self.file_number)
7882

7983
# READ THE CONTEXT-GLOBAL BYTECODE.
8084
# TODO: Does this run when the context is first loaded?
8185
if global_variables.version.is_first_generation_engine:
82-
section_type = Datum(stream)
86+
section_type = Datum(stream, Datum.Type.UINT16_1)
8387
self.init = []
8488
while section_type.d == GlobalParameters.SectionType.BYTECODE:
8589
self.init.append(Script(stream, in_independent_asset_chunk = False))
86-
section_type = Datum(stream)
90+
section_type = Datum(stream, Datum.Type.UINT16_1)
91+
8792

8893
# TODO: Document what this stuff is. My original code had zero documentation.
8994
def entity(self, section_type, stream):
@@ -100,28 +105,35 @@ def entity(self, section_type, stream):
100105
string = stream.read(size).decode('latin-1')
101106
entries.append(string)
102107

103-
else:
108+
# TODO: It looks like the only section types are
109+
# - 0x0005
110+
# - 0x0002
111+
# - 0x0003?
112+
# - 0x0001
113+
else: # literal
104114
entry = Datum(stream).d
105115
entries.append(entry)
106116

107117
return {"section_type": section_type.d, "entries": entries}
108118

109-
## I don't know what this structure is, but it's in every old-style game.
110-
## The fields aside from the file numbers are constant.
111-
## \param[in] stream - A binary stream that supports the read method.
112-
def read_file_number_section(self, stream):
119+
## I don't know what this structure is, but it's in every old-style game.
120+
## The fields aside from the file numbers are constant.
121+
## \param[in] stream - A binary stream that supports the read method.
122+
class UnknownFileNumberSection:
123+
def __init__(self, stream):
113124
# VERIFY THE FILE NUMBER.
114-
repeated_file_number = Datum(stream).d
115-
assert_equal(repeated_file_number, self.file_number)
125+
self.file_number = Datum(stream, Datum.Type.UINT16_1).d
116126
# TODO: Figure out what this is.
117-
unk1 = Datum(stream).d # This seems to always be 0x0001.
127+
unk1 = Datum(stream, Datum.Type.UINT16_1).d # This seems to always be 0x0001.
118128

119129
# VERIFY THE FILE NUMBER.
120-
repeated_file_number = Datum(stream).d
130+
repeated_file_number = Datum(stream, Datum.Type.UINT16_1).d
121131
assert_equal(repeated_file_number, self.file_number)
122132
# TODO: Figure out what these are.
123-
unk2 = Datum(stream).d # This seems to always be 0x0022.
124-
unk3 = Datum(stream).d
133+
unk2 = Datum(stream, Datum.Type.UINT16_1).d # This seems to always be 0x0022.
134+
unk3 = Datum(stream, Datum.Type.UINT16_1).d # Is this always zero?
135+
if unk3 != 0:
136+
raise ValueError('Not zero!')
125137

126138
## A "context" is the logical entity serialized in each CXT data file.
127139
## Subfile 0 of this file always contains the header sections for the context.

0 commit comments

Comments
 (0)