Decompiled source of Loud Horn Customizer v1.0.1

BepInEx/plugins/LoudHornCustomizer.dll

Decompiled a year ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LoudHornCustomizer.Coroutines;
using LoudHornCustomizer.Manager;
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("LoudHornCustomizer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LoudHornCustomizer")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("647e1dbe-eb5c-4b54-bc65-ef4df7c591ea")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LoudHornCustomizer
{
	[BepInPlugin("com.malco.lethalcompany.customhorn", "Custom LoudHorn SFX", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "com.malco.lethalcompany.customhorn";

		private const string NAME = "Custom LoudHorn SFX";

		private const string VERSION = "1.0.0";

		private readonly Harmony harmony = new Harmony("com.malco.lethalcompany.customhorn");

		public static Plugin Instance;

		public static ManualLogSource mls;

		private void Awake()
		{
			mls = Logger.CreateLogSource("com.malco.lethalcompany.customhorn");
			Instance = this;
			harmony.PatchAll();
			mls.LogInfo((object)"CustomHorn has been Patched");
		}
	}
}
namespace LoudHornCustomizer.Patches
{
	[HarmonyPatch]
	internal class ShipAlarmCordPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "Awake")]
		private static void LoadAudio()
		{
			HornManager.Load();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ShipAlarmCord), "Start")]
		private static void ChangeShipAlarmCordSFX(ShipAlarmCord __instance)
		{
			if (HornManager.Loaded)
			{
				HornManager.ChangeClip(__instance);
				return;
			}
			HornManager.OnLoaded += delegate
			{
				HornManager.ChangeClip(__instance);
			};
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(ShipAlarmCord), "StopHorn")]
		private static void ActuallyStopHorn(ShipAlarmCord __instance)
		{
			__instance.cordAudio.Stop();
			HornManager.NextClip(__instance);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ShipAlarmCord), "HoldCordDown")]
		private static void LoopNewHornSFX(ShipAlarmCord __instance)
		{
			if (!__instance.cordAudio.isPlaying)
			{
				__instance.cordAudio.PlayOneShot(__instance.cordPullSFX);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ShipAlarmCord), "PullCordClientRpc")]
		private static void LoopNewHornSFXClient(ShipAlarmCord __instance)
		{
			if (!__instance.cordAudio.isPlaying)
			{
				__instance.cordAudio.PlayOneShot(__instance.cordPullSFX);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(ShipAlarmCord), "StopPullingCordClientRpc")]
		private static void ActuallyStopHornClient(ShipAlarmCord __instance)
		{
			__instance.cordAudio.Stop();
			HornManager.NextClip(__instance);
		}
	}
}
namespace LoudHornCustomizer.Manager
{
	internal static class HornManager
	{
		public static List<AudioClip> clips = new List<AudioClip>();

		public static bool Loaded;

		public static int clipIndex = 0;

		private static bool canSwitch = true;

		public static event Action OnLoaded;

		public static void Load()
		{
			string path = Path.Combine(Paths.BepInExRootPath, "CustomHorn");
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			string[] files = Directory.GetFiles(path);
			if (files.Length < 1)
			{
				Plugin.mls.LogInfo((object)"No Horn Audio Found!");
				return;
			}
			List<Coroutine> list = new List<Coroutine>();
			string[] array = files;
			foreach (string filePath in array)
			{
				Coroutine item = CorStarter.StartCoroutine(LoadAudioClip(filePath));
				list.Add(item);
			}
			CorStarter.StartCoroutine(WaitForAllClips(list));
		}

		private static IEnumerator WaitForAllClips(List<Coroutine> coroutines)
		{
			foreach (Coroutine coroutine in coroutines)
			{
				yield return coroutine;
			}
			Loaded = true;
			HornManager.OnLoaded?.Invoke();
		}

		private static IEnumerator LoadAudioClip(string filePath)
		{
			Plugin.mls.LogInfo((object)("Loading " + filePath + "!"));
			AudioType audioType = GetAudioType(filePath);
			if ((int)audioType == 0)
			{
				Plugin.mls.LogError((object)("Failed to load AudioClip from " + filePath + "\nUnsupported file extension!"));
				yield break;
			}
			UnityWebRequest loader = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioType(filePath));
			loader.SendWebRequest();
			while (!loader.isDone)
			{
				yield return null;
			}
			if (loader.error != null)
			{
				Plugin.mls.LogError((object)("Error loading clip from path: " + filePath + "\n" + loader.error));
				Plugin.mls.LogError((object)loader.error);
				yield break;
			}
			AudioClip dlClip = DownloadHandlerAudioClip.GetContent(loader);
			if (Object.op_Implicit((Object)(object)dlClip) && (int)dlClip.loadState == 2)
			{
				Plugin.mls.LogInfo((object)("Loaded " + filePath));
				((Object)dlClip).name = Path.GetFileName(filePath);
				clips.Add(dlClip);
				Plugin.mls.LogInfo((object)$"Clips loaded: {clips.Count()}");
			}
			else
			{
				Plugin.mls.LogError((object)("Failed to load clip at: " + filePath + "\nThis might be due to an mismatch between the audio codec and the file extension!"));
			}
		}

		public static void ChangeClip(ShipAlarmCord __instance)
		{
			__instance.cordPullSFX = clips[0];
			__instance.hornClose.clip = null;
			__instance.hornFar.clip = null;
			__instance.cordAudio.maxDistance = 155f;
		}

		public static void NextClip(ShipAlarmCord __instance)
		{
			if (canSwitch)
			{
				clipIndex++;
				if (clipIndex >= clips.Count)
				{
					clipIndex = 0;
				}
				__instance.cordPullSFX = clips[clipIndex];
				Plugin.mls.LogInfo((object)$"Switched Clip to index {clipIndex}");
				canSwitch = false;
				CorStarter.StartCoroutine(WaitToSwitch());
			}
		}

		private static IEnumerator WaitToSwitch()
		{
			yield return (object)new WaitForSeconds(0.25f);
			canSwitch = true;
		}

		private static AudioType GetAudioType(string path)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.GetExtension(path).ToLower();
			switch (text)
			{
			case ".wav":
				return (AudioType)20;
			case ".ogg":
				return (AudioType)14;
			case ".mp3":
				return (AudioType)13;
			default:
				Plugin.mls.LogError((object)("Unsupported extension type: " + text));
				return (AudioType)0;
			}
		}
	}
}
namespace LoudHornCustomizer.Coroutines
{
	public class CorStarter : MonoBehaviour
	{
		private static CorStarter Instance;

		public static Coroutine StartCoroutine(IEnumerator routine)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = new GameObject("Shared Coroutine Starter").AddComponent<CorStarter>();
				Object.DontDestroyOnLoad((Object)(object)Instance);
			}
			return ((MonoBehaviour)Instance).StartCoroutine(routine);
		}
	}
}