Decompiled source of SGVigilante v1.0.1

LCKartHornMod.dll

Decompiled a year ago
using System;
using System.Collections;
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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LCKartHornMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Horn mod for Lethal Company, based on SRB2K's horn mods.")]
[assembly: AssemblyFileVersion("0.6.1.0")]
[assembly: AssemblyInformationalVersion("0.6.1+61696b3a5765dd5aa53cc112e668ed7a192b48dc")]
[assembly: AssemblyProduct("LCKartHornMod")]
[assembly: AssemblyTitle("LCKartHornMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.6.1.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 LCKartHornMod
{
	public class HornMaster
	{
		public static List<AudioClip> Horns = new List<AudioClip>();

		public static List<AudioClip> HornsFar = new List<AudioClip>();

		public static void LoadHorns()
		{
			string path = Path.Combine(Paths.BepInExRootPath, "plugins", "Horns");
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			string[] files = Directory.GetFiles(path, "*.ogg");
			if (files.Length == 0)
			{
				Plugin.HornLog.LogInfo((object)"No horns found!");
				return;
			}
			Plugin.HornLog.LogInfo((object)$"Attempting to load {files.Length} horns...");
			string[] array = files;
			foreach (string path2 in array)
			{
				AudioClip val = LoadAudioFile(path2);
				if ((Object)(object)val != (Object)null)
				{
					Horns.Add(val);
					HornsFar.Add(LowerVolumeByFactor(val, Plugin.Instance.FarVolumeReductionFactor.Value));
				}
			}
			Horns.Sort((AudioClip a, AudioClip b) => string.Compare(((Object)a).name, ((Object)b).name, StringComparison.Ordinal));
			HornsFar.Sort((AudioClip a, AudioClip b) => string.Compare(((Object)a).name, ((Object)b).name, StringComparison.Ordinal));
		}

		private static AudioClip LoadAudioFile(string path)
		{
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(path, (AudioType)14);
			audioClip.SendWebRequest();
			while (!audioClip.isDone)
			{
			}
			if (audioClip.error != null)
			{
				Plugin.HornLog.LogError((object)("Failed to load audio file " + path + ": " + audioClip.error));
				return null;
			}
			AudioClip content = DownloadHandlerAudioClip.GetContent(audioClip);
			((Object)content).name = Path.GetFileNameWithoutExtension(path);
			return content;
		}

		private static AudioClip LowerVolumeByFactor(AudioClip originalClip, float factor)
		{
			AudioClip val = AudioClip.Create(((Object)originalClip).name, originalClip.samples, originalClip.channels, originalClip.frequency, false);
			float[] array = new float[originalClip.samples * originalClip.channels];
			originalClip.GetData(array, 0);
			for (int i = 0; i < array.Length; i++)
			{
				array[i] *= factor;
			}
			val.SetData(array, 0);
			return val;
		}
	}
	[BepInPlugin("LCKartHornMod", "LCKartHornMod", "0.6.1")]
	[BepInProcess("Lethal Company.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance;

		public ConfigEntry<float> FarVolumeReductionFactor;

		public static ManualLogSource HornLog => ((BaseUnityPlugin)Instance).Logger;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			FarVolumeReductionFactor = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FarVolumeReductionFactor", 0.05f, "The factor by which the volume of the far audio is reduced. 0.05 means 5% of the original volume. Use 0 to disable far audio.");
			((Object)((Component)Instance).gameObject).hideFlags = (HideFlags)61;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LCKartHornMod is loading!");
			HornMaster.LoadHorns();
			if (HornMaster.Horns.Count > 0)
			{
				Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LCKartHornMod loaded succesfully!");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Disabling plugin since no horns were found...");
				Object.Destroy((Object)(object)this);
			}
		}

		[HarmonyPatch(typeof(NoisemakerProp), "Start")]
		[HarmonyPostfix]
		private static void ReplaceNoisemakerAudio(ref NoisemakerProp __instance)
		{
			if ((Object)(object)__instance != (Object)null && (Object)(object)((GrabbableObject)__instance).itemProperties != (Object)null && ((Object)((GrabbableObject)__instance).itemProperties).name == "ClownHorn")
			{
				HornLog.LogDebug((object)"Clown horn spawned! Waiting for NetworkObject ID...");
				((MonoBehaviour)Instance).StartCoroutine(Instance.WaitUntilNetworkObjectIDAvailable(__instance));
				HornLog.LogDebug((object)"Coroutine started!");
			}
		}

		public IEnumerator WaitUntilNetworkObjectIDAvailable(NoisemakerProp noisemakerProp)
		{
			HornLog.LogDebug((object)"Waiting for NetworkObject ID...");
			while ((Object)(object)((NetworkBehaviour)noisemakerProp).NetworkObject == (Object)null)
			{
				yield return (object)new WaitForSeconds(0.05f);
			}
			HornLog.LogDebug((object)"NetworkObject available!");
			ulong id;
			for (id = ((NetworkBehaviour)noisemakerProp).NetworkObject.NetworkObjectId; id == 0; id = ((NetworkBehaviour)noisemakerProp).NetworkObject.NetworkObjectId)
			{
				yield return (object)new WaitForSeconds(0.05f);
			}
			HornLog.LogDebug((object)("noisemakerProp NetworkObjectId: " + id));
			ReplaceClownhornAudio(noisemakerProp, id);
		}

		private void ReplaceClownhornAudio(NoisemakerProp noiseProp, ulong id)
		{
			HornLog.LogDebug((object)"Replacing clownhorn audio...");
			HornLog.LogDebug((object)("noiseProp: " + (object)noiseProp));
			HornLog.LogDebug((object)("id: " + id));
			int index = new Random(Convert.ToInt32(id)).Next(0, HornMaster.Horns.Count);
			HornLog.LogInfo((object)("Replaced clownhorn audio with " + ((Object)HornMaster.Horns[index]).name + "!"));
			noiseProp.noiseSFX[0] = HornMaster.Horns[index];
			noiseProp.noiseSFXFar[0] = HornMaster.HornsFar[index];
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LCKartHornMod";

		public const string PLUGIN_NAME = "LCKartHornMod";

		public const string PLUGIN_VERSION = "0.6.1";
	}
}