From caa4b9ccbd7b8f279eb609040147b4127667514e Mon Sep 17 00:00:00 2001 From: ivan tkachenko Date: Sat, 12 Jul 2025 20:16:59 +0300 Subject: [PATCH] Reorder some statements to make them visually more grouped together Postfix patch went from 5 if-blocks down to only 3 \o/ There is no need to stop the creatureVoice and start it delayed in two separate condition blocks. Also, the code should only rely on state transitions, and not on AudioSource.isPlaying property. --- MuzikaGromche/Plugin.cs | 47 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/MuzikaGromche/Plugin.cs b/MuzikaGromche/Plugin.cs index 7f256b3..c2ddb19 100644 --- a/MuzikaGromche/Plugin.cs +++ b/MuzikaGromche/Plugin.cs @@ -387,6 +387,8 @@ namespace MuzikaGromche } } + // farAudio is during windup, Start overrides popGoesTheWeaselTheme + // creatureVoice is when popped, Loop overrides screamingSFX [HarmonyPatch(typeof(JesterAI))] internal class JesterPatch { @@ -404,13 +406,18 @@ namespace MuzikaGromche { __state = new State { - previousState = __instance.previousState + farAudio = __instance.farAudio, + previousState = __instance.previousState, }; if (__instance.currentBehaviourStateIndex == 2 && __instance.previousState != 2) { - // if just popped out - // then override farAudio so that vanilla logic does not stop the music - __state.farAudio = __instance.farAudio; + // If just popped out, then override farAudio so that vanilla logic does not stop the modded Start music. + // The game will stop farAudio it during its Update, so we temporarily set it to any other AudioSource + // which we don't care about stopping for now. + // + // Why creatureVoice though? We gonna need creatureVoice later in Postfix to schedule the Loop, + // but right now we still don't care if it's stopped, so it shouldn't matter. + // And it's cheaper and simpler than figuring out how to instantiate an AudioSource behaviour. __instance.farAudio = __instance.creatureVoice; } } @@ -419,11 +426,6 @@ namespace MuzikaGromche [HarmonyPostfix] public static void DoNotStopTheMusic(JesterAI __instance, State __state) { - if (__state.farAudio != null) - { - __instance.farAudio = __state.farAudio; - } - if (__instance.previousState == 1 && __state.previousState != 1) { // if just started winding up @@ -433,18 +435,16 @@ namespace MuzikaGromche // ...and start modded music Plugin.CurrentTrack = Plugin.ChooseTrack(); + // Set up custom __instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer; + + // Override popGoesTheWeaselTheme with Start audio __instance.farAudio.maxDistance = 150; __instance.farAudio.clip = Plugin.CurrentTrack.LoadedStart; __instance.farAudio.loop = false; - Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}"); __instance.farAudio.Play(); - } - if (__instance.previousState == 2 && __state.previousState != 2) - { - __instance.creatureVoice.Stop(); - Plugin.StartLightSwitching(__instance); + Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}"); } if (__instance.previousState != 2 && __state.previousState == 2) @@ -453,15 +453,24 @@ namespace MuzikaGromche Plugin.ResetLightColor(); } - if (__instance.previousState == 2 && !__instance.creatureVoice.isPlaying) + if (__instance.previousState == 2 && __state.previousState != 2) { - __instance.creatureVoice.maxDistance = 150; - __instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop; + // Restore stashed AudioSource. See the comment in Prefix + __instance.farAudio = __state.farAudio; + var time = __instance.farAudio.time; var delay = Plugin.CurrentTrack.LoadedStart.length - time; + + // Override screamingSFX with Loop, delayed by the remaining time of the Start audio + __instance.creatureVoice.Stop(); + __instance.creatureVoice.maxDistance = 150; + __instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop; + __instance.creatureVoice.PlayDelayed(delay); + Debug.Log($"Start length: {Plugin.CurrentTrack.LoadedStart.length}; played time: {time}"); Debug.Log($"Playing loop music: maxDistance: {__instance.creatureVoice.maxDistance}, minDistance: {__instance.creatureVoice.minDistance}, volume: {__instance.creatureVoice.volume}, spread: {__instance.creatureVoice.spread}, in seconds: {delay}"); - __instance.creatureVoice.PlayDelayed(delay); + + Plugin.StartLightSwitching(__instance); } } }