diff --git a/src/lua/functions/lua_functions_loader.cpp b/src/lua/functions/lua_functions_loader.cpp index cb8d0d19d29..d4aeac2a7f1 100644 --- a/src/lua/functions/lua_functions_loader.cpp +++ b/src/lua/functions/lua_functions_loader.cpp @@ -30,7 +30,7 @@ class LuaScriptInterface; -void LuaFunctionsLoader::load(lua_State* L) { +void Lua::load(lua_State* L) { if (!L) { g_game().dieSafely("Invalid lua state, cannot load lua functions."); } @@ -45,7 +45,7 @@ void LuaFunctionsLoader::load(lua_State* L) { ZoneFunctions::init(L); } -std::string LuaFunctionsLoader::getErrorDesc(ErrorCode_t code) { +std::string Lua::getErrorDesc(ErrorCode_t code) { switch (code) { case LUA_ERROR_PLAYER_NOT_FOUND: return "Player not found"; @@ -92,7 +92,7 @@ std::string LuaFunctionsLoader::getErrorDesc(ErrorCode_t code) { } } -int LuaFunctionsLoader::protectedCall(lua_State* L, int nargs, int nresults) { +int Lua::protectedCall(lua_State* L, int nargs, int nresults) { if (const int ret = validateDispatcherContext(__FUNCTION__); ret != 0) { return ret; } @@ -106,7 +106,7 @@ int LuaFunctionsLoader::protectedCall(lua_State* L, int nargs, int nresults) { return ret; } -void LuaFunctionsLoader::reportError(const char* function, const std::string &error_desc, bool stack_trace /* = false*/) { +void Lua::reportError(const char* function, const std::string &error_desc, bool stack_trace /* = false*/) { int32_t scriptId; int32_t callbackId; bool timerEvent; @@ -144,7 +144,7 @@ void LuaFunctionsLoader::reportError(const char* function, const std::string &er g_logger().error(logMsg.str()); } -int LuaFunctionsLoader::luaErrorHandler(lua_State* L) { +int Lua::luaErrorHandler(lua_State* L) { const std::string &errorMessage = popString(L); const auto interface = getScriptEnv()->getScriptInterface(); if (!interface) { @@ -157,7 +157,7 @@ int LuaFunctionsLoader::luaErrorHandler(lua_State* L) { return 1; } -void LuaFunctionsLoader::pushVariant(lua_State* L, const LuaVariant &var) { +void Lua::pushVariant(lua_State* L, const LuaVariant &var) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -185,7 +185,7 @@ void LuaFunctionsLoader::pushVariant(lua_State* L, const LuaVariant &var) { setMetatable(L, -1, "Variant"); } -void LuaFunctionsLoader::pushThing(lua_State* L, const std::shared_ptr &thing) { +void Lua::pushThing(lua_State* L, const std::shared_ptr &thing) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -210,7 +210,7 @@ void LuaFunctionsLoader::pushThing(lua_State* L, const std::shared_ptr &t } } -void LuaFunctionsLoader::pushCylinder(lua_State* L, const std::shared_ptr &cylinder) { +void Lua::pushCylinder(lua_State* L, const std::shared_ptr &cylinder) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -231,7 +231,7 @@ void LuaFunctionsLoader::pushCylinder(lua_State* L, const std::shared_ptr weakObjectTypes; if (validateDispatcherContext(__FUNCTION__)) { return; @@ -317,7 +317,7 @@ void LuaFunctionsLoader::setWeakMetatable(lua_State* L, int32_t index, const std lua_setmetatable(L, index - 1); } -void LuaFunctionsLoader::setItemMetatable(lua_State* L, int32_t index, const std::shared_ptr &item) { +void Lua::setItemMetatable(lua_State* L, int32_t index, const std::shared_ptr &item) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -332,7 +332,7 @@ void LuaFunctionsLoader::setItemMetatable(lua_State* L, int32_t index, const std lua_setmetatable(L, index - 1); } -void LuaFunctionsLoader::setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr &creature) { +void Lua::setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr &creature) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -347,7 +347,7 @@ void LuaFunctionsLoader::setCreatureMetatable(lua_State* L, int32_t index, const lua_setmetatable(L, index - 1); } -CombatDamage LuaFunctionsLoader::getCombatDamage(lua_State* L) { +CombatDamage Lua::getCombatDamage(lua_State* L) { CombatDamage damage; damage.primary.value = getNumber(L, -4); damage.primary.type = getNumber(L, -3); @@ -359,7 +359,7 @@ CombatDamage LuaFunctionsLoader::getCombatDamage(lua_State* L) { } // Get -std::string LuaFunctionsLoader::getFormatedLoggerMessage(lua_State* L) { +std::string Lua::getFormatedLoggerMessage(lua_State* L) { const std::string format = getString(L, 1); const int n = lua_gettop(L); fmt::dynamic_format_arg_store args; @@ -395,7 +395,7 @@ std::string LuaFunctionsLoader::getFormatedLoggerMessage(lua_State* L) { return {}; } -std::string LuaFunctionsLoader::getString(lua_State* L, int32_t arg) { +std::string Lua::getString(lua_State* L, int32_t arg) { size_t len; const char* c_str = lua_tolstring(L, arg, &len); if (!c_str || len == 0) { @@ -404,7 +404,7 @@ std::string LuaFunctionsLoader::getString(lua_State* L, int32_t arg) { return std::string(c_str, len); } -Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg, int32_t &stackpos) { +Position Lua::getPosition(lua_State* L, int32_t arg, int32_t &stackpos) { Position position; position.x = getField(L, arg, "x"); position.y = getField(L, arg, "y"); @@ -421,7 +421,7 @@ Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg, int32_t &sta return position; } -Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg) { +Position Lua::getPosition(lua_State* L, int32_t arg) { Position position; position.x = getField(L, arg, "x"); position.y = getField(L, arg, "y"); @@ -431,7 +431,7 @@ Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg) { return position; } -Outfit_t LuaFunctionsLoader::getOutfit(lua_State* L, int32_t arg) { +Outfit_t Lua::getOutfit(lua_State* L, int32_t arg) { Outfit_t outfit; outfit.lookMountFeet = getField(L, arg, "lookMountFeet"); outfit.lookMountLegs = getField(L, arg, "lookMountLegs"); @@ -453,7 +453,7 @@ Outfit_t LuaFunctionsLoader::getOutfit(lua_State* L, int32_t arg) { return outfit; } -LuaVariant LuaFunctionsLoader::getVariant(lua_State* L, int32_t arg) { +LuaVariant Lua::getVariant(lua_State* L, int32_t arg) { LuaVariant var; var.instantName = getFieldString(L, arg, "instantName"); var.runeName = getFieldString(L, arg, "runeName"); @@ -487,7 +487,7 @@ LuaVariant LuaFunctionsLoader::getVariant(lua_State* L, int32_t arg) { return var; } -std::shared_ptr LuaFunctionsLoader::getThing(lua_State* L, int32_t arg) { +std::shared_ptr Lua::getThing(lua_State* L, int32_t arg) { std::shared_ptr thing; if (lua_getmetatable(L, arg) != 0) { lua_rawgeti(L, -1, 't'); @@ -521,14 +521,14 @@ std::shared_ptr LuaFunctionsLoader::getThing(lua_State* L, int32_t arg) { return thing; } -std::shared_ptr LuaFunctionsLoader::getCreature(lua_State* L, int32_t arg) { +std::shared_ptr Lua::getCreature(lua_State* L, int32_t arg) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } return g_game().getCreatureByID(getNumber(L, arg)); } -std::shared_ptr LuaFunctionsLoader::getPlayer(lua_State* L, int32_t arg, bool allowOffline /* = false */) { +std::shared_ptr Lua::getPlayer(lua_State* L, int32_t arg, bool allowOffline /* = false */) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } else if (isNumber(L, arg)) { @@ -536,11 +536,11 @@ std::shared_ptr LuaFunctionsLoader::getPlayer(lua_State* L, int32_t arg, } else if (isString(L, arg)) { return g_game().getPlayerByName(getString(L, arg), allowOffline); } - g_logger().warn("LuaFunctionsLoader::getPlayer: Invalid argument."); + g_logger().warn("Lua::getPlayer: Invalid argument."); return nullptr; } -std::shared_ptr LuaFunctionsLoader::getGuild(lua_State* L, int32_t arg, bool allowOffline /* = false */) { +std::shared_ptr Lua::getGuild(lua_State* L, int32_t arg, bool allowOffline /* = false */) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } else if (isNumber(L, arg)) { @@ -548,16 +548,16 @@ std::shared_ptr LuaFunctionsLoader::getGuild(lua_State* L, int32_t arg, b } else if (isString(L, arg)) { return g_game().getGuildByName(getString(L, arg), allowOffline); } - g_logger().warn("LuaFunctionsLoader::getGuild: Invalid argument."); + g_logger().warn("Lua::getGuild: Invalid argument."); return nullptr; } -std::string LuaFunctionsLoader::getFieldString(lua_State* L, int32_t arg, const std::string &key) { +std::string Lua::getFieldString(lua_State* L, int32_t arg, const std::string &key) { lua_getfield(L, arg, key.c_str()); return getString(L, -1); } -LuaData_t LuaFunctionsLoader::getUserdataType(lua_State* L, int32_t arg) { +LuaData_t Lua::getUserdataType(lua_State* L, int32_t arg) { if (lua_getmetatable(L, arg) == 0) { return LuaData_t::Unknown; } @@ -569,12 +569,12 @@ LuaData_t LuaFunctionsLoader::getUserdataType(lua_State* L, int32_t arg) { return type; } -std::string LuaFunctionsLoader::getUserdataTypeName(LuaData_t userType) { +std::string Lua::getUserdataTypeName(LuaData_t userType) { return magic_enum::enum_name(userType).data(); } // Push -void LuaFunctionsLoader::pushBoolean(lua_State* L, bool value) { +void Lua::pushBoolean(lua_State* L, bool value) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -582,7 +582,7 @@ void LuaFunctionsLoader::pushBoolean(lua_State* L, bool value) { lua_pushboolean(L, value ? 1 : 0); } -void LuaFunctionsLoader::pushCombatDamage(lua_State* L, const CombatDamage &damage) { +void Lua::pushCombatDamage(lua_State* L, const CombatDamage &damage) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -594,7 +594,7 @@ void LuaFunctionsLoader::pushCombatDamage(lua_State* L, const CombatDamage &dama lua_pushnumber(L, damage.origin); } -void LuaFunctionsLoader::pushInstantSpell(lua_State* L, const InstantSpell &spell) { +void Lua::pushInstantSpell(lua_State* L, const InstantSpell &spell) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -611,7 +611,7 @@ void LuaFunctionsLoader::pushInstantSpell(lua_State* L, const InstantSpell &spel setMetatable(L, -1, "Spell"); } -void LuaFunctionsLoader::pushPosition(lua_State* L, const Position &position, int32_t stackpos /* = 0*/) { +void Lua::pushPosition(lua_State* L, const Position &position, int32_t stackpos /* = 0*/) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -626,7 +626,7 @@ void LuaFunctionsLoader::pushPosition(lua_State* L, const Position &position, in setMetatable(L, -1, "Position"); } -void LuaFunctionsLoader::pushOutfit(lua_State* L, const Outfit_t &outfit) { +void Lua::pushOutfit(lua_State* L, const Outfit_t &outfit) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -647,7 +647,7 @@ void LuaFunctionsLoader::pushOutfit(lua_State* L, const Outfit_t &outfit) { setField(L, "lookFamiliarsType", outfit.lookFamiliarsType); } -void LuaFunctionsLoader::registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction /* = nullptr*/) { +void Lua::registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction /* = nullptr*/) { // className = {} lua_newtable(L); lua_pushvalue(L, -1); @@ -709,7 +709,7 @@ void LuaFunctionsLoader::registerClass(lua_State* L, const std::string &classNam lua_pop(L, 2); } -void LuaFunctionsLoader::registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func) { +void Lua::registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func) { // globalName.methodName = func lua_getglobal(L, globalName.c_str()); lua_pushcfunction(L, func); @@ -719,13 +719,13 @@ void LuaFunctionsLoader::registerMethod(lua_State* L, const std::string &globalN lua_pop(L, 1); } -void LuaFunctionsLoader::registerTable(lua_State* L, const std::string &tableName) { +void Lua::registerTable(lua_State* L, const std::string &tableName) { // _G[tableName] = {} lua_newtable(L); lua_setglobal(L, tableName.c_str()); } -void LuaFunctionsLoader::registerMetaMethod(lua_State* L, const std::string &className, const std::string &methodName, lua_CFunction func) { +void Lua::registerMetaMethod(lua_State* L, const std::string &className, const std::string &methodName, lua_CFunction func) { // className.metatable.methodName = func luaL_getmetatable(L, className.c_str()); lua_pushcfunction(L, func); @@ -735,7 +735,7 @@ void LuaFunctionsLoader::registerMetaMethod(lua_State* L, const std::string &cla lua_pop(L, 1); } -void LuaFunctionsLoader::registerVariable(lua_State* L, const std::string &tableName, const std::string &name, lua_Number value) { +void Lua::registerVariable(lua_State* L, const std::string &tableName, const std::string &name, lua_Number value) { // tableName.name = value lua_getglobal(L, tableName.c_str()); setField(L, name.c_str(), value); @@ -744,31 +744,31 @@ void LuaFunctionsLoader::registerVariable(lua_State* L, const std::string &table lua_pop(L, 1); } -void LuaFunctionsLoader::registerGlobalBoolean(lua_State* L, const std::string &name, bool value) { +void Lua::registerGlobalBoolean(lua_State* L, const std::string &name, bool value) { // _G[name] = value pushBoolean(L, value); lua_setglobal(L, name.c_str()); } -void LuaFunctionsLoader::registerGlobalMethod(lua_State* L, const std::string &functionName, lua_CFunction func) { +void Lua::registerGlobalMethod(lua_State* L, const std::string &functionName, lua_CFunction func) { // _G[functionName] = func lua_pushcfunction(L, func); lua_setglobal(L, functionName.c_str()); } -void LuaFunctionsLoader::registerGlobalVariable(lua_State* L, const std::string &name, lua_Number value) { +void Lua::registerGlobalVariable(lua_State* L, const std::string &name, lua_Number value) { // _G[name] = value lua_pushnumber(L, value); lua_setglobal(L, name.c_str()); } -void LuaFunctionsLoader::registerGlobalString(lua_State* L, const std::string &variable, const std::string &name) { +void Lua::registerGlobalString(lua_State* L, const std::string &variable, const std::string &name) { // Example: registerGlobalString(L, "VARIABLE_NAME", "variable string"); pushString(L, name); lua_setglobal(L, variable.c_str()); } -std::string LuaFunctionsLoader::escapeString(const std::string &string) { +std::string Lua::escapeString(const std::string &string) { std::string s = string; replaceString(s, "\\", "\\\\"); replaceString(s, "\"", "\\\""); @@ -777,17 +777,17 @@ std::string LuaFunctionsLoader::escapeString(const std::string &string) { return s; } -int LuaFunctionsLoader::luaUserdataCompare(lua_State* L) { +int Lua::luaUserdataCompare(lua_State* L) { pushBoolean(L, getUserdata(L, 1) == getUserdata(L, 2)); return 1; } -void LuaFunctionsLoader::registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction) { +void Lua::registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction) { registerClass(L, className, baseClass, newFunction); registerMetaMethod(L, className, "__gc", luaGarbageCollection); } -int LuaFunctionsLoader::luaGarbageCollection(lua_State* L) { +int Lua::luaGarbageCollection(lua_State* L) { const auto objPtr = static_cast*>(lua_touserdata(L, 1)); if (objPtr) { objPtr->reset(); @@ -795,7 +795,7 @@ int LuaFunctionsLoader::luaGarbageCollection(lua_State* L) { return 0; } -int LuaFunctionsLoader::validateDispatcherContext(std::string_view fncName) { +int Lua::validateDispatcherContext(std::string_view fncName) { if (DispatcherContext::isOn() && g_dispatcher().context().isAsync()) { g_logger().warn("[{}] The call to lua was ignored because the '{}' task is trying to communicate while in async mode.", fncName, g_dispatcher().context().getName()); return LUA_ERRRUN; diff --git a/src/lua/functions/lua_functions_loader.hpp b/src/lua/functions/lua_functions_loader.hpp index 50c5cade63a..aec36175880 100644 --- a/src/lua/functions/lua_functions_loader.hpp +++ b/src/lua/functions/lua_functions_loader.hpp @@ -32,7 +32,7 @@ struct LuaVariant; #define reportErrorFunc(a) reportError(__FUNCTION__, a, true) -class LuaFunctionsLoader { +class Lua { public: static void load(lua_State* L); @@ -231,7 +231,6 @@ class LuaFunctionsLoader { new (userData) std::shared_ptr(value); } -protected: static void registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction = nullptr); static void registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction = nullptr); static void registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func); diff --git a/src/lua/scripts/lua_environment.cpp b/src/lua/scripts/lua_environment.cpp index 4473a21ec90..9e95beaf8da 100644 --- a/src/lua/scripts/lua_environment.cpp +++ b/src/lua/scripts/lua_environment.cpp @@ -48,7 +48,7 @@ lua_State* LuaEnvironment::getLuaState() { bool LuaEnvironment::initState() { luaState = luaL_newstate(); - LuaFunctionsLoader::load(luaState); + Lua::load(luaState); runningEventId = EVENT_ID_USER; return true; diff --git a/src/lua/scripts/luascript.cpp b/src/lua/scripts/luascript.cpp index 1a26095bdc4..818eb6674f0 100644 --- a/src/lua/scripts/luascript.cpp +++ b/src/lua/scripts/luascript.cpp @@ -16,8 +16,8 @@ ScriptEnvironment::DBResultMap ScriptEnvironment::tempResults; uint32_t ScriptEnvironment::lastResultId = 0; std::multimap> ScriptEnvironment::tempItems; -ScriptEnvironment LuaFunctionsLoader::scriptEnv[16]; -int32_t LuaFunctionsLoader::scriptEnvIndex = -1; +ScriptEnvironment Lua::scriptEnv[16]; +int32_t Lua::scriptEnvIndex = -1; LuaScriptInterface::LuaScriptInterface(std::string initInterfaceName) : interfaceName(std::move(initInterfaceName)) { diff --git a/src/lua/scripts/luascript.hpp b/src/lua/scripts/luascript.hpp index 82ed9fa69af..c4d916a593c 100644 --- a/src/lua/scripts/luascript.hpp +++ b/src/lua/scripts/luascript.hpp @@ -12,7 +12,7 @@ #include "lua/functions/lua_functions_loader.hpp" #include "lua/scripts/script_environment.hpp" -class LuaScriptInterface : public LuaFunctionsLoader { +class LuaScriptInterface : public Lua { public: explicit LuaScriptInterface(std::string interfaceName); virtual ~LuaScriptInterface();