Skip to content

Commit

Permalink
add helper method for tokenizable string fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Feb 15, 2024
1 parent 5b1ad78 commit 3cc6908
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using StardewValley;
using StardewValley.ItemTypeDefinitions;
using StardewTokenParser = StardewValley.TokenizableStrings.TokenParser;

namespace ContentPatcher.Framework.Migrations.Internal
{
Expand Down Expand Up @@ -66,5 +67,16 @@ public static int CountFields(string row, char delimiter = '/')

return count;
}

/// <summary>Get the value to set for a tokenizable string field based on the edit state.</summary>
/// <param name="newValue">The literal text from the temporary data with the patch edit applied.</param>
/// <param name="prevValue">The literal text from the temporary data before the patch edit was applied.</param>
/// <param name="assetValue">The current tokenizable string value in the target asset.</param>
public static string? MigrateLiteralTextToTokenizableField(string? newValue, string? prevValue, string? assetValue)
{
return !string.IsNullOrWhiteSpace(newValue) && newValue != prevValue && newValue != StardewTokenParser.ParseText(assetValue)
? newValue
: assetValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,12 @@ private void MergeIntoNewFormat(IDictionary<string, BigCraftableData> 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
Expand Down
20 changes: 2 additions & 18 deletions ContentPatcher/Framework/Migrations/Migration_2_0.ForBlueprints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ private void MergeIntoNewFormat(IDictionary<string, BuildingData> 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);
Expand All @@ -185,18 +179,8 @@ private void MergeIntoNewFormat(IDictionary<string, BuildingData> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ private void MergeIntoNewFormat(IDictionary<string, CharacterData> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,9 @@ private void MergeIntoNewFormat(IDictionary<string, ObjectData> 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);
Expand Down

0 comments on commit 3cc6908

Please sign in to comment.