Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
223 lines (213 loc) · 9.49 KB
/
Copy pathmain.lua
File metadata and controls
223 lines (213 loc) · 9.49 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
-- SPDX-License-Identifier: LicenseRef-CDAL-BSX-Source-Available-1.0
-- Cold start, wait for startup below, Continue, equip and unequip, wait 40 seconds, quit.
-- Read-only API 3 diagnostic example.
-- Stored HP is not a call to the game's time-dependent regeneration calculation.
local checks, failures = {}, {}
local required = {"versions", "config", "frame", "save_load", "save_loaded", "player",
"hp", "equipment", "inventory", "equip_applied", "unequip_applied", "timer"}
local finished, loaded = false, false
local read_lines = {}
local frames, next_log = 0, os.time() + 10
local function log(line) read_lines[#read_lines + 1] = line end
local function flush()
while #read_lines > 0 do
if not bsx.log(read_lines[1]) then break end
table.remove(read_lines, 1)
end
end
-- No portable money ordinal is established by the images. -1 searches cached
-- definitions for Money_Copper; an explicit ItemId still selects an exact row.
local item_id = config.int("Smoke", "ItemId", -1)
checks.config = item_id >= -1 and item_id < 65535
checks.versions = bsx.api_version >= 2 and bsx.lua_api == 3
bsx.log("smoke: start BSX_API=" .. bsx.api_version .. " LUA_API=" .. bsx.lua_api .. " VM=" .. _VERSION)
bsx.log("smoke: config ItemId=" .. item_id)
local function finish()
if finished then return end
finished = true
for _, name in ipairs(required) do
if not checks[name] then failures[#failures + 1] = name end
end
if #failures == 0 then log("smoke: PASS " .. #required .. " checks")
else log("smoke: FAIL " .. table.concat(failures, ",")) end
flush()
end
local function observe(name, fn)
local ok, why = pcall(fn)
checks[name] = ok
if not ok then log("smoke: " .. name .. " unavailable (" .. tostring(why) .. ")") end
end
local function ptr(address)
local value, why = mem.read_ptr(address)
assert(value and value ~= 0, why or "null pointer")
return value
end
local function u16(address)
return assert(mem.read_u8(address)) + 256 * assert(mem.read_u8(address + 1))
end
-- Each group runs in its own deferred callback. Reacquire pointers in that
-- callback; only copied observations and the step index survive across frames.
local steps = {}
steps[#steps + 1] = {"player_hp", function()
local actor, why = game.player()
checks.player = actor ~= nil
log(actor and "smoke: player present" or ("smoke: player absent (" .. tostring(why) .. ")"))
observe("hp", function()
assert(actor, "no player")
local stats = assert(game.stats(actor))
local entry = ptr(stats + assert(cdal.value("StatusComponent::StatEntries")))
-- Entry +0 is the stat key (GetValueAt). Do not silently assume slot zero is HP.
assert(u16(entry) == 0, "first stat entry is not HP; mapping needs inspection")
local hp = assert(mem.read_u64(entry + assert(cdal.value("StatEntry::Current"))))
log("smoke: hp stored_x1000=" .. hp)
end)
end}
steps[#steps + 1] = {"equipment", function()
observe("equipment", function()
local actor = assert(game.player())
local component = assert(game.equipment(actor))
local storage = ptr(component + assert(cdal.value("CommonEquipSlotActorComponent::StorageOffset")))
-- InsertSlot's storage callee: ptr +8, u32 length +0x10, stride 0xD0, tag +0xC8, row +8.
local count = assert(mem.read_u32(storage + 0x10))
assert(count <= 64, "equipment slot limit")
local records = count > 0 and ptr(storage + 8) or 0
local items = {}
for i = 0, count - 1 do
local record = records + i * 0xD0
items[#items + 1] = u16(record + 0xC8) .. ":" .. u16(record + 8)
end
assert(mem.read_u32(storage + 0x10) == count, "equipment changed during read")
if count > 0 then assert(mem.read_ptr(storage + 8) == records, "equipment changed during read") end
log("smoke: equipment slots=" .. count .. " tag:item=" .. table.concat(items, ","))
end)
end}
steps[#steps + 1] = {"inventory", function()
observe("inventory", function()
assert(item_id >= 0, "Money_Copper not found in loaded item definitions")
local count = assert(game.inventory.count(item_id))
log("smoke: inventory item=" .. item_id .. " count=" .. count)
end)
end}
-- Expected scope refusals and unloaded caches are diagnostic observations,
-- not successful gameplay assertions.
local function read_line(label, fn, format)
local value, reason = fn()
log(value ~= nil and ("smoke: " .. label .. format(value)) or
("smoke: " .. label .. " unavailable (" .. tostring(reason) .. ")"))
end
local function refusal(label, fn)
local value, reason = fn()
if value == nil then
log("smoke: " .. label .. " refused (" .. tostring(reason) .. ")")
elseif type(value) == "table" or type(value) == "boolean" then
log("smoke: " .. label .. " snapshot ok (SnapshotProvider=1)")
else
log("smoke: " .. label .. " unexpected result")
end
end
steps[#steps + 1] = {"item_definitions", function()
read_line("item_name", function() return game.item_name(item_id) end, function(v) return "=" .. v end)
read_line("item_info", function() return game.item_info(item_id) end,
function(v) return " row=" .. v.row .. " key=" .. v.key .. " name=" .. v.name .. " max_stack=" .. v.max_stack end)
end}
steps[#steps + 1] = {"stat_name", function()
read_line("stat_name", function() return game.stat_name(0) end, function(v) return "=" .. v end)
end}
steps[#steps + 1] = {"world", function()
read_line("time", game.time, function(v) return " day=" .. v.day .. " hour=" .. v.hour .. " minute=" .. v.minute end)
read_line("weather", game.weather, function(v) return " rain_base=" .. v.rain_base .. " snow_base=" .. v.snow_base end)
end}
steps[#steps + 1] = {"scope_refusals", function()
local actor = game.player()
refusal("actors", function() return game.actors(16) end)
refusal("position", function() return game.position(actor) end)
refusal("stats.read", function() return game.stats.read(actor, 0) end)
refusal("in_cutscene", game.in_cutscene)
end}
local function snapshot_step(index)
bsx.after(0, function()
if finished then return end
local step = steps[index]
local before = os.clock()
local ok, why = pcall(step[2])
local elapsed = (os.clock() - before) * 1000
if not ok then
failures[#failures + 1] = "step:" .. step[1]
log("smoke: step " .. step[1] .. " unavailable (" .. tostring(why) .. ")")
end
log(string.format("smoke: step %s elapsed_ms=%.3f frame=%d", step[1], elapsed, frames))
if index < #steps then snapshot_step(index + 1)
else bsx.after(30000, function() checks.timer = true; finish() end) end
end)
end
local function find_money()
if item_id ~= -1 then snapshot_step(1); return end
local row = 0
local function scan()
-- Bound work on each callback. Skip unloaded slots without invoking a
-- native loader; retain only the copied row number across FRAMEs.
for _ = 1, 32 do
if row >= 65535 then break end
local info, why = game.item_info(row)
if info and info.name == "Money_Copper" then
item_id = info.row
log("smoke: money name=" .. info.name .. " row=" .. info.row .. " key=" .. info.key)
snapshot_step(1)
return
end
if not info and why ~= "definition row not loaded" and why ~= "fallback item row" then
log("smoke: money lookup stopped (" .. tostring(why) .. ")")
snapshot_step(1)
return
end
row = row + 1
end
if row >= 65535 then
log("smoke: Money_Copper not found in loaded item definitions")
snapshot_step(1)
else bsx.after(0, scan) end
end
bsx.after(0, scan)
end
local function subscribe(name, fn)
local ok, why = pcall(bsx.on, name, fn)
if not ok then
failures[#failures + 1] = "subscribe:" .. name
log("smoke: event unavailable " .. name .. " (" .. tostring(why) .. ")")
end
return ok
end
subscribe("frame", function(e)
-- Keep draining even after the verdict; a busy log window must not lose it.
flush()
if finished then return end
frames = frames + 1
checks.frame = true
-- Respect the host's ten-line/second budget; keep unsent copied observations.
if os.time() >= next_log then
log("smoke: frame count=" .. frames .. " host_frame=" .. e.frame)
next_log = os.time() + 10
end
end)
subscribe("save_load", function(e)
if finished then return end
checks.save_load = true
log("smoke: SAVE_LOAD key=" .. e.key .. " replayed=" .. tostring(e.replayed))
end)
local has_loaded = subscribe("save_loaded", function(e)
if finished or loaded then return end
loaded = true
checks.save_loaded = true
log("smoke: SAVE_LOADED generation=" .. e.generation .. " key=" .. e.key)
-- One chain from the first completion; another save cannot restart it.
find_money()
end)
for _, name in ipairs({"equip_applied", "unequip_applied"}) do
subscribe(name, function(e)
if finished or not loaded or e.controlled ~= true or checks[name] then return end
checks[name] = true
log("smoke: " .. string.upper(name) .. " tag=" .. e.tag .. " item=" .. e.row .. " controlled=true")
end)
end
-- No completion means the timed run cannot start. Report that explicitly instead of claiming PASS.
if not has_loaded then finish() end