From 1117b120558f9fe23307b88cfab2bd950695c190 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 22 Jul 2023 01:43:39 -0400 Subject: [PATCH] Revert "Change patch to transpiler" This reverts commit 1b7a5d26312af17c8285839ac86bfce0b50913ce. --- .../Patches/GlobalMusicControllerPatches.cs | 65 ++++++++----------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/NewHorizons/Patches/GlobalMusicControllerPatches.cs b/NewHorizons/Patches/GlobalMusicControllerPatches.cs index be0c66fb..776cb8e8 100644 --- a/NewHorizons/Patches/GlobalMusicControllerPatches.cs +++ b/NewHorizons/Patches/GlobalMusicControllerPatches.cs @@ -1,6 +1,4 @@ -using HarmonyLib; -using System.Collections.Generic; -using System.Reflection.Emit; +using HarmonyLib; using UnityEngine; namespace NewHorizons.Patches; @@ -10,40 +8,31 @@ public class GlobalMusicControllerPatches { private static AudioDetector _audioDetector; - [HarmonyTranspiler] - [HarmonyPatch(nameof(GlobalMusicController.UpdateBrambleMusic))] - public static IEnumerable GlobalMusicController_UpdateBrambleMusic(IEnumerable instructions, ILGenerator generator) - { - // This transpiler is to the check if dark bramble music should be playing #651 - // It essentially adds another boolean to a "should be playing" flag - return new CodeMatcher(instructions, generator) - // All the other bools point to this so we make a label there - .MatchForward(false, - new CodeMatch(OpCodes.Ldc_I4_0), - new CodeMatch(OpCodes.Stloc_0), - new CodeMatch(OpCodes.Ldarg_0), - new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(GlobalMusicController), nameof(GlobalMusicController._darkBrambleSource))), - new CodeMatch(OpCodes.Callvirt, AccessTools.Property(typeof(OWAudioSource), nameof(OWAudioSource.isPlaying)).GetGetMethod()) - ) - .CreateLabel(out Label label) - // Find the first part of the boolean assignment - .Start() - .MatchForward(true, - new CodeMatch(OpCodes.Call, typeof(Locator), nameof(Locator.GetPlayerSectorDetector)), - new CodeMatch(OpCodes.Callvirt, typeof(PlayerSectorDetector), nameof(PlayerSectorDetector.InBrambleDimension)), - new CodeMatch(OpCodes.Brfalse_S) - ) - // Insert a new check to it pointing to the same label as the others - .Insert( - new CodeMatch(OpCodes.Call, typeof(GlobalMusicControllerPatches), nameof(GlobalMusicControllerPatches.IsPlayerInNoAudioVolumes)), - new CodeMatch(OpCodes.Brfalse_S, label) - ) - .InstructionEnumeration(); - } + [HarmonyPrefix] + [HarmonyPatch(nameof(GlobalMusicController.UpdateBrambleMusic))] + public static bool GlobalMusicController_UpdateBrambleMusic(GlobalMusicController __instance) + { + // is this too hacky? + if (_audioDetector == null) _audioDetector = Object.FindObjectOfType(); - private static bool IsPlayerInNoAudioVolumes() - { - if (_audioDetector == null) _audioDetector = Object.FindObjectOfType(); - return _audioDetector._activeVolumes.Count == 0; - } + + var shouldBePlaying = Locator.GetPlayerSectorDetector().InBrambleDimension() && + !Locator.GetPlayerSectorDetector().InVesselDimension() && + PlayerState.AtFlightConsole() && + !PlayerState.IsHullBreached() && + !__instance._playingFinalEndTimes && + _audioDetector._activeVolumes.Count == 0; // change - don't play if in another audio volume + var playing = __instance._darkBrambleSource.isPlaying && + !__instance._darkBrambleSource.IsFadingOut(); + if (shouldBePlaying && !playing) + { + __instance._darkBrambleSource.FadeIn(5f); + } + else if (!shouldBePlaying && playing) + { + __instance._darkBrambleSource.FadeOut(5f); + } + + return false; + } }