forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LuaEngine.cpp
1158 lines (1016 loc) · 36.3 KB
/
LuaEngine.cpp
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
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#include "Hooks.h"
#include "LuaEngine.h"
#include "BindingMap.h"
#include "ElunaCompat.h"
#include "ElunaConfig.h"
#include "ElunaEventMgr.h"
#include "ElunaIncludes.h"
#include "ElunaLoader.h"
#include "ElunaTemplate.h"
#include "ElunaUtility.h"
#include "ElunaCreatureAI.h"
#include "ElunaInstanceAI.h"
extern "C"
{
// Base lua libraries
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
// Additional lua libraries
};
extern void RegisterFunctions(Eluna* E);
void Eluna::_ReloadEluna()
{
// Remove all timed events
eventMgr->SetStates(LUAEVENT_STATE_ERASE);
// Close lua
CloseLua();
// Open new lua and libraries
OpenLua();
// Run scripts from loaded paths
RunScripts();
reload = false;
}
Eluna::Eluna(Map* map, bool compatMode) :
event_level(0),
push_counter(0),
boundMap(map),
compatibilityMode(compatMode),
L(NULL),
eventMgr(NULL),
ServerEventBindings(NULL),
PlayerEventBindings(NULL),
GuildEventBindings(NULL),
GroupEventBindings(NULL),
VehicleEventBindings(NULL),
BGEventBindings(NULL),
PacketEventBindings(NULL),
CreatureEventBindings(NULL),
CreatureGossipBindings(NULL),
GameObjectEventBindings(NULL),
GameObjectGossipBindings(NULL),
SpellEventBindings(NULL),
ItemEventBindings(NULL),
ItemGossipBindings(NULL),
PlayerGossipBindings(NULL),
MapEventBindings(NULL),
InstanceEventBindings(NULL),
CreatureUniqueBindings(NULL)
{
OpenLua();
eventMgr = new EventMgr(this);
// if the script cache is ready, run scripts, otherwise flag state for reload
if (sElunaLoader->GetCacheState() == SCRIPT_CACHE_READY)
RunScripts();
else
reload = true;
}
Eluna::~Eluna()
{
CloseLua();
delete eventMgr;
eventMgr = NULL;
}
void Eluna::CloseLua()
{
OnLuaStateClose();
DestroyBindStores();
// Must close lua state after deleting stores and mgr
if (L)
lua_close(L);
L = NULL;
stacktraceFunctionStackIndex = 0;
instanceDataRefs.clear();
continentDataRefs.clear();
}
static int PrecompiledLoader(lua_State* L)
{
const char* modname = lua_tostring(L, 1);
if (modname == NULL)
return 0;
const std::vector<LuaScript>& scripts = sElunaLoader->GetLuaScripts();
auto it = std::find_if(scripts.begin(), scripts.end(), [modname](const LuaScript& script) { return script.filename == modname; });
if (it == scripts.end()) {
lua_pushfstring(L, "\n\tno precompiled script '%s' found", modname);
return 1;
}
if (luaL_loadbuffer(L, reinterpret_cast<const char*>(&it->bytecode[0]), it->bytecode.size(), it->filename.c_str()))
{
// Stack: modname, errmsg
return lua_error(L);
}
// Stack: modname, filefunction
lua_pushstring(L, it->filepath.c_str());
// Stack: modname, filefunction, modpath
return 2;
}
void Eluna::OpenLua()
{
L = luaL_newstate();
lua_pushlightuserdata(L, this);
lua_setfield(L, LUA_REGISTRYINDEX, ELUNA_STATE_PTR);
CreateBindStores();
// open base lua libraries
luaL_openlibs(L);
// Register methods and functions
RegisterFunctions(this);
// get require paths
const std::string& requirepath = sElunaLoader->GetRequirePath();
const std::string& requirecpath = sElunaLoader->GetRequireCPath();
// Set lua require folder paths (scripts folder structure)
lua_getglobal(L, "package");
lua_pushstring(L, requirepath.c_str());
lua_setfield(L, -2, "path");
lua_pushstring(L, requirecpath.c_str());
lua_setfield(L, -2, "cpath");
// Set package.loaders loader for precompiled scripts
lua_getfield(L, -1, "loaders");
if (lua_isnil(L, -1)) {
// Lua 5.2+ uses searchers instead of loaders
lua_pop(L, 1);
lua_getfield(L, -1, "searchers");
}
// insert the new loader to the loaders table by shifting other elements down by one
const int newLoaderIndex = 1;
for (int i = lua_rawlen(L, -1); i >= newLoaderIndex; --i) {
lua_rawgeti(L, -1, i);
lua_rawseti(L, -2, i + 1);
}
lua_pushcfunction(L, &PrecompiledLoader);
lua_rawseti(L, -2, newLoaderIndex);
lua_pop(L, 2); // pop loaders/searchers table, pop package table
// Leave StackTrace function on stack and save reference to it
lua_pushcfunction(L, &StackTrace);
stacktraceFunctionStackIndex = lua_gettop(L);
}
void Eluna::CreateBindStores()
{
DestroyBindStores();
ServerEventBindings = new BindingMap< EventKey<Hooks::ServerEvents> >(L);
PlayerEventBindings = new BindingMap< EventKey<Hooks::PlayerEvents> >(L);
GuildEventBindings = new BindingMap< EventKey<Hooks::GuildEvents> >(L);
GroupEventBindings = new BindingMap< EventKey<Hooks::GroupEvents> >(L);
VehicleEventBindings = new BindingMap< EventKey<Hooks::VehicleEvents> >(L);
BGEventBindings = new BindingMap< EventKey<Hooks::BGEvents> >(L);
PacketEventBindings = new BindingMap< EntryKey<Hooks::PacketEvents> >(L);
CreatureEventBindings = new BindingMap< EntryKey<Hooks::CreatureEvents> >(L);
CreatureGossipBindings = new BindingMap< EntryKey<Hooks::GossipEvents> >(L);
GameObjectEventBindings = new BindingMap< EntryKey<Hooks::GameObjectEvents> >(L);
GameObjectGossipBindings = new BindingMap< EntryKey<Hooks::GossipEvents> >(L);
SpellEventBindings = new BindingMap< EntryKey<Hooks::SpellEvents> >(L);
ItemEventBindings = new BindingMap< EntryKey<Hooks::ItemEvents> >(L);
ItemGossipBindings = new BindingMap< EntryKey<Hooks::GossipEvents> >(L);
PlayerGossipBindings = new BindingMap< EntryKey<Hooks::GossipEvents> >(L);
MapEventBindings = new BindingMap< EntryKey<Hooks::InstanceEvents> >(L);
InstanceEventBindings = new BindingMap< EntryKey<Hooks::InstanceEvents> >(L);
CreatureUniqueBindings = new BindingMap< UniqueObjectKey<Hooks::CreatureEvents> >(L);
}
void Eluna::DestroyBindStores()
{
delete ServerEventBindings;
delete PlayerEventBindings;
delete GuildEventBindings;
delete GroupEventBindings;
delete VehicleEventBindings;
delete PacketEventBindings;
delete CreatureEventBindings;
delete CreatureGossipBindings;
delete GameObjectEventBindings;
delete GameObjectGossipBindings;
delete SpellEventBindings;
delete ItemEventBindings;
delete ItemGossipBindings;
delete PlayerGossipBindings;
delete BGEventBindings;
delete MapEventBindings;
delete InstanceEventBindings;
delete CreatureUniqueBindings;
ServerEventBindings = NULL;
PlayerEventBindings = NULL;
GuildEventBindings = NULL;
GroupEventBindings = NULL;
VehicleEventBindings = NULL;
PacketEventBindings = NULL;
CreatureEventBindings = NULL;
CreatureGossipBindings = NULL;
GameObjectEventBindings = NULL;
GameObjectGossipBindings = NULL;
SpellEventBindings = NULL;
ItemEventBindings = NULL;
ItemGossipBindings = NULL;
PlayerGossipBindings = NULL;
BGEventBindings = NULL;
MapEventBindings = NULL;
InstanceEventBindings = NULL;
CreatureUniqueBindings = NULL;
}
void Eluna::RunScripts()
{
int32 const boundMapId = GetBoundMapId();
uint32 const boundInstanceId = GetBoundInstanceId();
ELUNA_LOG_DEBUG("[Eluna]: Running scripts for state: %i, instance: %u", boundMapId, boundInstanceId);
uint32 oldMSTime = ElunaUtil::GetCurrTime();
uint32 count = 0;
std::unordered_map<std::string, std::string> loaded; // filename, path
lua_getglobal(L, "require");
// Stack: require
const std::vector<LuaScript>& scripts = sElunaLoader->GetLuaScripts();
for (auto it = scripts.begin(); it != scripts.end(); ++it)
{
// if the Eluna state is in compatibility mode, it should load all scripts, including those tagged with a specific map ID
if (!GetCompatibilityMode())
{
// check that the script file is either global or meant to be loaded for this map
if (it->mapId != -1 && it->mapId != boundMapId)
{
ELUNA_LOG_DEBUG("[Eluna]: `%s` is tagged %i and will not load for map: %i", it->filename.c_str(), it->mapId, boundMapId);
continue;
}
}
// Check that no duplicate names exist
if (loaded.find(it->filename) != loaded.end())
{
ELUNA_LOG_ERROR("[Eluna]: Error loading `%s`. File with same name already loaded from `%s`, rename either file", it->filepath.c_str(), loaded[it->filename].c_str());
continue;
}
loaded[it->filename] = it->filepath;
// We call require on the filename to load the script
// A custom loader is used to load the script from the combined_scripts table
// The loader is set up in Eluna::OpenLua
lua_pushvalue(L, -1); // Stack: require, require
lua_pushstring(L, it->filename.c_str()); // Stack: require, require, filename
if (ExecuteCall(1, 0))
{
// Successfully called require on the script
ELUNA_LOG_DEBUG("[Eluna]: Successfully loaded `%s`", it->filepath.c_str());
++count;
continue;
}
// Stack: require
}
// Stack: require
lua_pop(L, 1);
ELUNA_LOG_INFO("[Eluna]: Executed %u Lua scripts in %u ms for map: %i, instance: %u", count, ElunaUtil::GetTimeDiff(oldMSTime), boundMapId, boundInstanceId);
OnLuaStateOpen();
}
#if !defined TRACKABLE_PTR_NAMESPACE
void Eluna::InvalidateObjects()
{
++callstackid;
ASSERT(callstackid && "Callstackid overflow");
}
#endif
void Eluna::Report(lua_State* _L)
{
const char* msg = lua_tostring(_L, -1);
ELUNA_LOG_ERROR("%s", msg);
lua_pop(_L, 1);
}
// Borrowed from http://stackoverflow.com/questions/12256455/print-stacktrace-from-c-code-with-embedded-lua
int Eluna::StackTrace(lua_State* _L)
{
// Stack: errmsg
if (!lua_isstring(_L, -1)) /* 'message' not a string? */
return 1; /* keep it intact */
// Stack: errmsg, debug
lua_getglobal(_L, "debug");
if (!lua_istable(_L, -1))
{
lua_pop(_L, 1);
return 1;
}
// Stack: errmsg, debug, traceback
lua_getfield(_L, -1, "traceback");
if (!lua_isfunction(_L, -1))
{
lua_pop(_L, 2);
return 1;
}
lua_pushvalue(_L, -3); /* pass error message */
lua_pushinteger(_L, 1); /* skip this function and traceback */
// Stack: errmsg, debug, traceback, errmsg, 2
lua_call(_L, 2, 1); /* call debug.traceback */
// dirty stack?
// Stack: errmsg, debug, tracemsg
return 1;
}
bool Eluna::ExecuteCall(int params, int res)
{
int top = lua_gettop(L);
int base = top - params;
// Expected: function, [parameters]
ASSERT(base > 0);
// Check function type
if (!lua_isfunction(L, base))
{
ELUNA_LOG_ERROR("[Eluna]: Cannot execute call: registered value is %s, not a function.", luaL_tolstring(L, base, NULL));
ASSERT(false); // stack probably corrupt
}
bool usetrace = sElunaConfig->GetConfig(CONFIG_ELUNA_TRACEBACK);
if (usetrace && !lua_iscfunction(L, stacktraceFunctionStackIndex))
{
ELUNA_LOG_ERROR("[Eluna]: Cannot execute call: registered value is %s, not a c-function.", luaL_tolstring(L, stacktraceFunctionStackIndex, NULL));
ASSERT(false); // stack probably corrupt
}
// Objects are invalidated when event_level hits 0
++event_level;
int result = lua_pcall(L, params, res, usetrace ? stacktraceFunctionStackIndex : 0);
--event_level;
// Stack: [results or errmsg]
// lua_pcall returns 0 on success.
// On error print the error and push nils for expected amount of returned values
if (result)
{
// Stack: errmsg
Report(L);
// Force garbage collect
lua_gc(L, LUA_GCCOLLECT, 0);
// Push nils for expected amount of results
for (int i = 0; i < res; ++i)
lua_pushnil(L);
// Stack: [nils]
return false;
}
// Stack: [results]
return true;
}
void Eluna::Push()
{
lua_pushnil(L);
}
void Eluna::Push(const long long l)
{
// pushing pointer to local is fine, a copy of value will be stored, not pointer itself
ElunaTemplate<long long>::Push(this, &l);
}
void Eluna::Push(const unsigned long long l)
{
// pushing pointer to local is fine, a copy of value will be stored, not pointer itself
ElunaTemplate<unsigned long long>::Push(this, &l);
}
void Eluna::Push(const long l)
{
Push(static_cast<long long>(l));
}
void Eluna::Push(const unsigned long l)
{
Push(static_cast<unsigned long long>(l));
}
void Eluna::Push(const int i)
{
lua_pushinteger(L, i);
}
void Eluna::Push(const unsigned int u)
{
lua_pushunsigned(L, u);
}
void Eluna::Push(const double d)
{
lua_pushnumber(L, d);
}
void Eluna::Push(const float f)
{
lua_pushnumber(L, f);
}
void Eluna::Push(const bool b)
{
lua_pushboolean(L, b);
}
void Eluna::Push(const std::string& str)
{
lua_pushstring(L, str.c_str());
}
void Eluna::Push(const char* str)
{
lua_pushstring(L, str);
}
void Eluna::Push(Pet const* pet)
{
Push<Creature>(pet);
}
void Eluna::Push(TempSummon const* summon)
{
Push<Creature>(summon);
}
void Eluna::Push(Unit const* unit)
{
if (!unit)
{
Push();
return;
}
switch (unit->GetTypeId())
{
case TYPEID_UNIT:
Push(unit->ToCreature());
break;
case TYPEID_PLAYER:
Push(unit->ToPlayer());
break;
default:
ElunaTemplate<Unit>::Push(this, unit);
}
}
void Eluna::Push(WorldObject const* obj)
{
if (!obj)
{
Push();
return;
}
switch (obj->GetTypeId())
{
case TYPEID_UNIT:
Push(obj->ToCreature());
break;
case TYPEID_PLAYER:
Push(obj->ToPlayer());
break;
case TYPEID_GAMEOBJECT:
Push(obj->ToGameObject());
break;
case TYPEID_CORPSE:
Push(obj->ToCorpse());
break;
default:
ElunaTemplate<WorldObject>::Push(this, obj);
}
}
void Eluna::Push(Object const* obj)
{
if (!obj)
{
Push();
return;
}
switch (obj->GetTypeId())
{
case TYPEID_UNIT:
Push(obj->ToCreature());
break;
case TYPEID_PLAYER:
Push(obj->ToPlayer());
break;
case TYPEID_GAMEOBJECT:
Push(obj->ToGameObject());
break;
case TYPEID_CORPSE:
Push(obj->ToCorpse());
break;
default:
ElunaTemplate<Object>::Push(this, obj);
}
}
void Eluna::Push(ObjectGuid const guid)
{
// pushing pointer to local is fine, a copy of value will be stored, not pointer itself
ElunaTemplate<ObjectGuid>::Push(this, &guid);
}
static int CheckIntegerRange(lua_State* luastate, int narg, int min, int max)
{
double value = luaL_checknumber(luastate, narg);
char error_buffer[64];
if (value > max)
{
snprintf(error_buffer, 64, "value must be less than or equal to %i", max);
return luaL_argerror(luastate, narg, error_buffer);
}
if (value < min)
{
snprintf(error_buffer, 64, "value must be greater than or equal to %i", min);
return luaL_argerror(luastate, narg, error_buffer);
}
return static_cast<int>(value);
}
static unsigned int CheckUnsignedRange(lua_State* luastate, int narg, unsigned int max)
{
double value = luaL_checknumber(luastate, narg);
if (value < 0)
return luaL_argerror(luastate, narg, "value must be greater than or equal to 0");
if (value > max)
{
char error_buffer[64];
snprintf(error_buffer, 64, "value must be less than or equal to %u", max);
return luaL_argerror(luastate, narg, error_buffer);
}
return static_cast<unsigned int>(value);
}
template<> bool Eluna::CHECKVAL<bool>(int narg)
{
return lua_toboolean(L, narg) != 0;
}
template<> float Eluna::CHECKVAL<float>(int narg)
{
return static_cast<float>(luaL_checknumber(L, narg));
}
template<> double Eluna::CHECKVAL<double>(int narg)
{
return luaL_checknumber(L, narg);
}
template<> signed char Eluna::CHECKVAL<signed char>(int narg)
{
return CheckIntegerRange(L, narg, SCHAR_MIN, SCHAR_MAX);
}
template<> unsigned char Eluna::CHECKVAL<unsigned char>(int narg)
{
return CheckUnsignedRange(L, narg, UCHAR_MAX);
}
template<> short Eluna::CHECKVAL<short>(int narg)
{
return CheckIntegerRange(L, narg, SHRT_MIN, SHRT_MAX);
}
template<> unsigned short Eluna::CHECKVAL<unsigned short>(int narg)
{
return CheckUnsignedRange(L, narg, USHRT_MAX);
}
template<> int Eluna::CHECKVAL<int>(int narg)
{
return CheckIntegerRange(L, narg, INT_MIN, INT_MAX);
}
template<> unsigned int Eluna::CHECKVAL<unsigned int>(int narg)
{
return CheckUnsignedRange(L, narg, UINT_MAX);
}
template<> const char* Eluna::CHECKVAL<const char*>(int narg)
{
return luaL_checkstring(L, narg);
}
template<> std::string Eluna::CHECKVAL<std::string>(int narg)
{
return luaL_checkstring(L, narg);
}
template<> long long Eluna::CHECKVAL<long long>(int narg)
{
if (lua_isnumber(L, narg))
return static_cast<long long>(CHECKVAL<double>(narg));
return *(Eluna::CHECKOBJ<long long>(narg, true));
}
template<> unsigned long long Eluna::CHECKVAL<unsigned long long>(int narg)
{
if (lua_isnumber(L, narg))
return static_cast<unsigned long long>(CHECKVAL<uint32>(narg));
return *(Eluna::CHECKOBJ<unsigned long long>(narg, true));
}
template<> long Eluna::CHECKVAL<long>(int narg)
{
return static_cast<long>(CHECKVAL<long long>(narg));
}
template<> unsigned long Eluna::CHECKVAL<unsigned long>(int narg)
{
return static_cast<unsigned long>(CHECKVAL<unsigned long long>(narg));
}
template<> ObjectGuid Eluna::CHECKVAL<ObjectGuid>(int narg)
{
ObjectGuid* guid = CHECKOBJ<ObjectGuid>(narg, true);
return guid ? *guid : ObjectGuid();
}
template<> Object* Eluna::CHECKOBJ<Object>(int narg, bool error)
{
Object* obj = CHECKOBJ<WorldObject>(narg, false);
if (!obj)
obj = CHECKOBJ<Item>(narg, false);
if (!obj)
obj = ElunaTemplate<Object>::Check(this, narg, error);
return obj;
}
template<> WorldObject* Eluna::CHECKOBJ<WorldObject>(int narg, bool error)
{
WorldObject* obj = CHECKOBJ<Unit>(narg, false);
if (!obj)
obj = CHECKOBJ<GameObject>(narg, false);
if (!obj)
obj = CHECKOBJ<Corpse>(narg, false);
if (!obj)
obj = ElunaTemplate<WorldObject>::Check(this, narg, error);
return obj;
}
template<> Unit* Eluna::CHECKOBJ<Unit>(int narg, bool error)
{
Unit* obj = CHECKOBJ<Player>(narg, false);
if (!obj)
obj = CHECKOBJ<Creature>(narg, false);
if (!obj)
obj = ElunaTemplate<Unit>::Check(this, narg, error);
return obj;
}
template<> ElunaObject* Eluna::CHECKOBJ<ElunaObject>(int narg, bool error)
{
return CHECKTYPE(narg, NULL, error);
}
ElunaObject* Eluna::CHECKTYPE(int narg, const char* tname, bool error)
{
if (lua_islightuserdata(L, narg))
{
if (error)
luaL_argerror(L, narg, "bad argument : userdata expected, got lightuserdata");
return NULL;
}
ElunaObject* elunaObject = static_cast<ElunaObject*>(lua_touserdata(L, narg));
if (!elunaObject || (tname && elunaObject->GetTypeName() != tname))
{
if (error)
{
char buff[256];
snprintf(buff, 256, "bad argument : %s expected, got %s", tname ? tname : "ElunaObject", elunaObject ? elunaObject->GetTypeName() : luaL_typename(L, narg));
luaL_argerror(L, narg, buff);
}
return NULL;
}
return elunaObject;
}
template<typename K>
static int cancelBinding(lua_State* L)
{
Eluna* E = Eluna::GetEluna(L);
uint64 bindingID = E->CHECKVAL<uint64>(lua_upvalueindex(1));
BindingMap<K>* bindings = (BindingMap<K>*)lua_touserdata(L, lua_upvalueindex(2));
ASSERT(bindings != NULL);
bindings->Remove(bindingID);
return 0;
}
template<typename K>
static void createCancelCallback(Eluna* e, uint64 bindingID, BindingMap<K>* bindings)
{
e->Push(bindingID);
lua_pushlightuserdata(e->L, bindings);
// Stack: bindingID, bindings
lua_pushcclosure(e->L, &cancelBinding<K>, 2);
// Stack: cancel_callback
}
// Saves the function reference ID given to the register type's store for given entry under the given event
int Eluna::Register(uint8 regtype, uint32 entry, ObjectGuid guid, uint32 instanceId, uint32 event_id, int functionRef, uint32 shots)
{
uint64 bindingID;
switch (regtype)
{
case Hooks::REGTYPE_SERVER:
if (event_id < Hooks::SERVER_EVENT_COUNT)
{
auto key = EventKey<Hooks::ServerEvents>((Hooks::ServerEvents)event_id);
bindingID = ServerEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, ServerEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_PLAYER:
if (event_id < Hooks::PLAYER_EVENT_COUNT)
{
auto key = EventKey<Hooks::PlayerEvents>((Hooks::PlayerEvents)event_id);
bindingID = PlayerEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, PlayerEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_GUILD:
if (event_id < Hooks::GUILD_EVENT_COUNT)
{
auto key = EventKey<Hooks::GuildEvents>((Hooks::GuildEvents)event_id);
bindingID = GuildEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, GuildEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_GROUP:
if (event_id < Hooks::GROUP_EVENT_COUNT)
{
auto key = EventKey<Hooks::GroupEvents>((Hooks::GroupEvents)event_id);
bindingID = GroupEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, GroupEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_VEHICLE:
if (event_id < Hooks::VEHICLE_EVENT_COUNT)
{
auto key = EventKey<Hooks::VehicleEvents>((Hooks::VehicleEvents)event_id);
bindingID = VehicleEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, VehicleEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_BG:
if (event_id < Hooks::BG_EVENT_COUNT)
{
auto key = EventKey<Hooks::BGEvents>((Hooks::BGEvents)event_id);
bindingID = BGEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, BGEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_PACKET:
if (event_id < Hooks::PACKET_EVENT_COUNT)
{
if (entry >= NUM_MSG_TYPES)
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a creature with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::PacketEvents>((Hooks::PacketEvents)event_id, entry);
bindingID = PacketEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, PacketEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_CREATURE:
if (event_id < Hooks::CREATURE_EVENT_COUNT)
{
if (entry != 0)
{
if (!eObjectMgr->GetCreatureTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a creature with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::CreatureEvents>((Hooks::CreatureEvents)event_id, entry);
bindingID = CreatureEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, CreatureEventBindings);
}
else
{
if (guid.IsEmpty())
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "guid was 0!");
return 0; // Stack: (empty)
}
auto key = UniqueObjectKey<Hooks::CreatureEvents>((Hooks::CreatureEvents)event_id, guid, instanceId);
bindingID = CreatureUniqueBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, CreatureUniqueBindings);
}
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_CREATURE_GOSSIP:
if (event_id < Hooks::GOSSIP_EVENT_COUNT)
{
if (!eObjectMgr->GetCreatureTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a creature with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::GossipEvents>((Hooks::GossipEvents)event_id, entry);
bindingID = CreatureGossipBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, CreatureGossipBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_GAMEOBJECT:
if (event_id < Hooks::GAMEOBJECT_EVENT_COUNT)
{
if (!eObjectMgr->GetGameObjectTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a gameobject with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::GameObjectEvents>((Hooks::GameObjectEvents)event_id, entry);
bindingID = GameObjectEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, GameObjectEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_GAMEOBJECT_GOSSIP:
if (event_id < Hooks::GOSSIP_EVENT_COUNT)
{
if (!eObjectMgr->GetGameObjectTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a gameobject with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::GossipEvents>((Hooks::GossipEvents)event_id, entry);
bindingID = GameObjectGossipBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, GameObjectGossipBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_SPELL:
if (event_id < Hooks::SPELL_EVENT_COUNT)
{
auto key = EntryKey<Hooks::SpellEvents>((Hooks::SpellEvents)event_id, entry);
bindingID = SpellEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, SpellEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_ITEM:
if (event_id < Hooks::ITEM_EVENT_COUNT)
{
if (!eObjectMgr->GetItemTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a item with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::ItemEvents>((Hooks::ItemEvents)event_id, entry);
bindingID = ItemEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, ItemEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_ITEM_GOSSIP:
if (event_id < Hooks::GOSSIP_EVENT_COUNT)
{
if (!eObjectMgr->GetItemTemplate(entry))
{
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
luaL_error(L, "Couldn't find a item with (ID: %d)!", entry);
return 0; // Stack: (empty)
}
auto key = EntryKey<Hooks::GossipEvents>((Hooks::GossipEvents)event_id, entry);
bindingID = ItemGossipBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, ItemGossipBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_PLAYER_GOSSIP:
if (event_id < Hooks::GOSSIP_EVENT_COUNT)
{
auto key = EntryKey<Hooks::GossipEvents>((Hooks::GossipEvents)event_id, entry);
bindingID = PlayerGossipBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, PlayerGossipBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_MAP:
if (event_id < Hooks::INSTANCE_EVENT_COUNT)
{
auto key = EntryKey<Hooks::InstanceEvents>((Hooks::InstanceEvents)event_id, entry);
bindingID = MapEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, MapEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_INSTANCE:
if (event_id < Hooks::INSTANCE_EVENT_COUNT)
{
auto key = EntryKey<Hooks::InstanceEvents>((Hooks::InstanceEvents)event_id, entry);
bindingID = InstanceEventBindings->Insert(key, functionRef, shots);
createCancelCallback(this, bindingID, InstanceEventBindings);
return 1; // Stack: callback
}
break;
}
luaL_unref(L, LUA_REGISTRYINDEX, functionRef);
std::ostringstream oss;
oss << "regtype " << static_cast<uint32>(regtype) << ", event " << event_id << ", entry " << entry << ", guid " <<
#if defined ELUNA_TRINITY
guid.ToHexString()
#else
guid.GetRawValue()
#endif
<< ", instance " << instanceId;
luaL_error(L, "Unknown event type (%s)", oss.str().c_str());
return 0;
}
void Eluna::UpdateEluna(uint32 diff)
{
if (reload && sElunaLoader->GetCacheState() == SCRIPT_CACHE_READY)
#if defined ELUNA_TRINITY
if(!GetQueryProcessor().HasPendingCallbacks())
#endif
_ReloadEluna();
eventMgr->globalProcessor->Update(diff);
#if defined ELUNA_TRINITY
GetQueryProcessor().ProcessReadyCallbacks();
#endif
}
/*
* Cleans up the stack, effectively undoing all Push calls and the Setup call.
*/