From 809a0ac2bc259ccda11ba046be39c917caa37785 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Sat, 20 Dec 2014 18:15:59 +0200 Subject: [PATCH 1/5] Fixes #130 --- LuaEngine.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/LuaEngine.cpp b/LuaEngine.cpp index ad5e26d..fbbbcfc 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -57,7 +57,9 @@ void Eluna::Initialize() ELUNA_LOG_INFO("[Eluna]: Searching scripts from `%s`", lua_folderpath.c_str()); lua_requirepath = ""; GetScripts(lua_folderpath); - lua_requirepath.erase(lua_requirepath.end() - 1); + // Erase last ; + if (!lua_requirepath.empty()) + lua_requirepath.erase(lua_requirepath.end() - 1); ELUNA_LOG_DEBUG("[Eluna]: Loaded %u scripts in %u ms", uint32(lua_scripts.size() + lua_extensions.size()), ElunaUtil::GetTimeDiff(oldMSTime)); From 631e220b314909a2e8860109dccab5be4a28e9d2 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Fri, 19 Dec 2014 12:39:37 +0200 Subject: [PATCH 2/5] Eluna changes * Attempt fix VS 2010 pow error * Tweak the doc parser a little to work with variable arugments ... and show enum names and similar. * Change push and check to work with more types like size and time (fix osx) * Add errors to using not implemented operations on eluna objects * Change some SQL comments to work with documentation better * Add functions to create uint64 and int64 values (may need rename) * Change doc generation documentation a little to make it clearer how proto is used --- CorpseMethods.h | 2 +- ElunaTemplate.h | 28 ++++++------ GlobalMethods.h | 94 ++++++++++++++++++++++++++++++--------- LuaEngine.cpp | 92 ++++++++++++++++++-------------------- LuaEngine.h | 12 ++--- LuaFunctions.cpp | 62 +++++++++++++++----------- docs/DOC_GEN.md | 5 ++- docs/ElunaDoc/__main__.py | 3 ++ docs/ElunaDoc/parser.py | 23 +++++++--- 9 files changed, 200 insertions(+), 121 deletions(-) diff --git a/CorpseMethods.h b/CorpseMethods.h index 3282295..fb85184 100644 --- a/CorpseMethods.h +++ b/CorpseMethods.h @@ -31,7 +31,7 @@ namespace LuaCorpse */ int GetGhostTime(Eluna* /*E*/, lua_State* L, Corpse* corpse) { - Eluna::Push(L, uint32(corpse->GetGhostTime())); + Eluna::Push(L, corpse->GetGhostTime()); return 1; } diff --git a/ElunaTemplate.h b/ElunaTemplate.h index 98fa444..e213949 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -425,19 +425,21 @@ public: return 1; } - static int Add(lua_State* L) { return 0; } - static int Substract(lua_State* L) { return 0; } - static int Multiply(lua_State* L) { return 0; } - static int Divide(lua_State* L) { return 0; } - static int Mod(lua_State* L) { return 0; } - static int Pow(lua_State* L) { return 0; } - static int UnaryMinus(lua_State* L) { return 0; } - static int Concat(lua_State* L) { return 0; } - static int Length(lua_State* L) { return 0; } - static int Equal(lua_State* L) { return 0; } - static int Less(lua_State* L) { return 0; } - static int LessOrEqual(lua_State* L) { return 0; } - static int Call(lua_State* L) { return 0; } + static int ArithmeticError(lua_State* L) { return luaL_error(L, "attempt to perform arithmetic on a %s value", tname); } + static int CompareError(lua_State* L) { return luaL_error(L, "attempt to compare %s", tname); } + static int Add(lua_State* L) { return ArithmeticError(L); } + static int Substract(lua_State* L) { return ArithmeticError(L); } + static int Multiply(lua_State* L) { return ArithmeticError(L); } + static int Divide(lua_State* L) { return ArithmeticError(L); } + static int Mod(lua_State* L) { return ArithmeticError(L); } + static int Pow(lua_State* L) { return ArithmeticError(L); } + static int UnaryMinus(lua_State* L) { return ArithmeticError(L); } + static int Concat(lua_State* L) { return luaL_error(L, "attempt to concatenate a %s value", tname); } + static int Length(lua_State* L) { return luaL_error(L, "attempt to get length of a %s value", tname); } + static int Equal(lua_State* L) { Eluna::Push(L, Eluna::CHECKOBJ(L, 1) == Eluna::CHECKOBJ(L, 2)); return 1; } + static int Less(lua_State* L) { return CompareError(L); } + static int LessOrEqual(lua_State* L) { return CompareError(L); } + static int Call(lua_State* L) { return luaL_error(L, "attempt to call a %s value", tname); } }; // //template const char* ElunaTemplate::tname; diff --git a/GlobalMethods.h b/GlobalMethods.h index b3b21e8..4a0f788 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -113,11 +113,7 @@ namespace LuaGlobalFunctions */ int GetGameTime(Eluna* /*E*/, lua_State* L) { - time_t time = eWorld->GetGameTime(); - if (time < 0) - Eluna::Push(L, int32(time)); - else - Eluna::Push(L, uint32(time)); + Eluna::Push(L, eWorld->GetGameTime()); return 1; } @@ -1182,10 +1178,10 @@ namespace LuaGlobalFunctions } /** - * Executes world database sql [Query] instantly and returns QueryResult object + * Executes an sql to your world database instantly and returns [ElunaQuery] * - * @param string query : sql [Query] to run - * @return QueryResult result + * @param string sql : sql to run + * @return [ElunaQuery] result */ int WorldDBQuery(Eluna* /*E*/, lua_State* L) { @@ -1208,9 +1204,9 @@ namespace LuaGlobalFunctions } /** - * Executes a sql [Query] (not instantly) to your world database + * Executes an sql to your character database. The SQL is not ran instantly. * - * @param string query : sql [Query] to execute + * @param string sql : sql [ElunaQuery] to execute */ int WorldDBExecute(Eluna* /*E*/, lua_State* L) { @@ -1220,10 +1216,10 @@ namespace LuaGlobalFunctions } /** - * Executes character database sql [Query] instantly and returns QueryResult object + * Executes an sql to your character database instantly and returns [ElunaQuery] * - * @param string query : sql [Query] to run - * @return [Query] result + * @param string sql : sql to run + * @return [ElunaQuery] result */ int CharDBQuery(Eluna* /*E*/, lua_State* L) { @@ -1246,9 +1242,9 @@ namespace LuaGlobalFunctions } /** - * Executes a [Query] (not instantly) to your character database + * Executes an sql to your character database. The SQL is not ran instantly. * - * @param string query : sql [Query] to execute + * @param string sql : sql to run */ int CharDBExecute(Eluna* /*E*/, lua_State* L) { @@ -1258,10 +1254,10 @@ namespace LuaGlobalFunctions } /** - * Executes auth database sql [Query] instantly and returns QueryResult object + * Executes an sql to your auth database instantly and returns [ElunaQuery] * - * @param string query : sql [Query] to run - * @return [Query] result + * @param string sql : sql to run + * @return [ElunaQuery] result */ int AuthDBQuery(Eluna* /*E*/, lua_State* L) { @@ -1284,9 +1280,9 @@ namespace LuaGlobalFunctions } /** - * Executes a [Query] (not instantly ) to your auth database + * Executes an sql to your auth database. The SQL is not ran instantly. * - * @param string query : sql [Query] to execute + * @param string sql : sql to run */ int AuthDBExecute(Eluna* /*E*/, lua_State* L) { @@ -2493,5 +2489,63 @@ namespace LuaGlobalFunctions ELUNA_LOG_DEBUG("%s", GetStackAsString(L).c_str()); return 0; } + + /** + * Returns an object represeting long long. + * The value by default is 0, but can be initialized to a value by passing a number or long long as a string. + * + * @proto value = () + * @proto value = (number) + * @proto value = (longlong) + * @proto value = (longlongstr) + * @param int32 number : regular lua number + * @param int64 longlong : a long long object + * @param string longlongstr : a long long as a string + * @return int64 value + */ + int CreateLongLong(Eluna* /*E*/, lua_State* L) + { + long long init = 0; + if (lua_isstring(L, 1)) + { + std::string str = Eluna::CHECKVAL(L, 1); + if (sscanf(str.c_str(), SI64FMTD, &init) != 1) + return luaL_argerror(L, 1, "long long (as string) could not be converted"); + } + else if (!lua_isnoneornil(L, 1)) + init = Eluna::CHECKVAL(L, 1); + + Eluna::Push(L, init); + return 1; + } + + /** + * Returns an object represeting unsigned long long. + * The value by default is 0, but can be initialized to a value by passing a number or unsigned long long as a string. + * + * @proto value = () + * @proto value = (number) + * @proto value = (ulonglong) + * @proto value = (ulonglongstr) + * @param uint32 number : regular lua number + * @param uint64 ulonglong : an unsigned long long object + * @param string ulonglongstr : an unsigned long long as a string + * @return uint64 value + */ + int CreateULongLong(Eluna* /*E*/, lua_State* L) + { + unsigned long long init = 0; + if (lua_isstring(L, 1)) + { + std::string str = Eluna::CHECKVAL(L, 1); + if (sscanf(str.c_str(), UI64FMTD, &init) != 1) + return luaL_argerror(L, 1, "unsigned long long (as string) could not be converted"); + } + else if (!lua_isnoneornil(L, 1)) + init = Eluna::CHECKVAL(L, 1); + + Eluna::Push(L, init); + return 1; + } } #endif diff --git a/LuaEngine.cpp b/LuaEngine.cpp index fbbbcfc..20845f6 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -450,30 +450,30 @@ void Eluna::Push(lua_State* L) { lua_pushnil(L); } -void Eluna::Push(lua_State* L, const uint64 l) +void Eluna::Push(lua_State* L, const long long l) { - ElunaTemplate::Push(L, new uint64(l)); + ElunaTemplate::Push(L, new long long(l)); } -void Eluna::Push(lua_State* L, const int64 l) +void Eluna::Push(lua_State* L, const unsigned long long l) { - ElunaTemplate::Push(L, new int64(l)); + ElunaTemplate::Push(L, new unsigned long long(l)); } -//void Eluna::Push(lua_State* L, const time_t l) -//{ -// ElunaTemplate::Push(L, new uint64(l)); -//} -//void Eluna::Push(lua_State* L, const size_t l) -//{ -// ElunaTemplate::Push(L, new int64(l)); -//} -void Eluna::Push(lua_State* L, const uint32 u) +void Eluna::Push(lua_State* L, const long l) { - lua_pushunsigned(L, u); + Push(L, static_cast(l)); } -void Eluna::Push(lua_State* L, const int32 i) +void Eluna::Push(lua_State* L, const unsigned long l) +{ + Push(L, static_cast(l)); +} +void Eluna::Push(lua_State* L, const int i) { lua_pushinteger(L, i); } +void Eluna::Push(lua_State* L, const unsigned int u) +{ + lua_pushunsigned(L, u); +} void Eluna::Push(lua_State* L, const double d) { lua_pushnumber(L, d); @@ -486,7 +486,7 @@ void Eluna::Push(lua_State* L, const bool b) { lua_pushboolean(L, b); } -void Eluna::Push(lua_State* L, const std::string str) +void Eluna::Push(lua_State* L, const std::string& str) { lua_pushstring(L, str.c_str()); } @@ -572,29 +572,29 @@ void Eluna::Push(lua_State* L, Object const* obj) } } -static int32 CheckIntegerRange(lua_State* L, int narg, int32 min, int32 max) +static int CheckIntegerRange(lua_State* L, int narg, int min, int max) { - int64 value = static_cast(luaL_checknumber(L, narg)); + double value = luaL_checknumber(L, narg); char error_buffer[64]; if (value > max) { - snprintf(error_buffer, 64, "value must be less than or equal to %d", max); + snprintf(error_buffer, 64, "value must be less than or equal to %i", max); return luaL_argerror(L, narg, error_buffer); } if (value < min) { - snprintf(error_buffer, 64, "value must be greater than or equal to %d", min); + snprintf(error_buffer, 64, "value must be greater than or equal to %i", min); return luaL_argerror(L, narg, error_buffer); } - return static_cast(value); + return static_cast(value); } -static uint32 CheckUnsignedRange(lua_State* L, int narg, uint32 max) +static unsigned int CheckUnsignedRange(lua_State* L, int narg, unsigned int max) { - int64 value = static_cast(luaL_checknumber(L, narg)); + double value = luaL_checknumber(L, narg); char error_buffer[64]; if (value < 0) @@ -606,7 +606,7 @@ static uint32 CheckUnsignedRange(lua_State* L, int narg, uint32 max) return luaL_argerror(L, narg, error_buffer); } - return static_cast(value); + return static_cast(value); } template<> bool Eluna::CHECKVAL(lua_State* L, int narg) @@ -621,27 +621,27 @@ template<> double Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checknumber(L, narg); } -template<> int8 Eluna::CHECKVAL(lua_State* L, int narg) +template<> signed char Eluna::CHECKVAL(lua_State* L, int narg) { return CheckIntegerRange(L, narg, SCHAR_MIN, SCHAR_MAX); } -template<> uint8 Eluna::CHECKVAL(lua_State* L, int narg) +template<> unsigned char Eluna::CHECKVAL(lua_State* L, int narg) { return CheckUnsignedRange(L, narg, UCHAR_MAX); } -template<> int16 Eluna::CHECKVAL(lua_State* L, int narg) +template<> short Eluna::CHECKVAL(lua_State* L, int narg) { return CheckIntegerRange(L, narg, SHRT_MIN, SHRT_MAX); } -template<> uint16 Eluna::CHECKVAL(lua_State* L, int narg) +template<> unsigned short Eluna::CHECKVAL(lua_State* L, int narg) { return CheckUnsignedRange(L, narg, USHRT_MAX); } -template<> int32 Eluna::CHECKVAL(lua_State* L, int narg) +template<> int Eluna::CHECKVAL(lua_State* L, int narg) { return CheckIntegerRange(L, narg, INT_MIN, INT_MAX); } -template<> uint32 Eluna::CHECKVAL(lua_State* L, int narg) +template<> unsigned int Eluna::CHECKVAL(lua_State* L, int narg) { return CheckUnsignedRange(L, narg, UINT_MAX); } @@ -653,30 +653,26 @@ template<> std::string Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkstring(L, narg); } -template<> int64 Eluna::CHECKVAL(lua_State* L, int narg) +template<> long long Eluna::CHECKVAL(lua_State* L, int narg) { if (lua_isnumber(L, narg)) - return static_cast(CHECKVAL(L, narg)); - return *(Eluna::CHECKOBJ(L, narg, true)); + return static_cast(CHECKVAL(L, narg)); + return *(Eluna::CHECKOBJ(L, narg, true)); } -template<> uint64 Eluna::CHECKVAL(lua_State* L, int narg) +template<> unsigned long long Eluna::CHECKVAL(lua_State* L, int narg) { if (lua_isnumber(L, narg)) - return static_cast(CHECKVAL(L, narg)); - return *(Eluna::CHECKOBJ(L, narg, true)); + return static_cast(CHECKVAL(L, narg)); + return *(Eluna::CHECKOBJ(L, narg, true)); +} +template<> long Eluna::CHECKVAL(lua_State* L, int narg) +{ + return static_cast(CHECKVAL(L, narg)); +} +template<> unsigned long Eluna::CHECKVAL(lua_State* L, int narg) +{ + return static_cast(CHECKVAL(L, narg)); } -//template<> time_t Eluna::CHECKVAL(lua_State* L, int narg) -//{ -// if (lua_isnumber(L, narg)) -// return static_cast(CHECKVAL(L, narg)); -// return static_cast(*(Eluna::CHECKOBJ(L, narg, true))); -//} -//template<> size_t Eluna::CHECKVAL(lua_State* L, int narg) -//{ -// if (lua_isnumber(L, narg)) -// return static_cast(CHECKVAL(L, narg)); -// return static_cast(*(Eluna::CHECKOBJ(L, narg, true))); -//} template<> Object* Eluna::CHECKOBJ(lua_State* L, int narg, bool error) { diff --git a/LuaEngine.h b/LuaEngine.h index 4cee8ba..b719260 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -160,15 +160,17 @@ public: // Pushes static void Push(lua_State* L); // nil - static void Push(lua_State* L, const uint64); - static void Push(lua_State* L, const int64); - static void Push(lua_State* L, const uint32); - static void Push(lua_State* L, const int32); + static void Push(lua_State* L, const long long); + static void Push(lua_State* L, const unsigned long long); + static void Push(lua_State* L, const long); + static void Push(lua_State* L, const unsigned long); + static void Push(lua_State* L, const int); + static void Push(lua_State* L, const unsigned int); static void Push(lua_State* L, const bool); static void Push(lua_State* L, const float); static void Push(lua_State* L, const double); + static void Push(lua_State* L, const std::string&); static void Push(lua_State* L, const char*); - static void Push(lua_State* L, const std::string); template static void Push(lua_State* L, T const* ptr) { ElunaTemplate::Push(L, ptr); diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index a889023..e2bd1d0 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -127,6 +127,8 @@ ElunaGlobal::ElunaRegister GlobalMethods[] = { "AddWeather", &LuaGlobalFunctions::AddWeather }, { "RemoveWeather", &LuaGlobalFunctions::RemoveWeather }, { "SendFineWeatherToPlayer", &LuaGlobalFunctions::SendFineWeatherToPlayer }, + { "CreateInt64", &LuaGlobalFunctions::CreateLongLong }, + { "CreateUint64", &LuaGlobalFunctions::CreateULongLong }, { NULL, NULL }, }; @@ -1266,38 +1268,46 @@ template<> int ElunaTemplate::CollectGarbage(lua_State* L) #endif // Template by Mud from http://stackoverflow.com/questions/4484437/lua-integer-type/4485511#4485511 -template<> int ElunaTemplate::Add(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) + Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Substract(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) - Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Multiply(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) * Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Divide(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) / Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Mod(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) % Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Pow(lua_State* L) { Eluna::Push(L, pow(Eluna::CHECKVAL(L, 1), Eluna::CHECKVAL(L, 2))); return 1; } -// template<> int ElunaTemplate::UnaryMinus(lua_State* L) { Eluna::Push(L, -Eluna::CHECKVAL(L, 1)); return 1; } -template<> int ElunaTemplate::Equal(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) == Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) < Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::ToString(lua_State* L) +template<> int ElunaTemplate::Add(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) + Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Substract(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) - Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Multiply(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) * Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Divide(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) / Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Mod(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) % Eluna::CHECKVAL(L, 2)); return 1; } +// template<> int ElunaTemplate::UnaryMinus(lua_State* L) { Eluna::Push(L, -Eluna::CHECKVAL(L, 1)); return 1; } +template<> int ElunaTemplate::Equal(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) == Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) < Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Pow(lua_State* L) { - uint64 l = Eluna::CHECKVAL(L, 1); + Eluna::Push(L, static_cast(std::powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); + return 1; +} +template<> int ElunaTemplate::ToString(lua_State* L) +{ + unsigned long long l = Eluna::CHECKVAL(L, 1); std::ostringstream ss; ss << l; Eluna::Push(L, ss.str()); return 1; } -template<> int ElunaTemplate::Add(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) + Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Substract(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) - Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Multiply(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) * Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Divide(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) / Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Mod(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) % Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Pow(lua_State* L) { Eluna::Push(L, pow(Eluna::CHECKVAL(L, 1), Eluna::CHECKVAL(L, 2))); return 1; } -template<> int ElunaTemplate::UnaryMinus(lua_State* L) { Eluna::Push(L, -Eluna::CHECKVAL(L, 1)); return 1; } -template<> int ElunaTemplate::Equal(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) == Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) < Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } -template<> int ElunaTemplate::ToString(lua_State* L) +template<> int ElunaTemplate::Add(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) + Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Substract(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) - Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Multiply(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) * Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Divide(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) / Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Mod(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) % Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::UnaryMinus(lua_State* L) { Eluna::Push(L, -Eluna::CHECKVAL(L, 1)); return 1; } +template<> int ElunaTemplate::Equal(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) == Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) < Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } +template<> int ElunaTemplate::Pow(lua_State* L) { - int64 l = Eluna::CHECKVAL(L, 1); + Eluna::Push(L, static_cast(std::powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); + return 1; +} +template<> int ElunaTemplate::ToString(lua_State* L) +{ + long long l = Eluna::CHECKVAL(L, 1); std::ostringstream ss; ss << l; Eluna::Push(L, ss.str()); @@ -1386,7 +1396,7 @@ void RegisterFunctions(Eluna* E) ElunaTemplate::Register(E, "ElunaQuery", true); ElunaTemplate::SetMethods(E, QueryMethods); - ElunaTemplate::Register(E, "uint64", true); + ElunaTemplate::Register(E, "long long", true); - ElunaTemplate::Register(E, "int64", true); + ElunaTemplate::Register(E, "unsigned long long", true); } diff --git a/docs/DOC_GEN.md b/docs/DOC_GEN.md index bfedec3..60bd022 100644 --- a/docs/DOC_GEN.md +++ b/docs/DOC_GEN.md @@ -43,7 +43,10 @@ This is a template for a function that takes in different parameters. When defin * * @proto returnValue = (object) * @proto returnValue = (x, y, z) - * @param Type paramName = defaultValue : parameter description + * @param [WorldObject] object = defaultValue : parameter description + * @param float x = defaultValue : parameter description + * @param float y = defaultValue : parameter description + * @param float z = defaultValue : parameter description * @return Type returnName : return value description */ ``` diff --git a/docs/ElunaDoc/__main__.py b/docs/ElunaDoc/__main__.py index 8434ac9..85cbdba 100644 --- a/docs/ElunaDoc/__main__.py +++ b/docs/ElunaDoc/__main__.py @@ -118,6 +118,7 @@ if __name__ == '__main__': 'string': 'http://www.lua.org/pil/2.4.html', 'table': 'http://www.lua.org/pil/2.5.html', 'function': 'http://www.lua.org/pil/2.6.html', + '...': 'http://www.lua.org/pil/5.2.html', } def data_type_parser(content): @@ -132,6 +133,8 @@ if __name__ == '__main__': url = '{}{}/index.html'.format(('../' * level), class_name) return '{}'.format(url, class_name) + return content[1:-1] + return link_parser, data_type_parser # Create the render function with the template path and parser maker. diff --git a/docs/ElunaDoc/parser.py b/docs/ElunaDoc/parser.py index 4db5032..a8308cf 100644 --- a/docs/ElunaDoc/parser.py +++ b/docs/ElunaDoc/parser.py @@ -18,6 +18,8 @@ class ParameterDoc(object): 'uint16': ('0', '65,535'), 'int32': ('-2,147,483,647', '2,147,483,647'), 'uint32': ('0', '4,294,967,295'), + 'int64': ('-9,223,372,036,854,775,808', '9,223,372,036,854,775,807'), + 'uint64': ('0', '18,446,744,073,709,551,615'), } @params(self=object, name=unicode, data_type=str, description=unicode, default_value=Nullable(unicode)) @@ -47,8 +49,8 @@ class ParameterDoc(object): elif self.data_type == 'bool': self.data_type = 'boolean' - elif self.data_type == 'uint64' or self.data_type == 'int64': - self.data_type = 'string' + elif self.data_type == 'int64' or self.data_type == 'uint64': + self.data_type = '[' + self.data_type + ']' class MethodDoc(object): @@ -112,7 +114,7 @@ class ClassParser(object): # An extra optional space (\s?) was thrown in to make it different from `class_body_regex`. param_regex = re.compile(r"""\s*\*\s@param\s # The @param tag starts with opt. whitespace followed by "* @param ". - ([\[\]\w]+)\s(\w+) # The data type, a space, and the name of the param. + ([^\s]+)\s(\w+) # The data type, a space, and the name of the param. (?:\s=\s(\w+))? # The default value: a = surrounded by spaces, followed by text. (?:\s:\s(.+))? # The description: a colon surrounded by spaces, followed by text. """, re.X) @@ -183,11 +185,18 @@ class ClassParser(object): if parameters != '': parameters = ' ' + parameters + ' ' - if self.returned: - return_values = ', '.join([param.name for param in self.returned]) - prototype = '{0} = {1}:{2}({3})'.format(return_values, self.class_name, self.method_name, parameters) + if self.class_name == 'Global': + if self.returned: + return_values = ', '.join([param.name for param in self.returned]) + prototype = '{0} = {1}({2})'.format(return_values, self.method_name, parameters) + else: + prototype = '{0}({1})'.format(self.method_name, parameters) else: - prototype = '{0}:{1}({2})'.format(self.class_name, self.method_name, parameters) + if self.returned: + return_values = ', '.join([param.name for param in self.returned]) + prototype = '{0} = {1}:{2}({3})'.format(return_values, self.class_name, self.method_name, parameters) + else: + prototype = '{0}:{1}({2})'.format(self.class_name, self.method_name, parameters) self.prototypes.append(prototype) else: From 78d18e8feeb5fec3435b7551ba713e883361ff0d Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Fri, 19 Dec 2014 14:19:00 +0200 Subject: [PATCH 3/5] Fix previous commits to be more efficient overall and have better logic. Also try fix clang error. --- ElunaBinding.h | 100 +++++-------- GlobalMethods.h | 382 ++++++++++++------------------------------------ HookMgr.cpp | 26 +--- 3 files changed, 131 insertions(+), 377 deletions(-) diff --git a/ElunaBinding.h b/ElunaBinding.h index 693afc2..d200967 100644 --- a/ElunaBinding.h +++ b/ElunaBinding.h @@ -30,7 +30,8 @@ public: functionReference(funcRef), isTemporary(shots != 0), remainingShots(shots) - {} + { + } }; typedef std::vector FunctionRefVector; typedef UNORDERED_MAP EventToFunctionsMap; @@ -49,9 +50,6 @@ public: // unregisters all registered functions and clears all registered events from the bindings virtual void Clear() { }; - - // Updates the counters on all temporary bindings and erases them if the counter would reach 0. - virtual void UpdateTemporaryBindings() { }; }; template @@ -74,35 +72,29 @@ public: Bindings.clear(); } - void UpdateTemporaryBindings() override + // Pushes the function references and updates the counters on the binds and erases them if the counter would reach 0 + void PushFuncRefs(lua_State* L, int event_id) { - for (EventToFunctionsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) + for (FunctionRefVector::iterator it = Bindings[event_id].begin(); it != Bindings[event_id].end();) { - for (FunctionRefVector::iterator it = itr->second.begin(); it != itr->second.end();) - { - Binding &b = (*it); - if (b.isTemporary && b.remainingShots == 0) - { - luaL_unref(E.L, LUA_REGISTRYINDEX, b.functionReference); - it = itr->second.erase(it); - } - else - { - it++; - } - } + FunctionRefVector::iterator it_old = it++; - // If there are no more entries in the vector, erase the vector. - if (itr->second.empty()) + lua_rawgeti(L, LUA_REGISTRYINDEX, (it_old->functionReference)); + + if (it_old->isTemporary) { - itr = Bindings.erase(itr); - } - else - { - itr++; + --it_old->remainingShots; + if (it_old->remainingShots == 0) + { + luaL_unref(L, LUA_REGISTRYINDEX, it_old->functionReference); + Bindings[event_id].erase(it_old); + } } } - } + + if (Bindings[event_id].empty()) + Bindings.erase(event_id); + }; void Insert(int eventId, int funcRef, uint32 shots) // Inserts a new registered event { @@ -148,48 +140,32 @@ public: Bindings.clear(); } - void UpdateTemporaryBindings() override + // Pushes the function references and updates the counters on the binds and erases them if the counter would reach 0 + void PushFuncRefs(lua_State* L, int event_id, uint32 entry) { - for (EntryToEventsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) + for (FunctionRefVector::iterator it = Bindings[entry][event_id].begin(); it != Bindings[entry][event_id].end();) { - for (EventToFunctionsMap::iterator it = itr->second.begin(); it != itr->second.end();) - { - for (FunctionRefVector::iterator i = it->second.begin(); i != it->second.end();) - { - Binding &b = (*i); - if (b.isTemporary && b.remainingShots == 0) - { - luaL_unref(E.L, LUA_REGISTRYINDEX, b.functionReference); - i = it->second.erase(i); - } - else - { - i++; - } - } + FunctionRefVector::iterator it_old = it++; - // If there are no more entries in the vector, erase the vector. - if (it->second.empty()) - { - it = itr->second.erase(it); - } - else - { - it++; - } - } + lua_rawgeti(L, LUA_REGISTRYINDEX, (it_old->functionReference)); - // If there are no more vector in the map, erase the map. - if (itr->second.empty()) + if (it_old->isTemporary) { - itr = Bindings.erase(itr); - } - else - { - itr++; + --it_old->remainingShots; + if (it_old->remainingShots == 0) + { + luaL_unref(L, LUA_REGISTRYINDEX, it_old->functionReference); + Bindings[entry][event_id].erase(it_old); + } } } - } + + if (Bindings[entry][event_id].empty()) + Bindings[entry].erase(event_id); + + if (Bindings[entry].empty()) + Bindings.erase(entry); + }; void Insert(uint32 entryId, int eventId, int funcRef, uint32 shots) // Inserts a new registered event { diff --git a/GlobalMethods.h b/GlobalMethods.h index 4a0f788..5f08647 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -471,47 +471,33 @@ namespace LuaGlobalFunctions return 1; } - /** - * Registers a packet event - * - *
-     * enum PacketEvents
-     * {
-     *     PACKET_EVENT_ON_PACKET_RECEIVE          =     5,
-     *     PACKET_EVENT_ON_PACKET_RECEIVE_UNKNOWN  =     6,
-     *     PACKET_EVENT_ON_PACKET_SEND             =     7,
-     *
-     *     PACKET_EVENT_COUNT
-     * };
-     * 
- * - * @param uint32 entry : opcode - * @param uint32 event : packet event Id, refer to PacketEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" - * @param function function : function to register - */ - int RegisterPacketEvent(Eluna* E, lua_State* L) + void RegisterEntryHelper(Eluna* E, lua_State* L, int regtype) { uint32 entry = Eluna::CHECKVAL(L, 1); uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } + luaL_checktype(L, 3, LUA_TFUNCTION); + lua_pushvalue(L, 3); + uint32 shots = Eluna::CHECKVAL(L, 4, 0); int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_PACKET, entry, ev, functionRef, shots); - return 0; + if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0) + E->Register(regtype, entry, ev, functionRef, shots); + else + luaL_argerror(L, 3, "unable to make a ref to function"); + } + + void RegisterEventHelper(Eluna* E, lua_State* L, int regtype) + { + uint32 ev = Eluna::CHECKVAL(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_pushvalue(L, 2); + uint32 shots = Eluna::CHECKVAL(L, 3, 0); + + int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); + if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0) + E->Register(regtype, 0, ev, functionRef, shots); + else + luaL_argerror(L, 2, "unable to make a ref to function"); } /** @@ -575,29 +561,12 @@ namespace LuaGlobalFunctions * * * @param uint32 event : server event Id, refer to ServerEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterServerEvent(Eluna* E, lua_State* L) { - uint32 ev = Eluna::CHECKVAL(L, 1); - uint32 shots; - - if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 2); - } - else // Otherwise, shots is the second argument and the function must be the third. - { - shots = Eluna::CHECKVAL(L, 2); - luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_SERVER, 0, ev, functionRef, shots); + RegisterEventHelper(E, L, HookMgr::REGTYPE_SERVER); return 0; } @@ -657,29 +626,12 @@ namespace LuaGlobalFunctions * * * @param uint32 event : [Player] event Id, refer to PlayerEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterPlayerEvent(Eluna* E, lua_State* L) { - uint32 ev = Eluna::CHECKVAL(L, 1); - uint32 shots; - - if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 2); - } - else // Otherwise, shots is the second argument and the function must be the third. - { - shots = Eluna::CHECKVAL(L, 2); - luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_PLAYER, 0, ev, functionRef, shots); + RegisterEventHelper(E, L, HookMgr::REGTYPE_PLAYER); return 0; } @@ -707,29 +659,12 @@ namespace LuaGlobalFunctions * * * @param uint32 event : [Guild] event Id, refer to GuildEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterGuildEvent(Eluna* E, lua_State* L) { - uint32 ev = Eluna::CHECKVAL(L, 1); - uint32 shots; - - if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 2); - } - else // Otherwise, shots is the second argument and the function must be the third. - { - shots = Eluna::CHECKVAL(L, 2); - luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_GUILD, 0, ev, functionRef, shots); + RegisterEventHelper(E, L, HookMgr::REGTYPE_GUILD); return 0; } @@ -752,29 +687,61 @@ namespace LuaGlobalFunctions * * * @param uint32 event : [Group] event Id, refer to GroupEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterGroupEvent(Eluna* E, lua_State* L) { - uint32 ev = Eluna::CHECKVAL(L, 1); - uint32 shots; + RegisterEventHelper(E, L, HookMgr::REGTYPE_GROUP); + return 0; + } - if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 2); - } - else // Otherwise, shots is the second argument and the function must be the third. - { - shots = Eluna::CHECKVAL(L, 2); - luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); - } + /** + * Registers a [Battleground] event + * + *
+     * enum BGEvents
+     * {
+     *     BG_EVENT_ON_START                               = 1,    // (event, bg, bgId, instanceId) - Needs to be added to TC
+     *     BG_EVENT_ON_END                                 = 2,    // (event, bg, bgId, instanceId, winner) - Needs to be added to TC
+     *     BG_EVENT_ON_CREATE                              = 3,    // (event, bg, bgId, instanceId) - Needs to be added to TC
+     *     BG_EVENT_ON_PRE_DESTROY                         = 4,    // (event, bg, bgId, instanceId) - Needs to be added to TC
+     *     BG_EVENT_COUNT
+     * };
+     * 
+ * + * @param uint32 event : [Battleground] event Id, refer to BGEvents above + * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" + */ + int RegisterBGEvent(Eluna* E, lua_State* L) + { + RegisterEventHelper(E, L, HookMgr::REGTYPE_BG); + return 0; + } - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_GROUP, 0, ev, functionRef, shots); + /** + * Registers a packet event + * + *
+     * enum PacketEvents
+     * {
+     *     PACKET_EVENT_ON_PACKET_RECEIVE          =     5,
+     *     PACKET_EVENT_ON_PACKET_RECEIVE_UNKNOWN  =     6,
+     *     PACKET_EVENT_ON_PACKET_SEND             =     7,
+     *
+     *     PACKET_EVENT_COUNT
+     * };
+     * 
+ * + * @param uint32 entry : opcode + * @param uint32 event : packet event Id, refer to PacketEvents above + * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" + */ + int RegisterPacketEvent(Eluna* E, lua_State* L) + { + RegisterEntryHelper(E, L, HookMgr::REGTYPE_PACKET); return 0; } @@ -792,30 +759,12 @@ namespace LuaGlobalFunctions * * @param uint32 menu_id : [Creature] entry Id * @param uint32 event : [Creature] gossip event Id, refer to GossipEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterCreatureGossipEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_CREATURE_GOSSIP, entry, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_CREATURE_GOSSIP); return 0; } @@ -833,30 +782,12 @@ namespace LuaGlobalFunctions * * @param uint32 menu_id : [GameObject] entry Id * @param uint32 event : [GameObject] gossip event Id, refer to GossipEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterGameObjectGossipEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_GAMEOBJECT_GOSSIP, entry, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_GAMEOBJECT_GOSSIP); return 0; } @@ -877,30 +808,12 @@ namespace LuaGlobalFunctions * * @param uint32 entry : [Item] entry Id * @param uint32 event : [Item] event Id, refer to ItemEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterItemEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_ITEM, entry, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_ITEM); return 0; } @@ -918,30 +831,12 @@ namespace LuaGlobalFunctions * * @param uint32 entry : [Item] entry Id * @param uint32 event : [Item] gossip event Id, refer to GossipEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterItemGossipEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_ITEM_GOSSIP, entry, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_ITEM_GOSSIP); return 0; } @@ -959,30 +854,12 @@ namespace LuaGlobalFunctions * * @param uint32 menu_id : [Player] gossip menu Id * @param uint32 event : [Player] gossip event Id, refer to GossipEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterPlayerGossipEvent(Eluna* E, lua_State* L) { - uint32 menu_id = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_PLAYER_GOSSIP, menu_id, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_PLAYER_GOSSIP); return 0; } @@ -1035,30 +912,12 @@ namespace LuaGlobalFunctions * * @param uint32 entry : [Creature] entry Id * @param uint32 event : [Creature] event Id, refer to CreatureEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterCreatureEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_CREATURE, entry, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_CREATURE); return 0; } @@ -1087,71 +946,12 @@ namespace LuaGlobalFunctions * * @param uint32 entry : [GameObject] entry Id * @param uint32 event : [GameObject] event Id, refer to GameObjectEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" * @param function function : function to register + * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" */ int RegisterGameObjectEvent(Eluna* E, lua_State* L) { - uint32 entry = Eluna::CHECKVAL(L, 1); - uint32 ev = Eluna::CHECKVAL(L, 2); - uint32 shots; - - if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 3); - } - else // Otherwise, shots is the third argument and the function must be the fourth. - { - shots = Eluna::CHECKVAL(L, 3); - luaL_checktype(L, 4, LUA_TFUNCTION); - lua_pushvalue(L, 4); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_GAMEOBJECT, entry, ev, functionRef, shots); - return 0; - } - - /** - * Registers a [Battleground] event - * - *
-     * enum BGEvents
-     * {
-     *     BG_EVENT_ON_START                               = 1,    // (event, bg, bgId, instanceId) - Needs to be added to TC
-     *     BG_EVENT_ON_END                                 = 2,    // (event, bg, bgId, instanceId, winner) - Needs to be added to TC
-     *     BG_EVENT_ON_CREATE                              = 3,    // (event, bg, bgId, instanceId) - Needs to be added to TC
-     *     BG_EVENT_ON_PRE_DESTROY                         = 4,    // (event, bg, bgId, instanceId) - Needs to be added to TC
-     *     BG_EVENT_COUNT
-     * };
-     * 
- * - * @param uint32 event : [Battleground] event Id, refer to BGEvents above - * @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function" - * @param function function : function to register - */ - int RegisterBGEvent(Eluna* E, lua_State* L) - { - uint32 ev = Eluna::CHECKVAL(L, 1); - uint32 shots; - - if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. - { - shots = 0; - lua_pushvalue(L, 2); - } - else // Otherwise, shots is the second argument and the function must be the third. - { - shots = Eluna::CHECKVAL(L, 2); - luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); - } - - int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (functionRef > 0) - E->Register(HookMgr::REGTYPE_BG, 0, ev, functionRef, shots); + RegisterEntryHelper(E, L, HookMgr::REGTYPE_GAMEOBJECT); return 0; } diff --git a/HookMgr.cpp b/HookMgr.cpp index 50772d7..c54554f 100644 --- a/HookMgr.cpp +++ b/HookMgr.cpp @@ -51,17 +51,7 @@ using namespace HookMgr; const char* _LuaBindType = this->BINDMAP->groupName; \ uint32 _LuaEvent = EVENT; \ int _LuaStackTop = lua_gettop(L); \ - bool _LuaTemporariesDied = false; \ - for (size_t i = 0; i < this->BINDMAP->Bindings[_LuaEvent].size(); ++i) \ - { \ - if (this->BINDMAP->Bindings[_LuaEvent][i].isTemporary) \ - { \ - this->BINDMAP->Bindings[_LuaEvent][i].remainingShots--; \ - if (this->BINDMAP->Bindings[_LuaEvent][i].remainingShots == 0) \ - _LuaTemporariesDied = true; \ - } \ - lua_rawgeti(L, LUA_REGISTRYINDEX, (this->BINDMAP->Bindings[_LuaEvent][i].functionReference)); \ - } \ + this->BINDMAP->PushFuncRefs(L, _LuaEvent); \ int _LuaFuncTop = lua_gettop(L); \ int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \ Eluna::Push(L, _LuaEvent); @@ -95,17 +85,7 @@ using namespace HookMgr; const char* _LuaBindType = _LuaBind->groupName; \ uint32 _LuaEvent = EVENT; \ int _LuaStackTop = lua_gettop(L); \ - bool _LuaTemporariesDied = false; \ - for (size_t i = 0; i < this->BINDMAP->Bindings[ENTRY][_LuaEvent].size(); ++i) \ - { \ - if (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].isTemporary) \ - { \ - this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].remainingShots--; \ - if (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].remainingShots == 0) \ - _LuaTemporariesDied = true; \ - } \ - lua_rawgeti(L, LUA_REGISTRYINDEX, (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].functionReference)); \ - } \ + this->BINDMAP->PushFuncRefs(L, _LuaEvent, ENTRY); \ int _LuaFuncTop = lua_gettop(L); \ int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \ Eluna::Push(L, _LuaEvent); @@ -141,8 +121,6 @@ using namespace HookMgr; ELUNA_LOG_ERROR("[Eluna]: Ending event %u for %s, stack top was %i and was supposed to be between %i and %i. Report to devs", _LuaEvent, _LuaBindType, lua_gettop(L), _LuaStackTop, _LuaStackTop + _LuaFuncCount * _LuaReturnValues); \ } \ lua_settop(L, _LuaStackTop); \ - if (_LuaTemporariesDied) \ - _LuaBind->UpdateTemporaryBindings(); \ if (!this->event_level) \ this->InvalidateObjects(); // Invalidate objects on outermost hook call From dcd0a0e89c689c90e57bc151f8cbfcdd98dece4d Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Fri, 19 Dec 2014 15:16:02 +0200 Subject: [PATCH 4/5] Fix linux build and warnings --- CorpseMethods.h | 6 +++--- ElunaEventMgr.h | 1 - GlobalMethods.h | 2 +- HookMgr.cpp | 4 +--- LuaEngine.cpp | 6 +++--- LuaFunctions.cpp | 4 ++-- PlayerMethods.h | 4 ++-- UnitMethods.h | 18 +++++++++--------- WorldObjectMethods.h | 2 +- 9 files changed, 22 insertions(+), 25 deletions(-) diff --git a/CorpseMethods.h b/CorpseMethods.h index fb85184..6da4ad9 100644 --- a/CorpseMethods.h +++ b/CorpseMethods.h @@ -59,7 +59,7 @@ namespace LuaCorpse * Resets the [Corpse] ghost time. * */ - int ResetGhostTime(Eluna* /*E*/, lua_State* L, Corpse* corpse) + int ResetGhostTime(Eluna* /*E*/, lua_State* /*L*/, Corpse* corpse) { corpse->ResetGhostTime(); return 0; @@ -69,7 +69,7 @@ namespace LuaCorpse * Saves the [Corpse] to the database. * */ - int SaveToDB(Eluna* /*E*/, lua_State* L, Corpse* corpse) + int SaveToDB(Eluna* /*E*/, lua_State* /*L*/, Corpse* corpse) { corpse->SaveToDB(); return 0; @@ -79,7 +79,7 @@ namespace LuaCorpse * Deletes the [Corpse] from the world. * */ - int DeleteBonesFromWorld(Eluna* /*E*/, lua_State* L, Corpse* corpse) + int DeleteBonesFromWorld(Eluna* /*E*/, lua_State* /*L*/, Corpse* corpse) { corpse->DeleteBonesFromWorld(); return 0; diff --git a/ElunaEventMgr.h b/ElunaEventMgr.h index 2575fe7..b1ae301 100644 --- a/ElunaEventMgr.h +++ b/ElunaEventMgr.h @@ -65,7 +65,6 @@ public: private: void RemoveEvents_internal(); void AddEvent(LuaEvent* Event); - bool removeAllEvents; EventList eventList; uint64 m_time; WorldObject* obj; diff --git a/GlobalMethods.h b/GlobalMethods.h index 5f08647..fda2407 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -2010,7 +2010,7 @@ namespace LuaGlobalFunctions * Removes old [Corpse]s from the world * */ - int RemoveOldCorpses(Eluna* /*E*/, lua_State* L) + int RemoveOldCorpses(Eluna* /*E*/, lua_State* /*L*/) { eObjectAccessor->RemoveOldCorpses(); return 0; diff --git a/HookMgr.cpp b/HookMgr.cpp index c54554f..eeb1266 100644 --- a/HookMgr.cpp +++ b/HookMgr.cpp @@ -47,7 +47,6 @@ using namespace HookMgr; if (!BINDMAP->HasEvents(EVENT)) \ RET; \ lua_State* L = this->L; \ - ElunaBind* _LuaBind = this->BINDMAP; \ const char* _LuaBindType = this->BINDMAP->groupName; \ uint32 _LuaEvent = EVENT; \ int _LuaStackTop = lua_gettop(L); \ @@ -81,8 +80,7 @@ using namespace HookMgr; if (!BINDMAP->HasEvents(ENTRY, EVENT)) \ RET; \ lua_State* L = this->L; \ - ElunaBind* _LuaBind = this->BINDMAP; \ - const char* _LuaBindType = _LuaBind->groupName; \ + const char* _LuaBindType = this->BINDMAP->groupName; \ uint32 _LuaEvent = EVENT; \ int _LuaStackTop = lua_gettop(L); \ this->BINDMAP->PushFuncRefs(L, _LuaEvent, ENTRY); \ diff --git a/LuaEngine.cpp b/LuaEngine.cpp index 20845f6..7305386 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -680,7 +680,7 @@ template<> Object* Eluna::CHECKOBJ(lua_State* L, int narg, bool error) if (!obj) obj = CHECKOBJ(L, narg, false); if (!obj) - obj = ElunaTemplate::Check(L, narg, false); + obj = ElunaTemplate::Check(L, narg, error); return obj; } template<> WorldObject* Eluna::CHECKOBJ(lua_State* L, int narg, bool error) @@ -691,7 +691,7 @@ template<> WorldObject* Eluna::CHECKOBJ(lua_State* L, int narg, boo if (!obj) obj = CHECKOBJ(L, narg, false); if (!obj) - obj = ElunaTemplate::Check(L, narg, false); + obj = ElunaTemplate::Check(L, narg, error); return obj; } template<> Unit* Eluna::CHECKOBJ(lua_State* L, int narg, bool error) @@ -700,7 +700,7 @@ template<> Unit* Eluna::CHECKOBJ(lua_State* L, int narg, bool error) if (!obj) obj = CHECKOBJ(L, narg, false); if (!obj) - obj = ElunaTemplate::Check(L, narg, false); + obj = ElunaTemplate::Check(L, narg, error); return obj; } diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index e2bd1d0..8f958a4 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -1279,7 +1279,7 @@ template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Pu template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } template<> int ElunaTemplate::Pow(lua_State* L) { - Eluna::Push(L, static_cast(std::powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); + Eluna::Push(L, static_cast(powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); return 1; } template<> int ElunaTemplate::ToString(lua_State* L) @@ -1302,7 +1302,7 @@ template<> int ElunaTemplate::Less(lua_State* L) { Eluna::Push(L, Elu template<> int ElunaTemplate::LessOrEqual(lua_State* L) { Eluna::Push(L, Eluna::CHECKVAL(L, 1) <= Eluna::CHECKVAL(L, 2)); return 1; } template<> int ElunaTemplate::Pow(lua_State* L) { - Eluna::Push(L, static_cast(std::powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); + Eluna::Push(L, static_cast(powl(static_cast(Eluna::CHECKVAL(L, 1)), static_cast(Eluna::CHECKVAL(L, 2))))); return 1; } template<> int ElunaTemplate::ToString(lua_State* L) diff --git a/PlayerMethods.h b/PlayerMethods.h index 12943cd..bebb8f0 100644 --- a/PlayerMethods.h +++ b/PlayerMethods.h @@ -1317,7 +1317,7 @@ namespace LuaPlayer /* OTHER */ #if (!defined(TBC) && !defined(CLASSIC)) - int ResetPetTalents(Eluna* /*E*/, lua_State* L, Player* player) + int ResetPetTalents(Eluna* /*E*/, lua_State* /*L*/, Player* player) { #ifndef TRINITY Pet* pet = player->GetPet(); @@ -1331,7 +1331,7 @@ namespace LuaPlayer return 0; } - int ResetAchievements(Eluna* /*E*/, lua_State* L, Player* player) + int ResetAchievements(Eluna* /*E*/, lua_State* /*L*/, Player* player) { #ifndef TRINITY player->GetAchievementMgr().Reset(); diff --git a/UnitMethods.h b/UnitMethods.h index ac2ad98..f796734 100644 --- a/UnitMethods.h +++ b/UnitMethods.h @@ -1212,7 +1212,7 @@ namespace LuaUnit }*/ /* OTHER */ - int ClearThreatList(Eluna* /*E*/, lua_State* L, Unit* unit) + int ClearThreatList(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { #ifdef MANGOS unit->GetThreatManager().clearReferences(); @@ -1230,7 +1230,7 @@ namespace LuaUnit return 0; } - int Dismount(Eluna* /*E*/, lua_State* L, Unit* unit) + int Dismount(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { if (unit->IsMounted()) { @@ -1293,7 +1293,7 @@ namespace LuaUnit // unit->GetMotionMaster()->Clear(); // all // } - int MoveStop(Eluna* /*E*/, lua_State* L, Unit* unit) + int MoveStop(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->StopMoving(); return 0; @@ -1313,7 +1313,7 @@ namespace LuaUnit return 0; } - int MoveIdle(Eluna* /*E*/, lua_State* L, Unit* unit) + int MoveIdle(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->GetMotionMaster()->MoveIdle(); return 0; @@ -1332,7 +1332,7 @@ namespace LuaUnit return 0; } - int MoveHome(Eluna* /*E*/, lua_State* L, Unit* unit) + int MoveHome(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->GetMotionMaster()->MoveTargetedHome(); return 0; @@ -1356,7 +1356,7 @@ namespace LuaUnit return 0; } - int MoveConfused(Eluna* /*E*/, lua_State* L, Unit* unit) + int MoveConfused(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->GetMotionMaster()->MoveConfused(); return 0; @@ -1450,7 +1450,7 @@ namespace LuaUnit return 0; } - int DeMorph(Eluna* /*E*/, lua_State* L, Unit* unit) + int DeMorph(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->DeMorph(); return 0; @@ -1511,7 +1511,7 @@ namespace LuaUnit return 0; } - int ClearInCombat(Eluna* /*E*/, lua_State* L, Unit* unit) + int ClearInCombat(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->ClearInCombat(); return 0; @@ -1591,7 +1591,7 @@ namespace LuaUnit return 0; } - int RemoveAllAuras(Eluna* /*E*/, lua_State* L, Unit* unit) + int RemoveAllAuras(Eluna* /*E*/, lua_State* /*L*/, Unit* unit) { unit->RemoveAllAuras(); return 0; diff --git a/WorldObjectMethods.h b/WorldObjectMethods.h index 1dd6ad9..2f69b83 100644 --- a/WorldObjectMethods.h +++ b/WorldObjectMethods.h @@ -653,7 +653,7 @@ namespace LuaWorldObject * Removes all timed events from a [WorldObject] * */ - int RemoveEvents(Eluna* /*E*/, lua_State* L, WorldObject* obj) + int RemoveEvents(Eluna* /*E*/, lua_State* /*L*/, WorldObject* obj) { obj->elunaEvents->RemoveEvents(); return 0; From cbd35438a3c180562a229498d1a26c66fd28b060 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Fri, 19 Dec 2014 17:41:34 +0200 Subject: [PATCH 5/5] Fix messing up optional args and a warning --- GlobalMethods.h | 8 ++++---- ItemMethods.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/GlobalMethods.h b/GlobalMethods.h index fda2407..c328456 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -476,11 +476,11 @@ namespace LuaGlobalFunctions uint32 entry = Eluna::CHECKVAL(L, 1); uint32 ev = Eluna::CHECKVAL(L, 2); luaL_checktype(L, 3, LUA_TFUNCTION); - lua_pushvalue(L, 3); uint32 shots = Eluna::CHECKVAL(L, 4, 0); + lua_pushvalue(L, 3); int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0) + if (functionRef >= 0) E->Register(regtype, entry, ev, functionRef, shots); else luaL_argerror(L, 3, "unable to make a ref to function"); @@ -490,11 +490,11 @@ namespace LuaGlobalFunctions { uint32 ev = Eluna::CHECKVAL(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); - lua_pushvalue(L, 2); uint32 shots = Eluna::CHECKVAL(L, 3, 0); + lua_pushvalue(L, 2); int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0) + if (functionRef >= 0) E->Register(regtype, 0, ev, functionRef, shots); else luaL_argerror(L, 2, "unable to make a ref to function"); diff --git a/ItemMethods.h b/ItemMethods.h index dfc8792..10ffb12 100644 --- a/ItemMethods.h +++ b/ItemMethods.h @@ -204,7 +204,7 @@ namespace LuaItem //if (!test.empty()) //{ name += ' '; - name += suffix[(name != temp->Name1) ? locale : DEFAULT_LOCALE]; + name += suffix[(name != temp->Name1) ? locale : uint8(DEFAULT_LOCALE)]; /*}*/ } }