1
0
forked from 0ad/0ad

redo 2d57478781, Create a ConfigDB_CreateAndWriteValueToFile function to absorb some duplication in the gui

Move two duplicated lines from 7e14a33411 in a function

fixes 2d57478781
Reviewed By: elexis
Comments By: Stan, vladislav
Differential Revision: https://code.wildfiregames.com/D1939
This was SVN commit r22615.
This commit is contained in:
bb 2019-08-05 14:12:55 +00:00
parent 69abe717e3
commit 7e1f959db0
15 changed files with 36 additions and 35 deletions

View File

@ -21,10 +21,7 @@ function openTerms(page)
function acceptTerms(data)
{
g_Terms[data.page].accepted = data.accepted;
let value = data.accepted ? getTermsHash(data.page) : "0";
Engine.ConfigDB_CreateValue("user", g_Terms[data.page].config, value);
Engine.ConfigDB_WriteValueToFile("user", g_Terms[data.page].config, value, "config/user.cfg");
Engine.ConfigDB_CreateAndWriteValueToFile("user", g_Terms[data.page].config, data.accepted ? getTermsHash(data.page) : "0", "config/user.cfg");
if (g_Terms[data.page].callback)
g_Terms[data.page].callback(data);

View File

@ -8,15 +8,6 @@ var g_SoundNotifications = {
"gamesetup.join": { "soundfile": "audio/interface/ui/gamesetup_join.ogg", "threshold": 0 }
};
/**
* Save setting for current instance and write setting to the user config file.
*/
function saveSettingAndWriteToUserConfig(setting, value)
{
Engine.ConfigDB_CreateValue("user", setting, value);
Engine.ConfigDB_WriteValueToFile("user", setting, value, "config/user.cfg");
}
/**
* Returns translated history and gameplay data of all civs, optionally including a mock gaia civ.
*/

View File

@ -1141,7 +1141,7 @@ function init(attribs)
// Replace empty playername when entering a singleplayermatch for the first time
if (!g_IsNetworked)
saveSettingAndWriteToUserConfig("playername.singleplayer", singleplayerName());
Engine.ConfigDB_CreateAndWriteValueToFile("user", "playername.singleplayer", singleplayerName(), "config/user.cfg");
initDefaults();
supplementDefaults();

View File

@ -125,7 +125,7 @@
<translatableAttribute id="caption">Show this message in the future.</translatableAttribute>
</object>
<object name="displaySPTips" size="8 100%-30 28 100%-8" type="checkbox" style="ModernTickBox">
<action on="Press">saveSettingAndWriteToUserConfig("gui.gamesetup.enabletips", String(this.checked));</action>
<action on="Press">Engine.ConfigDB_CreateAndWriteValueToFile("user", "gui.gamesetup.enabletips", String(this.checked), "config/user.cfg");</action>
</object>
</object>

View File

@ -284,9 +284,9 @@ function startHost(playername, servername, port)
{
startConnectionStatus("server");
saveSettingAndWriteToUserConfig("playername.multiplayer", playername);
Engine.ConfigDB_CreateAndWriteValueToFile("user", "playername.multiplayer", playername, "config/user.cfg");
saveSettingAndWriteToUserConfig("multiplayerhosting.port", port);
Engine.ConfigDB_CreateAndWriteValueToFile("user", "multiplayerhosting.port", port, "config/user.cfg");
let hostFeedback = Engine.GetGUIObjectByName("hostFeedback");
@ -361,9 +361,9 @@ function startJoin(playername, ip, port, useSTUN, hostJID = "")
else
{
// Only save the player name and host address if they're valid and we're not in the lobby
saveSettingAndWriteToUserConfig("playername.multiplayer", playername);
saveSettingAndWriteToUserConfig("multiplayerserver", ip);
saveSettingAndWriteToUserConfig("multiplayerjoining.port", port);
Engine.ConfigDB_CreateAndWriteValueToFile("user", "playername.multiplayer", playername, "config/user.cfg");
Engine.ConfigDB_CreateAndWriteValueToFile("user", "multiplayerserver", ip, "config/user.cfg");
Engine.ConfigDB_CreateAndWriteValueToFile("user", "multiplayerjoining.port", port, "config/user.cfg");
}
return true;
}

View File

@ -108,7 +108,7 @@
<object name="hostSTUNWrapper" size="120 106 100% 146">
<object name="useSTUN" size="0 10 32 100%" type="checkbox" style="ModernTickBox">
<action on="Press">saveSettingAndWriteToUserConfig("lobby.stun.enabled", String(this.checked));</action>
<action on="Press">Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.stun.enabled", String(this.checked), "config/user.cfg");</action>
</object>
<object type="text" size="26 0 100% 100%" style="ModernLeftLabelText">
<translatableAttribute id="caption">Use STUN to work around firewalls</translatableAttribute>

View File

@ -784,7 +784,7 @@ function toggleBuddy()
updateToggleBuddy();
saveSettingAndWriteToUserConfig("lobby.buddies", g_Buddies.filter(nick => nick).join(g_BuddyListDelimiter) || g_BuddyListDelimiter);
Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.buddies", g_Buddies.filter(nick => nick).join(g_BuddyListDelimiter) || g_BuddyListDelimiter, "config/user.cfg");
updatePlayerList();
updateGameList();

View File

@ -56,10 +56,10 @@ function toggleRememberPassword()
[translate("No"), translate("Yes")],
[
() => { checkbox.checked = true; },
() => { saveSettingAndWriteToUserConfig("lobby.rememberpassword", String(!enabled)); }
() => { Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.rememberpassword", String(!enabled), "config/user.cfg"); }
]);
else
saveSettingAndWriteToUserConfig("lobby.rememberpassword", String(!enabled));
Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.rememberpassword", String(!enabled), "config/user.cfg");
}
function getEncryptedPassword()
@ -78,11 +78,11 @@ function getEncryptedPassword()
function saveCredentials()
{
let username = Engine.GetGUIObjectByName("username").caption;
saveSettingAndWriteToUserConfig("playername.multiplayer", username);
saveSettingAndWriteToUserConfig("lobby.login", username);
Engine.ConfigDB_CreateAndWriteValueToFile("user", "playername.multiplayer", username, "config/user.cfg");
Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.login", username, "config/user.cfg");
if (Engine.ConfigDB_GetValue("user", "lobby.rememberpassword") == "true")
saveSettingAndWriteToUserConfig("lobby.password", getEncryptedPassword());
Engine.ConfigDB_CreateAndWriteValueToFile("user", "lobby.password", getEncryptedPassword(), "config/user.cfg");
else
{
Engine.ConfigDB_RemoveValue("user", "lobby.password");

View File

@ -357,7 +357,7 @@ function updateChatHistory()
function onToggleChatWindowExtended()
{
saveSettingAndWriteToUserConfig("chat.session.extended", String(Engine.GetGUIObjectByName("extendedChat").checked));
Engine.ConfigDB_CreateAndWriteValueToFile("user", "chat.session.extended", String(Engine.GetGUIObjectByName("extendedChat").checked), "config/user.cfg");
resizeChatWindow();

View File

@ -960,6 +960,11 @@ function updateGUIObjects()
g_DeveloperOverlay.update();
}
function saveResPopTooltipSort()
{
Engine.ConfigDB_CreateAndWriteValueToFile("user", "gui.session.respoptooltipsort", String((+Engine.ConfigDB_GetValue("user", "gui.session.respoptooltipsort") + 2) % 3 - 1), "config/user.cfg");
}
function onReplayFinished()
{
closeOpenDialogs();
@ -1333,7 +1338,7 @@ function recalculateStatusBarDisplay(remove = false)
function toggleConfigBool(configName)
{
let enabled = Engine.ConfigDB_GetValue("user", configName) != "true";
saveSettingAndWriteToUserConfig(configName, String(enabled));
Engine.ConfigDB_CreateAndWriteValueToFile("user", configName, String(enabled), "config/user.cfg");
return enabled;
}

View File

@ -4,7 +4,7 @@
<object size="0 -2 40 38" type="image" sprite="stretched:session/icons/resources/population.png" ghost="true"/>
<object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourcePop"/>
<action on="Press">
saveSettingAndWriteToUserConfig("gui.session.respoptooltipsort", (+Engine.ConfigDB_GetValue("user", "gui.session.respoptooltipsort") + 2) % 3 - 1);
saveResPopTooltipSort();
updatePlayerDisplay();
</action>
</object>

View File

@ -6,7 +6,7 @@
<object size="0 -2 40 38" type="image" name="resource[n]_icon" ghost="true"/>
<object size="34 0 100%-2 100%-2" type="text" style="resourceText" name="resource[n]_count"/>
<action on="Press">
saveSettingAndWriteToUserConfig("gui.session.respoptooltipsort", (+Engine.ConfigDB_GetValue("user", "gui.session.respoptooltipsort") + 2) % 3 - 1);
saveResPopTooltipSort();
updatePlayerDisplay();
</action>
</object>

View File

@ -28,8 +28,8 @@
<object name="btnOK" type="button" style="ModernButtonRed" size="18 100%-45 50%-5 100%-17" hotkey="cancel">
<translatableAttribute id="caption">OK</translatableAttribute>
<action on="Press"><![CDATA[
saveSettingAndWriteToUserConfig("gui.splashscreen.enable", String(Engine.GetGUIObjectByName("displaySplashScreen").checked));
saveSettingAndWriteToUserConfig("gui.splashscreen.version", Engine.GetFileMTime("gui/splashscreen/splashscreen.txt"));
Engine.ConfigDB_CreateAndWriteValueToFile("user", "gui.splashscreen.enable", String(Engine.GetGUIObjectByName("displaySplashScreen").checked), "config/user.cfg");
Engine.ConfigDB_CreateAndWriteValueToFile("user", "gui.splashscreen.version", Engine.GetFileMTime("gui/splashscreen/splashscreen.txt"), "config/user.cfg");
Engine.PopGuiPageCB();
]]></action>
</object>

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -140,6 +140,12 @@ bool JSI_ConfigDB::WriteValueToFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivat
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)
{
CreateValue(pCxPrivate, cfgNsString, name, value);
WriteValueToFile(pCxPrivate, cfgNsString, name, value, path);
}
bool JSI_ConfigDB::Reload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& cfgNsString)
{
EConfigNamespace cfgNs;
@ -168,6 +174,7 @@ void JSI_ConfigDB::RegisterScriptFunctions(const ScriptInterface& scriptInterfac
scriptInterface.RegisterFunction<bool, std::wstring, std::string, &JSI_ConfigDB::RemoveValue>("ConfigDB_RemoveValue");
scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::WriteFile>("ConfigDB_WriteFile");
scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, Path, &JSI_ConfigDB::WriteValueToFile>("ConfigDB_WriteValueToFile");
scriptInterface.RegisterFunction<void, std::wstring, std::string, std::string, Path, &JSI_ConfigDB::CreateAndWriteValueToFile>("ConfigDB_CreateAndWriteValueToFile");
scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::SetFile>("ConfigDB_SetFile");
scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::Reload>("ConfigDB_Reload");
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -34,6 +34,7 @@ namespace JSI_ConfigDB
bool RemoveValue(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& cfgNsString, const std::string& name);
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);
void CreateAndWriteValueToFile(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(const ScriptInterface& scriptInterface);