12#define TC_LOG_INFO SF_LOG_INFO
13#define TC_LOG_ERROR SF_LOG_ERROR
15#if PLATFORM == PLATFORM_UNIX
20template<
typename T>
const char*
GetTName() {
return NULL; }
46 TC_LOG_INFO(
"server.loading",
"[Eluna]: Restarting Lua Engine");
51 sEluna->m_EventMgr.RemoveEvents();
54 for (std::map<
int, std::vector<int> >::iterator itr =
sEluna->ServerEventBindings.begin(); itr !=
sEluna->ServerEventBindings.end(); ++itr)
56 for (std::vector<int>::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
57 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, (*it));
61 for (std::map<
int, std::vector<int> >::iterator itr =
sEluna->PlayerEventBindings.begin(); itr !=
sEluna->PlayerEventBindings.end(); ++itr)
63 for (std::vector<int>::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
64 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, (*it));
68 for (std::map<
int, std::vector<int> >::iterator itr =
sEluna->VehicleEventBindings.begin(); itr !=
sEluna->VehicleEventBindings.end(); ++itr)
70 for (std::vector<int>::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
71 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, (*it));
75 for (std::map<
int, std::vector<int> >::iterator itr =
sEluna->GuildEventBindings.begin(); itr !=
sEluna->GuildEventBindings.end(); ++itr)
77 for (std::vector<int>::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
78 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, (*it));
82 for (std::map<
int, std::vector<int> >::iterator itr =
sEluna->GroupEventBindings.begin(); itr !=
sEluna->GroupEventBindings.end(); ++itr)
84 for (std::vector<int>::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
85 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, (*it));
88 sEluna->CreatureEventBindings->Clear();
89 sEluna->CreatureGossipBindings->Clear();
90 sEluna->GameObjectEventBindings->Clear();
91 sEluna->GameObjectGossipBindings->Clear();
92 sEluna->ItemEventBindings->Clear();
93 sEluna->ItemGossipBindings->Clear();
94 sEluna->playerGossipBindings->Clear();
102 sEluna->L = luaL_newstate();
104 TC_LOG_INFO(
"server.loading",
"[Eluna]: Lua Engine loaded.");
107 std::string lua_folderpath =
sConfigMgr->GetStringDefault(
"Eluna.ScriptPath",
"lua_scripts");
110 sEluna->LoadDirectory(lua_folderpath.c_str(), &loadedScripts);
117 for (std::set<std::string>::const_iterator itr = loadedScripts.begin(); itr != loadedScripts.end(); ++itr)
119 strcpy(filename, itr->c_str());
120 if (luaL_loadfile(
sEluna->L, filename) != 0)
122 TC_LOG_INFO(
"server.loading",
"[Eluna]: Error loading file `%s`.", itr->c_str());
127 int err = lua_pcall(
sEluna->L, 0, 0, 0);
128 if (err != 0 && err == LUA_ERRRUN)
130 TC_LOG_INFO(
"server.loading",
"[Eluna]: Error loading file `%s`.", itr->c_str());
138 TC_LOG_INFO(
"server.loading",
"[Eluna]: Loaded %u Lua scripts..", count);
153 WIN32_FIND_DATA FindData;
154 memset(&FindData, 0,
sizeof(FindData));
155 char SearchName[MAX_PATH];
157 strcpy(SearchName, Dirname);
158 strcat(SearchName,
"\\*.*");
160 hFile = FindFirstFile(SearchName, &FindData);
161 if (hFile == INVALID_HANDLE_VALUE)
164 CreateDirectory(
"lua_scripts", NULL);
168 FindNextFile(hFile, &FindData);
169 while (FindNextFile(hFile, &FindData))
171 if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
173 strcpy(SearchName, Dirname);
174 strcat(SearchName,
"\\");
175 strcat(SearchName, FindData.cFileName);
180 std::string fname = Dirname;
182 fname += FindData.cFileName;
183 size_t len = strlen(fname.c_str());
188 ext[i++] = fname[--len];
189 if (fname[len] ==
'.')
193 if (!_stricmp(ext,
"aul."))
202 const char* dir = strrchr(Dirname,
'/');
203 if (strcmp(Dirname,
"..") == 0 || strcmp(Dirname,
".") == 0)
206 if (dir && (strcmp(dir,
"/..") == 0 || strcmp(dir,
"/.") == 0 || strcmp(dir,
"/.svn") == 0))
209 struct dirent** list;
210 int fileCount = scandir(Dirname, &list, 0, 0);
212 if (fileCount <= 0 || !list)
215 struct stat attributes;
220 sprintf(_path,
"%s/%s", Dirname, list[fileCount]->d_name);
221 if (stat(_path, &attributes) == -1)
224 TC_LOG_ERROR(
"server.loading",
"[Eluna]: Error opening `%s`", _path);
229 if (!error && S_ISDIR(attributes.st_mode))
233 char* ext = strrchr(list[fileCount]->d_name,
'.');
234 if (ext && !strcmp(ext,
".lua"))
237 free(list[fileCount]);
249 TC_LOG_ERROR(
"server.loading",
"[Eluna]: LuaEngine is Disabled. (If you want to use it please enable in cmake)");
255 const char* msg = lua_tostring(
L, -1);
259 printf(
"\t%s\n",msg);
260 msg = lua_tostring(
L, -1);
267 lua_rawgeti(
L, LUA_REGISTRYINDEX, (fReference));
273 int top = lua_gettop(
L);
275 if (lua_type(
L, top - params) == LUA_TFUNCTION)
277 if (lua_pcall(
L, params, res, 0))
288 for (
int i = top; i >= (top - params); i--)
290 if (!lua_isnone(
L, i))
300 for (
int i = res; i > 0; i--)
302 if (!lua_isnone(
L, res))
315 std::ostringstream ss;
316 ss <<
"0x" << std::hex << l;
322 std::ostringstream ss;
324 ss <<
"-0x" << std::hex << -l;
326 ss <<
"0x" << std::hex << l;
332 lua_pushunsigned(
L, u);
337 lua_pushinteger(
L, i);
342 lua_pushnumber(
L, d);
347 lua_pushnumber(
L, f);
352 lua_pushboolean(
L, b);
357 lua_pushstring(
L, str.c_str());
362 lua_pushstring(
L, str);
437 const char* c_str = luaL_optstring(
L, narg,
"0x0");
438 return strtoul(c_str, NULL, 0);
443 const char* c_str = luaL_optstring(
L, narg,
"0x0");
444 return strtol(c_str, NULL, 0);
502 luaL_error(
L,
"Couldn't find a creature with (ID: %d)!",
id);
506 sEluna->CreatureEventBindings->Insert(
id, evt, functionRef);
516 luaL_error(
L,
"Couldn't find a creature with (ID: %d)!",
id);
520 sEluna->CreatureGossipBindings->Insert(
id, evt, functionRef);
530 luaL_error(
L,
"Couldn't find a gameobject with (ID: %d)!",
id);
534 sEluna->GameObjectEventBindings->Insert(
id, evt, functionRef);
544 luaL_error(
L,
"Couldn't find a gameobject with (ID: %d)!",
id);
548 sEluna->GameObjectGossipBindings->Insert(
id, evt, functionRef);
558 luaL_error(
L,
"Couldn't find a item with (ID: %d)!",
id);
562 sEluna->ItemEventBindings->Insert(
id, evt, functionRef);
572 luaL_error(
L,
"Couldn't find a item with (ID: %d)!",
id);
576 sEluna->ItemGossipBindings->Insert(
id, evt, functionRef);
584 sEluna->playerGossipBindings->Insert(
id, evt, functionRef);
590 luaL_error(
L,
"Unknown register type (regtype %d, id %d, event %d)", regtype,
id, evt);
593 luaL_error(
L,
"Unknown event type (regtype %d, id %d, event %d)", regtype,
id, evt);
598 for (ElunaEntryMap::iterator itr =
Bindings.begin(); itr !=
Bindings.end(); ++itr)
600 for (ElunaBindingMap::const_iterator it = itr->second.begin(); it != itr->second.end(); ++it)
601 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, it->second);
611 luaL_error(
sEluna->L,
"A function is already registered for entry (%d) event (%d)", entryId, eventId);
612 luaL_unref(
sEluna->L, LUA_REGISTRYINDEX, funcRef);
615 Bindings[entryId][eventId] = funcRef;
623 sEluna->m_EventMgr.LuaEvents[_events].insert(
this);
631 EventMgr::EventMap::const_iterator it =
sEluna->m_EventMgr.LuaEvents.find(
events);
632 if (it !=
sEluna->m_EventMgr.LuaEvents.end())
649 sEluna->ExecuteCall(4, 0);
664 std::vector<uint32> nodes;
666 nodes[0] = path[0].index;
667 nodes[1] = path[path.
size() - 1].index;
TaxiPathNodesByPath sTaxiPathNodesByPath
Path< TaxiPathNodePtr, TaxiPathNodeEntry const > TaxiPathNodeList
@ REGTYPE_GAMEOBJECT_GOSSIP
@ REGTYPE_CREATURE_GOSSIP
const char * GetTName< Player >()
const char * GetTName< Group >()
const char * GetTName< Object >()
const char * GetTName< GameObject >()
void StartEluna(bool restart)
const char * GetTName< WorldPacket >()
const char * GetTName< Weather >()
const char * GetTName< Map >()
const char * GetTName< Creature >()
const char * GetTName< Aura >()
const char * GetTName< Item >()
const char * GetTName< Spell >()
const char * GetTName< Quest >()
const char * GetTName< QueryResult >()
const char * GetTName< Unit >()
const char * GetTName< Corpse >()
const char * GetTName< WorldObject >()
const char * GetTName< AuctionHouseObject >()
const char * GetTName< Guild >()
const char * GetTName< Vehicle >()
void StartEluna(bool restart)
std::set< std::string > LoadedScripts
void RegisterFunctions(lua_State *L)
int64 CHECK_LONG(lua_State *L, int narg)
Quest * CHECK_QUEST(lua_State *L, int narg)
Corpse * CHECK_CORPSE(lua_State *L, int narg)
Spell * CHECK_SPELL(lua_State *L, int narg)
Item * CHECK_ITEM(lua_State *L, int narg)
void BeginCall(int fReference)
std::map< int, std::vector< int > > PlayerEventBindings
std::map< int, std::vector< int > > GuildEventBindings
Object * CHECK_OBJECT(lua_State *L, int narg)
std::map< int, std::vector< int > > VehicleEventBindings
void Register(uint8 reg, uint32 id, uint32 evt, int func)
std::map< int, std::vector< int > > ServerEventBindings
WorldObject * CHECK_WORLDOBJECT(lua_State *L, int narg)
static void report(lua_State *)
std::map< int, std::vector< int > > GroupEventBindings
Vehicle * CHECK_VEHICLE(lua_State *L, int narg)
Creature * CHECK_CREATURE(lua_State *L, int narg)
static Eluna * instance()
void LoadDirectory(const char *directory, LoadedScripts *scr)
uint64 CHECK_ULONG(lua_State *L, int narg)
GameObject * CHECK_GAMEOBJECT(lua_State *L, int narg)
Player * CHECK_PLAYER(lua_State *L, int narg)
WorldPacket * CHECK_PACKET(lua_State *L, int narg)
bool ExecuteCall(uint8 params, uint8 res)
Unit * CHECK_UNIT(lua_State *L, int narg)
static T * check(lua_State *L, int narg)
static void StartTaxi(Player *player, uint32 pathid)
static uint32 AddPath(std::list< TaxiPathNodeEntry > nodes, uint32 mountA, uint32 mountH, uint32 price=0, uint32 pathId=0)
GameObject * ToGameObject()
bool ActivateTaxiPathTo(std::vector< uint32 > const &nodes, Creature *npc=NULL, uint32 spellid=0)
void Insert(uint32 entryId, uint32 eventId, int funcRef)
LuaEvent(EventProcessor *_events, int _funcRef, uint32 _delay, uint32 _calls, Object *_obj)
bool Execute(uint64 time, uint32 diff)