Decompiled source of KillSoundPlugin v14.15.0

plugins/KillSoundPlugin.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading;
using BepInEx;
using ComputerysModdingUtilities;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: StraftatMod(true)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("KillSoundPlugin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("KillSoundPlugin")]
[assembly: AssemblyTitle("KillSoundPlugin")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 ValorantKillSoundMod
{
	[BepInPlugin("Killsound", "Kill Sound Plugin", "14.15.0")]
	public class KillSoundPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static LogCallback <0>__OnLogMessageReceived;

			public static Func<char, bool> <1>__IsDigit;
		}

		public static KillSoundPlugin Instance;

		private static string soundsFolder;

		private static List<string> soundFilePaths = new List<string>();

		private static int globalKillCount = 0;

		private static int matchSeed = 0;

		private static string currentHostSteamId = string.Empty;

		private static SoundPlayer activeSoundPlayer = null;

		private static readonly object audioLock = new object();

		private void Awake()
		{
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			Instance = this;
			soundsFolder = Path.Combine(Paths.PluginPath, "Straftatjoguina-KillSoundPlugin", "audio");
			if (!Directory.Exists(soundsFolder))
			{
				Directory.CreateDirectory(soundsFolder);
			}
			soundFilePaths = (from f in Directory.GetFiles(soundsFolder)
				where f.EndsWith(".wav", StringComparison.OrdinalIgnoreCase)
				orderby Path.GetFileName(f).ToLowerInvariant()
				select f).ToList();
			object obj = <>O.<0>__OnLogMessageReceived;
			if (obj == null)
			{
				LogCallback val = OnLogMessageReceived;
				<>O.<0>__OnLogMessageReceived = val;
				obj = (object)val;
			}
			Application.logMessageReceived += (LogCallback)obj;
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"[KillSounds] Version 14.14.0 loaded. {soundFilePaths.Count} .WAV files ready in {soundsFolder}.");
		}

		private void OnDestroy()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			object obj = <>O.<0>__OnLogMessageReceived;
			if (obj == null)
			{
				LogCallback val = OnLogMessageReceived;
				<>O.<0>__OnLogMessageReceived = val;
				obj = (object)val;
			}
			Application.logMessageReceived -= (LogCallback)obj;
			StopActiveSound();
		}

		private static void StopActiveSound()
		{
			lock (audioLock)
			{
				try
				{
					if (activeSoundPlayer != null)
					{
						activeSoundPlayer.Stop();
						((Component)(object)activeSoundPlayer).Dispose();
						activeSoundPlayer = null;
					}
				}
				catch
				{
				}
			}
		}

		private static void OnLogMessageReceived(string condition, string stackTrace, LogType type)
		{
			try
			{
				if (string.IsNullOrEmpty(condition))
				{
					return;
				}
				string text = condition.ToLowerInvariant();
				if (text.Contains("hostaddress"))
				{
					string[] array = condition.Split(new char[1] { '=' });
					if (array.Length > 1)
					{
						string text2 = array[1].Trim();
						if (!string.IsNullOrEmpty(text2) && text2 != currentHostSteamId)
						{
							currentHostSteamId = text2;
							matchSeed = currentHostSteamId.GetHashCode();
							globalKillCount = 0;
							Debug.Log((object)$"[KillSounds] [CHIVATO] New lobby detected! Host SteamID Locked via HostAddress -> Host: {currentHostSteamId} | Seed reset to: {matchSeed} | Kill count reset to 0");
						}
					}
				}
				else if (text.Contains("added new player | steamid:") && text.Contains("playerid: 0"))
				{
					int num = condition.IndexOf("steamid:", StringComparison.OrdinalIgnoreCase);
					if (num != -1)
					{
						string text3 = new string(condition.Substring(num + 8).Trim().TakeWhile(char.IsDigit)
							.ToArray());
						if (!string.IsNullOrEmpty(text3) && text3 != currentHostSteamId)
						{
							currentHostSteamId = text3;
							matchSeed = currentHostSteamId.GetHashCode();
							globalKillCount = 0;
							Debug.Log((object)$"[KillSounds] [CHIVATO] New lobby detected! Host SteamID Locked via PlayerId: 0 -> Host: {currentHostSteamId} | Seed reset to: {matchSeed} | Kill count reset to 0");
						}
					}
				}
				if (text.Contains("match log:") && (text.Contains("was killed with") || text.Contains("was headshot with") || text.Contains("was slain") || text.Contains("was slain by") || text.Contains("was noscoped by") || text.Contains("fell into the void") || text.Contains("commited suicide")) && soundFilePaths.Count > 0)
				{
					TriggerKillSound();
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[KillSounds] Error reading console: " + ex.Message));
			}
		}

		private static void TriggerKillSound()
		{
			if (soundFilePaths.Count == 0)
			{
				return;
			}
			if (matchSeed == 0)
			{
				matchSeed = 1337;
			}
			int num = globalKillCount / soundFilePaths.Count;
			int num2 = globalKillCount % soundFilePaths.Count;
			globalKillCount++;
			Random random = new Random(matchSeed + num);
			int[] array = new int[soundFilePaths.Count];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = i;
			}
			for (int num3 = array.Length - 1; num3 > 0; num3--)
			{
				int num4 = random.Next(num3 + 1);
				int num5 = array[num4];
				array[num4] = array[num3];
				array[num3] = num5;
			}
			int index = array[num2];
			string targetFilePath = soundFilePaths[index];
			Debug.Log((object)$"[KillSounds] [CHIVATO] Playing sound #{globalKillCount} -> File: {Path.GetFileName(targetFilePath)} (Seed: {matchSeed})");
			StopActiveSound();
			ThreadPool.QueueUserWorkItem(delegate
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Expected O, but got Unknown
				try
				{
					SoundPlayer val = new SoundPlayer(targetFilePath);
					val.Load();
					lock (audioLock)
					{
						activeSoundPlayer = val;
						val.Play();
					}
				}
				catch (Exception ex)
				{
					Debug.LogError((object)("[KillSounds] Error playing audio: " + ex.Message));
				}
			});
		}
	}
}