2012-01-12 23:41:32 +03:00
|
|
|
#include "StdInc.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "CConfigHandler.h"
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2012-01-12 23:41:32 +03:00
|
|
|
#include "../lib/GameConstants.h"
|
2012-01-12 18:23:00 +03:00
|
|
|
#include "../lib/VCMIDirs.h"
|
2011-08-24 05:19:53 +03:00
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
using namespace config;
|
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CConfigHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
SettingsStorage settings;
|
2008-11-09 00:29:19 +02:00
|
|
|
CConfigHandler conf;
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
template<typename Accessor>
|
|
|
|
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
|
|
|
|
parent(_parent),
|
|
|
|
path(_path)
|
|
|
|
{
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
template<typename Accessor>
|
|
|
|
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator [](std::string nextNode) const
|
|
|
|
{
|
|
|
|
std::vector<std::string> newPath = path;
|
|
|
|
newPath.push_back(nextNode);
|
|
|
|
return NodeAccessor(parent, newPath);
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
template<typename Accessor>
|
|
|
|
SettingsStorage::NodeAccessor<Accessor>::operator Accessor() const
|
|
|
|
{
|
|
|
|
return Accessor(parent, path);
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
|
|
|
template<typename Accessor>
|
|
|
|
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path)
|
|
|
|
{
|
|
|
|
std::vector<std::string> newPath = path;
|
|
|
|
newPath.insert( newPath.end(), _path.begin(), _path.end());
|
|
|
|
return NodeAccessor(parent, newPath);
|
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
SettingsStorage::SettingsStorage():
|
2012-01-12 23:41:32 +03:00
|
|
|
write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
|
2012-01-12 18:23:00 +03:00
|
|
|
listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
|
|
|
|
{
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
|
|
|
void SettingsStorage::init()
|
|
|
|
{
|
2013-04-03 00:39:32 +03:00
|
|
|
std::string confName = "config/settings.json";
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
JsonUtils::assembleFromFiles(confName).swap(config);
|
|
|
|
|
|
|
|
// Probably new install. Create config file to save settings to
|
|
|
|
if (!CResourceHandler::get("local")->existsResource(ResourceID(confName)))
|
|
|
|
CResourceHandler::get("local")->createResource(confName);
|
2013-04-02 20:06:43 +03:00
|
|
|
|
|
|
|
JsonUtils::maximize(config, "vcmi:settings");
|
|
|
|
JsonUtils::validate(config, "vcmi:settings", "settings");
|
2012-01-12 23:41:32 +03:00
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(SettingsListener * listener : listeners)
|
2012-01-12 23:41:32 +03:00
|
|
|
listener->nodeInvalidated(changedPath);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
JsonNode savedConf = config;
|
2013-04-02 20:06:43 +03:00
|
|
|
JsonNode schema(ResourceID("config/schemas/settings.json"));
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
savedConf.Struct().erase("session");
|
2013-04-02 20:06:43 +03:00
|
|
|
JsonUtils::minimize(savedConf, "vcmi:settings");
|
2012-08-08 23:58:06 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::ofstream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::trunc);
|
2012-01-12 18:23:00 +03:00
|
|
|
file << savedConf;
|
|
|
|
}
|
2008-11-09 00:29:19 +02:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonNode & SettingsStorage::getNode(std::vector<std::string> path)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonNode *node = &config;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(std::string& value : path)
|
2012-01-12 18:23:00 +03:00
|
|
|
node = &(*node)[value];
|
|
|
|
|
|
|
|
return *node;
|
|
|
|
}
|
2011-08-24 05:19:53 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
Settings SettingsStorage::get(std::vector<std::string> path)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
return Settings(*this, path);
|
|
|
|
}
|
2008-11-12 20:26:23 +02:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
const JsonNode& SettingsStorage::operator [](std::string value)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
return config[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsListener::SettingsListener(SettingsStorage &_parent, const std::vector<std::string> &_path):
|
|
|
|
parent(_parent),
|
|
|
|
path(_path)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
parent.listeners.insert(this);
|
|
|
|
}
|
|
|
|
|
2012-09-06 13:26:18 +03:00
|
|
|
SettingsListener::SettingsListener(const SettingsListener &sl):
|
|
|
|
parent(sl.parent),
|
|
|
|
path(sl.path),
|
|
|
|
callback(sl.callback)
|
|
|
|
{
|
|
|
|
parent.listeners.insert(this);
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
SettingsListener::~SettingsListener()
|
|
|
|
{
|
|
|
|
parent.listeners.erase(this);
|
|
|
|
}
|
|
|
|
|
2012-09-26 16:13:39 +03:00
|
|
|
void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
|
2012-01-12 23:41:32 +03:00
|
|
|
{
|
|
|
|
if (!callback)
|
|
|
|
return;
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
size_t min = std::min(path.size(), changedPath.size());
|
|
|
|
size_t mismatch = std::mismatch(path.begin(), path.begin()+min, changedPath.begin()).first - path.begin();
|
|
|
|
|
|
|
|
if (min == mismatch)
|
|
|
|
callback(parent.getNode(path));
|
2012-01-12 23:41:32 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void SettingsListener::operator() (std::function<void(const JsonNode&)> _callback)
|
2012-01-12 23:41:32 +03:00
|
|
|
{
|
|
|
|
callback = _callback;
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Settings::Settings(SettingsStorage &_parent, const std::vector<std::string> &_path):
|
|
|
|
parent(_parent),
|
|
|
|
path(_path),
|
|
|
|
node(_parent.getNode(_path)),
|
|
|
|
copy(_parent.getNode(_path))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::~Settings()
|
|
|
|
{
|
|
|
|
if (node != copy)
|
|
|
|
parent.invalidateNode(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode* Settings::operator -> ()
|
|
|
|
{
|
|
|
|
return &node;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonNode* Settings::operator ->() const
|
|
|
|
{
|
|
|
|
return &node;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonNode& Settings::operator [](std::string value) const
|
|
|
|
{
|
|
|
|
return node[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode& Settings::operator [](std::string value)
|
|
|
|
{
|
|
|
|
return node[value];
|
|
|
|
}
|
2012-09-29 13:59:43 +03:00
|
|
|
//
|
|
|
|
// template DLL_LINKAGE struct SettingsStorage::NodeAccessor<SettingsListener>;
|
|
|
|
// template DLL_LINKAGE struct SettingsStorage::NodeAccessor<Settings>;
|
2008-11-09 00:29:19 +02:00
|
|
|
|
2011-08-24 05:19:53 +03:00
|
|
|
static void setButton(ButtonInfo &button, const JsonNode &g)
|
|
|
|
{
|
|
|
|
button.x = g["x"].Float();
|
|
|
|
button.y = g["y"].Float();
|
|
|
|
button.playerColoured = g["playerColoured"].Float();
|
|
|
|
button.defName = g["graphic"].String();
|
|
|
|
|
2011-08-30 09:19:07 +03:00
|
|
|
if (!g["additionalDefs"].isNull()) {
|
|
|
|
const JsonVector &defs_vec = g["additionalDefs"].Vector();
|
2011-08-24 05:19:53 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const JsonNode &def : defs_vec) {
|
2011-08-30 09:19:07 +03:00
|
|
|
button.additionalDefs.push_back(def.String());
|
|
|
|
}
|
2011-08-24 05:19:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setGem(AdventureMapConfig &ac, const int gem, const JsonNode &g)
|
|
|
|
{
|
|
|
|
ac.gemX[gem] = g["x"].Float();
|
|
|
|
ac.gemY[gem] = g["y"].Float();
|
|
|
|
ac.gemG.push_back(g["graphic"].String());
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
CConfigHandler::CConfigHandler(void): current(nullptr)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CConfigHandler::~CConfigHandler(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void config::CConfigHandler::init()
|
|
|
|
{
|
2011-08-24 05:19:53 +03:00
|
|
|
/* Read resolutions. */
|
2012-08-02 14:03:26 +03:00
|
|
|
const JsonNode config(ResourceID("config/resolutions.json"));
|
2011-08-24 05:19:53 +03:00
|
|
|
const JsonVector &guisettings_vec = config["GUISettings"].Vector();
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const JsonNode &g : guisettings_vec)
|
2012-01-03 04:55:26 +03:00
|
|
|
{
|
2011-08-24 05:19:53 +03:00
|
|
|
std::pair<int,int> curRes(g["resolution"]["x"].Float(), g["resolution"]["y"].Float());
|
|
|
|
GUIOptions *current = &conf.guiOptions[curRes];
|
|
|
|
|
|
|
|
current->ac.inputLineLength = g["InGameConsole"]["maxInputPerLine"].Float();
|
|
|
|
current->ac.outputLineLength = g["InGameConsole"]["maxOutputPerLine"].Float();
|
|
|
|
|
|
|
|
current->ac.advmapX = g["AdvMap"]["x"].Float();
|
|
|
|
current->ac.advmapY = g["AdvMap"]["y"].Float();
|
|
|
|
current->ac.advmapW = g["AdvMap"]["width"].Float();
|
|
|
|
current->ac.advmapH = g["AdvMap"]["height"].Float();
|
|
|
|
current->ac.smoothMove = g["AdvMap"]["smoothMove"].Float();
|
|
|
|
current->ac.puzzleSepia = g["AdvMap"]["puzzleSepia"].Float();
|
|
|
|
|
|
|
|
current->ac.infoboxX = g["InfoBox"]["x"].Float();
|
|
|
|
current->ac.infoboxY = g["InfoBox"]["y"].Float();
|
|
|
|
|
|
|
|
setGem(current->ac, 0, g["gem0"]);
|
|
|
|
setGem(current->ac, 1, g["gem1"]);
|
|
|
|
setGem(current->ac, 2, g["gem2"]);
|
|
|
|
setGem(current->ac, 3, g["gem3"]);
|
|
|
|
|
|
|
|
current->ac.mainGraphic = g["background"].String();
|
|
|
|
|
|
|
|
current->ac.hlistX = g["HeroList"]["x"].Float();
|
|
|
|
current->ac.hlistY = g["HeroList"]["y"].Float();
|
|
|
|
current->ac.hlistSize = g["HeroList"]["size"].Float();
|
|
|
|
current->ac.hlistMB = g["HeroList"]["movePoints"].String();
|
|
|
|
current->ac.hlistMN = g["HeroList"]["manaPoints"].String();
|
|
|
|
current->ac.hlistAU = g["HeroList"]["arrowUp"].String();
|
|
|
|
current->ac.hlistAD = g["HeroList"]["arrowDown"].String();
|
|
|
|
|
|
|
|
current->ac.tlistX = g["TownList"]["x"].Float();
|
|
|
|
current->ac.tlistY = g["TownList"]["y"].Float();
|
|
|
|
current->ac.tlistSize = g["TownList"]["size"].Float();
|
|
|
|
current->ac.tlistAU = g["TownList"]["arrowUp"].String();
|
|
|
|
current->ac.tlistAD = g["TownList"]["arrowDown"].String();
|
|
|
|
|
|
|
|
current->ac.minimapW = g["Minimap"]["width"].Float();
|
|
|
|
current->ac.minimapH = g["Minimap"]["height"].Float();
|
|
|
|
current->ac.minimapX = g["Minimap"]["x"].Float();
|
|
|
|
current->ac.minimapY = g["Minimap"]["y"].Float();
|
|
|
|
|
|
|
|
current->ac.overviewPics = g["Overview"]["pics"].Float();
|
|
|
|
current->ac.overviewSize = g["Overview"]["size"].Float();
|
|
|
|
current->ac.overviewBg = g["Overview"]["graphic"].String();
|
|
|
|
|
|
|
|
current->ac.statusbarX = g["Statusbar"]["x"].Float();
|
|
|
|
current->ac.statusbarY = g["Statusbar"]["y"].Float();
|
|
|
|
current->ac.statusbarG = g["Statusbar"]["graphic"].String();
|
|
|
|
|
|
|
|
current->ac.resdatabarX = g["ResDataBar"]["x"].Float();
|
|
|
|
current->ac.resdatabarY = g["ResDataBar"]["y"].Float();
|
|
|
|
current->ac.resOffsetX = g["ResDataBar"]["offsetX"].Float();
|
|
|
|
current->ac.resOffsetY = g["ResDataBar"]["offsetY"].Float();
|
|
|
|
current->ac.resDist = g["ResDataBar"]["resSpace"].Float();
|
|
|
|
current->ac.resDateDist = g["ResDataBar"]["resDateSpace"].Float();
|
|
|
|
current->ac.resdatabarG = g["ResDataBar"]["graphic"].String();
|
|
|
|
|
|
|
|
setButton(current->ac.kingOverview, g["ButtonKingdomOv"]);
|
|
|
|
setButton(current->ac.underground, g["ButtonUnderground"]);
|
|
|
|
setButton(current->ac.questlog, g["ButtonQuestLog"]);
|
|
|
|
setButton(current->ac.sleepWake, g["ButtonSleepWake"]);
|
|
|
|
setButton(current->ac.moveHero, g["ButtonMoveHero"]);
|
|
|
|
setButton(current->ac.spellbook, g["ButtonSpellbook"]);
|
|
|
|
setButton(current->ac.advOptions, g["ButtonAdvOptions"]);
|
|
|
|
setButton(current->ac.sysOptions, g["ButtonSysOptions"]);
|
|
|
|
setButton(current->ac.nextHero, g["ButtonNextHero"]);
|
|
|
|
setButton(current->ac.endTurn, g["ButtonEndTurn"]);
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
const JsonNode& screenRes = settings["video"]["screenRes"];
|
|
|
|
|
2012-05-18 20:35:46 +03:00
|
|
|
SetResolution(screenRes["width"].Float(), screenRes["height"].Float());
|
2009-05-24 01:57:39 +03:00
|
|
|
}
|
2012-09-29 15:02:46 +03:00
|
|
|
|
|
|
|
// Force instantiation of the SettingsStorage::NodeAccessor class template.
|
|
|
|
// That way method definitions can sit in the cpp file
|
|
|
|
template struct SettingsStorage::NodeAccessor<SettingsListener>;
|
2014-03-08 19:05:23 +03:00
|
|
|
template struct SettingsStorage::NodeAccessor<Settings>;
|