Ticket #2963: preserveGameAttributes.2.patch
File preserveGameAttributes.2.patch, 6.4 KB (added by , 9 years ago) |
---|
-
binaries/data/config/default.cfg
diff --git a/binaries/data/config/default.cfg b/binaries/data/config/default.cfg index 5103a54..3c8cbd0 100644
a b splashscreenversion = 0 32 32 ; Pause the game on window focus loss (Only applicable to single player mode) 33 33 pauseonfocusloss = true 34 34 35 ; Persist settings after leaving the game setup screen 36 persistmatchsettings = true 37 35 38 ; Default player name to use in multiplayer 36 39 ; playername = "anonymous" 37 40 -
binaries/data/mods/public/gui/gamesetup/gamesetup.js
diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index d7cf6cb..a90e5aa 100644
a b function initMain() 401 401 // to allow easy keyboard selection of maps 402 402 Engine.GetGUIObjectByName("mapSelection").focus(); 403 403 } 404 // Sync g_GameAttributes to everyone. 404 405 405 if (g_IsController) 406 { 407 loadGameAttributesFromUserCfg(); 408 // Sync g_GameAttributes to everyone. 406 409 updateGameAttributes(); 410 } 407 411 } 408 412 409 413 function handleNetMessage(message) … … function loadMapData(name) 688 692 return g_MapData[name]; 689 693 } 690 694 695 const FILEPATH_MATCHSETTINGS_SP = "config/matchsettings.json"; 696 const FILEPATH_MATCHSETTINGS_MP = "config/matchsettings.mp.json"; 697 function loadGameAttributesFromUserCfg() 698 { 699 if (Engine.ConfigDB_GetValue("user", "persistmatchsettings") != "true") 700 return; 701 702 var settingsFile = g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP; 703 if (!Engine.FileExists(settingsFile)) 704 return; 705 706 var attrs = Engine.ReadJSONFile(settingsFile); 707 if (!attrs || !attrs.settings) 708 return; 709 710 g_IsInGuiUpdate = true; 711 712 var mapName = attrs.map || ""; 713 var mapSettings = attrs.settings; 714 715 // Assign new seeds and match id 716 attrs.matchID = Engine.GetMatchID(); 717 mapSettings.Seed = Math.floor(Math.random() * 65536); 718 mapSettings.AISeed = Math.floor(Math.random() * 65536); 719 720 // TODO: Check new attributes for being semantically correct. 721 g_GameAttributes = attrs; 722 723 var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection"); 724 mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter); 725 726 var mapTypeSelection = Engine.GetGUIObjectByName("mapTypeSelection"); 727 mapTypeSelection.selected = mapTypeSelection.list_data.indexOf(attrs.mapType); 728 729 initMapNameList(); 730 731 var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection"); 732 mapSelectionBox.selected = mapSelectionBox.list_data.indexOf(mapName); 733 734 if (mapSettings.PopulationCap) 735 { 736 var populationCapBox = Engine.GetGUIObjectByName("populationCap"); 737 populationCapBox.selected = populationCapBox.list_data.indexOf(mapSettings.PopulationCap); 738 } 739 if (mapSettings.StartingResources) 740 { 741 var startingResourcesBox = Engine.GetGUIObjectByName("startingResources"); 742 startingResourcesBox.selected = startingResourcesBox.list_data.indexOf(mapSettings.StartingResources); 743 } 744 745 g_IsInGuiUpdate = false; 746 747 onGameAttributesChange(); 748 } 749 750 function saveGameAttributesToUserCfg() 751 { 752 var attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {}; 753 Engine.WriteJSONFile(g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP, attributes); 754 } 691 755 //////////////////////////////////////////////////////////////////////////////////////////////// 692 756 // GUI event handlers 693 757 694 758 function cancelSetup() 695 759 { 760 if (g_IsController) 761 saveGameAttributesToUserCfg(); 762 696 763 Engine.DisconnectNetworkGame(); 697 764 698 765 if (Engine.HasXmppClient()) … … function selectMapType(type) 803 870 { 804 871 case "scenario": 805 872 // Set a default map 806 // TODO: This should be remembered from the last session807 873 g_GameAttributes.mapPath = "maps/scenarios/"; 808 874 g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP); 809 875 g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536); … … function launchGame() 984 1050 } 985 1051 } 986 1052 1053 saveGameAttributesToUserCfg(); 1054 987 1055 if (g_IsNetworked) 988 1056 { 989 1057 Engine.SetNetworkGameAttributes(g_GameAttributes); -
binaries/data/mods/public/gui/options/options.js
diff --git a/binaries/data/mods/public/gui/options/options.js b/binaries/data/mods/public/gui/options/options.js index 84f42b8..7a5e862 100644
a b var options = { 13 13 [translate("FPS Overlay"), translate("Show frames per second in top right corner."), {"config":"overlay.fps"}, "boolean"], 14 14 [translate("Realtime Overlay"), translate("Show current system time in top right corner."), {"config":"overlay.realtime"}, "boolean"], 15 15 [translate("Gametime Overlay"), translate("Show current simulation time in top right corner."), {"config":"gui.session.timeelapsedcounter"}, "boolean"], 16 [translate("Persist match settings"), translate("Save and restore match settings for quick reuse when hosting an other game"), {"config":"persistmatchsettings"}, "boolean"], 16 17 ], 17 18 "graphicsSetting": 18 19 [ -
source/gui/scripting/ScriptFunctions.cpp
diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 004cad7..25cb52b 100644
a b CScriptVal ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring fil 842 842 return out.get(); 843 843 } 844 844 845 void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring filePath, CScriptVal scriptVal) 846 { 847 JS::RootedValue val(pCxPrivate->pScriptInterface->GetContext(), scriptVal.get()); 848 std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false)); 849 850 VfsPath path(filePath); 851 WriteBuffer buf; 852 buf.Append(str.c_str(), str.length()); 853 g_VFS->CreateFile(path, buf.Data(), buf.Size()); 854 } 855 845 856 CParamNode GetTemplate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName) 846 857 { 847 858 return g_GUI->GetTemplate(templateName); … … void GuiScriptingInit(ScriptInterface& scriptInterface) 1007 1018 scriptInterface.RegisterFunction<int, &GetFps>("GetFPS"); 1008 1019 scriptInterface.RegisterFunction<std::wstring, int, &GetBuildTimestamp>("GetBuildTimestamp"); 1009 1020 scriptInterface.RegisterFunction<CScriptVal, std::wstring, &ReadJSONFile>("ReadJSONFile"); 1021 scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &WriteJSONFile>("WriteJSONFile"); 1010 1022 scriptInterface.RegisterFunction<CParamNode, std::string, &GetTemplate>("GetTemplate"); 1011 1023 1012 1024 // User report functions