Skip to content

Commit

Permalink
Use SettingsManager in a different way:
Browse files Browse the repository at this point in the history
- client and server both define what settings do they want to load (no longer in the engine)
- if config file does not exists it's created a new one
  • Loading branch information
DDuarte committed Jul 8, 2012
1 parent d4ac5ee commit b0ea7ec
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 89 deletions.
16 changes: 13 additions & 3 deletions src/LeClient/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ using boost::shared_ptr;

void OnConfigLoad()
{
LeLog.EnableConsole(GetConfig(LogWriteToConsole));
LeLog.EnableTime(GetConfig(LogWithTimestamp));
LeLog.EnableConsole(GetConfig("logging.write_to_console", bool));
LeLog.EnableTime(GetConfig("logging.with_timestamp", bool));
// TODO: Change used .log files
// LeLog.SetLogFile(GetConfig<std::string>("logging.file));
}

void Application::Run(int argc, char* argv[])
Expand All @@ -26,7 +27,16 @@ void Application::Run(int argc, char* argv[])
if (!LeLog.Init())
return;

new SettingsManager(OnConfigLoad);
const std::string logFile = "client.conf";
SettingsManager* settings = new SettingsManager(logFile, OnConfigLoad);
settings->AddSetting("screen.X", 800);
settings->AddSetting("screen.Y", 600);
settings->AddSetting("screen.BPP", 24);
settings->AddSetting("screen.fullscreen", false);
settings->AddSetting("logging.file", "client.log");
settings->AddSetting("logging.write_to_console", true);
settings->AddSetting("logging.with_timestamp", true);
settings->LoadConfig();

new Kernel();

Expand Down
6 changes: 4 additions & 2 deletions src/LeClient/LeClient.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@
<AdditionalDependencies>glew_static.lib;GLFW.lib;GlU32.Lib;LeEngine.lib;SOIL.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(ProjectDir)settings.conf.dist" "$(SolutionDir)..\bin\settings.conf.dist"</Command>
<Command>
</Command>
</PostBuildEvent>
<PostBuildEvent>
<Message>Copying settings file</Message>
<Message>
</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down
19 changes: 0 additions & 19 deletions src/LeClient/settings.conf.dist

This file was deleted.

55 changes: 16 additions & 39 deletions src/LeEngine/SettingsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
#include <exception>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/filesystem.hpp>

SettingsManager::SettingsManager() : _fileName(SETTINGS_FILENAME), _loaded(false), _onLoadCallback(NULL)
SettingsManager::SettingsManager(const std::string& filename) : _fileName(filename), _loaded(false), _onLoadCallback(NULL)
{
LoadConfig();
}

SettingsManager::SettingsManager(void (*f)()) : _fileName(SETTINGS_FILENAME), _loaded(false), _onLoadCallback(NULL)
SettingsManager::SettingsManager(const std::string& filename, void (*f)()) : _fileName(filename), _loaded(false)
{
SetOnLoadCallback(f);
LoadConfig();
}


