Skip to content

Commit 9d315a4

Browse files
committed
Move config manager singleton to namespace
1 parent 2cdf1de commit 9d315a4

36 files changed

Lines changed: 318 additions & 378 deletions

src/actions.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
extern Game g_game;
1717
extern Spells* g_spells;
1818
extern Actions* g_actions;
19-
extern ConfigManager g_config;
2019

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

@@ -403,7 +402,7 @@ static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t coun
403402

404403
bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
405404
{
406-
int32_t cooldown = g_config.getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL);
405+
int32_t cooldown = getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL);
407406
player->setNextAction(OTSYS_TIME() + cooldown);
408407
player->sendUseItemCooldown(cooldown);
409408
if (item->isSupply()) {
@@ -416,7 +415,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
416415
player->getItemTypeCount(item->getID(), subType != item->getItemCount() ? subType : -1));
417416
}
418417

419-
if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
418+
if (getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
420419
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
421420
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
422421
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);
@@ -442,7 +441,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
442441
bool Actions::useItemEx(Player* player, const Position& fromPos, const Position& toPos, uint8_t toStackPos, Item* item,
443442
bool isHotkey, Creature* creature /* = nullptr*/)
444443
{
445-
int32_t cooldown = g_config.getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL);
444+
int32_t cooldown = getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL);
446445
player->setNextAction(OTSYS_TIME() + cooldown);
447446
player->sendUseItemCooldown(cooldown);
448447

@@ -464,7 +463,7 @@ bool Actions::useItemEx(Player* player, const Position& fromPos, const Position&
464463
player->getItemTypeCount(item->getID(), subType != item->getItemCount() ? subType : -1));
465464
}
466465

467-
if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
466+
if (getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
468467
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
469468
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
470469
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);

src/combat.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
extern Game g_game;
1616
extern Weapons* g_weapons;
17-
extern ConfigManager g_config;
1817
extern Events* g_events;
1918

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

273272
bool Combat::isProtected(const Player* attacker, const Player* target)
274273
{
275-
uint32_t protectionLevel = g_config.getNumber(ConfigManager::PROTECTION_LEVEL);
274+
uint32_t protectionLevel = getNumber(ConfigManager::PROTECTION_LEVEL);
276275
if (target->getLevel() < protectionLevel || attacker->getLevel() < protectionLevel) {
277276
return true;
278277
}

src/configmanager.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ extern Game g_game;
2525

2626
namespace {
2727

28+
std::array<std::string, ConfigManager::LAST_STRING_CONFIG> string = {};
29+
std::array<int32_t, ConfigManager::LAST_INTEGER_CONFIG> integer = {};
30+
std::array<bool, ConfigManager::LAST_BOOLEAN_CONFIG> boolean = {};
31+
32+
using ExperienceStages = std::vector<std::tuple<uint32_t, uint32_t, float>>;
33+
ExperienceStages expStages;
34+
35+
bool loaded = false;
36+
2837
template <typename T>
2938
auto getEnv(const char* envVar, T&& defaultValue)
3039
{
@@ -81,12 +90,6 @@ bool getGlobalBoolean(lua_State* L, const char* identifier, const bool defaultVa
8190
return val != 0;
8291
}
8392

84-
} // namespace
85-
86-
ConfigManager::ConfigManager() { string[CONFIG_FILE] = "config.lua"; }
87-
88-
namespace {
89-
9093
ExperienceStages loadLuaStages(lua_State* L)
9194
{
9295
ExperienceStages stages;
@@ -161,7 +164,8 @@ bool ConfigManager::load()
161164

162165
luaL_openlibs(L);
163166

164-
if (luaL_dofile(L, getString(CONFIG_FILE).c_str())) {
167+
string[CONFIG_FILE] = "config.lua";
168+
if (luaL_dofile(L, string[CONFIG_FILE].data())) {
165169
std::cout << "[Error - ConfigManager::load] " << lua_tostring(L, -1) << std::endl;
166170
lua_close(L);
167171
return false;
@@ -302,18 +306,17 @@ bool ConfigManager::load()
302306
return true;
303307
}
304308

305-
static std::string dummyStr;
306-
307-
const std::string& ConfigManager::getString(string_config_t what) const
309+
const std::string& ConfigManager::getString(string_config_t what)
308310
{
311+
static std::string dummyStr;
309312
if (what >= LAST_STRING_CONFIG) {
310313
std::cout << "[Warning - ConfigManager::getString] Accessing invalid index: " << what << std::endl;
311314
return dummyStr;
312315
}
313316
return string[what];
314317
}
315318

316-
int32_t ConfigManager::getNumber(integer_config_t what) const
319+
int32_t ConfigManager::getNumber(integer_config_t what)
317320
{
318321
if (what >= LAST_INTEGER_CONFIG) {
319322
std::cout << "[Warning - ConfigManager::getNumber] Accessing invalid index: " << what << std::endl;
@@ -322,7 +325,7 @@ int32_t ConfigManager::getNumber(integer_config_t what) const
322325
return integer[what];
323326
}
324327

325-
bool ConfigManager::getBoolean(boolean_config_t what) const
328+
bool ConfigManager::getBoolean(boolean_config_t what)
326329
{
327330
if (what >= LAST_BOOLEAN_CONFIG) {
328331
std::cout << "[Warning - ConfigManager::getBoolean] Accessing invalid index: " << what << std::endl;
@@ -331,7 +334,7 @@ bool ConfigManager::getBoolean(boolean_config_t what) const
331334
return boolean[what];
332335
}
333336

334-
float ConfigManager::getExperienceStage(uint32_t level) const
337+
float ConfigManager::getExperienceStage(uint32_t level)
335338
{
336339
auto it = std::find_if(expStages.begin(), expStages.end(), [level](auto&& stage) {
337340
auto&& [minLevel, maxLevel, _] = stage;

0 commit comments

Comments
 (0)