diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js b/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js index faefc71b6b..b839c67de0 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js @@ -193,7 +193,7 @@ function startHost(playername, servername) { // Save player name Engine.ConfigDB_CreateValue("user", "playername", playername); - Engine.ConfigDB_WriteFile("user", "config/user.cfg"); + Engine.ConfigDB_WriteValueToFile("user", "playername", playername, "config/user.cfg"); // Disallow identically named games in the multiplayer lobby if (Engine.HasXmppClient()) { @@ -253,11 +253,13 @@ function startJoin(playername, ip) if (Engine.HasXmppClient()) // Set player lobby presence Engine.LobbySetPlayerPresence("playing"); - else { + else + { // Only save the player name and host address if they're valid and we're not in the lobby Engine.ConfigDB_CreateValue("user", "playername", playername); + Engine.ConfigDB_WriteValueToFile("user", "playername", playername, "config/user.cfg"); Engine.ConfigDB_CreateValue("user", "multiplayerserver", ip); - Engine.ConfigDB_WriteFile("user", "config/user.cfg"); + Engine.ConfigDB_WriteValueToFile("user", "multiplayerserver", ip, "config/user.cfg"); } return true; } diff --git a/binaries/data/mods/public/gui/lobby/prelobby.js b/binaries/data/mods/public/gui/lobby/prelobby.js index 50171eed65..413b9c3a71 100644 --- a/binaries/data/mods/public/gui/lobby/prelobby.js +++ b/binaries/data/mods/public/gui/lobby/prelobby.js @@ -180,12 +180,14 @@ function onTick() Engine.PopGuiPage(); Engine.SwitchGuiPage("page_lobby.xml"); Engine.ConfigDB_CreateValue("user", "playername", sanitizePlayerName(username, true, true)); + Engine.ConfigDB_WriteValueToFile("user", "playername", sanitizePlayerName(username, true, true), "config/user.cfg"); Engine.ConfigDB_CreateValue("user", "lobby.login", username); + Engine.ConfigDB_WriteValueToFile("user", "lobby.login", username, "config/user.cfg"); // We only store the encrypted password, so make sure to re-encrypt it if changed before saving. if (password != g_EncrytedPassword.substring(0, 10)) g_EncrytedPassword = Engine.EncryptPassword(password, username); Engine.ConfigDB_CreateValue("user", "lobby.password", g_EncrytedPassword); - Engine.ConfigDB_WriteFile("user", "config/user.cfg"); + Engine.ConfigDB_WriteValueToFile("user", "lobby.password", g_EncrytedPassword, "config/user.cfg"); break; } } diff --git a/binaries/data/mods/public/gui/splashscreen/splashscreen.xml b/binaries/data/mods/public/gui/splashscreen/splashscreen.xml index e59802d8eb..0b90f350bf 100644 --- a/binaries/data/mods/public/gui/splashscreen/splashscreen.xml +++ b/binaries/data/mods/public/gui/splashscreen/splashscreen.xml @@ -27,11 +27,9 @@ OK diff --git a/source/i18n/L10n.cpp b/source/i18n/L10n.cpp index f442fe3535..622d16c1ec 100644 --- a/source/i18n/L10n.cpp +++ b/source/i18n/L10n.cpp @@ -93,7 +93,7 @@ bool L10n::SaveLocale(const Locale& locale) const return false; g_ConfigDB.SetValueString(CFG_USER, "locale", locale.getName()); - g_ConfigDB.WriteFile(CFG_USER); + g_ConfigDB.WriteValueToFile(CFG_USER, "locale", locale.getName()); return true; } diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index debff06e29..8e351e3224 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -310,7 +310,7 @@ void* CNetServerWorker::SetupUPnP(void*) // Cache root descriptor URL to try to avoid discovery next time. g_ConfigDB.SetValueString(CFG_USER, "network.upnprootdescurl", urls.controlURL); - g_ConfigDB.WriteFile(CFG_USER); + g_ConfigDB.WriteValueToFile(CFG_USER, "network.upnprootdescurl", urls.controlURL); LOGMESSAGE("Net server: cached UPnP root descriptor URL as %s", urls.controlURL); // Make sure everything is properly freed. diff --git a/source/ps/ConfigDB.cpp b/source/ps/ConfigDB.cpp index 720f9c0be5..44e8b6505a 100644 --- a/source/ps/ConfigDB.cpp +++ b/source/ps/ConfigDB.cpp @@ -404,4 +404,29 @@ bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path) const return true; } +bool CConfigDB::WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value) +{ + CHECK_NS(false); + + CScopeLock s(&cfgdb_mutex); + return WriteValueToFile(ns, name, value, m_ConfigFile[ns]); +} + +bool CConfigDB::WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path) +{ + CHECK_NS(false); + + CScopeLock s(&cfgdb_mutex); + + TConfigMap newMap; + m_Map[ns].swap(newMap); + if (!Reload(ns)) + return false; + + SetValueString(ns, name, value); + bool ret = WriteFile(ns, path); + m_Map[ns].swap(newMap); + return ret; +} + #undef CHECK_NS diff --git a/source/ps/ConfigDB.h b/source/ps/ConfigDB.h index fdaf6be86c..9e72718964 100644 --- a/source/ps/ConfigDB.h +++ b/source/ps/ConfigDB.h @@ -137,6 +137,17 @@ public: * false: if an error occurred */ bool WriteFile(EConfigNamespace ns) const; + + /** + * 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); }; diff --git a/source/ps/UserReport.cpp b/source/ps/UserReport.cpp index 469b157de7..ea1f7be1bb 100644 --- a/source/ps/UserReport.cpp +++ b/source/ps/UserReport.cpp @@ -538,7 +538,7 @@ std::string CUserReporter::LoadUserID() } g_ConfigDB.SetValueString(CFG_USER, "userreport.id", userID); - g_ConfigDB.WriteFile(CFG_USER); + g_ConfigDB.WriteValueToFile(CFG_USER, "userreport.id", userID); } return userID; @@ -555,7 +555,7 @@ void CUserReporter::SetReportingEnabled(bool enabled) { CStr val = CStr::FromInt(enabled ? REPORTER_VERSION : 0); g_ConfigDB.SetValueString(CFG_USER, "userreport.enabledversion", val); - g_ConfigDB.WriteFile(CFG_USER); + g_ConfigDB.WriteValueToFile(CFG_USER, "userreport.enabledversion", val); if (m_Worker) m_Worker->SetEnabled(enabled); diff --git a/source/ps/scripting/JSInterface_ConfigDB.cpp b/source/ps/scripting/JSInterface_ConfigDB.cpp index 45cbd74adb..1519b7394f 100644 --- a/source/ps/scripting/JSInterface_ConfigDB.cpp +++ b/source/ps/scripting/JSInterface_ConfigDB.cpp @@ -73,6 +73,16 @@ bool JSI_ConfigDB::WriteFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), con return ret; } +bool JSI_ConfigDB::WriteValueToFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path) +{ + EConfigNamespace cfgNs; + if (!GetConfigNamespace(cfgNsString, cfgNs)) + return false; + + bool ret = g_ConfigDB.WriteValueToFile(cfgNs, name, value, path); + return ret; +} + bool JSI_ConfigDB::Reload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString) { EConfigNamespace cfgNs; @@ -98,7 +108,7 @@ void JSI_ConfigDB::RegisterScriptFunctions(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("ConfigDB_GetValue"); scriptInterface.RegisterFunction("ConfigDB_CreateValue"); scriptInterface.RegisterFunction("ConfigDB_WriteFile"); + scriptInterface.RegisterFunction("ConfigDB_WriteValueToFile"); scriptInterface.RegisterFunction("ConfigDB_SetFile"); scriptInterface.RegisterFunction("ConfigDB_Reload"); - } diff --git a/source/ps/scripting/JSInterface_ConfigDB.h b/source/ps/scripting/JSInterface_ConfigDB.h index 42a7cb8c37..6600e2cdeb 100644 --- a/source/ps/scripting/JSInterface_ConfigDB.h +++ b/source/ps/scripting/JSInterface_ConfigDB.h @@ -27,6 +27,7 @@ namespace JSI_ConfigDB std::string GetValue(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const std::string& name); bool CreateValue(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const std::string& name, const std::string& value); bool WriteFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const Path& path); + bool WriteValueToFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const std::string& name, const std::string& value, const Path& path); bool Reload(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString); bool SetFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const Path& path); void RegisterScriptFunctions(ScriptInterface& scriptInterface);