Skip to content

Commit

Permalink
Merge pull request #179 from namreeb/config
Browse files Browse the repository at this point in the history
Rewrote config parser from scratch to remove ACE
  • Loading branch information
namreeb committed Mar 5, 2016
2 parents 089dc87 + ba1aa10 commit 564071d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/game/AuctionHouseBot/AuctionHouseBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ void AuctionBotConfig::GetConfigFromFile()
setConfigMax(CONFIG_UINT32_AHBOT_HORDE_ITEM_AMOUNT_RATIO , "AuctionHouseBot.Horde.Items.Amount.Ratio" , 100, 10000);
setConfigMax(CONFIG_UINT32_AHBOT_NEUTRAL_ITEM_AMOUNT_RATIO , "AuctionHouseBot.Neutral.Items.Amount.Ratio" , 100, 10000);

SetAHBotIncludes(m_AhBotCfg.GetStringDefault("AuctionHouseBot.forceIncludeItems", ""));
SetAHBotExcludes(m_AhBotCfg.GetStringDefault("AuctionHouseBot.forceExcludeItems", ""));
SetAHBotIncludes(m_AhBotCfg.GetStringDefault("AuctionHouseBot.forceIncludeItems"));
SetAHBotExcludes(m_AhBotCfg.GetStringDefault("AuctionHouseBot.forceExcludeItems"));

setConfig(CONFIG_BOOL_AHBOT_BUYER_ALLIANCE_ENABLED , "AuctionHouseBot.Buyer.Alliance.Enabled" , false);
setConfig(CONFIG_BOOL_AHBOT_BUYER_HORDE_ENABLED , "AuctionHouseBot.Buyer.Horde.Enabled" , false);
Expand Down
6 changes: 3 additions & 3 deletions src/game/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ void World::LoadConfigSettings(bool reload)
setConfig(CONFIG_BOOL_CLEAN_CHARACTER_DB, "CleanCharacterDB", true);
setConfig(CONFIG_BOOL_GRID_UNLOAD, "GridUnload", true);

