Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
151 lines (151 loc) · 7.65 KB
/
Copy pathmain.lua
File metadata and controls
151 lines (151 loc) · 7.65 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
-- SPDX-License-Identifier: LicenseRef-CDAL-BSX-Source-Available-1.0
-- Review SlotTag in settings.ini before approving the folder's new digest.
local tag = config.int("EquipToggle", "SlotTag", -1)
-- The loaded world can still be behind the game's "hold to continue" screen at SAVE_LOADED.
local start_delay = math.max(0, config.int("EquipToggle", "StartDelayFrames", 300))
local retry_frames, max_tries = 120, 30
local pending, armed, last_save, frame = nil, nil, nil, 0
local function log(s) bsx.log("equip_toggle: " .. s) end
local function cancel(why)
if pending or armed then log("cancelled: " .. why) end
pending, armed = nil, nil
end
local function ptr(at)
local p, why = mem.read_ptr(at)
assert(p and p ~= 0, why or "null pointer")
return p
end
local function u16(at) return assert(mem.read_u8(at)) + 256 * assert(mem.read_u8(at + 1)) end
local function offset(name) return assert(cdal.value(name)) end
local function word(at) return assert(mem.read_u64(at)) end
local function positive(at)
local n = word(at)
-- read_u64 is unsigned; signed positive amounts fit below 2^63.
return n ~= "0" and (#n < 19 or (#n == 19 and n < "9223372036854775808"))
end
local function equipped(actor)
local component = assert(game.equipment(actor))
local storage = ptr(component + offset("CommonEquipSlotActorComponent::StorageOffset"))
local count = assert(mem.read_u32(storage + 0x10))
assert(count <= 256, "equipment bound")
local records = count > 0 and ptr(storage + 8) or 0
local result
for i = 0, count - 1 do
local p = records + i * offset("EquipmentSlotRecord::Stride")
if u16(p + offset("EquipmentSlotRecord::SlotTag")) == tag then
local row = u16(p + offset("InventoryItem::Row"))
if row ~= 65535 and positive(p + offset("InventoryItem::Amount")) then
assert(not result, "duplicate equipped tag")
result = {row = row, word = word(p)}
end
end
end
assert(mem.read_u32(storage + 0x10) == count, "equipment changed")
return result
end
local function locate(actor, wanted)
local components = ptr(actor + offset("Actor::Components"))
local holder = ptr(components + offset("ActorComponents::Inventory"))
assert(ptr(holder) == cdal.address("pa::ClientInventoryActorComponent::vftable"), "inventory type")
local count = assert(mem.read_u32(holder + offset("InventoryHolder::BucketCount")))
assert(count <= 256, "bucket bound")
local buckets = count > 0 and ptr(holder + offset("InventoryHolder::Buckets")) or 0
local found, visited = nil, 0
for b = 0, count - 1 do
local bucket = ptr(buckets + b * 8)
local slots_count = u16(bucket + offset("InventoryBucket::SlotCount"))
visited = visited + slots_count
assert(slots_count < 32768 and visited <= 4096, "inventory bound")
local slots = slots_count > 0 and ptr(bucket) or 0
for i = 0, slots_count - 1 do
local p = slots + i * offset("InventoryItem::Stride")
if u16(p + offset("InventoryItem::Row")) == wanted.row and word(p) == wanted.word
and positive(p + offset("InventoryItem::Amount")) then
assert(not found, "ambiguous item identity")
-- +0x10 is the external bucket tag, not the array ordinal.
found = {bucket = u16(bucket + 0x10), index = i}
end
end
end
return found
end
local function same_player()
local actor = assert(game.player())
assert(actor == pending.actor and mem.read_u32(actor + offset("Actor::Key")) == pending.key, "player changed")
return actor
end
local function applied(e, phase)
if not pending or pending.phase ~= phase or e.controlled ~= true or e.generation ~= pending.generation
or e.tag ~= tag or e.row ~= pending.row or e.item_word0 ~= pending.word then return end
log((phase == "remove" and "UNEQUIP_APPLIED" or "EQUIP_APPLIED") .. " tag=" .. tag .. " row=" .. e.row)
if phase == "remove" then pending.phase = "find"
else pending.phase = "confirm" end
end
local events = pcall(function()
bsx.on("unequip_applied", function(e) applied(e, "remove") end)
bsx.on("equip_applied", function(e) applied(e, "equip") end)
bsx.on("save_load", function() cancel("new save request") end)
bsx.on("load_request", function() cancel("new loading request") end)
end)
if not events then log("required completion/load events unavailable") end
if not game.request then log("read-only; changes approval required") end
if tag < 0 or tag > 32767 then log("choose SlotTag in settings.ini and approve its digest") end
local function request_unequip()
local generation = armed.generation
armed.tries = armed.tries + 1
local ok, why = pcall(function()
local actor = assert(game.player())
local item = assert(equipped(actor), "chosen slot is empty")
assert(item.word ~= "0" and item.word ~= "18446744073709551615", "no usable item identity")
log("request unequip tag=" .. tag .. " row=" .. item.row .. " try=" .. armed.tries)
local queued, reason = game.request.unequip(tag)
if not queued then
-- Nothing was sent, so asking again later cannot double a request.
if reason == "native builder did not send a packet" and armed.tries < max_tries then
log("unequip not sent yet; trying again in " .. retry_frames .. " frames")
armed.due = frame + retry_frames
return
end
cancel("unequip refused: " .. tostring(reason)); return
end
armed = nil
pending = {actor = actor, key = assert(mem.read_u32(actor + offset("Actor::Key"))),
generation = generation, row = item.row, word = item.word, phase = "remove", deadline = frame + 600}
log("unequip queued; waiting for UNEQUIP_APPLIED")
end)
if not ok then cancel(tostring(why)); log("stopped: " .. tostring(why)) end
end
local loaded_ok = pcall(bsx.on, "save_loaded", function(e)
cancel("replacement SAVE_LOADED")
if last_save == e.generation then return end
last_save = e.generation
if not events or not game.request or tag < 0 or tag > 32767 then return end
armed = {generation = e.generation, due = frame + start_delay, tries = 0}
log("save loaded; unequip in " .. start_delay .. " frames")
end)
if not loaded_ok then log("SAVE_LOADED unavailable") end
bsx.on("frame", function(e)
frame = e.frame
if armed and frame >= armed.due then request_unequip() end
if not pending then return end
if frame > pending.deadline then cancel("timeout; inspect current equipment before retrying"); return end
local ok, why = pcall(function()
local actor = same_player()
if pending.phase == "find" then
assert(not equipped(actor), "slot was filled by another or fallback request")
local item = locate(actor, pending)
if not item then return end -- replication can follow the removal observation
pending.phase = "equip"
log("request equip bucket=" .. item.bucket .. " index=" .. item.index .. " tag=" .. tag)
local queued, reason = game.request.equip(item.bucket, item.index, tag)
if not queued then cancel("equip refused: " .. tostring(reason)); return end
log("equip queued; waiting for EQUIP_APPLIED")
elseif pending.phase == "confirm" then
local item = assert(equipped(actor), "confirmed slot absent")
assert(item.row == pending.row and item.word == pending.word, "confirmed item differs")
log("complete: original item observed in chosen slot")
pending = nil
end
end)
if not ok then cancel(tostring(why)) end
end)