Skip to content

Commit

Permalink
ConfigMangaer to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Apr 30, 2024
1 parent fd5c373 commit 2487aa3
Show file tree
Hide file tree
Showing 40 changed files with 447 additions and 500 deletions.
7 changes: 3 additions & 4 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
extern Game g_game;
extern Spells* g_spells;
extern Actions* g_actions;
extern ConfigManager g_config;

Actions::Actions() : scriptInterface("Action Interface") { scriptInterface.initState(); }

Expand Down Expand Up @@ -383,15 +382,15 @@ static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t coun

bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
{
player->setNextAction(OTSYS_TIME() + g_config[ConfigKeysInteger::ACTIONS_DELAY_INTERVAL]);
player->setNextAction(OTSYS_TIME() + getInteger(ConfigManager::ACTIONS_DELAY_INTERVAL));

if (isHotkey) {
uint16_t subType = item->getSubType();
showUseHotkeyMessage(player, item,
player->getItemTypeCount(item->getID(), subType != item->getItemCount() ? subType : -1));
}

if (g_config[ConfigKeysBoolean::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS]) {
if (getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (HouseTile* houseTile = dynamic_cast<HouseTile*>(item->getTile())) {
House* house = houseTile->getHouse();
if (house && !house->isInvited(player)) {
Expand All @@ -418,7 +417,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
bool Actions::useItemEx(Player* player, const Position& fromPos, const Position& toPos, uint8_t toStackPos, Item* item,
bool isHotkey, Creature* creature /* = nullptr*/)
{
player->setNextAction(OTSYS_TIME() + g_config[ConfigKeysInteger::EX_ACTIONS_DELAY_INTERVAL]);
player->setNextAction(OTSYS_TIME() + getInteger(ConfigManager::EX_ACTIONS_DELAY_INTERVAL));

Action* action = getAction(item);
if (!action) {
Expand Down
3 changes: 1 addition & 2 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

extern Game g_game;
extern Weapons* g_weapons;
extern ConfigManager g_config;
extern Events* g_events;

std::vector<Tile*> getList(const MatrixArea& area, const Position& targetPos, const Direction dir)
Expand Down Expand Up @@ -271,7 +270,7 @@ bool Combat::isInPvpZone(const Creature* attacker, const Creature* target)

bool Combat::isProtected(const Player* attacker, const Player* target)
{
const int64_t protectionLevel = g_config[ConfigKeysInteger::PROTECTION_LEVEL];
const int64_t protectionLevel = getInteger(ConfigManager::PROTECTION_LEVEL);
if (target->getLevel() < protectionLevel || attacker->getLevel() < protectionLevel) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "game.h"

extern Game g_game;
extern ConfigManager g_config;

bool Condition::setParam(ConditionParam_t param, int32_t value)
{
Expand Down Expand Up @@ -1029,7 +1028,7 @@ bool ConditionRegeneration::executeCondition(Creature* creature, int32_t interva
player->sendTextMessage(message);

g_game.addAnimatedText(fmt::format("{:+d}", realHealthGain), player->getPosition(),
static_cast<TextColor_t>(g_config[ConfigKeysInteger::HEALTH_GAIN_COLOUR]));
static_cast<TextColor_t>(getInteger(ConfigManager::HEALTH_GAIN_COLOUR)));

SpectatorVec spectators;
g_game.map.getSpectators(spectators, player->getPosition(), false, true);
Expand Down Expand Up @@ -1067,7 +1066,7 @@ bool ConditionRegeneration::executeCondition(Creature* creature, int32_t interva
player->sendTextMessage(message);

g_game.addAnimatedText(fmt::format("{:+d}", realManaGain), player->getPosition(),
static_cast<TextColor_t>(g_config[ConfigKeysInteger::MANA_GAIN_COLOUR]));
static_cast<TextColor_t>(getInteger(ConfigManager::MANA_GAIN_COLOUR)));

SpectatorVec spectators;
g_game.map.getSpectators(spectators, player->getPosition(), false, true);
Expand Down
293 changes: 145 additions & 148 deletions src/configmanager.cpp

Large diffs are not rendered by default.

68 changes: 18 additions & 50 deletions src/configmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#ifndef FS_CONFIGMANAGER_H
#define FS_CONFIGMANAGER_H

using ExperienceStages = std::vector<std::tuple<uint32_t, uint32_t, float>>;
using OTCFeatures = std::vector<uint8_t>;
namespace ConfigManager {

enum class ConfigKeysBoolean
enum Boolean
{
ALLOW_CHANGEOUTFIT,
ONE_PLAYER_ON_ACCOUNT,
Expand Down Expand Up @@ -47,11 +46,12 @@ enum class ConfigKeysBoolean
REMOVE_ON_DESPAWN,
MONSTER_OVERSPAWN,
ACCOUNT_MANAGER,
MANASHIELD_BREAKABLE,

LAST /* this must be the last one */
LAST_BOOLEAN /* this must be the last one */
};

enum class ConfigKeysString
enum String
{
MAP_NAME,
HOUSE_RENT_PERIOD,
Expand All @@ -72,10 +72,10 @@ enum class ConfigKeysString
MAP_AUTHOR,
CONFIG_FILE,

LAST /* this must be the last one */
LAST_STRING /* this must be the last one */
};

enum class ConfigKeysInteger
enum Integer
{
SQL_PORT,
MAX_PLAYERS,
Expand Down Expand Up @@ -128,52 +128,20 @@ enum class ConfigKeysInteger
RANGE_USE_ITEM_EX_INTERVAL,
RANGE_ROTATE_ITEM_INTERVAL,

LAST /* this must be the last one */
LAST_INTEGER /* this must be the last one */
};

template <typename T, typename E>
struct ConfigValues
{
using Container = std::array<T, static_cast<int>(E::LAST)>;

public:
T& operator[](E key) { return values[static_cast<int>(key)]; }
const T& operator[](E key) const { return values[static_cast<int>(key)]; }

private:
Container values;
};

class ConfigManager
{
public:
ConfigManager();
bool load();

bool load();
bool getBoolean(Boolean what);
std::string_view getString(String what);
int64_t getInteger(Integer what);
float getExperienceStage(uint32_t level);
const std::vector<uint8_t>& getOTCFeatures();

bool getBoolean(ConfigKeysBoolean what) const;
std::string_view getString(ConfigKeysString what) const;
int64_t getInteger(ConfigKeysInteger what) const;
float getExperienceStage(const uint32_t level) const;
OTCFeatures getOTCFeatures() const;

bool setBoolean(ConfigKeysBoolean what, const bool value);
bool setString(ConfigKeysString what, std::string_view value);
bool setInteger(ConfigKeysInteger what, const int64_t value);

auto operator[](ConfigKeysBoolean what) const { return getBoolean(what); }
auto operator[](ConfigKeysString what) const { return getString(what); }
auto operator[](ConfigKeysInteger what) const { return getInteger(what); }

private:
ConfigValues<bool, ConfigKeysBoolean> booleans;
ConfigValues<std::string, ConfigKeysString> strings;
ConfigValues<int64_t, ConfigKeysInteger> integers;

ExperienceStages expStages = {};
OTCFeatures otcFeatures = {};

bool loaded = false;
};
bool setBoolean(Boolean what, bool value);
bool setString(String what, std::string_view value);
bool setInteger(Integer what, int64_t value);
} // namespace ConfigManager

#endif // FS_CONFIGMANAGER_H
4 changes: 1 addition & 3 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "scheduler.h"
#include "server.h"

extern ConfigManager g_config;

Connection_ptr ConnectionManager::createConnection(boost::asio::io_service& io_service,
ConstServicePort_ptr servicePort)
{
Expand Down Expand Up @@ -129,7 +127,7 @@ void Connection::parseHeader(const boost::system::error_code& error)
}

uint32_t timePassed = std::max<uint32_t>(1, (time(nullptr) - timeConnected) + 1);
if ((++packetsSent / timePassed) > g_config[ConfigKeysInteger::MAX_PACKETS_PER_SECOND]) {
if ((++packetsSent / timePassed) > getInteger(ConfigManager::MAX_PACKETS_PER_SECOND)) {
std::cout << convertIPToString(getIP()) << " disconnected for exceeding packet per second limit." << std::endl;
close();
return;
Expand Down
28 changes: 20 additions & 8 deletions src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Item* Container::clone() const
return clone;
}

Container* Container::getParentContainer()
Container* Container::getParentContainer() const
{
Thing* thing = getParent();
if (!thing) {
Expand Down Expand Up @@ -135,7 +135,7 @@ bool Container::isHoldingItem(const Item* item) const
return false;
}

void Container::onAddContainerItem(Item* item)
void Container::onAddContainerItem(Item* item) const
{
SpectatorVec spectators;
g_game.map.getSpectators(spectators, getPosition(), false, true, 1, 1, 1, 1);
Expand All @@ -153,7 +153,7 @@ void Container::onAddContainerItem(Item* item)
}
}

void Container::onUpdateContainerItem(uint32_t index, Item* oldItem, Item* newItem)
void Container::onUpdateContainerItem(uint32_t index, Item* oldItem, Item* newItem) const
{
SpectatorVec spectators;
g_game.map.getSpectators(spectators, getPosition(), false, true, 1, 1, 1, 1);
Expand All @@ -171,7 +171,7 @@ void Container::onUpdateContainerItem(uint32_t index, Item* oldItem, Item* newIt
}
}

void Container::onRemoveContainerItem(uint32_t index, Item* item)
void Container::onRemoveContainerItem(uint32_t index, Item* item) const
{
SpectatorVec spectators;
g_game.map.getSpectators(spectators, getPosition(), false, true, 1, 1, 1, 1);
Expand Down Expand Up @@ -236,7 +236,15 @@ ReturnValue Container::queryAdd(int32_t index, const Thing& thing, uint32_t coun
}
}

const Cylinder* topParent = getTopParent();
const Cylinder* const topParent = getTopParent();
if (actor && getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (const HouseTile* houseTile = dynamic_cast<const HouseTile*>(topParent->getTile())) {
if (!topParent->getCreature() && !houseTile->getHouse()->isInvited(actor->getPlayer())) {
return RETURNVALUE_PLAYERISNOTINVITED;
}
}
}

if (topParent != this) {
return topParent->queryAdd(INDEX_WHEREEVER, *item, count, flags | FLAG_CHILDISOWNER, actor);
}
Expand Down Expand Up @@ -316,9 +324,13 @@ ReturnValue Container::queryRemove(const Thing& thing, uint32_t count, uint32_t
return RETURNVALUE_NOTMOVEABLE;
}

const HouseTile* houseTile = dynamic_cast<const HouseTile*>(getTopParent());
if (houseTile) {
return houseTile->queryRemove(thing, count, flags, actor);
if (actor && getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
const Cylinder* const topParent = getTopParent();
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(topParent->getTile())) {
if (!topParent->getCreature() && !houseTile->getHouse()->isInvited(actor->getPlayer())) {
return RETURNVALUE_PLAYERISNOTINVITED;
}
}
}

return RETURNVALUE_NOERROR;
Expand Down
8 changes: 4 additions & 4 deletions src/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ class Container : public Item, public Cylinder
uint32_t totalWeight = 0;
uint32_t serializationCount = 0;

void onAddContainerItem(Item* item);
void onUpdateContainerItem(uint32_t index, Item* oldItem, Item* newItem);
void onRemoveContainerItem(uint32_t index, Item* item);
void onAddContainerItem(Item* item) const;
void onUpdateContainerItem(uint32_t index, Item* oldItem, Item* newItem) const;
void onRemoveContainerItem(uint32_t index, Item* item) const;

Container* getParentContainer();
Container* getParentContainer() const;
void updateItemWeight(int32_t diff);

friend class ContainerIterator;
Expand Down
7 changes: 3 additions & 4 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "monster.h"
#include "scheduler.h"

extern ConfigManager g_config;
extern CreatureEvents* g_creatureEvents;
extern Events* g_events;
extern Game g_game;
Expand Down Expand Up @@ -635,7 +634,7 @@ CreatureVector Creature::getKillers()
{
CreatureVector killers;
const int64_t timeNow = OTSYS_TIME();
const int64_t inFightTicks = g_config[ConfigKeysInteger::PZ_LOCKED];
const int64_t inFightTicks = getInteger(ConfigManager::PZ_LOCKED);
for (const auto& it : damageMap) {
Creature* attacker = g_game.getCreatureByID(it.first);
if (attacker && attacker != this && timeNow - it.second.ticks <= inFightTicks) {
Expand All @@ -661,7 +660,7 @@ void Creature::onDeath()
Creature* mostDamageCreature = nullptr;

const int64_t timeNow = OTSYS_TIME();
const int64_t inFightTicks = g_config[ConfigKeysInteger::PZ_LOCKED];
const int64_t inFightTicks = getInteger(ConfigManager::PZ_LOCKED);
int32_t mostDamage = 0;
std::map<Creature*, uint64_t> experienceMap;
for (const auto& it : damageMap) {
Expand Down Expand Up @@ -787,7 +786,7 @@ bool Creature::hasBeenAttacked(uint32_t attackerId)
if (it == damageMap.end()) {
return false;
}
return (OTSYS_TIME() - it->second.ticks) <= g_config[ConfigKeysInteger::PZ_LOCKED];
return (OTSYS_TIME() - it->second.ticks) <= getInteger(ConfigManager::PZ_LOCKED);
}

Item* Creature::getCorpse(Creature*, Creature*) { return Item::CreateItem(getLookCorpse()); }
Expand Down
10 changes: 4 additions & 6 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <mysql/errmsg.h>

extern ConfigManager g_config;

static bool connectToDatabase(MYSQL*& handle, const bool retryIfError)
{
bool isFirstAttemptToConnect = true;
Expand All @@ -30,10 +28,10 @@ static bool connectToDatabase(MYSQL*& handle, const bool retryIfError)
goto error;
}
// connects to database
if (!mysql_real_connect(handle, g_config[ConfigKeysString::MYSQL_HOST].data(),
g_config[ConfigKeysString::MYSQL_USER].data(),
g_config[ConfigKeysString::MYSQL_PASS].data(), g_config[ConfigKeysString::MYSQL_DB].data(),
g_config[ConfigKeysInteger::SQL_PORT], g_config[ConfigKeysString::MYSQL_SOCK].data(), 0)) {
if (!mysql_real_connect(handle, getString(ConfigManager::MYSQL_HOST).data(),
getString(ConfigManager::MYSQL_USER).data(), getString(ConfigManager::MYSQL_PASS).data(),
getString(ConfigManager::MYSQL_DB).data(), getInteger(ConfigManager::SQL_PORT),
getString(ConfigManager::MYSQL_SOCK).data(), 0)) {
std::cout << std::endl << "MySQL Error Message: " << mysql_error(handle) << std::endl;
goto error;
}
Expand Down
8 changes: 3 additions & 5 deletions src/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
#include "configmanager.h"
#include "luascript.h"

extern ConfigManager g_config;

bool DatabaseManager::optimizeTables()
{
Database& db = Database::getInstance();

DBResult_ptr result = db.storeQuery(fmt::format(
"SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = {:s} AND `DATA_FREE` > 0",
db.escapeString(g_config[ConfigKeysString::MYSQL_DB])));
db.escapeString(getString(ConfigManager::MYSQL_DB))));
if (!result) {
return false;
}
Expand All @@ -40,7 +38,7 @@ bool DatabaseManager::tableExists(std::string_view tableName)
return db
.storeQuery(fmt::format(
"SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = {:s} AND `TABLE_NAME` = {:s} LIMIT 1",
db.escapeString(g_config[ConfigKeysString::MYSQL_DB]), db.escapeString(tableName)))
db.escapeString(getString(ConfigManager::MYSQL_DB)), db.escapeString(tableName)))
.get() != nullptr;
}

Expand All @@ -49,7 +47,7 @@ bool DatabaseManager::isDatabaseSetup()
Database& db = Database::getInstance();
return db.storeQuery(
fmt::format("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = {:s}",
db.escapeString(g_config[ConfigKeysString::MYSQL_DB])))
db.escapeString(getString(ConfigManager::MYSQL_DB))))
.get() != nullptr;
}

Expand Down
Loading

0 comments on commit 2487aa3

Please sign in to comment.