1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "ConfigHandler.h"
#include "ConfigLocater.h"
#include "ConfigSource.h"
#include "System/Util.h"
#include "System/Log/ILog.h"
#ifdef WIN32
#include <io.h>
#endif
#include <stdio.h>
#include <string.h>
#include <list>
#include <stdexcept>
#include <boost/thread/mutex.hpp>
/******************************************************************************/
using std::list;
using std::map;
using std::string;
using std::vector;
typedef map<string, string> StringMap;
ConfigHandler* configHandler = NULL;
/******************************************************************************/
class ConfigHandlerImpl : public ConfigHandler
{
public:
ConfigHandlerImpl(const vector<string>& locations, const bool safemode);
~ConfigHandlerImpl();
void SetString(const string& key, const string& value, bool useOverlay);
string GetString(const string& key) const;
bool IsSet(const string& key) const;
void Delete(const string& key);
string GetConfigFile() const;
const StringMap GetData() const;
void Update();
protected:
void AddObserver(ConfigNotifyCallback observer);
private:
void RemoveDefaults();
OverlayConfigSource* overlay;
FileConfigSource* writableSource;
DefaultConfigSource* defaultSource;
vector<ReadOnlyConfigSource*> sources;
// observer related
list<ConfigNotifyCallback> observers;
boost::mutex observerMutex;
StringMap changedValues;
};
/******************************************************************************/
#define for_each_source(it) \
for (vector<ReadOnlyConfigSource*>::iterator it = sources.begin(); it != sources.end(); ++it)
#define for_each_source_const(it) \
for (vector<ReadOnlyConfigSource*>::const_iterator it = sources.begin(); it != sources.end(); ++it)
/**
* @brief Fills the list of sources based on locations.
*
* Sources at lower indices take precedence over sources at higher indices.
* First there is the overlay, then one or more file sources, and the last
* source(s) specify default values.
*/
ConfigHandlerImpl::ConfigHandlerImpl(const vector<string>& locations, const bool safemode)
{
overlay = new OverlayConfigSource();
writableSource = new FileConfigSource(locations.front());
size_t sources_num = 3;
sources_num += (safemode) ? 1 : 0;
sources_num += locations.size() - 1;
sources.reserve(sources_num);
sources.push_back(overlay);
if (safemode) {
sources.push_back(new SafemodeConfigSource());
}
sources.push_back(writableSource);
vector<string>::const_iterator loc = locations.begin();
++loc; // skip writableSource
for (; loc != locations.end(); ++loc) {
sources.push_back(new FileConfigSource(*loc));
}
sources.push_back(new DefaultConfigSource());
assert(sources.size() <= sources_num);
// Perform migrations that need to happen on every load.
RemoveDefaults();
}
ConfigHandlerImpl::~ConfigHandlerImpl()
{
for_each_source(it) {
delete (*it);
}
}
/**
* @brief Remove config variables that are set to their default value.
*
* Algorithm is as follows:
*
* 1) Take all defaults from the DefaultConfigSource
* 2) Go in reverse through the FileConfigSources
* 3) For each one:
* 3a) delete setting if equal to the one in `defaults'
* 3b) add new value to `defaults' if different
*/
void ConfigHandlerImpl::RemoveDefaults()
{
StringMap defaults = sources.back()->GetData();
vector<ReadOnlyConfigSource*>::const_reverse_iterator rsource = sources.rbegin();
for (; rsource != sources.rend(); ++rsource) {
FileConfigSource* source = dynamic_cast<FileConfigSource*> (*rsource);
if (source != NULL) {
// Copy the map; we modify the original while iterating over the copy.
StringMap file = source->GetData();
for (StringMap::const_iterator it = file.begin(); it != file.end(); ++it) {
// Does the key exist in `defaults'?
StringMap::const_iterator pos = defaults.find(it->first);
if (pos != defaults.end() && pos->second == it->second) {
// Exists and value is equal => Delete.
source->Delete(it->first);
}
else {
// Doesn't exist or is not equal => Store new default.
// (It will be the default for the next FileConfigSource.)
defaults[it->first] = it->second;
}
}
}
}
}
void ConfigHandlerImpl::Delete(const string& key)
{
for_each_source(it) {
// The alternative to the dynamic cast is to merge ReadWriteConfigSource
// with ReadOnlyConfigSource, but then DefaultConfigSource would have to
// violate Liskov Substitution Principle... (by blocking modifications)
ReadWriteConfigSource* rwcs = dynamic_cast<ReadWriteConfigSource*> (*it);
if (rwcs != NULL) {
rwcs->Delete(key);
}
}
}
bool ConfigHandlerImpl::IsSet(const string& key) const
{
for_each_source_const(it) {
if ((*it)->IsSet(key)) {
return true;
}
}
return false;
}
string ConfigHandlerImpl::GetString(const string& key) const
{
const ConfigVariableMetaData* meta = ConfigVariable::GetMetaData(key);
for_each_source_const(it) {
if ((*it)->IsSet(key)) {
std::string value = (*it)->GetString(key);
if (meta != NULL) {
value = meta->Clamp(value);
}
return value;
}
}
throw std::runtime_error("ConfigHandler: Error: Key does not exist: " + key +
"\nPlease add the key to the list of allowed configuration values.");
}
/**
* @brief Sets a config string
*
* This does:
* 1) Lock file.
* 2) Read file (in case another program modified something)
* 3) Set data[key] = value.
* 4) Write file (so we keep the settings in the event of a crash or error)
* 5) Unlock file.
*
* We do not want conflicts when multiple instances are running
* at the same time (which would cause data loss).
* This would happen if e.g. unitsync and spring would access
* the config file at the same time, if we would not lock.
*/
void ConfigHandlerImpl::SetString(const string& key, const string& value, bool useOverlay)
{
// if we set something to be persisted,
// we do want to override the overlay value
if (!useOverlay) {
overlay->Delete(key);
}
// Don't do anything if value didn't change.
if (IsSet(key) && GetString(key) == value) {
return;
}
if (useOverlay) {
overlay->SetString(key, value);
}
else {
vector<ReadOnlyConfigSource*>::const_iterator it = sources.begin();
bool deleted = false;
++it; // skip overlay
++it; // skip writableSource
for (; it != sources.end(); ++it) {
if ((*it)->IsSet(key)) {
if ((*it)->GetString(key) == value) {
// key is being set to the default value,
// delete the key instead of setting it.
writableSource->Delete(key);
deleted = true;
}
break;
}
}
if (!deleted) {
// set key to the specified non-default value
writableSource->SetString(key, value);
}
}
boost::mutex::scoped_lock lck(observerMutex);
changedValues[key] = value;
}
void ConfigHandlerImpl::Update()
{
boost::mutex::scoped_lock lck(observerMutex);
for (StringMap::const_iterator ut = changedValues.begin(); ut != changedValues.end(); ++ut) {
const string& key = ut->first;
const string& value = ut->second;
for (list<ConfigNotifyCallback>::const_iterator it = observers.begin(); it != observers.end(); ++it) {
(*it)(key, value);
}
}
changedValues.clear();
}
string ConfigHandlerImpl::GetConfigFile() const {
return writableSource->GetFilename();
}
const StringMap ConfigHandlerImpl::GetData() const {
StringMap data;
for_each_source_const(it) {
const StringMap& sourceData = (*it)->GetData();
// insert doesn't overwrite, so this preserves correct overrides
data.insert(sourceData.begin(), sourceData.end());
}
return data;
}
void ConfigHandlerImpl::AddObserver(ConfigNotifyCallback observer) {
boost::mutex::scoped_lock lck(observerMutex);
observers.push_back(observer);
}
/******************************************************************************/
void ConfigHandler::Instantiate(const std::string configSource, const bool safemode)
{
Deallocate();
vector<string> locations;
if (configSource.empty()) {
ConfigLocater::GetDefaultLocations(locations);
}
else {
locations.push_back(configSource);
}
// log here so unitsync shows configuration source(s), too
vector<string>::const_iterator loc = locations.begin();
LOG("Using configuration source: \"%s\"", loc->c_str());
for (++loc; loc != locations.end(); ++loc) {
LOG("Using additional configuration source: \"%s\"", loc->c_str());
}
configHandler = new ConfigHandlerImpl(locations, safemode);
//assert(configHandler->GetString("test") == "x y z");
}
void ConfigHandler::Deallocate()
{
SafeDelete(configHandler);
}
/******************************************************************************/
|