diff --git a/LuaEngine.cpp b/LuaEngine.cpp index 746b521..dc1bd5c 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -22,13 +22,6 @@ #include #endif -extern "C" -{ -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -}; - Eluna::ScriptList Eluna::lua_scripts; Eluna::ScriptList Eluna::lua_extensions; std::string Eluna::lua_folderpath; @@ -462,150 +455,78 @@ void Eluna::Push(lua_State* L, Object const* obj) ElunaTemplate::push(L, obj); } } + template<> bool Eluna::CHECKVAL(lua_State* L, int narg) { - return lua_isnumber(L, narg) != 0 ? luaL_optnumber(L, narg, 1) ? true : false : lua_toboolean(L, narg) != 0; -} -template<> bool Eluna::CHECKVAL(lua_State* L, int narg, bool def) -{ - return lua_isnone(L, narg) != 0 ? def : lua_isnumber(L, narg) != 0 ? luaL_optnumber(L, narg, 1) != 0 ? true : false : lua_toboolean(L, narg) != 0; + return lua_toboolean(L, narg); } template<> float Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checknumber(L, narg); } -template<> float Eluna::CHECKVAL(lua_State* L, int narg, float def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optnumber(L, narg, def); -} template<> double Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checknumber(L, narg); } -template<> double Eluna::CHECKVAL(lua_State* L, int narg, double def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optnumber(L, narg, def); -} template<> int8 Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkint(L, narg); } -template<> int8 Eluna::CHECKVAL(lua_State* L, int narg, int8 def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optint(L, narg, def); -} template<> uint8 Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkunsigned(L, narg); } -template<> uint8 Eluna::CHECKVAL(lua_State* L, int narg, uint8 def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optunsigned(L, narg, def); -} template<> int16 Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkint(L, narg); } -template<> int16 Eluna::CHECKVAL(lua_State* L, int narg, int16 def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optint(L, narg, def); -} template<> uint16 Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkunsigned(L, narg); } -template<> uint16 Eluna::CHECKVAL(lua_State* L, int narg, uint16 def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optunsigned(L, narg, def); -} -template<> uint32 Eluna::CHECKVAL(lua_State* L, int narg) -{ - return luaL_checkunsigned(L, narg); -} -template<> uint32 Eluna::CHECKVAL(lua_State* L, int narg, uint32 def) -{ - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optunsigned(L, narg, def); -} template<> int32 Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checklong(L, narg); } -template<> int32 Eluna::CHECKVAL(lua_State* L, int narg, int32 def) +template<> uint32 Eluna::CHECKVAL(lua_State* L, int narg) { - if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) - return def; - return luaL_optlong(L, narg, def); + return luaL_checkunsigned(L, narg); } template<> const char* Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkstring(L, narg); } -template<> const char* Eluna::CHECKVAL(lua_State* L, int narg, const char* def) -{ - if (lua_isnoneornil(L, narg) || !lua_isstring(L, narg)) - return def; - return luaL_optstring(L, narg, def); -} template<> std::string Eluna::CHECKVAL(lua_State* L, int narg) { return luaL_checkstring(L, narg); } -template<> std::string Eluna::CHECKVAL(lua_State* L, int narg, std::string def) -{ - if (lua_isnoneornil(L, narg) || !lua_isstring(L, narg)) - return def; - return luaL_optstring(L, narg, def.c_str()); -} -template<> uint64 Eluna::CHECKVAL(lua_State* L, int narg) -{ - const char* c_str = CHECKVAL(L, narg, NULL); - if (!c_str) - return luaL_argerror(L, narg, "uint64 (as string) expected"); - uint64 l = 0; - sscanf(c_str, UI64FMTD, &l); - return l; -} -template<> uint64 Eluna::CHECKVAL(lua_State* L, int narg, uint64 def) -{ - const char* c_str = CHECKVAL(L, narg, NULL); - if (!c_str) - return def; - uint64 l = 0; - sscanf(c_str, UI64FMTD, &l); - return l; -} template<> int64 Eluna::CHECKVAL(lua_State* L, int narg) { const char* c_str = CHECKVAL(L, narg, NULL); if (!c_str) return luaL_argerror(L, narg, "int64 (as string) expected"); + int64 l = 0; - sscanf(c_str, SI64FMTD, &l); + int parsed_count = sscanf(c_str, SI64FMTD, &l); + if (parsed_count != 1) + return luaL_argerror(L, narg, "int64 (as string) could not be converted"); + return l; } -template<> int64 Eluna::CHECKVAL(lua_State* L, int narg, int64 def) +template<> uint64 Eluna::CHECKVAL(lua_State* L, int narg) { const char* c_str = CHECKVAL(L, narg, NULL); if (!c_str) - return def; - int64 l = 0; - sscanf(c_str, SI64FMTD, &l); + return luaL_argerror(L, narg, "uint64 (as string) expected"); + + uint64 l = 0; + int parsed_count = sscanf(c_str, UI64FMTD, &l); + if (parsed_count != 1) + return luaL_argerror(L, narg, "uint64 (as string) could not be converted"); + return l; } + #define TEST_OBJ(T, O, E, F)\ {\ if (!O || !O->F())\ diff --git a/LuaEngine.h b/LuaEngine.h index 73f08e7..6649e49 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -20,6 +20,13 @@ #include "World.h" #include "HookMgr.h" +extern "C" +{ +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +}; + #ifdef TRINITY struct ItemTemplate; #else @@ -163,7 +170,10 @@ public: // Checks template static T CHECKVAL(lua_State* L, int narg); - template static T CHECKVAL(lua_State* L, int narg, T def); + template static T CHECKVAL(lua_State* L, int narg, T def) + { + return lua_isnoneornil(L, narg) ? def : CHECKVAL(L, narg); + } template static T* CHECKOBJ(lua_State* L, int narg, bool error = true) { return ElunaTemplate::check(L, narg, error);