-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsbridge.lua
More file actions
618 lines (575 loc) · 26.9 KB
/
Copy pathlsbridge.lua
File metadata and controls
618 lines (575 loc) · 26.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
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
addon.name = 'lsbridge'
addon.author = 'TreeFidyDad'
addon.version = '1.4'
addon.desc = 'Two linkshell <-> Discord bridge via file IPC with Jarvis bot.'
addon.link = 'https://github.com/TreeFidyDad/lsbridge'
require('common')
local imgui = require('imgui')
local chat = require('chat')
------------------------------------------------------------
-- Config
------------------------------------------------------------
local DATA_DIR = 'C:\\Users\\Blake\\ffxi-jarvis\\data'
local FFXI_TO_DISCORD = DATA_DIR .. '\\ffxi_to_discord.txt'
local DISCORD_TO_FFXI = DATA_DIR .. '\\discord_to_ffxi.txt'
local DEBUG_LOG = DATA_DIR .. '\\modes_debug.txt'
-- Incoming-packet diagnostics (used to reverse-engineer the HorizonXI
-- "Linkshell online members" packet so we can list who's online like the
-- in-game Linkshell window). See /lsbridge pktscan and /lsbridge pktdump.
local PACKET_LOG = DATA_DIR .. '\\packets_debug.txt'
-- Map each text_in chat mode to which linkshell it belongs to.
-- Your own messages and other players' messages use different mode numbers.
-- LS1 on HorizonXI: 6 = self, 14 = others (confirmed via mode logging).
-- LS2 on HorizonXI: 27 = self, 15 = others (BEST GUESS - verify with /lsbridge logmode).
-- NOTE: FFXI sometimes ORs high-order bitflags onto the base mode (e.g. 33554446 = 14 + flags).
-- We mask to the lower 8 bits to extract the base chat type.
local BASE_MODE_TO_LS = {
[6] = 'LS1',
[14] = 'LS1',
[27] = 'LS2',
[15] = 'LS2',
}
local function modeToLS(mode)
local base = mode % 256 -- mask to lower 8 bits
return BASE_MODE_TO_LS[base]
end
-- The slash command used to broadcast back into each linkshell.
local LS_SEND_CMD = {
LS1 = '/l',
LS2 = '/l2',
}
-- Native-looking display per linkshell: the [n] prefix FFXI shows and the
-- chat color code (LS1 green, LS2 cyan) so injected Discord lines blend in.
local LS_DISPLAY = {
LS1 = { num = 1, color = 2 }, -- green
LS2 = { num = 2, color = 6 }, -- cyan
}
local POLL_INTERVAL = 1.0 -- seconds between file checks
------------------------------------------------------------
-- State
------------------------------------------------------------
local lastPoll = 0
local enabled = true
-- Per-linkshell enable toggle (both on by default).
local enabledLS = { LS1 = true, LS2 = true }
-- When true, Discord messages are broadcast into the real in-game linkshell
-- (via /l or /l2) so every OTHER LS member sees them too. This is stamped with
-- your own character name by FFXI (e.g. "[1]<You> [Discord] Valesti: ...") and
-- can't show another player's name, so it's off by default. When false, Discord
-- messages are instead printed into your local chat log formatted to look like a
-- native LS line ("[1]<Valesti> ..."), visible only to you. Toggle: /lsbridge say
local relayToLS = false
local lastFileSize = 0
-- Discord chat window visibility
local showDiscordWindow = { true }
-- Discord message history (ring buffer, max 100 messages)
local discordHistory = {}
local MAX_HISTORY = 100
local scrollToBottom = false
-- Packet diagnostics state (both off by default; the packet_in handler is a
-- no-op unless one of these is enabled).
-- pktScan : record a summary of every incoming packet id (count + last
-- size) so you can spot which new packet arrives when the
-- in-game Linkshell window opens/refreshes.
-- pktDumpId : when set to a packet id, write a hex+ASCII dump of just that
-- packet to PACKET_LOG so member names/zones/jobs are visible.
-- pktDumpAll : dump EVERY incoming packet (except the high-volume position/
-- entity noise below). The linkshell roster is only sent in a
-- burst at login/zone-in (never when you just open the window),
-- so enable this, then zone or relog, then grep the file for
-- member names to find the roster packet id + layout.
-- pktDumpNames: dump ONLY packets that contain player-name-like text, skipping
-- the known party/position/chat noise. Best net for the roster:
-- turn it on, then open the Linkshell window / play a few minutes
-- and any unexpected name-bearing packet is flagged automatically.
-- pktDumpGroup: dump ONLY the party/group/linkshell-structure packets (0x0C8,
-- 0x0DD, 0x0DF, 0x0E0, 0x0E1, 0x0E2). HorizonXI most likely reuses
-- the group-list packet (0x0DD/0x0E2) with a non-zero "Kind" byte
-- at offset 0x1C to push the online LS roster at login. This mode
-- captures the whole family in one relog so each entry's Kind /
-- name(0x28) / zone(0x20) / job(0x22) can be decoded. (names mode
-- deliberately skips 0x0DD, so use this to catch the roster.)
local pktScan = false
local pktScanSeen = {}
local pktDumpId = nil
local pktDumpAll = false
local pktDumpNames = false
local pktDumpGroup = false
local pktDumpCount = 0
local PKT_DUMP_MAX = 4000 -- auto-stop so a forgotten 'all' dump can't fill the disk
-- Ultra-frequent packets that flood during zone-in and carry no roster data
-- (position / entity spawn / char-status spam). Skipped in 'all' mode so the
-- file stays small enough to catch and read the roster burst.
local PKT_NOISE = { [0x00D] = true, [0x00E] = true, [0x037] = true }
-- In 'names' mode also skip the packets we've already decoded as NOT the roster
-- but which legitimately carry names: chat (0x017), self zone-in (0x00A), and the
-- party/alliance family (0x0C8 list, 0x0DD member setup, 0x0DF member update).
-- Whatever name-bearing packet remains is the linkshell roster candidate.
local PKT_NAMES_SKIP = {
[0x00D] = true, [0x00E] = true, [0x037] = true, [0x017] = true,
[0x00A] = true, [0x0C8] = true, [0x0DD] = true, [0x0DF] = true,
}
-- The party/group/linkshell-structure family, dumped together by 'group' mode.
-- 0x0DD/0x0E2 (GP_SERV_GROUP_LIST / GROUP_LIST2) carry name(0x28) + zone(0x20) +
-- main job(0x22) plus a "Kind" byte(0x1C): Kind 0 = your party/alliance, while a
-- non-zero Kind is the leading suspect for HorizonXI's online linkshell roster.
local PKT_GROUP = {
[0x0C8] = true, [0x0DD] = true, [0x0DF] = true,
[0x0E0] = true, [0x0E1] = true, [0x0E2] = true,
}
------------------------------------------------------------
-- Write FFXI linkshell message to file for Discord bot.
-- Line format is "LS1|sender|message" so the bot can route per linkshell.
------------------------------------------------------------
local function sendToDiscord(ls, sender, message)
local f = io.open(FFXI_TO_DISCORD, 'a')
if f then
f:write(ls .. '|' .. sender .. '|' .. message .. '\n')
f:close()
end
end
------------------------------------------------------------
-- Read Discord messages from file and show in custom window.
-- Line format from the bot is "LS1|username|message".
------------------------------------------------------------
local function pollDiscordMessages()
local f = io.open(DISCORD_TO_FFXI, 'r')
if not f then return end
local content = f:read('*a')
f:close()
if not content or #content == 0 then return end
-- File was truncated/cleared (it shrank since our last read) -- e.g. the
-- hourly clear or a bot restart. Reset the offset so we re-read from the
-- start instead of slicing past the end (which silently drops messages).
if #content < lastFileSize then
lastFileSize = 0
end
if #content == lastFileSize then return end
-- Only read new content
local newContent = content:sub(lastFileSize + 1)
lastFileSize = #content
if not newContent or #newContent == 0 then return end
local lines = {}
for line in newContent:gmatch('[^\n]+') do
table.insert(lines, line)
end
for _, line in ipairs(lines) do
if #line > 0 then
-- Parse routing tag: "LSx|user|message"
local ls, rest = line:match('^(LS%d)|(.+)$')
if not ls then
ls = 'LS1'
rest = line
end
-- Split "user|message"; fall back to the whole thing as the body.
local user, body = rest:match('^([^|]*)|(.*)$')
if not user then
user = 'Discord'
body = rest
end
-- Add to history (ring buffer)
table.insert(discordHistory, {
time = os.date('%H:%M'),
ls = ls,
text = string.format('%s: %s', user, body)
})
if #discordHistory > MAX_HISTORY then
table.remove(discordHistory, 1)
end
scrollToBottom = true
-- Show Discord messages in the game's chat log.
if enabledLS[ls] then
if relayToLS then
-- Broadcast into the real linkshell so everyone in-game
-- sees it. The "[Discord]" tag is what the text_in handler
-- keys off to avoid relaying our own broadcast back (loop
-- prevention). FFXI stamps this with OUR character name.
local cmd = LS_SEND_CMD[ls]
if cmd then
local text = string.format('[Discord] %s: %s', user, body)
if #text > 150 then text = text:sub(1, 150) end
AshitaCore:GetChatManager():QueueCommand(1, cmd .. ' ' .. text)
end
else
-- Local-only: print a line that looks like a native LS
-- message ("[1]<Valesti> ...") into our own chat log. Only
-- we see it, but the Discord user appears as the sender.
local disp = LS_DISPLAY[ls] or LS_DISPLAY.LS1
local line = string.format('[%d]<%s> %s', disp.num, user, body)
print(chat.color1(disp.color, line))
end
end
end
end
end
------------------------------------------------------------
-- Clear the discord_to_ffxi file periodically (prevent unbounded growth)
------------------------------------------------------------
local lastClear = os.time()
local function maybeClearFile()
if os.time() - lastClear > 3600 then -- every hour
local f = io.open(DISCORD_TO_FFXI, 'w')
if f then f:close() end
lastFileSize = 0
lastClear = os.time()
end
end
------------------------------------------------------------
-- Capture linkshell chat from text_in
------------------------------------------------------------
local debugModes = false -- set true to log all text_in modes to console
local logToFile = true -- TEMP: capturing all modes to diagnose missing messages
local function logMode(mode, text)
local f = io.open(DEBUG_LOG, 'a')
if f then
f:write(string.format('mode=%d | %s\n', mode, text))
f:close()
end
end
ashita.events.register('text_in', 'lsbridge_text_cb', function(e)
if e.injected then return end
local msg = e.message or ''
-- Decode auto-translate tokens into readable text (e.g. "All right!") using
-- Ashita's chat manager, then strip color/translate control codes. This is
-- the same approach chatfeed uses. Without this, auto-translate phrases are
-- opaque tokens that the Discord bot would post as garbage ("??").
local clean = msg
local ok = pcall(function()
clean = AshitaCore:GetChatManager():ParseAutoTranslate(msg, true)
clean = clean:strip_colors()
clean = clean:strip_translate(true)
end)
if not ok then clean = msg end
-- Belt-and-suspenders: strip any leftover FFXI control codes / stray bytes.
clean = clean:gsub('\x1E.', ''):gsub('\x1F.', ''):gsub('\x7F.', ''):gsub('%z', '')
clean = clean:gsub(string.char(0x07), ' ')
clean = clean:gsub('[%c]', '')
-- Drop any remaining non-ASCII bytes that can't survive the trip to Discord.
clean = clean:gsub('[\128-\255]', '')
-- Collapse the whitespace any stripped tokens left behind.
clean = clean:gsub('%s+', ' '):gsub('^%s+', ''):gsub('%s+$', '')
if #clean < 2 then return end
-- Diagnostic logging of every mode (helps identify LS modes)
if logToFile then logMode(e.mode, clean) end
if debugModes then
print(string.format('[LSBridge-DBG] mode=%d msg=%.50s', e.mode, clean))
return
end
if not enabled then return end
-- Which linkshell did this message come from? (nil = not a bridged LS)
-- Use modeToLS() which masks off high-order bitflags.
local ls = modeToLS(e.mode)
if not ls then return end
if not enabledLS[ls] then return end
-- Don't relay messages containing [Discord] (prevents loop)
if clean:match('%[Discord%]') then return end
-- Parse FFXI LS format: "[1]<CharName> message" or "<CharName> message"
local sender, text = clean:match('^%[%d+%]<(.-)>%s*(.+)$')
if not sender then
sender, text = clean:match('^<(.-)>%s*(.+)$')
end
if not sender then
-- Fallback: try "CharName : message" or just take everything
sender, text = clean:match('^(.-)%s*:%s*(.+)$')
end
if not sender or not text then
-- Last resort: send the whole line
sendToDiscord(ls, 'LS', clean)
return
end
-- Drop messages whose body was entirely auto-translate / non-ASCII and is
-- now empty after stripping, so we never post a blank/garbled line.
text = text:gsub('^%s+', ''):gsub('%s+$', '')
if #text == 0 then return end
sendToDiscord(ls, sender, text)
end)
------------------------------------------------------------
-- Packet diagnostics: find & inspect the HorizonXI "Linkshell online members"
-- packet. That rich roster (name + main job + zone) is a HorizonXI custom
-- feature, not retail FFXI, so there's no documented packet id -- we have to
-- capture it live. Workflow:
-- 1) /lsbridge pktscan (start recording packet ids)
-- 2) open the in-game Linkshell window so the server sends the roster
-- 3) /lsbridge pktscan (stop; prints a summary of ids seen)
-- 4) /lsbridge pktdump 0xNNN (dump the suspected id; names show in ASCII)
-- Once the id/layout is known we can parse it into an on-screen list.
------------------------------------------------------------
-- Heuristic: does this packet payload contain a FFXI-player-name-like token?
-- Names are 1 uppercase letter followed by 2-14 lowercase letters (e.g.
-- "Truedream", "Guivre"), terminated by a non-letter. Cheap scan, used by the
-- 'names' dump mode so any unexpected name-bearing packet (the roster) is caught
-- automatically whenever the server sends it.
local function hasNameLike(data, size)
local n = math.min(size or 0, #data)
local i = 1
while i <= n do
local b = data:byte(i) or 0
if b >= 65 and b <= 90 then -- uppercase A-Z starts a name
local len, j = 1, i + 1
while j <= n do
local c = data:byte(j) or 0
if c >= 97 and c <= 122 then -- lowercase a-z continues it
len = len + 1; j = j + 1
else
break
end
end
if len >= 3 and len <= 15 then return true end
i = j
else
i = i + 1
end
end
return false
end
-- Append a classic hex + ASCII dump of one packet to PACKET_LOG. Capped so a
-- large roster packet can't bloat the file. ASCII column makes player names,
-- zone strings, etc. jump out visually.
local function dumpPacket(id, size, data)
local f = io.open(PACKET_LOG, 'a')
if not f then return end
f:write(string.format('=== packet 0x%03X size=%d %s ===\n', id, size, os.date('%H:%M:%S')))
local n = math.min(size or 0, 512)
for off = 1, n, 16 do
local hex, ascii = '', ''
for i = off, math.min(off + 15, n) do
local b = data:byte(i) or 0
hex = hex .. string.format('%02X ', b)
ascii = ascii .. ((b >= 32 and b < 127) and string.char(b) or '.')
end
f:write(string.format('%04X %-48s %s\n', off - 1, hex, ascii))
end
f:write('\n')
f:close()
end
-- Print (and log) the ids seen during a pktscan, sorted, so the roster packet
-- is easy to pick out (usually an infrequent id that appears right as the
-- Linkshell window opens).
local function dumpScanSummary()
local ids = {}
for id in pairs(pktScanSeen) do ids[#ids + 1] = id end
table.sort(ids)
local f = io.open(PACKET_LOG, 'a')
if f then f:write(string.format('--- pktscan summary %s ---\n', os.date('%H:%M:%S'))) end
for _, id in ipairs(ids) do
local rec = pktScanSeen[id]
local line = string.format('0x%03X count=%d lastSize=%d', id, rec.count, rec.size)
print('[LSBridge] ' .. line)
if f then f:write(line .. '\n') end
end
if f then f:write('\n'); f:close() end
end
ashita.events.register('packet_in', 'lsbridge_packet_cb', function(e)
-- Read-only: never blocks or modifies packets. Fast no-op when idle.
if not pktScan and not pktDumpId and not pktDumpAll and not pktDumpNames and not pktDumpGroup then return end
if pktScan then
local rec = pktScanSeen[e.id]
if rec then
rec.count = rec.count + 1
rec.size = e.size
else
pktScanSeen[e.id] = { count = 1, size = e.size }
end
end
if pktDumpAll then
if not PKT_NOISE[e.id] and e.data then
dumpPacket(e.id, e.size, e.data)
pktDumpCount = pktDumpCount + 1
if pktDumpCount >= PKT_DUMP_MAX then
pktDumpAll = false
print(string.format('[LSBridge] pktdump all auto-stopped after %d packets.', PKT_DUMP_MAX))
end
end
elseif pktDumpNames then
if not PKT_NAMES_SKIP[e.id] and e.data and hasNameLike(e.data, e.size) then
dumpPacket(e.id, e.size, e.data)
pktDumpCount = pktDumpCount + 1
if pktDumpCount >= PKT_DUMP_MAX then
pktDumpNames = false
print(string.format('[LSBridge] pktdump names auto-stopped after %d packets.', PKT_DUMP_MAX))
end
end
elseif pktDumpGroup then
if PKT_GROUP[e.id] and e.data then
dumpPacket(e.id, e.size, e.data)
pktDumpCount = pktDumpCount + 1
if pktDumpCount >= PKT_DUMP_MAX then
pktDumpGroup = false
print(string.format('[LSBridge] pktdump group auto-stopped after %d packets.', PKT_DUMP_MAX))
end
end
elseif pktDumpId and e.id == pktDumpId and e.data then
dumpPacket(e.id, e.size, e.data)
end
end)
------------------------------------------------------------
-- Poll for Discord messages every frame (throttled)
------------------------------------------------------------
ashita.events.register('d3d_present', 'lsbridge_poll_cb', function()
if not enabled then return end
local now = os.clock()
if now - lastPoll < POLL_INTERVAL then return end
lastPoll = now
pollDiscordMessages()
maybeClearFile()
end)
------------------------------------------------------------
-- ImGui Discord Chat Window
------------------------------------------------------------
ashita.events.register('d3d_present', 'lsbridge_ui_cb', function()
if not showDiscordWindow[1] then return end
imgui.SetNextWindowSize({ 350, 200 }, ImGuiCond_FirstUseEver)
if imgui.Begin('Discord Chat', showDiscordWindow, ImGuiWindowFlags_None) then
-- Chat history area (scrollable)
local footerHeight = 0
imgui.BeginChild('ChatHistory', { 0, -footerHeight }, true, ImGuiWindowFlags_None)
for _, msg in ipairs(discordHistory) do
-- Color by linkshell
local color = msg.ls == 'LS2' and { 0.6, 0.8, 1.0, 1.0 } or { 0.4, 1.0, 0.6, 1.0 }
imgui.TextColored(color, string.format('[%s] [%s] %s', msg.time, msg.ls, msg.text))
end
-- Auto-scroll to bottom on new messages
if scrollToBottom then
imgui.SetScrollHereY(1.0)
scrollToBottom = false
end
imgui.EndChild()
end
imgui.End()
end)
------------------------------------------------------------
-- Commands
------------------------------------------------------------
ashita.events.register('command', 'lsbridge_cmd_cb', function(e)
local args = e.command:args()
if not args[1] or args[1]:lower() ~= '/lsbridge' then return end
e.blocked = true
local sub = (args[2] or ''):lower()
if sub == '' or sub == 'status' then
local status = enabled and 'ENABLED' or 'DISABLED'
print(string.format('[LSBridge] Status: %s | Poll: %.1fs', status, POLL_INTERVAL))
print(string.format('[LSBridge] LS1: %s (modes 6,14) | LS2: %s (modes 27,15)',
enabledLS.LS1 and 'on' or 'off', enabledLS.LS2 and 'on' or 'off'))
print(string.format('[LSBridge] Discord display: %s', relayToLS and 'BROADCAST to whole LS (/l)' or 'LOCAL native lines (only you)'))
print(string.format('[LSBridge] Files: %s', DATA_DIR))
elseif sub == 'on' or sub == 'enable' then
enabled = true
print('[LSBridge] Bridge enabled.')
elseif sub == 'off' or sub == 'disable' then
enabled = false
print('[LSBridge] Bridge disabled.')
elseif sub == 'ls1' then
enabledLS.LS1 = not enabledLS.LS1
print(string.format('[LSBridge] LS1 bridging: %s', enabledLS.LS1 and 'ON' or 'OFF'))
elseif sub == 'ls2' then
enabledLS.LS2 = not enabledLS.LS2
print(string.format('[LSBridge] LS2 bridging: %s', enabledLS.LS2 and 'ON' or 'OFF'))
elseif sub == 'say' or sub == 'broadcast' then
relayToLS = not relayToLS
print(string.format('[LSBridge] Discord display: %s', relayToLS and 'BROADCAST to whole LS (/l) -- shows your name' or 'LOCAL native lines (only you see them)'))
elseif sub == 'test' then
-- Send a test message to Discord (LS1 by default, or LS2 via "/lsbridge test ls2")
local ls = ((args[3] or ''):lower() == 'ls2') and 'LS2' or 'LS1'
sendToDiscord(ls, 'LSBridge', 'Test message from FFXI!')
print(string.format('[LSBridge] Test message sent to Discord file (%s).', ls))
elseif sub == 'clear' then
-- Clear both files
local f1 = io.open(FFXI_TO_DISCORD, 'w')
if f1 then f1:close() end
local f2 = io.open(DISCORD_TO_FFXI, 'w')
if f2 then f2:close() end
lastFileSize = 0
print('[LSBridge] Cleared bridge files.')
elseif sub == 'debug' then
debugModes = not debugModes
print(string.format('[LSBridge] Debug mode (console): %s (say something in LS now)', debugModes and 'ON' or 'OFF'))
elseif sub == 'logmode' then
logToFile = not logToFile
print(string.format('[LSBridge] Mode logging to file: %s -> %s', logToFile and 'ON' or 'OFF', DEBUG_LOG))
elseif sub == 'window' or sub == 'discord' then
showDiscordWindow[1] = not showDiscordWindow[1]
print(string.format('[LSBridge] Discord window: %s', showDiscordWindow[1] and 'SHOWN' or 'HIDDEN'))
elseif sub == 'clearchat' then
discordHistory = {}
print('[LSBridge] Discord chat history cleared.')
elseif sub == 'pktscan' then
-- Toggle a summary scan of incoming packet ids. Use it to find the
-- HorizonXI "Linkshell online members" packet: start scan, open the
-- in-game Linkshell window, stop scan, look for a new/infrequent id.
pktScan = not pktScan
if pktScan then
pktScanSeen = {}
print('[LSBridge] Packet scan STARTED. Now open the in-game Linkshell window, then run /lsbridge pktscan again to see the ids.')
else
print('[LSBridge] Packet scan STOPPED. Ids seen (also in packets_debug.txt):')
dumpScanSummary()
end
elseif sub == 'pktdump' then
-- Hex+ASCII dump of incoming packets to packets_debug.txt so member
-- names/zones/jobs are visible. Modes:
-- 0xNNN a single packet id
-- all everything except high-volume position/entity noise
-- names only packets containing player-name-like text, skipping the
-- known party/position/chat noise -> best net for the roster
-- off stop
local a = (args[3] or ''):lower()
if a == '' or a == 'off' then
pktDumpId = nil
pktDumpAll = false
pktDumpNames = false
pktDumpGroup = false
print('[LSBridge] Packet dump OFF.')
elseif a == 'all' then
pktDumpAll = true
pktDumpId = nil
pktDumpNames = false
pktDumpGroup = false
pktDumpCount = 0
print(string.format('[LSBridge] Packet dump ALL ON -> %s. Now ZONE or RELOG to capture the roster burst, then /lsbridge pktdump off.', PACKET_LOG))
elseif a == 'names' then
pktDumpNames = true
pktDumpAll = false
pktDumpId = nil
pktDumpGroup = false
pktDumpCount = 0
print(string.format('[LSBridge] Packet dump NAMES ON -> %s. Open the Linkshell window / play a few minutes; any name-bearing packet is flagged. Then /lsbridge pktdump off.', PACKET_LOG))
elseif a == 'group' then
pktDumpGroup = true
pktDumpAll = false
pktDumpNames = false
pktDumpId = nil
pktDumpCount = 0
print(string.format('[LSBridge] Packet dump GROUP ON -> %s. Dumps the party/group/linkshell packets (0x0C8/0x0DD/0x0DF/0x0E0/0x0E1/0x0E2). Now RELOG (log out to character select and back in) with your LS pearl equipped, then /lsbridge pktdump off.', PACKET_LOG))
else
local id = tonumber(a) -- accepts 0x0DD (hex) or a decimal id
if not id then
print('[LSBridge] Usage: /lsbridge pktdump 0x0DD | all | names | group | off')
else
pktDumpId = id
pktDumpAll = false
pktDumpNames = false
pktDumpGroup = false
print(string.format('[LSBridge] Packet dump ON for 0x%03X -> %s. Open the Linkshell window to capture it.', id, PACKET_LOG))
end
end
else
print('[LSBridge] Commands: /lsbridge [status|on|off|ls1|ls2|say|test [ls2]|clear|debug|logmode|window|clearchat|pktscan|pktdump <0xID|all|names|group|off>]')
end
end)
------------------------------------------------------------
-- Load / Unload
------------------------------------------------------------
ashita.events.register('load', 'lsbridge_load_cb', function()
-- Initialize file size tracker
local f = io.open(DISCORD_TO_FFXI, 'r')
if f then
local content = f:read('*a')
lastFileSize = content and #content or 0
f:close()
end
print('[LSBridge] Loaded! Bridging LS1 (6,14) + LS2 (27,15) <-> Discord.')
print('[LSBridge] Commands: /lsbridge [status|on|off|ls1|ls2|window|clearchat|test|clear|debug|logmode]')
end)
ashita.events.register('unload', 'lsbridge_unload_cb', function()
print('[LSBridge] Unloaded.')
end)