diff --git a/src/lua/functions/core/network/network_message_functions.cpp b/src/lua/functions/core/network/network_message_functions.cpp index d989df46095..a0f02631a1c 100644 --- a/src/lua/functions/core/network/network_message_functions.cpp +++ b/src/lua/functions/core/network/network_message_functions.cpp @@ -15,23 +15,14 @@ int NetworkMessageFunctions::luaNetworkMessageCreate(lua_State* L) { // NetworkMessage() - pushUserdata(L, new NetworkMessage); + pushUserdata(L, std::make_shared()); setMetatable(L, -1, "NetworkMessage"); return 1; } -int NetworkMessageFunctions::luaNetworkMessageDelete(lua_State* L) { - NetworkMessage** messagePtr = getRawUserdata(L, 1); - if (messagePtr && *messagePtr) { - delete *messagePtr; - *messagePtr = nullptr; - } - return 0; -} - int NetworkMessageFunctions::luaNetworkMessageGetByte(lua_State* L) { // networkMessage:getByte() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->getByte()); } else { @@ -42,7 +33,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetByte(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU16(lua_State* L) { // networkMessage:getU16() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -53,7 +44,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetU16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU32(lua_State* L) { // networkMessage:getU32() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -64,7 +55,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetU32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU64(lua_State* L) { // networkMessage:getU64() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -75,7 +66,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetU64(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetString(lua_State* L) { // networkMessage:getString() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { pushString(L, message->getString()); } else { @@ -86,7 +77,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetString(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetPosition(lua_State* L) { // networkMessage:getPosition() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { pushPosition(L, message->getPosition()); } else { @@ -98,7 +89,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetPosition(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddByte(lua_State* L) { // networkMessage:addByte(number) uint8_t number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->addByte(number); pushBoolean(L, true); @@ -111,7 +102,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddByte(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU16(lua_State* L) { // networkMessage:addU16(number) uint16_t number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -124,7 +115,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddU16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU32(lua_State* L) { // networkMessage:addU32(number) uint32_t number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -137,7 +128,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddU32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU64(lua_State* L) { // networkMessage:addU64(number) uint64_t number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -150,7 +141,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddU64(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd8(lua_State* L) { // networkMessage:add8(number) auto number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -163,7 +154,7 @@ int NetworkMessageFunctions::luaNetworkMessageAdd8(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd16(lua_State* L) { // networkMessage:add16(number) auto number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -176,7 +167,7 @@ int NetworkMessageFunctions::luaNetworkMessageAdd16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd32(lua_State* L) { // networkMessage:add32(number) auto number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -189,7 +180,7 @@ int NetworkMessageFunctions::luaNetworkMessageAdd32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd64(lua_State* L) { // networkMessage:add64(number) auto number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->add(number); pushBoolean(L, true); @@ -202,7 +193,7 @@ int NetworkMessageFunctions::luaNetworkMessageAdd64(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddString(lua_State* L) { // networkMessage:addString(string) const std::string &string = getString(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->addString(string); pushBoolean(L, true); @@ -215,7 +206,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddString(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddPosition(lua_State* L) { // networkMessage:addPosition(position) const Position &position = getPosition(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->addPosition(position); pushBoolean(L, true); @@ -228,7 +219,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddPosition(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddDouble(lua_State* L) { // networkMessage:addDouble(number) double number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->addDouble(number); pushBoolean(L, true); @@ -240,7 +231,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddDouble(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddItem(lua_State* L) { // networkMessage:addItem(item, player) - std::shared_ptr item = getUserdataShared(L, 2); + const auto &item = getUserdataShared(L, 2); if (!item) { reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); lua_pushnil(L); @@ -254,7 +245,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddItem(lua_State* L) { return 1; } - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message && player->client) { player->client->AddItem(*message, item); pushBoolean(L, true); @@ -266,7 +257,7 @@ int NetworkMessageFunctions::luaNetworkMessageAddItem(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageReset(lua_State* L) { // networkMessage:reset() - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->reset(); pushBoolean(L, true); @@ -279,7 +270,7 @@ int NetworkMessageFunctions::luaNetworkMessageReset(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageSkipBytes(lua_State* L) { // networkMessage:skipBytes(number) int16_t number = getNumber(L, 2); - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (message) { message->skipBytes(number); pushBoolean(L, true); @@ -291,13 +282,13 @@ int NetworkMessageFunctions::luaNetworkMessageSkipBytes(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageSendToPlayer(lua_State* L) { // networkMessage:sendToPlayer(player) - NetworkMessage* message = getUserdata(L, 1); + const auto &message = getUserdataShared(L, 1); if (!message) { lua_pushnil(L); return 1; } - std::shared_ptr player = getPlayer(L, 2); + const auto &player = getPlayer(L, 2); if (player) { player->sendNetworkMessage(*message); pushBoolean(L, true); diff --git a/src/lua/functions/core/network/network_message_functions.hpp b/src/lua/functions/core/network/network_message_functions.hpp index 637f3366da8..df27dbd19af 100644 --- a/src/lua/functions/core/network/network_message_functions.hpp +++ b/src/lua/functions/core/network/network_message_functions.hpp @@ -14,10 +14,9 @@ class NetworkMessageFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "NetworkMessage", "", NetworkMessageFunctions::luaNetworkMessageCreate); + registerSharedClass(L, "NetworkMessage", "", NetworkMessageFunctions::luaNetworkMessageCreate); registerMetaMethod(L, "NetworkMessage", "__eq", NetworkMessageFunctions::luaUserdataCompare); - registerMetaMethod(L, "NetworkMessage", "__gc", NetworkMessageFunctions::luaNetworkMessageDelete); - registerMethod(L, "NetworkMessage", "delete", NetworkMessageFunctions::luaNetworkMessageDelete); + registerMethod(L, "NetworkMessage", "delete", luaGarbageCollection); registerMethod(L, "NetworkMessage", "getByte", NetworkMessageFunctions::luaNetworkMessageGetByte); registerMethod(L, "NetworkMessage", "getU16", NetworkMessageFunctions::luaNetworkMessageGetU16); @@ -46,7 +45,6 @@ class NetworkMessageFunctions final : LuaScriptInterface { private: static int luaNetworkMessageCreate(lua_State* L); - static int luaNetworkMessageDelete(lua_State* L); static int luaNetworkMessageGetByte(lua_State* L); static int luaNetworkMessageGetU16(lua_State* L); diff --git a/src/lua/functions/creatures/combat/combat_functions.cpp b/src/lua/functions/creatures/combat/combat_functions.cpp index 0349456fd94..6c1529726bf 100644 --- a/src/lua/functions/creatures/combat/combat_functions.cpp +++ b/src/lua/functions/creatures/combat/combat_functions.cpp @@ -16,14 +16,14 @@ int CombatFunctions::luaCombatCreate(lua_State* L) { // Combat() - pushUserdata(L, g_luaEnvironment().createCombatObject(getScriptEnv()->getScriptInterface()).get()); + pushUserdata(L, g_luaEnvironment().createCombatObject(getScriptEnv()->getScriptInterface())); setMetatable(L, -1, "Combat"); return 1; } int CombatFunctions::luaCombatSetParameter(lua_State* L) { // combat:setParameter(key, value) - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; @@ -43,7 +43,7 @@ int CombatFunctions::luaCombatSetParameter(lua_State* L) { int CombatFunctions::luaCombatSetFormula(lua_State* L) { // combat:setFormula(type, mina, minb, maxa, maxb) - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; @@ -74,7 +74,7 @@ int CombatFunctions::luaCombatSetArea(lua_State* L) { return 1; } - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (combat) { auto areaClone = area->clone(); combat->setArea(areaClone); @@ -88,7 +88,7 @@ int CombatFunctions::luaCombatSetArea(lua_State* L) { int CombatFunctions::luaCombatSetCondition(lua_State* L) { // combat:addCondition(condition) std::shared_ptr condition = getUserdataShared(L, 2); - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (combat && condition) { combat->addCondition(condition->clone()); pushBoolean(L, true); @@ -100,7 +100,7 @@ int CombatFunctions::luaCombatSetCondition(lua_State* L) { int CombatFunctions::luaCombatSetCallback(lua_State* L) { // combat:setCallback(key, function) - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; @@ -125,7 +125,7 @@ int CombatFunctions::luaCombatSetCallback(lua_State* L) { int CombatFunctions::luaCombatSetOrigin(lua_State* L) { // combat:setOrigin(origin) - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (combat) { combat->setOrigin(getNumber(L, 2)); pushBoolean(L, true); @@ -137,7 +137,7 @@ int CombatFunctions::luaCombatSetOrigin(lua_State* L) { int CombatFunctions::luaCombatExecute(lua_State* L) { // combat:execute(creature, variant) - Combat* combat = getUserdata(L, 1); + const auto &combat = getUserdataShared(L, 1); if (!combat) { pushBoolean(L, false); return 1; diff --git a/src/lua/functions/creatures/combat/combat_functions.hpp b/src/lua/functions/creatures/combat/combat_functions.hpp index b6816c565ed..652255d7ae4 100644 --- a/src/lua/functions/creatures/combat/combat_functions.hpp +++ b/src/lua/functions/creatures/combat/combat_functions.hpp @@ -17,7 +17,7 @@ class CombatFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Combat", "", CombatFunctions::luaCombatCreate); + registerSharedClass(L, "Combat", "", CombatFunctions::luaCombatCreate); registerMetaMethod(L, "Combat", "__eq", CombatFunctions::luaUserdataCompare); registerMethod(L, "Combat", "setParameter", CombatFunctions::luaCombatSetParameter); diff --git a/src/lua/functions/lua_functions_loader.hpp b/src/lua/functions/lua_functions_loader.hpp index d4d312247a0..3ae8e3ec15b 100644 --- a/src/lua/functions/lua_functions_loader.hpp +++ b/src/lua/functions/lua_functions_loader.hpp @@ -215,10 +215,8 @@ class LuaFunctionsLoader { static void registerGlobalString(lua_State* L, const std::string &variable, const std::string &name); static int luaUserdataCompare(lua_State* L); + static int luaGarbageCollection(lua_State* L); static ScriptEnvironment scriptEnv[16]; static int32_t scriptEnvIndex; - -private: - static int luaGarbageCollection(lua_State* L); }; diff --git a/src/lua/modules/modules.cpp b/src/lua/modules/modules.cpp index 18d1107ff76..5e1565251c8 100644 --- a/src/lua/modules/modules.cpp +++ b/src/lua/modules/modules.cpp @@ -168,7 +168,7 @@ void Module::executeOnRecvbyte(std::shared_ptr player, NetworkMessage &m LuaScriptInterface::pushUserdata(L, player); LuaScriptInterface::setMetatable(L, -1, "Player"); - LuaScriptInterface::pushUserdata(L, &msg); + LuaScriptInterface::pushUserdata(L, std::shared_ptr(&msg)); LuaScriptInterface::setWeakMetatable(L, -1, "NetworkMessage"); lua_pushnumber(L, recvbyte);