From 2c20936ce36c4c77509a12a4820d1aee1d4cfc68 Mon Sep 17 00:00:00 2001 From: leper Date: Wed, 31 Dec 2014 00:21:24 +0000 Subject: [PATCH] Save map settings. Patch by @aBothe. Fixes #2963. This was SVN commit r16089. --- binaries/data/config/default.cfg | 3 + .../mods/public/gui/gamesetup/gamesetup.js | 72 ++++++++++++++++++- .../data/mods/public/gui/options/options.js | 1 + source/gui/scripting/ScriptFunctions.cpp | 12 ++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/binaries/data/config/default.cfg b/binaries/data/config/default.cfg index 5103a54bf9..3c8cbd0ad8 100644 --- a/binaries/data/config/default.cfg +++ b/binaries/data/config/default.cfg @@ -32,6 +32,9 @@ splashscreenversion = 0 ; Pause the game on window focus loss (Only applicable to single player mode) pauseonfocusloss = true +; Persist settings after leaving the game setup screen +persistmatchsettings = true + ; Default player name to use in multiplayer ; playername = "anonymous" diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index d7cf6cb931..5d4e2af99d 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -401,9 +401,13 @@ function initMain() // to allow easy keyboard selection of maps Engine.GetGUIObjectByName("mapSelection").focus(); } - // Sync g_GameAttributes to everyone. + if (g_IsController) + { + loadGameAttributes(); + // Sync g_GameAttributes to everyone. updateGameAttributes(); + } } function handleNetMessage(message) @@ -688,11 +692,74 @@ function loadMapData(name) return g_MapData[name]; } +const FILEPATH_MATCHSETTINGS_SP = "config/matchsettings.json"; +const FILEPATH_MATCHSETTINGS_MP = "config/matchsettings.mp.json"; +function loadGameAttributes() +{ + if (Engine.ConfigDB_GetValue("user", "persistmatchsettings") != "true") + return; + + var settingsFile = g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP; + if (!Engine.FileExists(settingsFile)) + return; + + var attrs = Engine.ReadJSONFile(settingsFile); + if (!attrs || !attrs.settings) + return; + + g_IsInGuiUpdate = true; + + var mapName = attrs.map || ""; + var mapSettings = attrs.settings; + + // Assign new seeds and match id + attrs.matchID = Engine.GetMatchID(); + mapSettings.Seed = Math.floor(Math.random() * 65536); + mapSettings.AISeed = Math.floor(Math.random() * 65536); + + // TODO: Check new attributes for being semantically correct. + g_GameAttributes = attrs; + + var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection"); + mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter); + + var mapTypeSelection = Engine.GetGUIObjectByName("mapTypeSelection"); + mapTypeSelection.selected = mapTypeSelection.list_data.indexOf(attrs.mapType); + + initMapNameList(); + + var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection"); + mapSelectionBox.selected = mapSelectionBox.list_data.indexOf(mapName); + + if (mapSettings.PopulationCap) + { + var populationCapBox = Engine.GetGUIObjectByName("populationCap"); + populationCapBox.selected = populationCapBox.list_data.indexOf(mapSettings.PopulationCap); + } + if (mapSettings.StartingResources) + { + var startingResourcesBox = Engine.GetGUIObjectByName("startingResources"); + startingResourcesBox.selected = startingResourcesBox.list_data.indexOf(mapSettings.StartingResources); + } + + g_IsInGuiUpdate = false; + + onGameAttributesChange(); +} + +function saveGameAttributes() +{ + var attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {}; + Engine.WriteJSONFile(g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP, attributes); +} //////////////////////////////////////////////////////////////////////////////////////////////// // GUI event handlers function cancelSetup() { + if (g_IsController) + saveGameAttributes(); + Engine.DisconnectNetworkGame(); if (Engine.HasXmppClient()) @@ -803,7 +870,6 @@ function selectMapType(type) { case "scenario": // Set a default map - // TODO: This should be remembered from the last session g_GameAttributes.mapPath = "maps/scenarios/"; g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP); g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536); @@ -984,6 +1050,8 @@ function launchGame() } } + saveGameAttributes(); + if (g_IsNetworked) { Engine.SetNetworkGameAttributes(g_GameAttributes); diff --git a/binaries/data/mods/public/gui/options/options.js b/binaries/data/mods/public/gui/options/options.js index 84f42b8d45..7a5e8624e8 100644 --- a/binaries/data/mods/public/gui/options/options.js +++ b/binaries/data/mods/public/gui/options/options.js @@ -13,6 +13,7 @@ var options = { [translate("FPS Overlay"), translate("Show frames per second in top right corner."), {"config":"overlay.fps"}, "boolean"], [translate("Realtime Overlay"), translate("Show current system time in top right corner."), {"config":"overlay.realtime"}, "boolean"], [translate("Gametime Overlay"), translate("Show current simulation time in top right corner."), {"config":"gui.session.timeelapsedcounter"}, "boolean"], + [translate("Persist match settings"), translate("Save and restore match settings for quick reuse when hosting an other game"), {"config":"persistmatchsettings"}, "boolean"], ], "graphicsSetting": [ diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 004cad721b..6c623333f0 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -842,6 +842,17 @@ CScriptVal ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring fil return out.get(); } +void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring filePath, CScriptVal scriptVal) +{ + JS::RootedValue val(pCxPrivate->pScriptInterface->GetContext(), scriptVal.get()); + std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false)); + + VfsPath path(filePath); + WriteBuffer buf; + buf.Append(str.c_str(), str.length()); + g_VFS->CreateFile(path, buf.Data(), buf.Size()); +} + CParamNode GetTemplate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName) { return g_GUI->GetTemplate(templateName); @@ -1007,6 +1018,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("GetFPS"); scriptInterface.RegisterFunction("GetBuildTimestamp"); scriptInterface.RegisterFunction("ReadJSONFile"); + scriptInterface.RegisterFunction("WriteJSONFile"); scriptInterface.RegisterFunction("GetTemplate"); // User report functions