From cbd5c3e13fa1548555569f97e894cbc5402a96c0 Mon Sep 17 00:00:00 2001 From: Rampastring Date: Fri, 3 May 2024 15:37:00 +0300 Subject: [PATCH] Add automatic fix for triggers potentially corrupted by bugs in earlier WAE versions --- src/TSMapEditor/Initialization/MapLoader.cs | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/TSMapEditor/Initialization/MapLoader.cs b/src/TSMapEditor/Initialization/MapLoader.cs index 67c5a1a2..59523937 100644 --- a/src/TSMapEditor/Initialization/MapLoader.cs +++ b/src/TSMapEditor/Initialization/MapLoader.cs @@ -6,9 +6,11 @@ using System.Globalization; using System.Linq; using System.Text; +using TSMapEditor.CCEngine; using TSMapEditor.Extensions; using TSMapEditor.GameMath; using TSMapEditor.Models; +using TSMapEditor.Models.Enums; using TSMapEditor.Models.MapFormat; using TSMapEditor.Rendering; @@ -813,6 +815,62 @@ public static void ReadTriggers(IMap map, IniFile mapIni) } Logger.Log("Triggers read successfully."); + + TriggerFix(map); + } + + /// + /// Checks for triggers having invalid values for uncustomizable parameters. + /// Some earlier versions of WAE could set these parameters wrong, so we are + /// cleaning up any potential mess we caused. + /// + private static void TriggerFix(IMap map) + { + Logger.Log("Checking for bugged triggers."); + + // Check for mismatched uncustomizable trigger action parameters + foreach (var trigger in map.Triggers) + { + foreach (var action in trigger.Actions) + { + if (!map.EditorConfig.TriggerActionTypes.TryGetValue(action.ActionIndex, out var triggerActionType)) + continue; + + for (int i = 0; i < triggerActionType.Parameters.Length - 1; i++) + { + var paramType = triggerActionType.Parameters[i].TriggerParamType; + + string valueToSet = null; + + if ((int)paramType < 0 && Conversions.IntFromString(action.Parameters[i], 0) != Math.Abs((int)paramType)) + { + valueToSet = Math.Abs((int)paramType).ToString(CultureInfo.InvariantCulture); + } + + if (paramType == TriggerParamType.Unused && action.Parameters[i] != "0") + { + valueToSet = "0"; + } + + if (valueToSet != null) + { + AddMapLoadError($"Trigger \"{trigger.Name}\" had action \"{triggerActionType.Name}\" with invalid value for uncustomizable parameter #{i}: \"{action.Parameters[i]}\". It has been automatically corrected to \"{valueToSet}\"."); + action.Parameters[i] = valueToSet; + } + } + + const int lastParamIndex = TriggerActionType.MAX_PARAM_COUNT - 1; + // Handle P7 separately due to WaypointZZ hardcoding + if (triggerActionType.Parameters[lastParamIndex].TriggerParamType == TriggerParamType.Unused && action.Parameters[lastParamIndex] != "A") + { + string valueToSet = "A"; + AddMapLoadError($"Trigger '{trigger.Name}' had action \"{triggerActionType.Name}\" with invalid value for uncustomizable parameter #{lastParamIndex}: \"{action.Parameters[lastParamIndex]}\". It has been automatically corrected to \"{valueToSet}\"."); + action.Parameters[lastParamIndex] = valueToSet; + } + } + } + + Logger.Log("Checking for bugged triggers completed."); } public static void ReadTags(IMap map, IniFile mapIni)