-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlua_bake.cpp
More file actions
1134 lines (1071 loc) · 42.2 KB
/
Copy pathlua_bake.cpp
File metadata and controls
1134 lines (1071 loc) · 42.2 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SacredSDK — Lua-driven script baker.
//
// Embeds Lua 5.4 inside the DLL. On attach we spawn a worker that scans
// `<game>/custom/lua/**.lua`, executes each file through Lua, expects it to
// return a list-of-records table, assembles the bytes into a FunkCode .bin,
// and writes the result to `<game>/custom/<mirrored-path>.bin` where
// fs_override picks it up at the next file open.
//
// .lua file shape
// ---------------
// Each .lua mod returns a list. Each record is `{ tag, flags, op1, op2, ... }`.
// Each op is `{ "LABEL", arg1, arg2, ... }`. Arg conventions per opcode kind:
//
// stack/halt/const-w0 : no args
// const w=1..16 : integer args (1, 1, 1, 2, 3, 4) of the obvious widths;
// for w=3 (FMT3) the single arg is a 3-byte Lua string.
// cstr1 : one string
// cstr2 : two strings
// cstr1+1 / cstr1+5 : string + 1-or-5-byte Lua string (the trailer)
// u32+cstr1 : integer + string
// u32+cstr2 : integer + two strings
//
// Strings are Lua byte-strings; we treat them as latin1. NUL is forbidden
// inside a cstring (the bytecode is null-terminated).
#include "sdk.h"
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <string>
#include <vector>
#include <unordered_map>
extern "C" {
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
}
namespace sdk { namespace lua_bake {
volatile long g_baked_files = 0;
volatile long g_baked_records = 0;
volatile bool g_busy = false;
static char g_last_status[512] = "(not run yet)";
const char* status() { return g_last_status; }
long baked_files() { return g_baked_files; }
long baked_records() { return g_baked_records; }
bool busy() { return g_busy; }
static void set_status(const char* fmt, ...) {
va_list ap; va_start(ap, fmt);
vsnprintf(g_last_status, sizeof(g_last_status), fmt, ap);
va_end(ap);
sdk_log("[lua_bake] %s", g_last_status);
}
// --- opcode table (port of funkcode_ops.LABEL_TO_OP) ----------------------
enum Kind : uint8_t {
KIND_UNKNOWN = 0,
KIND_STACK, // 1 byte (op)
KIND_HALT, // 1 byte (op)
KIND_CONST, // 1 + width bytes
KIND_CSTR1, // 1 + null-terminated string
KIND_CSTR2, // 1 + two null-terminated strings
KIND_CSTR1_1, // 1 + null-terminated string + 1 raw byte
KIND_CSTR1_5, // 1 + null-terminated string + 5 raw bytes
KIND_U32_CSTR1, // 1 + 4-byte u32 + null-terminated string
KIND_U32_CSTR2, // 1 + 4-byte u32 + two null-terminated strings
};
struct OpInfo {
const char* label;
uint8_t opcode;
Kind kind;
uint8_t width;
};
// Opcode table — the single source of truth shared with funkcode.cpp.
// (Historically generated; now maintained by hand in lua_bake_opcodes.inc,
// which is checked in. Keep it in sync with the FunkCode disassembler.)
static const OpInfo OP_TABLE[] = {
#include "lua_bake_opcodes.inc"
};
static const int OP_TABLE_N = sizeof(OP_TABLE) / sizeof(OP_TABLE[0]);
static std::unordered_map<std::string, const OpInfo*> g_label_map;
static void ensure_label_map() {
if (!g_label_map.empty()) return;
g_label_map.reserve(OP_TABLE_N * 2);
for (int i = 0; i < OP_TABLE_N; i++) {
g_label_map[OP_TABLE[i].label] = &OP_TABLE[i];
}
}
// --- encoder helpers ------------------------------------------------------
static void put_u32_le(std::string& out, uint32_t v) {
out.push_back((char)(v & 0xff));
out.push_back((char)((v >> 8) & 0xff));
out.push_back((char)((v >> 16) & 0xff));
out.push_back((char)((v >> 24) & 0xff));
}
// Read one Lua arg from the stack slot `idx`. Coerces:
// - integer / number -> int64_t
// - string -> bytes (sets `is_str=true`)
static bool read_arg_str(lua_State* L, int idx, std::string& out, bool& is_str) {
int t = lua_type(L, idx);
if (t == LUA_TSTRING) {
size_t n = 0;
const char* p = lua_tolstring(L, idx, &n);
out.assign(p, n);
is_str = true;
return true;
}
if (t == LUA_TNUMBER) {
is_str = false;
return true;
}
return false;
}
// Assemble one opcode to bytes. `args_start_idx` is the absolute stack index
// of the first arg (i.e. op-table[2] etc.) and `n_args` is how many.
static bool assemble_op_from_stack(lua_State* L, const OpInfo* info,
int op_table_idx, int n_args_total,
std::string& out, char err[256])
{
out.push_back((char)info->opcode);
int n_args = n_args_total - 1; // first is the label
auto getarg = [&](int k) {
// op_table_idx is the Lua-stack absolute index of the op table; arg k
// (1-based across the op contents starting at the LABEL=1) lives at
// op_table[1 + k]. We push and let caller pop.
lua_rawgeti(L, op_table_idx, 1 + k);
};
switch (info->kind) {
case KIND_STACK:
case KIND_HALT:
if (n_args != 0) {
_snprintf_s(err, 256, _TRUNCATE, "%s: expected 0 args, got %d",
info->label, n_args);
return false;
}
return true;
case KIND_CONST: {
int w = info->width;
if (w == 0) {
if (n_args != 0) {
_snprintf_s(err, 256, _TRUNCATE, "%s: expected 0 args, got %d",
info->label, n_args);
return false;
}
return true;
}
if (w == 3) {
// single 3-byte string arg
if (n_args != 1) {
_snprintf_s(err, 256, _TRUNCATE, "%s: needs 1 byte-string arg of length 3",
info->label);
return false;
}
getarg(1);
size_t n = 0;
const char* p = lua_tolstring(L, -1, &n);
if (!p || n != 3) {
_snprintf_s(err, 256, _TRUNCATE, "%s: 3-byte string arg required (got %zu)",
info->label, n);
lua_pop(L, 1);
return false;
}
out.append(p, 3);
lua_pop(L, 1);
return true;
}
// numeric widths: 1, 2, 4, 8, 12, 16
int expect = (w == 1 || w == 2 || w == 4) ? 1
: (w == 8) ? 2
: (w == 12) ? 3
: (w == 16) ? 4 : 0;
if (expect == 0) {
_snprintf_s(err, 256, _TRUNCATE, "%s: unhandled width %d", info->label, w);
return false;
}
if (n_args != expect) {
_snprintf_s(err, 256, _TRUNCATE, "%s: needs %d numeric args, got %d",
info->label, expect, n_args);
return false;
}
if (w == 1) {
getarg(1);
uint8_t v = (uint8_t)(lua_tointeger(L, -1) & 0xff);
lua_pop(L, 1);
out.push_back((char)v);
} else if (w == 2) {
getarg(1);
uint16_t v = (uint16_t)(lua_tointeger(L, -1) & 0xffff);
lua_pop(L, 1);
out.push_back((char)(v & 0xff));
out.push_back((char)((v >> 8) & 0xff));
} else {
// multi-u32: emit `expect` u32s LE
for (int k = 1; k <= expect; k++) {
getarg(k);
uint32_t v = (uint32_t)lua_tointeger(L, -1);
lua_pop(L, 1);
put_u32_le(out, v);
}
}
return true;
}
case KIND_CSTR1:
case KIND_CSTR2: {
int expect = (info->kind == KIND_CSTR1) ? 1 : 2;
if (n_args != expect) {
_snprintf_s(err, 256, _TRUNCATE, "%s: needs %d string arg(s), got %d",
info->label, expect, n_args);
return false;
}
for (int k = 1; k <= expect; k++) {
getarg(k);
size_t n = 0;
const char* p = lua_tolstring(L, -1, &n);
if (!p) {
_snprintf_s(err, 256, _TRUNCATE, "%s: arg %d must be a string", info->label, k);
lua_pop(L, 1);
return false;
}
out.append(p, n);
out.push_back('\0');
lua_pop(L, 1);
}
return true;
}
case KIND_CSTR1_1:
case KIND_CSTR1_5: {
int tail_len = (info->kind == KIND_CSTR1_1) ? 1 : 5;
if (n_args != 2) {
_snprintf_s(err, 256, _TRUNCATE, "%s: needs (string, tail-string)", info->label);
return false;
}
getarg(1);
size_t n = 0;
const char* p = lua_tolstring(L, -1, &n);
if (!p) {
_snprintf_s(err, 256, _TRUNCATE, "%s: arg 1 must be a string", info->label);
lua_pop(L, 1);
return false;
}
out.append(p, n);
out.push_back('\0');
lua_pop(L, 1);
getarg(2);
n = 0;
p = lua_tolstring(L, -1, &n);
if (!p || (int)n != tail_len) {
_snprintf_s(err, 256, _TRUNCATE, "%s: tail must be %d-byte string (got %zu)",
info->label, tail_len, n);
lua_pop(L, 1);
return false;
}
out.append(p, tail_len);
lua_pop(L, 1);
return true;
}
case KIND_U32_CSTR1:
case KIND_U32_CSTR2: {
int expect_strs = (info->kind == KIND_U32_CSTR1) ? 1 : 2;
if (n_args != 1 + expect_strs) {
_snprintf_s(err, 256, _TRUNCATE, "%s: needs u32 + %d string(s)",
info->label, expect_strs);
return false;
}
getarg(1);
uint32_t v = (uint32_t)lua_tointeger(L, -1);
lua_pop(L, 1);
put_u32_le(out, v);
for (int k = 0; k < expect_strs; k++) {
getarg(2 + k);
size_t n = 0;
const char* p = lua_tolstring(L, -1, &n);
if (!p) {
_snprintf_s(err, 256, _TRUNCATE, "%s: string arg required", info->label);
lua_pop(L, 1);
return false;
}
out.append(p, n);
out.push_back('\0');
lua_pop(L, 1);
}
return true;
}
default:
_snprintf_s(err, 256, _TRUNCATE, "%s: unknown kind %d", info->label, info->kind);
return false;
}
}
// --- decoder: bytes -> the record tables the baker eats --------------------
//
// The exact inverse of assemble_op_from_stack, row for row, so that
// `sacred.disasm(bytes)` hands a mod the same structure a pre-decompiled
// snapshot would have (`{ tag, flags, {LABEL, args...}, ... }`) and the mod can
// return it straight back to be baked. That is what lets `vanilla.load` work
// out of the box, with no snapshot files anywhere.
//
// Two rules keep it honest:
// * every record is VERIFIED by re-encoding it with the encoder above and
// comparing bytes. A record the vocabulary cannot spell exactly comes back
// as `{tag, flags, {"_HEX", "<payload>"}}`, which bakes back byte-identical.
// * the opcode table is the same OP_TABLE the encoder uses, so decode and
// encode can never drift apart.
static const OpInfo* g_op_by_code[256];
static bool g_op_by_code_ready = false;
static void ensure_op_index() {
if (g_op_by_code_ready) return;
for (int i = 0; i < 256; i++) g_op_by_code[i] = nullptr;
// First row wins: a few labels share an opcode, and the re-encode check
// decides whether the choice was right for this record.
for (int i = 0; i < OP_TABLE_N; i++) {
uint8_t c = OP_TABLE[i].opcode;
if (!g_op_by_code[c]) g_op_by_code[c] = &OP_TABLE[i];
}
g_op_by_code_ready = true;
}
static uint32_t le32_at(const uint8_t* p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
// Read an ASCIIZ starting at ip. False when it runs off the end.
static bool read_cstr_at(const uint8_t* p, size_t n, size_t& ip, const char*& out, size_t& len) {
size_t start = ip;
while (ip < n && p[ip] != 0) ip++;
if (ip >= n) return false;
out = (const char*)p + start;
len = ip - start;
ip++; // the terminator
return true;
}
// Decode one op at p[ip] and push { LABEL, args... }. On failure nothing is
// left on the stack and ip is unchanged.
static bool decode_op_to_lua(lua_State* L, const uint8_t* p, size_t n, size_t& ip) {
ensure_op_index();
const OpInfo* info = g_op_by_code[p[ip]];
if (!info) return false;
size_t save = ip;
ip++;
lua_newtable(L);
int t = lua_gettop(L);
lua_pushstring(L, info->label);
lua_rawseti(L, t, 1);
int slot = 2;
bool ok = true;
switch (info->kind) {
case KIND_STACK:
case KIND_HALT:
break;
case KIND_CONST: {
int w = info->width;
if (w == 0) break;
if (w == 3) {
if (ip + 3 > n) { ok = false; break; }
lua_pushlstring(L, (const char*)p + ip, 3);
lua_rawseti(L, t, slot++);
ip += 3;
break;
}
if (w == 1) {
if (ip + 1 > n) { ok = false; break; }
lua_pushinteger(L, (lua_Integer)p[ip]);
lua_rawseti(L, t, slot++);
ip += 1;
break;
}
if (w == 2) {
if (ip + 2 > n) { ok = false; break; }
lua_pushinteger(L, (lua_Integer)((uint32_t)p[ip] | ((uint32_t)p[ip + 1] << 8)));
lua_rawseti(L, t, slot++);
ip += 2;
break;
}
int cnt = (w == 4) ? 1 : (w == 8) ? 2 : (w == 12) ? 3 : (w == 16) ? 4 : 0;
if (cnt == 0 || ip + 4 * (size_t)cnt > n) { ok = false; break; }
for (int k = 0; k < cnt; k++) {
lua_pushinteger(L, (lua_Integer)le32_at(p + ip));
lua_rawseti(L, t, slot++);
ip += 4;
}
break;
}
case KIND_CSTR1:
case KIND_CSTR2: {
int cnt = (info->kind == KIND_CSTR1) ? 1 : 2;
for (int k = 0; k < cnt && ok; k++) {
const char* sp; size_t sl;
if (!read_cstr_at(p, n, ip, sp, sl)) { ok = false; break; }
lua_pushlstring(L, sp, sl);
lua_rawseti(L, t, slot++);
}
break;
}
case KIND_CSTR1_1:
case KIND_CSTR1_5: {
size_t tail = (info->kind == KIND_CSTR1_1) ? 1 : 5;
const char* sp; size_t sl;
if (!read_cstr_at(p, n, ip, sp, sl)) { ok = false; break; }
lua_pushlstring(L, sp, sl);
lua_rawseti(L, t, slot++);
if (ip + tail > n) { ok = false; break; }
lua_pushlstring(L, (const char*)p + ip, tail);
lua_rawseti(L, t, slot++);
ip += tail;
break;
}
case KIND_U32_CSTR1:
case KIND_U32_CSTR2: {
if (ip + 4 > n) { ok = false; break; }
lua_pushinteger(L, (lua_Integer)le32_at(p + ip));
lua_rawseti(L, t, slot++);
ip += 4;
int cnt = (info->kind == KIND_U32_CSTR1) ? 1 : 2;
for (int k = 0; k < cnt && ok; k++) {
const char* sp; size_t sl;
if (!read_cstr_at(p, n, ip, sp, sl)) { ok = false; break; }
lua_pushlstring(L, sp, sl);
lua_rawseti(L, t, slot++);
}
break;
}
default:
ok = false;
break;
}
if (!ok) {
lua_settop(L, t - 1);
ip = save;
return false;
}
return true;
}
static void push_hex_record(lua_State* L, uint8_t tag, const uint8_t* payload, size_t plen) {
static const char* HEX = "0123456789abcdef";
lua_newtable(L);
int rec = lua_gettop(L);
lua_pushinteger(L, (lua_Integer)tag);
lua_rawseti(L, rec, 1);
lua_pushinteger(L, (lua_Integer)(plen ? payload[0] : 0));
lua_rawseti(L, rec, 2);
lua_newtable(L);
lua_pushstring(L, "_HEX");
lua_rawseti(L, -2, 1);
std::string hex;
if (plen > 1) {
hex.reserve((plen - 1) * 2);
for (size_t i = 1; i < plen; i++) {
hex.push_back(HEX[payload[i] >> 4]);
hex.push_back(HEX[payload[i] & 0xF]);
}
}
lua_pushlstring(L, hex.data(), hex.size());
lua_rawseti(L, -2, 2);
lua_rawseti(L, rec, 3);
}
// Push one record table for `payload` (which starts with the flags byte).
// Returns true when it was spelled in mnemonics, false when it came back as
// _HEX. Either way exactly one table is left on the stack.
static bool push_record(lua_State* L, uint8_t tag, const uint8_t* payload, size_t plen) {
if (plen == 0) { // no flags byte: nothing to spell
push_hex_record(L, tag, payload, plen);
return false;
}
lua_newtable(L);
int rec = lua_gettop(L);
lua_pushinteger(L, (lua_Integer)tag);
lua_rawseti(L, rec, 1);
lua_pushinteger(L, (lua_Integer)payload[0]);
lua_rawseti(L, rec, 2);
bool ok = true;
int slot = 3;
size_t ip = 1;
while (ip < plen) {
if (!decode_op_to_lua(L, payload, plen, ip)) { ok = false; break; }
lua_rawseti(L, rec, slot++);
}
// Verify: assemble what we just built and demand the same bytes back.
if (ok) {
ensure_label_map();
std::string check;
check.push_back((char)payload[0]);
for (int j = 3; j < slot && ok; j++) {
lua_rawgeti(L, rec, j);
int op_idx = lua_gettop(L);
lua_rawgeti(L, op_idx, 1);
const char* label = lua_tostring(L, -1);
auto it = g_label_map.find(label ? label : "");
lua_pop(L, 1);
char op_err[256];
if (it == g_label_map.end() ||
!assemble_op_from_stack(L, it->second, op_idx,
(int)lua_rawlen(L, op_idx), check, op_err)) {
ok = false;
}
lua_pop(L, 1);
}
if (ok && (check.size() != plen || memcmp(check.data(), payload, plen) != 0)) ok = false;
}
if (!ok) {
lua_settop(L, rec - 1);
push_hex_record(L, tag, payload, plen);
return false;
}
return true;
}
// sacred.disasm(bytes) -> records, stats
//
// `bytes` is a whole FunkCode/QuestCode/StartCode blob, as sacred.read_file
// returns it. The result is the table the baker consumes, so:
//
// local raw = sacred.read_file("bin/TYPE_NPC_SERAPHIM/FunkCode.bin")
// local recs = sacred.disasm(raw) -- no snapshot file needed
// ... -- rewrite what you like
// return recs
//
// stats = { records = N, mnemonic = N, hex = N, bytes = N }.
static int l_sacred_disasm(lua_State* L) {
size_t n = 0;
const char* data = luaL_checklstring(L, 1, &n);
const uint8_t* b = (const uint8_t*)data;
lua_newtable(L);
int arr = lua_gettop(L);
int count = 0, mnem = 0, hex = 0;
size_t off = 0;
while (off + 3 <= n) {
uint8_t tag = b[off];
size_t size = ((size_t)b[off + 1] << 8) | (size_t)b[off + 2];
if (size < 3 || off + size > n) {
return luaL_error(L, "sacred.disasm: bad record at offset %d (tag 0x%02x, size %d)",
(int)off, (int)tag, (int)size);
}
if (!lua_checkstack(L, 8)) return luaL_error(L, "sacred.disasm: out of Lua stack");
if (push_record(L, tag, b + off + 3, size - 3)) mnem++; else hex++;
lua_rawseti(L, arr, ++count);
off += size;
}
if (off != n) {
return luaL_error(L, "sacred.disasm: %d trailing byte(s) after the last record",
(int)(n - off));
}
sdk_log("[lua_bake] sacred.disasm: %d records (%d mnemonic, %d hex) from %d bytes",
count, mnem, hex, (int)n);
lua_newtable(L);
lua_pushinteger(L, count); lua_setfield(L, -2, "records");
lua_pushinteger(L, mnem); lua_setfield(L, -2, "mnemonic");
lua_pushinteger(L, hex); lua_setfield(L, -2, "hex");
lua_pushinteger(L, (lua_Integer)n); lua_setfield(L, -2, "bytes");
return 2;
}
// --- main bake: take the table on top of stack, produce .bin bytes --------
static bool table_to_bytes(lua_State* L, std::string& out, char err[256]) {
if (lua_type(L, -1) != LUA_TTABLE) {
_snprintf_s(err, 256, _TRUNCATE, "script must return a table (got %s)",
luaL_typename(L, -1));
return false;
}
int n_records = (int)lua_rawlen(L, -1);
int rec_count = 0;
for (int i = 1; i <= n_records; i++) {
lua_rawgeti(L, -1, i); // push record
int rec_idx = lua_gettop(L);
if (lua_type(L, -1) != LUA_TTABLE) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d not a table (got %s)", i, luaL_typename(L, -1));
lua_pop(L, 1);
return false;
}
int rec_len = (int)lua_rawlen(L, -1);
if (rec_len < 2) {
_snprintf_s(err, 256, _TRUNCATE, "record %d too short (need tag+flags)", i);
lua_pop(L, 1);
return false;
}
lua_rawgeti(L, rec_idx, 1);
uint8_t tag = (uint8_t)lua_tointeger(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, rec_idx, 2);
uint8_t flags = (uint8_t)lua_tointeger(L, -1);
lua_pop(L, 1);
std::string payload;
payload.push_back((char)flags);
// Each remaining element is an op table.
for (int j = 3; j <= rec_len; j++) {
lua_rawgeti(L, rec_idx, j);
int op_idx = lua_gettop(L);
if (lua_type(L, -1) != LUA_TTABLE) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d not a table", i, j - 2);
lua_pop(L, 1); lua_pop(L, 1);
return false;
}
int op_total = (int)lua_rawlen(L, -1);
if (op_total < 1) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d empty (need label)", i, j - 2);
lua_pop(L, 1); lua_pop(L, 1);
return false;
}
lua_rawgeti(L, op_idx, 1);
const char* label = lua_tostring(L, -1);
if (!label) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d label not a string", i, j - 2);
lua_pop(L, 1); lua_pop(L, 1); lua_pop(L, 1);
return false;
}
// `_HEX` pseudo-op: copy raw hex-encoded bytes verbatim. Used by
// the Python decompiler for opcodes that can't be cleanly mnemonized
// (so round-trip stays byte-perfect). Lua mods can also write
// `{"_HEX", "deadbeef"}` to inject literal bytes.
if (strcmp(label, "_HEX") == 0) {
lua_pop(L, 1); // pop label
if (op_total != 2) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d: _HEX needs exactly 1 hex string arg",
i, j - 2);
lua_pop(L, 1); lua_pop(L, 1);
return false;
}
lua_rawgeti(L, op_idx, 2);
size_t n = 0;
const char* hp = lua_tolstring(L, -1, &n);
if (!hp || (n & 1)) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d: _HEX arg must be even-length hex string",
i, j - 2);
lua_pop(L, 1); lua_pop(L, 1); lua_pop(L, 1);
return false;
}
for (size_t k = 0; k < n; k += 2) {
auto hd = [](char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
};
int hi = hd(hp[k]);
int lo = hd(hp[k + 1]);
if (hi < 0 || lo < 0) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d: bad hex byte at %zu", i, j - 2, k);
lua_pop(L, 1); lua_pop(L, 1); lua_pop(L, 1);
return false;
}
payload.push_back((char)((hi << 4) | lo));
}
lua_pop(L, 1); // pop hex string
lua_pop(L, 1); // pop op table
continue;
}
auto it = g_label_map.find(label);
if (it == g_label_map.end()) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d: unknown opcode label '%s'",
i, j - 2, label);
lua_pop(L, 1); lua_pop(L, 1); lua_pop(L, 1);
return false;
}
lua_pop(L, 1); // pop label string
char op_err[256];
if (!assemble_op_from_stack(L, it->second, op_idx, op_total, payload, op_err)) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d op %d: %s", i, j - 2, op_err);
lua_pop(L, 1); lua_pop(L, 1);
return false;
}
lua_pop(L, 1); // pop op table
}
// Emit record header: tag, size (big-endian), payload.
int size = 3 + (int)payload.size();
if (size > 0xFFFF) {
_snprintf_s(err, 256, _TRUNCATE,
"record %d payload too large (%zu bytes)", i, payload.size());
lua_pop(L, 1);
return false;
}
out.push_back((char)tag);
out.push_back((char)((size >> 8) & 0xff));
out.push_back((char)(size & 0xff));
out.append(payload);
lua_pop(L, 1); // pop record
rec_count++;
}
InterlockedExchangeAdd(&g_baked_records, rec_count);
return true;
}
// --- file walker ----------------------------------------------------------
// Mods are read from TWO trees, in priority order:
//
// <game>/custom/lua the player's own mods -- these WIN
// <game>/sdk/custom/lua the framework that ships with the SDK
//
// A file present in both is baked from the player's tree only, so a mod can
// replace a shipped one by putting a file at the same relative path. Bake
// OUTPUT always goes to <game>/custom/<rel>.bin: that is the tree fs_override
// serves to the engine, and it keeps the distributed sdk/ tree read-only.
static void resolve_dirs(char user_lua[MAX_PATH], char sdk_lua[MAX_PATH],
char out_dir[MAX_PATH]) {
char exe[MAX_PATH] = {0};
GetModuleFileNameA(g_attach.exe_module, exe, MAX_PATH);
char* slash = strrchr(exe, '\\'); if (slash) *slash = 0;
_snprintf_s(user_lua, MAX_PATH, _TRUNCATE, "%s\\custom\\lua", exe);
_snprintf_s(sdk_lua, MAX_PATH, _TRUNCATE, "%s\\sdk\\custom\\lua", exe);
_snprintf_s(out_dir, MAX_PATH, _TRUNCATE, "%s\\custom", exe);
}
static bool file_exists(const char* path) {
DWORD a = GetFileAttributesA(path);
return a != INVALID_FILE_ATTRIBUTES && !(a & FILE_ATTRIBUTE_DIRECTORY);
}
static void mkdirs(const char* path) {
char dir[MAX_PATH];
strncpy_s(dir, _TRUNCATE, path, _TRUNCATE);
for (char* p = dir + 1; *p; p++) {
if (*p == '\\' || *p == '/') {
char saved = *p;
*p = 0;
CreateDirectoryA(dir, nullptr);
*p = saved;
}
}
}
// Lua-side `sacred.log("msg")` -> appends to sdk_log + the overlay ring.
static int l_sacred_log(lua_State* L) {
const char* msg = luaL_checkstring(L, 1);
sdk_log("[lua] %s", msg);
return 0;
}
// Lua-side `sacred.read_file(rel)` -> reads bytes of `<game>/<rel>`, returns a
// Lua string of bytes. Used by lib/vanilla.lua to ingest raw .bin files.
static int l_sacred_read_file(lua_State* L) {
const char* rel = luaL_checkstring(L, 1);
char exe[MAX_PATH] = {0};
GetModuleFileNameA(g_attach.exe_module, exe, MAX_PATH);
char* slash = strrchr(exe, '\\'); if (slash) *slash = 0;
char abs[MAX_PATH];
_snprintf_s(abs, _TRUNCATE, "%s\\%s", exe, rel);
FILE* f = nullptr;
if (fopen_s(&f, abs, "rb") != 0 || !f) {
lua_pushnil(L);
lua_pushfstring(L, "open failed: %s (errno=%d)", abs, errno);
return 2;
}
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
std::string buf;
buf.resize((size_t)n);
fread(&buf[0], 1, (size_t)n, f);
fclose(f);
lua_pushlstring(L, buf.data(), buf.size());
return 1;
}
// Lua-side `sacred.write_file(rel, bytes)` -> writes bytes to `<game>/<rel>`.
// Restricted to the `custom/` tree so a buggy mod can never overwrite vanilla.
// Used by lib/text.lua to patch `custom/scripts/<lang>/global.res`.
static int l_sacred_write_file(lua_State* L) {
const char* rel = luaL_checkstring(L, 1);
size_t n = 0;
const char* data = luaL_checklstring(L, 2, &n);
if (strstr(rel, "..")) {
return luaL_error(L, "sacred.write_file: path traversal denied: %s", rel);
}
if (strncmp(rel, "custom\\", 7) != 0 && strncmp(rel, "custom/", 7) != 0) {
return luaL_error(L,
"sacred.write_file: restricted to custom/ tree, got: %s", rel);
}
char exe[MAX_PATH] = {0};
GetModuleFileNameA(g_attach.exe_module, exe, MAX_PATH);
char* slash = strrchr(exe, '\\'); if (slash) *slash = 0;
char abs[MAX_PATH];
_snprintf_s(abs, _TRUNCATE, "%s\\%s", exe, rel);
mkdirs(abs);
FILE* f = nullptr;
if (fopen_s(&f, abs, "wb") != 0 || !f) {
return luaL_error(L, "sacred.write_file: open failed: %s (errno=%d)",
abs, errno);
}
size_t wrote = fwrite(data, 1, n, f);
fclose(f);
if (wrote != n) {
return luaL_error(L, "sacred.write_file: short write %zu/%zu", wrote, n);
}
sdk_log("[lua_bake] sacred.write_file '%s' (%zu bytes)", rel, n);
lua_pushinteger(L, (lua_Integer)n);
return 1;
}
// Register the `sacred` table on the global env of a fresh state. Anything
// users need from C lives here.
static void register_sacred_api(lua_State* L) {
lua_newtable(L);
lua_pushcfunction(L, l_sacred_log); lua_setfield(L, -2, "log");
lua_pushcfunction(L, l_sacred_read_file); lua_setfield(L, -2, "read_file");
lua_pushcfunction(L, l_sacred_write_file); lua_setfield(L, -2, "write_file");
lua_pushcfunction(L, l_sacred_disasm); lua_setfield(L, -2, "disasm");
lua_setglobal(L, "sacred");
// Extend the `sacred` table with runtime-trigger entries
// (sacred.on_trigger / sacred.clear_triggers). Defined in
// sdk/runtime_triggers.cpp. Mods can call these during bake; the
// closures persist into the runtime state we hand off below.
runtime_triggers::install_lua_api(L);
}
// Override package.path / package.cpath so `require("vanilla")` etc. find the
// libraries. BOTH trees are searched, the player's first, so a modder can drop
// a patched copy of any framework module into <game>/custom/lua/lib/ and have
// it win over the one the SDK ships.
static void configure_package_path(lua_State* L) {
char exe[MAX_PATH] = {0};
GetModuleFileNameA(g_attach.exe_module, exe, MAX_PATH);
char* slash = strrchr(exe, '\\'); if (slash) *slash = 0;
lua_getglobal(L, "package");
// lib/?.lua first (the blessed helpers), then ?.lua under lua/; the player's
// tree before the SDK's.
lua_pushfstring(L,
"%s\\custom\\lua\\lib\\?.lua;"
"%s\\custom\\lua\\lib\\?\\init.lua;"
"%s\\custom\\lua\\?.lua;"
"%s\\custom\\lua\\?\\init.lua;"
"%s\\sdk\\custom\\lua\\lib\\?.lua;"
"%s\\sdk\\custom\\lua\\lib\\?\\init.lua;"
"%s\\sdk\\custom\\lua\\?.lua;"
"%s\\sdk\\custom\\lua\\?\\init.lua",
exe, exe, exe, exe, exe, exe, exe, exe);
lua_setfield(L, -2, "path");
// Disable cpath entirely — we don't want users loading random DLLs into
// Sacred's process from the script tree.
lua_pushstring(L, "");
lua_setfield(L, -2, "cpath");
lua_pop(L, 1);
}
// Bake one .lua file using a SHARED lua_State. The shared state means that
// `require`d modules (lib/text, lib/state, …) accumulate state across the
// whole bake — that's how lib/text.lua can collect inline-T() strings from
// every mod and write a single combined custom/scripts/<lang>/global.res at
// the end. Each mod's `return {records}` table is consumed and dropped
// before the next mod runs, so per-mod failures stay isolated.
static bool bake_one_file_using(lua_State* L,
const char* lua_path,
const char* out_bin_path)
{
int top_before = lua_gettop(L);
int r = luaL_loadfile(L, lua_path);
if (r != LUA_OK) {
const char* msg = lua_tostring(L, -1);
set_status("%s: load error: %s", lua_path, msg ? msg : "?");
lua_settop(L, top_before);
return false;
}
r = lua_pcall(L, 0, 1, 0);
if (r != LUA_OK) {
const char* msg = lua_tostring(L, -1);
set_status("%s: lua error: %s", lua_path, msg ? msg : "?");
lua_settop(L, top_before);
return false;
}
if (lua_type(L, -1) != LUA_TTABLE) {
set_status("%s: script must return a records table", lua_path);
lua_settop(L, top_before);
return false;
}
std::string bytes;
bytes.reserve(1 << 20);
char err[256];
if (!table_to_bytes(L, bytes, err)) {
set_status("%s: assemble failed: %s", lua_path, err);
lua_settop(L, top_before);
return false;
}
lua_settop(L, top_before); // drop the returned table
mkdirs(out_bin_path);
FILE* f = nullptr;
if (fopen_s(&f, out_bin_path, "wb") != 0 || !f) {
set_status("%s: cannot open output (err=%d)", out_bin_path, errno);
return false;
}
fwrite(bytes.data(), 1, bytes.size(), f);
fclose(f);
sdk_log("[lua_bake] baked '%s' -> '%s' (%zu bytes)", lua_path, out_bin_path, bytes.size());
InterlockedIncrement(&g_baked_files);
return true;
}
// Back-compat shim used by callers we haven't updated yet. Creates a private
// state, runs the bake, tears down. Avoid for the auto-bake worker — it uses
// a single shared state via `bake_one_file_using`.
static bool bake_one_file(const char* lua_path, const char* out_bin_path) {
lua_State* L = luaL_newstate();
if (!L) {
set_status("luaL_newstate returned NULL");
return false;
}
luaL_openlibs(L);
register_sacred_api(L);
configure_package_path(L);
bool ok = bake_one_file_using(L, lua_path, out_bin_path);
lua_close(L);
return ok;
}
// Walk `<lua_dir>` recursively. For each `<lua_dir>/<rel>.lua` produce
// `<custom_dir>/<rel>.bin`. Skips files that fail to bake (logged).
// Uses the shared lua_State `L` so module-level state (e.g. lib/text.lua's
// inline-string registry) accumulates across all baked mods.
// `shadow_dir` is the higher-priority tree, or nullptr. A .lua file that also
// exists there at the same relative path is skipped here: the other walk bakes
// it, and baking both would run the same mod twice.
static int walk_and_bake(lua_State* L,
const char* lua_dir, const char* custom_dir,
const char* sub_prefix,
const char* shadow_dir = nullptr)
{
// sub_prefix accumulates the relative path under lua/. Example final
// mapping: lua/bin/TYPE_NPC_SERAPHIM/FunkCode.lua -> custom/bin/TYPE_NPC_SERAPHIM/FunkCode.bin
char glob[MAX_PATH];
if (sub_prefix && sub_prefix[0]) {
_snprintf_s(glob, _TRUNCATE, "%s\\%s\\*", lua_dir, sub_prefix);
} else {
_snprintf_s(glob, _TRUNCATE, "%s\\*", lua_dir);
}
WIN32_FIND_DATAA fd;
HANDLE h = FindFirstFileA(glob, &fd);
if (h == INVALID_HANDLE_VALUE) return 0;
int baked = 0;
do {
if (fd.cFileName[0] == '.') continue;
// Skip lib/ (helper modules loaded via `require`) and anything
// starting with `_` (vanilla snapshots, internal storage). These
// are NOT mods — they're support data.
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (_stricmp(fd.cFileName, "lib") == 0) continue;
// examples/ are illustrative docs, NOT mods: executing them
// registered duplicate on_tick spawns over the real mod
// ("three Captains" bug). Copy patterns into bin/ to use them.
if (_stricmp(fd.cFileName, "examples") == 0) continue;
if (fd.cFileName[0] == '_') continue;
}
char full_lua[MAX_PATH];
if (sub_prefix && sub_prefix[0]) {
_snprintf_s(full_lua, _TRUNCATE, "%s\\%s\\%s", lua_dir, sub_prefix, fd.cFileName);
} else {
_snprintf_s(full_lua, _TRUNCATE, "%s\\%s", lua_dir, fd.cFileName);
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
char child_prefix[MAX_PATH];
if (sub_prefix && sub_prefix[0]) {
_snprintf_s(child_prefix, _TRUNCATE, "%s\\%s", sub_prefix, fd.cFileName);
} else {
_snprintf_s(child_prefix, _TRUNCATE, "%s", fd.cFileName);
}
baked += walk_and_bake(L, lua_dir, custom_dir, child_prefix, shadow_dir);
continue;
}
// Must end in ".lua"
size_t fname_len = strlen(fd.cFileName);
if (fname_len < 4 ||
_stricmp(fd.cFileName + fname_len - 4, ".lua") != 0) continue;
// The player's tree wins: if the same mod exists there, skip ours.
if (shadow_dir) {
char shadowed[MAX_PATH];
if (sub_prefix && sub_prefix[0]) {