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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
#!/usr/bin/env python3
import argparse
import json
import csv
import os
import re
parser = argparse.ArgumentParser(description="Computes current progress throughout the whole project.")
parser.add_argument("format", nargs="?", default="text", choices=["text", "verbose", "totalBadge", "gameBadge", "mainBadge", "endingBadge", "racingBadge", "audioBadge", "osBadge", "bytesToDecompile", "globalAsmFuncs", "m2cFuncs", "nonmatchingFuncs"])
args = parser.parse_args()
NON_MATCHING_PATTERN = r"#ifdef\s+NON_MATCHING.*?#pragma\s+GLOBAL_ASM\s*\(\s*\"(.*?)\"\s*\).*?#endif"
MIPS_TO_C_FUNC_COUNT_PATTERN = r"#ifdef\s+MIPS_TO_C.*?GLOBAL_ASM\s*\(\s*\"(.*?)\"\s*\).*?#endif"
NON_MATCHING_FUNC_COUNT_PATTERN = r"#ifdef\s+NON_MATCHING.*?GLOBAL_ASM\s*\(\s*\"(.*?)\"\s*\).*?#endif"
GLOBAL_ASM_FUNC_COUNT_PATTERN = r"GLOBAL_ASM\s*\(\s*\"(.*?)\"\s*\)."
def GetNonMatchingFunctions(files):
functions = []
for file in files:
with open(file) as f:
functions += re.findall(NON_MATCHING_PATTERN, f.read(), re.DOTALL)
return functions
def CountMipsToCFunctions(files):
functions = []
for file in files:
with open(file) as f:
functions += re.findall(MIPS_TO_C_FUNC_COUNT_PATTERN, f.read(), re.DOTALL)
return functions
def CountNonMatchingFunctions(files):
functions = []
for file in files:
with open(file) as f:
functions += re.findall(NON_MATCHING_FUNC_COUNT_PATTERN, f.read(), re.DOTALL)
return functions
def CountGlobalAsmFunctions(files):
functions = []
for file in files:
with open(file) as f:
functions += re.findall(GLOBAL_ASM_FUNC_COUNT_PATTERN, f.read(), re.DOTALL)
return functions
C_FUNCTION_PATERN_REGEX = r'^(?<!static\s)(?:(\/[*][*!][*]*\n(?:[^/]*\n)+?\s*[*]\/\n)(?:\s*)*?)?(?:\s*UNUSED\s+)?([^\s]+)\s(?:\s|[*])*?([0-9A-Za-z_]+)\s*[(][^)]*[)]\s*{'
def GetCFunctions(files):
functions = []
for file in files:
with open(file) as f:
source_code = f.read()
matches = re.finditer(C_FUNCTION_PATERN_REGEX, source_code, re.MULTILINE)
for match in matches:
function_name = match.group(3)
functions.append((file, function_name))
return functions
def ReadAllLines(fileName):
lineList = list()
with open(fileName) as f:
lineList = f.readlines()
return lineList
def GetFiles(path, ext):
files = []
for r, d, f in os.walk(path):
for file in f:
if file.endswith(ext):
files.append(os.path.join(r, file))
return files
def GetFilesBlackList(path, ext, blacklist=[]):
files = []
for r, d, f in os.walk(path):
d[:] = [dir for dir in d if dir not in blacklist]
for file in f:
if file.endswith(ext):
files.append(os.path.join(r, file))
return files
def GetFilesWhiteList(path, ext, whitelist=[]):
files = []
for root, dirs, filenames in os.walk(path):
if root == path or any(subdir in root for subdir in whitelist):
for filename in filenames:
if filename.endswith(ext):
files.append(os.path.join(root, filename))
return files
gameExclusiveDirs=["audio", "os", "ending", "racing"]
nonMatchingFunctions = GetNonMatchingFunctions(GetFiles("src", ".c"))
TotalMipsToCFunctions = len(CountMipsToCFunctions(GetFiles("src", ".c")))
TotalNonMatchingFunctions = len(CountNonMatchingFunctions(GetFiles("src", ".c")))
TotalGlobalAsmFunctions = len(CountGlobalAsmFunctions(GetFiles("src", ".c")))
TotalCFunctions = len(GetCFunctions(GetFilesWhiteList("src", ".c", gameExclusiveDirs)))
TotalCFunctions -= TotalGlobalAsmFunctions + (TotalGlobalAsmFunctions - TotalNonMatchingFunctions - TotalMipsToCFunctions)
def GetNonMatchingSize(path):
size = 0
if (path == "main"):
size += getDataBlackList("asm/non_matchings", gameExclusiveDirs)
elif (path == "racing"):
size += getData("asm/non_matchings/racing")
elif (path == "ending"):
size += getData("asm/non_matchings/ending")
elif (path == "os"):
size += getData("asm/non_matchings/os")
elif (path == "audio"):
size += getData("asm/non_matchings/audio")
else: size = getData(path)
return size
def getData(path):
size = 0
asmFiles = GetFiles(path, ".s")
for asmFilePath in asmFiles:
if asmFilePath not in nonMatchingFunctions:
asmLines = ReadAllLines(asmFilePath)
for asmLine in asmLines:
if (asmLine.startswith("/*")):
size += 4
return size
def getDataBlackList(path, bl=[]):
size = 0
asmFiles = GetFilesBlackList(path, ".s", bl)
for asmFilePath in asmFiles:
if asmFilePath not in nonMatchingFunctions:
asmLines = ReadAllLines(asmFilePath)
for asmLine in asmLines:
if (asmLine.startswith("/*")):
size += 4
return size
mapFile = ReadAllLines("build/us/mk64.us.map")
src = 0
segMain = 0
segEnding = 0
segRacing = 0
audio = 0
libultra = 0
for line in mapFile:
lineSplit = list(filter(None, line.split(" ")))
if (len(lineSplit) == 4 and lineSplit[0].startswith(".")):
section = lineSplit[0]
size = int(lineSplit[2], 16)
objFile = lineSplit[3]
if (section == ".text"):
if (objFile.startswith("build/us/src")):
src += size
if (objFile.startswith("build/us/src") and objFile.count("/") == 3):
segMain += size
if (objFile.startswith("build/us/src/ending")):
segEnding += size
if (objFile.startswith("build/us/src/racing")):
segRacing += size
if (objFile.startswith("build/us/src/os")):
libultra += size
if (objFile.startswith("build/us/src/audio")):
audio += size
nonMatchingASM = GetNonMatchingSize("asm/non_matchings")
nonMatchingMain = GetNonMatchingSize("main")
nonMatchingEnding = GetNonMatchingSize("ending")
nonMatchingRacing = GetNonMatchingSize("racing")
nonMatchingLibultra = GetNonMatchingSize("os")
nonMatchingAudio = GetNonMatchingSize("audio")
game = segMain + segEnding + segRacing
segMain -= nonMatchingMain
segEnding -= nonMatchingEnding
segRacing -= nonMatchingRacing
audio -= nonMatchingAudio
libultra -= nonMatchingLibultra
src -= nonMatchingASM
game -= nonMatchingMain + nonMatchingEnding + nonMatchingRacing
decompilable = src
seg_main_size = 744112
seg_ending_size = 20032
seg_racing_size = 174224
libultra_size = 48848
audio_size = 86912
game_code_size = seg_main_size + seg_ending_size + seg_racing_size # 938368
total_code_size = seg_main_size + seg_ending_size + seg_racing_size + libultra_size + audio_size # 1074128
remaining_size = total_code_size - decompilable
segMainPct = 100 * segMain / seg_main_size
segEndingPct = 100 * segEnding / seg_ending_size
segRacingPct = 100 * segRacing / seg_racing_size
libultraPct = 100 * libultra / libultra_size
audioPct = 100 * audio / audio_size
gamePct = 100 * game / game_code_size
srcPct = 100 * src / total_code_size
def get_string_from_table(num, table):
if 0 <= num < len(table):
return table[num]
else:
return "Number out of range"
def find_closest_divisible(number, divisor):
closest_smaller = number - (number % divisor)
closest_larger = closest_smaller + divisor
if abs(number - closest_smaller) < abs(number - closest_larger):
return closest_smaller
else:
return closest_larger
def center_text(text, total_width, fill_character=" "):
empty_spaces = total_width - len(text)
left_padding = empty_spaces // 2
right_padding = empty_spaces - left_padding
centered_text = fill_character * left_padding + text + fill_character * right_padding
return centered_text
def move_character_from_bar(position, total_length, charbase="0", charfill="1"):
if position < 0:
position = 0
elif position > total_length:
position = total_length
line = charfill * total_length
line = list(line)
line[position] = charbase
return "".join(line)
def check_table_cond(cond, total, table):
if total > cond:
sym = " (V)"
elif total == cond:
sym = " (~)"
else:
sym = " (X)"
return str(table[cond]) + str(sym)
mkCups = [
"Mushroom Cup",
"Flower Cup",
"Star Cup",
"Special Cup",
]
mkCourses = [
"Luigi Raceway",
"Moo Moo Farm",
"Koopa Troopa Beach",
"Kalimari Desert",
"Toad's Turnpike",
"Frappe Snowland",
"Choco Mountain",
"Mario Raceway",
"Wario Stadium",
"Sherbet Land",
"Royal Raceway",
"Bowser's Castle",
"D.K.'s Jungle Parkway",
"Yoshi Valley",
"Banshee Boardwalk",
"Rainbow Road",
]
if args.format == 'totalBadge':
print(str(round(srcPct, 2))+"%")
elif args.format == 'gameBadge':
print(str(round(gamePct, 2))+"%")
elif args.format == 'mainBadge':
print(str(round(segMainPct, 2))+"%")
elif args.format == 'endingBadge':
print(str(round(segEndingPct, 2))+"%")
elif args.format == 'racingBadge':
print(str(round(segRacingPct, 2))+"%")
elif args.format == 'audioBadge':
print(str(round(audioPct, 2))+"%")
elif args.format == 'osBadge':
print(str(round(libultraPct, 2))+"%")
elif args.format == 'bytesToDecompile':
print(str(remaining_size)+" of "+str(total_code_size)+"\n")
elif args.format == 'globalAsmFuncs':
print(str(TotalGlobalAsmFunctions))
elif args.format == 'm2cFuncs':
print(str(TotalMipsToCFunctions))
elif args.format == 'nonmatchingFuncs':
print(str(TotalNonMatchingFunctions))
elif args.format == 'text':
bytesPerTotalLaps = total_code_size // 47
srcDivNear = find_closest_divisible(src, 49)
lapTotalCounts = int(srcDivNear / bytesPerTotalLaps)
curLapProgress = int(((srcDivNear % bytesPerTotalLaps) * 66) / (bytesPerTotalLaps))
curLapCount = int((lapTotalCounts % 3) + 1)
curCourseCount = int(lapTotalCounts / 3)
curCupCount = int((lapTotalCounts / 12) % 12)
print(str(center_text("", 67, "=")))
print(str(center_text(" All Cups (Decompilation) ", 67)))
print(str(center_text(" " + str(round(srcPct, 2))+"% Complete ", 67, "-")))
print(str(center_text(" # Decompiled functions: " + str(TotalCFunctions) + " ", 67)))
print(str(center_text(" # GLOBAL_ASM remaining: " + str(TotalGlobalAsmFunctions) + " ", 67)))
print(str(center_text(" # NON_MATCHING remaining: " + str(TotalNonMatchingFunctions) + " ", 67)))
print(str(center_text(" # MIPS_TO_C remaining: " + str(TotalMipsToCFunctions) + " ", 67)))
print(str(center_text(" Game Status ", 67, "-")))
if TotalGlobalAsmFunctions > 0:
print(str(center_text(check_table_cond(0, curCupCount, mkCups) + " - " + check_table_cond(1, curCupCount, mkCups), 67)))
print(str(center_text(check_table_cond(2, curCupCount, mkCups) + " - " + check_table_cond(3, curCupCount, mkCups), 67)))
print(str(center_text(" Lap Progress Bar and Race Status", 67, "-")))
print(str(move_character_from_bar(curLapProgress, 67, "O", "-")))
print(str(center_text("We are in " + str(get_string_from_table(curCupCount, mkCups)) + " racing at " + str(get_string_from_table(curCourseCount, mkCourses)) + " (Lap " + str(curLapCount) + "/3)", 67)))
else:
print(str(center_text("Mushroom Cup (V) - Flower Cup (V)", 67)))
print(str(center_text("Star Cup (V) - Special Cup (V)", 67)))
print(str(center_text("We finished All Cups! We got all 4 Gold Cups!", 67)))
print(str(center_text("", 67, "=")))
elif args.format == 'verbose':
print("Total decompilable bytes remaining: "+str(remaining_size)+" out of "+str(total_code_size)+"\n")
print(str(src) + "/" + str(total_code_size) + " bytes " + "decompiled" + " in total code " + str(srcPct) + "%\n")
print(str(TotalCFunctions) + " decompiled functions - " +str(TotalGlobalAsmFunctions)+" GLOBAL_ASM functions remaining."+"\n")
print(str(TotalNonMatchingFunctions)+" NON_MATCHING functions - " + str(TotalMipsToCFunctions) + " MIPS_TO_C functions." +"\n")
print("------------------------------------\n")
print(str(segMain) + "/" + str(seg_main_size) + " bytes " + "decompiled" + " in segMain " + str(segMainPct) + "%\n")
print(str(segEnding) + "/" + str(seg_ending_size) + " bytes " + "decompiled" + " in segEnding " + str(segEndingPct) + "%\n")
print(str(segRacing) + "/" + str(seg_racing_size) + " bytes " + "decompiled" + " in segRacing " + str(segRacingPct) + "%\n")
print(str(game) + "/" + str(game_code_size) + " bytes " + "decompiled" + " in game code " + str(gamePct) + "%\n")
print("------------------------------------\n")
print(str(audio) + "/" + str(audio_size) + " bytes " + "decompiled" + " in audio " + str(audioPct) + "%\n")
print(str(libultra) + "/" + str(libultra_size) + " bytes " + "decompiled" + " in libultra " + str(libultraPct) + "%\n")
else:
print("Unknown format argument: " + args.format)
|