Line data Source code
1 : /* Copyright (C) 2026 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/CLogger.h"
23 : #include "ps/CStr.h"
24 : #include "ps/ConfigDB.h"
25 : #include "ps/VideoMode.h"
26 : #include "scriptinterface/FunctionWrapper.h"
27 :
28 : #include <string>
29 : #include <unordered_set>
30 : #include <vector>
31 :
32 : namespace JSI_ConfigDB
33 : {
34 : // These entries will not be readable nor writable for JS, so that e.g. malicious mods can't leak personal or sensitive data
35 : static const std::unordered_set<std::string> g_ProtectedConfigNames = {
36 : "modio.public_key", // See ModIO.cpp
37 : "modio.v1.baseurl",
38 : "modio.v1.api_key",
39 : "modio.v1.name_id",
40 : "userreport.id" // Acts as authentication token for GDPR personal data requests.
41 : };
42 :
43 0 : bool IsProtectedConfigName(const std::string& name)
44 : {
45 0 : if (g_ProtectedConfigNames.find(name) != g_ProtectedConfigNames.end())
46 : {
47 0 : LOGERROR("Access denied ({})", name);
48 0 : return true;
49 : }
50 : return false;
51 : }
52 :
53 0 : bool GetConfigNamespace(const std::wstring& cfgNsString, EConfigNamespace& cfgNs)
54 : {
55 0 : if (cfgNsString == L"default")
56 0 : cfgNs = CFG_DEFAULT;
57 0 : else if (cfgNsString == L"mod")
58 0 : cfgNs = CFG_MOD;
59 0 : else if (cfgNsString == L"system")
60 0 : cfgNs = CFG_SYSTEM;
61 0 : else if (cfgNsString == L"user")
62 0 : cfgNs = CFG_USER;
63 0 : else if (cfgNsString == L"hwdetect")
64 0 : cfgNs = CFG_HWDETECT;
65 : else
66 : {
67 0 : LOGERROR("Invalid namespace name passed to the ConfigDB!");
68 0 : cfgNs = CFG_DEFAULT;
69 0 : return false;
70 : }
71 : return true;
72 : }
73 :
74 0 : bool HasChanges(const std::wstring& cfgNsString)
75 : {
76 0 : EConfigNamespace cfgNs;
77 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
78 : return false;
79 :
80 0 : return g_ConfigDB.HasChanges(cfgNs);
81 : }
82 :
83 0 : bool SetChanges(const std::wstring& cfgNsString, bool value)
84 : {
85 0 : EConfigNamespace cfgNs;
86 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
87 : return false;
88 :
89 0 : g_ConfigDB.SetChanges(cfgNs, value);
90 : return true;
91 : }
92 :
93 0 : std::string GetValue(const std::wstring& cfgNsString, const std::string& name)
94 : {
95 0 : if (IsProtectedConfigName(name))
96 0 : return "";
97 :
98 0 : EConfigNamespace cfgNs;
99 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
100 0 : return std::string();
101 :
102 0 : std::string value;
103 0 : g_ConfigDB.GetValue(cfgNs, name, value);
104 0 : return value;
105 0 : }
106 :
107 0 : bool CreateValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value)
108 : {
109 0 : if (IsProtectedConfigName(name))
110 : return false;
111 :
112 0 : EConfigNamespace cfgNs;
113 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
114 : return false;
115 :
116 0 : g_ConfigDB.SetValueString(cfgNs, name, value);
117 0 : g_ConfigDB.SetChanges(cfgNs, true);
118 : return true;
119 : }
120 :
121 0 : bool CreateValues(const std::wstring& cfgNsString, const std::string& name, const std::vector<CStr>& values)
122 : {
123 0 : if (IsProtectedConfigName(name))
124 : return false;
125 :
126 0 : EConfigNamespace cfgNs;
127 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
128 : return false;
129 :
130 0 : g_ConfigDB.SetValueList(cfgNs, name, values);
131 0 : g_ConfigDB.SetChanges(cfgNs, true);
132 : return true;
133 : }
134 :
135 :
136 0 : bool RemoveValue(const std::wstring& cfgNsString, const std::string& name)
137 : {
138 0 : if (IsProtectedConfigName(name))
139 : return false;
140 :
141 0 : EConfigNamespace cfgNs;
142 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
143 : return false;
144 :
145 0 : bool result = g_ConfigDB.RemoveValue(cfgNs, name);
146 0 : if (result)
147 0 : g_ConfigDB.SetChanges(cfgNs, true);
148 :
149 : return result;
150 : }
151 :
152 0 : bool SaveChanges(const std::wstring& cfgNsString)
153 : {
154 0 : EConfigNamespace cfgNs;
155 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
156 : return false;
157 :
158 0 : bool result = g_ConfigDB.WriteFile(cfgNs);
159 0 : if (result)
160 0 : g_ConfigDB.SetChanges(cfgNs, false);
161 :
162 : return result;
163 : }
164 :
165 0 : bool RemoveValueAndSave(const std::wstring& cfgNsString, const std::string& name)
166 : {
167 0 : if (RemoveValue(cfgNsString, name))
168 0 : return SaveChanges(cfgNsString);
169 :
170 : return false;
171 : }
172 :
173 :
174 0 : bool SaveValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value)
175 : {
176 0 : if (IsProtectedConfigName(name))
177 : return false;
178 :
179 0 : EConfigNamespace cfgNs;
180 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
181 : return false;
182 :
183 0 : return g_ConfigDB.WriteValueToFile(cfgNs, name, value);
184 : }
185 :
186 0 : void CreateAndSaveValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value)
187 : {
188 0 : if (CreateValue(cfgNsString, name, value))
189 0 : SaveValue(cfgNsString, name, value);
190 0 : }
191 :
192 0 : bool Reload(const std::wstring& cfgNsString)
193 : {
194 0 : EConfigNamespace cfgNs;
195 0 : if (!GetConfigNamespace(cfgNsString, cfgNs))
196 : return false;
197 :
198 0 : return g_ConfigDB.Reload(cfgNs);
199 : }
200 :
201 0 : void PauseOnFocusLoss(bool pause)
202 : {
203 0 : g_PauseOnFocusLoss = pause;
204 0 : }
205 :
206 0 : void SetGUIScale(float scale)
207 : {
208 0 : g_VideoMode.Rescale(scale);
209 0 : }
210 :
211 26 : void RegisterScriptFunctions(const Script::Request& rq)
212 : {
213 26 : Script::Function::Register<&HasChanges>(rq, "ConfigDB_HasChanges");
214 26 : Script::Function::Register<&SetChanges>(rq, "ConfigDB_SetChanges");
215 26 : Script::Function::Register<&GetValue>(rq, "ConfigDB_GetValue");
216 26 : Script::Function::Register<&CreateValue>(rq, "ConfigDB_CreateValue");
217 26 : Script::Function::Register<&CreateValues>(rq, "ConfigDB_CreateValues");
218 26 : Script::Function::Register<&RemoveValue>(rq, "ConfigDB_RemoveValue");
219 26 : Script::Function::Register<&RemoveValueAndSave>(rq, "ConfigDB_RemoveValueAndSave");
220 26 : Script::Function::Register<&SaveChanges>(rq, "ConfigDB_SaveChanges");
221 26 : Script::Function::Register<&SaveValue>(rq, "ConfigDB_SaveValue");
222 26 : Script::Function::Register<&CreateAndSaveValue>(rq, "ConfigDB_CreateAndSaveValue");
223 26 : Script::Function::Register<&Reload>(rq, "ConfigDB_Reload");
224 26 : Script::Function::Register<&PauseOnFocusLoss>(rq, "PauseOnFocusLoss");
225 26 : Script::Function::Register<&SetGUIScale>(rq, "SetGUIScale");
226 26 : }
227 : }
|