Decompiled source of MarcusBracken v1.0.5

BepInEx/plugins/RytmusBracken.dll

Decompiled 2 weeks ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using RytmusBracken.Patches;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RytmusBracken")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RytmusBracken")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("271eebe8-7c68-4ad9-94f6-c98973d5ebd7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace RytmusBracken
{
	public class Config
	{
		public static ConfigEntry<float> volume;

		public Config(ConfigFile configFile)
		{
			volume = configFile.Bind<float>("General", "volume", 1f, "Volume multiplier of the bracken's new voice.");
		}
	}
	[BepInPlugin("KamenVacuumCleaner.RytmusBracken", "RytmusBracken", "1.2.1")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "KamenVacuumCleaner.RytmusBracken";

		private const string NAME = "RytmusBracken";

		private const string VERSION = "1.2.1";

		private readonly Harmony harmony = new Harmony("RytmusBracken");

		public static Plugin instance;

		private static string location;

		internal ManualLogSource mls;

		public static AudioClip[] angeredClips;

		public static AudioClip[] foundClips;

		public static AudioClip testClip;

		private static int nextFound = -1;

		private static int nextAngered = -1;

		public static Config config { get; internal set; }

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			mls = Logger.CreateLogSource("KamenVacuumCleaner.RytmusBracken");
			config = new Config(((BaseUnityPlugin)this).Config);
			location = ((BaseUnityPlugin)this).Info.Location.TrimEnd("RytmusBracken.dll".ToCharArray());
			Log("Loading RytmusBracken v1.2.1...");
			string path = location + "sounds/Angered/";
			string path2 = location + "sounds/Found/";
			string[] files = Directory.GetFiles(path);
			string[] files2 = Directory.GetFiles(path2);
			List<AudioClip> list = new List<AudioClip>();
			List<AudioClip> list2 = new List<AudioClip>();
			string[] array = files;
			foreach (string path3 in array)
			{
				AudioClip val = LoadAudio(path3, (AudioType)14);
				if ((Object)(object)val != (Object)null)
				{
					list.Add(val);
				}
			}
			angeredClips = list.ToArray();
			nextAngered = Random.Range(0, angeredClips.Length);
			string[] array2 = files2;
			foreach (string path4 in array2)
			{
				AudioClip val2 = LoadAudio(path4, (AudioType)14);
				if ((Object)(object)val2 != (Object)null)
				{
					list2.Add(val2);
				}
			}
			foundClips = list2.ToArray();
			nextFound = Random.Range(0, foundClips.Length);
			harmony.PatchAll(typeof(FlowermanAIPatch));
			Log("RytmusBracken v1.2.1 has been loaded up successfully!");
		}

		private AudioClip LoadAudio(string path, AudioType type)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			AudioClip result = null;
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("File://" + path, type);
			audioClip.SendWebRequest();
			while (!audioClip.isDone)
			{
			}
			if ((int)audioClip.result == 1)
			{
				result = DownloadHandlerAudioClip.GetContent(audioClip);
				Log("Successfully loaded sound: " + path.TrimStart(location.ToCharArray()));
			}
			else
			{
				Log("Could not load audio file");
			}
			return result;
		}

		public static void Log(string message)
		{
			instance.mls.LogInfo((object)message);
		}

		public static void LogError(string message)
		{
			instance.mls.LogError((object)message);
		}

		public static AudioClip GetRandomAngeredClip()
		{
			if (angeredClips.Length == 0 || nextAngered < 0)
			{
				return null;
			}
			int num = nextAngered;
			while (angeredClips.Length > 1 && num == nextAngered)
			{
				nextAngered = Random.Range(0, angeredClips.Length);
			}
			return angeredClips[num];
		}

		public static AudioClip GetRandomFoundClip()
		{
			if (foundClips.Length == 0 || nextFound < 0)
			{
				return null;
			}
			int num = nextFound;
			while (foundClips.Length > 1 && num == nextFound)
			{
				nextFound = Random.Range(0, foundClips.Length);
			}
			return foundClips[num];
		}
	}
}
namespace RytmusBracken.Patches
{
	[HarmonyPatch(typeof(FlowermanAI))]
	internal static class FlowermanAIPatch
	{
		private static bool fixAngeredPitch;

		private static PlayAudioAnimationEvent audioEvent;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		public static void PostStart(FlowermanAI __instance)
		{
			audioEvent = ((Component)__instance).GetComponentInChildren<PlayAudioAnimationEvent>();
			AudioSource creatureSFX = ((EnemyAI)__instance).creatureSFX;
			creatureSFX.volume *= Config.volume.Value;
			AudioSource creatureAngerVoice = __instance.creatureAngerVoice;
			creatureAngerVoice.volume *= Config.volume.Value;
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		public static void PreUpdate(FlowermanAI __instance)
		{
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 1 && ((EnemyAI)__instance).previousBehaviourStateIndex != 1)
			{
				AudioClip randomFoundClip = Plugin.GetRandomFoundClip();
				if ((Object)(object)randomFoundClip != (Object)null)
				{
					audioEvent.audioClip = randomFoundClip;
				}
				else
				{
					Plugin.LogError("Unable to replace found clip");
				}
			}
		}

		[HarmonyPatch("EnterAngerModeClientRpc")]
		[HarmonyPostfix]
		public static void PostEnterAngerModeClientRpc(FlowermanAI __instance)
		{
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 2)
			{
				AudioClip randomAngeredClip = Plugin.GetRandomAngeredClip();
				if ((Object)(object)randomAngeredClip != (Object)null)
				{
					__instance.creatureAngerVoice.clip = randomAngeredClip;
					fixAngeredPitch = true;
				}
				else
				{
					Plugin.LogError("Unable to replace angered clip");
				}
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void PostUpdate(FlowermanAI __instance)
		{
			if (fixAngeredPitch && __instance.creatureAngerVoice.isPlaying)
			{
				__instance.creatureAngerVoice.pitch = 1f;
				fixAngeredPitch = false;
			}
		}
	}
}