Ticket #2963: preserveGameAttributes.2.patch

File preserveGameAttributes.2.patch, 6.4 KB (added by Alex, 9 years ago)

Implemented a WriteJSONFile method to let it dump the settings directly into .json files

  • 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  
    3232; Pause the game on window focus loss (Only applicable to single player mode)
    3333pauseonfocusloss = true
    3434
     35; Persist settings after leaving the game setup screen
     36persistmatchsettings = true
     37
    3538; Default player name to use in multiplayer
    3639; playername = "anonymous"
    3740
  • 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()  
    401401        // to allow easy keyboard selection of maps
    402402        Engine.GetGUIObjectByName("mapSelection").focus();
    403403    }
    404     // Sync g_GameAttributes to everyone.
     404
    405405    if (g_IsController)
     406    {
     407        loadGameAttributesFromUserCfg();
     408        // Sync g_GameAttributes to everyone.
    406409        updateGameAttributes();
     410    }
    407411}
    408412
    409413function handleNetMessage(message)
    function loadMapData(name)  
    688692    return g_MapData[name];
    689693}
    690694
     695const FILEPATH_MATCHSETTINGS_SP = "config/matchsettings.json";
     696const FILEPATH_MATCHSETTINGS_MP = "config/matchsettings.mp.json";
     697function 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
     750function 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}
    691755////////////////////////////////////////////////////////////////////////////////////////////////
    692756// GUI event handlers
    693757
    694758function cancelSetup()
    695759{
     760    if (g_IsController)
     761        saveGameAttributesToUserCfg();
     762
    696763    Engine.DisconnectNetworkGame();
    697764
    698765    if (Engine.HasXmppClient())
    function selectMapType(type)  
    803870    {
    804871    case "scenario":
    805872        // Set a default map
    806         // TODO: This should be remembered from the last session
    807873        g_GameAttributes.mapPath = "maps/scenarios/";
    808874        g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP);
    809875        g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536);
    function launchGame()  
    9841050        }
    9851051    }
    9861052
     1053    saveGameAttributesToUserCfg();
     1054
    9871055    if (g_IsNetworked)
    9881056    {
    9891057        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 = {  
    1313        [translate("FPS Overlay"), translate("Show frames per second in top right corner."), {"config":"overlay.fps"}, "boolean"],
    1414        [translate("Realtime Overlay"), translate("Show current system time in top right corner."), {"config":"overlay.realtime"}, "boolean"],
    1515        [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"],
    1617    ],
    1718    "graphicsSetting":
    1819    [
  • 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  
    842842    return out.get();
    843843}
    844844
     845void 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
    845856CParamNode GetTemplate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName)
    846857{
    847858    return g_GUI->GetTemplate(templateName);
    void GuiScriptingInit(ScriptInterface& scriptInterface)  
    10071018    scriptInterface.RegisterFunction<int, &GetFps>("GetFPS");
    10081019    scriptInterface.RegisterFunction<std::wstring, int, &GetBuildTimestamp>("GetBuildTimestamp");
    10091020    scriptInterface.RegisterFunction<CScriptVal, std::wstring, &ReadJSONFile>("ReadJSONFile");
     1021    scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &WriteJSONFile>("WriteJSONFile");
    10101022    scriptInterface.RegisterFunction<CParamNode, std::string, &GetTemplate>("GetTemplate");
    10111023
    10121024    // User report functions