Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_ConfigDB.cpp
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | bool CreateValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value) | ||||
if (IsProtectedConfigName(name)) | if (IsProtectedConfigName(name)) | ||||
return false; | return false; | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
g_ConfigDB.SetValueString(cfgNs, name, value); | g_ConfigDB.SetValueString(cfgNs, name, value); | ||||
g_ConfigDB.SetChanges(cfgNs, true); | |||||
return true; | return true; | ||||
} | } | ||||
bool CreateValues(const std::wstring& cfgNsString, const std::string& name, const std::vector<CStr>& values) | bool CreateValues(const std::wstring& cfgNsString, const std::string& name, const std::vector<CStr>& values) | ||||
{ | { | ||||
if (IsProtectedConfigName(name)) | if (IsProtectedConfigName(name)) | ||||
return false; | return false; | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
g_ConfigDB.SetValueList(cfgNs, name, values); | g_ConfigDB.SetValueList(cfgNs, name, values); | ||||
g_ConfigDB.SetChanges(cfgNs, true); | |||||
return true; | return true; | ||||
} | } | ||||
bool RemoveValue(const std::wstring& cfgNsString, const std::string& name) | bool RemoveValue(const std::wstring& cfgNsString, const std::string& name) | ||||
{ | { | ||||
if (IsProtectedConfigName(name)) | if (IsProtectedConfigName(name)) | ||||
return false; | return false; | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
g_ConfigDB.RemoveValue(cfgNs, name); | bool result = g_ConfigDB.RemoveValue(cfgNs, name); | ||||
return true; | if (result) | ||||
g_ConfigDB.SetChanges(cfgNs, true); | |||||
return result; | |||||
} | } | ||||
bool WriteFile(const std::wstring& cfgNsString, const Path& path) | bool SaveChanges(const std::wstring& cfgNsString) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
return g_ConfigDB.WriteFile(cfgNs, path); | bool result = g_ConfigDB.WriteFile(cfgNs); | ||||
if (result) | |||||
g_ConfigDB.SetChanges(cfgNs, false); | |||||
return result; | |||||
} | } | ||||
bool WriteValueToFile(const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | bool RemoveValueAndSave(const std::wstring& cfgNsString, const std::string& name) | ||||
{ | |||||
if (RemoveValue(cfgNsString, name)) | |||||
return SaveChanges(cfgNsString); | |||||
return false; | |||||
} | |||||
bool SaveValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value) | |||||
{ | { | ||||
if (IsProtectedConfigName(name)) | if (IsProtectedConfigName(name)) | ||||
return false; | return false; | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
return g_ConfigDB.WriteValueToFile(cfgNs, name, value, path); | return g_ConfigDB.WriteValueToFile(cfgNs, name, value); | ||||
} | } | ||||
void CreateAndWriteValueToFile(const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | void CreateAndSaveValue(const std::wstring& cfgNsString, const std::string& name, const std::string& value) | ||||
{ | { | ||||
CreateValue(cfgNsString, name, value); | if (CreateValue(cfgNsString, name, value)) | ||||
WriteValueToFile(cfgNsString, name, value, path); | SaveValue(cfgNsString, name, value); | ||||
} | } | ||||
bool Reload(const std::wstring& cfgNsString) | bool Reload(const std::wstring& cfgNsString) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
return g_ConfigDB.Reload(cfgNs); | return g_ConfigDB.Reload(cfgNs); | ||||
} | } | ||||
bool SetFile(const std::wstring& cfgNsString, const Path& path) | |||||
{ | |||||
EConfigNamespace cfgNs; | |||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | |||||
return false; | |||||
g_ConfigDB.SetConfigFile(cfgNs, path); | |||||
return true; | |||||
} | |||||
void PauseOnFocusLoss(bool pause) | void PauseOnFocusLoss(bool pause) | ||||
{ | { | ||||
g_PauseOnFocusLoss = pause; | g_PauseOnFocusLoss = pause; | ||||
} | } | ||||
void SetGUIScale(float scale) | void SetGUIScale(float scale) | ||||
{ | { | ||||
g_VideoMode.Rescale(scale); | g_VideoMode.Rescale(scale); | ||||
} | } | ||||
void RegisterScriptFunctions(const ScriptRequest& rq) | void RegisterScriptFunctions(const ScriptRequest& rq) | ||||
{ | { | ||||
ScriptFunction::Register<&HasChanges>(rq, "ConfigDB_HasChanges"); | ScriptFunction::Register<&HasChanges>(rq, "ConfigDB_HasChanges"); | ||||
ScriptFunction::Register<&SetChanges>(rq, "ConfigDB_SetChanges"); | ScriptFunction::Register<&SetChanges>(rq, "ConfigDB_SetChanges"); | ||||
ScriptFunction::Register<&GetValue>(rq, "ConfigDB_GetValue"); | ScriptFunction::Register<&GetValue>(rq, "ConfigDB_GetValue"); | ||||
ScriptFunction::Register<&CreateValue>(rq, "ConfigDB_CreateValue"); | ScriptFunction::Register<&CreateValue>(rq, "ConfigDB_CreateValue"); | ||||
ScriptFunction::Register<&CreateValues>(rq, "ConfigDB_CreateValues"); | ScriptFunction::Register<&CreateValues>(rq, "ConfigDB_CreateValues"); | ||||
ScriptFunction::Register<&RemoveValue>(rq, "ConfigDB_RemoveValue"); | ScriptFunction::Register<&RemoveValue>(rq, "ConfigDB_RemoveValue"); | ||||
ScriptFunction::Register<&WriteFile>(rq, "ConfigDB_WriteFile"); | ScriptFunction::Register<&RemoveValueAndSave>(rq, "ConfigDB_RemoveValueAndSave"); | ||||
ScriptFunction::Register<&WriteValueToFile>(rq, "ConfigDB_WriteValueToFile"); | ScriptFunction::Register<&SaveChanges>(rq, "ConfigDB_SaveChanges"); | ||||
ScriptFunction::Register<&CreateAndWriteValueToFile>(rq, "ConfigDB_CreateAndWriteValueToFile"); | ScriptFunction::Register<&SaveValue>(rq, "ConfigDB_SaveValue"); | ||||
ScriptFunction::Register<&SetFile>(rq, "ConfigDB_SetFile"); | ScriptFunction::Register<&CreateAndSaveValue>(rq, "ConfigDB_CreateAndSaveValue"); | ||||
ScriptFunction::Register<&Reload>(rq, "ConfigDB_Reload"); | ScriptFunction::Register<&Reload>(rq, "ConfigDB_Reload"); | ||||
ScriptFunction::Register<&PauseOnFocusLoss>(rq, "PauseOnFocusLoss"); | ScriptFunction::Register<&PauseOnFocusLoss>(rq, "PauseOnFocusLoss"); | ||||
ScriptFunction::Register<&SetGUIScale>(rq, "SetGUIScale"); | ScriptFunction::Register<&SetGUIScale>(rq, "SetGUIScale"); | ||||
} | } | ||||
} | } |
Wildfire Games · Phabricator