Decompiled source of WadadadaTurretSounds v1.0.0

plugins/TurretSounds/TurretSounds.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
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: AssemblyTitle("turretboy")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("turretboy")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6cab5558-2c91-40c4-94d0-f194b8a1c832")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TurretSounds;

[BepInPlugin("ken.turretsounds", "Turret Sounds", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string ModGuid = "ken.turretsounds";

	public const string ModName = "Turret Sounds";

	public const string ModVersion = "1.0.0";

	internal static Plugin Instance;

	private static readonly Dictionary<string, AudioClip> Replacements = new Dictionary<string, AudioClip>();

	private static readonly string[] TargetClipNames = new string[4] { "TurretBerserkMode", "TurretFire", "TurretFireDistance", "TurretSeePlayer" };

	private void Awake()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Expected O, but got Unknown
		Instance = this;
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string text = Path.Combine(directoryName, "sounds");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Loading Turret Sounds...");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin folder: " + directoryName));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Sounds folder: " + text));
		LoadAllSounds(text);
		Harmony val = new Harmony("ken.turretsounds");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Turret Sounds loaded. Replacement count: " + Replacements.Count));
	}

	private void LoadAllSounds(string soundsFolder)
	{
		if (!Directory.Exists(soundsFolder))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Sounds folder was not found: " + soundsFolder));
			return;
		}
		string[] targetClipNames = TargetClipNames;
		foreach (string targetName in targetClipNames)
		{
			LoadReplacementSound(soundsFolder, targetName);
		}
	}

	private void LoadReplacementSound(string soundsFolder, string targetName)
	{
		string path = Path.Combine(soundsFolder, targetName + ".wav");
		if (!File.Exists(path))
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("No replacement file found for: " + targetName));
			return;
		}
		AudioClip val = LoadWav(path);
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to load replacement sound: " + Path.GetFileName(path)));
			return;
		}
		((Object)val).name = targetName + "_replacement";
		Replacements[targetName] = val;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded replacement for " + targetName + ": " + Path.GetFileName(path)));
	}

	private AudioClip LoadWav(string path)
	{
		string text = "file:///" + path.Replace("\\", "/");
		UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(text, (AudioType)20);
		try
		{
			UnityWebRequestAsyncOperation val = audioClip.SendWebRequest();
			while (!((AsyncOperation)val).isDone)
			{
			}
			if (audioClip.isNetworkError || audioClip.isHttpError)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Audio load error: " + audioClip.error + " | " + path));
				return null;
			}
			AudioClip content = DownloadHandlerAudioClip.GetContent(audioClip);
			if ((Object)(object)content == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Loaded AudioClip was null: " + path));
				return null;
			}
			return content;
		}
		finally
		{
			((IDisposable)audioClip)?.Dispose();
		}
	}

	internal static AudioClip GetReplacement(string originalClipName)
	{
		if (string.IsNullOrEmpty(originalClipName))
		{
			return null;
		}
		if (Replacements.TryGetValue(originalClipName, out var value))
		{
			return value;
		}
		return null;
	}
}
[HarmonyPatch(typeof(AudioSource))]
internal static class AudioSourcePatch
{
	[HarmonyPatch("PlayOneShot", new Type[] { typeof(AudioClip) })]
	[HarmonyPrefix]
	private static void PlayOneShotPatch(ref AudioClip clip)
	{
		if (!((Object)(object)clip == (Object)null))
		{
			AudioClip replacement = Plugin.GetReplacement(((Object)clip).name);
			if ((Object)(object)replacement != (Object)null)
			{
				clip = replacement;
			}
		}
	}

	[HarmonyPatch("Play", new Type[] { })]
	[HarmonyPrefix]
	private static void PlayPatch(AudioSource __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.clip == (Object)null))
		{
			AudioClip replacement = Plugin.GetReplacement(((Object)__instance.clip).name);
			if ((Object)(object)replacement != (Object)null)
			{
				__instance.clip = replacement;
			}
		}
	}

	[HarmonyPatch("Play", new Type[] { typeof(ulong) })]
	[HarmonyPrefix]
	private static void PlayWithDelayPatch(AudioSource __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.clip == (Object)null))
		{
			AudioClip replacement = Plugin.GetReplacement(((Object)__instance.clip).name);
			if ((Object)(object)replacement != (Object)null)
			{
				__instance.clip = replacement;
			}
		}
	}
}