Decompiled source of RepoFunnyDeathSounds v1.1.0

RepoFunnyDeathSounds.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RepoFunnyDeathSounds")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RepoFunnyDeathSounds")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("82778b08-088f-441f-86a6-ddcf5fa20c13")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RepoFunnyDeathSounds;

[BepInPlugin("com.spelledeye.repofunnydeathsounds", "RepoFunnyDeathSounds", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
	private const string MOD_GUID = "com.spelledeye.repofunnydeathsounds";

	private const string MOD_NAME = "RepoFunnyDeathSounds";

	private const string MOD_VERSION = "1.1.0";

	private readonly Harmony harmony = new Harmony("com.spelledeye.repofunnydeathsounds");

	public static ManualLogSource Log;

	public static AudioClip[] DeathSounds;

	public static string ModDirectory;

	public static ConfigEntry<float> VolumeConfig;

	private void Awake()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		ModDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		Log.LogInfo((object)"RepoFunnyDeathSounds successfully initialized!");
		VolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 1f, new ConfigDescription("Death sound volume", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		if (Random.Range(1, 11) == 1)
		{
			Log.LogWarning((object)"File: 'Machine_Uprising_Plan' not initialized");
		}
		LoadSounds();
		harmony.PatchAll();
	}

	private void LoadSounds()
	{
		string modDirectory = ModDirectory;
		string[] files = Directory.GetFiles(modDirectory, "*.wav");
		if (files.Length == 0)
		{
			Log.LogWarning((object)("In the folder " + modDirectory + " No .wav audio files found!"));
			return;
		}
		DeathSounds = (AudioClip[])(object)new AudioClip[files.Length];
		for (int i = 0; i < files.Length; i++)
		{
			try
			{
				byte[] wavFile = File.ReadAllBytes(files[i]);
				AudioClip val = OpenWavParser.ToAudioClip(wavFile, Path.GetFileNameWithoutExtension(files[i]));
				if ((Object)(object)val != (Object)null)
				{
					DeathSounds[i] = val;
					Log.LogInfo((object)("Track successfully uploaded: " + ((Object)val).name));
				}
				else
				{
					Log.LogError((object)("Failed to parse the WAV file: " + files[i]));
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to process the file " + files[i] + ": " + ex.Message));
			}
		}
		Log.LogInfo((object)$"Total sounds loaded for the mod: {DeathSounds.Length}");
	}
}
public static class OpenWavParser
{
	public static AudioClip ToAudioClip(byte[] wavFile, string name)
	{
		try
		{
			if (wavFile.Length < 44 || wavFile[0] != 82 || wavFile[1] != 73 || wavFile[2] != 70 || wavFile[3] != 70)
			{
				return null;
			}
			int num = BitConverter.ToInt16(wavFile, 22);
			int num2 = BitConverter.ToInt32(wavFile, 24);
			int i;
			for (i = 12; i < wavFile.Length - 4 && (wavFile[i] != 100 || wavFile[i + 1] != 97 || wavFile[i + 2] != 116 || wavFile[i + 3] != 97); i++)
			{
			}
			i += 4;
			int num3 = BitConverter.ToInt32(wavFile, i);
			i += 4;
			int num4 = num3 / 2;
			float[] array = new float[num4];
			for (int j = 0; j < num4; j++)
			{
				if (i + 1 >= wavFile.Length)
				{
					break;
				}
				short num5 = BitConverter.ToInt16(wavFile, i);
				array[j] = (float)num5 / 32768f;
				i += 2;
			}
			AudioClip val = AudioClip.Create(name, num4 / num, num, num2, false);
			val.SetData(array, 0);
			return val;
		}
		catch
		{
			return null;
		}
	}
}
[HarmonyPatch(typeof(PlayerHealth), "Death")]
public class DeathPatch
{
	[HarmonyPostfix]
	public static void OnPlayerDie(PlayerHealth __instance)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Plugin.Log.LogInfo((object)"[[Patch] Call to the original Death() method intercepted!");
			if (Plugin.DeathSounds == null || Plugin.DeathSounds.Length == 0)
			{
				Plugin.Log.LogWarning((object)"[Patch] Sound list is empty. Playback cancelled.");
				return;
			}
			int num = Random.Range(0, Plugin.DeathSounds.Length);
			AudioClip val = Plugin.DeathSounds[num];
			Vector3 position = ((Component)__instance).transform.position;
			float value = Plugin.VolumeConfig.Value;
			AudioSource.PlayClipAtPoint(val, position, value);
			Plugin.Log.LogInfo((object)("[Patch] Added a random death sound: " + ((Object)val).name));
		}
		catch (Exception arg)
		{
			Plugin.Log.LogError((object)$"[Patch] Error during patch code execution: {arg}");
		}
	}
}