From b1cf0aa2fc30a5d9165e6f5406fab41294f6269a Mon Sep 17 00:00:00 2001 From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com> Date: Sun, 4 Sep 2022 03:16:33 -0500 Subject: [PATCH] Avoid changing intro dictionary in HTTP controller --- .../Controllers/SkipIntroController.cs | 3 ++- ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs index 5a47f15..d6c97f6 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs @@ -54,7 +54,8 @@ public class SkipIntroController : ControllerBase /// Intro object if the provided item has an intro, null otherwise. private Intro? GetIntro(Guid id) { - return Plugin.Instance!.Intros.TryGetValue(id, out var intro) ? intro : null; + // Returns a copy to avoid mutating the original Intro object stored in the dictionary. + return Plugin.Instance!.Intros.TryGetValue(id, out var intro) ? new Intro(intro) : null; } /// diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs index 4cd88e5..731778c 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs @@ -32,6 +32,17 @@ public class Intro IntroEnd = 0; } + /// + /// Initializes a new instance of the class. + /// + /// intro. + public Intro(Intro intro) + { + EpisodeId = intro.EpisodeId; + IntroStart = intro.IntroStart; + IntroEnd = intro.IntroEnd; + } + /// /// Initializes a new instance of the class. ///