Skip to content

Commit

Permalink
shared_ptr || reload
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh authored and elsongabriel committed Apr 26, 2024
1 parent a820101 commit 03aac62
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 147 deletions.
3 changes: 3 additions & 0 deletions data/scripts/talkactions/god/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions src/creatures/appearance/mounts/mounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ bool Mounts::loadFromXml() {
}

for (auto mountNode : doc.child("mounts").children()) {
uint16_t lookType = pugi::cast<uint16_t>(mountNode.attribute("clientid").value());
auto lookType = pugi::cast<uint16_t>(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<Mount>(
mounts.emplace(std::make_shared<Mount>(
static_cast<uint8_t>(pugi::cast<uint16_t>(mountNode.attribute("id").value())),
lookType,
mountNode.attribute("name").as_string(),
Expand All @@ -44,12 +44,11 @@ bool Mounts::loadFromXml() {
mountNode.attribute("type").as_string()
));
}
mounts.shrink_to_fit();
return true;
}

std::shared_ptr<Mount> Mounts::getMountByID(uint8_t id) {
auto it = std::find_if(mounts.begin(), mounts.end(), [id](const std::shared_ptr<Mount> mount) {
auto it = std::find_if(mounts.begin(), mounts.end(), [id](const std::shared_ptr<Mount> &mount) {
return mount->id == id; // Note the use of -> operator to access the members of the Mount object
});

Expand All @@ -58,15 +57,15 @@ std::shared_ptr<Mount> Mounts::getMountByID(uint8_t id) {

std::shared_ptr<Mount> 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> mount) {
auto it = std::find_if(mounts.begin(), mounts.end(), [mountName](const std::shared_ptr<Mount> &mount) {
return strcasecmp(mountName, mount->name.c_str()) == 0;
});

return it != mounts.end() ? *it : nullptr;
}

std::shared_ptr<Mount> Mounts::getMountByClientID(uint16_t clientId) {
auto it = std::find_if(mounts.begin(), mounts.end(), [clientId](const std::shared_ptr<Mount> mount) {
auto it = std::find_if(mounts.begin(), mounts.end(), [clientId](const std::shared_ptr<Mount> &mount) {
return mount->clientId == clientId; // Note the use of -> operator to access the members of the Mount object
});

Expand Down
8 changes: 4 additions & 4 deletions src/creatures/appearance/mounts/mounts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,10 +30,10 @@ class Mounts {
std::shared_ptr<Mount> getMountByName(const std::string &name);
std::shared_ptr<Mount> getMountByClientID(uint16_t clientId);

[[nodiscard]] const std::vector<std::shared_ptr<Mount>> &getMounts() const {
[[nodiscard]] const phmap::parallel_flat_hash_set<std::shared_ptr<Mount>> &getMounts() const {
return mounts;
}

private:
std::vector<std::shared_ptr<Mount>> mounts;
phmap::parallel_flat_hash_set<std::shared_ptr<Mount>> mounts;
};
36 changes: 17 additions & 19 deletions src/creatures/appearance/outfit/outfit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#include "utils/tools.hpp"
#include "game/game.hpp"

bool Outfits::reload() {
for (auto &outfitsVector : outfits) {

Check failure on line 18 in src/creatures/appearance/outfit/outfit.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘outfits’ was not declared in this scope; did you mean ‘Outfits’?

Check failure on line 18 in src/creatures/appearance/outfit/outfit.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘outfits’ was not declared in this scope; did you mean ‘Outfits’?
outfitsVector.clear();
}
return loadFromXml();
}

bool Outfits::loadFromXml() {
pugi::xml_document doc;
auto folder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__) + "/XML/outfits.xml";
Expand All @@ -34,7 +41,7 @@ bool Outfits::loadFromXml() {
continue;
}

uint16_t type = pugi::cast<uint16_t>(attr.value());
auto type = pugi::cast<uint16_t>(attr.value());
if (type > PLAYERSEX_LAST) {
g_logger().warn("[Outfits::loadFromXml] - Invalid outfit type {}", type);
continue;
Expand All @@ -46,7 +53,7 @@ bool Outfits::loadFromXml() {
continue;
}

if (uint16_t lookType = pugi::cast<uint16_t>(lookTypeAttribute.value());
if (auto lookType = pugi::cast<uint16_t>(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);
Expand All @@ -68,10 +75,12 @@ bool Outfits::loadFromXml() {
}

std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const {

Check failure on line 77 in src/creatures/appearance/outfit/outfit.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

no declaration matches ‘std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t, uint16_t) const’

Check failure on line 77 in src/creatures/appearance/outfit/outfit.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

no declaration matches ‘std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t, uint16_t) 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;
}
Expand All @@ -82,18 +91,7 @@ std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t l
* @param lookType current looktype
* @return <b>const</b> pointer to the outfit or <b>nullptr</b> if it could not be found.
*/

std::shared_ptr<Outfit> Outfits::getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) {
[[maybe_unused]] std::shared_ptr<Outfit> 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);
}
14 changes: 10 additions & 4 deletions src/creatures/appearance/outfit/outfit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,9 +43,10 @@ class Outfits {
return inject<Outfits>();

Check failure on line 43 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘inject’ was not declared in this scope

Check failure on line 43 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘inject’ was not declared in this scope
}

std::shared_ptr<Outfit> getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType);
[[maybe_unused]] std::shared_ptr<Outfit> getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const;

Check failure on line 46 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘PlayerSex_t’ has not been declared

Check failure on line 46 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘PlayerSex_t’ has not been declared

bool loadFromXml();
bool reload();

[[nodiscard]] std::shared_ptr<Outfit> getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const;

Check failure on line 51 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘PlayerSex_t’ has not been declared

Check failure on line 51 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘PlayerSex_t’ has not been declared
[[nodiscard]] const std::vector<std::shared_ptr<Outfit>> &getOutfits(PlayerSex_t sex) const {

Check failure on line 52 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘PlayerSex_t’ has not been declared

Check failure on line 52 in src/creatures/appearance/outfit/outfit.hpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘PlayerSex_t’ has not been declared
Expand Down
27 changes: 0 additions & 27 deletions src/creatures/creatures_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1539,19 +1525,6 @@ using ItemsTierCountList = std::map<uint16_t, std::map<uint8_t, uint32_t>>;
| ...
*/

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) { }
Expand Down
31 changes: 23 additions & 8 deletions src/creatures/players/grouping/familiars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Familiars>();
}

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";
Expand All @@ -35,7 +47,7 @@ bool Familiars::loadFromXml() {
continue;
}

uint16_t vocation = pugi::cast<uint16_t>(attr.value());
auto vocation = pugi::cast<uint16_t>(attr.value());
if (vocation > VOCATION_LAST) {
g_logger().warn("[Familiars::loadFromXml] - Invalid familiar vocation {}", vocation);
continue;
Expand All @@ -47,25 +59,28 @@ bool Familiars::loadFromXml() {
continue;
}

familiars[vocation].emplace_back(
familiars[vocation].emplace_back(std::make_shared<Familiar>(
familiarsNode.attribute("name").as_string(),
pugi::cast<uint16_t>(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();
}
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<Familiar> 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;
}
34 changes: 26 additions & 8 deletions src/creatures/players/grouping/familiars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Familiars>();
}
static Familiars &getInstance();

bool loadFromXml();
const std::vector<Familiar> &getFamiliars(uint16_t vocation) const {
bool reload();

std::vector<std::shared_ptr<Familiar>> &getFamiliars(uint16_t vocation) {
return familiars[vocation];
}
const Familiar* getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const;

[[nodiscard]] std::shared_ptr<Familiar> getFamiliarByLookType(uint16_t vocation, uint16_t lookType) const;

private:
std::vector<Familiar> familiars[VOCATION_LAST + 1];
std::vector<std::shared_ptr<Familiar>> familiars[VOCATION_LAST + 1];
};
16 changes: 9 additions & 7 deletions src/creatures/players/grouping/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PlayerFlags_t Groups::getFlagFromNumber(uint8_t value) {
return magic_enum::enum_value<PlayerFlags_t>(value);
}

bool Groups::reload() const {
bool Groups::reload() {
// Clear groups
g_game().groups.getGroups().clear();
return g_game().groups.load();
Expand Down Expand Up @@ -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>(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<Group> 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;
}
Loading

0 comments on commit 03aac62

Please sign in to comment.