forked from KristalTeam/Kristal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
281 lines (221 loc) · 8.7 KB
/
build.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
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
import os
import os.path
import argparse
import sys
import shutil
import grope
from pe_tools import parse_pe, IMAGE_DIRECTORY_ENTRY_RESOURCE
from pe_tools.rsrc import parse_pe_resources, pe_resources_prepack, parse_prelink_resources, KnownResourceTypes
from pe_tools.version_info import parse_version_info, VersionInfo
ver_str = "0.1.0"
windows_ver = "0, 1, 0, 0"
file_description = "Mod for deltarune kristal"
# Contains code from https://github.com/avast/pe_tools/blob/master/pe_tools/peresed.py
RT_VERSION = KnownResourceTypes.RT_VERSION
class _IdentityReplace:
def __init__(self, val):
self._val = val
def __call__(self, s):
return self._val
class Version:
def __init__(self, s):
s = s.strip("-beta")
s = s.strip("-alpha")
s = s.strip("-dev")
parts = s.split(',')
if len(parts) == 1:
parts = parts[0].split('.')
self._parts = [int(part.strip()) for part in parts]
if not self._parts or len(self._parts) > 4 or any(part < 0 or part >= 2**16 for part in self._parts):
raise ValueError('invalid version')
while len(self._parts) < 4:
self._parts.append(0)
def get_ms_ls(self):
ms = (self._parts[0] << 16) + self._parts[1]
ls = (self._parts[2] << 16) + self._parts[3]
return ms, ls
def format(self):
return ', '.join(str(part) for part in self._parts)
def setInfo(key, value):
ver_data = None
for name in resources.get(RT_VERSION, ()):
for lang in resources[RT_VERSION][name]:
if ver_data is not None:
print('error: multiple manifest resources found', file=sys.stderr)
return 4
ver_data = resources[RT_VERSION][name][lang]
ver_name = name
ver_lang = lang
if ver_data is None:
ver_data = VersionInfo()
params = {}
params[key] = _IdentityReplace(value)
vi = parse_version_info(ver_data)
fvi = vi.get_fixed_info()
if 'FileVersion' in params:
ver = Version(params['FileVersion'](None))
fvi.dwFileVersionMS, fvi.dwFileVersionLS = ver.get_ms_ls()
if 'ProductVersion' in params:
ver = Version(params['ProductVersion'](None))
fvi.dwProductVersionMS, fvi.dwProductVersionLS = ver.get_ms_ls()
vi.set_fixed_info(fvi)
sfi = vi.string_file_info()
for _, strings in sfi.items():
for k, fn in params.items():
val = fn(strings.get(k, ''))
if val:
strings[k] = val
elif k in strings:
del strings[k]
vi.set_string_file_info(sfi)
resources[RT_VERSION][ver_name][ver_lang] = vi.pack()
build_path = "build"
output_path = "output"
kristal_path = "Kristal"
try:
os.makedirs(os.path.join(build_path, "executable"))
except FileExistsError:
pass
try:
os.makedirs(os.path.join(build_path, "kristal"))
except FileExistsError:
pass
try:
os.makedirs(output_path)
except FileExistsError:
pass
def fatal(error):
print(error)
sys.exit(1)
parser = argparse.ArgumentParser(prog="kristal_build.py", description='Utility script to compile Kristal.')
parser.add_argument('--love', nargs=1, help='The path to the LÖVE folder (not the executable)')
parser.add_argument('--kristal', nargs=1, help='The path to the Kristal folder')
args = parser.parse_args()
if args.kristal:
kristal_path = args.kristal[0]
print(f"Compiling Kristal...")
print("Reading Kristal version...")
try:
with open(os.path.join(kristal_path, "VERSION"), "r") as file:
ver_str = file.read()
windows_ver = Version(ver_str).format()
except:
pass
kristal_love_path = os.path.join(output_path, "kristal-"+ver_str+".love")
print("Copying engine files...")
ignorefiles = [
".github",
".git",
".vscode",
"docs",
"lib",
"build"
]
try:
for file in os.listdir(kristal_path):
if not file in ignorefiles:
if os.path.isfile(os.path.join(kristal_path, file)):
shutil.copy(os.path.join(kristal_path, file), os.path.join(build_path, "kristal"))
elif os.path.isdir(os.path.join(kristal_path, file)):
shutil.copytree(os.path.join(kristal_path, file), os.path.join(build_path, "kristal", file))
except FileNotFoundError:
fatal("Error: \"kristal\" folder missing! Please place a clean copy of Kristal's source code next to this script in a folder titled \"kristal\".")
print("Making .zip file...")
shutil.make_archive(os.path.join(build_path, "kristal"), 'zip', os.path.join(build_path, "kristal"))
print("Removing copied files...")
shutil.rmtree(os.path.join(build_path, "kristal"))
print("Renaming .zip to .love and moving it to the output folder...")
shutil.move(os.path.join(build_path, "kristal.zip"), kristal_love_path)
love2d_path = None
if args.love:
love2d_path = args.love[0]
print("Using supplied LÖVE path...")
if os.path.isfile(os.path.join(love2d_path, "love.exe")):
print("LÖVE found!")
else:
fatal("Error: LÖVE not found at passed directory")
else:
print("Finding LÖVE...")
print("Checking PATH...")
path_var = os.getenv('PATH')
if path_var is None:
fatal("Error: PATH not found! Please specify the path to LÖVE with --love.")
for path in path_var.split(";"):
if path == "":
continue
if os.path.isfile(os.path.join(path, "love.exe")):
love2d_path = path
print(f"LÖVE found: {path}")
break
else:
fatal("Error: LÖVE not found! Please specify the path to LÖVE with --love.")
# Search PATH
print("Compiling into exe...")
try:
with open(os.path.join(love2d_path, "love.exe"), "rb") as file1, open(kristal_love_path, "rb") as file2, open(os.path.join(build_path, "kristal_noicon.exe"), "wb") as output:
output.write(file1.read())
output.write(file2.read())
except FileNotFoundError:
fatal("Error: LÖVE or Kristal not found!")
print("Patching in custom icon...")
fin = open(os.path.join(build_path, "kristal_noicon.exe"), 'rb')
pe = parse_pe(grope.wrap_io(fin))
resources = pe.parse_resources()
try:
res_fin = open(os.path.join(kristal_path, "icon.res"), 'rb')
except FileNotFoundError:
fatal("Error: icon.res not found! To compile one yourself, run \"rc icon.rc\" in a Visual Studio developer console.")
r = parse_prelink_resources(grope.wrap_io(res_fin))
for resource_type in r:
for name in r[resource_type]:
for lang in r[resource_type][name]:
resources.setdefault(resource_type, {}).setdefault(name, {})[lang] = r[resource_type][name][lang]
print("Patching in custom information...")
setInfo("FileVersion", windows_ver)
setInfo("ProductVersion", windows_ver)
setInfo("FileDescription", file_description)
setInfo("InternalName", "Kristal")
setInfo("LegalCopyright", "Copyright © 2023 Kristal Team")
setInfo("OriginalFilename", "kristal.exe")
setInfo("ProductName", "Kristal")
prepacked = pe_resources_prepack(resources)
addr = pe.resize_directory(IMAGE_DIRECTORY_ENTRY_RESOURCE, prepacked.size)
pe.set_directory(IMAGE_DIRECTORY_ENTRY_RESOURCE, prepacked.pack(addr))
print("Writing new file...")
with open(os.path.join(build_path, "executable", "kristal.exe"), 'wb') as fout:
grope.dump(pe.to_blob(), fout)
res_fin.close()
fin.close()
print("Copying files...")
copyfiles = [
"SDL2.dll",
"OpenAL32.dll",
"license.txt",
"love.dll",
"lua51.dll",
"mpg123.dll",
"msvcp120.dll",
"msvcr120.dll",
]
for file in copyfiles:
shutil.copy(os.path.join(love2d_path, file), os.path.join(build_path, "executable"))
print("Copying libraries...")
for file in os.listdir(os.path.join(kristal_path, "lib")):
shutil.copy(os.path.join(kristal_path, "lib", file), os.path.join(build_path, "executable"))
print("Zipping built file...")
shutil.make_archive(os.path.join(output_path, "kristal-"+ver_str+"-win"), 'zip', os.path.join(build_path, "executable"))
print("Packaging example mod...")
try:
os.makedirs(os.path.join(build_path, "example"))
except FileExistsError:
pass
shutil.copytree(os.path.join(kristal_path, "mod_template", "assets"), os.path.join(build_path, "example", "assets"))
shutil.copytree(os.path.join(kristal_path, "mod_template", "scripts"), os.path.join(build_path, "example", "scripts"))
shutil.copy(os.path.join(kristal_path, "mods", "example", "mod.json"), os.path.join(build_path, "example", "mod.json"))
shutil.copy(os.path.join(kristal_path, "mod_template", "mod.lua"), os.path.join(build_path, "example", "mod.lua"))
shutil.make_archive(os.path.join(output_path, "example-mod"), 'zip', os.path.join(build_path, "example"))
print("Done!")
print("Generated files:")
print("> kristal-"+ver_str+".love")
print("> kristal-"+ver_str+".zip")
print("> example-mod.zip")