Patched in event for CreditsBuilt and refactored NHGameOverManager.LoadCustomCreditsScene() to implement it and remove the janky task

This commit is contained in:
josshmot 2025-04-08 17:30:43 +10:00
parent 0e6555d933
commit 5d680277ec
2 changed files with 28 additions and 28 deletions

View File

@ -1,9 +1,11 @@
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.External.SerializableEnums; using NewHorizons.External.SerializableEnums;
using NewHorizons.Handlers; using NewHorizons.Handlers;
using NewHorizons.Patches.CreditsScenePatches;
using NewHorizons.Utility.Files; using NewHorizons.Utility.Files;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using OWML.Common; using OWML.Common;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -143,15 +145,13 @@ namespace NewHorizons.Components
private void LoadCustomCreditsScene(GameOverModule gameOver, IModBehaviour mod) private void LoadCustomCreditsScene(GameOverModule gameOver, IModBehaviour mod)
{ {
var fromScene = LoadManager.GetCurrentScene(); LoadManager.LoadScene(OWScene.Credits_Fast, LoadManager.FadeType.ToBlack);
var toScene = OWScene.Credits_Fast;
LoadManager.LoadScene(toScene, LoadManager.FadeType.ToBlack); // 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.
// 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. onCreditsBuilt = (sender, e) =>
LoadManager.SceneLoadEvent completeCreditsLoad = null; // needs to be done so we can unsubscribe from within the lambda.
completeCreditsLoad = (fromScene, toScene) =>
{ {
// Patch new music clip // Patch new music clip
var musicSource = Locator.FindObjectsOfType<OWAudioSource>().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. var musicSource = Locator.FindObjectsOfType<OWAudioSource>().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) if (mod is not null)
@ -167,31 +167,18 @@ namespace NewHorizons.Components
musicSource.loop = gameOver.audioLooping; musicSource.loop = gameOver.audioLooping;
musicSource._maxSourceVolume = gameOver.audioVolume; musicSource._maxSourceVolume = gameOver.audioVolume;
// Janky wait until credits are built // Override fade in
Task.Run( () => musicSource.Stop();
{ musicSource.Play();
var startTime = Time.time;
while (Locator.FindObjectsOfType<CreditsScrollSection>().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 // Patch scroll duration
musicSource.Stop(); var creditsScroll = Locator.FindObjectOfType<CreditsScrollSection>();
musicSource.Play(); creditsScroll._scrollDuration = gameOver.length;
// Patch scroll duration CreditsPatches.CreditsBuilt -= onCreditsBuilt;
var creditsScroll = Locator.FindObjectOfType<CreditsScrollSection>();
creditsScroll._scrollDuration = gameOver.length;
});
LoadManager.OnCompleteSceneLoad -= completeCreditsLoad;
}; };
LoadManager.OnCompleteSceneLoad += completeCreditsLoad; CreditsPatches.CreditsBuilt += onCreditsBuilt;
} }
} }
} }

View File

@ -1,17 +1,30 @@
using HarmonyLib; using HarmonyLib;
using NewHorizons.Handlers; using NewHorizons.Handlers;
using System;
namespace NewHorizons.Patches.CreditsScenePatches namespace NewHorizons.Patches.CreditsScenePatches
{ {
[HarmonyPatch(typeof(Credits))] [HarmonyPatch(typeof(Credits))]
public static class CreditsPatches public static class CreditsPatches
{ {
public static event EventHandler CreditsBuilt; // Used in NHGameOverManager to patch credits music and scroll speed
[HarmonyPrefix] [HarmonyPrefix]
[HarmonyPatch(nameof(Credits.Start))] [HarmonyPatch(nameof(Credits.Start))]
public static void Credits_Start(Credits __instance) public static void Credits_Start(Credits __instance)
{ {
CreditsHandler.AddCredits(__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());
}
} }
} }