-
Notifications
You must be signed in to change notification settings - Fork 1
/
psp_libdoc.py
executable file
·361 lines (270 loc) · 10.6 KB
/
psp_libdoc.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#! /usr/bin/env python3
import argparse
import hashlib
import itertools
import os
import re
import sys
from collections import namedtuple
from lxml import etree as ET
NIDEntry = namedtuple('NIDEntry', ['nid', 'name', 'prx', 'prxName', 'libraryName', 'libraryFlags'])
def compute_nid(name):
return hashlib.sha1(name.encode('ascii')).digest()[:4][::-1].hex().upper()
def loadPSPLibdoc(xmlFile):
tree = ET.parse(xmlFile)
root = tree.getroot()
entries = []
for prx in root.findall("PRXFILES/PRXFILE"):
prxFile = prx.find("PRX").text
prxName = prx.find("PRXNAME").text
for library in prx.findall("LIBRARIES/LIBRARY"):
libraryName = library.find("NAME").text
libraryFlags = library.find("FLAGS").text
for function in library.findall("FUNCTIONS/FUNCTION"):
functionNID = function.find("NID").text.upper().removeprefix('0X')
functionName = function.find("NAME").text
entries.append(NIDEntry(nid=functionNID, name=functionName, prx=prxFile,
prxName=prxName, libraryName=libraryName, libraryFlags=libraryFlags))
return entries
def updatePSPLibdoc(nidEntries, xmlFile):
xmlParser = ET.XMLParser(strip_cdata=False, remove_blank_text=True)
tree = ET.parse(xmlFile, xmlParser)
root = tree.getroot()
numTotalFunctions = 0
numUnkFunctions = 0
numUpdatedFunctions = 0
entries = {}
for entry in nidEntries:
entries[entry.nid] = entry
for library in root.findall("PRXFILES/PRXFILE/LIBRARIES/LIBRARY"):
libraryName = library.find("NAME").text
for function in library.findall("FUNCTIONS/FUNCTION"):
numTotalFunctions = numTotalFunctions + 1
functionNID = function.find("NID").text.upper().removeprefix('0X')
functionName = function.find("NAME").text
libDocNidNameUnk = functionName.upper().endswith(functionNID)
if libDocNidNameUnk:
numUnkFunctions = numUnkFunctions + 1
if functionNID in entries:
nidEntry = entries[functionNID]
dictNidNameUnk = nidEntry.name.upper().endswith(functionNID)
if libDocNidNameUnk and not dictNidNameUnk:
print("Updating {} -> {}".format(functionName, nidEntry.name))
function.find("NAME").text = nidEntry.name
numUpdatedFunctions = numUpdatedFunctions + 1
functionsKnown = numTotalFunctions - numUnkFunctions + numUpdatedFunctions
print("{:#04}/{:#04} unknown NIDs updated".format(numUpdatedFunctions, numUnkFunctions))
print("{:#04}/{:#04} total NIDs known".format(functionsKnown, numTotalFunctions))
if numTotalFunctions > 0:
print("{:#03}%".format(int(functionsKnown / numTotalFunctions * 100)))
else:
print("100%")
tree.write(xmlFile, encoding='utf-8', method="xml", xml_declaration=True, pretty_print=True)
def getNidForString(string):
sha1Hash = hashlib.sha1(bytes(string, 'utf-8'))
hashBytes = sha1Hash.digest()[0:4]
nid = hashBytes[::-1].hex().upper()
return nid
def loadPSPExportFile(exportFile):
with open(exportFile) as f:
lines = f.readlines()
entries = []
libraryName = " "
libraryFlags = " "
for line in lines:
if line.startswith("PSP_EXPORT_START"):
libraryEntry = line[line.find("(") + 1 : line.find(")")]
library, unk, flags = libraryEntry.split(',')
libraryName = library.strip()
libraryFlags = flags.strip()
elif line.startswith("PSP_EXPORT_FUNC_NID"):
nidNamePair = line[line.find("(") + 1 : line.find(")")]
functionName, functionNID = nidNamePair.split(',')
functionName = functionName.strip()
functionNID = functionNID.strip().upper().removeprefix('0X')
entries.append(NIDEntry(nid=functionNID, name=functionName, prx=" ",
prxName=" ", libraryName=libraryName, libraryFlags=libraryFlags))
elif line.startswith("PSP_EXPORT_FUNC_HASH"):
functionName = line[line.find("(") + 1 : line.find(")")].strip()
functionNID = getNidForString(functionName)
entries.append(NIDEntry(nid=functionNID, name=functionName, prx=" ",
prxName=" ", libraryName=libraryName, libraryFlags=libraryFlags))
elif line.startswith("PSP_EXPORT_END"):
libraryName = " "
libraryFlags = " "
return entries
def loadFunctionFile(xmlFile):
with open(xmlFile) as f:
it = itertools.chain("<root>", f, "</root>")
root = ET.fromstringlist(it)
entries = []
for function in root.findall("FUNC"):
functionNID = function.find("NID").text.upper().removeprefix('0X')
functionName =function.find("NAME").text
entries.append(NIDEntry(nid=functionNID, name=functionName, prx=" ",
prxName=" ", libraryName=" ", libraryFlags=" "))
return entries
def loadHLEFunctionFile(inputFile):
with open(inputFile) as f:
fileText = f.read()
entries = []
hleArrayStartRegex = re.compile(r'.*HLEFunction(.*)\[\]')
hleArrayEntryRegex = re.compile(r'{(.*)}')
hleArrayMatches = hleArrayStartRegex.finditer(fileText)
for match in hleArrayMatches:
libraryName = match.group(1).strip()
arrayStart = match.start();
arrayEnd = arrayStart + re.search('};', fileText[arrayStart:]).end()
hleArray = fileText[arrayStart:arrayEnd]
hleArrayEntryMatches = hleArrayEntryRegex.finditer(hleArray)
for match in hleArrayEntryMatches:
hleEntry = match.group(1).split(',')
functionNID = hleEntry[0].upper().removeprefix('0X')
functionName = hleEntry[2].strip()[1:-1]
entries.append(NIDEntry(nid=functionNID, name=functionName, prx=" ",
prxName=" ", libraryName=libraryName, libraryFlags=" "))
return entries
def exportNids(nidEntries, outFile):
with open(outFile, "w") as f:
for entry in nidEntries:
f.write("0x" + entry.nid + '\n')
def exportUnknownNids(nidEntries, outFile):
with open(outFile, "w") as f:
for entry in nidEntries:
functionNID = entry.nid
functionName = entry.name
libDocNidNameUnk = functionName.upper().endswith(functionNID)
if libDocNidNameUnk:
f.write("0x" + functionNID + '\n')
def exportFunctionNames(nidEntries, outFile):
with open(outFile, "w") as f:
for nidEntry in nidEntries:
f.write(nidEntry.name + '\n')
def exportKnownFunctionNames(nidEntries, outFile):
with open(outFile, "w") as f:
for nidEntry in nidEntries:
functionNID = nidEntry.nid
functionName = nidEntry.name
libDocNidNameUnk = functionName.upper().endswith(functionNID)
if not libDocNidNameUnk:
f.write(nidEntry.name + '\n')
def exportPSPLibdocCombined(nidEntries, outFile):
entries = sorted(nidEntries, key=lambda x: [x.prx, x.libraryName, int(x.nid, 16)])
root = ET.Element("PSPLIBDOC")
root.addprevious(ET.ProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="psplibdocdisplay.xsl" '))
prxfiles = ET.SubElement(root, "PRXFILES")
lastPrxFile = ""
lastLibrary = ""
for entry in entries:
if lastPrxFile != entry.prx:
prxfile = ET.SubElement(prxfiles, "PRXFILE")
prx = ET.SubElement(prxfile, "PRX")
prx.text = entry.prx
prxName = ET.SubElement(prxfile, "PRXNAME")
prxName.text = entry.prxName
libraries = ET.SubElement(prxfile, "LIBRARIES")
lastPrxFile = entry.prx
if lastLibrary != entry.libraryName:
library = ET.SubElement(libraries, "LIBRARY")
name = ET.SubElement(library, "NAME")
name.text = entry.libraryName
flags = ET.SubElement(library, "FLAGS")
flags.text = entry.libraryFlags
functions = ET.SubElement(library, "FUNCTIONS")
lastLibrary = entry.libraryName
function = ET.SubElement(functions, "FUNCTION")
nid = ET.SubElement(function, "NID")
nid.text = "0x" + entry.nid
name = ET.SubElement(function, "NAME")
name.text = entry.name
ET.ElementTree(root).write(outFile, encoding='utf-8', method="xml", xml_declaration=True, pretty_print=True)
def exportPSPLibdocModules(nidEntries, outFolder):
os.makedirs(outFolder)
prxDict = {}
for entry in nidEntries:
prxDict.setdefault(entry.prx, []).append(entry)
for key in prxDict.keys():
outfile = outFolder + "/" + key.split('.')[0] + ".xml"
exportPSPLibdocCombined(prxDict[key], outfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--libdoc',
required=False,
nargs='+',
type=str,
help='Load a PSP-Libdoc XML file.')
parser.add_argument('-e', '--export',
required=False,
nargs='+',
type=str,
help='Load a PSP-Export file.')
parser.add_argument('-f', '--func',
required=False,
nargs='+',
type=str,
help='Load function only XML file.')
parser.add_argument('-p', '--ppsspp',
required=False,
nargs='+',
type=str,
help='Load ppsspp source file (HLEFunction arrays)')
parser.add_argument('-u', '--updateLibdoc',
required=False,
type=str,
help='Update specified PSP-Libdoc XML file with loaded NID names.')
parser.add_argument('-n', '--exportNids',
required=False,
type=str,
help='Export all loaded NIDs to the specified file.')
parser.add_argument('-o', '--exportUnknownNids',
required=False,
type=str,
help='Export all loaded NIDs with unknown function name to the specified file.')
parser.add_argument('-d', '--exportFunctionNames',
required=False,
type=str,
help='Export all loaded function names to the specified file.')
parser.add_argument('-k', '--exportKnownFunctionNames',
required=False,
type=str,
help='Export all known loaded function names to the specified file.')
parser.add_argument('-c', '--writeLibdocCombined',
required=False,
type=str,
help='Write combined PSP-Libdoc XML file for all loaded PRX modules to the specified file.')
parser.add_argument('-s', '--writeLibdocSplit',
required=False,
type=str,
help='Write PSP-Libdoc XML file for each loaded PRX module to the specified folder.')
nidEntries = []
args = parser.parse_args(sys.argv[1:])
if(args.libdoc):
for libdoc in args.libdoc:
libdocEntries = loadPSPLibdoc(libdoc)
nidEntries.extend(libdocEntries)
if(args.export):
for export in args.export:
exportEntries = loadPSPExportFile(export)
nidEntries.extend(exportEntries)
if(args.func):
for func in args.func:
funcEntries = loadFunctionFile(func)
nidEntries.extend(funcEntries)
if(args.ppsspp):
for ppsspp in args.ppsspp:
ppssppEntries = loadHLEFunctionFile(ppsspp)
nidEntries.extend(ppssppEntries)
if(args.updateLibdoc):
updatePSPLibdoc(nidEntries, args.updateLibdoc)
if(args.exportNids):
exportNids(nidEntries, args.exportNids)
if(args.exportUnknownNids):
exportUnknownNids(nidEntries, args.exportUnknownNids)
if(args.exportFunctionNames):
exportFunctionNames(nidEntries, args.exportFunctionNames)
if(args.exportKnownFunctionNames):
exportKnownFunctionNames(nidEntries, args.exportKnownFunctionNames)
if(args.writeLibdocCombined):
exportPSPLibdocCombined(nidEntries, args.writeLibdocCombined)
if(args.writeLibdocSplit):
exportPSPLibdocModules(nidEntries, args.writeLibdocSplit)