Refactor CurrentTrack to be less dependent on a global static

This commit is contained in:
ivan tkachenko 2025-08-22 04:39:55 +03:00
parent 73ad702684
commit 525c0e108f
1 changed files with 18 additions and 16 deletions

View File

@ -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<Config>
#endif
{
// Latest set track, used for loading palette and timings.
public static IAudioTrack? CurrentTrack { get; internal set; } = null;
public static ConfigEntry<bool> DisplayLyrics { get; private set; } = null!;
public static ConfigEntry<float> 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<MuzikaGromcheJesterNetworkBehaviour>();
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<MuzikaGromcheJesterNetworkBehaviour>();
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.
}
}
}