-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert_sdk.py
executable file
·101 lines (76 loc) · 3.5 KB
/
convert_sdk.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
# NSMB Nitro SDK converter by TheGameratorT
# 30th October 2020
import re
import glob
import sys
# Headers to ignore the global to local header change
libHeaders = ['stdio.h', 'stdarg.h']
# Regular Expressions
regex_clz = re.compile('\s+asm\s+\{\s+clz\s+x,\s+x\}')
regex_include = re.compile('(#include)\s*(<)')
regex_pragma_1 = re.compile('(#pragma)\s*(warn_padding)')
regex_pragma_2 = re.compile('(#pragma)\s*(unused)')
regex_pragma_3 = re.compile('(#pragma)\s*(thumb)')
regex_multichar = re.compile('\'.{4}\'')
def isLibHeader(txt: str):
for x in libHeaders:
if x in txt:
return True
return False
for filename in glob.iglob('./include/**/*.h', recursive=True):
unix_filename = filename.replace('\\', '/')
print('Processing {0}'.format(unix_filename))
new_lines = []
with open(filename, 'r') as f:
text = ''.join(f.readlines())
# Paretheses warning supression
if(
unix_filename == './include/nitro/gx/gx.h' or
unix_filename == './include/nitro/gx/g3x.h'
):
text = '#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored "-Wparentheses"\n' + text + '\n#pragma GCC diagnostic pop'
if(unix_filename == './include/nitro.h'):
# Fix comment bug
text = text.replace('mi/*.h', 'mi/(...).h')
elif(
unix_filename == './include/nitro/gx/g2.h' or
unix_filename == './include/nitro/gx/gx.h' or
unix_filename == './include/nitro/gx/gx_bgcnt.h'
):
text = text.replace('*(volatile', '*(')
elif(unix_filename == './include/nitro/os/common/spinLock.h'):
text = text.replace('typedef volatile struct', 'typedef struct')
elif(unix_filename == './include/nitro/math/math.h'):
text = regex_clz.sub('\n asm("clz x, x");', text)
elif(unix_filename == './include/nitro/ctrdg/ARM9/ctrdg_backup.h'):
text = text.replace('(*maxtime)', '*maxtime')
text = text.replace('(*ctrdgi_fl_maxtime)', '*ctrdgi_fl_maxtime')
elif(unix_filename == './include/nitro/wfs/format.h'):
text = text.replace('(*sinit_init)', '*sinit_init')
text = text.replace('(*sinit_init_end)', '*sinit_init_end')
elif(unix_filename == './include/nnsys/inline.h'):
text = text.replace('E inline', 'E static inline')
for line in text.split('\n'):
# Convert lib header path to local header path
if isLibHeader(line):
newLine = line
else:
newLine = regex_include.sub('#include "', line)
newLine = newLine.replace('.h>', '.h"')
# Remove unknown pragmas
newLine = regex_pragma_1.sub('//#pragma warn_padding', newLine)
newLine = regex_pragma_2.sub('//#pragma unused', newLine)
newLine = regex_pragma_3.sub('//#pragma thumb', newLine)
# Multi-character constants
mCharFound = regex_multichar.search(newLine)
if mCharFound is not None:
str1 = newLine[mCharFound.start():mCharFound.end()]
str2 = str1[1:5]
encoded = str2.encode('ascii')
value = int.from_bytes(encoded, sys.byteorder)
hex_str = hex(value)
newLine = newLine.replace(str1, hex_str + ' //' + str1)
# print(newLine)
new_lines.append(newLine + '\n')
with open(filename, 'w') as f:
f.writelines(new_lines)