Ticket #2982: preserveSettingsFix.patch

File preserveSettingsFix.patch, 3.5 KB (added by Alex, 9 years ago)

Not the best approach but it's working now.

  • 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 5f71e4a..658bf06 100644
    a b var g_AssignedCount = 0;  
    7474// tick handler
    7575var g_LoadingState = 0; // 0 = not started, 1 = loading, 2 = loaded
    7676
     77// Used when restoring previously saved game settings in networked games.
     78// Used for assigning the host player to the slot he/she previously was at.
     79var g_HostPlayerSlot = -1;
     80
    7781////////////////////////////////////////////////////////////////////////////////////////////////
    7882
    7983function init(attribs)
    function handleNetMessage(message)  
    480484
    481485        // Update the player list
    482486        g_PlayerAssignments = message.hosts;
     487
     488        // Assign the host player to the previously selected slot.
     489        if (g_HostPlayerSlot > 0)
     490        {
     491            g_GameAttributes.settings.PlayerData[g_HostPlayerSlot].AI = g_AIs[0].id;
     492            updateGameAttributes();
     493            swapPlayers(Object.keys(g_PlayerAssignments)[0], g_HostPlayerSlot);
     494            g_HostPlayerSlot = -1;
     495        }
     496
    483497        updatePlayerList();
    484498        if (g_PlayerAssignments[newPlayer] && g_PlayerAssignments[newPlayer].player != -1)
    485499            resetReady = true;
    function loadGameAttributes()  
    717731    mapSettings.Seed = Math.floor(Math.random() * 65536);
    718732    mapSettings.AISeed = Math.floor(Math.random() * 65536);
    719733
     734    var aiCodes = [ ai.id for each (ai in g_AIs) ];
     735    var civListCodes = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     736    civListCodes.push("random");
     737
     738    g_HostPlayerSlot = -1;
     739    var playerData = mapSettings.PlayerData;
     740    if (playerData)
     741    {
     742        for (var i = 0; i < playerData.length; ++i)
     743        {
     744            // Discard previously unassigned/player slots
     745            if (aiCodes.indexOf(playerData[i].AI) < 0)
     746                playerData[i].AI = aiCodes[0];
     747
     748            if (playerData[i].isHostPlayer)
     749            {
     750                g_HostPlayerSlot = i;
     751                playerData[i].isHostPlayer = undefined;
     752            }
     753
     754            // Validate player civs.
     755            if (civListCodes.indexOf(playerData[i].Civ) <0)
     756                playerData[i].Civ = "random";
     757        }
     758    }
     759
    720760    // TODO: Check new attributes for being semantically correct.
    721761    g_GameAttributes = attrs;
    722762
    function loadGameAttributes()  
    745785    g_IsInGuiUpdate = false;
    746786
    747787    onGameAttributesChange();
     788
     789    // Assign the host player to the previously selected slot.
     790    if (g_HostPlayerSlot > 0 && !g_IsNetworked)
     791    {
     792        Engine.GetGUIObjectByName("playerAssignment["+g_HostPlayerSlot+"]").selected = 0;
     793        g_HostPlayerSlot = -1;
     794    }
     795    // If it's a networked game, assign the host player slot as soon as the first player data arrives in handleNetMessage()
    748796}
    749797
    750798function saveGameAttributes()
    751799{
    752     var attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {};
     800    var attributes = {};
     801    if(Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true")
     802    {
     803        attributes = g_GameAttributes;
     804
     805        // Assign a flag that determines the first real player
     806        if (g_PlayerAssignments)
     807        {
     808            var hostGuid = Object.keys(g_PlayerAssignments)[0];
     809            var hostPlayerIndex = g_PlayerAssignments[hostGuid].player - 1;
     810
     811            if (hostPlayerIndex >= 0 && attributes.settings.PlayerData.length > hostPlayerIndex)
     812                attributes.settings.PlayerData[hostPlayerIndex].isHostPlayer=true;
     813        }
     814    }
     815
    753816    Engine.WriteJSONFile(g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP, attributes);
    754817}
    755818////////////////////////////////////////////////////////////////////////////////////////////////