std::string forceLoadGridOnMaps = sConfig.GetStringDefault("LoadAllGridsOnMaps", "");
std::string forceLoadGridOnMaps = sConfig.GetStringDefault("LoadAllGridsOnMaps");
if (!forceLoadGridOnMaps.empty())
{
unsigned int pos = 0;
Expand Down Expand Up @@ -849,7 +849,7 @@ void World::LoadConfigSettings(bool reload)
setConfig(CONFIG_BOOL_VMAP_INDOOR_CHECK, "vmap.enableIndoorCheck", true);
bool enableLOS = sConfig.GetBoolDefault("vmap.enableLOS", false);
bool enableHeight = sConfig.GetBoolDefault("vmap.enableHeight", false);
std::string ignoreSpellIds = sConfig.GetStringDefault("vmap.ignoreSpellIds", "");
std::string ignoreSpellIds = sConfig.GetStringDefault("vmap.ignoreSpellIds");

if (!enableHeight)
sLog.outError("VMAP height use disabled! Creatures movements and other things will be in broken state.");
Expand All @@ -862,7 +862,7 @@ void World::LoadConfigSettings(bool reload)
sLog.outString("WORLD: VMap data directory is: %svmaps", m_dataPath.c_str());

setConfig(CONFIG_BOOL_MMAP_ENABLED, "mmap.enabled", true);
std::string ignoreMapIds = sConfig.GetStringDefault("mmap.ignoreMapIds", "");
std::string ignoreMapIds = sConfig.GetStringDefault("mmap.ignoreMapIds");
MMAP::MMapFactory::preventPathfindingOnMaps(ignoreMapIds.c_str());
sLog.outString("WORLD: MMap pathfinding %sabled", getConfig(CONFIG_BOOL_MMAP_ENABLED) ? "en" : "dis");

Expand Down
8 changes: 4 additions & 4 deletions src/mangosd/Master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Master::~Master()
int Master::Run()
{
/// worldd PID file creation
std::string pidfile = sConfig.GetStringDefault("PidFile", "");
std::string pidfile = sConfig.GetStringDefault("PidFile");
if (!pidfile.empty())
{
uint32 pid = CreatePIDFile(pidfile);
Expand Down Expand Up @@ -420,7 +420,7 @@ int Master::Run()
bool Master::_StartDB()
{
///- Get world database info from configuration file
std::string dbstring = sConfig.GetStringDefault("WorldDatabaseInfo", "");
std::string dbstring = sConfig.GetStringDefault("WorldDatabaseInfo");
int nConnections = sConfig.GetIntDefault("WorldDatabaseConnections", 1);
if (dbstring.empty())
{
Expand All @@ -443,7 +443,7 @@ bool Master::_StartDB()
return false;
}

dbstring = sConfig.GetStringDefault("CharacterDatabaseInfo", "");
dbstring = sConfig.GetStringDefault("CharacterDatabaseInfo");
nConnections = sConfig.GetIntDefault("CharacterDatabaseConnections", 1);
if (dbstring.empty())
{
Expand Down Expand Up @@ -474,7 +474,7 @@ bool Master::_StartDB()
}

///- Get login database info from configuration file
dbstring = sConfig.GetStringDefault("LoginDatabaseInfo", "");
dbstring = sConfig.GetStringDefault("LoginDatabaseInfo");
nConnections = sConfig.GetIntDefault("LoginDatabaseConnections", 1);
if (dbstring.empty())
{
Expand Down
4 changes: 2 additions & 2 deletions src/realmd/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ extern int main(int argc, char** argv)
sLog.outBasic("Max allowed open files is %d", ACE::max_handles());

/// realmd PID file creation
std::string pidfile = sConfig.GetStringDefault("PidFile", "");
std::string pidfile = sConfig.GetStringDefault("PidFile");
if (!pidfile.empty())
{
uint32 pid = CreatePIDFile(pidfile);
Expand Down Expand Up @@ -375,7 +375,7 @@ void OnSignal(int s)
/// Initialize connection to the database
bool StartDB()
{
std::string dbstring = sConfig.GetStringDefault("LoginDatabaseInfo", "");
std::string dbstring = sConfig.GetStringDefault("LoginDatabaseInfo");
if (dbstring.empty())
{
sLog.outError("Database not specified");
Expand Down
2 changes: 1 addition & 1 deletion src/scriptdev2/ScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void FillSpellSummary();

void LoadDatabase()
{
std::string strSD2DBinfo = SD2Config.GetStringDefault("WorldDatabaseInfo", "");
std::string strSD2DBinfo = SD2Config.GetStringDefault("WorldDatabaseInfo");

if (strSD2DBinfo.empty())
{
Expand Down
121 changes: 57 additions & 64 deletions src/shared/Config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,96 +17,89 @@
*/

#include "Config.h"
#include "ace/Configuration_Import_Export.h"

#include "Policies/Singleton.h"

#include <boost/algorithm/string.hpp>

#include <unordered_map>
#include <string>
#include <fstream>

INSTANTIATE_SINGLETON_1(Config);

static bool GetValueHelper(ACE_Configuration_Heap* mConf, const char* name, ACE_TString& result)
bool Config::SetSource(const std::string &file)
{
m_filename = file;

return Reload();
}

bool Config::Reload()
{
if (!mConf)
std::ifstream in(m_filename, std::ifstream::in);

if (in.fail())
return false;

ACE_TString section_name;
ACE_Configuration_Section_Key section_key;
ACE_Configuration_Section_Key root_key = mConf->root_section();
std::unordered_map<std::string, std::string> newEntries;

int i = 0;
while (mConf->enumerate_sections(root_key, i, section_name) == 0)
do
{
mConf->open_section(root_key, section_name.c_str(), 0, section_key);
if (mConf->get_string_value(section_key, name, result) == 0)
return true;
++i;
}
std::string line;
std::getline(in, line);

return false;
}
boost::algorithm::trim_left(line);

Config::Config()
: mConf(nullptr)
{
}
if (!line.length())
continue;

Config::~Config()
{
delete mConf;
}
if (line[0] == '#' || line[0] == '[')
continue;

bool Config::SetSource(const char* file)
{
mFilename = file;
auto const equals = line.find('=');
if (equals == std::string::npos)
return false;

return Reload();
}
auto const entry = boost::algorithm::trim_copy(boost::algorithm::to_lower_copy(line.substr(0, equals)));
auto const value = boost::algorithm::trim_copy_if(boost::algorithm::trim_copy(line.substr(equals + 1)), boost::algorithm::is_any_of("\""));

bool Config::Reload()
{
delete mConf;
mConf = new ACE_Configuration_Heap;
newEntries[entry] = value;
} while (in.good());

if (mConf->open() == 0)
{
ACE_Ini_ImpExp config_importer(*mConf);
if (config_importer.import_config(mFilename.c_str()) == 0)
return true;
}

delete mConf;
mConf = nullptr;
return false;
m_entries = std::move(newEntries);

return true;
}

std::string Config::GetStringDefault(const char* name, const char* def)
const std::string &Config::GetStringDefault(const std::string &name, const std::string &def) const
{
ACE_TString val;
return GetValueHelper(mConf, name, val) ? val.c_str() : def;
auto const nameLower = boost::algorithm::to_lower_copy(name);

auto const entry = m_entries.find(nameLower);

return entry == m_entries.cend() ? def : entry->second;
}

bool Config::GetBoolDefault(const char* name, bool def)
bool Config::GetBoolDefault(const std::string &name, bool def) const
{
ACE_TString val;
if (!GetValueHelper(mConf, name, val))
return def;

const char* str = val.c_str();
if (strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 ||
strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 ||
strcmp(str, "1") == 0)
return true;
else
return false;
auto const value = GetStringDefault(name, def ? "true" : "false");

std::string valueLower;
std::transform(value.cbegin(), value.cend(), std::back_inserter(valueLower), ::tolower);

return valueLower == "true" || valueLower == "1" || valueLower == "yes";
}

int32 Config::GetIntDefault(const char* name, int32 def)
int32 Config::GetIntDefault(const std::string &name, int32 def) const
{
ACE_TString val;
return GetValueHelper(mConf, name, val) ? atoi(val.c_str()) : def;
auto const value = GetStringDefault(name, std::to_string(def));

return std::stoi(value);
}

float Config::GetFloatDefault(const char* name, float def)
float Config::GetFloatDefault(const std::string &name, float def) const
{
ACE_TString val;
return GetValueHelper(mConf, name, val) ? (float)atof(val.c_str()) : def;
auto const value = GetStringDefault(name, std::to_string(def));

return std::stof(value);
}
31 changes: 13 additions & 18 deletions src/shared/Config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,27 @@
#define CONFIG_H

#include "Common.h"
#include <Policies/Singleton.h>
#include "Platform/Define.h"
#include "Policies/Singleton.h"

class ACE_Configuration_Heap;
#include <string>
#include <unordered_map>

class MANGOS_DLL_SPEC Config
{
public:

Config();
~Config();
private:
std::string m_filename;
std::unordered_map<std::string, std::string> m_entries; // keys are converted to lower case. values cannot be.

bool SetSource(const char* file);
public:
bool SetSource(const std::string &file);
bool Reload();

std::string GetStringDefault(const char* name, const char* def);
bool GetBoolDefault(const char* name, const bool def = false);
int32 GetIntDefault(const char* name, const int32 def);
float GetFloatDefault(const char* name, const float def);

std::string GetFilename() const { return mFilename; }

private:
const std::string &GetStringDefault(const std::string &name, const std::string &def = "") const;
bool GetBoolDefault(const std::string &name, bool def) const;
int32 GetIntDefault(const std::string &name, int32 def) const;
float GetFloatDefault(const std::string &name, float def) const;

std::string mFilename;
ACE_Configuration_Heap* mConf;
const std::string &GetFilename() const { return m_filename; }
};

#define sConfig MaNGOS::Singleton<Config>::Instance()
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool Database::Initialize(const char* infoString, int nConns /*= 1*/)
// Enable logging of SQL commands (usually only GM commands)
// (See method: PExecuteLog)
m_logSQL = sConfig.GetBoolDefault("LogSQL", false);
m_logsDir = sConfig.GetStringDefault("LogsDir", "");
m_logsDir = sConfig.GetStringDefault("LogsDir");
if (!m_logsDir.empty())
{
if ((m_logsDir.at(m_logsDir.length() - 1) != '/') && (m_logsDir.at(m_logsDir.length() - 1) != '\\'))
Expand Down
8 changes: 4 additions & 4 deletions src/shared/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void Log::SetLogFileLevel(char* level)
void Log::Initialize()
{
/// Common log files data
m_logsDir = sConfig.GetStringDefault("LogsDir", "");
m_logsDir = sConfig.GetStringDefault("LogsDir");
if (!m_logsDir.empty())
{
if ((m_logsDir.at(m_logsDir.length() - 1) != '/') && (m_logsDir.at(m_logsDir.length() - 1) != '\\'))
Expand All @@ -237,7 +237,7 @@ void Log::Initialize()
else
{
// GM log settings for per account case
m_gmlog_filename_format = sConfig.GetStringDefault("GMLogFile", "");
m_gmlog_filename_format = sConfig.GetStringDefault("GMLogFile");
if (!m_gmlog_filename_format.empty())
{
bool m_gmlog_timestamp = sConfig.GetBoolDefault("GmLogTimestamp", false);
Expand Down Expand Up @@ -272,7 +272,7 @@ void Log::Initialize()
m_includeTime = sConfig.GetBoolDefault("LogTime", false);
m_logLevel = LogLevel(sConfig.GetIntDefault("LogLevel", 0));
m_logFileLevel = LogLevel(sConfig.GetIntDefault("LogFileLevel", 0));
InitColors(sConfig.GetStringDefault("LogColors", ""));
InitColors(sConfig.GetStringDefault("LogColors"));

m_logFilter = 0;
for (int i = 0; i < LOG_FILTER_COUNT; ++i)
Expand All @@ -286,7 +286,7 @@ void Log::Initialize()

FILE* Log::openLogFile(char const* configFileName, char const* configTimeStampFlag, char const* mode)
{
std::string logfn = sConfig.GetStringDefault(configFileName, "");
std::string logfn = sConfig.GetStringDefault(configFileName);
if (logfn.empty())
return nullptr;

Expand Down

0 comments on commit 564071d

Please sign in to comment.