-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.py
382 lines (325 loc) · 13.5 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
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
from pathlib import Path
from sys import argv
from argparse import ArgumentParser
from importlib import util
def check_project_structure(src_directory: Path, gfx_directory: Path,
lang_directory: Path):
has_lang_dir = True
# Check that the project is properly structured
if not src_directory.exists():
print("\"src\" directory not found. Aborting")
return (False, -1)
if not gfx_directory.exists():
print("\"gfx\" directory not found. Aborting")
return (False, -1)
if not lang_directory.exists():
print(
"\"lang\" directory not found. Assuming hard-coded strings (this is not best practice)"
)
has_lang_dir = False
# Find the grf, railtypes, and templates files
if not src_directory.joinpath("grf.pnml").exists():
print(
"\"grf.pnml\" not found. It should be in \"src\" and contain the grf block"
)
return (False, -1)
if not src_directory.joinpath("railtypes.pnml").exists():
print(
"\"railtypes.pnml\" not found. It should be in \"src\" and contain the railtypetable block"
)
return (False, -1)
if not src_directory.joinpath("sound.pnml").exists():
print(
"\"sounds.pnml\" not found. Assuming no sounds are required"
)
if not src_directory.joinpath("templates_shared.pnml").exists():
print(
"\"templates_shared.pnml\" not found. Assuming no templates are required"
)
if not src_directory.joinpath("templates_trains.pnml").exists():
print(
"\"templates_trains.pnml\" not found. Assuming no templates are required"
)
if not src_directory.joinpath("templates_trams.pnml").exists():
print(
"\"templates_trams.pnml\" not found. Assuming no templates are required"
)
print("Project structure is correct\n")
return (has_lang_dir, 0)
def copy_file(filepath: Path, nml_file: str):
# If the pnml filepath doesn't exist, exit
if not filepath.exists():
print("The file <%s> does not exist" % str(filepath))
return -1
# Read the pnml file into the internal nml
with open(str(filepath), "r") as file:
nml_file += "// " + filepath.stem + filepath.suffix + "\n"
for line in file:
nml_file += line
nml_file += "\n\n"
return nml_file
def find_pnml_files(src_directory: Path):
print("Finding pnml files in %s" % src_directory)
file_list = dict()
# Iterate through all files in src_directory recursively, finding any that end in .pnml
for path in src_directory.rglob("*.pnml"):
# Don't add the special ones
if path.stem in ["railtypes", "grf", "sounds", "templates_shared", "templates_trains", "templates_trams"]:
continue
if path.parent.stem not in file_list.keys():
file_list[path.parent.stem] = list()
file_list[path.parent.stem].append(path)
# List found files
for directory in file_list.keys():
print("Found in directory [%s]:" % directory)
print([str(file.stem + file.suffix) for file in file_list[directory]])
return file_list
def write_file(filename: str, nml_file: str):
from os import makedirs
# Generate the filepath and check if it exists
filepath = Path("build/" + filename + ".nml")
build_dir = Path("build/")
if not build_dir.exists():
makedirs("build")
if filepath.exists():
print("'%s.nml' already exists. Overwriting" % filename)
# Write the internal nml to the file
with open(filepath, "w") as file_writer:
for line in nml_file:
file_writer.write(line)
print("Written all files to '%s.nml' file\n" % filename)
return 0
def compile_grf(has_lang_dir, grf_name, lang_dir):
# Check if we have the nml package
found_nml = util.find_spec("nml")
if found_nml is not None:
# Import nml's main module
import nml.main
parameters = []
if has_lang_dir:
# If we have a lang directory, add it to the parameters
parameters = ["--lang", str(lang_dir), "build/" + grf_name + ".nml"]
else:
# If not, just the nml name
parameters = ["build/" + grf_name + ".nml"]
try:
# Try to compile the nml file
nml.main.main(parameters)
except SystemExit:
# nml uses sys.exit(), so catch this to stop the program exiting
print("nml tried to exit but was stopped")
print("Finished compiling grf file\n")
return 1
else:
# nml isn't installed
print("nml is not installed. You can get it using 'pip install nml'")
return -2
def run_game(grf_name):
from sys import platform
print("Detecting platform")
# Change default paths depending on whether we use Linux or Windows (sorry OSX)
if platform.startswith("linux"):
newgrf_dir = Path.home().joinpath(".openttd", "newgrf")
executable_path = "/usr/bin/openttd"
kill_cmd = ["killall","openttd"]
print("Detected as Linux")
elif platform.startswith("win32"):
newgrf_dir = Path.home().joinpath("Documents", "OpenTTD", "newgrf")
executable_path = "C:/Program Files/OpenTTD/openttd.exe"
kill_cmd = ["taskkill.exe" "/IM" "OpenTTD.exe"]
print("Detected as Windows")
else:
print("Detected as Other. Cannot run game.")
return -3
print("Attempting to read config")
json_read_ok = False
# Check that the config file exists
if Path("build/build.json").exists():
from json import load, decoder
with open("build/build.json") as json_data:
# Try to read the config file
try:
data = load(json_data)
# Errors if the file in invalid
except decoder.JSONDecodeError:
print("The config file is invalid")
json_read_ok = False
else:
# Read successfully
# Try to read the keys from the json file
try:
newgrf_dir = data["newgrf_dir"]
executable_path = data["executable"]
# Errors if not all keys are found
except KeyError:
print("The config json file is invalid")
json_read_ok = False
# Read successfully, set read_ok to true
else:
json_read_ok = True
print("Read config successfully")
# If reading the json didn't work
if not json_read_ok:
from json import dump
from os import access, X_OK
print("No config, require user input")
# Prompt the user for the "newgrf" directory until we get something like it
while not Path(newgrf_dir).exists():
newgrf_dir = input("Enter the newgrf directory: ")
if len(newgrf_dir) > 6:
newgrf_dir = "~/.openttd/newgrf"
continue
if newgrf_dir[-6:] != "newgrf":
newgrf_dir = "~/.openttd/newgrf"
# Prompt the user for the executable path until we get an executable
while not Path(executable_path).exists():
executable_path = input("Enter the OpenTTD executable path: ")
if not (access(executable_path, X_OK)):
executable_path = "/usr/bin/openttd"
# Dump the two paths to a json file for next time
with open("build/build.json", "w") as json_data:
data = {
"newgrf_dir": str(newgrf_dir),
"executable": str(executable_path)
}
dump(data, json_data)
from shutil import copy
from subprocess import Popen
from os import devnull
# Kill existing processes
print("Killing existing processes")
try:
kill_process = Popen(kill_cmd)
kill_process.wait()
except:
print("Something went wrong when trying to kill processes")
# Copy grf
print("Copying grf")
copy("build/" + grf_name + ".grf", Path(newgrf_dir))
# Run the game in it's root directory
print("Running game\n")
# Redirect stdout and stderr
null = open(devnull, "w")
Popen([executable_path, "-t", "2050", "-g"], cwd=Path(executable_path).parent, stdout=null, stderr=null)
null
return 2
def main(grf_name, src_dir, lang_dir, gfx_dir, b_compile_grf, b_run_game):
src_directory = Path("src")
lang_directory = Path("lang")
gfx_directory = Path("gfx")
nml_file = ""
has_lang_dir = False
# Check if the project is set up properly and we have a lang directory
(has_lang_dir,
error_code) = check_project_structure(src_directory, gfx_directory,
lang_directory)
if error_code != 0:
return -1
# Add the special files to the internal nml file
nml_file = copy_file(src_directory.joinpath("grf.pnml"), nml_file)
nml_file = copy_file(src_directory.joinpath("railtypes.pnml"), nml_file)
nml_file = copy_file(src_directory.joinpath("sounds.pnml"), nml_file)
nml_file = copy_file(src_directory.joinpath("templates_shared.pnml"), nml_file)
nml_file = copy_file(src_directory.joinpath("templates_trains.pnml"), nml_file)
nml_file = copy_file(src_directory.joinpath("templates_trams.pnml"), nml_file)
# Get a list of all the pnml files in src
file_list = find_pnml_files(src_directory)
print("Finished finding pnml files\n")
pushpull_files = list()
priority_files = list()
pnml_files = list()
append_files = list()
# Priority folders: Read all the files in folders that begin with "_" into the internal nml
for directory in file_list:
# Group pushpull files
if directory.startswith("PushPull"):
pushpull_files += file_list[directory]
continue
# Grab priority files that need to be run first
if directory.startswith("_"):
priority_files += file_list[directory]
continue
# Grab "append" files (that go at the end)
if directory == "append":
append_files += file_list[directory]
continue
# Everything else
else:
pnml_files += file_list[directory]
# Special pushpull file first
for file in sorted(pushpull_files):
if file.stem.startswith("PushPull"):
print("Found PushPull.pnml special item")
nml_file = copy_file(file, nml_file)
# Read the other pushpull files
for file in sorted(pushpull_files):
if file.stem.startswith("PushPull"):
continue;
print("Reading pushpull file '%s'" % (file.stem + file.suffix))
nml_file = copy_file(file, nml_file)
# Read the priority files
for file in sorted(priority_files):
print("Reading priority file '%s'" % (file.stem + file.suffix))
nml_file = copy_file(file, nml_file)
# Read the regular files
for file in sorted(pnml_files):
# print("Reading '%s'" % (file.stem + file.suffix))
nml_file = copy_file(file, nml_file)
# Read the append files (mostly switches to disable units)
for file in sorted(append_files):
# print("Reading '%s'" % (file.stem + file.suffix))
nml_file = copy_file(file, nml_file)
print("Copied all files to internal buffer\n")
# Try to write the internal nml to a file
if write_file(grf_name, nml_file) != 0:
print("The nml file failed to compile")
return -1
# If we're compiling or running the game
if b_compile_grf or b_run_game:
# Try to compile the GRF
error = compile_grf(has_lang_dir, grf_name, lang_dir)
if error == -2:
return -2
elif b_run_game == False:
return 1
# Optionally run the game
if b_run_game:
return run_game(grf_name)
return 0
if __name__ == "__main__":
# Parser arguments
parser = ArgumentParser(description="Compile pnml files into one nml file")
parser.add_argument("grf_name")
parser.add_argument("--src", default="src", help="Source files directory")
parser.add_argument("--lang",
default="lang",
help="Language files directory")
parser.add_argument("--gfx",
default="gfx",
help="Graphics files directory")
parser.add_argument("--compile",
action="store_true",
help="Compile the nml file with nmlc")
parser.add_argument(
"--run",
action="store_true",
help="Run the game after compilation (will also compile the file. Also kills existing instances)")
args = parser.parse_args()
# Reports any errors in the nml file compilation process
error_code = main(args.grf_name, args.src, args.lang, args.gfx,
args.compile, args.run)
if error_code == -1:
print(
"The nml file failed to compile properly. Please consult the log")
elif error_code == -2:
print("The nml file compiled correctly, but nml failed to compile it")
elif error_code == -3:
print(
"The grf file compiled successfully but the game failed to start")
elif error_code == 1:
print("The grf file was compiled successfully")
elif error_code == 2:
print(
"The grf file was compiled successfully, and the game was started")
else:
print("The nml file was compiled successfully (this is the not grf)")