From 63cd86883b7d6fafe3ee8c67137b534d157bae97 Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Fri, 14 Jun 2024 15:07:40 -0400 Subject: [PATCH] Patch UpdateEndTimesMusic --- .../External/Configs/StarSystemConfig.cs | 4 +- .../Patches/GlobalMusicControllerPatches.cs | 41 +++++++++++++++++-- NewHorizons/Utility/NewHorizonExtensions.cs | 24 +++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 1ce52e0e..a5fcb088 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -204,12 +204,12 @@ namespace NewHorizons.External.Configs public string travelAudio; /// - /// The audio that will play for 85 seconds before the loop ends. Can be a path to a .wav/.ogg/.mp3 file, or taken from the AudioClip list. + /// The audio that will play right before the loop ends. Can be a path to a .wav/.ogg/.mp3 file, or taken from the AudioClip list. /// public string endTimesAudio; /// - /// The audio that will play for 85 seconds before the loop ends while inside the dreamworld. Can be a path to a .wav/.ogg/.mp3 file, or taken from the AudioClip list. + /// The audio that will play right before the loop ends while inside the dreamworld. Can be a path to a .wav/.ogg/.mp3 file, or taken from the AudioClip list. /// public string endTimesDreamAudio; diff --git a/NewHorizons/Patches/GlobalMusicControllerPatches.cs b/NewHorizons/Patches/GlobalMusicControllerPatches.cs index b6fbb823..9697beb7 100644 --- a/NewHorizons/Patches/GlobalMusicControllerPatches.cs +++ b/NewHorizons/Patches/GlobalMusicControllerPatches.cs @@ -1,11 +1,14 @@ using HarmonyLib; using NewHorizons.Components.EOTE; +using NewHorizons.Utility; +using System.Collections.Generic; +using System.Reflection.Emit; using UnityEngine; namespace NewHorizons.Patches; [HarmonyPatch(typeof(GlobalMusicController))] -public class GlobalMusicControllerPatches +public static class GlobalMusicControllerPatches { private static AudioDetector _audioDetector; @@ -35,10 +38,42 @@ public class GlobalMusicControllerPatches } return false; - } + } + /// + /// Replaces any 85f with + /// + [HarmonyTranspiler] + [HarmonyPatch(nameof(GlobalMusicController.UpdateEndTimesMusic))] + public static IEnumerable GlobalMusicController_UpdateEndTimesMusic(IEnumerable instructions, ILGenerator generator) + { + return new CodeMatcher(instructions, generator).MatchForward(true, + new CodeMatch(OpCodes.Call), + new CodeMatch(OpCodes.Ldc_R4, 85f), + new CodeMatch(OpCodes.Ble_Un) + ).Advance(-1).RemoveInstruction().Insert( + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(NewHorizonsExtensions), nameof(NewHorizonsExtensions.GetSecondsBeforeSupernovaPlayTime))) + ).MatchForward(true, + new CodeMatch(OpCodes.Call), + new CodeMatch(OpCodes.Ldc_R4, 85f), + new CodeMatch(OpCodes.Bge_Un) + ).Advance(-1).RemoveInstruction().Insert( + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(NewHorizonsExtensions), nameof(NewHorizonsExtensions.GetSecondsBeforeSupernovaPlayTime))) + ).MatchForward(false, + new CodeMatch(OpCodes.Ldc_R4, 85f), + new CodeMatch(OpCodes.Call), + new CodeMatch(OpCodes.Sub) + ).RemoveInstruction().Insert( + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(NewHorizonsExtensions), nameof(NewHorizonsExtensions.GetSecondsBeforeSupernovaPlayTime))) + ).InstructionEnumeration(); + } - [HarmonyPrefix] + // Custom end times for dreamworld + + [HarmonyPrefix] [HarmonyPatch(nameof(GlobalMusicController.OnEnterDreamWorld))] public static bool GlobalMusicController_OnEnterDreamWorld(GlobalMusicController __instance) { diff --git a/NewHorizons/Utility/NewHorizonExtensions.cs b/NewHorizons/Utility/NewHorizonExtensions.cs index e11609f3..2e52a5e7 100644 --- a/NewHorizons/Utility/NewHorizonExtensions.cs +++ b/NewHorizons/Utility/NewHorizonExtensions.cs @@ -1,3 +1,4 @@ +using HarmonyLib; using NewHorizons.External.Configs; using NewHorizons.External.Modules.VariableSize; using NewHorizons.External.SerializableData; @@ -425,5 +426,28 @@ namespace NewHorizons.Utility playerCameraEffectController._owCamera.postProcessingSettings.bloom.threshold = 0f; playerCameraEffectController._owCamera.postProcessingSettings.eyeMaskEnabled = true; } + + public static float GetSecondsBeforeSupernovaPlayTime(this GlobalMusicController globalMusicController) + { + var clip = globalMusicController._endTimesSource.audioLibraryClip; + if (clip == AudioType.EndOfTime || clip == AudioType.EndOfTime_Dream) + return GlobalMusicController.secondsBeforeSupernovaPlayTime; + return globalMusicController._endTimesSource.clip.length; + } + + public static CodeMatcher LogInstructions(this CodeMatcher matcher, string prefix) + { + matcher.InstructionEnumeration().LogInstructions(prefix); + return matcher; + } + + public static IEnumerable LogInstructions(this IEnumerable instructions, string prefix) + { + var message = prefix; + foreach (var instruction in instructions) + message += $"\n{instruction}"; + Debug.LogError(message); + return instructions; + } } }