Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
171 lines (170 loc) · 7.76 KB
/
Copy pathmain.lua
File metadata and controls
171 lines (170 loc) · 7.76 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
-- Session-only, inventory-only example using equip/unequip requests.
local saved, pending, save_generation, frame = nil, nil, nil, 0
local function log(s) bsx.log("outfits: " .. s) end
local function cancel(why)
if pending or saved then log("cancelled: " .. why .. "; save the current set again") end
pending, saved = nil, nil
end
local function same(a, b)
return a and b and a.row == b.row and a.count == b.count and a.instance_id == b.instance_id
end
local function usable(item)
assert(item.instance_id and item.instance_id ~= "0", "item has no usable ItemNo")
assert(item.slot_tag == nil or item.slot_tag <= 32767, "unsupported request slot tag")
end
local function snapshots()
local eq, why = game.equipment_snapshot()
assert(eq, why)
local inv, reason = game.inventory.list(4096)
assert(inv, reason)
assert(not inv.truncated and not eq.truncated, "snapshot truncated")
assert(eq.generation == inv.generation and eq.actor_key == inv.actor_key, "snapshot generation changed")
local tags = {}
for _, item in ipairs(eq) do
assert(not tags[item.slot_tag], "duplicate equipment tag")
tags[item.slot_tag] = item
end
return eq, inv, tags
end
local function guarded(fn)
local ok, why = pcall(fn)
if not ok then cancel(tostring(why)); log("stopped: " .. tostring(why)) end
end
local function current(eq)
assert(saved and eq.generation == saved.generation and eq.actor_key == saved.actor_key
and save_generation == saved.save_generation, "player or save generation changed")
end
local function locate(inv, wanted)
local found
for _, item in ipairs(inv) do
if item.instance_id == wanted.instance_id then
assert(same(item, wanted) and not found, "changed or ambiguous inventory instance")
found = item
end
end
return found
end
local function validate_wanted(inv, tags, locations)
for tag, wanted in pairs(saved.tags) do
local equipped
for other_tag, item in pairs(tags) do
if item.instance_id == wanted.instance_id then
assert(other_tag == tag and same(item, wanted) and not equipped, "saved instance changed slot or contents")
equipped = item
end
end
local item = locate(inv, wanted)
assert(not (equipped and item), "instance exists in both snapshots")
assert(equipped or item, "saved instance is missing from owned inventory")
if item then
local old = locations[wanted.instance_id]
assert(not old or (old.bucket == item.bucket and old.index == item.index), "inventory location changed")
locations[wanted.instance_id] = {bucket = item.bucket, index = item.index}
end
end
end
local function equipment_matches(tags, expected)
for tag, item in pairs(tags) do if not same(item, expected[tag]) then return false end end
for tag, item in pairs(expected) do if not same(item, tags[tag]) then return false end end
return true
end
local function copy_tags(tags)
local result = {}
for tag, item in pairs(tags) do result[tag] = item end
return result
end
local function applied(e, kind)
local op = pending and pending.op
if not op or op.kind ~= kind or e.controlled ~= true or e.generation ~= saved.save_generation
or e.tag ~= op.tag or e.row ~= op.item.row or e.item_word0 ~= op.item.instance_id
or e.count ~= op.item.count then return end
op.applied = true
end
local events = pcall(function()
bsx.on("equip_applied", function(e) applied(e, "equip") end)
bsx.on("unequip_applied", function(e) applied(e, "unequip") end)
bsx.on("save_load", function() cancel("save request"); save_generation = nil end)
bsx.on("load_request", function() cancel("loading request"); save_generation = nil end)
bsx.on("title_screen", function() cancel("title screen"); save_generation = nil end)
bsx.on("save_loaded", function(e) cancel("save replaced"); save_generation = e.generation end)
end)
if not events then log("required lifecycle/APPLIED events unavailable") end
if not game.request then log("read-only; Changes approval is required to restore") end
bsx.hotkey("ctrl+shift+f7", function()
if pending then return end
guarded(function()
assert(events and save_generation, "wait for SAVE_LOADED before saving an outfit")
local eq, inv, tags = snapshots()
local identities = {}
for _, item in ipairs(eq) do
usable(item)
assert(not identities[item.instance_id], "ambiguous equipped instance")
identities[item.instance_id] = true
end
saved = {tags = tags, generation = eq.generation, actor_key = eq.actor_key,
save_generation = save_generation, size = #eq}
validate_wanted(inv, tags, {})
log("saved " .. #eq .. " equipped items for this session")
end)
end)
bsx.hotkey("ctrl+shift+f8", function()
if pending then return end -- no second send while an outcome is uncertain
if not game.request then log("restore requires Changes approval"); return end
guarded(function()
assert(saved, "save an outfit with Ctrl+Shift+F7 first")
local eq, inv, tags = snapshots()
current(eq)
local locations = {}
validate_wanted(inv, tags, locations)
pending = {expected = tags, locations = locations}
log("restore started; each request waits for APPLIED and matching state")
end)
end)
bsx.on("frame", function(e)
frame = e.frame
if not pending then return end
guarded(function()
local eq, inv, tags = snapshots()
current(eq)
if pending.op then
local op = pending.op
assert(frame <= op.deadline, "APPLIED/state timeout; uncertain request will not be retried")
local after = copy_tags(pending.expected)
after[op.tag] = op.kind == "equip" and op.item or nil
assert(equipment_matches(tags, pending.expected) or equipment_matches(tags, after), "unexpected equipment change")
if not op.applied or not equipment_matches(tags, after) then return end
pending.expected, pending.op = after, nil
-- Reacquire on a later FRAME before making another request.
return
end
assert(equipment_matches(tags, pending.expected), "equipment changed before request")
validate_wanted(inv, tags, pending.locations)
local op
-- Remove extra or replaced pieces before putting saved pieces back.
for _, item in ipairs(eq) do
if not same(item, saved.tags[item.slot_tag]) then
usable(item); op = {kind = "unequip", item = item, tag = item.slot_tag}; break
end
end
if not op then
local order = {}
for tag in pairs(saved.tags) do order[#order + 1] = tag end
table.sort(order)
for _, tag in ipairs(order) do
if not tags[tag] then
local item = assert(locate(inv, saved.tags[tag]), "saved instance missing before equip")
op = {kind = "equip", item = saved.tags[tag], tag = tag, bucket = item.bucket, index = item.index}
break
end
end
end
if not op then log("restored " .. saved.size .. " equipped items"); pending = nil; return end
op.deadline = frame + 600
pending.op = op -- arm before the request; a sender result is never APPLIED
local queued, why
if op.kind == "equip" then queued, why = game.request.equip(op.bucket, op.index, op.tag)
else queued, why = game.request.unequip(op.tag) end
if not queued then cancel(op.kind .. " refused or uncertain: " .. tostring(why)); return end
log(op.kind .. " queued for tag " .. op.tag .. "; waiting for APPLIED")
end)
end)