From 525c0e108fd0769cc5c3cd4739b9ef50cf2550e0 Mon Sep 17 00:00:00 2001 From: ivan tkachenko Date: Fri, 22 Aug 2025 04:39:55 +0300 Subject: [PATCH] Refactor CurrentTrack to be less dependent on a global static --- MuzikaGromche/Plugin.cs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/MuzikaGromche/Plugin.cs b/MuzikaGromche/Plugin.cs index 1bc5ba9..b4a9a28 100644 --- a/MuzikaGromche/Plugin.cs +++ b/MuzikaGromche/Plugin.cs @@ -604,8 +604,6 @@ namespace MuzikaGromche return Tracks.SelectMany(track => track.GetTracks()).FirstOrDefault(track => track.Name == name); } - internal static IAudioTrack? CurrentTrack; - public static void SetLightColor(Color color) { foreach (var light in RoundManager.Instance.allPoweredLights) @@ -1855,6 +1853,9 @@ namespace MuzikaGromche : SyncedConfig2 #endif { + // Latest set track, used for loading palette and timings. + public static IAudioTrack? CurrentTrack { get; internal set; } = null; + public static ConfigEntry DisplayLyrics { get; private set; } = null!; public static ConfigEntry AudioOffset { get; private set; } = null!; @@ -2067,7 +2068,7 @@ namespace MuzikaGromche void load() { - var palette = (Plugin.CurrentTrack as CoreAudioTrack)?._Palette ?? Palette.DEFAULT; + var palette = (CurrentTrack as CoreAudioTrack)?._Palette ?? Palette.DEFAULT; var colors = palette.Colors; var count = Math.Min(colors.Count(), maxCustomPaletteSize); @@ -2207,7 +2208,7 @@ namespace MuzikaGromche void load() { - var track = Plugin.CurrentTrack; + var track = CurrentTrack; foreach (var entry in entries) { entry.Load(track as CoreAudioTrack); @@ -2265,6 +2266,7 @@ namespace MuzikaGromche // Resets on SettingChanged. private int SelectedTrackIndex = 0; + internal IAudioTrack? CurrentTrack = null; internal BeatTimeState? BeatTimeState = null; internal AudioSource IntroAudioSource = null!; internal AudioSource LoopAudioSource = null!; @@ -2352,7 +2354,7 @@ namespace MuzikaGromche public void SetTrackClientRpc(string name) { Debug.Log($"{nameof(MuzikaGromche)} SetTrackClientRpc {name}"); - Plugin.CurrentTrack = Plugin.FindTrackNamed(name); + Config.CurrentTrack = CurrentTrack = Plugin.FindTrackNamed(name); } [ServerRpc] @@ -2413,7 +2415,11 @@ namespace MuzikaGromche [HarmonyPostfix] static void JesterUpdatePostfix(JesterAI __instance, State __state) { - if (Plugin.CurrentTrack == null) + var behaviour = __instance.GetComponent(); + var introAudioSource = behaviour.IntroAudioSource; + var loopAudioSource = behaviour.LoopAudioSource; + + if (behaviour.CurrentTrack == null) { #if DEBUG Debug.LogError($"{nameof(MuzikaGromche)} CurrentTrack is not set!"); @@ -2421,10 +2427,6 @@ namespace MuzikaGromche return; } - var behaviour = __instance.GetComponent(); - var introAudioSource = behaviour.IntroAudioSource; - var loopAudioSource = behaviour.LoopAudioSource; - // This switch statement resembles the one from JesterAI.Update switch (__state.currentBehaviourStateIndex) { @@ -2434,18 +2436,18 @@ namespace MuzikaGromche // if just started winding up // then stop the default music... (already done above) // ...and set up both modded audio clips in advance - introAudioSource.clip = Plugin.CurrentTrack.LoadedIntro; - loopAudioSource.clip = Plugin.CurrentTrack.LoadedLoop; - behaviour.BeatTimeState = new BeatTimeState(Plugin.CurrentTrack); + introAudioSource.clip = behaviour.CurrentTrack.LoadedIntro; + loopAudioSource.clip = behaviour.CurrentTrack.LoadedLoop; + behaviour.BeatTimeState = new BeatTimeState(behaviour.CurrentTrack); // Set up custom popup timer, which is shorter than Intro audio - __instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer; + __instance.popUpTimer = behaviour.CurrentTrack.WindUpTimer; if (Config.ShouldSkipWindingPhase) { var rewind = 5f; __instance.popUpTimer = rewind; - introAudioSource.time = Plugin.CurrentTrack.WindUpTimer - rewind; + introAudioSource.time = behaviour.CurrentTrack.WindUpTimer - rewind; } else { @@ -2537,7 +2539,7 @@ namespace MuzikaGromche DiscoBallManager.Disable(); // Just in case if players have spawned multiple Jesters, // Don't reset Plugin.CurrentTrack to null, - // so that the code wouldn't crash without extra null checks. + // so that the latest chosen track remains set. } } }