Decompiled source of MoreRandomHorn v1.0.0

BepInEx/plugins/MoreRandomHorn/RandomHornMod.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RandomHornMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Random Horn Mod")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RandomHornMod")]
[assembly: AssemblyTitle("RandomHornMod")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace RandomHornMod
{
	[BepInPlugin("HenDGS.RandomHorn", "Lethal Company Random Horn", "1.0.0")]
	public class RandomHornMod : BaseUnityPlugin
	{
		private const string ModGUID = "HenDGS.RandomHorn";

		private const string ModName = "Lethal Company Random Horn";

		private const string ModVersion = "1.0.0";

		private readonly Harmony _harmony = new Harmony("HenDGS.RandomHorn");

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"HenDGS.RandomHorn has loaded successfully.");
			_harmony.PatchAll();
		}
	}
	[HarmonyPatch(typeof(NoisemakerProp), "ItemActivate")]
	internal class AudioPatch
	{
		[HarmonyPrefix]
		private static void Prefix(NoisemakerProp __instance)
		{
			if ((Object)(object)__instance == (Object)null || __instance.noiseSFX == null || __instance.noiseSFX.Length == 0)
			{
				return;
			}
			string randomMp3FilePath = ReadMp3Files.GetRandomMp3FilePath("Music");
			if (string.IsNullOrEmpty(randomMp3FilePath))
			{
				return;
			}
			Patches.LoadAudioClipAsync(randomMp3FilePath, delegate(AudioClip clip)
			{
				if ((Object)(object)clip != (Object)null)
				{
					__instance.noiseSFX[0] = clip;
					__instance.noiseSFXFar[0] = clip;
				}
			});
		}
	}
	internal static class ReadMp3Files
	{
		private static readonly List<string> CachedMp3Files = new List<string>();

		private static bool _isLoaded = false;

		private static readonly ManualLogSource Log = Logger.CreateLogSource("RandomHorn");

		public static List<string> ReadMp3FilesInDirectory(string path)
		{
			if (_isLoaded)
			{
				return CachedMp3Files;
			}
			string text = Path.Combine(Directory.GetCurrentDirectory(), path);
			CachedMp3Files.Clear();
			if (Directory.Exists(text))
			{
				CachedMp3Files.AddRange(Directory.GetFiles(text, "*.mp3"));
				_isLoaded = true;
			}
			else
			{
				Log.LogWarning((object)("Music folder not found: " + text));
			}
			return CachedMp3Files;
		}

		public static string GetRandomMp3FilePath(string path)
		{
			List<string> list = ReadMp3FilesInDirectory(path);
			if (list.Count == 0)
			{
				return null;
			}
			Random random = new Random();
			return list[random.Next(list.Count)];
		}
	}
	internal static class Patches
	{
		private const float MaxAudioLength = 5f;

		private static readonly Dictionary<string, AudioClip> AudioClipCache = new Dictionary<string, AudioClip>();

		private static readonly ManualLogSource Log = Logger.CreateLogSource("RandomHorn");

		public static async void LoadAudioClipAsync(string filepath, Action<AudioClip> callback)
		{
			if (AudioClipCache.TryGetValue(filepath, out var cachedClip))
			{
				callback?.Invoke(cachedClip);
				return;
			}
			UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(filepath, (AudioType)13);
			try
			{
				request.SendWebRequest();
				while (!request.isDone)
				{
					await Task.Yield();
				}
				if ((int)request.result != 1)
				{
					Log.LogError((object)("Error loading sound: " + filepath + " - " + request.error));
					callback?.Invoke(null);
					return;
				}
				AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
				if ((Object)(object)clip == (Object)null || (int)clip.loadState != 2)
				{
					Log.LogError((object)("Failed to load audio clip: " + filepath));
					callback?.Invoke(null);
					return;
				}
				((Object)clip).name = Path.GetFileName(filepath);
				if (clip.length > 5f)
				{
					Log.LogWarning((object)$"Audio clip {((Object)clip).name} exceeds max length ({clip.length}s). Trimming to {5f}s.");
					clip = TrimAudioClip(clip, 5f);
				}
				AudioClipCache[filepath] = clip;
				callback?.Invoke(clip);
			}
			finally
			{
				((IDisposable)request)?.Dispose();
			}
		}

		private static AudioClip TrimAudioClip(AudioClip clip, float maxLength)
		{
			int num = Mathf.FloorToInt((float)clip.frequency * maxLength);
			float[] array = new float[num * clip.channels];
			clip.GetData(array, 0);
			AudioClip val = AudioClip.Create(((Object)clip).name + "_trimmed", num, clip.channels, clip.frequency, false);
			val.SetData(array, 0);
			return val;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "RandomHornMod";

		public const string PLUGIN_NAME = "RandomHornMod";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}