From 3cc690825292aabbd352d2f1b8040c1d8696a8b0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 14 Feb 2024 23:32:54 -0500 Subject: [PATCH] add helper method for tokenizable string fields --- .../Internal/RuntimeMigrationHelper.cs | 12 +++++++++++ ...gration_2_0.ForBigCraftablesInformation.cs | 11 ++-------- .../Migrations/Migration_2_0.ForBlueprints.cs | 20 ++----------------- .../Migration_2_0.ForNpcDispositions.cs | 4 +--- .../Migration_2_0.ForObjectInformation.cs | 12 +++-------- 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/ContentPatcher/Framework/Migrations/Internal/RuntimeMigrationHelper.cs b/ContentPatcher/Framework/Migrations/Internal/RuntimeMigrationHelper.cs index 4980cec4e..5820125bc 100644 --- a/ContentPatcher/Framework/Migrations/Internal/RuntimeMigrationHelper.cs +++ b/ContentPatcher/Framework/Migrations/Internal/RuntimeMigrationHelper.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using StardewValley; using StardewValley.ItemTypeDefinitions; +using StardewTokenParser = StardewValley.TokenizableStrings.TokenParser; namespace ContentPatcher.Framework.Migrations.Internal { @@ -66,5 +67,16 @@ public static int CountFields(string row, char delimiter = '/') return count; } + + /// Get the value to set for a tokenizable string field based on the edit state. + /// The literal text from the temporary data with the patch edit applied. + /// The literal text from the temporary data before the patch edit was applied. + /// The current tokenizable string value in the target asset. + public static string? MigrateLiteralTextToTokenizableField(string? newValue, string? prevValue, string? assetValue) + { + return !string.IsNullOrWhiteSpace(newValue) && newValue != prevValue && newValue != StardewTokenParser.ParseText(assetValue) + ? newValue + : assetValue; + } } } diff --git a/ContentPatcher/Framework/Migrations/Migration_2_0.ForBigCraftablesInformation.cs b/ContentPatcher/Framework/Migrations/Migration_2_0.ForBigCraftablesInformation.cs index e111dcfdd..cd19dea26 100644 --- a/ContentPatcher/Framework/Migrations/Migration_2_0.ForBigCraftablesInformation.cs +++ b/ContentPatcher/Framework/Migrations/Migration_2_0.ForBigCraftablesInformation.cs @@ -143,19 +143,12 @@ private void MergeIntoNewFormat(IDictionary asset, IDi entry.Name = ArgUtility.Get(fields, 0, entry.Name, allowBlank: false); entry.Price = ArgUtility.GetInt(fields, 1, entry.Price); - - string description = ArgUtility.Get(fields, 4); - if (!string.IsNullOrWhiteSpace(description) && description != ArgUtility.Get(backupFields, 4) && description != StardewTokenParser.ParseText(entry.Description)) - entry.Description = description; - + entry.Description = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 4), ArgUtility.Get(backupFields, 4), entry.Description); entry.CanBePlacedOutdoors = ArgUtility.GetBool(fields, 5, entry.CanBePlacedOutdoors); entry.CanBePlacedIndoors = ArgUtility.GetBool(fields, 6, entry.CanBePlacedIndoors); entry.Fragility = ArgUtility.GetInt(fields, 7, entry.Fragility); entry.IsLamp = ArgUtility.GetBool(fields, 8, entry.IsLamp); - - string displayName = ArgUtility.Get(fields, 9); - if (!string.IsNullOrWhiteSpace(displayName) && displayName != ArgUtility.Get(backupFields, 9) && displayName != StardewTokenParser.ParseText(entry.DisplayName)) - entry.DisplayName = displayName; + entry.DisplayName = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 9), ArgUtility.Get(backupFields, 9), entry.DisplayName); } // set value diff --git a/ContentPatcher/Framework/Migrations/Migration_2_0.ForBlueprints.cs b/ContentPatcher/Framework/Migrations/Migration_2_0.ForBlueprints.cs index be52b6932..2415d4930 100644 --- a/ContentPatcher/Framework/Migrations/Migration_2_0.ForBlueprints.cs +++ b/ContentPatcher/Framework/Migrations/Migration_2_0.ForBlueprints.cs @@ -156,12 +156,6 @@ private void MergeIntoNewFormat(IDictionary asset, IDictio { string[] fields = fromEntry.Split('/'); - /* - fields[14] = entry.MaxOccupants.ToString(); - fields[17] = entry.BuildCost.ToString(); - fields[18] = entry.MagicalConstruction.ToString().ToLowerInvariant(); - */ - string rawItemsRequired = ArgUtility.Get(fields, 0); if (rawItemsRequired != ArgUtility.Get(backupFields, 0)) this.MergeItemsRequiredFieldIntoNewFormat(entry, rawItemsRequired); @@ -185,18 +179,8 @@ private void MergeIntoNewFormat(IDictionary asset, IDictio if (string.IsNullOrWhiteSpace(entry.IndoorMap) || entry.IndoorMapType == "null") entry.IndoorMap = null; - { - string displayName = ArgUtility.Get(fields, 8); - if (!string.IsNullOrWhiteSpace(displayName) && displayName != ArgUtility.Get(backupFields, 8) && displayName != StardewTokenParser.ParseText(entry.Name)) - entry.Name = displayName; - } - - { - string description = ArgUtility.Get(fields, 9); - if (!string.IsNullOrWhiteSpace(description) && description != ArgUtility.Get(backupFields, 9) && description != StardewTokenParser.ParseText(entry.Description)) - entry.Description = description; - } - + entry.Name = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 8), ArgUtility.Get(backupFields, 8), entry.Name); + entry.Description = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 9), ArgUtility.Get(backupFields, 9), entry.Description); entry.MaxOccupants = ArgUtility.GetInt(fields, 14, entry.MaxOccupants); entry.BuildCost = ArgUtility.GetInt(fields, 17, entry.BuildCost); entry.MagicalConstruction = ArgUtility.GetBool(fields, 18, entry.MagicalConstruction); diff --git a/ContentPatcher/Framework/Migrations/Migration_2_0.ForNpcDispositions.cs b/ContentPatcher/Framework/Migrations/Migration_2_0.ForNpcDispositions.cs index 3c75eab4b..7bac82d1e 100644 --- a/ContentPatcher/Framework/Migrations/Migration_2_0.ForNpcDispositions.cs +++ b/ContentPatcher/Framework/Migrations/Migration_2_0.ForNpcDispositions.cs @@ -170,9 +170,7 @@ private void MergeIntoNewFormat(IDictionary asset, IDicti if (rawHome != ArgUtility.Get(backupFields, 10)) this.MergeHomeIntoNewFormat(entry, rawHome); - string displayName = ArgUtility.Get(fields, 11); - if (!string.IsNullOrWhiteSpace(displayName) && displayName != ArgUtility.Get(backupFields, 11) && displayName != StardewTokenParser.ParseText(entry.DisplayName)) - entry.DisplayName = displayName; + entry.DisplayName = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 11), ArgUtility.Get(backupFields, 11), entry.DisplayName); } // set value diff --git a/ContentPatcher/Framework/Migrations/Migration_2_0.ForObjectInformation.cs b/ContentPatcher/Framework/Migrations/Migration_2_0.ForObjectInformation.cs index 408a95944..d1f63a0e9 100644 --- a/ContentPatcher/Framework/Migrations/Migration_2_0.ForObjectInformation.cs +++ b/ContentPatcher/Framework/Migrations/Migration_2_0.ForObjectInformation.cs @@ -162,15 +162,9 @@ private void MergeIntoNewFormat(IDictionary asset, IDictiona entry.Category = ArgUtility.GetInt(parts, 1, entry.Category); } - // display name - string displayName = ArgUtility.Get(fields, 4); - if (!string.IsNullOrWhiteSpace(displayName) && displayName != ArgUtility.Get(backupFields, 4) && displayName != StardewTokenParser.ParseText(entry.DisplayName)) - entry.DisplayName = displayName; - - // description - string description = ArgUtility.Get(fields, 5); - if (!string.IsNullOrWhiteSpace(description) && description != ArgUtility.Get(backupFields, 5) && description != StardewTokenParser.ParseText(entry.Description)) - entry.Description = description; + // display text + entry.DisplayName = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 4), ArgUtility.Get(backupFields, 4), entry.DisplayName); + entry.Description = RuntimeMigrationHelper.MigrateLiteralTextToTokenizableField(ArgUtility.Get(fields, 5), ArgUtility.Get(backupFields, 5), StardewTokenParser.ParseText(entry.Description)); // miscellaneous string rawMiscellaneous = ArgUtility.Get(fields, 6);