-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.odin
More file actions
659 lines (624 loc) · 16 KB
/
Copy pathmain.odin
File metadata and controls
659 lines (624 loc) · 16 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package main
import "core:fmt"
import "core:os"
import "core:strings"
import "core:path/filepath"
import "core:strconv"
USAGE :: `Dethrace modding tools: FLI/FLC frames and encrypted game .TXT files.
Requires ffmpeg on PATH for FLI extraction. Repacking needs Aseprite (aseprite -b).
Configure paths once (stored in ~/.local/share/dethrace/mod/settings.ini):
drmod settings set game /path/to/CARMA
drmod settings set work /path/to/mod-workspace
Commands:
extract [anim_dir] [out_dir] [--file NAME]
pack <frames_dir> [output]
repack [work_dir] [anim_dir]
decode <input> [output] [--method auto|1|2] [--line-ending lf|crlf]
encode <input> [output] [--method auto|1|2] [--wrap] [--line-ending lf|crlf]
config get|set|keys ...
settings show|get|set|init|path ...
`
main :: proc() {
os.exit(run_cli(os.args))
}
run_cli :: proc(argv: []string) -> int {
if len(argv) < 2 {
fmt.println(USAGE)
return 1
}
cmd := argv[1]
rest := argv[2:]
switch cmd {
case "extract":
return cmd_extract(rest)
case "pack":
return cmd_pack(rest)
case "repack":
return cmd_repack(rest)
case "decode":
return cmd_decode(rest)
case "encode":
return cmd_encode(rest)
case "config":
return cmd_config(rest)
case "settings":
return cmd_settings(rest)
case "help", "-h", "--help":
fmt.println(USAGE)
return 0
case:
fmt.fprintf(os.stderr, "error: unknown command: %s\n", cmd)
fmt.println(USAGE)
return 1
}
}
Arg_Flags :: struct {
file: string,
method: Maybe(int),
line_ending: string,
wrap: bool,
force: bool,
}
parse_flags :: proc(args: ^[dynamic]string) -> Arg_Flags {
flags: Arg_Flags
flags.method = nil
flags.line_ending = ""
i := 0
for i < len(args) {
a := args[i]
if a == "--file" && i + 1 < len(args) {
flags.file = args[i + 1]
remove_args(args, i, 2)
continue
}
if a == "--method" && i + 1 < len(args) {
switch args[i + 1] {
case "auto":
flags.method = nil
case "1":
flags.method = 1
case "2":
flags.method = 2
}
remove_args(args, i, 2)
continue
}
if a == "--line-ending" && i + 1 < len(args) {
flags.line_ending = args[i + 1]
remove_args(args, i, 2)
continue
}
if a == "--wrap" {
flags.wrap = true
remove_args(args, i, 1)
continue
}
if a == "--force" {
flags.force = true
remove_args(args, i, 1)
continue
}
i += 1
}
return flags
}
line_ending_bytes :: proc(value: string, default: string) -> string {
switch value {
case "crlf":
return "\r\n"
case "lf":
return "\n"
case "":
return default
case:
return default
}
}
cmd_extract :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
anim_dir: string
out_root: string
if len(positional) > 0 {
anim_dir = positional[0]
} else {
dir, err := default_anim_dir()
if err.msg != "" {
print_settings_error(err)
return 1
}
anim_dir = dir
}
if len(positional) > 1 {
out_root = positional[1]
} else {
dir, err := default_fli_work_dir()
if err.msg != "" {
print_settings_error(err)
return 1
}
out_root = dir
}
if !os.is_dir(anim_dir) {
fmt.fprintf(os.stderr, "error: not a directory: %s\n", anim_dir)
return 1
}
fli_files: [dynamic]string
defer delete(fli_files)
if flags.file != "" {
single := join_path({anim_dir, flags.file}, context.temp_allocator)
if !os.is_file(single) {
fmt.fprintf(os.stderr, "error: file not found: %s\n", single)
return 1
}
append(&fli_files, strings.clone(single))
} else {
found, err := find_fli_files(anim_dir)
if err != nil {
fmt.fprintf(os.stderr, "error: %v\n", err)
return 1
}
defer delete(found)
if len(found) == 0 {
fmt.fprintf(os.stderr, "error: no FLI/FLC files in %s\n", anim_dir)
return 1
}
fli_files = found
}
failures := 0
for fli_path in fli_files {
stem := strings.trim_suffix(filepath.base(fli_path), filepath.ext(fli_path))
target := join_path({out_root, stem}, context.temp_allocator)
count, err := extract_fli(fli_path, target)
if err != nil {
failures += 1
if _, ok := find_executable("ffmpeg"); !ok {
fmt.fprintf(os.stderr, "%s: FAILED (ffmpeg not found on PATH; needed for FLI extraction)\n", filepath.base(fli_path))
} else {
fmt.fprintf(os.stderr, "%s: FAILED (%v)\n", filepath.base(fli_path), err)
}
continue
}
fmt.printf("%s: %d frame(s) -> %s/\n", filepath.base(fli_path), count, target)
}
return failures > 0 ? 1 : 0
}
cmd_pack :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
_ = parse_flags(&positional)
if len(positional) < 1 {
fmt.fprintf(os.stderr, "error: pack requires a frames_dir argument\n")
return 1
}
frames_dir, se := resolve_work_path(positional[0])
if se.msg != "" {
print_settings_error(se)
return 1
}
if len(positional) < 2 {
print_settings_error(settings_error("pack requires an output .FLI path"))
return 1
}
output := positional[1]
out_fli: string
if filepath.is_abs(output) {
out_fli = clean_path(output, context.temp_allocator)
} else {
resolved, re := resolve_game_path(output)
if re.msg != "" {
print_settings_error(re)
return 1
}
out_fli = resolved
}
if !os.is_dir(frames_dir) {
fmt.fprintf(os.stderr, "error: not a directory: %s\n", frames_dir)
return 1
}
if err := pack_frames(frames_dir, out_fli); err != nil {
if _, ok := find_executable("aseprite"); !ok {
fmt.fprintf(os.stderr, "error: Aseprite CLI not found on PATH (needed to write FLI/FLC)\n")
} else {
fmt.fprintf(os.stderr, "error: %v\n", err)
}
return 1
}
fmt.printf("packed -> %s\n", out_fli)
return 0
}
cmd_repack :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
_ = parse_flags(&positional)
work_root: string
anim_dir: string
if len(positional) > 0 {
work_root = positional[0]
} else {
dir, err := default_fli_work_dir()
if err.msg != "" {
print_settings_error(err)
return 1
}
work_root = dir
}
if len(positional) > 1 {
anim_dir = positional[1]
} else {
dir, err := default_anim_dir()
if err.msg != "" {
print_settings_error(err)
return 1
}
anim_dir = dir
}
if !os.is_dir(work_root) {
fmt.fprintf(os.stderr, "error: not a directory: %s\n", work_root)
return 1
}
os.make_directory_all(anim_dir)
entries, err := os.read_all_directory_by_path(work_root, context.temp_allocator)
if err != nil {
fmt.fprintf(os.stderr, "error: %v\n", err)
return 1
}
defer os.file_info_slice_delete(entries, context.temp_allocator)
subdirs := make([dynamic]string, context.temp_allocator)
for entry in entries {
if entry.type == .Directory {
append(&subdirs, entry.name)
}
}
if len(subdirs) == 0 {
fmt.fprintf(os.stderr, "error: no extracted animation folders in %s\n", work_root)
return 1
}
failures := 0
for name in subdirs {
subdir := join_path({work_root, name}, context.temp_allocator)
out_fli := join_path({anim_dir, fmt.tprintf("%s.FLI", name)}, context.temp_allocator)
if pack_err := pack_frames(subdir, out_fli); pack_err != nil {
failures += 1
fmt.fprintf(os.stderr, "%s: skipped (%v)\n", name, pack_err)
continue
}
fmt.printf("%s: packed -> %s\n", name, out_fli)
}
return failures > 0 ? 1 : 0
}
default_decoded_path :: proc(src: string, allocator := context.allocator) -> string {
ext := filepath.ext(src)
stem := strings.trim_suffix(filepath.base(src), ext)
dir := filepath.dir(src)
return join_path({dir, fmt.tprintf("%s.plain%s", stem, ext)}, allocator)
}
default_encoded_path :: proc(src: string, allocator := context.allocator) -> string {
base := filepath.base(src)
if strings.has_suffix(base, ".plain.TXT") {
new_base, _ := strings.replace(base, ".plain.TXT", ".TXT", 1)
return join_path({filepath.dir(src), new_base}, allocator)
}
if strings.has_suffix(base, ".plain.txt") {
new_base, _ := strings.replace(base, ".plain.txt", ".txt", 1)
return join_path({filepath.dir(src), new_base}, allocator)
}
ext := filepath.ext(src)
stem := strings.trim_suffix(base, ext)
return join_path({filepath.dir(src), fmt.tprintf("%s.encoded.txt", stem)}, allocator)
}
cmd_decode :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
if len(positional) < 1 {
fmt.fprintf(os.stderr, "error: decode requires an input file\n")
return 1
}
src, se := resolve_game_path(positional[0], true)
if se.msg != "" {
print_settings_error(se)
return 1
}
dst: string
if len(positional) > 1 {
if filepath.is_abs(positional[1]) {
dst = clean_path(positional[1], context.temp_allocator)
} else {
resolved, re := resolve_work_path(positional[1])
if re.msg != "" {
print_settings_error(re)
return 1
}
dst = resolved
}
} else {
resolved, re := resolve_work_path(fmt.tprintf("%s.plain%s", strings.trim_suffix(filepath.base(src), filepath.ext(src)), filepath.ext(src)))
if re.msg != "" {
print_settings_error(re)
return 1
}
dst = resolved
}
if !os.is_file(src) {
fmt.fprintf(os.stderr, "error: file not found: %s\n", src)
return 1
}
ending := line_ending_bytes(flags.line_ending, "\n")
count, err := decode_file(src, dst, ending, flags.method)
if err != nil {
fmt.fprintf(os.stderr, "error: %v\n", err)
return 1
}
fmt.printf("decoded %d line(s) -> %s\n", count, dst)
return 0
}
cmd_encode :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
if len(positional) < 1 {
fmt.fprintf(os.stderr, "error: encode requires an input file\n")
return 1
}
src: string
if filepath.is_abs(positional[0]) {
src = clean_path(positional[0], context.temp_allocator)
} else {
resolved, re := resolve_work_path(positional[0])
if re.msg != "" {
print_settings_error(re)
return 1
}
src = resolved
}
dst: string
if len(positional) > 1 {
if filepath.is_abs(positional[1]) {
dst = clean_path(positional[1], context.temp_allocator)
} else {
resolved, re := resolve_game_path(positional[1])
if re.msg != "" {
print_settings_error(re)
return 1
}
dst = resolved
}
} else {
encoded_name := filepath.base(default_encoded_path(src, context.temp_allocator))
resolved, re := resolve_game_path(encoded_name)
if re.msg != "" {
print_settings_error(re)
return 1
}
dst = resolved
}
if !os.is_file(src) {
fmt.fprintf(os.stderr, "error: file not found: %s\n", src)
return 1
}
ending := line_ending_bytes(flags.line_ending, DEFAULT_LINE_ENDING)
count, err := encode_file(src, dst, ending, flags.method, flags.wrap)
if err != nil {
fmt.fprintf(os.stderr, "error: %v\n", err)
return 1
}
fmt.printf("encoded %d line(s) -> %s\n", count, dst)
return 0
}
cmd_settings :: proc(args: []string) -> int {
if len(args) < 1 {
fmt.fprintf(os.stderr, "error: settings subcommand required\n")
return 1
}
sub := args[0]
rest := args[1:]
switch sub {
case "show":
return cmd_settings_show(rest)
case "get":
return cmd_settings_get(rest)
case "set":
return cmd_settings_set(rest)
case "init":
return cmd_settings_init(rest)
case "path":
return cmd_settings_path(rest)
case:
fmt.fprintf(os.stderr, "error: unknown settings command: %s\n", sub)
return 1
}
}
cmd_settings_show :: proc(args: []string) -> int {
_ = args
path := settings_path(context.temp_allocator)
game, has_game := game_dir()
work, has_work := work_dir()
fmt.printf("settings: %s\n", path)
if has_game {
fmt.printf(" game = %s\n", game)
} else {
fmt.println(" game = (not set)")
}
if has_work {
fmt.printf(" work = %s\n", work)
} else {
fmt.println(" work = (not set)")
}
if has_game {
anim := join_path({game, "ANIM"}, context.temp_allocator)
missing := os.is_dir(anim) ? "" : " (missing)"
fmt.printf(" anim = %s%s\n", anim, missing)
}
if has_work {
fli_work := join_path({work, "fli_work"}, context.temp_allocator)
missing := os.is_dir(fli_work) ? "" : " (will be created on extract)"
fmt.printf(" fli_work = %s%s\n", fli_work, missing)
}
return 0
}
cmd_settings_set :: proc(args: []string) -> int {
if len(args) < 2 {
fmt.fprintf(os.stderr, "error: settings set requires key and path\n")
return 1
}
key, ok := normalize_setting_key(args[0])
if !ok || (key != GAME_KEY && key != WORK_KEY) {
fmt.fprintf(os.stderr, "error: key must be 'game' or 'work'\n")
return 1
}
target := clean_path(args[1], context.temp_allocator)
if !os.is_dir(target) {
fmt.fprintf(os.stderr, "error: not a directory: %s\n", target)
return 1
}
saved, err := set_path_setting(key, target)
if err.msg != "" {
fmt.fprintf(os.stderr, "error: %s\n", err.msg)
return 1
}
fmt.printf("%s = %s\n", key, target)
fmt.printf("saved -> %s\n", saved)
return 0
}
cmd_settings_get :: proc(args: []string) -> int {
if len(args) < 1 {
fmt.fprintf(os.stderr, "error: settings get requires a key\n")
return 1
}
value, err := get_setting_value(args[0])
if err.msg != "" {
fmt.fprintf(os.stderr, "error: %s\n", err.msg)
return 1
}
if value == "" {
canonical, _ := normalize_setting_key(args[0])
hint := "drmod settings init"
switch canonical {
case GAME_KEY, WORK_KEY:
hint = fmt.tprintf("drmod settings set %s <path>", canonical)
case DERIVED_ANIM:
hint = "drmod settings set game <path>"
case DERIVED_FLI_WORK:
hint = "drmod settings set work <path>"
}
fmt.fprintf(os.stderr, "error: %s is not set; run: %s\n", canonical != "" ? canonical : args[0], hint)
return 1
}
fmt.println(value)
return 0
}
cmd_settings_init :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
path, err := init_settings(flags.force)
if err != nil {
fmt.fprintf(os.stderr, "error: %v\n", err)
return 1
}
fmt.printf("settings file: %s\n", path)
fmt.println("Run: drmod settings set game /path/to/CARMA")
fmt.println(" drmod settings set work /path/to/mod-workspace")
return 0
}
cmd_settings_path :: proc(args: []string) -> int {
_ = args
fmt.println(settings_path(context.temp_allocator))
return 0
}
cmd_config :: proc(args: []string) -> int {
if len(args) < 1 {
fmt.fprintf(os.stderr, "error: config subcommand required\n")
return 1
}
sub := args[0]
rest := args[1:]
switch sub {
case "get":
return cmd_config_get(rest)
case "set":
return cmd_config_set(rest)
case "keys":
return cmd_config_keys(rest)
case:
fmt.fprintf(os.stderr, "error: unknown config command: %s\n", sub)
return 1
}
}
cmd_config_get :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
if len(positional) < 2 {
fmt.fprintf(os.stderr, "error: config get requires file and key\n")
return 1
}
value, err := config_get(positional[0], positional[1], flags.method)
if err.msg != "" {
print_config_error(err)
return 1
}
defer delete(value)
fmt.println(value)
return 0
}
cmd_config_set :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
if len(positional) < 3 {
fmt.fprintf(os.stderr, "error: config set requires file, key, and value\n")
return 1
}
saved, err := config_set(positional[0], positional[1], positional[2], flags.method, flags.wrap)
if err.msg != "" {
print_config_error(err)
return 1
}
fmt.println(saved)
return 0
}
cmd_config_keys :: proc(args: []string) -> int {
positional := make([dynamic]string, context.temp_allocator)
for a in args {
append(&positional, a)
}
flags := parse_flags(&positional)
if len(positional) < 1 {
fmt.fprintf(os.stderr, "error: config keys requires a file\n")
return 1
}
keys, err := config_keys(positional[0], flags.method)
if err.msg != "" {
print_config_error(err)
return 1
}
defer {
for k in keys {
delete(k)
}
delete(keys)
}
for key in keys {
fmt.println(key)
}
return 0
}