void SettingsManager::LoadConfig()
{
LeLog.WriteP("Loading configuration file %s...", _fileName.c_str());
try
{
_settings.Load(_fileName);
if (!boost::filesystem::exists(_fileName))
Save(_fileName, true);

Load(_fileName);
_loaded = true;

if (_onLoadCallback)
Expand All @@ -37,16 +38,10 @@ void SettingsManager::LoadConfig()

void SettingsManager::SaveConfig()
{
if (!_loaded)
{
LeLog.Write("Saving configuration file failed because configuration was not loaded before.");
return;
}

LeLog.WriteP( "Saving configuration file %s...", _fileName.c_str());
try
{
_settings.Save(_fileName);
Save(_fileName);
}
catch (std::exception &e)
{
Expand All @@ -67,44 +62,26 @@ void SettingsManager::ReloadConfig()
LoadConfig();
}

void SettingsManager::Settings::Load(const std::string& fileName)
void SettingsManager::Load(const std::string& fileName)
{
using boost::property_tree::ptree;
ptree pt;

read_info(fileName, pt);

// screen
ScreenWidth = pt.get("screen.X", 800);
ScreenHeight = pt.get("screen.Y", 600);
ScreenBPP = pt.get("screen.BPP", 24);
ScreenFullScreen = pt.get("screen.fullscreen", false);

// logging
LogClientFileName = pt.get("logging.files.client", "clntlog.log");
LogApplicationFileName = pt.get("logging.files.app", "applog.log");
LogServerFileName = pt.get<std::string>("logging.files.server", "srvrlog.log");
LogWriteToConsole = pt.get("logging.write_to_console", true);
LogWithTimestamp = pt.get("logging.with_timestamp", true);
for (auto itr = _settings.begin(); itr != _settings.end(); ++itr)
{
itr->second.first = pt.get(itr->first, itr->second.second);
}
}

void SettingsManager::Settings::Save( const std::string& fileName )
void SettingsManager::Save(const std::string& fileName, bool firstLoad /*=false*/)
{
using boost::property_tree::ptree;
ptree pt;

// screen
pt.put("screen.X", ScreenWidth);
pt.put("screen.Y", ScreenHeight);
pt.put("screen.BPP", ScreenBPP);
pt.put("screen.fullscreen", ScreenFullScreen);

// logging
pt.put("logging.files.client", LogClientFileName);
pt.put("logging.files.app", LogApplicationFileName);
pt.put("logging.files.server", LogServerFileName);
pt.put("logging.write_to_console", LogWriteToConsole);
pt.put("logging.with_timestamp", LogWithTimestamp);
for (auto itr = _settings.begin(); itr != _settings.end(); ++itr)
pt.put(itr->first, firstLoad ? itr->second.second : itr->second.first);

write_info(fileName, pt);
}
55 changes: 30 additions & 25 deletions src/LeEngine/SettingsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,52 @@
#include "Singleton.h"
#include <string>
#include <boost/noncopyable.hpp>
#include <utility>
#include <boost/lexical_cast.hpp>
#include <map>

#define SETTINGS_FILENAME "settings.conf"
#define GetConfig(name) SettingsManager::Get().GetSettings()->name
#define GetConfig(name, T) SettingsManager::Get().GetSetting<T>(name)

class SettingsManager : public Singleton<SettingsManager>
{
public:
struct Settings
{
// screen
int ScreenWidth;
int ScreenHeight;
int ScreenBPP;
bool ScreenFullScreen;

// logging
std::string LogClientFileName;
std::string LogApplicationFileName;
std::string LogServerFileName;
bool LogWriteToConsole;
bool LogWithTimestamp;

void Load(const std::string& fileName);
void Save(const std::string& fileName);
};
private:
std::string _fileName;
bool _loaded;
Settings _settings;
void (*_onLoadCallback)();

std::map< std::string, std::pair<std::string, std::string> > _settings;
void Load(const std::string& fileName);
void Save(const std::string& fileName, bool firstLoad = false);

public:
SettingsManager();
SettingsManager(void (*f)());
SettingsManager(const std::string& filename);
SettingsManager(const std::string& filename, void (*f)());

void SetOnLoadCallback(void (*f)()) { _onLoadCallback = f; }

void LoadConfig();
void SaveConfig();
void ReloadConfig();
Settings* GetSettings() const { return _loaded ? const_cast<Settings*>(&_settings) : NULL;}

template <typename T>
T GetSetting(std::string name);

template <typename T>
void AddSetting(std::string name, T defaultValue);
};

template <typename T>
T SettingsManager::GetSetting(std::string name)
{
if (_settings.find(name) != _settings.end())
return boost::lexical_cast<T>(_settings[name].first);
return T();
}

template <typename T>
void SettingsManager::AddSetting(std::string name, T defaultValue)
{
_settings[name] = std::make_pair("", boost::lexical_cast<std::string>(defaultValue));
}

#endif // SETTINGSMANAGER_H
2 changes: 1 addition & 1 deletion src/LeEngine/VideoUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bool VideoUpdate::Start()
return false;
}

_window = new Window("CHANGEME", GetConfig(ScreenWidth), GetConfig(ScreenHeight), GetConfig(ScreenFullScreen), GetConfig(ScreenBPP));
_window = new Window("CHANGEME", GetConfig("screen.X", int), GetConfig("screen.Y", int), GetConfig("screen.fullscreen", bool), GetConfig("screen.BPP", int));
if (!_window->Create())
{
glfwTerminate();
Expand Down

0 comments on commit b0ea7ec

Please sign in to comment.