-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·203 lines (167 loc) · 5.73 KB
/
build
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
#!/usr/bin/env python3
import os, glob, shutil
from os import name as _osname
#CONSTANTS
# generic
BUILD_PATH = "dbuild"
# executables / tools
CSC_PATH = "mcs" if _osname == "posix" else "csc.exe"
ASM_PATH = "rgbasm"
LINK_PATH = "rgblink"
CONVERTER_BUILD_FOLDER = "monconverter"
PAYLOAD_BUILD_FOLDER = "bytes"
# converter stuff
CONV_BUILD_NAME = "converter" if _osname == "posix" else "converter.exe"
CONV_DEPENDENCIES = [
"PKHeX.Core.dll",
]
CONV_DEPENDENCIES_PATH = "converter/dependencies"
CONV_SRC_PATH = f"converter/converter.cs"
#payload stuff
ASM_ARGS = "" #"-gbz80 -nocase -chklabels -dotdir -Fbin"
LINK_ARGS = "-x"
PAYLOAD_SRC_PATH = "payload/payload.asm"
PAYLOAD_BUILDS = {
"R-B_English" : 1,
"R-B_Europe" : 2,
"G-S_English" : 3,
}
#reader
READER_PATH = "byteReader"
READER_NAME = "reader"
#FUNCTIONS
def build_payload():
if not os.path.isdir(BUILD_PATH):
os.mkdir(BUILD_PATH)
# rgbasm [file] -o [out]
# rgblink [out] -x -o out.temp
# out.temp->remove_unnecessary
# writebytes.txt(out.temp)
# remove(out, out.temp)
try:
print("assembling the payloads...")
retval = 0 #if any thing has a return value of not 0 then this will be != 0
TEMPFILE_NAME = f"{BUILD_PATH}/payload.temp" #name of the temp file created by the assembler
LINK_TEMP_NAME= f"{BUILD_PATH}/payload.link" #name of the temp file created by the linker
for VERSION in PAYLOAD_BUILDS:
print("building the", VERSION, "version")
BUILT_NAME = f"{BUILD_PATH}/{VERSION}.txt" #name of the built file
retval += os.system(f'{ASM_PATH} {ASM_ARGS} -D VERSION={PAYLOAD_BUILDS[VERSION]} -o "{TEMPFILE_NAME}" "{PAYLOAD_SRC_PATH}"')
retval += os.system(f'{LINK_PATH} {LINK_ARGS} -x -o "{LINK_TEMP_NAME}" "{TEMPFILE_NAME}"')
if retval != 0: raise Exception("assembly failed")
print("writing the bytes.txt file...")
with open(LINK_TEMP_NAME, "rb") as ifile, open(BUILT_NAME, "w") as ofile:
allbytes = list(ifile.read(-1))
res = ""
for byte in allbytes:
byte = "%0.2X" % byte
res += byte + "\n"
res = res[:-1] #get rid of the last endline
ofile.write(res)
print("removing temp files...")
os.remove(TEMPFILE_NAME)
os.remove(LINK_TEMP_NAME)
print("\n\n")
#END FOR
if retval != 0:
RET_CODE = 1
else:
print("done!")
RET_CODE = 0
except Exception as e:
print("error while building:\n" + str(e))
RET_CODE = 1
return (RET_CODE)
def build_converter():
if not os.path.isdir(BUILD_PATH):
os.mkdir(BUILD_PATH)
try:
command = f'{CSC_PATH} /r:"{CONV_DEPENDENCIES_PATH}/netstandard.dll" ' # compiler, first options and default dependency
command += "".join( #all dependency
f'/r:"{CONV_DEPENDENCIES_PATH}/{dependency}" ' for dependency in CONV_DEPENDENCIES
)
command += f'/out:{BUILD_PATH}/{CONV_BUILD_NAME} {CONV_SRC_PATH}'
retval = os.system(command)
if retval != 0:
RET_CODE = 1
else:
# if _osname == "posix":
# os.system(f"chmod +x {BUILD_PATH}/{CONV_BUILD_NAME}")
print("done!")
RET_CODE = 0
except Exception as e:
print("error while building:\n" + str(e))
RET_CODE = 1
return (RET_CODE)
def build_all():
try:
if not os.path.isdir(BUILD_PATH):
os.mkdir(BUILD_PATH)
print("building the converter...")
if build_converter() != 0:
print("build aborted")
return (-1)
print("\n")
if build_payload() != 0:
print("build aborted")
return (-1)
print("\n")
print("moving everything in a folder...")
os.mkdir (f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}")
os.mkdir (f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}")
os.mkdir (f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/res")
os.rename(f"{BUILD_PATH}/{CONV_BUILD_NAME}", f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}/{CONV_BUILD_NAME}")
for dependency in CONV_DEPENDENCIES:
shutil.copyfile(f"{CONV_DEPENDENCIES_PATH}/{dependency}", f'{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}/{dependency}')
#copy py script into pyw
shutil.copyfile(f"{READER_PATH}/{READER_NAME}.py", f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/{READER_NAME}.pyw")
#copy bytes files
for file in glob.glob(f"{BUILD_PATH}/*.txt"):
os.rename(file, f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/res/{os.path.basename(file)}")
#copy any asset
for file in glob.glob("{READER_PATH}/res/*"):
shutil.copyfile(file, f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/res/{os.path.basename(file)}")
print("zipping...")
shutil.make_archive(f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}", 'zip', f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}")
shutil.make_archive(f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}", 'zip', f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}")
print("done!")
RET_CODE = 0
except Exception as e:
print("error while building:\n" + str(e))
RET_CODE = 1
finally:
print("cleaning up build files")
files_for_cleanup = [
f"{BUILD_PATH}/{CONV_BUILD_NAME}",
f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}/{CONV_BUILD_NAME}",
f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/{READER_NAME}.pyw"
]
files_for_cleanup += glob.glob(f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/res/*.txt") + glob.glob(f"{BUILD_PATH}/*.txt")
files_for_cleanup += glob.glob(f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}/*.dll")
dirs_for_cleanup = [
f"{BUILD_PATH}/{CONVERTER_BUILD_FOLDER}",
f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}/res",
f"{BUILD_PATH}/{PAYLOAD_BUILD_FOLDER}"
]
for file in files_for_cleanup:
if os.path.isfile(file):
os.remove(file)
for dir in dirs_for_cleanup:
if os.path.isdir(dir):
os.rmdir (dir)
# /bin/bash -c 'read -s -n 1 -p \"Press any key to continue...\n\"'
os.system("" if os.name == "posix" else "pause")
exit(RET_CODE)
from sys import argv
match argv[1]:
case "asm":
build_payload()
case "conv":
build_converter()
case "all":
build_all()
case "clear":
for f in glob.glob(BUILD_PATH + "/*"):
os.remove(f)
case _:
print("invalid option; must be one of:\n\t- asm\n\t- conv\n\t- all")