From 28c78e8f25a0d062d0dda49bc089ebebef8bfa20 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 8 Mar 2017 15:30:27 -0500 Subject: add dict content helper method to replace values based on a lambda (#173) --- .../Framework/Content/ContentEventHelperForDictionary.cs | 9 +++++++++ src/StardewModdingAPI/IContentEventHelperForDictionary.cs | 4 ++++ 2 files changed, 13 insertions(+) (limited to 'src') 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]); } + + /// Dynamically replace values in the dictionary. + /// A lambda which takes the current key and value for an entry, and returns the new value. + public void Replace(Func 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 /// A callback which accepts the current value and returns the new value. void SetEntry(TKey key, Func value); + /// Dynamically replace values in the dictionary. + /// A lambda which takes the current key and value for an entry, and returns the new value. + void Replace(Func replacer); + /// 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. /// The new content value. /// The is null. -- cgit