Decompiled source of YajuDeathAudio v1.0.4

plugins/INMDeathAudio.dll

Decompiled 19 hours 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 GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("INMDeathAudio")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("INMDeathAudio")]
[assembly: AssemblyTitle("INMDeathAudio")]
[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 DeathSoundPack
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerPatches
	{
		private static Dictionary<ulong, float> lastDeathTime = new Dictionary<ulong, float>();

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void OnPlayerStart(PlayerControllerB __instance)
		{
			Plugin.GetOrCreateAudioSource(((NetworkBehaviour)__instance).NetworkObjectId);
		}

		[HarmonyPrefix]
		[HarmonyPatch("KillPlayerClientRpc")]
		private static void OnPlayerKilled(PlayerControllerB __instance)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			ulong networkObjectId = ((NetworkBehaviour)__instance).NetworkObjectId;
			float unscaledTime = Time.unscaledTime;
			if (lastDeathTime.TryGetValue(networkObjectId, out var value) && unscaledTime - value < 0.5f)
			{
				return;
			}
			lastDeathTime[networkObjectId] = unscaledTime;
			AudioClip randomClip = Plugin.GetRandomClip();
			if (!((Object)(object)randomClip == (Object)null))
			{
				AudioSource orCreateAudioSource = Plugin.GetOrCreateAudioSource(networkObjectId);
				if (!((Object)(object)orCreateAudioSource == (Object)null))
				{
					((Component)orCreateAudioSource).transform.position = ((Component)__instance).transform.position;
					orCreateAudioSource.spatialBlend = 1f;
					orCreateAudioSource.PlayOneShot(randomClip, Plugin.Volume?.Value ?? 1f);
				}
			}
		}
	}
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("ShowPlayersFiredScreen")]
		private static void OnPlayerFired(HUDManager __instance, bool show)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			if (!show)
			{
				return;
			}
			PlayerControllerB val = GameNetworkManager.Instance?.localPlayerController;
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			AudioClip randomClip = Plugin.GetRandomClip();
			if (!((Object)(object)randomClip == (Object)null))
			{
				AudioSource orCreateAudioSource = Plugin.GetOrCreateAudioSource(((NetworkBehaviour)val).NetworkObjectId);
				if (!((Object)(object)orCreateAudioSource == (Object)null))
				{
					((Component)orCreateAudioSource).transform.position = ((Component)val).transform.position;
					orCreateAudioSource.spatialBlend = 1f;
					orCreateAudioSource.PlayOneShot(randomClip, Plugin.Volume?.Value ?? 1f);
				}
			}
		}
	}
	[BepInPlugin("com.suwako.INMDeathAudio", "INM Death Audio", "1.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		internal static List<AudioClip> DeathClips = new List<AudioClip>();

		internal static Dictionary<ulong, AudioSource> PlayerAudioSources = new Dictionary<ulong, AudioSource>();

		internal static ConfigEntry<float> Volume;

		private static Random _rng = new Random();

		public static Plugin Instance { get; private set; }

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				throw new Exception("Duplicate plugin instance!");
			}
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Volume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 1f, "Volume (0.0 - 1.0).");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.suwako.INMDeathAudio");
			Log.LogInfo((object)"INM Death Audio v1.0.3 loaded!");
		}

		private void Start()
		{
			((MonoBehaviour)this).StartCoroutine(LoadAudioFiles());
		}

		private IEnumerator LoadAudioFiles()
		{
			DeathClips.Clear();
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			string audioDir = Path.Combine(directoryName, "Audio");
			if (!Directory.Exists(audioDir))
			{
				Log.LogWarning((object)("Audio folder not found: " + audioDir));
				yield break;
			}
			string[] array = new string[3] { "*.ogg", "*.wav", "*.mp3" };
			string[] array2 = array;
			foreach (string searchPattern in array2)
			{
				string[] files = Directory.GetFiles(audioDir, searchPattern);
				foreach (string text in files)
				{
					string fileName = Path.GetFileName(text);
					string text2 = "file:///" + text.Replace("\\", "/");
					AudioType val = (AudioType)14;
					if (fileName.EndsWith(".wav"))
					{
						val = (AudioType)20;
					}
					else if (fileName.EndsWith(".mp3"))
					{
						val = (AudioType)13;
					}
					UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(text2, val);
					try
					{
						yield return request.SendWebRequest();
						if ((int)request.result == 1)
						{
							AudioClip content = DownloadHandlerAudioClip.GetContent(request);
							if ((Object)(object)content != (Object)null)
							{
								((Object)content).name = Path.GetFileNameWithoutExtension(fileName);
								DeathClips.Add(content);
								Log.LogInfo((object)$"Loaded: {fileName} ({content.length:F1}s)");
							}
						}
						else
						{
							Log.LogError((object)("Failed to load " + fileName + ": " + request.error));
						}
					}
					finally
					{
						((IDisposable)request)?.Dispose();
					}
				}
			}
			Log.LogInfo((object)$"Total death sounds loaded: {DeathClips.Count}");
		}

		internal static AudioClip GetRandomClip()
		{
			if (DeathClips.Count == 0)
			{
				return null;
			}
			return DeathClips[_rng.Next(DeathClips.Count)];
		}

		internal static AudioSource GetOrCreateAudioSource(ulong networkObjectId)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if (PlayerAudioSources.TryGetValue(networkObjectId, out var value) && (Object)(object)value != (Object)null)
			{
				return value;
			}
			AudioSource val = new GameObject($"DeathSoundPack_Audio_{networkObjectId}").AddComponent<AudioSource>();
			val.playOnAwake = false;
			PlayerAudioSources[networkObjectId] = val;
			return val;
		}
	}
	internal static class PluginInfo
	{
		public const string GUID = "com.suwako.INMDeathAudio";

		public const string NAME = "INM Death Audio";

		public const string VERSION = "1.0.3";
	}
}