|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using ContentPatcher.Framework.ConfigModels; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using StardewModdingAPI; |
| 6 | +using StardewValley.GameData.Characters; |
| 7 | + |
| 8 | +namespace ContentPatcher.Framework.Migrations; |
| 9 | + |
| 10 | +/// <summary>Migrates patches to format version 2.6.</summary> |
| 11 | +[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named for clarity.")] |
| 12 | +internal class Migration_2_7 : BaseMigration |
| 13 | +{ |
| 14 | + /********* |
| 15 | + ** Public methods |
| 16 | + *********/ |
| 17 | + /// <summary>Construct an instance.</summary> |
| 18 | + public Migration_2_7() |
| 19 | + : base(new SemanticVersion(2, 7, 0)) { } |
| 20 | + |
| 21 | + /// <inheritdoc /> |
| 22 | + public override bool TryMigrate(ref PatchConfig[] patches, [NotNullWhen(false)] out string? error) |
| 23 | + { |
| 24 | + if (!base.TryMigrate(ref patches, out error)) |
| 25 | + return false; |
| 26 | + |
| 27 | + // 2.7 fixes a bug where you could set an enum field to a boolean value. This would previously be converted |
| 28 | + // numerically (like true -> 1 -> "MainGroup"), so match that logic for affected fields. |
| 29 | + // |
| 30 | + // This is based on the known affected content packs; it's not intended to cover every possible scenario (e.g. |
| 31 | + // patches which target "Data\Characters" or use 'Fields' instead). |
| 32 | + foreach (PatchConfig patch in patches) |
| 33 | + { |
| 34 | + if (patch.Entries.Count > 0 && string.Equals(patch.Target, "Data/Characters", StringComparison.OrdinalIgnoreCase)) |
| 35 | + { |
| 36 | + foreach (JToken? rawEntry in patch.Entries.Values) |
| 37 | + { |
| 38 | + if (rawEntry is not JObject entry) |
| 39 | + continue; |
| 40 | + |
| 41 | + JProperty? property = entry.Property(nameof(CharacterData.EndSlideShow), StringComparison.OrdinalIgnoreCase); |
| 42 | + if (property is null) |
| 43 | + continue; |
| 44 | + |
| 45 | + string? rawValue = property.Value.Value<string>(); |
| 46 | + if (string.Equals(rawValue, bool.TrueString, StringComparison.OrdinalIgnoreCase)) |
| 47 | + property.Value = nameof(EndSlideShowBehavior.MainGroup); |
| 48 | + else if (string.Equals(rawValue, bool.FalseString, StringComparison.OrdinalIgnoreCase)) |
| 49 | + property.Value = nameof(EndSlideShowBehavior.Hidden); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | +} |
0 commit comments