Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
478 lines (451 loc) · 17.9 KB
/
Copy pathmain.lua
File metadata and controls
478 lines (451 loc) · 17.9 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
-- Appearance Studio: Lua API 4 visual gear hiding (keeps stats), equipped gear inspector,
-- JSON visibility presets, and Character Customization observer.
local function clamp_text(text)
local s = tostring(text or ""):gsub("%z", " ")
if #s <= 127 then return s end
return string.sub(s, 1, 124) .. "..."
end
local function toast_msg(msg, level)
if bsx.ui and bsx.ui.toast then
bsx.ui.toast(clamp_text(msg), { level = level or "info", duration_ms = 2600 })
end
end
local function json_encode_flat(tbl)
local keys = {}
for k in pairs(tbl) do
if type(k) == "string" then keys[#keys + 1] = k end
end
table.sort(keys)
local lines = { "{" }
for i = 1, #keys do
local k = keys[i]
local v = tbl[k]
local vs = tostring(math.floor(tonumber(v) or -1))
local comma = (i < #keys) and "," or ""
lines[#lines + 1] = ' "' .. k .. '": ' .. vs .. comma
end
lines[#lines + 1] = "}"
return table.concat(lines, "\n") .. "\n"
end
local function json_decode_flat(text)
if type(text) ~= "string" then return nil end
local out = {}
local pos = 1
local len = #text
while pos <= len do
local nl = string.find(text, "\n", pos, true)
local line = nl and string.sub(text, pos, nl - 1) or string.sub(text, pos)
pos = nl and (nl + 1) or (len + 1)
local q1 = string.find(line, '"', 1, true)
if q1 then
local q2 = string.find(line, '"', q1 + 1, true)
if q2 then
local col = string.find(line, ":", q2 + 1, true)
if col then
local key = string.sub(line, q1 + 1, q2 - 1)
local val_str = string.sub(line, col + 1)
while #val_str > 0 and (string.sub(val_str, 1, 1) == " " or string.sub(val_str, 1, 1) == "\t") do
val_str = string.sub(val_str, 2)
end
while #val_str > 0 do
local last = string.sub(val_str, -1)
if last == " " or last == "\t" or last == "\r" or last == "," then
val_str = string.sub(val_str, 1, -2)
else
break
end
end
local num = tonumber(val_str)
if num ~= nil then out[key] = math.floor(num) end
end
end
end
end
return out
end
local PART_GROUPS = {
{ id = "helmet", label = "Hide Helmet / Headgear", terms = { "helm", "helmet", "hat", "hood", "mask", "crown", "circlet", "headgear", "head_", "earring" } },
{ id = "cape", label = "Hide Cape / Cloak", terms = { "cape", "cloak", "mantle", "scarf", "back", "shawl", "poncho" } },
{ id = "lantern", label = "Hide Lantern", terms = { "lantern", "lamp" } },
{ id = "bow", label = "Hide Bow & Quiver", terms = { "bow", "quiver", "arw", "arrow" } },
{ id = "weapons", label = "Hide Weapons & Shield", terms = { "mainweapon", "subweapon", "weapon", "sword", "axe", "dagger", "spear", "shield", "mace", "hammer", "bomb" } },
}
local function match_terms(name, terms)
local lo = tostring(name or ""):lower()
if #lo == 0 then return false end
for i = 1, #terms do
if lo:find(terms[i], 1, true) then return true end
end
return false
end
local function categorize_part(e)
if e.override_mode == 0 then return "Hidden" end
if e.override_mode == 1 then return "Forced Show" end
local nm = tostring(e.name or ""):lower()
if nm:find("weapon", 1, true) or nm:find("sword", 1, true) or nm:find("axe", 1, true) or
nm:find("dagger", 1, true) or nm:find("bow", 1, true) or nm:find("arw", 1, true) or
nm:find("quiver", 1, true) or nm:find("shield", 1, true) or nm:find("bomb", 1, true) then
return "Weapons"
end
if nm:find("helm", 1, true) or nm:find("head", 1, true) or nm:find("cape", 1, true) or
nm:find("cloak", 1, true) or nm:find("armor", 1, true) or nm:find("glove", 1, true) or
nm:find("boot", 1, true) or nm:find("hood", 1, true) then
return "Armor & Head"
end
if nm:find("lantern", 1, true) or nm:find("earring", 1, true) or nm:find("plug", 1, true) or nm:find("gauntlet", 1, true) then
return "Accessories"
end
return "Other"
end
local auto_refresh = config.bool("AppearanceStudio", "AutoRefresh", true)
local world_ready = false
local logged_ready = false
local parts_list = {}
local parts_view = {}
local part_idx = 1
local gear_slots = {}
local gear_idx = 1
local preset_labels = { "Preset 1", "Preset 2", "Preset 3" }
local preset_idx = 1
local group_cbs = {}
local panel = bsx.ui.panel("Appearance Studio")
if panel and panel.set_theme then
pcall(panel.set_theme, panel, {
accent = "#A855F7",
header = "#241935",
child_bg = "#13111A",
frame_bg = "#1D1A26",
border = "#483766",
})
end
local status_id = panel:text("Appearance Studio: waiting for world_ready...")
local quick_info_id, gear_info_id, gear_listbox_id, part_detail_id, part_listbox_id, preset_combo_id, cust_info_id
local function set_status(msg)
panel:set_text(status_id, clamp_text(msg))
end
local function mode_tag(mode)
if mode == 0 then return "HIDDEN" end
if mode == 1 then return "SHOW" end
return "native"
end
local function selected_part_entry()
return parts_view[part_idx]
end
local function update_selected_part_detail()
local e = selected_part_entry()
if not e then
panel:set_text(part_detail_id, string.format("Parts: %d observed", #parts_list))
return
end
local nm = e.name or string.format("0x%X", e.key or 0)
panel:set_text(part_detail_id, clamp_text(string.format(
"#%d/%d %s [0x%X / %d] -> %s",
part_idx, #parts_list, nm, e.key or 0, e.key or 0, mode_tag(e.override_mode))))
end
local function update_quick_summary()
local hidden_cnt = 0
for i = 1, #parts_list do
if parts_list[i].override_mode == 0 then hidden_cnt = hidden_cnt + 1 end
end
if quick_info_id then
panel:set_text(quick_info_id, clamp_text(string.format(
"Active Visual Hides: %d part(s) hidden | Live character MeshSet (%d parts)", hidden_cnt, #parts_list)))
end
for _, g in ipairs(PART_GROUPS) do
local cb = group_cbs[g.id]
if cb then
local any_hidden = false
for i = 1, #parts_list do
local e = parts_list[i]
if e.override_mode == 0 and match_terms(e.name, g.terms) then
any_hidden = true
break
end
end
pcall(panel.set_value, panel, cb, any_hidden)
end
end
end
local function refresh_parts(silent)
if not world_ready then
if not silent then set_status("Waiting for world_ready...") end
return
end
local list, why = game.appearance.parts()
if not list then
if not silent then set_status("Parts unavailable: " .. tostring(why)) end
return
end
local prev_e = selected_part_entry()
local prev_key = prev_e and prev_e.key or nil
parts_list = list
parts_view = {}
local payload = {}
local target_idx = 1
if #list == 0 then
payload[1] = { label = "(no parts observed yet - draw weapon / move)", category = "Info" }
else
for i = 1, #list do
local e = list[i]
local nm = (type(e.name) == "string" and #e.name > 0) and e.name or string.format("Part_0x%X", e.key or 0)
local lbl = clamp_text(string.format("%s [%d]", nm, e.key or 0))
parts_view[#parts_view + 1] = e
payload[#payload + 1] = {
label = lbl,
category = categorize_part(e),
}
if prev_key and e.key == prev_key then
target_idx = #parts_view
end
end
end
part_idx = (#parts_view > 0) and target_idx or 0
panel:set_items(part_listbox_id, payload, part_idx)
update_selected_part_detail()
update_quick_summary()
if not silent then
set_status(string.format("Refreshed %d observed player part(s)", #list))
end
end
local function apply_group_hide(group, hide_on)
if not world_ready then
update_quick_summary()
return set_status("Waiting for world_ready...")
end
if #parts_list == 0 then refresh_parts(true) end
local matched, changed = 0, 0
local mode = hide_on and 0 or -1
for i = 1, #parts_list do
local e = parts_list[i]
if e.key and match_terms(e.name, group.terms) then
matched = matched + 1
if game.appearance.set_part_visibility(e.key, mode) then
changed = changed + 1
end
end
end
refresh_parts(true)
if matched == 0 then
set_status("No matching parts found on current character; click Refresh Parts")
toast_msg("No matching parts found in current MeshSet", "warn")
else
local verb = hide_on and "Hidden" or "Restored"
local msg = string.format("%s %d part(s) (%s)", verb, changed, group.id)
set_status(msg)
toast_msg(msg, "info")
end
end
local function apply_selected_part(mode)
if not world_ready then return set_status("Waiting for world_ready...") end
local e = selected_part_entry()
if not e or not e.key then
return set_status("Select a part first (press Refresh Parts)")
end
local ok, why = game.appearance.set_part_visibility(e.key, mode)
if ok then
local nm = (type(e.name) == "string" and #e.name > 0) and e.name or ("key " .. tostring(e.key))
local msg = string.format("%s -> %s", nm, mode_tag(mode))
set_status(msg)
toast_msg(msg, "info")
refresh_parts(true)
else
set_status("Override blocked: " .. tostring(why))
toast_msg("Override blocked: " .. tostring(why), "warn")
end
end
local function refresh_gear()
if not world_ready then return set_status("Waiting for world_ready...") end
local eq, why = game.equipment_snapshot()
if not eq then
gear_slots = {}
panel:set_items(gear_listbox_id, { { label = "(gear unavailable)", category = "Info" } }, 0)
panel:set_text(gear_info_id, clamp_text("Gear unavailable: " .. tostring(why)))
return
end
local slots = {}
local payload = {}
for i = 1, #eq do
local row = eq[i].row
local cnt = tonumber(eq[i].count) or 0
if row and row > 0 and cnt > 0 then
local disp = game.item_display_name(row)
local internal = game.item_name(row)
local title = (disp and disp ~= "" and internal and disp ~= internal)
and string.format("%s (%s)", disp, internal) or (disp or internal or ("row#" .. tostring(row)))
local st = eq[i].slot_tag or 0
local lbl = clamp_text(string.format("Slot %d: %s [r%d]", st, title, row))
slots[#slots + 1] = { slot_tag = st, row = row, label = title }
payload[#payload + 1] = { label = lbl, category = "Slot " .. tostring(st) }
end
end
gear_slots = slots
if #payload == 0 then
gear_idx = 0
panel:set_items(gear_listbox_id, { { label = "(no equipped gear)", category = "Info" } }, 0)
else
if gear_idx < 1 or gear_idx > #gear_slots then gear_idx = 1 end
panel:set_items(gear_listbox_id, payload, gear_idx)
end
panel:set_text(gear_info_id, clamp_text(string.format("Equipped gear: %d slot(s)", #slots)))
end
local function refresh_customization(silent)
if not cust_info_id or not world_ready then return end
local rec, why = game.appearance.customization()
if not rec then
if not silent then panel:set_text(cust_info_id, clamp_text("Customization: " .. tostring(why))) end
return
end
if not rec.valid then
panel:set_text(cust_info_id, "Customization: open Character Creator to observe edits")
return
end
local mp = rec.mesh_parts or {}
local dc = rec.decorations or {}
panel:set_text(cust_info_id, clamp_text(string.format(
"Customization: %d mesh part(s), %d decoration(s)", #mp, #dc)))
end
panel:section("Quick Visual Hide (Keeps Item & Stats)", true)
quick_info_id = panel:text("Keeps stats! Toggles apply live via priority layer 15. Hidden: 0")
for _, g in ipairs(PART_GROUPS) do
local grp = g
group_cbs[grp.id] = panel:checkbox(grp.label, false, function(on)
apply_group_hide(grp, on == true)
end)
end
panel:button("Refresh Parts", function() refresh_parts(false) end)
panel:same_line()
panel:button("Clear All Overrides", function()
if not world_ready then return set_status("Waiting for world_ready...") end
local ok, why = game.appearance.clear_part_overrides()
if ok then
set_status("Cleared all visual hides")
toast_msg("Cleared all visual hides", "info")
refresh_parts(true)
else
set_status("Clear blocked: " .. tostring(why))
end
end)
panel:end_section()
panel:section("All Character Mesh Parts (Advanced)", false)
part_detail_id = panel:text("Parts: 0 observed")
part_listbox_id = panel:listbox("Observed Parts", { { label = "(no parts observed yet)", category = "Info" } }, { height = 220, searchable = true }, function(idx)
part_idx = idx
update_selected_part_detail()
end)
panel:button("Hide (0)", function() apply_selected_part(0) end)
panel:same_line()
panel:button("Show (1)", function() apply_selected_part(1) end)
panel:same_line()
panel:button("Native (-1)", function() apply_selected_part(-1) end)
panel:end_section()
panel:section("Equipped Gear (Unequip Removes Stats)", false)
gear_info_id = panel:text("Unequip moves item to bag & removes stats (use Quick Hide above to keep stats).")
gear_listbox_id = panel:listbox("Equipped Gear", { { label = "(press Refresh Gear)", category = "Info" } }, { height = 160, searchable = true }, function(idx)
gear_idx = idx
end)
panel:button("Refresh Gear", refresh_gear)
panel:same_line()
panel:button("Unequip Selected", function()
if not world_ready then return set_status("Waiting for world_ready...") end
local entry = gear_slots[gear_idx]
if not entry then return set_status("Select an equipped gear row first") end
if not (game.equipment and game.equipment.unequip) then
return set_status("Unequip unavailable (ServerCommands=1 required)")
end
local ok, why = game.equipment.unequip(entry.slot_tag)
if ok then
local msg = string.format("Unequipped slot %d (%s)", entry.slot_tag, entry.label)
set_status(msg)
toast_msg(msg, "info")
bsx.after(150, refresh_gear)
else
set_status("Unequip blocked: " .. tostring(why))
end
end)
panel:end_section()
panel:section("Visibility Presets & Customization", false)
cust_info_id = panel:text("Customization: waiting for world_ready...")
preset_combo_id = panel:combo("Preset Slot", preset_labels, preset_idx, function(idx) preset_idx = idx end)
panel:button("Save Preset", function()
if not world_ready then return set_status("Waiting for world_ready...") end
local list = game.appearance.parts()
if not list then return set_status("Parts unavailable") end
local flat = {}
local count = 0
for i = 1, #list do
local ov = list[i].override_mode
if ov == 0 or ov == 1 then
flat["p_" .. tostring(list[i].key)] = ov
count = count + 1
end
end
local path = "preset_" .. tostring(preset_idx) .. ".json"
if not (bsx.fs and bsx.fs.write) then return set_status("bsx.fs.write unavailable") end
local rok, res, why = pcall(bsx.fs.write, path, json_encode_flat(flat))
local ok = rok and res == true
if ok then
set_status(string.format("Saved %d override(s) to data/%s", count, path))
toast_msg("Saved Appearance Preset #" .. tostring(preset_idx), "info")
else
set_status("Save failed: " .. tostring(why or res))
end
end)
panel:button("Load Preset", function()
if not world_ready then return set_status("Waiting for world_ready...") end
local path = "preset_" .. tostring(preset_idx) .. ".json"
if not (bsx.fs and bsx.fs.read) then return set_status("bsx.fs.read unavailable") end
local rok, raw = pcall(bsx.fs.read, path)
local flat = (rok and type(raw) == "string") and json_decode_flat(raw) or nil
if not flat then return set_status("Preset " .. tostring(preset_idx) .. " not found") end
game.appearance.clear_part_overrides()
local applied = 0
for k, v in pairs(flat) do
if string.sub(k, 1, 2) == "p_" then
local key = tonumber(string.sub(k, 3))
if key and key >= 1 and (v == 0 or v == 1) then
if game.appearance.set_part_visibility(math.floor(key), v) then
applied = applied + 1
end
end
end
end
refresh_parts(true)
set_status(string.format("Loaded %d override(s) from data/%s", applied, path))
toast_msg("Loaded Appearance Preset #" .. tostring(preset_idx), "info")
end)
local function reset_state()
world_ready = false
logged_ready = false
set_status("Appearance Studio: waiting for world_ready...")
end
local function on_world_entered()
if world_ready then return end
world_ready = true
refresh_parts(true)
refresh_gear()
refresh_customization(true)
if not logged_ready then
logged_ready = true
set_status("Appearance Studio ready")
bsx.log("APPEARANCE_STUDIO_READY")
end
end
bsx.on("world_ready", on_world_entered)
bsx.on("save_load", reset_state)
pcall(bsx.on, "load_request", reset_state)
pcall(bsx.on, "title_screen", reset_state)
local poll_tick = 0
bsx.on("frame", function()
if not world_ready then
local ps = game.player_state()
if ps and ps.world_ready then on_world_entered() end
return
end
if not auto_refresh then return end
poll_tick = poll_tick + 1
if poll_tick >= 90 then
poll_tick = 0
refresh_parts(true)
refresh_customization(true)
end
end)