-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathce_server.lua
More file actions
2091 lines (1982 loc) · 73.6 KB
/
Copy pathce_server.lua
File metadata and controls
2091 lines (1982 loc) · 73.6 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
local PIPE_NAME = "UEScanRemote"
local PIPE_BUFFER = 65536
local VERSION = "ce-server v1.8.4 (CE 7.5 restartable pipe)"
local MAX_RESP = 48000
local MAX_OFFSETS = 512
local MAX_SCRIPT_BYTES = 262144 -- 256 KiB staging cap
local DEFAULT_SCRIPT_CHUNK = 16384
local DEFAULT_ST_ELEM_LIMIT = 500
local MAX_ST_CLEAR_ELEMS = 50000
local MAX_AL_APPLY_OPS = 100
local MAX_AUDIT = 64
-- Ring buffer of recent command verbs (T11e); not a substitute for user save
if not _G._ue_audit then
_G._ue_audit = {}
end
-- CE 7.5 empty global structure list → dissect TreeView "list index (0) out of bounds".
-- Always keep a tiny seed (UnrealEdit75 CE75-DISSECT-CRASH.md).
local ST_PLACEHOLDER_NAME = "DO_NOT_DELETE_PLACEHOLDER"
local ST_PLACEHOLDER_LEGACY = "UE_Seed"
local VT_BYTE = 0 -- CE vtByte
-- Track structures with open beginUpdate (agent-owned; warn on double begin)
if not _G._ue_st_updating then
_G._ue_st_updating = {}
end
-- CE 7.5: vtAutoAssembler observed as Type=11 on DL2 tables (T00); also accept 8.
local VT_AUTO_ASSEMBLER_A = 11
local VT_AUTO_ASSEMBLER_B = 8
local HEX = "0123456789ABCDEF"
-- Staging for chunked script upload (T04). Key = tostring(memrec id).
if not _G._ue_script_stage then
_G._ue_script_stage = {}
end
local function bytes_to_hex(t)
local parts = {}
for i = 1, #t do
parts[#parts + 1] = string.sub(HEX, bShr(t[i], 4) + 1, bShr(t[i], 4) + 1)
parts[#parts + 1] = string.sub(HEX, t[i] % 16 + 1, t[i] % 16 + 1)
end
return table.concat(parts)
end
local function hex_to_table(s)
s = s:gsub("%s+", "")
if #s % 2 ~= 0 then return nil end
local t = {}
for i = 1, #s, 2 do
t[#t + 1] = tonumber(s:sub(i, i + 1), 16)
end
return t
end
local function str_to_hex(s)
s = tostring(s or "")
local parts = {}
for i = 1, #s do
local b = string.byte(s, i)
parts[#parts + 1] = string.sub(HEX, bShr(b, 4) + 1, bShr(b, 4) + 1)
parts[#parts + 1] = string.sub(HEX, b % 16 + 1, b % 16 + 1)
end
return table.concat(parts)
end
local function hex_to_str(h)
local t = hex_to_table(h)
if not t then return nil end
local chars = {}
for i = 1, #t do
chars[i] = string.char(t[i])
end
return table.concat(chars)
end
local function stage_key(id)
return tostring(id)
end
local function stage_clear(id)
_G._ue_script_stage[stage_key(id)] = nil
end
local function stage_get(id)
return _G._ue_script_stage[stage_key(id)]
end
-- Main-thread: true if memrec has AA script object with text access
local function mr_is_aa_script(mr)
local ok, scr = pcall(function() return mr.Script end)
if not ok then return false end
-- Empty script string still counts as AA slot if Type is AA, but setScript
-- only works when AutoAssemblerData.script ~= nil — non-nil Script property.
if scr ~= nil then return true end
local vt = mr.Type
return vt == VT_AUTO_ASSEMBLER_A or vt == VT_AUTO_ASSEMBLER_B
end
local function scrub(s)
s = tostring(s or "")
return (s:gsub("[\r\n\t]", " "))
end
local function fit_response(s)
s = tostring(s or "")
if #s > MAX_RESP then
return "ERROR: RESPONSE_TOO_LARGE: " .. tostring(#s)
end
return s
end
local function trunc_str(s, n)
s = scrub(s)
if #s > n then
return s:sub(1, n)
end
return s
end
-- Run fn on CE main thread. Returns { ok=true, data=... } or { ok=false, err=... }.
-- Table/UI/AA work MUST use this (VCL is not safe from the pipe background thread).
local function sync_call(fn)
local packed = synchronize(function()
local ok, result = pcall(fn)
if not ok then
return { ok = false, err = tostring(result) }
end
return { ok = true, data = result }
end)
if type(packed) ~= "table" then
return { ok = false, err = "synchronize returned non-table: " .. tostring(packed) }
end
return packed
end
local function ok_data(packed)
if not packed or not packed.ok then
local err = packed and packed.err or "unknown"
return nil, "ERROR: SYNC: " .. tostring(err)
end
return packed.data
end
-- Main-thread only (call inside sync_call).
local function al_find_by_id(id)
local al = getAddressList()
if not al then return nil, nil end
local mr = al.getMemoryRecordByID(id)
return al, mr
end
-- Main-thread only. NEVER pass a name string to getStructure() — index only.
local function st_find_by_name(name)
local n = getStructureCount()
if not n then return nil, nil end
for i = 0, n - 1 do
local s = getStructure(i)
if s and s.Name == name then
return s, i
end
end
return nil, nil
end
local function st_count()
local n = 0
pcall(function() n = getStructureCount() or 0 end)
return n or 0
end
local function st_is_protected_name(name)
name = tostring(name or "")
return name == ST_PLACEHOLDER_NAME or name == ST_PLACEHOLDER_LEGACY
end
-- Main-thread: ensure at least one global structure exists (placeholder preferred).
-- Empty DissectedStructs list crashes CE dissect UI / callbacks (list index 0).
-- force_placeholder=true: create DO_NOT_DELETE_PLACEHOLDER even if other structs exist.
-- Returns: ok (bool), status ("present"|"legacy"|"has_structures"|"created"|"error:..."), count
local function st_ensure_seed(force_placeholder)
if st_find_by_name(ST_PLACEHOLDER_NAME) then
return true, "present", st_count()
end
if st_find_by_name(ST_PLACEHOLDER_LEGACY) then
return true, "legacy", st_count()
end
local n = st_count()
-- Any structure avoids the empty-list crash. Placeholder is still best practice.
if n > 0 and not force_placeholder then
return true, "has_structures", n
end
if type(createStructure) ~= "function" then
return false, "error:NO_CREATESTRUCTURE", 0
end
local ok, err = pcall(function()
local s = createStructure(ST_PLACEHOLDER_NAME)
if not s then error("createStructure returned nil") end
pcall(function() s.beginUpdate() end)
local e = s.addElement()
if e then
pcall(function() e.Name = "_placeholder" end)
pcall(function() e.Offset = 0 end)
pcall(function() e.Vartype = VT_BYTE end)
end
pcall(function() s.endUpdate() end)
-- Name after elements (CE UI refresh / DoFullStructChangeNotification)
pcall(function() s.Name = ST_PLACEHOLDER_NAME end)
s.addToGlobalStructureList()
end)
if not ok then
return false, "error:" .. tostring(err), st_count()
end
return true, "created", st_count()
end
-- Main-thread: commit pending beginUpdate (CE "save" of in-memory definition).
-- Required before rename — renaming mid-edit is unsafe / fails in CE.
local function st_commit(s, name)
name = name or (s and s.Name) or ""
if s then
-- Drain update counter (endUpdate is a no-op when not updating)
for _ = 1, 8 do
local updating = false
pcall(function()
if s.isUpdating then updating = s.isUpdating() end
end)
pcall(function() s.endUpdate() end)
if not updating then break end
end
end
if name and name ~= "" then
_G._ue_st_updating[name] = nil
end
end
-- Main-thread: rename only after commit. Refuse mid-beginUpdate and protected names.
local function st_set_name_safe(oldn, newn)
if st_is_protected_name(oldn) then
return "ERROR: PROTECTED: " .. scrub(oldn)
end
if st_is_protected_name(newn) then
return "ERROR: PROTECTED: " .. scrub(newn)
end
local s = st_find_by_name(oldn)
if not s then return "ERROR: NOT_FOUND: " .. scrub(oldn) end
if oldn ~= newn and st_find_by_name(newn) then
return "ERROR: EXISTS: " .. scrub(newn)
end
if _G._ue_st_updating[oldn] then
return "ERROR: STILL_UPDATING: call stEnd before rename (commit structure first)"
end
-- Commit any CE-side update counter even if agent forgot stBegin tracking
st_commit(s, oldn)
pcall(function() s.Name = newn end)
local check = st_find_by_name(newn)
if not check then
return "ERROR: RENAME_FAILED: " .. scrub(oldn) .. " -> " .. scrub(newn)
end
return string.format("OK OLD=%s NEW=%s COMMITTED=1", scrub(oldn), scrub(newn))
end
local function st_elem_count(s)
local c = 0
pcall(function() c = s.Count or 0 end)
return c or 0
end
local function st_size_str(s)
local sz = ""
pcall(function()
if s.Size ~= nil then sz = tostring(s.Size) end
end)
if sz == "" or sz == "nil" then sz = "?" end
return sz
end
-- Main-thread: dump element lines for structure s
local function st_element_lines(s, elemOff, elemLimit)
local n = st_elem_count(s)
if elemOff < 0 then elemOff = 0 end
if elemLimit < 1 then elemLimit = 1 end
local lines = {}
lines[#lines + 1] = "IDX\tOFF\tNAME\tVTYPE\tBYTES\tCHILD\tCHILDSTART"
if elemOff >= n then
return lines, n
end
local last = math.min(n, elemOff + elemLimit) - 1
for j = elemOff, last do
local e = nil
pcall(function() e = s.Element[j] end)
if not e then
pcall(function() e = s.getElement(j) end)
end
if e then
local off = 0
local ename = ""
local vt = -1
local bsz = 0
local child = ""
local cstart = 0
pcall(function() off = e.Offset or 0 end)
pcall(function() ename = tostring(e.Name or "") end)
pcall(function() vt = e.Vartype or e.Type or -1 end)
pcall(function() bsz = e.Bytesize or 0 end)
pcall(function()
local cs = e.ChildStruct
if cs and cs.Name then child = tostring(cs.Name) end
end)
pcall(function() cstart = e.ChildStructStart or 0 end)
lines[#lines + 1] = string.format(
"%d\t%X\t%s\t%s\t%s\t%s\t%s",
j, off, trunc_str(ename, 80), tostring(vt), tostring(bsz),
scrub(child), tostring(cstart))
end
end
return lines, n
end
-- Split "a b" / "a -> b" / "a|b" into two names. Optional existCheck runs on main thread.
-- When existCheck=true, prefer longest left part that is an existing structure name.
local function st_split_two_names(rest, prefer_existing_src)
rest = (rest or ""):match("^%s*(.-)%s*$") or ""
if rest == "" then return nil, nil, "EMPTY" end
local a, b = rest:match("^(.-)%s*%->%s*(.+)$")
if not a then a, b = rest:match("^(.-)%s*|%s*(.+)$") end
if a then
a = a:match("^%s*(.-)%s*$") or a
b = b:match("^%s*(.-)%s*$") or b
if a == "" or b == "" then return nil, nil, "EMPTY" end
return a, b, nil
end
local parts = {}
for w in rest:gmatch("%S+") do parts[#parts + 1] = w end
if #parts < 2 then return nil, nil, "NEED_TWO_NAMES" end
if prefer_existing_src then
for i = #parts - 1, 1, -1 do
local src = table.concat(parts, " ", 1, i)
local dst = table.concat(parts, " ", i + 1, #parts)
if st_find_by_name(src) then return src, dst, nil end
end
end
-- Fallback: last token is dst, rest is src
local dst = parts[#parts]
local src = table.concat(parts, " ", 1, #parts - 1)
return src, dst, nil
end
-- Main-thread: element at exact Offset (not "at least" from getElementByOffset alone)
local function st_elem_at_offset(s, off)
off = tonumber(off) or 0
local e = nil
pcall(function()
if s.getElementByOffset then
e = s.getElementByOffset(off)
end
end)
if e then
local eo = nil
pcall(function() eo = e.Offset end)
if eo ~= nil and tonumber(eo) == off then return e end
end
local n = st_elem_count(s)
for j = 0, n - 1 do
local ej = nil
pcall(function() ej = s.Element[j] end)
if not ej then pcall(function() ej = s.getElement(j) end) end
if ej then
local eo = nil
pcall(function() eo = ej.Offset end)
if eo ~= nil and tonumber(eo) == off then return ej end
end
end
return nil
end
local function st_apply_elem_fields(e, ename, vtype, bsz, childName, cstart)
if ename ~= nil then
pcall(function() e.Name = tostring(ename) end)
end
if vtype ~= nil then
pcall(function() e.Vartype = tonumber(vtype) end)
end
if bsz ~= nil and bsz >= 0 then
pcall(function() e.Bytesize = tonumber(bsz) end)
end
if childName ~= nil then
if childName == "" then
pcall(function() e.ChildStruct = nil end)
else
local cs = st_find_by_name(childName)
if not cs then return "ERROR: CHILD_NOT_FOUND: " .. scrub(childName) end
pcall(function() e.ChildStruct = cs end)
end
end
if cstart ~= nil then
pcall(function() e.ChildStructStart = tonumber(cstart) or 0 end)
end
return nil
end
-- Main-thread: two-pass clone of structure definitions
local function st_clone_structure(srcName, dstName)
if st_is_protected_name(dstName) then
return "ERROR: PROTECTED: " .. scrub(dstName)
end
-- Empty global list crashes dissect; seed first (CE75-DISSECT-CRASH)
local seed_ok, seed_st = st_ensure_seed()
if not seed_ok then
return "ERROR: SEED_FAILED: " .. scrub(tostring(seed_st))
end
local src = st_find_by_name(srcName)
if not src then return "ERROR: NOT_FOUND: " .. scrub(srcName) end
if st_find_by_name(dstName) then return "ERROR: EXISTS: " .. scrub(dstName) end
if type(createStructure) ~= "function" then
return "ERROR: NO_CREATESTRUCTURE"
end
-- Create with a temp name; set final Name only AFTER elements + endUpdate
-- (CE: Name triggers full UI refresh; rename-while-editing is unsafe).
local tick = 0
pcall(function()
if type(getTickCount) == "function" then tick = getTickCount() end
end)
if tick == 0 then pcall(function() tick = os.time() or 0 end) end
local tmpName = "UE_CLONE_TMP_" .. tostring(tick) .. "_" .. tostring(st_count())
if st_find_by_name(tmpName) then
tmpName = tmpName .. "_x"
end
local dst = createStructure(tmpName)
if not dst then return "ERROR: CREATE_FAILED: " .. scrub(dstName) end
local added = false
pcall(function()
dst.addToGlobalStructureList()
added = true
end)
if not added then
pcall(function() if dst.destroy then dst.destroy() end end)
st_ensure_seed()
return "ERROR: ADD_GLOBAL_FAILED: " .. scrub(dstName)
end
local n = st_elem_count(src)
local copied = 0
local child_ok, child_fail = 0, 0
local ok_copy, err_copy = pcall(function()
pcall(function() dst.beginUpdate() end)
for i = 0, n - 1 do
local e = nil
pcall(function() e = src.Element[i] end)
if not e then pcall(function() e = src.getElement(i) end) end
if e then
local ne = dst.addElement()
if ne then
local off, ename, vt, bsz = 0, "", -1, nil
pcall(function() off = e.Offset or 0 end)
pcall(function() ename = tostring(e.Name or "") end)
pcall(function() vt = e.Vartype or e.Type or -1 end)
pcall(function() bsz = e.Bytesize end)
pcall(function() ne.Offset = off end)
pcall(function() ne.Name = ename end)
pcall(function() ne.Vartype = vt end)
if bsz ~= nil then pcall(function() ne.Bytesize = bsz end) end
-- ChildStruct deferred to pass 2
copied = copied + 1
end
end
end
pcall(function() dst.endUpdate() end)
-- Pass 2: ChildStruct by name (same global name; clone children first if renaming family)
pcall(function() dst.beginUpdate() end)
for i = 0, n - 1 do
local e = nil
pcall(function() e = src.Element[i] end)
if not e then pcall(function() e = src.getElement(i) end) end
if e then
local cname, cstart = nil, 0
local has_child = false
pcall(function()
local cs = e.ChildStruct
if cs and cs.Name then
has_child = true
cname = tostring(cs.Name)
end
end)
pcall(function() cstart = e.ChildStructStart or 0 end)
if has_child and cname and cname ~= "" then
local ne = nil
pcall(function() ne = dst.Element[i] end)
if not ne then pcall(function() ne = dst.getElement(i) end) end
local child = st_find_by_name(cname)
if ne and child then
local okc = pcall(function()
ne.ChildStruct = child
ne.ChildStructStart = cstart
end)
if okc then child_ok = child_ok + 1 else child_fail = child_fail + 1 end
else
child_fail = child_fail + 1
end
end
end
end
pcall(function() dst.endUpdate() end)
-- Commit then rename to final name (save-then-rename rule)
st_commit(dst, tmpName)
if st_find_by_name(dstName) then
error("DEST_EXISTS_AFTER_FILL")
end
pcall(function() dst.Name = dstName end)
end)
if not ok_copy then
pcall(function() dst.removeFromGlobalStructureList() end)
pcall(function() if dst.destroy then dst.destroy() end end)
st_ensure_seed() -- never leave global list empty after rollback
return "ERROR: CLONE_COPY: " .. scrub(tostring(err_copy))
end
local final = st_find_by_name(dstName)
if not final then
-- rename failed; leave tmp if present for recovery
return "ERROR: RENAME_AFTER_CLONE: tmp=" .. scrub(tmpName) .. " wanted=" .. scrub(dstName)
end
local dst_n = st_elem_count(final)
return string.format(
"OK SRC=%s DST=%s ELEMS=%d COPIED=%d CHILD_OK=%d CHILD_FAIL=%d SEED=%s",
scrub(srcName), scrub(dstName), dst_n, copied, child_ok, child_fail, scrub(tostring(seed_st)))
end
-- Main-thread: delete all elements (destroy Element[0] loop; CE has no clear())
local function st_clear_elements(s, name)
name = name or ""
pcall(function() if s and s.Name then name = tostring(s.Name) end end)
if st_is_protected_name(name) then
return "ERROR: PROTECTED: " .. scrub(name)
end
pcall(function() s.beginUpdate() end)
local deleted = 0
local guard = 0
while guard < MAX_ST_CLEAR_ELEMS do
local c = st_elem_count(s)
if c <= 0 then break end
local ok_del = false
pcall(function()
local e = s.Element[0]
if e and e.destroy then
e.destroy()
ok_del = true
end
end)
if not ok_del then
pcall(function()
local e = s.getElement and s.getElement(0)
if e and e.destroy then
e.destroy()
ok_del = true
end
end)
end
if not ok_del then break end
deleted = deleted + 1
guard = guard + 1
end
st_commit(s, name)
local left = st_elem_count(s)
if left > 0 then
return string.format(
"ERROR: CLEAR_PARTIAL: deleted=%d left=%d", deleted, left)
end
return string.format("OK NAME=%s DELETED=%d ELEMS=0", scrub(tostring(s.Name or name)), deleted)
end
-- Main-thread only helpers for address-list inventory (T02)
local function mr_script_info(mr)
local has, slen = 0, 0
local ok, scr = pcall(function() return mr.Script end)
if ok and scr and scr ~= "" then
has = 1
slen = #scr
end
return has, slen
end
local function mr_offsets_str(mr, sep)
sep = sep or "+"
local oc = mr.OffsetCount or 0
if oc <= 0 then return "", 0 end
local parts = {}
for i = 0, oc - 1 do
local off = 0
pcall(function() off = mr.Offset[i] or 0 end)
parts[#parts + 1] = string.format("%X", off)
end
return table.concat(parts, sep), oc
end
local function mr_parent_id(mr)
local pid = -1
pcall(function()
local p = mr.Parent
if p and p.ID then pid = p.ID end
end)
return pid
end
local function mr_cur_hex(mr)
local cur = 0
pcall(function() cur = mr.CurrentAddress or 0 end)
if not cur then cur = 0 end
return string.format("%X", cur)
end
local function mr_is_group(mr)
local g = false
pcall(function()
if mr.IsGroupHeader or mr.IsAddressGroupHeader then g = true end
end)
return g
end
-- CLASS: AA | EXPR | POINTER | STATIC | GROUP | OTHER (T00: EXPR is common on DL2)
local function mr_classify(mr, addr, offc, hasScript, typeNum)
if mr_is_group(mr) then return "GROUP" end
if hasScript == 1 or typeNum == VT_AUTO_ASSEMBLER_A or typeNum == VT_AUTO_ASSEMBLER_B then
return "AA"
end
if offc and offc > 0 then return "POINTER" end
addr = addr or ""
if addr ~= "" then
if string.find(addr, "+", 1, true) or string.find(addr, "[", 1, true) then
return "EXPR"
end
return "STATIC"
end
return "OTHER"
end
local function mr_value_trunc(mr, n)
n = n or 80
local v = ""
pcall(function() v = tostring(mr.Value or "") end)
return trunc_str(v, n)
end
local function mr_readable_flag(mr)
-- celua: IsReadable reliable after Value accessed at least once
pcall(function() local _ = mr.Value end)
local r = 0
pcall(function()
if mr.IsReadable then r = 1 end
end)
return r
end
-- Read offsets into 1-based Lua table for setAddress(expr, offs)
local function mr_get_offset_table(mr)
local oc = mr.OffsetCount or 0
local t = {}
for i = 0, oc - 1 do
local off = 0
pcall(function() off = mr.Offset[i] or 0 end)
t[i + 1] = off
end
return t, oc
end
-- CE setAddress clears offsets unless table passed; preserve when only base changes.
local function mr_set_address_preserve_offsets(mr, expr)
expr = tostring(expr or "")
local offs, oc = mr_get_offset_table(mr)
local ok, err = pcall(function()
if type(mr.setAddress) == "function" then
if oc > 0 then
mr.setAddress(expr, offs)
else
mr.setAddress(expr)
end
else
mr.Address = expr
if oc > 0 then
mr.OffsetCount = oc
for i = 1, oc do
mr.Offset[i - 1] = offs[i]
end
end
end
end)
return ok, err
end
-- Parse "10,2A0,8" or "10+2A0+8" or empty → table, or nil,"error"
local function parse_offset_list(s)
s = tostring(s or ""):gsub("%s+", "")
if s == "" or s == "-" then
return {}, 0
end
s = s:gsub("%+", ",")
local t = {}
for part in string.gmatch(s, "[^,]+") do
part = part:gsub("^0[xX]", "")
if part == "" then
return nil, "ERROR: BAD_OFFSET_TOKEN"
end
local n = tonumber(part, 16)
if not n then
return nil, "ERROR: BAD_OFFSET: " .. part
end
t[#t + 1] = n
end
if #t > MAX_OFFSETS then
return nil, "ERROR: TOO_MANY_OFFSETS: " .. tostring(#t)
end
return t, #t
end
local function mr_set_offsets(mr, offs)
local base = tostring(mr.Address or "")
local ok, err = pcall(function()
if type(mr.setAddress) == "function" then
if #offs > 0 then
mr.setAddress(base, offs)
else
mr.setAddress(base)
mr.OffsetCount = 0
end
else
mr.Address = base
mr.OffsetCount = #offs
for i = 1, #offs do
mr.Offset[i - 1] = offs[i]
end
end
end)
return ok, err
end
local function audit_push(verb, detail, result)
local a = _G._ue_audit
if type(a) ~= "table" then
_G._ue_audit = {}
a = _G._ue_audit
end
local tick = 0
pcall(function()
if type(getTickCount) == "function" then tick = getTickCount() end
end)
local line = string.format(
"%d\t%s\t%s\t%s",
tick, scrub(verb or "?"), scrub(detail or ""), scrub(tostring(result or ""):sub(1, 80)))
a[#a + 1] = line
while #a > MAX_AUDIT do
table.remove(a, 1)
end
end
-- Preview of inbound command for CE Lua Engine console (crash forensics).
-- Last printed "EXEC start" before silence = the call that killed the server thread.
local function cmd_preview(cmd, max_len)
max_len = max_len or 240
local s = scrub(tostring(cmd or ""))
-- Collapse huge hex payloads (script chunks, alApply) so console stays readable
if #s > 80 and (s:find("hex=") or s:match("^alSetScriptChunk") or s:match("^runScript")) then
local verb = s:match("^(%S+)") or "?"
local rest = s:sub(#verb + 2)
if #rest > 60 then rest = rest:sub(1, 60) .. "…[trunc]" end
s = verb .. " " .. rest
end
if #s > max_len then
s = s:sub(1, max_len) .. "…[len=" .. tostring(#tostring(cmd or "")) .. "]"
end
return s
end
-- Main-thread only: single alSet* op for alApply / shared handlers
local function al_op_set_desc(id, text)
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
local ok, err = pcall(function() mr.Description = text end)
if not ok then return "ERROR: SET_DESC: " .. tostring(err) end
return string.format("OK ID=%s DESC=%s", tostring(mr.ID or id), scrub(mr.Description or ""))
end
local function al_op_set_address(id, expr)
expr = scrub(expr or "")
if expr == "" then return "ERROR: EMPTY_ADDRESS" end
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
local ok, err = mr_set_address_preserve_offsets(mr, expr)
if not ok then return "ERROR: SET_ADDRESS: " .. tostring(err) end
local offs, oc = mr_offsets_str(mr, "+")
return string.format(
"OK ID=%s ADDR=%s OFFC=%d OFFS=%s CUR=%s",
tostring(mr.ID or id), scrub(tostring(mr.Address or "")), oc, offs, mr_cur_hex(mr))
end
local function al_op_set_offsets(id, list)
local offs, n_or_err = parse_offset_list(list)
if not offs then return n_or_err end
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
local ok, err = mr_set_offsets(mr, offs)
if not ok then return "ERROR: SET_OFFSETS: " .. tostring(err) end
local offsStr, oc = mr_offsets_str(mr, ",")
return string.format(
"OK ID=%s OFFC=%d OFFS=%s CUR=%s ADDR=%s",
tostring(mr.ID or id), oc, offsStr, mr_cur_hex(mr), scrub(tostring(mr.Address or "")))
end
local function al_op_set_type(id, tnum)
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
local ok, err = pcall(function() mr.Type = tnum end)
if not ok then return "ERROR: SET_TYPE: " .. tostring(err) end
return string.format("OK ID=%s TYPE=%s", tostring(mr.ID or id), tostring(mr.Type))
end
-- Main-thread: aaCheck body for one memrec
local function al_op_aa_check(id)
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
if not mr_is_aa_script(mr) then return "ERROR: NOT_AA: " .. tostring(id) end
local ok, scr = pcall(function() return mr.Script end)
if not ok or scr == nil or scr == "" then
return "ERROR: AACHECK: empty script"
end
if type(autoAssembleCheck) ~= "function" then
return "ERROR: AACHECK: autoAssembleCheck unavailable"
end
local okc, a, b = pcall(autoAssembleCheck, scr, true, false)
if not okc then return "ERROR: AACHECK: " .. tostring(a) end
if a == true or a == 1 then
return string.format("OK ID=%s", tostring(mr.ID or id))
end
local msg = b or a or "failed"
return "ERROR: AACHECK: " .. scrub(tostring(msg))
end
-- Main-thread: set Active; do_check=true runs aaCheck before enable (AA rows only)
local function al_op_set_active(id, want, do_check)
local al, mr = al_find_by_id(id)
if not mr then return "ERROR: NOT_FOUND: " .. tostring(id) end
if want and do_check and mr_is_aa_script(mr) then
local chk = al_op_aa_check(id)
if tostring(chk):match("^ERROR:") then
return "ERROR: SET_ACTIVE: " .. tostring(chk)
end
end
local ok, err = pcall(function()
mr.Active = want
end)
if not ok then return "ERROR: SET_ACTIVE: " .. tostring(err) end
local act = 0
pcall(function() if mr.Active then act = 1 end end)
if want and act ~= 1 then
return string.format(
"ERROR: SET_ACTIVE: enable failed ID=%s ACTIVE=%d", tostring(mr.ID or id), act)
end
if (not want) and act ~= 0 then
return string.format(
"ERROR: SET_ACTIVE: disable failed ID=%s ACTIVE=%d", tostring(mr.ID or id), act)
end
local checked = (want and do_check) and 1 or 0
return string.format(
"OK ID=%s ACTIVE=%d CHECKED=%d", tostring(mr.ID or id), act, checked)
end
-- Main-thread: run one alApply line (setDesc/setAddress/setOffsets/setType)
local function al_apply_one_op(line)
line = (line or ""):match("^%s*(.-)%s*$") or ""
if line == "" then return "ERROR: EMPTY_OP" end
local op = line:match("^(%S+)") or ""
op = op:lower()
if op == "setdesc" or op == "alsetdesc" then
local id, text = line:match("^%S+%s+(%-?%d+)%s*(.*)$")
if not id then return "ERROR: USAGE: setDesc <id> <text>" end
return al_op_set_desc(tonumber(id), text or "")
elseif op == "setaddress" or op == "alsetaddress" then
local id, expr = line:match("^%S+%s+(%-?%d+)%s+(.+)$")
if not id then return "ERROR: USAGE: setAddress <id> <expr>" end
return al_op_set_address(tonumber(id), expr)
elseif op == "setoffsets" or op == "alsetoffsets" then
local id, list = line:match("^%S+%s+(%-?%d+)%s*(.*)$")
if not id then return "ERROR: USAGE: setOffsets <id> <hex,hex>" end
return al_op_set_offsets(tonumber(id), list or "")
elseif op == "settype" or op == "alsettype" then
local id, tnum = line:match("^%S+%s+(%-?%d+)%s+(%-?%d+)$")
if not id then return "ERROR: USAGE: setType <id> <typeInt>" end
return al_op_set_type(tonumber(id), tonumber(tnum))
end
return "ERROR: UNKNOWN_OP: " .. scrub(op)
end
local function run_script_body(code, safe)
if not code or #code == 0 then return "ERROR: Empty script" end
if safe then
local low = code:lower()
local banned = {
"enummemoryregions", "creatememscan", "varscan_", "memscan_firstscan",
"getstructure%(%s*['\"]", -- getStructure("name") pattern
}
for i = 1, #banned do
if low:find(banned[i]) then
return "ERROR: RUNSCRIPT_SAFE: banned pattern: " .. banned[i]
end
end
end
local fn, err = loadstring("return " .. code)
if fn then
local ok, result = pcall(fn)
if ok then
return fit_response(tostring(result))
else
return "ERROR: " .. tostring(result)
end
else
fn, err = loadstring(code)
if fn then
local ok, result = pcall(fn)
if ok then
return fit_response(tostring(result))
else
return "ERROR: " .. tostring(result)
end
else
return "ERROR: Compile error: " .. tostring(err)
end
end
end
local function write_length_prefixed(pipe, msg)
local len = #msg
local len_bytes = string.char(len % 256, bShr(len, 8) % 256,
bShr(len, 16) % 256, bShr(len, 24) % 256)
pipe.writeBytes({len_bytes:byte(1, 4)}, 4)
if len > 0 then
pipe.writeString(msg, false)
end
end
local function read_length_prefixed(pipe)
local len_header = pipe.readBytes(4)
if not len_header or #len_header < 4 then
return nil
end
local len = len_header[1] + len_header[2] * 256 + len_header[3] * 65536 + len_header[4] * 16777216
if len == 0 then return "" end
local data = pipe.readBytes(len)
if not data or #data < len then return nil end
local chars = {}
for i = 1, #data do
chars[i] = string.char(data[i])
end
return table.concat(chars)
end
local function process_command(cmd)
if cmd == "ping" then
return "pong"
elseif cmd == "help" then
return table.concat({
"ping", "getVersion", "help", "tableStatus", "debugSync",
"alDump [offset] [limit]", "alGet <id>", "alResolve <id>",
"alSetDesc <id> <text>", "alSetAddress <id> <expr>",
"alSetOffsets <id> <hex,hex,...>", "alSetType <id> <typeInt>",
"alGetScript <id> [off] [len]", "alSetScriptBegin <id> <totalLen>",
"alSetScriptChunk <id> <off> <hex>", "alSetScriptCommit <id>", "alSetScriptAbort <id>",
"aaCheck <id>", "alSetActive <id> 0|1 [nocheck]", "alDisableSoft <id>",
"alApply stop=1 hex=<ops> OR alApply stop=1 op ;; op",
"alAudit [n]", "symGet <name>", "symSet <name> <addr> [donotsave]",
"stDump", "stFind <name>", "stGet <name> [elemOff] [elemLimit]",
"stEnsureSeed", "stClone <src> <dst> (or src -> dst)",
"stBegin <name>", "stEnd <name>",
"stUpsertElem name/off/elem/vtype (prefer pipe form; see docs)",
"stClearElements <name>", "stSetName <old> <new> (stEnd first)",
"readByte <hexaddr>", "readBytes <hexaddr> <size>",
"readQword <hexaddr>", "readDword <hexaddr>", "readString <hexaddr> <maxlen>",
"writeBytes <hexaddr> <hex>",
"getAddress <symbol>", "resolveSymbol <name>",
"AOBScan <hexpattern with optional ** wildcards>",
"GroupScan <group command string> (CE vtGrouped; main-thread memscan)",
"enumModules", "runScript <code>", "runScriptSafe <code>", "close", "shutdown"
}, "|")
elseif cmd == "getVersion" then
return VERSION
-- T01: foundation smoke — main-thread table/process snapshot
elseif cmd == "tableStatus" then
local packed = sync_call(function()
local proc = tostring(process or "?")
local pid = "?"
if type(getOpenedProcessID) == "function" then
local okp, p = pcall(getOpenedProcessID)
if okp and p then pid = tostring(p) end
end
local alCount = 0
local al = getAddressList()
if al and al.Count then alCount = al.Count end
local stCount = 0