Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_ConfigDB.cpp
Show All 16 Lines | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "JSInterface_ConfigDB.h" | #include "JSInterface_ConfigDB.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include <string> | #include <string> | ||||
#include <unordered_set> | #include <unordered_set> | ||||
// These entries will not be readable nor writable for JS, so that e.g. malicious mods can't leak personal or sensitive data | // These entries will not be readable nor writable for JS, so that e.g. malicious mods can't leak personal or sensitive data | ||||
static const std::unordered_set<std::string> g_ProtectedConfigNames = { | static const std::unordered_set<std::string> g_ProtectedConfigNames = { | ||||
"modio.public_key", // See ModIO.cpp | "modio.public_key", // See ModIO.cpp | ||||
"modio.v1.baseurl", | "modio.v1.baseurl", | ||||
Show All 28 Lines | bool JSI_ConfigDB::GetConfigNamespace(const std::wstring& cfgNsString, EConfigNamespace& cfgNs) | ||||
{ | { | ||||
LOGERROR("Invalid namespace name passed to the ConfigDB!"); | LOGERROR("Invalid namespace name passed to the ConfigDB!"); | ||||
cfgNs = CFG_DEFAULT; | cfgNs = CFG_DEFAULT; | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool JSI_ConfigDB::HasChanges(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString) | bool JSI_ConfigDB::HasChanges(const std::wstring& cfgNsString) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
return g_ConfigDB.HasChanges(cfgNs); | return g_ConfigDB.HasChanges(cfgNs); | ||||
} | } | ||||
bool JSI_ConfigDB::SetChanges(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, bool value) | bool JSI_ConfigDB::SetChanges(const std::wstring& cfgNsString, bool value) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
g_ConfigDB.SetChanges(cfgNs, value); | g_ConfigDB.SetChanges(cfgNs, value); | ||||
return true; | return true; | ||||
} | } | ||||
std::string JSI_ConfigDB::GetValue(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const std::string& name) | std::string JSI_ConfigDB::GetValue(const std::wstring& cfgNsString, const std::string& name) | ||||
{ | { | ||||
if (IsProtectedConfigName(name)) | if (IsProtectedConfigName(name)) | ||||
return ""; | return ""; | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return std::string(); | return std::string(); | ||||
std::string value; | std::string value; | ||||
g_ConfigDB.GetValue(cfgNs, name, value); | g_ConfigDB.GetValue(cfgNs, name, value); | ||||
return value; | return value; | ||||
} | } | ||||
bool JSI_ConfigDB::CreateValue(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const std::string& name, const std::string& value) | bool JSI_ConfigDB::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); | ||||
return true; | return true; | ||||
} | } | ||||
bool JSI_ConfigDB::RemoveValue(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const std::string& name) | bool JSI_ConfigDB::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); | g_ConfigDB.RemoveValue(cfgNs, name); | ||||
return true; | return true; | ||||
} | } | ||||
bool JSI_ConfigDB::WriteFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const Path& path) | bool JSI_ConfigDB::WriteFile(const std::wstring& cfgNsString, const Path& path) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
return g_ConfigDB.WriteFile(cfgNs, path); | return g_ConfigDB.WriteFile(cfgNs, path); | ||||
} | } | ||||
bool JSI_ConfigDB::WriteValueToFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | bool JSI_ConfigDB::WriteValueToFile(const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | ||||
{ | { | ||||
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, path); | ||||
} | } | ||||
void JSI_ConfigDB::CreateAndWriteValueToFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | void JSI_ConfigDB::CreateAndWriteValueToFile(const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) | ||||
{ | { | ||||
CreateValue(pCxPrivate, cfgNsString, name, value); | CreateValue(cfgNsString, name, value); | ||||
WriteValueToFile(pCxPrivate, cfgNsString, name, value, path); | WriteValueToFile(cfgNsString, name, value, path); | ||||
} | } | ||||
bool JSI_ConfigDB::Reload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString) | bool JSI_ConfigDB::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 JSI_ConfigDB::SetFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const Path& path) | bool JSI_ConfigDB::SetFile(const std::wstring& cfgNsString, const Path& path) | ||||
{ | { | ||||
EConfigNamespace cfgNs; | EConfigNamespace cfgNs; | ||||
if (!GetConfigNamespace(cfgNsString, cfgNs)) | if (!GetConfigNamespace(cfgNsString, cfgNs)) | ||||
return false; | return false; | ||||
g_ConfigDB.SetConfigFile(cfgNs, path); | g_ConfigDB.SetConfigFile(cfgNs, path); | ||||
return true; | return true; | ||||
} | } | ||||
void JSI_ConfigDB::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_ConfigDB::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::HasChanges>("ConfigDB_HasChanges"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&JSI_ConfigDB::name), &JSI_ConfigDB::name>(scriptInterface, "ConfigDB_" #name) | ||||
scriptInterface.RegisterFunction<bool, std::wstring, bool, &JSI_ConfigDB::SetChanges>("ConfigDB_SetChanges"); | Register(HasChanges); | ||||
scriptInterface.RegisterFunction<std::string, std::wstring, std::string, &JSI_ConfigDB::GetValue>("ConfigDB_GetValue"); | Register(SetChanges); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, &JSI_ConfigDB::CreateValue>("ConfigDB_CreateValue"); | Register(GetValue); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, std::string, &JSI_ConfigDB::RemoveValue>("ConfigDB_RemoveValue"); | Register(CreateValue); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::WriteFile>("ConfigDB_WriteFile"); | Register(RemoveValue); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, Path, &JSI_ConfigDB::WriteValueToFile>("ConfigDB_WriteValueToFile"); | Register(WriteFile); | ||||
scriptInterface.RegisterFunction<void, std::wstring, std::string, std::string, Path, &JSI_ConfigDB::CreateAndWriteValueToFile>("ConfigDB_CreateAndWriteValueToFile"); | Register(WriteValueToFile); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::SetFile>("ConfigDB_SetFile"); | Register(CreateAndWriteValueToFile); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::Reload>("ConfigDB_Reload"); | Register(SetFile); | ||||
Register(Reload); | |||||
#undef Register | |||||
Stan: likewise | |||||
} | } |
Wildfire Games · Phabricator
likewise