-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.py
149 lines (125 loc) · 4.97 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class MessageGroup:
def __init__(self, group_index, rom):
self.group_index = group_index
self.rom = rom
self.read()
def read(self):
# We can determine the number of messages in this message group by reading the offset of the first message string in this group relative to the start of the group, and dividing by 4.
# The reason this works is that the start of each group is a list of each message's offset (each is 4 bytes), and the first message's string comes right after the last offset.
group_offset = self.rom.read_u32(0x089B1D90 + self.group_index*4)
first_message_offset = self.rom.read_u32(0x089B1D90 + group_offset)
self.num_messages = first_message_offset//4
class Message:
def __init__(self, message_id, rom):
self.message_id = message_id
self.rom = rom
self.read()
def read(self):
group_index = (self.message_id & 0xFF00) >> 8
msg_index = (self.message_id & 0x00FF)
group_offset = self.rom.read_u32(0x089B1D90 + group_index*4)
message_offset = self.rom.read_u32(0x089B1D90 + group_offset + msg_index*4)
self.string_ptr = 0x089B1D90 + group_offset + message_offset
self.read_string()
def read_string(self):
#print("%04X %s" % (self.message_id, self.string))
self.string = ""
curr_char_ptr = self.string_ptr
max_address = self.rom.max_offset()
while curr_char_ptr < max_address:
byte = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
if byte == 0x00:
break
elif byte == 0x01:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x02:
color_index = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Color %01X]" % color_index
elif byte == 0x03:
sfx_id = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
sfx_id <<= 8
sfx_id |= self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Sound %04X]" % sfx_id
elif byte == 0x04:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
if arg == 0x10:
arg2 = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string + "[%02X %02X %02X]" % (byte, arg, arg2)
else:
self.string + "[%02X %02X]" % (byte, arg)
elif byte == 0x05:
other_message_id = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
if other_message_id == 0xFF:
self.string += "[Choice %02X]" % other_message_id
else:
other_message_id <<= 8
other_message_id |= self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Choice %04X]" % other_message_id
elif byte == 0x06:
variable_type = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
if variable_type == 0:
self.string += "[Player]"
elif variable_type == 5:
other_message_id = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
other_message_id <<= 8
other_message_id |= self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Variable %02X %04X]" % (variable_type, other_message_id)
else:
self.string += "[Variable %02X]" % variable_type
elif byte == 0x07:
other_message_id = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
other_message_id <<= 8
other_message_id |= self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Message %04X]" % other_message_id
elif byte == 0x08:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x09:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x0A:
self.string += "\\n\n"
elif byte == 0x0B:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x0C:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Button %01X]" % arg
elif byte == 0x0D:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x0E:
arg = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[%02X %02X]" % (byte, arg)
elif byte == 0x0F:
symbol_index = self.rom.read_u8(curr_char_ptr)
curr_char_ptr += 1
self.string += "[Symbol %02X]" % symbol_index
else:
char = chr(byte)
if char == "[":
char = "\\["
if char == "]":
char = "\\]"
self.string += char