Decompiled source of Cogwork Waltz v1.0.0

Cogwork Waltz.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using Cogwork_Waltz.Components;
using HutongGames.PlayMaker;
using HutongGames.PlayMaker.Actions;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;
using WavLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Cogwork Waltz")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f39ca46b2c8d488a4d777bbbe365c692e4300ac4")]
[assembly: AssemblyProduct("Cogwork Waltz")]
[assembly: AssemblyTitle("Cogwork_Waltz")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/kaycodes13/Cogwork Waltz")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace Cogwork_Waltz.Components
{
	[RequireComponent(typeof(AudioSource))]
	[RequireComponent(typeof(PlayMakerFSM))]
	internal class ResyncCogworkDancers : MonoBehaviour
	{
		private AudioSource music;

		private PlayMakerFSM dancerControl;

		private PlayMakerFSM beatControl;

		private int mult;

		private float prevMusicTime;

		private bool justLooped;

		private float CurrentBeatTiming => beatControl.FsmVariables.FloatVariables[0].Value;

		private void Awake()
		{
			music = ((Component)this).GetComponent<AudioSource>();
			dancerControl = ((Component)this).GetComponents<PlayMakerFSM>().First((PlayMakerFSM x) => x.FsmName == "Control");
			beatControl = ((Component)this).GetComponents<PlayMakerFSM>().First((PlayMakerFSM x) => x.FsmName == "Beat Control");
			mult = 1;
			prevMusicTime = -1f;
			justLooped = false;
		}

		private void OnDestroy()
		{
			RestartBeatFSM();
		}

		private void Update()
		{
			if (!Object.op_Implicit((Object)(object)music.clip) || !music.loop || (!music.isPlaying && !HeroController.instance.IsPaused()))
			{
				if (!((Behaviour)beatControl).enabled)
				{
					mult = 1;
					prevMusicTime = -1f;
					justLooped = false;
					RestartBeatFSM();
				}
				return;
			}
			((Behaviour)beatControl).enabled = false;
			float currentBeatTiming = CurrentBeatTiming;
			if (prevMusicTime > music.time)
			{
				justLooped = true;
			}
			prevMusicTime = music.time;
			if ((float)mult * currentBeatTiming <= music.time)
			{
				mult++;
				SendBeatEvent();
			}
			else if (justLooped && (float)mult * currentBeatTiming - music.clip.length <= music.time)
			{
				justLooped = false;
				mult = 1;
				SendBeatEvent();
			}
		}

		private void SendBeatEvent()
		{
			dancerControl.SendEvent("BEAT");
			EventRegister.SendEvent("BEAT", ((Component)this).gameObject);
		}

		private void RestartBeatFSM()
		{
			((Behaviour)beatControl).enabled = true;
			beatControl.SendEvent("STOP");
			beatControl.SendEvent("BEGIN");
		}
	}
}
namespace CogworkWaltz
{
	[BepInPlugin("io.github.kaycodes13.cogworkwaltz", "Cogwork_Waltz", "1.0.0")]
	public class CogworkWaltzPlugin : BaseUnityPlugin
	{
		private static List<AudioClip> Phases = new List<AudioClip>();

		public const string Id = "io.github.kaycodes13.cogworkwaltz";

		public static string Name => "Cogwork_Waltz";

		public static string Version => "1.0.0";

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin " + Name + " (io.github.kaycodes13.cogworkwaltz) has loaded!"));
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
			Phases.Add(LoadEmbeddedWav(manifestResourceNames.First((string n) => n.EndsWith("sting.wav")), executingAssembly, "sting"));
			int i;
			for (i = 1; i <= 4; i++)
			{
				Phases.Add(LoadEmbeddedWav(manifestResourceNames.First((string n) => n.EndsWith($"phase_{i}.wav")), executingAssembly, $"phase_{i}"));
			}
			SceneManager.sceneLoaded += ReplaceMusic;
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= ReplaceMusic;
			Phases.Clear();
		}

		private void ReplaceMusic(Scene scene, LoadSceneMode _)
		{
			if (((Scene)(ref scene)).name != "Cog_Dancers_boss")
			{
				return;
			}
			GameObject obj = GameObject.Find("Dancer Control");
			obj.AddComponent<ResyncCogworkDancers>();
			FsmState[] fsmStates = obj.GetComponents<PlayMakerFSM>().First((PlayMakerFSM fsm) => fsm.FsmName == "Music Control").FsmStates;
			foreach (FsmState val in fsmStates)
			{
				if (val.Name == "Stop")
				{
					val.Actions.OfType<AudioPlaySimple>().First().oneShotClip = FsmObject.op_Implicit((Object)(object)Phases[0]);
					continue;
				}
				for (int j = 1; j <= 4; j++)
				{
					if (val.Name == $"P{j} Music")
					{
						val.Actions.OfType<SetAudioClip>().First().audioClip = FsmObject.op_Implicit((Object)(object)Phases[j]);
						break;
					}
				}
			}
		}

		private static AudioClip LoadEmbeddedWav(string path, Assembly asm, string name)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			WavData val = new WavData();
			using (Stream stream = asm.GetManifestResourceStream(path))
			{
				val.Parse(stream, (Action<string>)null);
			}
			float[] samples = val.GetSamples();
			AudioClip obj = AudioClip.Create(name, samples.Length / val.FormatChunk.NumChannels, (int)val.FormatChunk.NumChannels, (int)val.FormatChunk.SampleRate, false);
			obj.SetData(samples, 0);
			return obj;
		}
	}
}