Decompiled source of MvmDeathSound v1.0.2

BepInEx/plugins/CustomDeathAudio.dll

Decompiled 3 months 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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LCSoundTool;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[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("CustomDeathAudio")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Customise the death audio.")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyInformationalVersion("2.1.0+f05a1df3c1a0b90e7f72964dcc0de7469e543610")]
[assembly: AssemblyProduct("CustomDeathAudio")]
[assembly: AssemblyTitle("CustomDeathAudio")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.1.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 CustomDeathAudio
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.bean.CustomDeathAudio";

		public const string PLUGIN_NAME = "CustomDeathAudio";

		public const string PLUGIN_VERSION = "2.1.0";
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		public static Dictionary<ulong, GameObject> AudioSources = new Dictionary<ulong, GameObject>();

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void PlayerStartPatch(PlayerControllerB __instance)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			if (!((Object)(object)Plugin.CustomAudioClip == (Object)null) && !AudioSources.TryGetValue(((NetworkBehaviour)__instance).NetworkObjectId, out GameObject _))
			{
				GameObject val = new GameObject();
				AudioSource val2 = val.AddComponent<AudioSource>();
				if (Plugin.CustomPitch != null)
				{
					val2.pitch = Plugin.CustomPitch.Value;
				}
				AudioSources[((NetworkBehaviour)__instance).NetworkObjectId] = val;
				Plugin.AddLog($"Init audio source for player {((NetworkBehaviour)__instance).NetworkObjectId}");
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("KillPlayerClientRpc")]
		private static void KillPlayerPatch(PlayerControllerB __instance)
		{
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			Plugin.AddLog($"Player being killed. {((NetworkBehaviour)__instance).NetworkObjectId}");
			if (!((Object)(object)Plugin.CustomAudioClip != (Object)null) || !AudioSources.TryGetValue(((NetworkBehaviour)__instance).NetworkObjectId, out GameObject value))
			{
				return;
			}
			AudioSource component = value.GetComponent<AudioSource>();
			if ((Object)(object)__instance == (Object)(object)Plugin.Player)
			{
				component.spatialBlend = 0f;
				if (Plugin.Custom2DVolume != null)
				{
					component.PlayOneShot(Plugin.CustomAudioClip, Plugin.Custom2DVolume.Value);
					WalkieTalkie.TransmitOneShotAudio(component, Plugin.CustomAudioClip, Plugin.Custom2DVolume.Value);
				}
				Plugin.AddLog("Playing audio for you.");
				return;
			}
			value.transform.position = ((Component)__instance).transform.position;
			component.spatialBlend = 1f;
			if (Plugin.CustomVolume != null)
			{
				component.PlayOneShot(Plugin.CustomAudioClip, Plugin.CustomVolume.Value);
				WalkieTalkie.TransmitOneShotAudio(component, Plugin.CustomAudioClip, Plugin.CustomVolume.Value);
			}
			Plugin.AddLog($"Playing audio for player {((NetworkBehaviour)__instance).NetworkObjectId}.");
		}
	}
	internal class OtherPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "ShowPlayersFiredScreen")]
		private static void FirePlayerPatch(HUDManager __instance, bool show)
		{
			Plugin.AddLog("Firing player.");
			if (!((Object)(object)Plugin.CustomAudioClip == (Object)null) && show && !((Object)(object)Plugin.Player == (Object)null))
			{
				AudioSource component = PlayerControllerBPatch.AudioSources[((NetworkBehaviour)Plugin.Player).NetworkObjectId].GetComponent<AudioSource>();
				if (Plugin.Custom2DVolume != null)
				{
					component.PlayOneShot(Plugin.CustomAudioClip, Plugin.Custom2DVolume.Value);
				}
			}
		}
	}
	[BepInPlugin("com.bean.CustomDeathAudio", "CustomDeathAudio", "2.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin? Instance;

		internal static ManualLogSource? LogSource;

		public static AudioClip? CustomAudioClip;

		private const string PluginPath = "BeanCan-CustomDeathAudio";

		private const string OriginFileName = "DeathAudio";

		internal static ConfigEntry<string>? CustomRelativePath;

		internal static ConfigEntry<float>? CustomVolume;

		internal static ConfigEntry<float>? Custom2DVolume;

		internal static ConfigEntry<float>? CustomPitch;

		public static PlayerControllerB? Player => GameNetworkManager.Instance?.localPlayerController;

		public static void AddLog(string str)
		{
			ManualLogSource? logSource = LogSource;
			if (logSource != null)
			{
				logSource.LogInfo((object)str);
			}
		}

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				throw new Exception("More than 1 plugin instance.");
			}
			Instance = this;
			LogSource = ((BaseUnityPlugin)this).Logger;
			CustomRelativePath = ((BaseUnityPlugin)this).Config.Bind<string>("General", "CustomRelatedPath", "./DeathAudio.wav", "Customize the path of your audio relative to the plugin's folder. \ne.g. \"../another-path/MyAudio.ogg\" \nIf no suffix is provided, \".wav\" will be considered as the default.");
			CustomVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CustomVolume", 1f, "Customize the Volume of the 3D audio. \nConfig this for other players' audio. \n3D means the audio plays from the body's location. \nShould be a float number. (Default: 100%)");
			Custom2DVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Custom2DVolume", 0.5f, "Customize the Volume of the 2D audio. \nConfig this for your audio. \n2D audio only plays for the killed player. \nShould be a float number. (Default: 50%)");
			CustomPitch = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CustomPitch", 1f, "Customize the pitch(playback speed) of your audio. \nShould be a float number. (Default: 100%)");
			AddLog("Plugin CustomDeathAudio is loaded!");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.bean.CustomDeathAudio");
		}

		internal void Start()
		{
			AddLog("Start loading custom audio.");
			ProcessAudioFile();
		}

		internal void OnDestroy()
		{
			AddLog("Start loading custom audio.");
			ProcessAudioFile();
		}

		private static void ProcessAudioFile()
		{
			string text = CustomRelativePath?.Value ?? "DeathAudio.wav";
			string directoryName = Path.GetDirectoryName(text);
			string fileName = Path.GetFileName(text);
			try
			{
				AddLog("Loading BeanCan-CustomDeathAudio/" + text + ".");
				CustomAudioClip = SoundTool.GetAudioClip("BeanCan-CustomDeathAudio", directoryName, fileName);
			}
			catch (Exception ex)
			{
				AddLog(ex.Message);
			}
			if ((Object)(object)CustomAudioClip != (Object)null)
			{
				AddLog("Audio clip loaded.");
				return;
			}
			try
			{
				AddLog("Loading BeanCan-CustomDeathAudio/DeathAudio.wav.");
				CustomAudioClip = SoundTool.GetAudioClip("BeanCan-CustomDeathAudio", "DeathAudio.wav");
			}
			catch (Exception ex2)
			{
				AddLog(ex2.Message);
			}
			if ((Object)(object)CustomAudioClip != (Object)null)
			{
				AddLog("Audio clip loaded.");
			}
			else
			{
				AddLog("Failed to load the audio.");
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "CustomDeathAudio";

		public const string PLUGIN_NAME = "CustomDeathAudio";

		public const string PLUGIN_VERSION = "2.1.0";
	}
}