Ticket #3737: optionsReset-v2.patch
File optionsReset-v2.patch, 15.0 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/options/options.js
1 1 var g_hasCallback = false; 2 2 var g_controls; 3 var g_options; 3 4 4 5 function init(data) 5 6 { … … 7 8 g_hasCallback = true; 8 9 let revert = data && data.revert; 9 10 g_controls = []; 11 g_options = Engine.ReadJSONFile("gui/options/options.json"); 10 12 11 var options = Engine.ReadJSONFile("gui/options/options.json"); 12 for (let category of Object.keys(options)) 13 for (let category of Object.keys(g_options)) 13 14 { 14 15 let lastSize; 15 for (let i = 0; i < options[category].length; ++i)16 for (let i = 0; i < g_options[category].length; ++i) 16 17 { 17 let option = options[category][i];18 let option = g_options[category][i]; 18 19 if (!option.label) 19 20 continue; 20 21 let body = Engine.GetGUIObjectByName(category + "[" + i + "]"); … … 78 79 if (checked === undefined || revert) 79 80 checked = Engine.ConfigDB_GetValue("user", keyConfig) === "true"; 80 81 else if ((Engine.ConfigDB_GetValue("user", keyConfig) === "true") !== checked) 82 { 81 83 Engine.ConfigDB_CreateValue("user", keyConfig, String(checked)); 84 updateStatus(); 85 } 82 86 break; 83 87 case "renderer": 84 88 keyRenderer = option.parameters.renderer; … … 106 110 Engine["Renderer_Set" + keyRenderer + "Enabled"](this.checked); 107 111 if (keyConfig) 108 112 Engine.ConfigDB_CreateValue("user", keyConfig, String(this.checked)); 109 updateStatus( true);113 updateStatus(); 110 114 }; 111 115 }(keyRenderer, keyConfig); 112 116 … … 164 168 Engine.ConfigDB_CreateValue("user", key, this.caption); 165 169 if (functionBody) 166 170 Engine[functionBody](+this.caption); 167 updateStatus( true);171 updateStatus(); 168 172 }; 169 173 }(key, functionBody, minval, maxval); 170 174 … … 211 215 if (key === "materialmgr.quality") 212 216 val = val == 0 ? 2 : val == 1 ? 5 : 8; 213 217 Engine.ConfigDB_CreateValue("user", key, val); 214 updateStatus( true);218 updateStatus(); 215 219 }; 216 220 }(key); 217 221 … … 227 231 return control; 228 232 } 229 233 230 function updateStatus( val)234 function updateStatus() 231 235 { 232 if (typeof val == "boolean") 233 Engine.ConfigDB_CreateValue("user", "nosave.haschanges", String(val)); 234 else 235 val = Engine.ConfigDB_GetValue("user", "nosave.haschanges") === "true"; 236 237 Engine.GetGUIObjectByName("loadOptions").enabled = val; 238 Engine.GetGUIObjectByName("saveOptions").enabled = val; 236 let val = Engine.ConfigDB_HasChanges("user"); 237 Engine.GetGUIObjectByName("revertChanges").enabled = val; 238 Engine.GetGUIObjectByName("saveChanges").enabled = val; 239 239 } 240 240 241 241 /** … … 247 247 control.onPress(); 248 248 } 249 249 250 function resetChanges() 251 { 252 let btCaptions = [translate("No"), translate("Yes")]; 253 let btCode = [null, function(){ reallyResetChanges(); }]; 254 messageBox(500, 200, translate("Resetting the options needs to close the game. Are you sure you want to continue ?"), 255 translate("Warning"), 0, btCaptions, btCode); 256 } 257 258 function reallyResetChanges() 259 { 260 for (let category in g_options) 261 for (let setting of g_options[category]) 262 if (setting.parameters.config) 263 Engine.ConfigDB_RemoveValue("user", setting.parameters.config); 264 265 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 266 Engine.Exit(); 267 } 268 250 269 function revertChanges() 251 270 { 252 271 Engine.ConfigDB_Reload("user"); 253 updateStatus( false);272 updateStatus(); 254 273 init({ "revert": true }); 255 274 } 256 275 … … 258 277 { 259 278 registerChanges(); 260 279 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 261 updateStatus( false);280 updateStatus(); 262 281 } 263 282 264 283 /** … … 267 286 function closePage() 268 287 { 269 288 registerChanges(); 270 if (Engine.ConfigDB_ GetValue("user", "nosave.haschanges") === "true")289 if (Engine.ConfigDB_HasChanges("user")) 271 290 { 272 291 let btCaptions = [translate("No"), translate("Yes")]; 273 292 let btCode = [null, function(){ closePageWithoutConfirmation(); }]; -
binaries/data/mods/public/gui/options/options.xml
69 69 </object> 70 70 </repeat> 71 71 </object> 72 <object name="loadOptions" type="button" style="ModernButtonRed" size="50%-170 100%-44 50%-70 100%-16" hotkey="cancel"> 72 <object type="button" style="ModernButtonRed" size="50%-236 100%-44 50%-136 100%-16" hotkey="cancel"> 73 <translatableAttribute id="caption">Reset</translatableAttribute> 74 <translatableAttribute id="tooltip">Reset all user settings (will close the game)</translatableAttribute> 75 <action on="Press">resetChanges();</action> 76 </object> 77 <object name="revertChanges" type="button" style="ModernButtonRed" size="50%-104 100%-44 50%-4 100%-16" hotkey="cancel"> 73 78 <translatableAttribute id="caption">Revert</translatableAttribute> 74 79 <translatableAttribute id="tooltip">Revert to previous saved settings</translatableAttribute> 75 80 <action on="Press">revertChanges();</action> 76 81 </object> 77 <object name="save Options" type="button" style="ModernButtonRed" size="50%-62 100%-44 50%+38100%-16">82 <object name="saveChanges" type="button" style="ModernButtonRed" size="50%+4 100%-44 50%+104 100%-16"> 78 83 <translatableAttribute id="caption">Save</translatableAttribute> 79 84 <translatableAttribute id="tooltip">Save changes</translatableAttribute> 80 85 <action on="Press">saveChanges();</action> 81 86 </object> 82 <object type="button" style="ModernButtonRed" size="50%+ 70 100%-44 50%+170100%-16">87 <object type="button" style="ModernButtonRed" size="50%+136 100%-44 50%+236 100%-16"> 83 88 <translatableAttribute id="caption">Quit</translatableAttribute> 84 89 <translatableAttribute id="tooltip">Unsaved changes affect this session only</translatableAttribute> 85 90 <action on="Press">closePage();</action> -
binaries/data/mods/public/gui/splashscreen/splashscreen.xml
27 27 <object name="btnOK" type="button" style="ModernButtonRed" size="18 100%-45 50%-5 100%-17" hotkey="cancel"> 28 28 <translatableAttribute id="caption">OK</translatableAttribute> 29 29 <action on="Press"><![CDATA[ 30 if (Engine.GetGUIObjectByName("displaySplashScreen").checked) 31 Engine.ConfigDB_CreateValue("user", "splashscreenversion", 0); 32 else 33 Engine.ConfigDB_CreateValue("user", "splashscreenversion", Engine.GetFileMTime("gui/splashscreen/splashscreen.txt")); 34 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 30 let value = Engine.GetGUIObjectByName("displaySplashScreen").checked ? 0 : Engine.GetFileMTime("gui/splashscreen/splashscreen.txt"); 31 Engine.ConfigDB_CreateValueAndWriteFile("user", "splashscreenversion", value, "config/user.cfg"); 35 32 Engine.PopGuiPageCB(); 36 33 ]]></action> 37 34 </object> -
source/ps/ConfigDB.cpp
29 29 typedef std::map<CStr, CConfigValueSet> TConfigMap; 30 30 TConfigMap CConfigDB::m_Map[CFG_LAST]; 31 31 VfsPath CConfigDB::m_ConfigFile[CFG_LAST]; 32 bool CConfigDB::m_HasChanges[CFG_LAST]; 32 33 33 34 static pthread_mutex_t cfgdb_mutex = PTHREAD_MUTEX_INITIALIZER; 34 35 … … 114 115 GETVAL(std::string) 115 116 #undef GETVAL 116 117 118 bool CConfigDB::HasChanges(EConfigNamespace ns) const 119 { 120 CHECK_NS(false); 121 122 CScopeLock s(&cfgdb_mutex); 123 return m_HasChanges[ns]; 124 } 125 117 126 void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const 118 127 { 119 128 CHECK_NS(;); … … 187 196 it = m_Map[ns].insert(m_Map[ns].begin(), make_pair(name, CConfigValueSet(1))); 188 197 189 198 it->second[0] = value; 199 m_HasChanges[ns] = true; 190 200 } 191 201 202 void CConfigDB::RemoveValue(EConfigNamespace ns, const CStr& name) 203 { 204 CHECK_NS(;); 205 206 CScopeLock s(&cfgdb_mutex); 207 TConfigMap::iterator it = m_Map[ns].find(name); 208 if (it == m_Map[ns].end()) 209 return; 210 m_Map[ns].erase(it); 211 m_HasChanges[ns] = true; 212 } 213 192 214 void CConfigDB::SetConfigFile(EConfigNamespace ns, const VfsPath& path) 193 215 { 194 216 CHECK_NS(;); … … 357 379 358 380 m_Map[ns].swap(newMap); 359 381 382 m_HasChanges[ns] = false; 360 383 return true; 361 384 } 362 385 … … 378 401 char* pos = (char*)buf.get(); 379 402 for (const std::pair<CStr, CConfigValueSet>& p : m_Map[ns]) 380 403 { 381 if (boost::algorithm::starts_with(p.first, "nosave."))382 continue;383 404 size_t i; 384 405 pos += sprintf(pos, "%s = ", p.first.c_str()); 385 406 for (i = 0; i < p.second.size() - 1; ++i) … … 395 416 return false; 396 417 } 397 418 419 m_HasChanges[ns] = false; 398 420 return true; 399 421 } 400 422 423 bool CConfigDB::SetValueAndWriteFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path) 424 { 425 CHECK_NS(false); 426 427 CScopeLock s(&cfgdb_mutex); 428 bool changed = m_HasChanges[ns]; 429 430 // if there are no previous changes, we can just set the value and then write to file 431 if (!changed) 432 { 433 SetValueString(ns, name, value); 434 bool ret = WriteFile(ns, path); 435 m_HasChanges[ns] = !ret; 436 return ret; 437 } 438 439 // otherwise we set this value in the specified namespace 440 SetValueString(ns, name, value); 441 TConfigMap newMap; 442 m_Map[ns].swap(newMap); 443 if (!Reload(ns)) 444 { 445 m_Map[ns].swap(newMap); 446 m_HasChanges[ns] = true; 447 return false; 448 } 449 // and add it to be written on file 450 SetValueString(ns, name, value); 451 bool ret = WriteFile(ns, path); 452 m_Map[ns].swap(newMap); 453 m_HasChanges[ns] = changed || !ret; 454 return ret; 455 } 456 401 457 #undef CHECK_NS -
source/ps/ConfigDB.h
52 52 { 53 53 static std::map<CStr, CConfigValueSet> m_Map[]; 54 54 static VfsPath m_ConfigFile[]; 55 static bool m_HasChanges[]; 55 56 56 57 public: 57 58 CConfigDB(); … … 72 73 void GetValue(EConfigNamespace ns, const CStr& name, std::string& value); 73 74 74 75 /** 76 * Returns true if changed with respect to last write on file 77 */ 78 bool HasChanges(EConfigNamespace ns) const; 79 80 /** 75 81 * Attempt to retrieve a vector of values corresponding to the given setting; 76 82 * will search CFG_COMMAND first, and then all namespaces from the specified 77 83 * namespace down. … … 96 102 * existed the value is replaced. 97 103 */ 98 104 void SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value); 105 106 /** 107 * Remove a config value in the specified namespace. 108 */ 109 void RemoveValue(EConfigNamespace ns, const CStr& name); 99 110 100 111 /** 101 112 * Set the path to the config file used to populate the specified namespace … … 135 146 * false: if an error occurred 136 147 */ 137 148 bool WriteFile(EConfigNamespace ns) const; 149 150 /** 151 * Save a config value in the specified namespace (if the config variable 152 * existed the value is replaced) and write it to file. 153 * 154 * Returns: 155 * true: if the config value was successfully saved and written to the file 156 * false: if an error occurred 157 */ 158 bool SetValueAndWriteFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path); 138 159 }; 139 160 140 161 -
source/ps/scripting/JSInterface_ConfigDB.cpp
42 42 return true; 43 43 } 44 44 45 bool JSI_ConfigDB::HasChanges(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString) 46 { 47 EConfigNamespace cfgNs; 48 if (!GetConfigNamespace(cfgNsString, cfgNs)) 49 return false; 50 51 return g_ConfigDB.HasChanges(cfgNs); 52 } 53 45 54 std::string JSI_ConfigDB::GetValue(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString, std::string name) 46 55 { 47 56 EConfigNamespace cfgNs; … … 63 72 return true; 64 73 } 65 74 75 bool JSI_ConfigDB::RemoveValue(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString, std::string name) 76 { 77 EConfigNamespace cfgNs; 78 if (!GetConfigNamespace(cfgNsString, cfgNs)) 79 return false; 80 81 g_ConfigDB.RemoveValue(cfgNs, name); 82 return true; 83 } 84 66 85 bool JSI_ConfigDB::WriteFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString, Path path) 67 86 { 68 87 EConfigNamespace cfgNs; … … 73 92 return ret; 74 93 } 75 94 95 bool JSI_ConfigDB::CreateValueAndWriteFile(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString, std::string name, std::string value, Path path) 96 { 97 EConfigNamespace cfgNs; 98 if (!GetConfigNamespace(cfgNsString, cfgNs)) 99 return false; 100 101 bool ret = g_ConfigDB.SetValueAndWriteFile(cfgNs, name, value, path); 102 return ret; 103 } 104 76 105 bool JSI_ConfigDB::Reload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring cfgNsString) 77 106 { 78 107 EConfigNamespace cfgNs; … … 95 124 96 125 void JSI_ConfigDB::RegisterScriptFunctions(ScriptInterface& scriptInterface) 97 126 { 127 scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::HasChanges>("ConfigDB_HasChanges"); 98 128 scriptInterface.RegisterFunction<std::string, std::wstring, std::string, &JSI_ConfigDB::GetValue>("ConfigDB_GetValue"); 99 129 scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, &JSI_ConfigDB::CreateValue>("ConfigDB_CreateValue"); 130 scriptInterface.RegisterFunction<bool, std::wstring, std::string, &JSI_ConfigDB::RemoveValue>("ConfigDB_RemoveValue"); 100 131 scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::WriteFile>("ConfigDB_WriteFile"); 132 scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, Path, &JSI_ConfigDB::CreateValueAndWriteFile>("ConfigDB_CreateValueAndWriteFile"); 101 133 scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::SetFile>("ConfigDB_SetFile"); 102 134 scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::Reload>("ConfigDB_Reload"); 103 135 -
source/ps/scripting/JSInterface_ConfigDB.h
24 24 namespace JSI_ConfigDB 25 25 { 26 26 bool GetConfigNamespace(std::wstring cfgNsString, EConfigNamespace& cfgNs); 27 bool HasChanges(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString); 27 28 std::string GetValue(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, std::string name); 28 29 bool CreateValue(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, std::string name, std::string value); 30 bool RemoveValue(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, std::string name); 29 31 bool WriteFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, Path path); 32 bool CreateValueAndWriteFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, std::string name, std::string value, Path path); 30 33 bool Reload(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString); 31 34 bool SetFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring cfgNsString, Path path); 32 35 void RegisterScriptFunctions(ScriptInterface& scriptInterface);