-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_Loader.lua
More file actions
658 lines (567 loc) · 19.7 KB
/
_Loader.lua
File metadata and controls
658 lines (567 loc) · 19.7 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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
--[[
nExBot - Tibia Bot for OTClientV8 and OpenTibiaBR
Main Loader Script
This file loads all UI styles and scripts in the correct order.
Core libraries must be loaded before dependent modules.
]]--
local startTime = os.clock()
local loadTimes = {}
-- Initialize global nExBot namespace if not exists
nExBot = nExBot or {}
nExBot.loadTimes = loadTimes
-- ============================================================================
-- CENTRALIZED PATH RESOLUTION (single source of truth)
-- ============================================================================
local ok, configName = pcall(function()
return modules.game_bot.contentsPanel.config:getCurrentOption().text
end)
if not ok or not configName or configName == "" then
warn("[nExBot] Failed to resolve bot config name — cannot initialize.")
return
end
nExBot.paths = {
config = configName,
base = "/bot/" .. configName,
core = "/bot/" .. configName .. "/core",
private = "/bot/" .. configName .. "/private",
}
local P = nExBot.paths -- shorthand for this file
-- Read version from file or storage fallback
do
local versionStr = nil
local ok, content = pcall(g_resources.readFileContents, P.base .. "/version")
if ok and content then versionStr = content:match("^%s*(.-)%s*$") end
if not versionStr and storage and storage.updaterInstalledVersion then
versionStr = storage.updaterInstalledVersion
end
nExBot.version = versionStr or "0.0.0"
end
-- Detect mod-overlay: write a probe to user-data and read it back.
-- If the read returns stale/different content, mods are shadowing writes
-- and auto-updates won't take effect.
do
local probe = P.base .. "/_probe"
local marker = tostring(os.time())
pcall(g_resources.writeFileContents, probe, marker)
local okR, readBack = pcall(g_resources.readFileContents, probe)
local match = okR and readBack and readBack:match("^%s*(.-)%s*$") == marker
pcall(g_resources.deleteFile, probe)
if not match then
nExBot.isModInstall = true
warn("[nExBot] Bot is running from a mod folder — auto-updates are disabled.")
warn("[nExBot] To enable auto-updates, move the bot to the /bot/ folder in your client's data directory (e.g. %AppData% or ~/.local/share).")
end
end
-- Suppress noisy debug prints by default
nExBot.showDebug = nExBot.showDebug or false
nExBot.suppressDebugPrefixes = nExBot.suppressDebugPrefixes or {"[HealBot]", "[MonsterInspector]"}
nExBot.slowOpInstrumentation = nExBot.slowOpInstrumentation or false
-- NativeProfiler removed: wrapping every callback with pcall+os.clock added
-- significant aggregate overhead during bulk events (z-change floor transitions).
local _orig_print = print
print = function(...)
if nExBot.showDebug then return _orig_print(...) end
local first = (...)
local firstStr = nil
if type(first) == "string" then
firstStr = first
else
local ok, s = pcall(tostring, first)
if ok then firstStr = s end
end
if firstStr then
for _, p in ipairs(nExBot.suppressDebugPrefixes) do
if firstStr:sub(1, #p) == p then
return
end
end
end
return _orig_print(...)
end
-- ============================================================================
-- STORAGE SANITIZER (Fix sparse arrays that prevent saving)
-- ============================================================================
local function isSparseArray(tbl)
if type(tbl) ~= "table" then return false end
local minIndex, maxIndex, count = nil, nil, 0
for k, v in pairs(tbl) do
if type(k) == "number" and k % 1 == 0 and k > 0 then
if not minIndex or k < minIndex then minIndex = k end
if not maxIndex or k > maxIndex then maxIndex = k end
count = count + 1
end
end
return count > 0 and (maxIndex - minIndex + 1 > count)
end
local function sanitizeTable(tbl, path, depth)
if type(tbl) ~= "table" or depth > 5 then return tbl end
if isSparseArray(tbl) then
local fixed = {}
for k, v in pairs(tbl) do
if type(k) == "number" then
fixed[tostring(k)] = sanitizeTable(v, path .. "." .. tostring(k), depth + 1)
else
fixed[k] = sanitizeTable(v, path .. "." .. tostring(k), depth + 1)
end
end
warn("[nExBot] Fixed sparse array at: " .. path)
return fixed
end
for k, v in pairs(tbl) do
if type(v) == "table" then
tbl[k] = sanitizeTable(v, path .. "." .. tostring(k), depth + 1)
end
end
return tbl
end
local function sanitizeStorage()
if not storage then return end
local sanitizeStart = os.clock()
local keys = {}
for k, v in pairs(storage) do
if type(v) == "table" then keys[#keys + 1] = k end
end
local idx = 1
local chunkSize = 20
local function processChunk()
local stopAt = math.min(idx + chunkSize - 1, #keys)
for i = idx, stopAt do
local k = keys[i]
if type(storage[k]) == 'table' then
storage[k] = sanitizeTable(storage[k], k, 0)
end
end
idx = stopAt + 1
if idx <= #keys then
schedule(50, processChunk)
else
loadTimes["sanitize"] = math.floor((os.clock() - sanitizeStart) * 1000)
end
end
schedule(1, processChunk)
end
sanitizeStorage()
-- ============================================================================
-- OPTIMIZED STYLE LOADING
-- ============================================================================
local function loadStyles()
local styleStart = os.clock()
local styleFiles = {}
local configFiles = g_resources.listDirectoryFiles(P.core, true, false)
for i = 1, #configFiles do
local file = configFiles[i]
local ext = file:split(".")
local extension = ext[#ext]:lower()
if extension == "ui" or extension == "otui" then
-- Ensure full path: if file doesn't start with '/' it's just a filename
local fullPath = file
if file:sub(1,1) ~= "/" then
fullPath = P.core .. "/" .. file
end
styleFiles[#styleFiles + 1] = fullPath
end
end
local failedStyles = {}
for i = 1, #styleFiles do
local ok, err = pcall(function() g_ui.importStyle(styleFiles[i]) end)
if not ok then
failedStyles[#failedStyles + 1] = styleFiles[i] .. ": " .. tostring(err)
end
end
if #failedStyles > 0 then
warn("[nExBot] Failed to load " .. #failedStyles .. " style(s): " .. table.concat(failedStyles, "; "))
end
loadTimes["styles"] = math.floor((os.clock() - styleStart) * 1000)
loadTimes["_styles_count"] = #styleFiles
loadTimes["_styles_failed"] = #failedStyles
end
-- ============================================================================
-- SCRIPT LOADING UTILITIES
-- ============================================================================
local OPTIONAL_MODULES = {
["HealBot"] = true,
["bot_core/init"] = true,
}
local function loadScript(name, category, basePath)
basePath = basePath or "/core/"
local scriptStart = os.clock()
local status, result = pcall(function()
return dofile(basePath .. name .. ".lua")
end)
local elapsed = math.floor((os.clock() - scriptStart) * 1000)
loadTimes[name] = elapsed
if not status then
local errorMsg = tostring(result)
nExBot.loadErrors = nExBot.loadErrors or {}
nExBot.loadErrors[name] = errorMsg
local isOptional = OPTIONAL_MODULES[name]
local isNotFound = errorMsg:match("not found") or errorMsg:match("No such file")
if not isOptional and not isNotFound then
warn("[nExBot] Failed to load '" .. name .. "' (" .. elapsed .. "ms): " .. errorMsg)
end
return nil
end
return result
end
local function loadCategory(categoryName, scripts, basePath)
local catStart = os.clock()
for i = 1, #scripts do
loadScript(scripts[i], categoryName, basePath)
end
loadTimes["_category_" .. categoryName] = math.floor((os.clock() - catStart) * 1000)
end
-- ============================================================================
-- LOAD STYLES FIRST
-- ============================================================================
loadStyles()
-- ============================================================================
-- PHASE 1: ACL AND CLIENT ABSTRACTION
-- ============================================================================
loadCategory("acl", {
"acl/init",
"client_service",
})
loadCategory("acl_compat", {
"acl/compat",
})
-- Store client info
-- Detection runs inline to avoid dependency on adapter loading success.
-- We re-use the same fingerprint logic from acl/init.lua but self-contained.
do
local detected = false
-- Try ACL module first (may have been loaded in Phase 1)
local aclStatus, acl = pcall(function()
return dofile("/core/acl/init.lua")
end)
if aclStatus and acl and acl.getClientType then
local ctype = acl.getClientType()
if ctype and ctype ~= 0 then
nExBot.clientType = ctype
nExBot.clientName = acl.getClientName()
nExBot.isOTCv8 = (ctype == 1)
nExBot.isOpenTibiaBR = (ctype == 2)
detected = true
end
end
-- Fallback: lightweight inline fingerprint if ACL failed or returned UNKNOWN
if not detected then
local isOTBR = false
-- Check OTBR-only module files on disk
if g_resources and type(g_resources.fileExists) == "function" then
local otbrPaths = {
"/modules/game_cyclopedia/game_cyclopedia.otmod",
"/modules/game_forge/game_forge.otmod",
"/modules/game_healthcircle/game_healthcircle.otmod",
}
for i = 1, #otbrPaths do
if g_resources.fileExists(otbrPaths[i]) then
isOTBR = true
break
end
end
end
-- Check OTBR-exclusive APIs (only if moveRaw is absent — moveRaw means OTCv8)
if not isOTBR then
local hasMoveRaw = g_game and type(g_game.moveRaw) == "function"
if not hasMoveRaw then
if g_game and type(g_game.forceWalk) == "function" then
isOTBR = true
end
end
end
if isOTBR then
nExBot.clientType = 2
nExBot.clientName = "OpenTibiaBR"
nExBot.isOTCv8 = false
nExBot.isOpenTibiaBR = true
else
nExBot.clientType = 1
nExBot.clientName = "OTCv8"
nExBot.isOTCv8 = true
nExBot.isOpenTibiaBR = false
end
end
if not nExBot._clientPrinted then
nExBot._clientPrinted = true
print("[nExBot] Client detected: " .. tostring(nExBot.clientName) .. " (" .. tostring(nExBot.clientType) .. ")")
end
end
-- Re-check detection after startup when globals are more likely to exist
local function autoDetectClient(attempt, maxAttempts)
schedule(1500, function()
local ok, acl = pcall(function()
return dofile("/core/acl/init.lua")
end)
if ok and acl and acl.refreshDetection then
local prevType = nExBot.clientType
local prevName = nExBot.clientName
local newType = acl.refreshDetection()
nExBot.clientType = newType
nExBot.clientName = acl.getClientName()
nExBot.isOTCv8 = acl.isOTCv8()
nExBot.isOpenTibiaBR = acl.isOpenTibiaBR()
if newType ~= prevType or nExBot.clientName ~= prevName then
print("[nExBot] Client detected (late): " .. tostring(nExBot.clientName) .. " (" .. tostring(newType) .. ")")
end
if nExBot.isOpenTibiaBR then
return
end
if attempt >= maxAttempts then
if acl.getDetectionInfo then
local info = acl.getDetectionInfo()
if info and info.signals then
local keys = {}
for k, v in pairs(info.signals) do
if v then
table.insert(keys, k)
end
end
print("[nExBot] Client signals: signals=" .. table.concat(keys, ","))
end
end
return
end
end
autoDetectClient(attempt + 1, maxAttempts)
end)
end
autoDetectClient(1, 8)
-- ============================================================================
-- PHASE 2: CONSTANTS
-- ============================================================================
loadCategory("constants", {
"constants/floor_items",
"constants/food_items",
"constants/directions",
}, "/")
-- ============================================================================
-- PHASE 3: UTILS (Core shared utilities)
-- ============================================================================
loadCategory("utils", {
"utils/shared",
"utils/ring_buffer",
"utils/client_helper",
"utils/safe_creature",
"utils/weak_cache",
"utils/vocation_utils",
"utils/event_debouncer",
"utils/path_utils",
"utils/path_strategy",
"utils/waypoint_navigator",
}, "/")
-- ============================================================================
-- PHASE 4: CORE LIBRARIES (Legacy compatibility)
-- ============================================================================
loadScript("updater", "core") -- Load updater first so its UI appears above main.lua
loadCategory("core", {
"main",
"items",
"lib",
"safe_call",
"new_cavebot_lib",
"configs",
"bot_database",
"character_db",
})
-- ============================================================================
-- PHASE 6: ARCHITECTURE LAYER
-- ============================================================================
loadCategory("architecture", {
"event_bus",
"unified_storage",
"unified_tick",
"creature_cache",
"door_items",
"global_config",
"bot_core/init",
})
-- ============================================================================
-- PHASE 8: LEGACY FEATURE MODULES
-- ============================================================================
loadCategory("features_legacy", {
"extras",
"cavebot",
"alarms",
"Conditions",
"Equipper",
"pushmax",
"combo",
"HealBot",
"new_healer",
"AttackBot",
})
-- ============================================================================
-- PHASE 9: LEGACY TOOLS
-- ============================================================================
loadCategory("tools_legacy", {
"ingame_editor",
"Dropper",
"Containers",
"container_opener",
"quiver_manager",
"quiver_label",
"tools",
"antiRs",
"depot_withdraw",
"eat_food",
"equip",
"exeta",
"outfit_cloner",
})
-- ============================================================================
-- PHASE 11: ANALYTICS AND UI
-- ============================================================================
loadCategory("analytics", {
"analyzer",
"smart_hunt",
"spy_level",
"supplies",
"depositer_config",
"npc_talk",
"xeno_menu",
"hold_target",
"cavebot_control_panel",
})
-- NOTE: TargetBot scripts are loaded by core/cavebot.lua (in features_legacy phase)
-- to avoid duplicating the loading, we don't load them again here.
-- NOTE: CaveBot scripts are loaded by core/cavebot.lua (in features_legacy phase)
-- to avoid duplicating the loading, we don't load them again here.
-- ============================================================================
-- STARTUP COMPLETE
-- ============================================================================
local totalTime = math.floor((os.clock() - startTime) * 1000)
loadTimes["_total"] = totalTime
-- ============================================================================
-- STARTUP PROFILING SUMMARY
-- ============================================================================
-- Collect and sort all module load times for analysis
local function getTopSlowestModules(n)
local modules = {}
for name, time in pairs(loadTimes) do
if not name:match("^_") then
modules[#modules + 1] = { name = name, time = time }
end
end
table.sort(modules, function(a, b) return a.time > b.time end)
local top = {}
for i = 1, math.min(n, #modules) do
top[i] = modules[i]
end
return top
end
-- Always show top 5 slowest modules when debug is enabled
if nExBot.showDebug then
local top5 = getTopSlowestModules(5)
print("[nExBot] Startup profiling - Top 5 slowest modules:")
for i, m in ipairs(top5) do
print(string.format(" %d. %s: %dms", i, m.name, m.time))
end
print(string.format(" Total startup time: %dms", totalTime))
end
-- Export profiling helper for runtime analysis
nExBot.getTopSlowestModules = getTopSlowestModules
nExBot.printStartupProfile = function()
local top = getTopSlowestModules(10)
print("[nExBot] Startup Profile (Top 10):")
for i, m in ipairs(top) do
print(string.format(" %d. %s: %dms", i, m.name, m.time))
end
print(string.format(" Total: %dms", loadTimes["_total"] or 0))
end
if totalTime > 1000 then
warn("[nExBot] Slow startup: " .. totalTime .. "ms")
local slowModules = {}
for name, time in pairs(loadTimes) do
if time > 100 and not name:match("^_") then
slowModules[#slowModules + 1] = name .. ":" .. time .. "ms"
end
end
if #slowModules > 0 then
warn("[nExBot] Slow modules: " .. table.concat(slowModules, ", "))
end
else
info("[nExBot v" .. nExBot.version .. "] Loaded in " .. totalTime .. "ms")
end
-- ============================================================================
-- PRIVATE SCRIPTS AUTO-LOADER
-- ============================================================================
local PRIVATE_DOFILE_PATH = "/private"
local function collectLuaFiles(folderPath, dofileBase, collected)
collected = collected or {}
local status, items = pcall(function()
return g_resources.listDirectoryFiles(folderPath, false, false)
end)
if not status or not items then
return collected
end
for i = 1, #items do
local item = items[i]
local fullPath = folderPath .. "/" .. item
local dofilePath = dofileBase .. "/" .. item
if item:match("%.lua$") then
collected[#collected + 1] = {
name = item,
path = dofilePath
}
elseif not item:match("%.") then
local subStatus, subItems = pcall(function()
return g_resources.listDirectoryFiles(fullPath, false, false)
end)
if subStatus and subItems then
collectLuaFiles(fullPath, dofilePath, collected)
end
end
end
return collected
end
local function loadPrivateScripts()
local status, items = pcall(function()
return g_resources.listDirectoryFiles(P.private, false, false)
end)
if not status or not items or #items == 0 then
return
end
local privateStart = os.clock()
local luaFiles = collectLuaFiles(P.private, PRIVATE_DOFILE_PATH)
if #luaFiles == 0 then
return
end
table.sort(luaFiles, function(a, b) return a.path < b.path end)
local loadedCount = 0
for i = 1, #luaFiles do
local file = luaFiles[i]
local scriptStart = os.clock()
local loadStatus, err = pcall(function()
dofile(file.path)
end)
local elapsed = math.floor((os.clock() - scriptStart) * 1000)
if loadStatus then
loadedCount = loadedCount + 1
loadTimes["private:" .. file.name] = elapsed
else
warn("[Private] Failed to load '" .. file.path .. "': " .. tostring(err))
nExBot.loadErrors = nExBot.loadErrors or {}
nExBot.loadErrors["private:" .. file.name] = tostring(err)
end
end
loadTimes["_private_total"] = math.floor((os.clock() - privateStart) * 1000)
if loadedCount > 0 then
info("[nExBot] Loaded " .. loadedCount .. " private script(s)")
end
end
loadPrivateScripts()
-- Return to Main tab
setDefaultTab("Main")
-- ============================================================================
-- ACTIVATE UNIFIED TICK SYSTEM
-- ============================================================================
-- Start the consolidated tick system now that all modules are loaded
-- This reduces ~30+ separate macro timers to a single 50ms master tick
if UnifiedTick and UnifiedTick.start then
schedule(100, function()
UnifiedTick.start()
if nExBot.showDebug then
print("[nExBot] UnifiedTick master loop activated")
end
end)
end