-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc2ea.py
223 lines (177 loc) · 8.9 KB
/
c2ea.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
import nightmare, sys, csv, glob, os
TABLE_INLINED = False
def showExceptionAndExit(exc_type, exc_value, tb):
import traceback
traceback.print_exception(exc_type, exc_value, tb)
input("Press Enter key to exit.")
sys.exit(-1)
def getArgLength(nmmentry):
"""takes the nmm entry object and returns the appropriate EA marker"""
if (nmmentry.length==4) & (nmmentry.offset%4==0):
return "WORD "
elif (nmmentry.length==2) & (nmmentry.offset%2==0):
return "SHORT "
else:
return "BYTE "
def addToInstaller(csvList,installername):
"""Takes a list of csv files and adds them to the EA installer"""
with open(installername,"w") as myfile:
myfile.write("//EA Table Installation file generated by c2ea.exe\n\n")
myfile.write('#include "Table Definitions.txt"\n\n')
for csv in csvList:
nmpath = csv.replace(".csv",".nmm")
nmm = nightmare.NightmareTable(nmpath)
filename = csv.replace(".csv",".event") #I don't wanna use .txt because it conflicts, but this is supposed to be a text file!
filename = os.path.relpath(filename, os.path.dirname(installername)) # filename.replace(os.getcwd()+'\\','')
with open(installername,'a') as myfile:
#myfile.write("ORG " + hex(nmm.offset) + '\n') Don't put the offset here, have it in the dmp.
myfile.write('#include "' + filename + '"\n\n')
def process(inputCSV, inputNMM, filename, rom):
"""Takes a csv and spits out an EA macro file (.event, but actually text). Requires a nmm with the same name in the same folder.""" #is it possible to tell if it's inline?
global TABLE_INLINED
macroName = "_C2EA_{}".format(os.path.split(os.path.splitext(inputCSV)[0])[1].replace(' ', '_'))
nmm = nightmare.NightmareTable(inputNMM)
rompath = rom
macroArgs = [] #params for macro
macroOutput = '' #expanded macro form
outputlines = []
originalOffset = nmm.offset
tableOffset = originalOffset #this gets changed to whatever is in the first cell of the csv actually
currlen = '' #because we start with BYTE
fillwithzero = None
for x in range(nmm.colNum):
argx = "arg"+'{0:03d}'.format(x)
macroArgs.append(argx) #arg000, arg001, arg002 etc up to 1000 columns.
arglen = getArgLength(nmm.columns[x]) #this gets the appropriate length string.
if arglen!=currlen: #only append if needed else just add arg.
if currlen!='': #this should only be on the first line
macroOutput+=';'
#assuming arglen does not equal currlen
macroOutput+=(arglen + argx + ' ')
currlen = arglen
else:
macroOutput+=(argx + ' ')
with open(inputCSV, 'r') as myfile:
table = csv.reader(myfile)
tableOffset = next(table)[0]
for row in table:
outputline = "{}(".format(macroName)
items = zip(nmm.columns, row[1:])
for entry, data in items:
thisentry = ''
#output.extend(int(data, 0).to_bytes(entry.length, 'little', signed=entry.signed))
if data=='':
if fillwithzero == None:
fillwithzero = input("Warning: "+ inputCSV + " has a blank cell.\nContinue anyway? Fills cells with '0' (y/n)").strip().lower()=='y'
if fillwithzero==True:
data = '0'
else:
input("Press Enter to quit.")
sys.exit(-1)
try:
arglen = getArgLength(entry)
if (arglen=="WORD ")|(arglen=="SHORT "):
outputline += data + ','
else:
dt = int(data, 0).to_bytes(entry.length, 'little', signed=entry.signed) #this is a string i guess
for byte in dt:
thisentry += (hex(byte)+' ')
outputline += thisentry[:-1] + ','
except ValueError: #if it's not a number, just add it directly
outputline += (data+',')
outputline = outputline[:-1] + ')'
outputlines.append(outputline)
with open(filename, 'w') as dumpfile:
inline = False
dumpfile.write("#define {}(".format(macroName))
dumpfile.write(','.join(macroArgs)) #turns list into 'arg000,arg001' etc
dumpfile.write(') "')
dumpfile.write(macroOutput + '"\n\n') #e.g. BYTE arg000, WORD arg001, etc
if tableOffset.strip()[0:6]=="INLINE":
from c2eaPfinder import pointerOffsets
TABLE_INLINED = True
if rompath == None:
import tkinter as tk
root = tk.Tk()
root.withdraw()
from tkinter import filedialog
rompath = filedialog.askopenfilename(filetypes=[("GBA files",".gba"),("All files",".*")],initialdir=os.getcwd(),title="Select ROM to use for repointing")
label = tableOffset.replace("INLINE",'').strip()
# Here we do *not* want to use PFinder
dumpfile.write("PUSH\n")
for offset in pointerOffsets(rompath, originalOffset | 0x8000000):
dumpfile.write("ORG ${:X}\n".format(offset))
dumpfile.write("POIN {}\n".format(label))
dumpfile.write("POP\n")
# There, much better :)
dumpfile.write("ALIGN 4\n{}:\n".format(label))
inline = True
else:
dumpfile.write("PUSH\nORG "+tableOffset+"\n")
dumpfile.write('\n'.join(outputlines))
if not inline:
dumpfile.write("\nPOP")
#dumpfile.write('\n}\n')
print("Wrote to " + filename)
return rompath
def main():
sys.excepthook = showExceptionAndExit
doSingleFile = False
folder = os.getcwd()
installer = "Table Installer.event"
rom = None
csvFile = None
nmmFile = None
outFile = None
if len(sys.argv) > 1:
import argparse
parser = argparse.ArgumentParser(description = 'Convert CSV file(s) to EA events using NMM file(s) are reference. Defaults to looking for CSVs in the current directory. You can specify a directory to look in using -folder, or you can switch to processing singles CSVs using -csv.')
# Common arguments
parser.add_argument('rom', nargs='?', help = 'reference ROM (for pointer searching)')
# parser.add_argument('-nocache') # sounds like no$ xd
# parser.add_argument('-clearcache')
# Arguments for single CSV processing
parser.add_argument('-csv', help = 'CSV for single csv processing')
parser.add_argument('-nmm', help = '(use with -csv) reference NMM (default: [CSVFile]:.csv=.nmm)')
parser.add_argument('-out', help = '(use with -csv) output event (default: [CSVFile]:.csv=.event)')
# Arguments for folder processing
parser.add_argument('-folder', help = 'folder to look for csvs in')
parser.add_argument('-installer', help = 'output installer event (default: [Folder]/Table Installer.event)')
args = parser.parse_args()
rom = args.rom
if args.csv != None:
if (args.folder != None) or (args.installer != None):
sys.exit("ERROR: -folder or -installer argument specified with -csv, aborting.")
doSingleFile = True
csvFile = args.csv
nmmFile = args.nmm if args.nmm != None else csvFile.replace(".csv", ".nmm")
outFile = args.out if args.out != None else csvFile.replace(".csv", ".event")
else:
if (args.nmm != None) or (args.out != None):
sys.exit("ERROR: -nmm or -out argument specified without -csv, aborting.")
if args.folder != None:
folder = args.folder
installer = args.installer if args.installer != None else (folder + '/Table Installer.event')
if doSingleFile:
if not os.path.exists(csvFile):
sys.exit("ERROR: CSV File `{}` doesn't exist!".format(csvFile))
if not os.path.exists(nmmFile):
sys.exit("ERROR: NMM File `{}` doesn't exist!".format(nmmFile))
process(csvFile, nmmFile, outFile, rom)
else: # not doSingleFile
csvList = glob.glob(folder + '/**/*.csv', recursive = True)
for filename in csvList:
rom = process(
filename,
filename.replace(".csv",".nmm"),
filename.replace(".csv",".event"),
rom
)
addToInstaller(csvList, installer)
if TABLE_INLINED:
# If we ran successfully and used pfinder, save the pfinder cache.
from c2eaPfinder import writeCache
writeCache()
input("Press Enter to continue")
if __name__ == '__main__':
main()