forked from jk3064/new_widgethandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.lua
More file actions
1848 lines (1491 loc) · 48.7 KB
/
Copy pathhandler.lua
File metadata and controls
1848 lines (1491 loc) · 48.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: handler.lua
-- brief: the addon (widget/gadget) manager, a call-in router
-- author: jK (based heavily on code by Dave Rodgers)
--
-- Copyright (C) 2007-2011.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--FIXME name widgets & gadgets AddOns internally
--FIXME rev2 & handler:Remove()
--FIXME finish BlockAddon + add BlockWidget syn.
--FIXME cleanup
--// Note: all here included modules/utilities are auto exposed to the addons, too!
require "setupdefs.lua"
require "savetable.lua"
require "keysym.lua"
require "actions.lua"
--// make a copy of the engine exported enviroment (we use this later for the addons!)
local EG = {}
for i,v in pairs(_G) do
EG[i] = v
end
--// don't auto expose the following the addons
require "list.lua"
--[[
do
local i=0
local function hook(event)
i = i + 1
if ((i % (10^7)) < 1) then
i = 0
Spring.Echo(Spring.GetGameFrame(), event, debug.getinfo(2).name)
Spring.Echo(debug.traceback())
end
end
debug.sethook(hook,"r",10^100)
end
--]]
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- SpeedUp & Helpers
local spEcho = Spring.Echo
local glPopAttrib = gl.PopAttrib
local glPushAttrib = gl.PushAttrib
local type = type
local pcall = pcall
local pairs = pairs
local ipairs = ipairs
local emptyTable = {}
if (not VFS.GetFileChecksum) then
function VFS.GetFileChecksum(file, _VFSMODE)
local data = VFS.LoadFile(file, _VFSMODE)
if (data) then
local datalen = data:len()/4 --// 'x/4' cause we use UnpackU32
local striplength = 2 * 1024 --// 2kB
if (striplength >= datalen) then
local bytes = VFS.UnpackU32(data,nil,datalen)
local checksum = math.bit_xor(0,unpack(bytes))
return checksum
end
--// stack is limited, so split up the data
local start = 1
local crcs = {}
repeat
local strip = data:sub(start,start+striplength)
local bytes = VFS.UnpackU32(strip,nil,strip:len()/4)
local checksum = math.bit_xor(0,unpack(bytes))
crcs[#crcs+1] = checksum
start = start + striplength
until (start >= datalen)
local checksum = math.bit_xor(0,unpack(crcs))
return checksum
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Table functions
local function tcopy(t1, t2)
--FIXME recursive?
for i,v in pairs(t2) do
t1[i] = v
end
end
local function tappend(t1, t2)
for i=1,#t2 do
t1[#t1+1] = t2[i]
end
end
local function tfind(t, item)
if (not t)or(item == nil) then return false end
for i=1,#t do
if t[i] == item then
return true
end
end
return false
end
local function tprinttable(t, columns)
local formatstr = " " .. string.rep("%-25s, ", columns)
for i=1, #t, columns do
if (i+columns > #t) then
formatstr = " " .. string.rep("%-25s, ", #t - i - 1) .. "%-25s"
end
local s = formatstr:format(select(i,unpack(t)))
spEcho(" " .. s)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- A Lua List Object
local function SortAddonsFunc(ki1, ki2)
local one_before_two = tfind(ki1.before, ki2.name) or tfind(ki2.after, ki1.name)
local two_before_one = tfind(ki2.before, ki1.name) or tfind(ki1.after, ki2.name)
local one_before_all = tfind(ki1.before, "all")
local two_before_all = tfind(ki2.before, "all")
if (one_before_all ~= two_before_all) then
return one_before_all
end
if (ki1.api ~= ki2.api) then
return (ki1.api)
end
local l1 = ki1.layer or math.huge
local l2 = ki2.layer or math.huge
if (l1 ~= l2) then
return (l1 < l2)
end
local o1 = handler.orderList[n1] or math.huge
local o2 = handler.orderList[n2] or math.huge
if (o1 ~= o2) then
return (o1 < o2)
end
if (ki1.fromZip ~= ki2.fromZip) then --// load zip files first, so they can prevent hacks/cheats ...
return (ki1.fromZip)
end
return (ki1.name < ki2.name)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- the handler object
--
handler = {
name = "widgetHandler";
addonName = "widget";
verbose = true;
autoUserWidgets = true; --// if false it auto disables widgets from rawFS (FIXME do via BlockAddon)
addons = CreateList("addons", SortAddonsFunc); --// all loaded addons
configData = {};
orderList = {};
knownWidgets = {}; --// cached Load...Info() results of all known/available Addons (even unloaded ones)
knownChanged = 0;
commands = {}; --FIXME where used?
customCommands = {};
inCommandsChanged = false;
EG = EG; --// engine global (all published funcs by the engine)
SG = {}; --// shared table for addons
globals = {}; --// global vars/funcs
knownCallIns = {};
callInLists = setmetatable({}, {__index = function(self, key) self[key] = CreateList(key, SortAddonsFunc); return self[key]; end});
callInHookFuncs = {};
mouseOwner = nil;
initialized = false;
actionHandler = actionHandler; --for handler=true widgets
}
handler.AddonName = handler.addonName:gsub("^%l", string.upper) --// widget -> Widget
--// Backwardcompability
handler[handler.addonName .. "s"] = handler.addons --// handler.widgets == handler.addons
--// backward compability, so you can still call handler:UnitCreated() etc.
setmetatable(handler, {
__index = function(self, key)
local firstChar = key:sub(1,1)
if (firstChar == firstChar:upper()) then
return function(_, ...)
if (self.callInHookFuncs[key]) then
return self.callInHookFuncs[key](...)
else
--error(LUA_NAME .. ": No CallIn-Handler for \"" .. key .. "\"")
Spring.Echo(LUA_NAME .. ": ERROR No CallIn-Handler for \"" .. key .. "\"") --no need for panic
end
end
end
end
})
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Create list of known CallIns
--
--// always register those callins even when not used by any addon
local staticCallInList = {
'ConfigureLayout',
'Shutdown',
'Update',
}
for _,ciName in ipairs(staticCallInList) do
staticCallInList[ciName] = true
end
--// Load all known engine callins
local engineCallIns = Script.GetCallInList() --// important!
--// Create list of all known callins (any others used in addons won't work!)
local knownCallIns = handler.knownCallIns
for ciName,ciParams in pairs(engineCallIns) do
if (ciParams.controller and (not ciParams.unsynced) and (not Script.GetSynced())) then
--// skip synced only events when we are in an unsynced enviroment
else
knownCallIns[ciName] = ciParams
end
end
--// Registers custom (non-engine) callins
function handler:AddNewCallIn(ciName, unsynced, controller)
if (knownCallIns[ciName]) then
return
end
knownCallIns[ciName] = {unsynced = unsynced, controller = controller, custom = true}
for _,addon in handler.addons:iter() do
handler:UpdateWidgetCallIn(ciName, addon)
end
end
--// Standard Custom CallIns
handler:AddNewCallIn("Initialize", true, false) --// ()
handler:AddNewCallIn("AddonAdded", true, false) --// (addon_name)
handler:AddNewCallIn("WidgetAdded", true, false) --// ''
handler:AddNewCallIn("AddonRemoved", true, false) --// (addon_name, reason) -- reason can either be "crash" | "user" | "auto" | "dependency"
handler:AddNewCallIn("WidgetRemoved", true, false) --// ''
handler:AddNewCallIn("SelectionChanged", true, true) --// (selection = {unitID1, unitID1}) -> [newSelection]
handler:AddNewCallIn("CommandsChanged", true, false) --// ()
handler:AddNewCallIn("TextCommand", true, false) --// ("command") -- renamed ConfigureLayout
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Custom iterator for all known callins
local function knownCallins_iter(addon, key)
local ciFunc
repeat
key = next(knownCallIns, key)
if (key) then
ciFunc = addon[key]
if (type(ciFunc) == "function") then
return key, ciFunc
end
end
until (not key)
end
local function knownCallins(addon)
return knownCallins_iter, addon, nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Initialize
function handler:Initialize()
--// Create the "LuaUI/Config" directory
Spring.CreateDir(LUAUI_DIRNAME .. 'Config')
handler:UpdateAddonList()
handler.initialized = true
end
function handler:UpdateAddonList()
handler:LoadOrderList()
handler:LoadConfigData()
handler:LoadKnownData()
--// GetInfo() of new/changed files
handler:SearchForNew()
--// Create list all to load files
spEcho(("%s: Loading %ss <>=vfs **=raw ()=unknown"):format(LUA_NAME, handler.addonName))
handler:DetectEnabledAddons()
local loadList = {}
for name,order in pairs(handler.orderList) do
if (order > 0) then
local ki = handler.knownWidgets[name]
if ki then
loadList[#loadList+1] = name
else
if (handler.verbose) then spEcho(("Couldn't find a %s named \"%s\""):format(handler.addonName, name)) end
handler.knownWidgets[name] = nil
handler.orderList[name] = nil
end
end
end
--// Sort them
local SortFunc = function(n1, n2)
local ki1 = handler.knownWidgets[n1]
local ki2 = handler.knownWidgets[n2]
--assert(wi1 and wi2)
return SortAddonsFunc(ki1 or emptyTable, ki2 or emptyTable)
end
table.sort(loadList, SortFunc)
if (not handler.verbose) then
--// if not in verbose mode, print the to be load addons (in a nice table) BEFORE loading them!
local st = {}
for _,name in ipairs(loadList) do
st[#st+1] = handler:GetFancyString(name)
end
tprinttable(st, 4)
end
--// Load them
for _,name in ipairs(loadList) do
local ki = handler.knownWidgets[name]
handler:Load(ki.filepath)
end
--// Save the active addons, and their ordering
handler:SaveOrderList()
handler:SaveConfigData()
handler:SaveKnownData()
end
handler[("Update%sList"):format(handler.AddonName)] = handler.UpdateAddonList
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Addon Files Finder
local function GetAllAddonFiles()
local addonFiles = {}
for i,dir in pairs(WIDGET_DIRS) do
spEcho(LUA_NAME .. " Scanning: " .. dir)
local files = VFS.DirList(dir, "*.lua", VFSMODE)
if (files) then
tappend(addonFiles, files)
end
end
return addonFiles
end
function handler:FindNameByPath(path)
for _,ki in pairs(handler.knownWidgets) do
if (ki.filepath == path) then
return ki.name
end
end
end
function handler:SearchForNew(quiet)
if (quiet) then spEcho = function() end end
spEcho(LUA_NAME .. ": Searching for new Widgets")
local addonFiles = GetAllAddonFiles()
for _,fpath in ipairs(addonFiles) do
local name = handler:FindNameByPath(fpath)
local ki = name and handler.knownWidgets[name]
if ki and ((not handler.initialized) or ((ki._rev >= 2) and (not ki.active))) then --// don't override the knownWidgets[name] of _loaded_ addons!
if ki and ki.checksum then --// rev2 addons don't save a checksum!
local checksum = VFS.GetFileChecksum(fpath, VFSMODE)
if (checksum and (ki.checksum ~= checksum)) then
ki = nil
end
else
ki = nil
end
end
if (not ki) then
if (handler.verbose) then spEcho(("%s: Found new %s \"%s\""):format(LUA_NAME, handler.addonName, fpath)) end
if name then handler.knownWidgets[name] = nil end
handler:LoadWidgetInfo(fpath)
end
end
handler:DetectEnabledAddons()
if (quiet) then spEcho = Spring.Echo end
end
function handler:DetectEnabledAddons()
for i,ki in pairs(handler.knownWidgets) do
if (not ki.active) then
--// default enabled?
local defEnabled = ki.enabled
--// enabled or not?
local order = handler.orderList[ki.name]
if ((order or 0) > 0)
or ((order == nil) and defEnabled and (handler.autoUserWidgets or ki.fromZip))
then
--// this will be an active addon
handler.orderList[ki.name] = order or 1235 --// back of the pack for unknown order
--// we don't auto start addons when just updating the available list
ki.active = (not handler.initialized)
else
--// deactive the addon
handler.orderList[ki.name] = 0
ki.active = false
end
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Addon Crash Handlers
local SafeCallAddon
local SafeWrapFunc
do
--// small helper
local isDrawCallIn = setmetatable({}, {__index = function(self,ciName)
self[ciName] = ((ciName:sub(1, 4) == 'Draw')or(ciName:sub(1, 9) == 'TweakDraw'));
return self[ciName];
end})
local function HandleError(addon, funcName, status, ...)
if (status) then
--// no error
return ...
end
handler:Remove(addon, "crash")
local name = addon._info.name
local err = select(1,...)
spEcho(('Error in %s(): %s'):format(funcName, tostring(err)))
spEcho(('Removed %s: %s'):format(handler.addonName, handler:GetFancyString(name)))
return nil
end
local function HandleErrorGL(addon, funcName, status, ...)
glPopAttrib()
--gl.PushMatrix()
return HandleError(addon, funcName, status, ...)
end
local function SafeWrapFuncNoGL(addon, func, funcName)
return function(...)
return HandleError(addon, funcName, pcall(func, ...))
end
end
local function SafeWrapFuncGL(addon, func, funcName)
return function(...)
glPushAttrib()
--gl.PushMatrix()
return HandleErrorGL(addon, funcName, pcall(func, ...))
end
end
SafeWrapFunc = function(addon, func, funcName)
if (SAFEWRAP <= 0) then
return func
elseif (SAFEWRAP == 1) then
if (addon._info.unsafe) then
return func
end
end
if (not SAFEDRAW) then
return SafeWrapFuncNoGL(addon, func, funcName)
else
if (isDrawCallIn[funcName]) then
return SafeWrapFuncGL(addon, func, funcName)
else
return SafeWrapFuncNoGL(addon, func, funcName)
end
end
end
SafeCallAddon = function(addon, ciName, ...)
local f = addon[ciName]
if (not f) then
return
end
local ki = addon._info
if (SAFEWRAP <= 0)or
((SAFEWRAP == 1)and(ki and ki.unsafe))
then
return f(addon, ...)
end
if (SAFEDRAW and isDrawCallIn[ciName]) then
glPushAttrib()
return HandleErrorGL(addon, ciName, pcall(f, addon, ...))
else
return HandleError(addon, ciName, pcall(f, addon, ...))
end
end
end
--// so addons can use it, too
handler[("SafeCall%s"):format(handler.AddonName)] = SafeCallAddon
handler.SafeCallAddon = SafeCallAddon
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Addon Info
do
local function GetDefaultKnownInfo(filepath, basename)
return {
filepath = filepath,
basename = basename,
name = basename,
version = "0.1",
layer = 0,
desc = "",
author = "",
license = "",
enabled = false,
api = false,
handler = false,
before = {},
after = {},
depend = {},
_rev = 0,
}
end
local function LoadAddonRev2Info(filepath, _VFSMODE)
local basename = Basename(filepath)
local ki = GetDefaultKnownInfo(filepath, basename)
ki._rev = 2
_VFSMODE = _VFSMODE or VFSMODE
local loadEnv = {addon = {InGetInfo = true}, math = math}
local success, rvalue = pcall(VFS.Include, filepath, loadEnv, _VFSMODE)
if not success then
return "Failed to load: " .. basename .. " (" .. rvalue .. ")"
end
if rvalue == false then
return true --// addon asked for a silent death
end
if type(rvalue) ~= "table" then
return "Wrong return value: " .. basename
end
tcopy(ki, rvalue)
return false, ki
end
local function LoadAddonRev1Info(addon, filepath)
local basename = Basename(filepath)
local ki = GetDefaultKnownInfo(filepath, basename)
ki._rev = 1
if (addon.GetInfo) then
local rvalue = SafeCallAddon(addon, "GetInfo")
if type(rvalue) ~= "table" then
return "Failed to call GetInfo() in: " .. basename
else
tcopy(ki, rvalue)
end
else
return "Missing GetInfo() in: " .. basename
end
return false, ki
end
local function ValidateKnownInfo(ki, _VFSMODE)
if not ki then
return "No KnownInfo given"
end
--// load/create data
local knownInfo = handler.knownWidgets[ki.name]
if (not knowInfo) then
knownInfo = {}
handler.knownWidgets[ki.name] = knownInfo
end
--// check for duplicated name
if (knownInfo.filepath)and(knownInfo.filepath ~= ki.filepath) then
return "Failed to load: " .. ki.basename .. " (duplicate name)"
end
--// create/update knownInfo table
tcopy(knownInfo, ki) --// update table
end
function handler:LoadWidgetInfo(filepath, _VFSMODE)
--FIXME check checksum for rev1 addons!
--// update so addons can see if something got changed
handler.knownChanged = handler.knownChanged + 1
--// clear old knownInfo
local name = handler:FindNameByPath(filepath)
if (name) then
handler.knownWidgets[name] = nil --FIXME addon._info and handler.knownWidgets[name] point should point to the same table?
end
local err, ki = LoadAddonRev2Info(filepath, _VFSMODE)
if (err == true) then
return nil --// addon asked for a silent death
end
if (not ki) then
--// try to load it as rev1 addon
local addon = handler:ParseAddonRev1(filepath, _VFSMODE)
if (addon) then
err, ki = LoadAddonRev1Info(addon, filepath)
end
end
--// fail
if (not ki) then
--spEcho(err)
return nil
end
--// create checksum for rev1 addons
if (ki._rev <= 1) then
ki.checksum = VFS.GetFileChecksum(ki.filepath, _VFSMODE or VFSMODE)
end
--// check if it's loaded from a zip (game or map)
ki.fromZip = true
if (_VFSMODE == VFS.ZIP_FIRST) then
ki.fromZip = VFS.FileExists(ki.filepath,VFS.ZIP_ONLY)
else
ki.fromZip = not VFS.FileExists(ki.filepath,VFS.RAW_ONLY)
end
--// causality
tappend(ki.after, ki.depend)
--// validate
err = ValidateKnownInfo(ki, _VFSMODE)
if (err) then
spEcho(err)
return nil
end
return ki
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Addon Parsing
local function ValidateAddon(addon)
if (addon.GetTooltip and not addon.IsAbove) then
return ("%s has GetTooltip() but not IsAbove()"):format(handler.AddonName)
end
return nil
end
function handler:NewAddonRev2()
local addonEnv = {}
local addon = addonEnv
addonEnv.widget = addon
addonEnv.addon = addon --// makes `function Initizalize` & `function addon.Initialize` point to the same data
--// copy the engine enviroment to the addon
tcopy(addonEnv, EG)
--// the shared table
addonEnv.SG = handler.SG
--// addon related methods
addonEnv.addon = {
Remove = handler.Remove,
IsMouseOwner = function() return (handler.mouseOwner == addon) end,
DisownMouse = function()
if (handler.mouseOwner == addon) then
handler.mouseOwner = nil
end
end,
UpdateCallIn = function(name) handler:UpdateWidgetCallIn(name, addon) end,
RemoveCallIn = function(name) handler:RemoveWidgetCallIn(name, addon) end,
AddAction = function(cmd, func, data, types) return actionHandler.AddWidgetAction(addon, cmd, func, data, types) end,
RemoveAction = function(cmd, types) return actionHandler.RemoveWidgetAction(addon, cmd, types) end,
TextAction = function(command) return actionHandler.TextAction(command) end,
--[[
AddLayoutCommand = function(_, cmd)
if (handler.inCommandsChanged) then
table.insert(handler.customCommands, cmd)
else
spEcho("AddLayoutCommand() can only be used in CommandsChanged()")
end
end,
GetCommands = function() return handler.commands end,
--]]
RegisterGlobal = function(name, value) return handler:RegisterGlobal(addon, name, value) end,
DeregisterGlobal = function(name) return handler:DeregisterGlobal(addon, name) end,
SetGlobal = function(name, value) return handler:SetGlobal(addon, name, value) end,
}
--// insert handler
addonEnv.handler = handler
addonEnv[handler.name] = handler
return addon
end
function handler:NewAddonRev1()
local addonEnv = {}
local addon = addonEnv --// easy self referencing
addonEnv.addon = addon
addonEnv.widget = addon
--// copy the engine enviroment to the addon
tcopy(addonEnv, EG)
--// the shared table
addonEnv.SG = handler.SG
addonEnv.WG = handler.SG
--// wrapped calls (closures)
local h = {}
addonEnv.handler = h
addonEnv[handler.name] = h
addonEnv.include = function(f) return include(f, addon) end
h.ForceLayout = handler.ForceLayout
h.RemoveWidget = function() handler:Remove(addon, "auto") end
h.GetCommands = function() return handler.commands end
h.GetViewSizes = handler.GetViewSizes
h.GetHourTimer = handler.GetHourTimer
h.IsMouseOwner = function() return (handler.mouseOwner == addon) end
h.DisownMouse = function()
if (handler.mouseOwner == addon) then
handler.mouseOwner = nil
end
end
h.UpdateCallIn = function(_, name) handler:UpdateWidgetCallIn(name, addon) end
h.RemoveCallIn = function(_, name) handler:RemoveWidgetCallIn(name, addon) end
h.AddAction = function(_, cmd, func, data, types) return actionHandler.AddWidgetAction(addon, cmd, func, data, types) end
h.RemoveAction = function(_, cmd, types) return actionHandler.RemoveWidgetAction(addon, cmd, types) end
h.TextAction = function(_, command) return actionHandler.TextAction(command) end
h.AddLayoutCommand = function(_, cmd)
if (handler.inCommandsChanged) then
table.insert(handler.customCommands, cmd)
else
spEcho("AddLayoutCommand() can only be used in CommandsChanged()")
end
end
h.ConfigLayoutHandler = handler.ConfigLayoutHandler
h.RegisterGlobal = function(_, name, value) return handler:RegisterGlobal(addon, name, value) end
h.DeregisterGlobal = function(_, name) return handler:DeregisterGlobal(addon, name) end
h.SetGlobal = function(_, name, value) return handler:SetGlobal(addon, name, value) end
return addonEnv
end
function handler:ParseAddonRev2(filepath, _VFSMODE)
_VFSMODE = _VFSMODE or VFSMODE
local basename = Basename(filepath)
--// load the code
local addonEnv = handler:NewWidgetRev2()
local success, err = pcall(VFS.Include, filepath, addonEnv, _VFSMODE)
if (not success) then
spEcho('Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
if (err == false) then
return nil --// addon asked for a silent death
end
local addon = addonEnv.addon
--// Validate Callins
err = ValidateAddon(addon)
if (err) then
spEcho('Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
return addon
end
function handler:ParseAddonRev1(filepath, _VFSMODE)
_VFSMODE = _VFSMODE or VFSMODE
local basename = Basename(filepath)
if not VFS.FileExists(filepath, _VFSMODE) then --doesn't exist in this VFS
return nil
end
--// load the code
local addonEnv = handler:NewAddonRev1()
local success, err = pcall(VFS.Include, filepath, addonEnv, _VFSMODE)
if (not success) then
spEcho('Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
if (err == false) then
return nil --// addon asked for a silent death
end
local addon = addonEnv.widget
--// Validate Callins
err = ValidateAddon(addon)
if (err) then
spEcho('Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
return addon
end
function handler:ParseAddon(ki, filepath, _VFSMODE)
if ((ki._rev or 0) >= 2) then
return handler:ParseAddonRev2(filepath, _VFSMODE)
else
return handler:ParseAddonRev1(filepath, _VFSMODE)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function handler:GetFancyString(name, str)
if not str then str = name end
local ki = handler.knownWidgets[name]
if ki then
if ki.fromZip then
return ("<%s>"):format(str)
else
return ("*%s*"):format(str)
end
else
return ("(%s)"):format(str)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Callin Closures
local function InsertAddonCallIn(ciName, addon)
if (knownCallIns[ciName]) then
local f = addon[ciName]
--// use callInName__ to respect when a addon dislinked the function via :RemoveWidgetCallIn (and there is is still a func named addon[callInName])
addon[ciName .. "__"] = f --// non closure!
if ((addon._info._rev or 0) <= 1) then
--// old addons had addon:CallInXYZ, so we need to pass the addon as self object
local f_ = f
f = function(...) return f_(addon, ...) end
end
local swf = SafeWrapFunc(addon, f, ciName)
return handler.callInLists[ciName]:Insert(addon, swf)
elseif (handler.verbose) then
spEcho(LUA_NAME .. "::InsertWidgetCallIn: Unknown CallIn \"" .. ciName.. "\"")
end
return false
end
local function RemoveAddonCallIn(ciName, addon)
if (knownCallIns[ciName]) then
addon[ciName .. "__"] = nil
return handler.callInLists[ciName]:Remove(addon)
elseif (handler.verbose) then
spEcho(LUA_NAME .. "::RemoveWidgetCallIn: Unknown CallIn \"" .. ciName.. "\"")
end
return false
end
local function RemoveAddonCallIns(addon)
for ciName,ciList in pairs(handler.callInLists) do
ciList:Remove(addon)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function handler:Load(filepath, _VFSMODE)
--FIXME handler:AllowWidgetLoading(filepath)
--always load the newer version first
if (not _VFSMODE) or _VFSMODE == VFS.RAW_FIRST or _VFSMODE == VFS.ZIP_FIRST then
local ki_RAW = handler:LoadWidgetInfo(filepath, VFS.RAW_ONLY)
if ki_RAW then
local ki_ZIP = handler:LoadWidgetInfo(filepath, VFS.ZIP) --map and mod
if ki_ZIP then
local zipV, rawV = (tonumber(ki_ZIP.version) or -1), (tonumber(ki_RAW.version) or -1)
if zipV ~= rawV then
if zipV > rawV then
_VFSMODE = VFS.ZIP
else
_VFSMODE = VFS.RAW_ONLY
end
end
end
end
end
--// Load KnownInfo
local ki = handler:LoadWidgetInfo(filepath, _VFSMODE)
if (not ki) then
return
end
--// check dependencies
for i=1,#ki.depend do
local dep = ki.depend[i]
if not (handler.knownWidgets[dep] or {}).active then
spEcho(("%s: Missing/Unloaded dependency \"%s\" for \"%s\"."):format(LUA_NAME, dep, ki.name))
return
end
end
--// Load Addon
local addon = handler:ParseAddon(ki, filepath, _VFSMODE)