Ticket #2963: preserveGameAttributes.patch

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

Map reselection works now. It's still needed to test multiplayer behaviour.

  • 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..5d1e81d 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 CONFIGID_GAMEATTRIBUTES_SP = "lobbysettings";
     696const CONFIGID_GAMEATTRIBUTES_MP = "lobbysettings_mp";
     697function loadGameAttributesFromUserCfg()
     698{
     699    var newAttributes_str = Engine.ConfigDB_GetValue("user", g_IsNetworked ? CONFIGID_GAMEATTRIBUTES_MP : CONFIGID_GAMEATTRIBUTES_SP);
     700    if (newAttributes_str == null || newAttributes_str == "")
     701        return;
     702
     703    try
     704    {
     705        var attrs = JSON.parse(newAttributes_str);
     706    }
     707    catch (e)
     708    {
     709        warn("Error while loading game settings from user config: "+e);
     710        return;
     711    }
     712
     713    if (!attrs.settings)
     714        return;
     715
     716    g_IsInGuiUpdate = true;
     717
     718    // TODO: Check new attributes for being semantically correct.
     719    g_GameAttributes = attrs;
     720
     721    var mapName = attrs.map || "";
     722    var mapSettings = attrs.settings;
     723
     724    // Assign new seeds and match id
     725    attrs.matchID = Engine.GetMatchID();
     726    mapSettings.Seed = Math.floor(Math.random() * 65536);
     727    mapSettings.AISeed = Math.floor(Math.random() * 65536);
     728
     729    var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection");
     730    mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter);
     731
     732    var mapTypeSelection = Engine.GetGUIObjectByName("mapTypeSelection");
     733    mapTypeSelection.selected = mapTypeSelection.list_data.indexOf(attrs.mapType);
     734
     735    initMapNameList();
     736
     737    var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection");
     738    mapSelectionBox.selected = mapSelectionBox.list_data.indexOf(mapName);
     739
     740    if (mapSettings.PopulationCap)
     741    {
     742        var populationCapBox = Engine.GetGUIObjectByName("populationCap");
     743        populationCapBox.selected = populationCapBox.list_data.indexOf(mapSettings.PopulationCap);
     744    }
     745    if (mapSettings.StartingResources)
     746    {
     747        var startingResourcesBox = Engine.GetGUIObjectByName("startingResources");
     748        startingResourcesBox.selected = startingResourcesBox.list_data.indexOf(mapSettings.StartingResources);
     749    }
     750
     751    g_IsInGuiUpdate = false;
     752
     753    onGameAttributesChange();
     754}
     755
     756function saveGameAttributesToUserCfg()
     757{
     758    Engine.ConfigDB_CreateValue("user",  g_IsNetworked ? CONFIGID_GAMEATTRIBUTES_MP : CONFIGID_GAMEATTRIBUTES_SP, JSON.stringify(g_GameAttributes));
     759    Engine.ConfigDB_WriteFile("user", "config/user.cfg");
     760}
    691761////////////////////////////////////////////////////////////////////////////////////////////////
    692762// GUI event handlers
    693763
    694764function cancelSetup()
    695765{
     766    if (g_IsController)
     767        saveGameAttributesToUserCfg();
     768
    696769    Engine.DisconnectNetworkGame();
    697770
    698771    if (Engine.HasXmppClient())
    function selectMapType(type)  
    803876    {
    804877    case "scenario":
    805878        // Set a default map
    806         // TODO: This should be remembered from the last session
    807879        g_GameAttributes.mapPath = "maps/scenarios/";
    808880        g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP);
    809881        g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536);
    function launchGame()  
    9841056        }
    9851057    }
    9861058
     1059    saveGameAttributesToUserCfg();
     1060
    9871061    if (g_IsNetworked)
    9881062    {
    9891063        Engine.SetNetworkGameAttributes(g_GameAttributes);