Decompiled source of DankAudioCore v0.7.0

plugins/DankAudioCore/DankAudioCore.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("DankAudioCore")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("DankAudioCore")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DankAudioCore")]
[assembly: AssemblyTitle("DankAudioCore")]
[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 DankAudioCore
{
	[BepInPlugin("DankAudioCore", "DankAudioCore", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static AudioClip CustomClip;

		public static ManualLogSource Log;

		private static readonly HashSet<string> DankBirdSounds = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "LRADAlarm3", "LradBrainwashingSignal1", "LradBrainwashingSignal4", "LradBrainwashingSignal6", "LradBrainwashingSignal7", "LradBrainwashingSignal8", "robotTune", "RobotTune", "ToWar" };

		private static readonly HashSet<string> LoudBirdSounds = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "RadMechExplosion", "RadMechExplosion1", "RadMechExplosion2", "RadMechExplosion3", "RadMechExplosion4" };

		private static readonly HashSet<string> QuietSounds = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "RadMechAmbientSFX", "RadMechWake" };

		private static readonly HashSet<string> StompSounds = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "RadMechStomp1", "RadMechStomp2", "RadMechStomp3", "RadMechStomp4" };

		private static readonly Dictionary<RadMechAI, float> ActiveBirds = new Dictionary<RadMechAI, float>();

		private void Awake()
		{
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"DankAudioCore is loading!");
			if (!Chainloader.PluginInfos.ContainsKey("LCSoundTool"))
			{
				Log.LogError((object)"DankAudioCore requires LCSoundTool to load the MP3.");
				return;
			}
			try
			{
				Type type = Type.GetType("LCSoundTool.SoundTool, LC_SoundTool");
				if (type == null)
				{
					Log.LogError((object)"Could not find LCSoundTool.SoundTool!");
					return;
				}
				MethodInfo method = type.GetMethod("GetAudioClip", new Type[2]
				{
					typeof(string),
					typeof(string)
				});
				if (method == null)
				{
					Log.LogError((object)"Could not find LCSoundTool GetAudioClip!");
					return;
				}
				object? obj = method.Invoke(null, new object[2] { "DankAudioCore", "Pussy Pussy Marijuana.mp3" });
				CustomClip = (AudioClip)((obj is AudioClip) ? obj : null);
				if ((Object)(object)CustomClip == (Object)null)
				{
					Log.LogError((object)"Could not load Pussy Pussy Marijuana.mp3!");
					return;
				}
				Log.LogInfo((object)"Successfully loaded Pussy Pussy Marijuana.mp3!");
				Harmony val = new Harmony("DankAudioCore");
				val.PatchAll();
				Log.LogInfo((object)"DankAudioCore audio interception patches installed!");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("DankAudioCore failed to initialize: " + ex));
			}
		}

		public static bool IsDankBirdSound(AudioClip clip)
		{
			if ((Object)(object)clip == (Object)null)
			{
				return false;
			}
			return DankBirdSounds.Contains(((Object)clip).name);
		}

		public static bool IsLoudBirdSound(AudioClip clip)
		{
			if ((Object)(object)clip == (Object)null)
			{
				return false;
			}
			return LoudBirdSounds.Contains(((Object)clip).name);
		}

		public static bool IsQuietSound(AudioClip clip)
		{
			if ((Object)(object)clip == (Object)null)
			{
				return false;
			}
			return QuietSounds.Contains(((Object)clip).name);
		}

		public static bool IsStompSound(AudioClip clip)
		{
			if ((Object)(object)clip == (Object)null)
			{
				return false;
			}
			return StompSounds.Contains(((Object)clip).name);
		}

		public static RadMechAI FindOldBird(AudioSource source)
		{
			if ((Object)(object)source == (Object)null)
			{
				return null;
			}
			return ((Component)source).GetComponentInParent<RadMechAI>();
		}

		public static bool TryPlayDankBird(AudioSource source, AudioClip originalClip, float volumeScale)
		{
			if ((Object)(object)source == (Object)null || (Object)(object)originalClip == (Object)null || (Object)(object)CustomClip == (Object)null)
			{
				return false;
			}
			RadMechAI val = FindOldBird(source);
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			if (ActiveBirds.TryGetValue(val, out var value))
			{
				if (Time.time < value)
				{
					return true;
				}
				ActiveBirds.Remove(val);
			}
			ActiveBirds[val] = Time.time + CustomClip.length;
			source.PlayOneShot(CustomClip, volumeScale * 2.5f);
			Log.LogInfo((object)("DANKAUDIOCORE: " + ((Object)originalClip).name + " -> PUSSY AUDIO"));
			return true;
		}

		public static void HandleNormalSound(AudioSource source, AudioClip clip)
		{
			if (!((Object)(object)source == (Object)null) && !((Object)(object)clip == (Object)null))
			{
				if (IsLoudBirdSound(clip))
				{
					source.volume *= 0.5f;
					Log.LogInfo((object)("DANKAUDIOCORE: Reduced loud Old Bird sound: " + ((Object)clip).name));
				}
				else if (IsStompSound(clip))
				{
					source.volume *= 0.7f;
					Log.LogInfo((object)("DANKAUDIOCORE: Reduced Old Bird stomp: " + ((Object)clip).name));
				}
				else if (IsQuietSound(clip))
				{
					source.volume *= 0.3f;
					Log.LogInfo((object)("DANKAUDIOCORE: Quieted Old Bird sound: " + ((Object)clip).name));
				}
			}
		}
	}
	[HarmonyPatch]
	public static class AudioSourcePlayPatch
	{
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method(typeof(AudioSource), "Play", new Type[0], (Type[])null);
		}

		private static bool Prefix(AudioSource __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			AudioClip clip = __instance.clip;
			if ((Object)(object)clip == (Object)null)
			{
				return true;
			}
			RadMechAI val = Plugin.FindOldBird(__instance);
			if ((Object)(object)val == (Object)null)
			{
				return true;
			}
			if (Plugin.IsDankBirdSound(clip))
			{
				bool flag = Plugin.TryPlayDankBird(__instance, clip, 1f);
				return !flag;
			}
			Plugin.HandleNormalSound(__instance, clip);
			return true;
		}
	}
	[HarmonyPatch]
	public static class AudioSourcePlayOneShotPatch
	{
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method(typeof(AudioSource), "PlayOneShot", new Type[2]
			{
				typeof(AudioClip),
				typeof(float)
			}, (Type[])null);
		}

		private static bool Prefix(AudioSource __instance, AudioClip clip, float volumeScale)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)clip == (Object)null)
			{
				return true;
			}
			RadMechAI val = Plugin.FindOldBird(__instance);
			if ((Object)(object)val == (Object)null)
			{
				return true;
			}
			if (Plugin.IsDankBirdSound(clip))
			{
				bool flag = Plugin.TryPlayDankBird(__instance, clip, volumeScale);
				return !flag;
			}
			Plugin.HandleNormalSound(__instance, clip);
			return true;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "DankAudioCore";

		public const string PLUGIN_NAME = "DankAudioCore";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}