1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-28 07:07:45 +00:00
sapphire/src/common/Config/ConfigMgr.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2018-06-10 16:28:03 +00:00
#include "ConfigMgr.h"
#include <boost/property_tree/ini_parser.hpp>
#include <boost/filesystem.hpp>
/**
* Loads an ini file and parses it
* @param configName the name of ini file relative to m_configFolderRoot to load alongside global.ini
* @return true if loading was successful
*/
bool Core::ConfigMgr::loadConfig( const std::string& configName )
{
std::stringstream configStream;
// get global config
auto configDir = boost::filesystem::path( m_configFolderRoot );
if( !boost::filesystem::exists( configDir ) )
{
return false;
}
auto globalConfig = boost::filesystem::path( configDir / m_globalConfigFile );
if( !boost::filesystem::exists( globalConfig ) )
{
if( !copyDefaultConfig( globalConfig.filename().string() ) )
return false;
}
std::ifstream globalConfigFile( globalConfig.c_str() );
configStream << globalConfigFile.rdbuf();
// add some newlines just in case there's no newline at the end of the global file
configStream << "\n\n";
// get local config
auto localConfig = boost::filesystem::path( configDir / configName );
if( !boost::filesystem::exists( localConfig ) )
{
if( !copyDefaultConfig( localConfig.filename().string() ) )
return false;
}
std::ifstream configFile( localConfig.c_str() );
configStream << configFile.rdbuf();
// parse the tree and we're fuckin done
boost::property_tree::read_ini( configStream, m_propTree );
return true;
}
bool Core::ConfigMgr::copyDefaultConfig( const std::string& configName )
{
boost::filesystem::path configPath( m_configFolderRoot );
configPath /= configName;
2018-06-10 17:17:16 +00:00
if( !boost::filesystem::exists( configPath.string() + m_configDefaultSuffix ) )
2018-06-10 16:28:03 +00:00
{
// no default file :(
return false;
}
2018-06-10 17:17:16 +00:00
boost::filesystem::copy_file( configPath.string() + m_configDefaultSuffix, configPath );
2018-06-10 16:28:03 +00:00
return true;
}