Skip to content

Commit

Permalink
improve: ConfigManager better functionality/code clarity/maintainabil…
Browse files Browse the repository at this point in the history
…ity (#1887)

Revamp of the ConfigManager component, aimed at not only improving functionality and code clarity but also enhancing long-term maintainability.

• Changes Made:
◦ Extensively restructured the ConfigManager to optimize functionality, making it more robust and versatile.
◦ Focused on enhancing code clarity, making the codebase more comprehensible and maintainable for future contributors.
◦ Introduced magic enums to streamline enum registration for the Lua interface, improving maintainability and reducing potential issues.
  • Loading branch information
beats-dh authored Nov 23, 2023
1 parent 125c5f4 commit 94f9769
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 686 deletions.
29 changes: 2 additions & 27 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#pragma once

// Enum
enum booleanConfig_t {
enum ConfigKey_t : uint16_t {
ALLOW_CHANGEOUTFIT,
ONE_PLAYER_ON_ACCOUNT,
AIMBOT_HOTKEY_ENABLED,
Expand Down Expand Up @@ -91,11 +91,6 @@ enum booleanConfig_t {
TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART,
TOGGLE_RECEIVE_REWARD,
TOGGLE_MAINTAIN_MODE,

LAST_BOOLEAN_CONFIG
};

enum stringConfig_t {
MAP_NAME,
MAP_DOWNLOAD_URL,
MAP_AUTHOR,
Expand Down Expand Up @@ -128,11 +123,6 @@ enum stringConfig_t {
TIBIADROME_CONCOCTION_TICK_TYPE,
M_CONST,
MAINTAIN_MODE_MESSAGE,

LAST_STRING_CONFIG
};

enum integerConfig_t {
SQL_PORT,
MAX_PLAYERS,
PZ_LOCKED,
Expand Down Expand Up @@ -251,53 +241,38 @@ enum integerConfig_t {
PARALLELISM,
BOSS_DEFAULT_TIME_TO_FIGHT_AGAIN,
BOSS_DEFAULT_TIME_TO_DEFEAT,

VIP_BONUS_EXP,
VIP_BONUS_LOOT,
VIP_BONUS_SKILL,
VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION,

REWARD_CHEST_MAX_COLLECT_ITEMS,
DISCORD_WEBHOOK_DELAY_MS,

PVP_MAX_LEVEL_DIFFERENCE,

LAST_INTEGER_CONFIG
};

enum floatingConfig_t {
BESTIARY_RATE_CHARM_SHOP_PRICE,

RATE_HEALTH_REGEN,
RATE_HEALTH_REGEN_SPEED,
RATE_MANA_REGEN,
RATE_MANA_REGEN_SPEED,
RATE_SOUL_REGEN,
RATE_SOUL_REGEN_SPEED,

RATE_SPELL_COOLDOWN,
RATE_ATTACK_SPEED,
RATE_OFFLINE_TRAINING_SPEED,
RATE_EXERCISE_TRAINING_SPEED,

RATE_MONSTER_HEALTH,
RATE_MONSTER_ATTACK,
RATE_MONSTER_DEFENSE,
RATE_BOSS_HEALTH,
RATE_BOSS_ATTACK,
RATE_BOSS_DEFENSE,

RATE_NPC_HEALTH,
RATE_NPC_ATTACK,
RATE_NPC_DEFENSE,
LOYALTY_BONUS_PERCENTAGE_MULTIPLIER,
PARTY_SHARE_LOOT_BOOSTS_DIMINISHING_FACTOR,

PVP_RATE_DAMAGE_TAKEN_PER_LEVEL,
PVP_RATE_DAMAGE_REDUCTION_PER_LEVEL,

HOUSE_PRICE_RENT_MULTIPLIER,
HOUSE_RENT_RATE,

LAST_FLOATING_CONFIG
LOGLEVEL,
};
741 changes: 360 additions & 381 deletions src/config/configmanager.cpp

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions src/config/configmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "declarations.hpp"
#include "lib/di/container.hpp"

using ConfigValue = std::variant<std::string, int32_t, bool, float>;

class ConfigManager {
public:
ConfigManager() = default;
Expand All @@ -27,28 +29,27 @@ class ConfigManager {
bool load();
bool reload();

const std::string &getString(stringConfig_t what) const;
int32_t getNumber(integerConfig_t what) const;
int16_t getShortNumber(integerConfig_t what) const;
bool getBoolean(booleanConfig_t what) const;
float getFloat(floatingConfig_t what) const;

const std::string &setConfigFileLua(const std::string &what) {
configFileLua = { what };
return configFileLua;
};
const std::string &getConfigFileLua() const {
[[nodiscard]] const std::string &getConfigFileLua() const {
return configFileLua;
};

private:
std::string configFileLua = { "config.lua" };
[[nodiscard]] const std::string &getString(const ConfigKey_t &key) const;
[[nodiscard]] int32_t getNumber(const ConfigKey_t &key) const;
[[nodiscard]] bool getBoolean(const ConfigKey_t &key) const;
[[nodiscard]] float getFloat(const ConfigKey_t &key) const;

std::string string[LAST_STRING_CONFIG] = {};
int32_t integer[LAST_INTEGER_CONFIG] = {};
bool boolean[LAST_BOOLEAN_CONFIG] = {};
float floating[LAST_FLOATING_CONFIG] = {};
private:
phmap::flat_hash_map<ConfigKey_t, ConfigValue> configs;
std::string loadStringConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const std::string &defaultValue);
int32_t loadIntConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const int32_t &defaultValue);
bool loadBoolConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const bool &defaultValue);
float loadFloatConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const float &defaultValue);

std::string configFileLua = { "config.lua" };
bool loaded = false;
};

Expand Down
Loading

0 comments on commit 94f9769

Please sign in to comment.