From 03aac62fca51d2efa005fc264d87af047c5a4626 Mon Sep 17 00:00:00 2001 From: Beats Date: Sun, 24 Mar 2024 23:09:14 -0300 Subject: [PATCH] shared_ptr || reload --- data/scripts/talkactions/god/reload.lua | 3 ++ src/creatures/appearance/mounts/mounts.cpp | 11 ++--- src/creatures/appearance/mounts/mounts.hpp | 8 ++-- src/creatures/appearance/outfit/outfit.cpp | 36 +++++++------- src/creatures/appearance/outfit/outfit.hpp | 14 ++++-- src/creatures/creatures_definitions.hpp | 27 ----------- src/creatures/players/grouping/familiars.cpp | 31 ++++++++---- src/creatures/players/grouping/familiars.hpp | 34 ++++++++++---- src/creatures/players/grouping/groups.cpp | 16 ++++--- src/creatures/players/grouping/groups.hpp | 8 ++-- src/creatures/players/player.cpp | 12 ++--- src/creatures/players/player.hpp | 8 ++-- src/game/functions/game_reload.cpp | 47 ++++++++++++------- src/game/functions/game_reload.hpp | 34 ++++++++------ src/game/game.hpp | 2 + src/io/functions/iologindata_load_player.cpp | 4 +- .../creatures/player/group_functions.cpp | 16 +++---- .../creatures/player/group_functions.hpp | 2 +- .../creatures/player/player_functions.cpp | 2 +- src/server/network/protocol/protocolgame.cpp | 14 +++--- 20 files changed, 182 insertions(+), 147 deletions(-) diff --git a/data/scripts/talkactions/god/reload.lua b/data/scripts/talkactions/god/reload.lua index 20b9431dbba..d0c0cdd45a8 100644 --- a/data/scripts/talkactions/god/reload.lua +++ b/data/scripts/talkactions/god/reload.lua @@ -7,6 +7,7 @@ local reloadTypes = { ["configuration"] = RELOAD_TYPE_CONFIG, ["core"] = RELOAD_TYPE_CORE, ["events"] = RELOAD_TYPE_EVENTS, + ["familiar"] = RELOAD_TYPE_FAMILIARS, ["global"] = RELOAD_TYPE_CORE, ["group"] = RELOAD_TYPE_GROUPS, ["groups"] = RELOAD_TYPE_GROUPS, @@ -20,6 +21,8 @@ local reloadTypes = { ["monsters"] = RELOAD_TYPE_MONSTERS, ["mount"] = RELOAD_TYPE_MOUNTS, ["mounts"] = RELOAD_TYPE_MOUNTS, + ["outfit"] = RELOAD_TYPE_OUTFITS, + ["outfits"] = RELOAD_TYPE_OUTFITS, ["npc"] = RELOAD_TYPE_NPCS, ["npcs"] = RELOAD_TYPE_NPCS, ["raid"] = RELOAD_TYPE_RAIDS, diff --git a/src/creatures/appearance/mounts/mounts.cpp b/src/creatures/appearance/mounts/mounts.cpp index dff0f02a451..7014d0b026b 100644 --- a/src/creatures/appearance/mounts/mounts.cpp +++ b/src/creatures/appearance/mounts/mounts.cpp @@ -29,13 +29,13 @@ bool Mounts::loadFromXml() { } for (auto mountNode : doc.child("mounts").children()) { - uint16_t lookType = pugi::cast(mountNode.attribute("clientid").value()); + auto lookType = pugi::cast(mountNode.attribute("clientid").value()); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS, __FUNCTION__) && lookType != 0 && !g_game().isLookTypeRegistered(lookType)) { g_logger().warn("{} - An unregistered creature mount with id '{}' was blocked to prevent client crash.", __FUNCTION__, lookType); continue; } - mounts.emplace_back(std::make_shared( + mounts.emplace(std::make_shared( static_cast(pugi::cast(mountNode.attribute("id").value())), lookType, mountNode.attribute("name").as_string(), @@ -44,12 +44,11 @@ bool Mounts::loadFromXml() { mountNode.attribute("type").as_string() )); } - mounts.shrink_to_fit(); return true; } std::shared_ptr Mounts::getMountByID(uint8_t id) { - auto it = std::find_if(mounts.begin(), mounts.end(), [id](const std::shared_ptr mount) { + auto it = std::find_if(mounts.begin(), mounts.end(), [id](const std::shared_ptr &mount) { return mount->id == id; // Note the use of -> operator to access the members of the Mount object }); @@ -58,7 +57,7 @@ std::shared_ptr Mounts::getMountByID(uint8_t id) { std::shared_ptr Mounts::getMountByName(const std::string &name) { auto mountName = name.c_str(); - auto it = std::find_if(mounts.begin(), mounts.end(), [mountName](const std::shared_ptr mount) { + auto it = std::find_if(mounts.begin(), mounts.end(), [mountName](const std::shared_ptr &mount) { return strcasecmp(mountName, mount->name.c_str()) == 0; }); @@ -66,7 +65,7 @@ std::shared_ptr Mounts::getMountByName(const std::string &name) { } std::shared_ptr Mounts::getMountByClientID(uint16_t clientId) { - auto it = std::find_if(mounts.begin(), mounts.end(), [clientId](const std::shared_ptr mount) { + auto it = std::find_if(mounts.begin(), mounts.end(), [clientId](const std::shared_ptr &mount) { return mount->clientId == clientId; // Note the use of -> operator to access the members of the Mount object }); diff --git a/src/creatures/appearance/mounts/mounts.hpp b/src/creatures/appearance/mounts/mounts.hpp index 05bd330a66b..ca4f969842a 100644 --- a/src/creatures/appearance/mounts/mounts.hpp +++ b/src/creatures/appearance/mounts/mounts.hpp @@ -11,8 +11,8 @@ struct Mount { Mount(uint8_t initId, uint16_t initClientId, std::string initName, int32_t initSpeed, bool initPremium, std::string initType) : - name(initName), speed(initSpeed), clientId(initClientId), id(initId), premium(initPremium), - type(initType) { } + name(std::move(initName)), speed(initSpeed), clientId(initClientId), id(initId), premium(initPremium), + type(std::move(initType)) { } std::string name; int32_t speed; @@ -30,10 +30,10 @@ class Mounts { std::shared_ptr getMountByName(const std::string &name); std::shared_ptr getMountByClientID(uint16_t clientId); - [[nodiscard]] const std::vector> &getMounts() const { + [[nodiscard]] const phmap::parallel_flat_hash_set> &getMounts() const { return mounts; } private: - std::vector> mounts; + phmap::parallel_flat_hash_set> mounts; }; diff --git a/src/creatures/appearance/outfit/outfit.cpp b/src/creatures/appearance/outfit/outfit.cpp index 6f0490339de..90dc05fbaa5 100644 --- a/src/creatures/appearance/outfit/outfit.cpp +++ b/src/creatures/appearance/outfit/outfit.cpp @@ -14,6 +14,13 @@ #include "utils/tools.hpp" #include "game/game.hpp" +bool Outfits::reload() { + for (auto &outfitsVector : outfits) { + outfitsVector.clear(); + } + return loadFromXml(); +} + bool Outfits::loadFromXml() { pugi::xml_document doc; auto folder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__) + "/XML/outfits.xml"; @@ -34,7 +41,7 @@ bool Outfits::loadFromXml() { continue; } - uint16_t type = pugi::cast(attr.value()); + auto type = pugi::cast(attr.value()); if (type > PLAYERSEX_LAST) { g_logger().warn("[Outfits::loadFromXml] - Invalid outfit type {}", type); continue; @@ -46,7 +53,7 @@ bool Outfits::loadFromXml() { continue; } - if (uint16_t lookType = pugi::cast(lookTypeAttribute.value()); + if (auto lookType = pugi::cast(lookTypeAttribute.value()); g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS, __FUNCTION__) && lookType != 0 && !g_game().isLookTypeRegistered(lookType)) { g_logger().warn("[Outfits::loadFromXml] An unregistered creature looktype type with id '{}' was ignored to prevent client crash.", lookType); @@ -68,10 +75,12 @@ bool Outfits::loadFromXml() { } std::shared_ptr Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const { - for (const auto &outfit : outfits[sex]) { - if (outfit->lookType == lookType) { - return outfit; - } + auto it = std::ranges::find_if(outfits[sex], [&lookType](const auto &outfit) { + return outfit->lookType == lookType; + }); + + if (it != outfits[sex].end()) { + return *it; } return nullptr; } @@ -82,18 +91,7 @@ std::shared_ptr Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t l * @param lookType current looktype * @return const pointer to the outfit or nullptr if it could not be found. */ - -std::shared_ptr Outfits::getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) { +[[maybe_unused]] std::shared_ptr Outfits::getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const { PlayerSex_t searchSex = (sex == PLAYERSEX_MALE) ? PLAYERSEX_FEMALE : PLAYERSEX_MALE; - - for (uint16_t i = 0; i < outfits[sex].size(); i++) { - if (outfits[sex].at(i)->lookType == lookType) { - if (outfits[searchSex].size() > i) { - return outfits[searchSex].at(i); - } else { // looktype found but the oposite sex array doesn't have this index. - return nullptr; - } - } - } - return nullptr; + return getOutfitByLookType(searchSex, lookType); } diff --git a/src/creatures/appearance/outfit/outfit.hpp b/src/creatures/appearance/outfit/outfit.hpp index e1a5143c673..4192ef8d938 100644 --- a/src/creatures/appearance/outfit/outfit.hpp +++ b/src/creatures/appearance/outfit/outfit.hpp @@ -9,12 +9,17 @@ #pragma once -#include "declarations.hpp" -#include "lib/di/container.hpp" +struct OutfitEntry { + constexpr OutfitEntry(uint16_t initLookType, uint8_t initAddons) : + lookType(initLookType), addons(initAddons) { } + + uint16_t lookType; + uint8_t addons; +}; struct Outfit { Outfit(std::string initName, uint16_t initLookType, bool initPremium, bool initUnlocked, std::string initFrom) : - name(initName), lookType(initLookType), premium(initPremium), unlocked(initUnlocked), from(initFrom) { } + name(std::move(initName)), lookType(initLookType), premium(initPremium), unlocked(initUnlocked), from(std::move(initFrom)) { } std::string name; uint16_t lookType; @@ -38,9 +43,10 @@ class Outfits { return inject(); } - std::shared_ptr getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType); + [[maybe_unused]] std::shared_ptr getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const; bool loadFromXml(); + bool reload(); [[nodiscard]] std::shared_ptr getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const; [[nodiscard]] const std::vector> &getOutfits(PlayerSex_t sex) const { diff --git a/src/creatures/creatures_definitions.hpp b/src/creatures/creatures_definitions.hpp index 46fb5fe5979..27b8a5fd4f1 100644 --- a/src/creatures/creatures_definitions.hpp +++ b/src/creatures/creatures_definitions.hpp @@ -1415,20 +1415,6 @@ struct VIPEntry { bool notify; }; -struct OutfitEntry { - constexpr OutfitEntry(uint16_t initLookType, uint8_t initAddons) : - lookType(initLookType), addons(initAddons) { } - - uint16_t lookType; - uint8_t addons; -}; - -struct FamiliarEntry { - constexpr explicit FamiliarEntry(uint16_t initLookType) : - lookType(initLookType) { } - uint16_t lookType; -}; - struct Skill { uint64_t tries = 0; uint16_t level = 10; @@ -1539,19 +1525,6 @@ using ItemsTierCountList = std::map>; | ... */ -struct Familiar { - Familiar(std::string initName, uint16_t initLookType, bool initPremium, bool initUnlocked, std::string initType) : - name(initName), lookType(initLookType), - premium(initPremium), unlocked(initUnlocked), - type(initType) { } - - std::string name; - uint16_t lookType; - bool premium; - bool unlocked; - std::string type; -}; - struct ProtocolFamiliars { ProtocolFamiliars(const std::string &initName, uint16_t initLookType) : name(initName), lookType(initLookType) { } diff --git a/src/creatures/players/grouping/familiars.cpp b/src/creatures/players/grouping/familiars.cpp index 538519685c3..b85cd78c19f 100644 --- a/src/creatures/players/grouping/familiars.cpp +++ b/src/creatures/players/grouping/familiars.cpp @@ -10,10 +10,22 @@ #include "pch.hpp" #include "creatures/players/grouping/familiars.hpp" +#include "lib/di/container.hpp" #include "config/configmanager.hpp" #include "utils/pugicast.hpp" #include "utils/tools.hpp" +Familiars &Familiars::getInstance() { + return inject(); +} + +bool Familiars::reload() { + for (auto &familiarsVector : familiars) { + familiarsVector.clear(); + } + return loadFromXml(); +} + bool Familiars::loadFromXml() { pugi::xml_document doc; auto folder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__) + "/XML/familiars.xml"; @@ -35,7 +47,7 @@ bool Familiars::loadFromXml() { continue; } - uint16_t vocation = pugi::cast(attr.value()); + auto vocation = pugi::cast(attr.value()); if (vocation > VOCATION_LAST) { g_logger().warn("[Familiars::loadFromXml] - Invalid familiar vocation {}", vocation); continue; @@ -47,13 +59,13 @@ bool Familiars::loadFromXml() { continue; } - familiars[vocation].emplace_back( + familiars[vocation].emplace_back(std::make_shared( familiarsNode.attribute("name").as_string(), pugi::cast(lookTypeAttribute.value()), familiarsNode.attribute("premium").as_bool(), familiarsNode.attribute("unlocked").as_bool(true), familiarsNode.attribute("type").as_string() - ); + )); } for (uint16_t vocation = VOCATION_NONE; vocation <= VOCATION_LAST; ++vocation) { familiars[vocation].shrink_to_fit(); @@ -61,11 +73,14 @@ bool Familiars::loadFromXml() { return true; } -const Familiar* Familiars::getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const { - for (const Familiar &familiar : familiars[vocation]) { - if (familiar.lookType == lookType) { - return &familiar; - } +std::shared_ptr Familiars::getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const { + const auto &familiarList = familiars[vocation]; + auto it = std::find_if(familiarList.begin(), familiarList.end(), [lookType](const auto &familiar) { + return familiar->lookType == lookType; + }); + + if (it != familiarList.end()) { + return *it; } return nullptr; } diff --git a/src/creatures/players/grouping/familiars.hpp b/src/creatures/players/grouping/familiars.hpp index aae9b34e227..f567bdafc4d 100644 --- a/src/creatures/players/grouping/familiars.hpp +++ b/src/creatures/players/grouping/familiars.hpp @@ -9,20 +9,38 @@ #pragma once -#include "declarations.hpp" -#include "lib/di/container.hpp" +struct FamiliarEntry { + constexpr explicit FamiliarEntry(uint16_t initLookType) : + lookType(initLookType) { } + uint16_t lookType; +}; + +struct Familiar { + Familiar(std::string initName, uint16_t initLookType, bool initPremium, bool initUnlocked, std::string initType) : + name(std::move(initName)), lookType(initLookType), + premium(initPremium), unlocked(initUnlocked), + type(std::move(initType)) { } + + std::string name; + uint16_t lookType; + bool premium; + bool unlocked; + std::string type; +}; class Familiars { public: - static Familiars &getInstance() { - return inject(); - } + static Familiars &getInstance(); + bool loadFromXml(); - const std::vector &getFamiliars(uint16_t vocation) const { + bool reload(); + + std::vector> &getFamiliars(uint16_t vocation) { return familiars[vocation]; } - const Familiar* getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const; + + [[nodiscard]] std::shared_ptr getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const; private: - std::vector familiars[VOCATION_LAST + 1]; + std::vector> familiars[VOCATION_LAST + 1]; }; diff --git a/src/creatures/players/grouping/groups.cpp b/src/creatures/players/grouping/groups.cpp index 40dd13bc385..69f2f395267 100644 --- a/src/creatures/players/grouping/groups.cpp +++ b/src/creatures/players/grouping/groups.cpp @@ -43,7 +43,7 @@ PlayerFlags_t Groups::getFlagFromNumber(uint8_t value) { return magic_enum::enum_value(value); } -bool Groups::reload() const { +bool Groups::reload() { // Clear groups g_game().groups.getGroups().clear(); return g_game().groups.load(); @@ -93,17 +93,19 @@ bool Groups::load() { // Parsing group flags parseGroupFlags(group, groupNode); - groups_vector.push_back(group); + groups_vector.emplace_back(std::make_shared(group)); } groups_vector.shrink_to_fit(); return true; } -Group* Groups::getGroup(uint16_t id) { - for (Group &group : groups_vector) { - if (group.id == id) { - return &group; - } +std::shared_ptr Groups::getGroup(uint16_t id) { + auto it = std::find_if(groups_vector.begin(), groups_vector.end(), [id](const auto &group) { + return group->id == id; + }); + + if (it != groups_vector.end()) { + return *it; } return nullptr; } diff --git a/src/creatures/players/grouping/groups.hpp b/src/creatures/players/grouping/groups.hpp index 3d74564173d..c269fea85ef 100644 --- a/src/creatures/players/grouping/groups.hpp +++ b/src/creatures/players/grouping/groups.hpp @@ -24,13 +24,13 @@ class Groups { public: static uint8_t getFlagNumber(PlayerFlags_t playerFlags); static PlayerFlags_t getFlagFromNumber(uint8_t value); - bool reload() const; + bool reload(); bool load(); - Group* getGroup(uint16_t id); - std::vector &getGroups() { + std::shared_ptr getGroup(uint16_t id); + std::vector> &getGroups() { return groups_vector; } private: - std::vector groups_vector; + std::vector> groups_vector; }; diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 1f0c9b45dce..f96aa7bbb18 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -1925,7 +1925,7 @@ void Player::onWalk(Direction &dir) { Creature::onWalk(dir); setNextActionTask(nullptr); - + g_callbacks().executeCallback(EventCallback_t::playerOnWalk, &EventCallback::playerOnWalk, getPlayer(), dir); } @@ -5006,7 +5006,7 @@ bool Player::canFamiliar(uint16_t lookType) const { return true; } - const Familiar* familiar = Familiars::getInstance().getFamiliarByLookType(getVocationId(), lookType); + const auto &familiar = Familiars::getInstance().getFamiliarByLookType(getVocationId(), lookType); if (!familiar) { return false; } @@ -5047,24 +5047,24 @@ bool Player::removeFamiliar(uint16_t lookType) { return false; } -bool Player::getFamiliar(const Familiar &familiar) const { +bool Player::getFamiliar(const std::shared_ptr &familiar) const { if (group->access) { return true; } - if (familiar.premium && !isPremium()) { + if (familiar->premium && !isPremium()) { return false; } for (const FamiliarEntry &familiarEntry : familiars) { - if (familiarEntry.lookType != familiar.lookType) { + if (familiarEntry.lookType != familiar->lookType) { continue; } return true; } - if (!familiar.unlocked) { + if (!familiar->unlocked) { return false; } diff --git a/src/creatures/players/player.hpp b/src/creatures/players/player.hpp index 70c8c05aecd..230e289049c 100644 --- a/src/creatures/players/player.hpp +++ b/src/creatures/players/player.hpp @@ -510,10 +510,10 @@ class Player final : public Creature, public Cylinder, public Bankable { void genReservedStorageRange(); - void setGroup(Group* newGroup) { + void setGroup(std::shared_ptr newGroup) { group = newGroup; } - Group* getGroup() const { + std::shared_ptr getGroup() const { return group; } @@ -1027,7 +1027,7 @@ class Player final : public Creature, public Cylinder, public Bankable { bool canFamiliar(uint16_t lookType) const; void addFamiliar(uint16_t lookType); bool removeFamiliar(uint16_t lookType); - bool getFamiliar(const Familiar &familiar) const; + bool getFamiliar(const std::shared_ptr &familiar) const; void setFamiliarLooktype(uint16_t familiarLooktype) { this->defaultOutfit.lookFamiliarsType = familiarLooktype; } @@ -2760,7 +2760,7 @@ class Player final : public Creature, public Cylinder, public Bankable { std::shared_ptr bedItem = nullptr; std::shared_ptr guild = nullptr; GuildRank_ptr guildRank; - Group* group = nullptr; + std::shared_ptr group = nullptr; std::shared_ptr inbox; std::shared_ptr imbuingItem = nullptr; std::shared_ptr tradeItem = nullptr; diff --git a/src/game/functions/game_reload.cpp b/src/game/functions/game_reload.cpp index 9a7246a86ec..546a56288f7 100644 --- a/src/game/functions/game_reload.cpp +++ b/src/game/functions/game_reload.cpp @@ -44,6 +44,8 @@ bool GameReload::init(Reload_t reloadTypes) const { return reloadMonsters(); case Reload_t::RELOAD_TYPE_MOUNTS: return reloadMounts(); + case Reload_t::RELOAD_TYPE_OUTFITS: + return reloadOutfits(); case Reload_t::RELOAD_TYPE_NPCS: return reloadNpcs(); case Reload_t::RELOAD_TYPE_RAIDS: @@ -54,12 +56,14 @@ bool GameReload::init(Reload_t reloadTypes) const { return reloadGroups(); case Reload_t::RELOAD_TYPE_VOCATIONS: return reloadVocations(); + case Reload_t::RELOAD_TYPE_FAMILIARS: + return reloadFamiliars(); default: return false; } } -uint8_t GameReload::getReloadNumber(Reload_t reloadTypes) const { +uint8_t GameReload::getReloadNumber(Reload_t reloadTypes) { return magic_enum::enum_integer(reloadTypes); } @@ -93,30 +97,29 @@ bool GameReload::reloadAll() const { return std::ranges::any_of(reloadResults, [](bool result) { return result; }); } -bool GameReload::reloadChat() const { +bool GameReload::reloadChat() { const bool result = g_chat().load(); logReloadStatus("Chat", result); return result; } -bool GameReload::reloadConfig() const { +bool GameReload::reloadConfig() { const bool result = g_configManager().reload(); logReloadStatus("Config", result); return result; } -bool GameReload::reloadEvents() const { +bool GameReload::reloadEvents() { const bool result = g_events().loadFromXml(); logReloadStatus("Events", result); return result; } -bool GameReload::reloadCore() const { +bool GameReload::reloadCore() { const auto &coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__); const bool coreLoaded = g_luaEnvironment().loadFile(coreFolder + "/core.lua", "core.lua") == 0; if (coreLoaded) { - const auto &datapackFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__); const bool scriptsLoaded = g_scripts().loadScripts(coreFolder + "/scripts/lib", true, false); if (scriptsLoaded) { return true; @@ -127,25 +130,25 @@ bool GameReload::reloadCore() const { return false; } -bool GameReload::reloadImbuements() const { +bool GameReload::reloadImbuements() { const bool result = g_imbuements().reload(); logReloadStatus("Imbuements", result); return result; } -bool GameReload::reloadItems() const { +bool GameReload::reloadItems() { const bool result = Item::items.reload(); logReloadStatus("Items", result); return result; } -bool GameReload::reloadModules() const { +bool GameReload::reloadModules() { const bool result = g_modules().reload(); logReloadStatus("Modules", result); return result; } -bool GameReload::reloadMonsters() const { +bool GameReload::reloadMonsters() { g_monsters().clear(); const auto &datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__); const auto &coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__); @@ -162,25 +165,31 @@ bool GameReload::reloadMonsters() const { } } -bool GameReload::reloadMounts() const { +bool GameReload::reloadMounts() { const bool result = g_game().mounts.reload(); logReloadStatus("Mounts", result); return result; } -bool GameReload::reloadNpcs() const { +bool GameReload::reloadOutfits() { + const bool result = g_game().outfits.reload(); + logReloadStatus("Outfits", result); + return result; +} + +bool GameReload::reloadNpcs() { const bool result = g_npcs().reload(); logReloadStatus("NPCs", result); return result; } -bool GameReload::reloadRaids() const { +bool GameReload::reloadRaids() { const bool result = g_game().raids.reload() && g_game().raids.startup(); logReloadStatus("Raids", result); return result; } -bool GameReload::reloadScripts() const { +bool GameReload::reloadScripts() { g_scripts().clearAllScripts(); Zone::clearZones(); @@ -198,14 +207,20 @@ bool GameReload::reloadScripts() const { return true; } -bool GameReload::reloadGroups() const { +bool GameReload::reloadGroups() { const bool result = g_game().groups.reload(); logReloadStatus("Groups", result); return result; } -bool GameReload::reloadVocations() const { +bool GameReload::reloadVocations() { const bool result = g_vocations().reload(); logReloadStatus("Vocations", result); return result; } + +bool GameReload::reloadFamiliars() { + const bool result = g_game().familiars.reload(); + logReloadStatus("Familiars", result); + return result; +} diff --git a/src/game/functions/game_reload.hpp b/src/game/functions/game_reload.hpp index 45ff35c01a2..d220f43b049 100644 --- a/src/game/functions/game_reload.hpp +++ b/src/game/functions/game_reload.hpp @@ -25,11 +25,13 @@ enum class Reload_t : uint8_t { RELOAD_TYPE_MODULES, RELOAD_TYPE_MONSTERS, RELOAD_TYPE_MOUNTS, + RELOAD_TYPE_OUTFITS, RELOAD_TYPE_NPCS, RELOAD_TYPE_RAIDS, RELOAD_TYPE_SCRIPTS, RELOAD_TYPE_GROUPS, RELOAD_TYPE_VOCATIONS, + RELOAD_TYPE_FAMILIARS, // Every is last RELOAD_TYPE_LAST @@ -49,24 +51,26 @@ class GameReload : public Game { } bool init(Reload_t reloadType) const; - uint8_t getReloadNumber(Reload_t reloadTypes) const; + static uint8_t getReloadNumber(Reload_t reloadTypes); private: bool reloadAll() const; - bool reloadChat() const; - bool reloadConfig() const; - bool reloadEvents() const; - bool reloadCore() const; - bool reloadImbuements() const; - bool reloadItems() const; - bool reloadModules() const; - bool reloadMonsters() const; - bool reloadMounts() const; - bool reloadNpcs() const; - bool reloadRaids() const; - bool reloadScripts() const; - bool reloadGroups() const; - bool reloadVocations() const; + static bool reloadChat(); + static bool reloadConfig(); + static bool reloadEvents(); + static bool reloadCore(); + static bool reloadImbuements(); + static bool reloadItems(); + static bool reloadModules(); + static bool reloadMonsters(); + static bool reloadMounts(); + static bool reloadNpcs(); + static bool reloadRaids(); + static bool reloadScripts(); + static bool reloadGroups(); + static bool reloadVocations(); + static bool reloadFamiliars(); + static bool reloadOutfits(); }; constexpr auto g_gameReload = GameReload::getInstance; diff --git a/src/game/game.hpp b/src/game/game.hpp index 708aa4b1782..b6fc844ad7c 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -576,8 +576,10 @@ class Game { bool hasDistanceEffect(uint16_t effectId); Groups groups; + Familiars familiars; Map map; Mounts mounts; + Outfits outfits; Raids raids; std::unique_ptr m_appearancesPtr; diff --git a/src/io/functions/iologindata_load_player.cpp b/src/io/functions/iologindata_load_player.cpp index d4a4f33774d..7beb680195e 100644 --- a/src/io/functions/iologindata_load_player.cpp +++ b/src/io/functions/iologindata_load_player.cpp @@ -66,7 +66,7 @@ bool IOLoginDataLoad::preLoadPlayer(std::shared_ptr player, const std::s } player->setGUID(result->getNumber("id")); - Group* group = g_game().groups.getGroup(result->getNumber("group_id")); + std::shared_ptr group = g_game().groups.getGroup(result->getNumber("group_id")); if (!group) { g_logger().error("Player {} has group id {} which doesn't exist", player->name, result->getNumber("group_id")); return false; @@ -118,7 +118,7 @@ bool IOLoginDataLoad::loadPlayerFirst(std::shared_ptr player, DBResult_p player->setAccount(result->getNumber("account_id")); } - Group* group = g_game().groups.getGroup(result->getNumber("group_id")); + std::shared_ptr group = g_game().groups.getGroup(result->getNumber("group_id")); if (!group) { g_logger().error("Player {} has group id {} which doesn't exist", player->name, result->getNumber("group_id")); return false; diff --git a/src/lua/functions/creatures/player/group_functions.cpp b/src/lua/functions/creatures/player/group_functions.cpp index 0ea195fe745..5acc16d72a9 100644 --- a/src/lua/functions/creatures/player/group_functions.cpp +++ b/src/lua/functions/creatures/player/group_functions.cpp @@ -17,7 +17,7 @@ int GroupFunctions::luaGroupCreate(lua_State* L) { // Group(id) uint32_t id = getNumber(L, 2); - Group* group = g_game().groups.getGroup(id); + std::shared_ptr group = g_game().groups.getGroup(id); if (group) { pushUserdata(L, group); setMetatable(L, -1, "Group"); @@ -29,7 +29,7 @@ int GroupFunctions::luaGroupCreate(lua_State* L) { int GroupFunctions::luaGroupGetId(lua_State* L) { // group:getId() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->id); } else { @@ -40,7 +40,7 @@ int GroupFunctions::luaGroupGetId(lua_State* L) { int GroupFunctions::luaGroupGetName(lua_State* L) { // group:getName() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { pushString(L, group->name); } else { @@ -51,7 +51,7 @@ int GroupFunctions::luaGroupGetName(lua_State* L) { int GroupFunctions::luaGroupGetFlags(lua_State* L) { // group:getFlags() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { std::bitset flags; for (uint8_t i = 0; i < magic_enum::enum_integer(PlayerFlags_t::FlagLast); ++i) { @@ -68,7 +68,7 @@ int GroupFunctions::luaGroupGetFlags(lua_State* L) { int GroupFunctions::luaGroupGetAccess(lua_State* L) { // group:getAccess() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { pushBoolean(L, group->access); } else { @@ -79,7 +79,7 @@ int GroupFunctions::luaGroupGetAccess(lua_State* L) { int GroupFunctions::luaGroupGetMaxDepotItems(lua_State* L) { // group:getMaxDepotItems() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->maxDepotItems); } else { @@ -90,7 +90,7 @@ int GroupFunctions::luaGroupGetMaxDepotItems(lua_State* L) { int GroupFunctions::luaGroupGetMaxVipEntries(lua_State* L) { // group:getMaxVipEntries() - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->maxVipEntries); } else { @@ -101,7 +101,7 @@ int GroupFunctions::luaGroupGetMaxVipEntries(lua_State* L) { int GroupFunctions::luaGroupHasFlag(lua_State* L) { // group:hasFlag(flag) - Group* group = getUserdata(L, 1); + std::shared_ptr group = getUserdataShared(L, 1); if (group) { auto flag = static_cast(getNumber(L, 2)); pushBoolean(L, group->flags[Groups::getFlagNumber(flag)]); diff --git a/src/lua/functions/creatures/player/group_functions.hpp b/src/lua/functions/creatures/player/group_functions.hpp index ecc3ac07280..4a33d1b988e 100644 --- a/src/lua/functions/creatures/player/group_functions.hpp +++ b/src/lua/functions/creatures/player/group_functions.hpp @@ -14,7 +14,7 @@ class GroupFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Group", "", GroupFunctions::luaGroupCreate); + registerSharedClass(L, "Group", "", GroupFunctions::luaGroupCreate); registerMetaMethod(L, "Group", "__eq", GroupFunctions::luaUserdataCompare); registerMethod(L, "Group", "getId", GroupFunctions::luaGroupGetId); diff --git a/src/lua/functions/creatures/player/player_functions.cpp b/src/lua/functions/creatures/player/player_functions.cpp index 94da8f4ffbc..8da17ae09ee 100644 --- a/src/lua/functions/creatures/player/player_functions.cpp +++ b/src/lua/functions/creatures/player/player_functions.cpp @@ -1602,7 +1602,7 @@ int PlayerFunctions::luaPlayerGetGroup(lua_State* L) { int PlayerFunctions::luaPlayerSetGroup(lua_State* L) { // player:setGroup(group) - Group* group = getUserdata(L, 2); + std::shared_ptr group = getUserdataShared(L, 2); if (!group) { pushBoolean(L, false); return 1; diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 13b328dd2cf..3358080a35e 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -3767,14 +3767,14 @@ void ProtocolGame::sendCyclopediaCharacterOutfitsMounts() { auto startFamiliars = msg.getBufferPosition(); msg.skipBytes(2); const auto familiars = Familiars::getInstance().getFamiliars(player->getVocationId()); - for (const Familiar &familiar : familiars) { - const std::string type = familiar.type; + for (const auto &familiar : familiars) { + const std::string type = familiar->type; if (!player->getFamiliar(familiar)) { continue; } ++familiarsSize; - msg.add(familiar.lookType); - msg.addString(familiar.name, "ProtocolGame::sendCyclopediaCharacterOutfitsMounts - familiar.name"); + msg.add(familiar->lookType); + msg.addString(familiar->name, "ProtocolGame::sendCyclopediaCharacterOutfitsMounts - familiar.name"); if (type == "quest") { msg.addByte(CYCLOPEDIA_CHARACTERINFO_OUTFITTYPE_QUEST); } else { @@ -6778,13 +6778,13 @@ void ProtocolGame::sendOutfitWindow() { const auto familiars = Familiars::getInstance().getFamiliars(player->getVocationId()); - for (const Familiar &familiar : familiars) { + for (const auto &familiar : familiars) { if (!player->getFamiliar(familiar)) { continue; } - msg.add(familiar.lookType); - msg.addString(familiar.name, "ProtocolGame::sendOutfitWindow - familiar.name"); + msg.add(familiar->lookType); + msg.addString(familiar->name, "ProtocolGame::sendOutfitWindow - familiar.name"); msg.addByte(0x00); if (++familiarSize == limitFamiliars) { break;