/
gadgets.lua
2282 lines (1893 loc) · 55.6 KB
/
gadgets.lua
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
-- NOTE overrides section at bottom!
-- ALSO look for comments -- FIXME: not in base
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: gadgets.lua
-- brief: the gadget manager, a call-in router
-- author: Dave Rodgers
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- TODO: - get rid of the ':'/self referencing, it's a waste of cycles
-- - (De)RegisterCOBCallback(data)
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local HANDLER_BASENAME = "gadgets.lua"
local isMission = VFS.FileExists("mission.lua") -- or Game.gameName:find("Scenario Editor")
local HANDLER_DIR = 'LuaGadgets/'
local GADGETS_DIR = Script.GetName():gsub('US$', '') .. '/Gadgets/'
local SCRIPT_DIR = Script.GetName() .. '/'
local VFSMODE = VFS.ZIP_ONLY
if (Spring.IsDevLuaEnabled()) then
VFSMODE = VFS.RAW_ONLY
end
VFS.Include(HANDLER_DIR .. 'setupdefs.lua', nil, VFSMODE)
VFS.Include(HANDLER_DIR .. 'system.lua', nil, VFSMODE)
VFS.Include(HANDLER_DIR .. 'callins.lua', nil, VFSMODE)
VFS.Include(SCRIPT_DIR .. 'utilities.lua', nil, VFSMODE)
local actionHandler = VFS.Include(HANDLER_DIR .. 'actions.lua', nil, VFSMODE)
local reverseCompatAllowStartPosition = not Spring.Utilities.IsCurrentVersionNewerThan(103, 629)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- the gadgetHandler object
--
gadgetHandler = {
gadgets = {},
orderList = {},
knownGadgets = {},
knownCount = 0,
knownChanged = true,
GG = {}, -- shared table for gadgets
globals = {}, -- global vars/funcs
CMDIDs = {},
xViewSize = 1,
yViewSize = 1,
xViewSizeOld = 1,
yViewSizeOld = 1,
mouseOwner = nil,
actionHandler = actionHandler, -- FIXME: not in base
}
-- these call-ins are set to 'nil' if not used
-- they are setup in UpdateCallIns()
local callInLists = {
"Shutdown",
"GamePreload",
"GameStart",
"GameOver",
"GameID",
"TeamDied",
"PlayerAdded",
"PlayerChanged",
"PlayerRemoved",
"GameFrame",
"ViewResize", -- FIXME ?
"TextCommand", -- FIXME ?
"GotChatMsg",
"RecvLuaMsg",
-- Unit CallIns
"UnitCreated",
"UnitFinished",
"UnitReverseBuilt",
"UnitFromFactory",
"UnitDestroyed",
"RenderUnitDestroyed",
"UnitExperience",
"UnitIdle",
"UnitCmdDone",
"UnitPreDamaged",
"UnitDamaged",
"UnitStunned",
"UnitTaken",
"UnitGiven",
"UnitEnteredRadar",
"UnitEnteredLos",
"UnitLeftRadar",
"UnitLeftLos",
"UnitSeismicPing",
"UnitLoaded",
"UnitUnloaded",
"UnitCloaked",
"UnitDecloaked",
-- optional
-- "UnitUnitCollision",
-- "UnitFeatureCollision",
-- "UnitMoveFailed",
"StockpileChanged",
-- Feature CallIns
"FeatureCreated",
"FeatureDestroyed",
-- Projectile CallIns
"ProjectileCreated",
"ProjectileDestroyed",
-- Shield CallIns
"ShieldPreDamaged",
-- Misc Synced CallIns
"Explosion",
-- LuaRules CallIns (note: the *PreDamaged calls belong here too)
"CommandFallback",
"AllowCommand",
"AllowStartPosition",
"AllowUnitCreation",
"AllowUnitTransfer",
"AllowUnitBuildStep",
"AllowUnitTransport",
"AllowUnitTransportLoad",
"AllowUnitTransportUnload",
"AllowUnitCloak",
"AllowUnitDecloak",
"AllowUnitTargetRange",
"AllowFeatureBuildStep",
"AllowFeatureCreation",
"AllowResourceLevel",
"AllowResourceTransfer",
"AllowDirectUnitControl",
"AllowBuilderHoldFire",
"MoveCtrlNotify",
"TerraformComplete",
"AllowWeaponTargetCheck",
"AllowWeaponTarget",
"AllowWeaponInterceptTarget",
-- unsynced
"DrawUnit",
"DrawFeature",
"DrawShield",
"DrawProjectile",
"RecvSkirmishAIMessage",
-- COB CallIn (FIXME?)
"CobCallback",
-- Unsynced CallIns
"Update",
"DefaultCommand",
"DrawGenesis",
"DrawWorld",
"DrawWorldPreUnit",
"DrawWorldShadow",
"DrawWorldReflection",
"DrawWorldRefraction",
"DrawScreenEffects",
"DrawScreenPost",
"DrawScreen",
"DrawInMiniMap",
"RecvFromSynced",
-- moved from LuaUI
"KeyPress",
"KeyRelease",
"MousePress",
"MouseRelease",
"MouseMove",
"MouseWheel",
"IsAbove",
"GetTooltip",
-- FIXME -- not implemented (more of these?)
"WorldTooltip",
"MapDrawCmd",
"GameSetup",
"DefaultCommand",
-- Save/Load
"Save",
"Load",
-- FIXME: NOT IN BASE
"UnitCommand",
"UnitEnteredWater",
"UnitEnteredAir",
"UnitLeftWater",
"UnitLeftAir",
"UnsyncedHeightMapUpdate"
}
-- initialize the call-in lists
do
for _,listname in ipairs(callInLists) do
gadgetHandler[listname .. 'List'] = {}
end
end
-- Utility call
local isSyncedCode = (SendToUnsynced ~= nil)
local function IsSyncedCode()
return isSyncedCode
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- returns: basename, dirname
--
local function Basename(fullpath)
local _,_,base = string.find(fullpath, "([^\\/:]*)$")
local _,_,path = string.find(fullpath, "(.*[\\/:])[^\\/:]*$")
if (path == nil) then path = "" end
return base, path
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadgetHandler:Initialize()
local unsortedGadgets = {}
-- get the gadget names
local gadgetFiles = VFS.DirList(GADGETS_DIR, "*.lua", VFSMODE)
-- table.sort(gadgetFiles)
-- for k,gf in ipairs(gadgetFiles) do
-- Spring.Echo('gf1 = ' .. gf) -- FIXME
-- end
-- stuff the gadgets into unsortedGadgets
for k,gf in ipairs(gadgetFiles) do
-- Spring.Echo('gf2 = ' .. gf) -- FIXME
local gadget = self:LoadGadget(gf)
if (gadget) then
table.insert(unsortedGadgets, gadget)
end
end
-- sort the gadgets
table.sort(unsortedGadgets, function(g1, g2)
local l1 = g1.ghInfo.layer
local l2 = g2.ghInfo.layer
if (l1 ~= l2) then
return (l1 < l2)
end
local n1 = g1.ghInfo.name
local n2 = g2.ghInfo.name
local o1 = self.orderList[n1]
local o2 = self.orderList[n2]
if (o1 ~= o2) then
return (o1 < o2)
else
return (n1 < n2)
end
end)
-- add the gadgets
for _,g in ipairs(unsortedGadgets) do
gadgetHandler:InsertGadget(g)
local name = g.ghInfo.name
local basename = g.ghInfo.basename
print(string.format("Loaded gadget: %-18s <%s>", name, basename))
end
end
function gadgetHandler:LoadGadget(filename)
local basename = Basename(filename)
local text = VFS.LoadFile(filename, VFSMODE)
if (text == nil) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Failed to load: ' .. filename)
return nil
end
local chunk, err = loadstring(text, filename)
if (chunk == nil) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
local gadget = gadgetHandler:NewGadget()
setfenv(chunk, gadget)
local success
success, err = pcall(chunk)
if (not success) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
if (err == false) then
return nil -- gadget asked for a quiet death
end
-- raw access to gadgetHandler
if (gadget.GetInfo and gadget:GetInfo().handler) then
gadget.gadgetHandler = self
end
self:FinalizeGadget(gadget, filename, basename)
local name = gadget.ghInfo.name
err = self:ValidateGadget(gadget)
if (err) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
local knownInfo = self.knownGadgets[name]
if (knownInfo) then
if (knownInfo.active) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Failed to load: ' .. basename .. ' (duplicate name)')
return nil
end
else
-- create a knownInfo table
knownInfo = {}
knownInfo.desc = gadget.ghInfo.desc
knownInfo.author = gadget.ghInfo.author
knownInfo.basename = gadget.ghInfo.basename
knownInfo.filename = gadget.ghInfo.filename
self.knownGadgets[name] = knownInfo
self.knownCount = self.knownCount + 1
self.knownChanged = true
end
knownInfo.active = true
local info = gadget.GetInfo and gadget:GetInfo()
local order = self.orderList[name]
if (((order ~= nil) and (order > 0)) or
((order == nil) and ((info == nil) or info.enabled))) then
-- this will be an active gadget
if (order == nil) then
self.orderList[name] = 12345 -- back of the pack
else
self.orderList[name] = order
end
else
self.orderList[name] = 0
self.knownGadgets[name].active = false
return nil
end
return gadget
end
function gadgetHandler:NewGadget()
local gadget = {}
-- load the system calls into the gadget table
for k,v in pairs(System) do
gadget[k] = v
end
gadget._G = _G -- the global table
gadget.GG = self.GG -- the shared table
gadget.gadget = gadget -- easy self referencing
-- wrapped calls (closures)
gadget.gadgetHandler = {}
local gh = gadget.gadgetHandler
gh.gadgetHandler = self -- NOT IN BASE (required for api_subdir_gadgets)
gadget.include = function (f)
return VFS.Include(f, gadget, VFSMODE)
end
gh.RaiseGadget = function (_) self:RaiseGadget(gadget) end
gh.LowerGadget = function (_) self:LowerGadget(gadget) end
gh.RemoveGadget = function (_) self:RemoveGadget(gadget) end
gh.GetViewSizes = function (_) return self:GetViewSizes() end
gh.GetHourTimer = function (_) return self:GetHourTimer() end
gh.IsSyncedCode = function (_) return IsSyncedCode() end
gh.UpdateCallIn = function (_, name)
self:UpdateGadgetCallIn(name, gadget)
end
gh.RemoveCallIn = function (_, name)
self:RemoveGadgetCallIn(name, gadget)
end
gh.RegisterCMDID = function(_, id)
self:RegisterCMDID(gadget, id)
end
gh.RegisterGlobal = function(_, name, value)
return self:RegisterGlobal(gadget, name, value)
end
gh.DeregisterGlobal = function(_, name)
return self:DeregisterGlobal(gadget, name)
end
gh.SetGlobal = function(_, name, value)
return self:SetGlobal(gadget, name, value)
end
gh.AddChatAction = function (_, cmd, func, help)
return actionHandler.AddChatAction(gadget, cmd, func, help)
end
gh.RemoveChatAction = function (_, cmd)
return actionHandler.RemoveChatAction(gadget, cmd)
end
if (not IsSyncedCode()) then
gh.AddSyncAction = function(_, cmd, func, help)
return actionHandler.AddSyncAction(gadget, cmd, func, help)
end
gh.RemoveSyncAction = function(_, cmd)
return actionHandler.RemoveSyncAction(gadget, cmd)
end
end
-- for proxied call-ins
gh.IsMouseOwner = function (_)
return (self.mouseOwner == gadget)
end
gh.DisownMouse = function (_)
if (self.mouseOwner == gadget) then
self.mouseOwner = nil
end
end
return gadget
end
function gadgetHandler:FinalizeGadget(gadget, filename, basename)
local gi = {}
gi.filename = filename
gi.basename = basename
if (gadget.GetInfo == nil) then
gi.name = basename
gi.layer = 0
else
local info = gadget:GetInfo()
gi.name = info.name or basename
gi.layer = info.layer or 0
gi.desc = info.desc or ""
gi.author = info.author or ""
gi.license = info.license or ""
gi.enabled = info.enabled or false
end
gadget.ghInfo = {} -- a proxy table
local mt = {
__index = gi,
__newindex = function() error("ghInfo tables are read-only") end,
__metatable = "protected"
}
setmetatable(gadget.ghInfo, mt)
end
function gadgetHandler:ValidateGadget(gadget)
if (gadget.GetTooltip and not gadget.IsAbove) then
return "Gadget has GetTooltip() but not IsAbove()"
end
if (gadget.TweakGetTooltip and not gadget.TweakIsAbove) then
return "Gadget has TweakGetTooltip() but not TweakIsAbove()"
end
return nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function ArrayInsert(t, f, g)
if (f) then
local layer = g.ghInfo.layer
local index = 1
for i,v in ipairs(t) do
if (v == g) then
return -- already in the table
end
if (layer >= v.ghInfo.layer) then
index = i + 1
end
end
table.insert(t, index, g)
end
end
local function ArrayRemove(t, g)
for k,v in ipairs(t) do
if (v == g) then
table.remove(t, k)
-- break
end
end
end
function gadgetHandler:InsertGadget(gadget)
if (gadget == nil) then
return
end
ArrayInsert(self.gadgets, true, gadget)
for _,listname in ipairs(callInLists) do
local func = gadget[listname]
if (type(func) == 'function') then
ArrayInsert(self[listname..'List'], func, gadget)
end
end
self:UpdateCallIns()
if (gadget.Initialize) then
gadget:Initialize()
end
self:UpdateCallIns()
end
function gadgetHandler:RemoveGadget(gadget)
if (gadget == nil) then
return
end
local name = gadget.ghInfo.name
self.knownGadgets[name].active = false
--Spring.Echo(name)
if (gadget.Shutdown) then
gadget:Shutdown()
end
ArrayRemove(self.gadgets, gadget)
self:RemoveGadgetGlobals(gadget)
actionHandler.RemoveGadgetActions(gadget)
for _,listname in ipairs(callInLists) do
ArrayRemove(self[listname..'List'], gadget)
end
for id,g in pairs(self.CMDIDs) do
if (g == gadget) then
self.CMDIDs[id] = nil
end
end
self:UpdateCallIns()
end
--------------------------------------------------------------------------------
function gadgetHandler:UpdateCallIn(name)
local listName = name .. 'List'
if ((#self[listName] > 0) or
(name == 'GotChatMsg') or
(name == 'RecvFromSynced')) then
local selffunc = self[name]
_G[name] = function(...)
return selffunc(self, ...)
end
else
_G[name] = nil
end
Script.UpdateCallIn(name)
end
function gadgetHandler:UpdateGadgetCallIn(name, g)
local listName = name .. 'List'
local ciList = self[listName]
if (ciList) then
local func = g[name]
if (type(func) == 'function') then
ArrayInsert(ciList, func, g)
else
ArrayRemove(ciList, g)
end
self:UpdateCallIn(name)
else
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'UpdateGadgetCallIn: bad name: ' .. name)
end
end
function gadgetHandler:RemoveGadgetCallIn(name, g)
local listName = name .. 'List'
local ciList = self[listName]
if (ciList) then
ArrayRemove(ciList, g)
self:UpdateCallIn(name)
else
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'RemoveGadgetCallIn: bad name: ' .. name)
end
end
function gadgetHandler:UpdateCallIns()
for _,name in ipairs(callInLists) do
self:UpdateCallIn(name)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadgetHandler:EnableGadget(name)
local ki = self.knownGadgets[name]
if (not ki) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, "EnableGadget(), could not find gadget: " .. tostring(name))
return false
end
if (not ki.active) then
Spring.Echo('Loading: '..ki.filename)
local order = gadgetHandler.orderList[name]
if (not order or (order <= 0)) then
self.orderList[name] = 1
end
local w = self:LoadGadget(ki.filename)
if (not w) then return false end
self:InsertGadget(w)
end
return true
end
function gadgetHandler:DisableGadget(name)
local ki = self.knownGadgets[name]
if (not ki) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, "DisableGadget(), could not find gadget: " .. tostring(name))
return false
end
if (ki.active) then
local w = self:FindGadget(name)
if (not w) then return false end
Spring.Echo('Removed: '..ki.filename)
self:RemoveGadget(w) -- deactivate
self.orderList[name] = 0 -- disable
end
return true
end
function gadgetHandler:ToggleGadget(name)
local ki = self.knownGadgets[name]
if (not ki) then
Spring.Echo("ToggleGadget(), could not find gadget: " .. tostring(name))
return
end
if (ki.active) then
return self:DisableGadget(name)
elseif (self.orderList[name] <= 0) then
return self:EnableGadget(name)
else
-- the gadget is not active, but enabled; disable it
self.orderList[name] = 0
end
return true
end
--------------------------------------------------------------------------------
local function FindGadgetIndex(t, w)
for k,v in ipairs(t) do
if (v == w) then
return k
end
end
return nil
end
local function FindLowestIndex(t, i, layer)
for x = (i - 1), 1, -1 do
if (t[x].ghInfo.layer < layer) then
return x + 1
end
end
return 1
end
function gadgetHandler:RaiseGadget(gadget)
if (gadget == nil) then
return
end
local function Raise(t, f, w)
if (f == nil) then return end
local i = FindGadgetIndex(t, w)
if (i == nil) then return end
local n = FindLowestIndex(t, i, w.ghInfo.layer)
if (n and (n < i)) then
table.remove(t, i)
table.insert(t, n, w)
end
end
Raise(self.gadgets, true, gadget)
for _,listname in ipairs(callInLists) do
Raise(self[listname..'List'], gadget[listname], gadget)
end
end
local function FindHighestIndex(t, i, layer)
local ts = #t
for x = (i + 1),ts do
if (t[x].ghInfo.layer > layer) then
return (x - 1)
end
end
return (ts + 1)
end
function gadgetHandler:LowerGadget(gadget)
if (gadget == nil) then
return
end
local function Lower(t, f, w)
if (f == nil) then return end
local i = FindGadgetIndex(t, w)
if (i == nil) then return end
local n = FindHighestIndex(t, i, w.ghInfo.layer)
if (n and (n > i)) then
table.insert(t, n, w)
table.remove(t, i)
end
end
Lower(self.gadgets, true, gadget)
for _,listname in ipairs(callInLists) do
Lower(self[listname..'List'], gadget[listname], gadget)
end
end
function gadgetHandler:FindGadget(name)
if (type(name) ~= 'string') then
return nil
end
for k,v in ipairs(self.gadgets) do
if (name == v.ghInfo.name) then
return v,k
end
end
return nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Global var/func management
--
function gadgetHandler:RegisterGlobal(owner, name, value)
if ((name == nil) or
(_G[name]) or
(self.globals[name]) or
((CallInsMap and CallInsMap[name]) or (CALLIN_MAP and CALLIN_MAP[name]))) then
return false
end
_G[name] = value
self.globals[name] = owner
return true
end
function gadgetHandler:DeregisterGlobal(owner, name)
if (name == nil) then
return false
end
_G[name] = nil
self.globals[name] = nil
return true
end
function gadgetHandler:SetGlobal(owner, name, value)
if ((name == nil) or (self.globals[name] ~= owner)) then
return false
end
_G[name] = value
return true
end
function gadgetHandler:RemoveGadgetGlobals(owner)
local count = 0
for name, o in pairs(self.globals) do
if (o == owner) then
_G[name] = nil
self.globals[name] = nil
count = count + 1
end
end
return count
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Helper facilities
--
local hourTimer = 0
function gadgetHandler:GetHourTimer()
return hourTimer
end
function gadgetHandler:GetViewSizes()
return self.xViewSize, self.yViewSize
end
function gadgetHandler:RegisterCMDID(gadget, id)
if not id then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Gadget (' .. gadget.ghInfo.name .. ') ' ..
'tried to register a NIL CMD_ID')
else
if (id < 1000) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Gadget (' .. gadget.ghInfo.name .. ') ' ..
'tried to register a reserved CMD_ID')
Script.Kill('Reserved CMD_ID code: ' .. id)
end
if (self.CMDIDs[id] ~= nil) then
Spring.Log(HANDLER_BASENAME, LOG.ERROR, 'Gadget (' .. gadget.ghInfo.name .. ') ' ..
'tried to register a duplicated CMD_ID')
Script.Kill('Duplicate CMD_ID code: ' .. id)
end
self.CMDIDs[id] = gadget
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- The call-in distribution routines
--
function gadgetHandler:GamePreload()
for _,g in ipairs(self.GamePreloadList) do
g:GamePreload()
end
return
end
function gadgetHandler:GameStart()
for _,g in ipairs(self.GameStartList) do
g:GameStart()
end
return
end
function gadgetHandler:Shutdown()
Spring.Echo("Start gadgetHandler:Shutdown")
for _,g in ipairs(self.ShutdownList) do
local name = g.ghInfo.name or "UNKNOWN NAME"
Spring.Echo("Shutdown - " .. name)
g:Shutdown()
end
Spring.Echo("End gadgetHandler:Shutdown")
return
end
function gadgetHandler:GameFrame(frameNum)
for _,g in ipairs(self.GameFrameList) do
g:GameFrame(frameNum)
end
return
end
function gadgetHandler:RecvFromSynced(...)
if (actionHandler.RecvFromSynced(...)) then
return
end
for _,g in ipairs(self.RecvFromSyncedList) do
if (g:RecvFromSynced(...)) then
return
end
end
return
end
--function gadgetHandler:GotChatMsg(msg, player)
-- if ((player == 0) and Spring.IsCheatingEnabled()) then
-- local sp = '^%s*' -- start pattern
-- local ep = '%s+(.*)' -- end pattern
-- local s, e, match
-- s, e, match = string.find(msg, sp..'togglegadget'..ep)
-- if (match) then
-- self:ToggleGadget(match)
-- return true
-- end
-- s, e, match = string.find(msg, sp..'enablegadget'..ep)
-- if (match) then
-- self:EnableGadget(match)
-- return true
-- end
-- s, e, match = string.find(msg, sp..'disablegadget'..ep)
-- if (match) then
-- self:DisableGadget(match)
-- return true
-- end
-- end
--
-- if (actionHandler.GotChatMsg(msg, player)) then
-- return true
-- end
--
-- for _,g in ipairs(self.GotChatMsgList) do
-- if (g:GotChatMsg(msg, player)) then
-- return true
-- end
-- end
--
-- return false
--end
function gadgetHandler:RecvLuaMsg(msg, player)
for _,g in ipairs(self.RecvLuaMsgList) do
if (g:RecvLuaMsg(msg, player)) then
return true
end
end
return false
end
--------------------------------------------------------------------------------
--
-- Drawing call-ins
--
-- generates ViewResize() calls for the gadgets
function gadgetHandler:SetViewSize(vsx, vsy)
self.xViewSize = vsx
self.yViewSize = vsy
if ((self.xViewSizeOld ~= vsx) or
(self.yViewSizeOld ~= vsy)) then
gadgetHandler:ViewResize(vsx, vsy)
self.xViewSizeOld = vsx
self.yViewSizeOld = vsy
end
end
function gadgetHandler:ViewResize(vsx, vsy)
for _,g in ipairs(self.ViewResizeList) do
g:ViewResize(vsx, vsy)
end
return
end
--------------------------------------------------------------------------------
--
-- Game call-ins
--