-
Notifications
You must be signed in to change notification settings - Fork 3
/
progress.py
230 lines (188 loc) · 6.12 KB
/
progress.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
#!/usr/bin/env python3
import argparse
import json
import csv
import git
import os
import re
import math
parser = argparse.ArgumentParser(description="Computes current decompilation progress")
parser.add_argument("-s", "--silent", dest='silent', action='store_true', help="Run silently")
parser.add_argument("-u", "--update", dest='update', action='store_true', help="Update the readme")
parser.add_argument("-d", "--dumpcsv", dest='dumpcsv', action='store_true', help="Append progress data to csv file")
parser.add_argument("-f", "--csvfile", dest='csvfile', type=str, default="")
args = parser.parse_args()
NON_MATCHING_PATTERN = r"#ifdef\s+NON_MATCHING.*?#pragma\s+GLOBAL_ASM\s*\(\s*\"(.*?)\"\s*\).*?#endif"
PROGRAM = "mischiefmakers"
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 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 GetNonMatchingSize(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 UpdateReadme(tofind, pct):
with open('README.md', 'r') as file:
filedata = file.read()
index = filedata.find(tofind)
if (not index == -1):
index += len(tofind)
qndex = filedata.find("-", index)
if (not qndex == -1):
filedata = filedata.replace(filedata[index:qndex], '%.2f' % pct, 1)
with open('README.md', 'w') as file:
file.write(filedata)
mapFile = ReadAllLines("build/" + PROGRAM + ".map")
src = 0
asm = 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/src")):
src += size
elif (objFile.startswith("build/asm")):
asm += size
total = src + asm
nonMatchingFunctions = []
nonMatchingSize = GetNonMatchingSize("asm/nonmatchings")
srcSize = src - nonMatchingSize
asmSize = asm + nonMatchingSize
nonMatchingFunctions = GetNonMatchingFunctions(GetFiles("src", ".c"))
nonMatchingSize = GetNonMatchingSize("asm/nonmatchings")
nonMatchingSrcSize = src - nonMatchingSize
nonMatchingAsmSize = asm + nonMatchingSize
srcPct = 100 * srcSize / total
asmPct = 100 * asmSize / total
nonMatchingSrcPct = 100 * nonMatchingSrcSize / total
nonMatchingAsmPct = 100 * nonMatchingAsmSize / total
hexformat = '%06x'
levelNames = [
[
"Meet Marina!!",
"Meet Calina!!",
"Clanball Land",
"Spike Land",
"3 Clancer Kids",
"Blockman Rises",
"Wormin\' Up!!",
"Crisis: Nepton",
"Western World",
"Volcano!!"
],
[
"Sea of Lava",
"Vertigo!!",
"Sink or Float!",
"Hot Rush",
"Searin\' Swing!",
"Flambéé!!",
"Tightrope Ride",
"Freefall!!",
"Magma Rafts!!",
"Seasick Climb",
"Migen Brawl!!"
],
[
"Clanpot Shake",
"Clance War",
"Missile Surf!!",
"Clanball Lift!",
"Go Marzen 64",
"Chilly Dog!!",
"Snowstorm Maze",
"LUNAR!!",
"The Day Before",
"The Day Of",
"Cat-astrophe!!",
"CERBERUS α"
],
[
"Rolling Rock!!",
"Toadly Raw!!",
"7 Clancer Kids",
"Rescue! Act 1",
"Rescue! Act 2",
"TARUS!!",
"Ghost Catcher!",
"Aster\'s Tryke!",
"Moley Cow!!",
"Aster\'s Maze!",
"SASQUATCH β"
],
[
"Clance War II",
"Counterattack",
"Bee\'s the One!",
"MERCO!!",
"Trapped!?",
"PHOENIX γ",
"Inner Struggle",
"Final Battle",
"Ending"
]
]
chapters = len(levelNames)
totalLevels = 0
for levels in levelNames:
totalLevels += len(levels)
if (args.dumpcsv):
git_object = git.Repo().head.object
data = [
str(git_object.committed_date),
git_object.hexsha,
str(srcSize), str(asmSize),
str(nonMatchingSrcSize), str(nonMatchingAsmSize),
str(len(nonMatchingFunctions)),
str(total)
]
if (not args.silent):
print(",".join(data))
dir = args.csvfile
if (dir == ""):
dir = "progress.csv"
with open(dir, 'a') as file:
file.write("\n" + ",".join(data))
if (not args.silent):
levelIndex = math.floor((nonMatchingSrcSize / total) * totalLevels)
chapter = math.floor((nonMatchingSrcSize / total) * chapters)
levelCounter = levelIndex
# Super laziness
for levels in levelNames:
if (len(levels) > levelCounter): break
else: levelCounter -= len(levels)
levelName = levelNames[chapter][levelIndex]
goldGems = math.floor((srcSize / total) * totalLevels)
print("0x" + (hexformat % total).upper() + " total bytes of decompilable code")
print("\033[0;32m0x" + (hexformat % srcSize).upper() + " / 0x" + (hexformat % total).upper() + " matching (" + str(srcPct) + "%)\033[0;0m")
print("\033[0;33m0x" + (hexformat % nonMatchingSrcSize).upper() + " / 0x" + (hexformat % total).upper() + " decompiled (" + str(nonMatchingSrcPct) + "%)\033[0;0m")
print("------------------------------------")
print("You are on " + str(chapter + 1) + "-" + str(levelIndex + 1) + ": " + levelName)
print("You have collected " + str(goldGems) + " / " + str(totalLevels) + " gold gems!")
if (args.update):
UpdateReadme("/matched-", srcPct)
UpdateReadme("/decompiled-", nonMatchingSrcPct)