2019-01-13 07:11:40 -08:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-09 06:53:32 -07:00
|
|
|
/*
|
|
|
|
|
CConfigDB - Load, access and store configuration variables
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2012-12-11 15:04:03 -08:00
|
|
|
TDD : http://www.wildfiregames.com/forum/index.php?showtopic=1125
|
2004-06-09 06:53:32 -07:00
|
|
|
OVERVIEW:
|
2004-07-04 08:36:48 -07:00
|
|
|
|
2013-09-29 11:56:50 -07:00
|
|
|
JavaScript: Check this documentation: http://trac.wildfiregames.com/wiki/Exposed_ConfigDB_Functions
|
2004-06-09 06:53:32 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-05-07 09:33:24 -07:00
|
|
|
#ifndef INCLUDED_CONFIGDB
|
|
|
|
|
#define INCLUDED_CONFIGDB
|
2004-06-09 06:53:32 -07:00
|
|
|
|
2011-02-16 12:40:15 -08:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2015-07-28 18:07:23 -07:00
|
|
|
#include "ps/CStr.h"
|
|
|
|
|
#include "ps/Singleton.h"
|
2011-02-16 12:40:15 -08:00
|
|
|
|
2018-12-28 06:58:35 -08:00
|
|
|
#include <map>
|
|
|
|
|
|
2004-10-23 07:39:28 -07:00
|
|
|
// Namespace priorities: User supersedes mod supersedes system.
|
|
|
|
|
// Command-line arguments override everything.
|
|
|
|
|
|
2004-06-09 06:53:32 -07:00
|
|
|
enum EConfigNamespace
|
|
|
|
|
{
|
2009-06-23 10:48:34 -07:00
|
|
|
CFG_DEFAULT,
|
2004-06-09 06:53:32 -07:00
|
|
|
CFG_SYSTEM,
|
|
|
|
|
CFG_MOD,
|
|
|
|
|
CFG_USER,
|
2004-10-23 07:39:28 -07:00
|
|
|
CFG_COMMAND,
|
2004-06-09 06:53:32 -07:00
|
|
|
CFG_LAST
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-16 17:03:59 -08:00
|
|
|
typedef std::vector<CStr> CConfigValueSet;
|
2004-07-21 09:34:07 -07:00
|
|
|
|
2004-06-09 06:53:32 -07:00
|
|
|
#define g_ConfigDB CConfigDB::GetSingleton()
|
|
|
|
|
|
2019-01-13 07:11:40 -08:00
|
|
|
class CConfigDB : public Singleton<CConfigDB>
|
2004-06-09 06:53:32 -07:00
|
|
|
{
|
|
|
|
|
public:
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
2013-12-29 15:56:18 -08:00
|
|
|
* Attempt to retrieve the value of a config variable with the given name;
|
|
|
|
|
* will search CFG_COMMAND first, and then all namespaces from the specified
|
|
|
|
|
* namespace down.
|
2010-10-22 19:37:00 -07:00
|
|
|
*/
|
2014-11-16 17:03:59 -08:00
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, bool& value);
|
|
|
|
|
///@copydoc CConfigDB::GetValue
|
|
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, int& value);
|
|
|
|
|
///@copydoc CConfigDB::GetValue
|
2018-06-06 10:55:08 -07:00
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, u32& value);
|
|
|
|
|
///@copydoc CConfigDB::GetValue
|
2014-11-16 17:03:59 -08:00
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, float& value);
|
|
|
|
|
///@copydoc CConfigDB::GetValue
|
|
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, double& value);
|
|
|
|
|
///@copydoc CConfigDB::GetValue
|
|
|
|
|
void GetValue(EConfigNamespace ns, const CStr& name, std::string& value);
|
2012-12-25 14:49:18 -08:00
|
|
|
|
2016-02-07 07:10:44 -08:00
|
|
|
/**
|
|
|
|
|
* Returns true if changed with respect to last write on file
|
|
|
|
|
*/
|
|
|
|
|
bool HasChanges(EConfigNamespace ns) const;
|
|
|
|
|
|
2016-02-07 11:06:15 -08:00
|
|
|
void SetChanges(EConfigNamespace ns, bool value);
|
2016-02-07 07:10:44 -08:00
|
|
|
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
|
|
|
|
* Attempt to retrieve a vector of values corresponding to the given setting;
|
2012-12-25 14:49:18 -08:00
|
|
|
* will search CFG_COMMAND first, and then all namespaces from the specified
|
2013-12-29 15:56:18 -08:00
|
|
|
* namespace down.
|
2010-10-22 19:37:00 -07:00
|
|
|
*/
|
2015-07-28 18:07:23 -07:00
|
|
|
void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const;
|
2004-07-21 09:34:07 -07:00
|
|
|
|
2011-03-02 17:49:49 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the namespace that the value returned by GetValues was defined in,
|
|
|
|
|
* or CFG_LAST if it wasn't defined at all.
|
|
|
|
|
*/
|
2015-07-28 18:07:23 -07:00
|
|
|
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name) const;
|
2011-03-02 17:49:49 -08:00
|
|
|
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
2012-04-17 06:14:34 -07:00
|
|
|
* Retrieve a map of values corresponding to settings whose names begin
|
2010-10-22 19:37:00 -07:00
|
|
|
* with the given prefix;
|
2013-12-29 15:56:18 -08:00
|
|
|
* will search all namespaces from default up to the specified namespace.
|
2010-10-22 19:37:00 -07:00
|
|
|
*/
|
2015-07-28 18:07:23 -07:00
|
|
|
std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const;
|
2010-10-22 19:37:00 -07:00
|
|
|
|
|
|
|
|
/**
|
2013-12-29 15:56:18 -08:00
|
|
|
* Save a config value in the specified namespace. If the config variable
|
|
|
|
|
* existed the value is replaced.
|
2010-10-22 19:37:00 -07:00
|
|
|
*/
|
2013-12-29 15:56:18 -08:00
|
|
|
void SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value);
|
2016-02-07 03:24:09 -08:00
|
|
|
|
|
|
|
|
void SetValueBool(EConfigNamespace ns, const CStr& name, const bool value);
|
2016-02-07 07:10:44 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a config value in the specified namespace.
|
|
|
|
|
*/
|
|
|
|
|
void RemoveValue(EConfigNamespace ns, const CStr& name);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
|
|
|
|
* Set the path to the config file used to populate the specified namespace
|
|
|
|
|
* Note that this function does not actually load the config file. Use
|
|
|
|
|
* the Reload() method if you want to read the config file at the same time.
|
|
|
|
|
*
|
|
|
|
|
* 'path': The path to the config file.
|
|
|
|
|
*/
|
2011-02-16 12:40:15 -08:00
|
|
|
void SetConfigFile(EConfigNamespace ns, const VfsPath& path);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
|
|
|
|
* Reload the config file associated with the specified config namespace
|
|
|
|
|
* (the last config file path set with SetConfigFile)
|
|
|
|
|
*
|
|
|
|
|
* Returns:
|
|
|
|
|
* true: if the reload succeeded,
|
|
|
|
|
* false: if the reload failed
|
|
|
|
|
*/
|
2004-06-09 06:53:32 -07:00
|
|
|
bool Reload(EConfigNamespace);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2010-10-22 19:37:00 -07:00
|
|
|
/**
|
|
|
|
|
* Write the current state of the specified config namespace to the file
|
|
|
|
|
* specified by 'path'
|
|
|
|
|
*
|
|
|
|
|
* Returns:
|
|
|
|
|
* true: if the config namespace was successfully written to the file
|
|
|
|
|
* false: if an error occurred
|
|
|
|
|
*/
|
2015-07-28 18:07:23 -07:00
|
|
|
bool WriteFile(EConfigNamespace ns, const VfsPath& path) const;
|
2011-02-16 12:40:15 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write the current state of the specified config namespace to the file
|
|
|
|
|
* it was originally loaded from.
|
|
|
|
|
*
|
|
|
|
|
* Returns:
|
|
|
|
|
* true: if the config namespace was successfully written to the file
|
|
|
|
|
* false: if an error occurred
|
|
|
|
|
*/
|
2015-07-28 18:07:23 -07:00
|
|
|
bool WriteFile(EConfigNamespace ns) const;
|
2016-02-07 03:31:23 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write a config value to the file specified by 'path'
|
|
|
|
|
*
|
|
|
|
|
* Returns:
|
|
|
|
|
* true: if the config value was successfully saved and written to the file
|
|
|
|
|
* false: if an error occurred
|
|
|
|
|
*/
|
|
|
|
|
bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path);
|
|
|
|
|
|
|
|
|
|
bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value);
|
2019-01-13 07:11:40 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static std::map<CStr, CConfigValueSet> m_Map[];
|
|
|
|
|
static VfsPath m_ConfigFile[];
|
|
|
|
|
static bool m_HasChanges[];
|
2004-06-09 06:53:32 -07:00
|
|
|
};
|
|
|
|
|
|
2005-08-09 08:55:44 -07:00
|
|
|
|
|
|
|
|
// stores the value of the given key into <destination>. this quasi-template
|
2014-11-17 15:29:49 -08:00
|
|
|
// convenience wrapper on top of GetValue simplifies user code
|
|
|
|
|
#define CFG_GET_VAL(name, destination)\
|
2014-11-16 17:03:59 -08:00
|
|
|
g_ConfigDB.GetValue(CFG_USER, name, destination)
|
2005-08-09 08:55:44 -07:00
|
|
|
|
2015-07-28 18:07:23 -07:00
|
|
|
#endif // INCLUDED_CONFIGDB
|