Skip to content

Commit

Permalink
Add automatic fix for triggers potentially corrupted by bugs in earli…
Browse files Browse the repository at this point in the history
…er WAE versions
  • Loading branch information
Rampastring committed May 3, 2024
1 parent 8a00dbd commit cbd5c3e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/TSMapEditor/Initialization/MapLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -813,6 +815,62 @@ public static void ReadTriggers(IMap map, IniFile mapIni)
}

Logger.Log("Triggers read successfully.");

TriggerFix(map);
}

/// <summary>
/// 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.
/// </summary>
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)
Expand Down

0 comments on commit cbd5c3e

Please sign in to comment.