summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-08 15:30:27 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-08 15:30:27 -0500
commit28c78e8f25a0d062d0dda49bc089ebebef8bfa20 (patch)
treef7a10064799420daf692f8d6f24634735e079cb6
parentb2e88bccf63545d950f4cbf47a0466321c614245 (diff)
downloadSMAPI-28c78e8f25a0d062d0dda49bc089ebebef8bfa20.tar.gz
SMAPI-28c78e8f25a0d062d0dda49bc089ebebef8bfa20.tar.bz2
SMAPI-28c78e8f25a0d062d0dda49bc089ebebef8bfa20.zip
add dict content helper method to replace values based on a lambda (#173)
-rw-r--r--src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs9
-rw-r--r--src/StardewModdingAPI/IContentEventHelperForDictionary.cs4
2 files changed, 13 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
index 8fcaae3c..9bfc16d8 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
+++ b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
namespace StardewModdingAPI.Framework.Content
{
@@ -32,5 +33,13 @@ namespace StardewModdingAPI.Framework.Content
{
this.Data[key] = value(this.Data[key]);
}
+
+ /// <summary>Dynamically replace values in the dictionary.</summary>
+ /// <param name="replacer">A lambda which takes the current key and value for an entry, and returns the new value.</param>
+ public void Replace(Func<TKey, TValue, TValue> replacer)
+ {
+ foreach (var pair in this.Data.ToArray())
+ this.Data[pair.Key] = replacer(pair.Key, pair.Value);
+ }
}
}
diff --git a/src/StardewModdingAPI/IContentEventHelperForDictionary.cs b/src/StardewModdingAPI/IContentEventHelperForDictionary.cs
index 1f259920..34796d5c 100644
--- a/src/StardewModdingAPI/IContentEventHelperForDictionary.cs
+++ b/src/StardewModdingAPI/IContentEventHelperForDictionary.cs
@@ -36,6 +36,10 @@ namespace StardewModdingAPI
/// <param name="value">A callback which accepts the current value and returns the new value.</param>
void SetEntry(TKey key, Func<TValue, TValue> value);
+ /// <summary>Dynamically replace values in the dictionary.</summary>
+ /// <param name="replacer">A lambda which takes the current key and value for an entry, and returns the new value.</param>
+ void Replace(Func<TKey, TValue, TValue> replacer);
+
/// <summary>Replace the entire content value with the given value. This is generally not recommended, since it may break compatibility with other mods or different versions of the game.</summary>
/// <param name="value">The new content value.</param>
/// <exception cref="ArgumentNullException">The <paramref name="value"/> is null.</exception>