Decompiled source of Music Fixes Extended v2.0.0

PataponAudioFixes.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using P1.Sound;
using P1.System.Sound;
using P1.System.Sound.Rhythm;
using P2.Sound;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("PataponAudioFixes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PataponAudioFixes")]
[assembly: AssemblyTitle("PataponAudioFixes")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace PataponAudioFix
{
	[HarmonyPatch(typeof(Rhythm), "playRhythm")]
	internal static class P1RhythmPlayCountVirtualWrapFix
	{
		private static void Prefix(Rhythm __instance, PlayRequest rhythmPlayRequest, out PlayCountFixState __state)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			__state = new PlayCountFixState();
			if (!Plugin.ThemeLoopFixEnabled)
			{
				return;
			}
			try
			{
				if (__instance != null && rhythmPlayRequest != null && (int)rhythmPlayRequest.rhythmMood == 2 && (int)__instance.lastMood_ == 2)
				{
					uint playCount_ = __instance.playCount_;
					if (playCount_ > 16)
					{
						uint num = (playCount_ - 1) % 16 + 1;
						__state.Applied = true;
						__state.OriginalPlayCount = playCount_;
						__state.VirtualPlayCount = num;
						__instance.playCount_ = num;
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(24, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 FIX] Prefix failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}

		private static void Postfix(Rhythm __instance, bool __result, PlayCountFixState __state)
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			try
			{
				if (__instance != null && __state != null && __state.Applied)
				{
					__instance.playCount_ = (__result ? (__state.OriginalPlayCount + 1) : __state.OriginalPlayCount);
					Plugin.D($"[P1 FIX] Virtualized Mood_High playCount {__state.OriginalPlayCount} -> {__state.VirtualPlayCount}, restored to {__instance.playCount_}");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(25, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 FIX] Postfix failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}
	}
	internal static class P1HelperMidiState
	{
		public static BgmPlayer? HelperPlayer;

		public static bool SequenceActive;

		public static bool HelperFadedOut;

		public static float CachedTargetVolume = 1f;

		private static bool HasLastRhythmState;

		private static Mood LastMood;

		private static uint LastPlayCount;

		private static bool LastIsPlay;

		public static void Reset(string source)
		{
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (HelperFadedOut && HelperPlayer != null)
				{
					HelperPlayer.setMute(0u);
					Player? innerPlayer = GetInnerPlayer(HelperPlayer);
					if (innerPlayer != null)
					{
						innerPlayer.setVolumeDirect(CachedTargetVolume);
					}
					Plugin.D($"[P1 HELPER MIDI] reset: inner restored to {CachedTargetVolume} from {source}");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(52, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] reset restore-volume failed from ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(source);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
			SequenceActive = false;
			HelperFadedOut = false;
			HelperPlayer = null;
			HasLastRhythmState = false;
			LastMood = (Mood)0;
			LastPlayCount = 0u;
			LastIsPlay = false;
			Plugin.D("[P1 HELPER MIDI] reset from " + source);
		}

		private static Player? GetInnerPlayer(BgmPlayer player)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			try
			{
				RhythmBgmPlayer obj = ((Il2CppObjectBase)player).TryCast<RhythmBgmPlayer>();
				return (obj != null) ? obj.rhythm_ : null;
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(40, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] GetInnerPlayer failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
				return null;
			}
		}

		public static float ReadInnerVolume(BgmPlayer player)
		{
			Player innerPlayer = GetInnerPlayer(player);
			if (innerPlayer == null)
			{
				return -1f;
			}
			return innerPlayer.getVolume();
		}

		public static void SetHelperVolume(bool fadedOut, string source, bool force = false)
		{
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				if (!force && HelperFadedOut == fadedOut)
				{
					return;
				}
				BgmPlayer helperPlayer = HelperPlayer;
				if (helperPlayer == null)
				{
					ManualLogSource l = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(65, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] wanted fadedOut=");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(fadedOut);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" from ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(source);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", but helperPlayer is null");
					}
					l.LogWarning(val);
					HelperFadedOut = fadedOut;
					return;
				}
				Player innerPlayer = GetInnerPlayer(helperPlayer);
				if (innerPlayer == null)
				{
					ManualLogSource l2 = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(75, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] inner rhythm Player is null from ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(source);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("; falling back to setMute");
					}
					l2.LogWarning(val);
					helperPlayer.setMute(fadedOut ? uint.MaxValue : 0u);
					HelperFadedOut = fadedOut;
					return;
				}
				if (fadedOut)
				{
					innerPlayer.setVolumeDirect(0f);
					Plugin.D($"[P1 HELPER MIDI] INNER-ZERO from {source}, innerVol={innerPlayer.getVolume()}, rid={helperPlayer.getRID()}, isPlay={helperPlayer.isPlay()}");
				}
				else
				{
					innerPlayer.setVolumeDirect(CachedTargetVolume);
					Plugin.D($"[P1 HELPER MIDI] INNER-FULL vol={CachedTargetVolume} from {source}, innerVol={innerPlayer.getVolume()}, rid={helperPlayer.getRID()}, isPlay={helperPlayer.isPlay()}");
				}
				HelperFadedOut = fadedOut;
			}
			catch (Exception ex)
			{
				ManualLogSource l3 = Plugin.L;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(58, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] SetHelperVolume(fadedOut=");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(fadedOut);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(") failed from ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(source);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l3.LogWarning(val);
			}
		}

		public static void ResetProgress(string source)
		{
			SequenceActive = false;
			SetHelperVolume(fadedOut: false, source, force: true);
			Plugin.D("[P1 HELPER MIDI] progress reset from " + source);
		}

		public static void OnSuccessfulRhythm(Rhythm rhythm, PlayRequest request)
		{
		}

		public static void WatchRhythmState(Rhythm rhythm)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Expected O, but got Unknown
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Mood lastMood_ = rhythm.lastMood_;
				uint playCount_ = rhythm.playCount_;
				bool isPlay_ = rhythm.isPlay_;
				if (!HasLastRhythmState)
				{
					HasLastRhythmState = true;
					LastMood = lastMood_;
					LastPlayCount = playCount_;
					LastIsPlay = isPlay_;
					return;
				}
				if (lastMood_ != LastMood || playCount_ != LastPlayCount || isPlay_ != LastIsPlay)
				{
					Plugin.D($"[P1 RHYTHM TRANS] mood={LastMood}->{lastMood_}, playCount={LastPlayCount}->{playCount_}, isPlay={LastIsPlay}->{isPlay_}, seqActive={SequenceActive}, fadedOut={HelperFadedOut}");
				}
				if (isPlay_ && !LastIsPlay)
				{
					SequenceActive = true;
					SetHelperVolume(fadedOut: true, $"chant begin (isPlay->True) mood={lastMood_}, playCount={playCount_}");
				}
				else if (!isPlay_ && LastIsPlay)
				{
					SequenceActive = false;
					SetHelperVolume(fadedOut: false, $"input window (isPlay->False) mood={lastMood_}, playCount={playCount_}");
				}
				LastMood = lastMood_;
				LastPlayCount = playCount_;
				LastIsPlay = isPlay_;
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(42, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] WatchRhythmState failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}
	}
	[HarmonyPatch(typeof(Director), "playMidi")]
	internal static class P1CaptureHelperMidiPlayer
	{
		private static void Postfix(Director __instance, int midiIndex, MixingCategory cate, float fadeTime)
		{
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Invalid comparison between Unknown and I4
			if (!Plugin.HelperMuteEnabled)
			{
				return;
			}
			try
			{
				if (!P1HelperMidiState.SequenceActive && midiIndex == 0 && (int)cate == 3)
				{
					P1HelperMidiState.Reset("playMidi(0, BGM)");
				}
				else
				{
					if (midiIndex != 0 || (int)cate != 4)
					{
						return;
					}
					BgmPlayer curPlayer_ = __instance.curPlayer_;
					if (curPlayer_ == null)
					{
						Plugin.L.LogWarning((object)"[P1 HELPER MIDI] wanted to capture helper player, but curPlayer_ is null");
						return;
					}
					bool value = P1HelperMidiState.HelperPlayer != null;
					P1HelperMidiState.HelperPlayer = curPlayer_;
					float num = -1f;
					if (!P1HelperMidiState.HelperFadedOut)
					{
						num = P1HelperMidiState.ReadInnerVolume(curPlayer_);
						if (num > 0f)
						{
							P1HelperMidiState.CachedTargetVolume = num;
						}
					}
					Plugin.D($"[P1 HELPER MIDI] CAPTURED helper from playMidi(0, Rhythm), hadCachedPlayer={value}, playId={curPlayer_.playId_}, rid={curPlayer_.getRID()}, innerVol={num}, cachedTarget={P1HelperMidiState.CachedTargetVolume}, isPlay={curPlayer_.isPlay()}, sequenceActive={P1HelperMidiState.SequenceActive}");
					if (P1HelperMidiState.SequenceActive && P1HelperMidiState.HelperFadedOut)
					{
						P1HelperMidiState.SetHelperVolume(fadedOut: true, "re-applied fade after playMidi(0, Rhythm) recapture", force: true);
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(33, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P1 HELPER MIDI] capture failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}
	}
	[HarmonyPatch(typeof(Rhythm), "playRhythm")]
	internal static class P1HelperOnSuccessfulRhythm
	{
		private static void Postfix(Rhythm __instance, PlayRequest rhythmPlayRequest, bool __result)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.HelperMuteEnabled && __instance != null && rhythmPlayRequest != null)
			{
				Plugin.D($"[P1 EVENT] playRhythm result={__result}, mood={rhythmPlayRequest.rhythmMood}, lastMood={__instance.lastMood_}, playCount={__instance.playCount_}, playSEIndex={__instance.playSEIndex_}, isPlay={__instance.isPlay_}, seqActive={P1HelperMidiState.SequenceActive}");
				if (__result)
				{
					P1HelperMidiState.OnSuccessfulRhythm(__instance, rhythmPlayRequest);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Rhythm), "update")]
	internal static class P1HelperWatchRhythmState
	{
		private static void Postfix(Rhythm __instance)
		{
			if (Plugin.HelperMuteEnabled && __instance != null)
			{
				P1HelperMidiState.WatchRhythmState(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Rhythm), "clearPlayCount")]
	internal static class P1HelperResetOnClearPlayCount
	{
		private static void Postfix()
		{
			Plugin.D($"[P1 EVENT] Rhythm.clearPlayCount fired, seqActive={P1HelperMidiState.SequenceActive}, fadedOut={P1HelperMidiState.HelperFadedOut}");
			if (Plugin.HelperMuteEnabled && (P1HelperMidiState.SequenceActive || P1HelperMidiState.HelperFadedOut))
			{
				P1HelperMidiState.ResetProgress("Rhythm.clearPlayCount");
			}
		}
	}
	[HarmonyPatch(typeof(Rhythm), "resetPlayCount")]
	internal static class P1HelperResetOnResetPlayCount
	{
		private static void Postfix()
		{
			Plugin.D($"[P1 EVENT] Rhythm.resetPlayCount fired, seqActive={P1HelperMidiState.SequenceActive}, fadedOut={P1HelperMidiState.HelperFadedOut}");
			if (Plugin.HelperMuteEnabled && (P1HelperMidiState.SequenceActive || P1HelperMidiState.HelperFadedOut))
			{
				P1HelperMidiState.ResetProgress("Rhythm.resetPlayCount");
			}
		}
	}
	[HarmonyPatch(typeof(Director), "moodDown")]
	internal static class P1HelperResetOnMoodDown
	{
		private static void Postfix()
		{
			Plugin.D($"[P1 EVENT] Director.moodDown fired, seqActive={P1HelperMidiState.SequenceActive}, fadedOut={P1HelperMidiState.HelperFadedOut}");
			if (Plugin.HelperMuteEnabled && (P1HelperMidiState.SequenceActive || P1HelperMidiState.HelperFadedOut))
			{
				P1HelperMidiState.ResetProgress("Director.moodDown");
			}
		}
	}
	[HarmonyPatch(typeof(Director), "startPlayGame")]
	internal static class P1HelperResetOnStartPlayGame
	{
		private static void Prefix()
		{
			if (Plugin.HelperMuteEnabled)
			{
				P1HelperMidiState.Reset("Director.startPlayGame");
			}
		}
	}
	[HarmonyPatch(typeof(Director), "exitPlayGame")]
	internal static class P1HelperResetOnExitPlayGame
	{
		private static void Prefix()
		{
			if (Plugin.HelperMuteEnabled)
			{
				P1HelperMidiState.Reset("Director.exitPlayGame");
			}
		}
	}
	[HarmonyPatch(typeof(Rhythm), "playRhythm")]
	internal static class P2RhythmPlayCountVirtualWrapFix
	{
		private static void Prefix(Rhythm __instance, out PlayCountFixState __state)
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Invalid comparison between Unknown and I4
			__state = new PlayCountFixState();
			if (!Plugin.ThemeLoopFixEnabled)
			{
				return;
			}
			try
			{
				if (__instance != null && (int)__instance.lastMood_ == 2)
				{
					uint playCount_ = __instance.playCount_;
					if (playCount_ > 16)
					{
						uint num = (playCount_ - 1) % 16 + 1;
						__state.Applied = true;
						__state.OriginalPlayCount = playCount_;
						__state.VirtualPlayCount = num;
						__instance.playCount_ = num;
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(24, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P2 FIX] Prefix failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}

		private static void Postfix(Rhythm __instance, bool __result, PlayCountFixState __state)
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			try
			{
				if (__instance != null && __state != null && __state.Applied)
				{
					__instance.playCount_ = (__result ? (__state.OriginalPlayCount + 1) : __state.OriginalPlayCount);
					Plugin.D($"[P2 FIX] Virtualized Mood_High playCount {__state.OriginalPlayCount} -> {__state.VirtualPlayCount}, restored to {__instance.playCount_}");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(25, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P2 FIX] Postfix failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogWarning(val);
			}
		}
	}
	[HarmonyPatch(typeof(BeatCommander), "update")]
	internal static class P2HelperFadeController
	{
		private const float KaesiBeat = 4f;

		private static int WarnCount;

		private static float CurVol = 1f;

		private static bool Applying;

		private static readonly Stopwatch Clock = Stopwatch.StartNew();

		private static long LastMs;

		private static float LastBeat;

		private static bool HasLastBeat;

		private static float MsPerBeat = 500f;

		private static void SetCategoryVolume(float vol)
		{
			CriSoundManager instance = SingletonMonobehaviour<CriSoundManager>.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				instance.setCategorySetVolume((MixingCategory)Plugin.P2HelperCategory, vol, 0f);
			}
		}

		private static void Postfix(BeatCommander __instance)
		{
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Expected O, but got Unknown
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Invalid comparison between Unknown and I4
			if (!Plugin.HelperMuteEnabled || __instance == null)
			{
				return;
			}
			try
			{
				float p2FadeMs = Plugin.P2FadeMs;
				float p2FullVolume = Plugin.P2FullVolume;
				long elapsedMilliseconds = Clock.ElapsedMilliseconds;
				float num = ((LastMs == 0L) ? 16f : Math.Min(p2FadeMs, elapsedMilliseconds - LastMs));
				LastMs = elapsedMilliseconds;
				float commandBeatCount = __instance.getCommandBeatCount();
				if (HasLastBeat)
				{
					float num2 = commandBeatCount - LastBeat;
					if (num2 > 0.0001f && num2 < 2f && num > 0f)
					{
						float num3 = num / num2;
						if (num3 > 50f && num3 < 5000f)
						{
							MsPerBeat = MsPerBeat * 0.9f + num3 * 0.1f;
						}
					}
				}
				LastBeat = commandBeatCount;
				HasLastBeat = true;
				bool num4 = (int)__instance.getState() == 3;
				float num5 = ((MsPerBeat > 0f) ? (Plugin.P2FadeLeadMs / MsPerBeat) : 0f);
				bool flag = !num4 && commandBeatCount < 4f && commandBeatCount >= 4f - num5;
				float num6 = ((num4 || flag) ? 0f : p2FullVolume);
				float num7 = p2FullVolume * (num / p2FadeMs);
				if (CurVol < num6)
				{
					CurVol = Math.Min(num6, CurVol + num7);
				}
				else if (CurVol > num6)
				{
					CurVol = Math.Max(num6, CurVol - num7);
				}
				if (num4 || CurVol < p2FullVolume - 0.001f)
				{
					SetCategoryVolume(CurVol);
					Applying = true;
				}
				else if (Applying)
				{
					SetCategoryVolume(p2FullVolume);
					Applying = false;
				}
			}
			catch (Exception ex)
			{
				if (WarnCount < 3)
				{
					WarnCount++;
					ManualLogSource l = Plugin.L;
					bool flag2 = default(bool);
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(35, 2, ref flag2);
					if (flag2)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[P2 HELPER] controller failed (#");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(WarnCount);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("): ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
					}
					l.LogWarning(val);
				}
			}
		}
	}
	[BepInPlugin("juras.patapon.audiofix", "Music Fixes Extended", "2.0.0")]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource L;

		internal static ConfigEntry<uint> AudioFix_ThemeLoopFix;

		internal static ConfigEntry<uint> AudioFix_HelperMute;

		internal static ConfigEntry<uint> AudioFix_P2HelperCategory;

		internal static ConfigEntry<uint> AudioFix_P2FadeMs;

		internal static ConfigEntry<uint> AudioFix_P2FadeLeadMs;

		internal static ConfigEntry<uint> AudioFix_P2FullVolumePercent;

		internal const bool DebugLog = false;

		internal static bool ThemeLoopFixEnabled => AudioFix_ThemeLoopFix.Value != 0;

		internal static bool HelperMuteEnabled => AudioFix_HelperMute.Value != 0;

		internal static uint P2HelperCategory => AudioFix_P2HelperCategory.Value;

		internal static float P2FadeMs
		{
			get
			{
				if (AudioFix_P2FadeMs.Value >= 1)
				{
					return AudioFix_P2FadeMs.Value;
				}
				return 1f;
			}
		}

		internal static float P2FadeLeadMs => AudioFix_P2FadeLeadMs.Value;

		internal static float P2FullVolume
		{
			get
			{
				uint num = AudioFix_P2FullVolumePercent.Value;
				if (num > 100)
				{
					num = 100u;
				}
				return (float)num / 100f;
			}
		}

		internal static void D(string message)
		{
		}

		public override void Load()
		{
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Expected O, but got Unknown
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Expected O, but got Unknown
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			L = ((BasePlugin)this).Log;
			AudioFix_ThemeLoopFix = ((BasePlugin)this).Config.Bind<uint>("AudioFix", "AudioFix_ThemeLoopFix", 1u, "Set to 1 to enable the rhythm/theme loop playCount fix. Set to 0 to disable it.");
			AudioFix_HelperMute = ((BasePlugin)this).Config.Bind<uint>("AudioFix", "AudioFix_HelperMute", 1u, "Set to 1 to enable helper mute/fade behavior. Set to 0 to disable it.");
			AudioFix_P2HelperCategory = ((BasePlugin)this).Config.Bind<uint>("AudioFix_P2", "AudioFix_P2HelperCategory", 3u, "P2 only: CriSoundManager mixing category that carries the helper (3 = BGM). Only change if a game update moves the helper to a different category.");
			AudioFix_P2FadeMs = ((BasePlugin)this).Config.Bind<uint>("AudioFix_P2", "AudioFix_P2FadeMs", 400u, "P2 only: helper fade duration in milliseconds at the start/end of the chant phase. Higher = more gradual.");
			AudioFix_P2FadeLeadMs = ((BasePlugin)this).Config.Bind<uint>("AudioFix_P2", "AudioFix_P2FadeLeadMs", 250u, "P2 only: how many milliseconds BEFORE the chant the fade-out begins (anticipation). 0 = start exactly at the chant. Tempo-adaptive (stays constant during Fever).");
			AudioFix_P2FullVolumePercent = ((BasePlugin)this).Config.Bind<uint>("AudioFix_P2", "AudioFix_P2FullVolumePercent", 70u, "P2 only: the helper's full (un-ducked) volume as a percent (0-100). Lower it if the helper jumps louder at the chant start; raise it if the helper is too quiet while drumming.");
			L.LogInfo((object)"Music Fixes Extended 2.0.0 loaded");
			ManualLogSource l = L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(23, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("AudioFix_ThemeLoopFix: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ThemeLoopFixEnabled ? "enabled" : "disabled");
			}
			l.LogInfo(val);
			ManualLogSource l2 = L;
			val = new BepInExInfoLogInterpolatedStringHandler(21, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("AudioFix_HelperMute: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(HelperMuteEnabled ? "enabled" : "disabled");
			}
			l2.LogInfo(val);
			new Harmony("juras.patapon.audiofix").PatchAll();
		}
	}
	internal sealed class PlayCountFixState
	{
		public bool Applied;

		public uint OriginalPlayCount;

		public uint VirtualPlayCount;
	}
}