diff --git a/NewHorizons/Components/NHGameOverManager.cs b/NewHorizons/Components/NHGameOverManager.cs index 25f3549e..087205f6 100644 --- a/NewHorizons/Components/NHGameOverManager.cs +++ b/NewHorizons/Components/NHGameOverManager.cs @@ -1,9 +1,11 @@ using NewHorizons.External.Modules; using NewHorizons.External.SerializableEnums; using NewHorizons.Handlers; +using NewHorizons.Patches.CreditsScenePatches; using NewHorizons.Utility.Files; using NewHorizons.Utility.OWML; using OWML.Common; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -143,15 +145,13 @@ namespace NewHorizons.Components private void LoadCustomCreditsScene(GameOverModule gameOver, IModBehaviour mod) { - var fromScene = LoadManager.GetCurrentScene(); - var toScene = OWScene.Credits_Fast; + LoadManager.LoadScene(OWScene.Credits_Fast, LoadManager.FadeType.ToBlack); - LoadManager.LoadScene(toScene, LoadManager.FadeType.ToBlack); - - // Unfortunately we can't make this a private method, as LoadManager.SceneLoadEvent enforces the (fromScene, toScene) parameters, which prevents us from passing in gameOver and mod, which we need. - LoadManager.SceneLoadEvent completeCreditsLoad = null; // needs to be done so we can unsubscribe from within the lambda. - completeCreditsLoad = (fromScene, toScene) => + // Unfortunately we can't make this a private method, as EventArgs/EventHandler enforces the (sender, e) parameters, which prevents us from passing in gameOver and mod, which we need. + EventHandler onCreditsBuilt = null; // needs to be done so we can unsubscribe from within the lambda. + onCreditsBuilt = (sender, e) => { + // Patch new music clip var musicSource = Locator.FindObjectsOfType().Where(x => x.name == "AudioSource").Single(); // AudioSource that plays the credits music is literally called "AudioSource", luckily it's the only one called that. Lazy OW devs do be lazy. if (mod is not null) @@ -167,31 +167,18 @@ namespace NewHorizons.Components musicSource.loop = gameOver.audioLooping; musicSource._maxSourceVolume = gameOver.audioVolume; - // Janky wait until credits are built - Task.Run( () => - { - var startTime = Time.time; - while (Locator.FindObjectsOfType().Length == 0) { - if (Time.time > startTime + 0.1f) - { - NHLogger.LogError("Timeout while waiting for credits to be built. Scroll duration won't be changed."); - return; - } - } + // Override fade in + musicSource.Stop(); + musicSource.Play(); - // Override fade in - musicSource.Stop(); - musicSource.Play(); + // Patch scroll duration + var creditsScroll = Locator.FindObjectOfType(); + creditsScroll._scrollDuration = gameOver.length; - // Patch scroll duration - var creditsScroll = Locator.FindObjectOfType(); - creditsScroll._scrollDuration = gameOver.length; - }); - - LoadManager.OnCompleteSceneLoad -= completeCreditsLoad; + CreditsPatches.CreditsBuilt -= onCreditsBuilt; }; - LoadManager.OnCompleteSceneLoad += completeCreditsLoad; + CreditsPatches.CreditsBuilt += onCreditsBuilt; } } } diff --git a/NewHorizons/Patches/CreditsScenePatches/CreditsPatches.cs b/NewHorizons/Patches/CreditsScenePatches/CreditsPatches.cs index 8892f334..037b30bf 100644 --- a/NewHorizons/Patches/CreditsScenePatches/CreditsPatches.cs +++ b/NewHorizons/Patches/CreditsScenePatches/CreditsPatches.cs @@ -1,17 +1,30 @@ using HarmonyLib; using NewHorizons.Handlers; +using System; namespace NewHorizons.Patches.CreditsScenePatches { [HarmonyPatch(typeof(Credits))] public static class CreditsPatches { + public static event EventHandler CreditsBuilt; // Used in NHGameOverManager to patch credits music and scroll speed + [HarmonyPrefix] [HarmonyPatch(nameof(Credits.Start))] public static void Credits_Start(Credits __instance) { CreditsHandler.AddCredits(__instance); } + + [HarmonyPostfix] + [HarmonyPatch(nameof(Credits.BuildCredits))] + public static void Credits_BuildCredits_Post(Credits __instance) + { + // Do things BuildCredits() normally does + + // Fire event once finished + CreditsBuilt?.Invoke(__instance, new EventArgs()); + } } }