Ticket #3431: t3431_sanitize_matchsettings_v1.patch
File t3431_sanitize_matchsettings_v1.patch, 4.8 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/gamesetup/gamesetup.js
1 1 //////////////////////////////////////////////////////////////////////////////////////////////// 2 2 // Constants 3 const g_MatchSettings_SP = "config/matchsettings.json"; 4 const g_MatchSettings_MP = "config/matchsettings.mp.json"; 5 6 // TODO: these constants actually don't have an effect and that is not a scenario map, remove them 3 7 const DEFAULT_NETWORKED_MAP = "Acropolis 01"; 4 8 const DEFAULT_OFFLINE_MAP = "Acropolis 01"; 5 9 6 10 const g_Ceasefire = prepareForDropdown(g_Settings ? g_Settings.Ceasefire : undefined); 7 11 const g_GameSpeeds = prepareForDropdown(g_Settings ? g_Settings.GameSpeeds.filter(speed => !speed.ReplayOnly) : undefined); … … function loadMapData(name) 677 681 } 678 682 679 683 return g_MapData[name]; 680 684 } 681 685 682 const FILEPATH_MATCHSETTINGS_SP = "config/matchsettings.json";683 const FILEPATH_MATCHSETTINGS_MP = "config/matchsettings.mp.json";684 686 function loadGameAttributes() 685 687 { 686 688 if (Engine.ConfigDB_GetValue("user", "persistmatchsettings") != "true") 687 689 return; 688 690 689 var settingsFile = g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP;691 var settingsFile = g_IsNetworked ? g_MatchSettings_MP : g_MatchSettings_SP; 690 692 if (!Engine.FileExists(settingsFile)) 691 693 return; 692 694 693 695 var attrs = Engine.ReadJSONFile(settingsFile); 694 696 if (!attrs || !attrs.settings) … … function loadGameAttributes() 735 737 // Set player data 736 738 if (playerData) 737 739 mapSettings.PlayerData = playerData; 738 740 } 739 741 742 if (mapSettings.PlayerData) 743 sanitizePlayerData(mapSettings.PlayerData); 744 740 745 var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection"); 741 746 mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter); 742 747 743 748 Engine.GetGUIObjectByName("mapTypeSelection").selected = g_MapTypes.Name.indexOf(attrs.mapType); 744 749 … … function loadGameAttributes() 785 790 } 786 791 787 792 function saveGameAttributes() 788 793 { 789 794 var attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {}; 790 Engine.WriteJSONFile(g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP, attributes); 795 Engine.WriteJSONFile(g_IsNetworked ? g_MatchSettings_MP : g_MatchSettings_SP, attributes); 796 } 797 798 function sanitizePlayerData(playerData) 799 { 800 // Ignore gaia 801 if (playerData.length && !playerData[0]) 802 playerData.shift(); 803 804 // Set default color if no color present 805 playerData.forEach((pData, index) => { 806 if (pData && !pData.Color) 807 pData.Color = g_PlayerColors[index]; 808 }); 809 810 // Replace colors with the best matching color of PlayerDefaults 811 playerData.forEach((pData, index) => { 812 let colorDistances = g_PlayerColors.map(color => colorDistance(color, pData.Color)); 813 let smallestDistance = colorDistances.find(distance => colorDistances.every(distance2 => (distance2 >= distance))); 814 pData.Color = g_PlayerColors.find(color => colorDistance(color, pData.Color) == smallestDistance); 815 }); 816 817 ensureUniquePlayerColors(playerData); 791 818 } 819 792 820 //////////////////////////////////////////////////////////////////////////////////////////////// 793 821 // GUI event handlers 794 822 795 823 function cancelSetup() 796 824 { … … function selectMap(name) 1026 1054 let victoryIdx = mapSettings.GameType !== undefined && g_VictoryConditions.Name.indexOf(mapSettings.GameType) != -1 ? g_VictoryConditions.Name.indexOf(mapSettings.GameType) : g_VictoryConditions.Default; 1027 1055 g_GameAttributes.settings.GameType = g_VictoryConditions.Name[victoryIdx]; 1028 1056 g_GameAttributes.settings.VictoryScripts = g_VictoryConditions.Scripts[victoryIdx]; 1029 1057 } 1030 1058 1031 // Sanitize player data1032 1059 if (mapSettings.PlayerData) 1033 { 1034 // Ignore gaia 1035 if (mapSettings.PlayerData.length && !mapSettings.PlayerData[0]) 1036 mapSettings.PlayerData.shift(); 1037 1038 // Set default color if no color present 1039 mapSettings.PlayerData.forEach((pData, index) => { 1040 if (pData && !pData.Color) 1041 pData.Color = g_PlayerColors[index]; 1042 }); 1043 1044 // Replace colors with the best matching color of PlayerDefaults 1045 mapSettings.PlayerData.forEach((pData, index) => { 1046 let colorDistances = g_PlayerColors.map(color => colorDistance(color, pData.Color)); 1047 let smallestDistance = colorDistances.find(distance => colorDistances.every(distance2 => (distance2 >= distance))); 1048 pData.Color = g_PlayerColors.find(color => colorDistance(color, pData.Color) == smallestDistance); 1049 }); 1050 1051 ensureUniquePlayerColors(mapSettings.PlayerData); 1052 } 1060 sanitizePlayerData(mapSettings.PlayerData); 1053 1061 1054 1062 // Copy any new settings 1055 1063 g_GameAttributes.map = name; 1056 1064 g_GameAttributes.script = mapSettings.Script; 1057 1065 if (g_GameAttributes.map !== "random")