/
make_adventure.py
422 lines (377 loc) · 12.1 KB
/
make_adventure.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/python3
#
# MIT License
#
# Copyright (c) 2024 Sergio Chico
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from __future__ import print_function
from operator import itemgetter, attrgetter
import sys
import os
import gettext
import argparse
import subprocess
def run_exec(exec_path, parameter_list=[], capture_output=False):
"""_summary_
Args:
zx0_path (_type_): _description_
chunk (_type_): _description_
"""
exec_path = os.path.abspath(exec_path) # Get the absolute path of the executable
command_line = [exec_path] + parameter_list
try:
stdout = None
# stdout = subprocess.DEVNULL
# stdout = subprocess.STDOUT
stderr = None
# stderr=subprocess.DEVNULL
result = subprocess.run(
args=command_line,
check=False,
stdout=stdout,
stderr=stderr,
text=capture_output,
capture_output=capture_output,
universal_newlines=capture_output,
)
except subprocess.CalledProcessError as exc:
raise OSError from exc
if result.returncode != 0:
raise OSError(result.stderr)
return result
def dir_path(string):
"""_summary_
Args:
string (_type_): _description_
Raises:
NotADirectoryError: _description_
Returns:
_type_: _description_
"""
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
def file_path(string):
"""_summary_
Args:
string (_type_): _description_
Raises:
FileNotFoundError: _description_
Returns:
_type_: _description_
"""
if os.path.isfile(string):
return string
else:
raise FileNotFoundError(string)
def main():
version = "0.1.1"
program = "Make Adventure " + version
exec = "make_adventure"
if sys.version_info[0] < 3: # Python 2
sys.exit(_("ERROR: Invalid python version"))
gettext.bindtextdomain(
exec, os.path.join(os.path.abspath(os.path.dirname(__file__)), "locale")
)
gettext.textdomain(exec)
_ = gettext.gettext
# setting paths
curr_path = os.path.abspath(os.path.dirname(__file__))
dist_path = os.path.join(curr_path, "dist")
tools_path = os.path.join(curr_path, "tools")
external_path = os.path.join(curr_path, "external")
if os.name == "nt":
csc_path = os.path.join(dist_path, "csc.exe")
python_path = os.path.join(dist_path, "python", "python.exe")
sjasmplus_path = os.path.join(tools_path, "sjasmplus.exe")
mkp3fs_path = os.path.join(tools_path, "mkp3fs.exe")
else:
csc_path = os.path.join(dist_path, "csc")
python_path = "/usr/bin/python"
sjasmplus_path = os.path.join(external_path, "sjasmplus")
sjasmplus_path = os.path.join(sjasmplus_path, "sjasmplus")
mkp3fs_path = os.path.join(external_path, "taptools-1.1.1")
mkp3fs_path = os.path.join(mkp3fs_path, "mkp3fs")
cydc_path = os.path.join(dist_path, "cydc_cli.py")
#########################################################
arg_parser = argparse.ArgumentParser(sys.argv[0], description=program)
arg_parser.add_argument(
"-n",
"--name",
metavar=_("NAME"),
help=_("Name of the adventure"),
default="test",
)
arg_parser.add_argument(
"-o",
"--output-path",
type=dir_path,
metavar=_("OUTPUT_PATH"),
help=_("Output path to files"),
default=curr_path,
)
arg_parser.add_argument(
"-csc",
"--csc-images-path",
type=dir_path,
help=_("path to the directory with the SCR files"),
default=os.path.join(curr_path, "IMAGES"),
)
arg_parser.add_argument(
"-pt3",
"--pt3-tracks-path",
type=dir_path,
help=_("path to the directory with the PT3 tracks"),
default=os.path.join(curr_path, "TRACKS"),
)
arg_parser.add_argument(
"-sfx",
"--sfx-asm-file",
help=_("path to the asm file generated by beepfx"),
default=os.path.join(curr_path, "SFX.ASM"),
)
arg_parser.add_argument(
"-scr",
"--load-scr-file",
help=_("path to the SCR file used as Loading screen"),
default=os.path.join(curr_path, "IMAGES/000.SCR"),
)
arg_parser.add_argument(
"-tok",
"--tokens-file",
help=_("path to the token json file"),
default=os.path.join(curr_path, "tokens.json"),
)
arg_parser.add_argument(
"-chr",
"--charset-file",
help=_("path to the charset json file"),
default=os.path.join(curr_path, "charset.json"),
)
###
arg_parser.add_argument(
"-il",
"--image-lines",
metavar=_("NUM_IMAGE_LINES"),
type=int,
help=_("Number of lines of the image to use (default: %(default)d)"),
default=192,
)
###
arg_parser.add_argument(
"-v", "--verbose", action="store_true", help=_("show additional information")
)
arg_parser.add_argument(
"-V",
"--version",
action="version",
version=program,
help=_("show program's version number and exit"),
)
##
arg_parser.add_argument(
"-l",
"--min-length",
metavar=_("MIN_LENGTH"),
type=int,
help=_("minimum abbreviation length (default: %(default)d)"),
default=3,
)
arg_parser.add_argument(
"-L",
"--max-length",
metavar=_("MAX_LENGTH"),
type=int,
help=_("maximum abbreviation length (default: %(default)d)"),
default=30,
)
arg_parser.add_argument(
"-s",
"--superset-limit",
metavar=_("SUPERSET_LIMIT"),
type=int,
help=_("limit for the superset search heuristic (default: %(default)d)"),
default=100,
)
# token_group = arg_parser.add_mutually_exclusive_group()
arg_parser.add_argument(
"-T",
"--export-tokens-file",
metavar=_("EXPORT-TOKENS_FILE"),
help=_("file to export the found tokens"),
)
arg_parser.add_argument(
"-t",
"--import-tokens-file",
metavar=_("IMPORT-TOKENS-FILE"),
help=_("file with the tokens to use"),
)
###
arg_parser.add_argument(
"-C",
"--export-charset",
metavar=_("EXPORT-CHARSET"),
help=_("file to export the current character set"),
)
arg_parser.add_argument(
"-c",
"--import-charset",
metavar=_("IMPORT-CHARSET"),
help=_("file with the character set to use"),
)
###
arg_parser.add_argument(
"-S",
"--slice-texts",
action="store_true",
default=False,
help=_("The text string will be sliced between two banks"),
)
arg_parser.add_argument(
"-trim",
"--trim-interpreter",
action="store_true",
help=_("exclude code of unused commands"),
)
arg_parser.add_argument(
"-code",
"--show-bytecode",
action="store_true",
help=_("show the generated bytecode"),
)
##
arg_parser.add_argument(
"model",
choices=["48k", "128k", "plus3"],
help=_("Model of spectrum to target"),
type=str.lower,
default="plus3",
)
arg_parser.add_argument(
"csc_path",
type=file_path,
nargs="?",
metavar=_("CSC_PATH"),
help=_("path to csc executable"),
default=csc_path,
)
arg_parser.add_argument(
"sjasmplus_path",
nargs="?",
metavar=_("SJASMPLUS_PATH"),
type=file_path,
help=_("path to sjasmplus executable"),
default=sjasmplus_path,
)
arg_parser.add_argument(
"mkp3fs_path",
type=file_path,
nargs="?",
metavar=_("MKP3FS_PATH"),
help=_("path to mkp3fs executable"),
default=mkp3fs_path,
)
try:
args = arg_parser.parse_args()
except FileNotFoundError as f1:
sys.exit(_("ERROR: File not found:") + f"{f1}")
except NotADirectoryError as f2:
sys.exit(_("ERROR: Not a valid path:") + f"{f2}")
input_file = os.path.join(curr_path, f"{args.name}.cyd")
if not os.path.isfile(input_file):
sys.exit(_("ERROR: Input file does not exists."))
# Setting parameters
cydc_params = ["-csc", f"{args.csc_images_path}", "-pt3", f"{args.pt3_tracks_path}"]
if not os.path.isfile(args.tokens_file):
cydc_params = ["-T", f"{args.tokens_file}"] + cydc_params
else:
cydc_params = ["-t", f"{args.tokens_file}"] + cydc_params
if os.path.isfile(args.charset_file):
cydc_params = ["-c", f"{args.charset_file}"] + cydc_params
if os.path.isfile(args.sfx_asm_file):
cydc_params = ["-sfx", f"{args.sfx_asm_file}"] + cydc_params
if os.path.isfile(args.load_scr_file):
cydc_params = ["-scr", f"{args.load_scr_file}"] + cydc_params
if args.min_length:
cydc_params = ["-l", f"{args.min_length}"] + cydc_params
if args.max_length:
cydc_params = ["-L", f"{args.max_length}"] + cydc_params
if args.superset_limit:
cydc_params = ["-L", f"{args.superset_limit}"] + cydc_params
if args.verbose:
cydc_params = ["-v"] + cydc_params
if args.slice_texts:
cydc_params = ["-S"] + cydc_params
if args.trim_interpreter:
cydc_params = ["-trim"] + cydc_params
if args.show_bytecode:
cydc_params = ["-code"] + cydc_params
cydc_params = [cydc_path] + cydc_params
cydc_params += [
args.model,
input_file,
args.sjasmplus_path,
args.mkp3fs_path,
args.output_path,
]
# Compressing CSC files
print("Preparing images (if any)...")
for f in range(256):
scr_file_path = os.path.join(args.csc_images_path, f"{f:03d}.SCR")
csc_file_path = os.path.join(args.csc_images_path, f"{f:03d}.CSC")
do_csc = False
if os.path.isfile(scr_file_path):
if os.path.isfile(csc_file_path):
if os.path.getmtime(scr_file_path) >= os.path.getmtime(csc_file_path):
do_csc = True
else:
do_csc = True
if do_csc:
try:
run_exec(
csc_path,
[
f"-l={args.image_lines}",
f"-f",
f"-o={csc_file_path}",
scr_file_path,
],
)
except OSError as os1:
err = _("ERROR: Error running CSC.") + str(os1)
sys.exit(err)
try:
print("Compiling the script...")
run_exec(python_path, cydc_params)
except OSError as os1:
err = _("ERROR: Error running CYDC.") + str(os1)
sys.exit(err)
if args.model == "plus3":
print("Cleaning...")
files_to_clean = ["SCRIPT.DAT", "DISK", "CYD.BIN"]
for f in files_to_clean:
p = os.path.join(args.output_path, f)
if os.path.isfile(p):
os.remove(p)
sys.exit(0)
if __name__ == "__main__":
main()