Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSInterface_ConfigDB.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "JSInterface_ConfigDB.h"
21 
22 #include "ps/ConfigDB.h"
23 #include "ps/CLogger.h"
25 
26 bool JSI_ConfigDB::GetConfigNamespace(std::wstring cfgNsString, EConfigNamespace& cfgNs)
27 {
28  if (cfgNsString == L"default")
29  cfgNs = CFG_DEFAULT;
30  else if (cfgNsString == L"system")
31  cfgNs = CFG_SYSTEM;
32  else if (cfgNsString == L"user")
33  cfgNs = CFG_USER;
34  else if (cfgNsString == L"mod")
35  cfgNs = CFG_MOD;
36  else
37  {
38  LOGERROR(L"Invalid namespace name passed to the ConfigDB!");
39  return false;
40  }
41  return true;
42 }
43 
44 std::string JSI_ConfigDB::GetValue(void* UNUSED(cbdata), std::wstring cfgNsString, std::string name)
45 {
46  EConfigNamespace cfgNs;
47  if (!GetConfigNamespace(cfgNsString, cfgNs))
48  return std::string();
49 
50  CConfigValue *val = g_ConfigDB.GetValue(cfgNs, name);
51  if (val)
52  {
53  return val->m_String;
54  }
55  else
56  {
57  LOGWARNING(L"Config setting %hs does not exist!", name.c_str());
58  return std::string();
59  }
60 }
61 
62 bool JSI_ConfigDB::CreateValue(void* UNUSED(cbdata), std::wstring cfgNsString, std::string name, std::string value)
63 {
64  EConfigNamespace cfgNs;
65  if (!GetConfigNamespace(cfgNsString, cfgNs))
66  return false;
67 
68  CConfigValue *val = g_ConfigDB.CreateValue(cfgNs, name);
69  val->m_String = value;
70  return true;
71 }
72 
73 bool JSI_ConfigDB::WriteFile(void* UNUSED(cbdata), std::wstring cfgNsString, Path path)
74 {
75  EConfigNamespace cfgNs;
76  if (!GetConfigNamespace(cfgNsString, cfgNs))
77  return false;
78 
79  bool ret = g_ConfigDB.WriteFile(cfgNs, path);
80  return ret;
81 }
82 
83 bool JSI_ConfigDB::Reload(void* UNUSED(cbdata), std::wstring cfgNsString)
84 {
85  EConfigNamespace cfgNs;
86  if (!GetConfigNamespace(cfgNsString, cfgNs))
87  return false;
88 
89  bool ret = g_ConfigDB.Reload(cfgNs);
90  return ret;
91 }
92 
93 bool JSI_ConfigDB::SetFile(void* UNUSED(cbdata), std::wstring cfgNsString, Path path)
94 {
95  EConfigNamespace cfgNs;
96  if (!GetConfigNamespace(cfgNsString, cfgNs))
97  return false;
98 
99  g_ConfigDB.SetConfigFile(cfgNs, path);
100  return true;
101 }
102 
104 {
105  scriptInterface.RegisterFunction<std::string, std::wstring, std::string, &JSI_ConfigDB::GetValue>("ConfigDB_GetValue");
106  scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, &JSI_ConfigDB::CreateValue>("ConfigDB_CreateValue");
107  scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::WriteFile>("ConfigDB_WriteFile");
108  scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::SetFile>("ConfigDB_SetFile");
109  scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::Reload>("ConfigDB_Reload");
110 
111 }
bool Reload(void *cbdata, std::wstring cfgNsString)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
std::string GetValue(void *cbdata, std::wstring cfgNsString, std::string name)
#define LOGERROR
Definition: CLogger.h:35
std::string m_String
Definition: Parser.h:112
void RegisterScriptFunctions(ScriptInterface &scriptInterface)
bool SetFile(void *cbdata, std::wstring cfgNsString, Path path)
bool GetConfigNamespace(std::wstring cfgNsString, EConfigNamespace &cfgNs)
#define LOGWARNING
Definition: CLogger.h:34
bool WriteFile(void *cbdata, std::wstring cfgNsString, Path path)
Definition: path.h:75
bool CreateValue(void *cbdata, std::wstring cfgNsString, std::string name, std::string value)
#define g_ConfigDB
Definition: ConfigDB.h:52
EConfigNamespace
Definition: ConfigDB.h:39
Abstraction around a SpiderMonkey JSContext.