-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocess.py
More file actions
241 lines (215 loc) · 9.55 KB
/
Copy pathprocess.py
File metadata and controls
241 lines (215 loc) · 9.55 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import sys
import typing
class Parser:
def __init__(self):
self.child: Parser = None
def parseCommand(self, line: str) -> dict:
commentStart = line.find("//")
comment = None
if commentStart != -1:
comment = line[commentStart + 2:].strip()
line = line[:commentStart]
if not line.strip().startswith("/") or not line.strip().endswith("/"):
return None
result = {}
parts = line.split("/")
if (len(parts) < 2):
return None
result["preamble"] = parts[0]
result["command"] = parts[1].lower()
if comment is not None:
result["comment"] = comment
noncolon = 0
for part in parts[2:]:
if len(part.strip()) == 0:
continue
if "@" in part:
key, value = part.split("@", 1)
result[key.lower()] = value
else:
result[str(noncolon)] = part
noncolon += 1
return result
class Line(Parser):
def __init__(self, line: str):
super().__init__()
self.line = line
def repr(self):
return self.line
def write(self, o: typing.TextIO):
o.write(self.repr())
class ClassParser(Parser):
class Member:
def __init__(self, parent, command: dict, isunknown: bool = False):
if not "name" in command and not "0" in command:
raise ValueError("NAME parameter is mandatory")
if not "size" in command and not "1" in command:
raise ValueError("SIZE parameter is mandatory")
self.size = int(command.get("size", command.get("1")), base=0)
self.offset = int(command.get("offset", command.get("2", "-1")), base=0)
self.name = command.get("name", command.get("0"))
self.unknown = isunknown
self.preamble = command["preamble"]
self.comment = " " + command.get("comment", "")
self.parent = parent
self.reprPadding = None
self.isGap = command.get("gap", False)
if len(self.comment) == 1: self.comment = ""
def repr(self, withComment: bool):
name = self.name
if self.unknown:
name = self.name + " unk_0x{:X}".format(self.offset)
if (withComment and not self.isGap):
if self.reprPadding is None:
maxLen = self.parent.getLongestElementReprLen()
while (maxLen % 4 != 0): maxLen += 1
self.reprPadding = " " * (maxLen - (len(self.preamble) + len(name) + 1))
return "{}{};{}// (O:0x{:X},S:0x{:X}){}\n".format(self.preamble, name, self.reprPadding, self.offset, self.size, self.comment)
else:
return "{}{};\n".format(self.preamble, name)
def write(self, o: typing.TextIO):
o.write(self.repr(True))
def __init__(self, command: dict, isStruct: bool):
super().__init__()
if not "name" in command:
raise ValueError("NAME parameter is mandatory")
if not "size" in command:
raise ValueError("SIZE parameter is mandatory")
self.preamble = command["preamble"]
self.name = command["name"]
self.size = int(command["size"], base=0)
self.base_size = int(command.get("bsize", "0"), base=0)
self.base = command.get("base", None)
self.hasvtable = command.get("vtable", "False").lower() == "true"
self.sizeof = command.get("sizeof")
self.template = command.get("template")
self.elements = []
self.maxnamelen = None
if self.template is not None:
self.elements.append(Line("{}{}\n".format(self.preamble, self.template)))
if self.base is not None:
self.elements.append(Line("{}{} {} : public {}\n".format(self.preamble, "struct" if isStruct else "class", self.name, self.base)))
else:
self.elements.append(Line("{}{} {}\n".format(self.preamble, "struct" if isStruct else "class", self.name)))
self.elements.append(Line("{}{{\n".format(self.preamble)))
def processLine(self, line: str):
res = True
if self.child is not None:
if not self.child.processLine(line):
self.child = None
else:
command = self.parseCommand(line)
if command is None:
self.elements.append(Line(line))
else:
c = command["command"]
if c == "m" or c == "u":
self.elements.append(ClassParser.Member(self, command, c == "u"))
elif c == "end":
res = False
elif c == "start_class" or c == "start_struct":
self.child = ClassParser(command, c == "start_struct")
self.elements.append(self.child)
else:
raise ValueError("Unknown command: {}".format(c))
return res
def getLongestElementReprLen(self):
if self.maxnamelen is None:
maxlen = len(self.preamble) + 32
for el in self.elements:
if type(el) is ClassParser.Member:
l = len(el.repr(False))
if l > maxlen: maxlen = l
self.maxnamelen = maxlen
return self.maxnamelen
def _calcElements(self):
currOffset = self.base_size
if self.base is None and self.hasvtable:
currOffset += 4
i = 0
lastMember = None
while i < len(self.elements):
currEl = self.elements[i]
if type(currEl) is ClassParser.Member:
lastMember = currEl
if currEl.offset == -1:
currEl.offset = currOffset
if currOffset > currEl.offset:
raise ValueError("Current offset (0x{:X}) greater than current element offset.\n{}".format(currOffset, currEl.repr(False)))
elif currOffset < currEl.offset:
command = {}
command["preamble"] = currEl.preamble
command["name"] = "u8 gap_0x{:X}[0x{:X}]".format(currOffset, currEl.offset - currOffset)
command["offset"] = str(currOffset)
command["size"] = str(currEl.offset - currOffset)
command["gap"] = True
padMember = ClassParser.Member(self, command)
self.elements = self.elements[:i] + [padMember] + self.elements[i:]
i += 1
currOffset += padMember.size
currOffset += currEl.size
if currOffset > self.size:
raise ValueError("Too many members for defined class size")
i += 1
if currOffset < self.size:
preamble = self.preamble + " "
if lastMember is not None:
preamble = lastMember.preamble
command = {}
command["preamble"] = preamble
command["name"] = "u8 gap_0x{:X}[0x{:X}]".format(currOffset, self.size - currOffset)
command["offset"] = str(currOffset)
command["size"] = str(self.size - currOffset)
command["gap"] = True
padMember = ClassParser.Member(self, command)
self.elements.append(padMember)
self.elements.append(Line("{}}};\n".format(self.preamble)))
self.elements.append(Line("{}static_assert(sizeof({}) == 0x{:X});\n".format(self.preamble, self.sizeof if self.sizeof is not None else self.name, self.size)))
def write(self, o: typing.TextIO):
self._calcElements()
for el in self.elements:
el.write(o)
class FileParser(Parser):
def __init__(self, input: typing.TextIO, output: typing.TextIO):
super().__init__()
self.input = input
self.output = output
self.elements = []
def processLine(self, line: str):
res = True
if self.child is not None:
if not self.child.processLine(line):
self.child = None
else:
command = self.parseCommand(line)
if command is None:
self.elements.append(Line(line))
else:
c = command["command"]
if c == "start_class" or c == "start_struct":
self.child = ClassParser(command, c == "start_struct")
self.elements.append(self.child)
else:
raise ValueError("Unknown command: {}".format(c))
return res
def write(self, o: typing.TextIO):
for el in self.elements:
el.write(o)
def processFile(self):
self.elements.append(Line("/*******************************************************/\n"))
self.elements.append(Line("/* This file has been generated by the MK7-Memory tool */\n"))
self.elements.append(Line("/* !!! Do not edit this file manually !!! */\n"))
self.elements.append(Line("/*******************************************************/\n\n"))
for line in self.input:
self.processLine(line)
self.write(self.output)
def main():
if len(sys.argv) != 3:
print("Usage: {} (inputFile) (outputFile)".format(sys.argv[0]))
return
print("Processing {}...".format(sys.argv[1]))
with open(sys.argv[2], "w", encoding="UTF-8") as o:
with open(sys.argv[1], "r", encoding="UTF-8") as i:
FileParser(i, o).processFile()
if __name__ == "__main__":
sys.exit(main())