Decompiled source of Taborusic Modpack v0.2.0

plugins/Taborusic/Taborusic.SonicThumperCompat.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Taborusic.SonicThumperCompat;

[BepInPlugin("Dieforl0l.Taborusic.SonicThumperCompat", "Taborusic SonicThumper Compatibility", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "Dieforl0l.Taborusic.SonicThumperCompat";

	public const string PluginName = "Taborusic SonicThumper Compatibility";

	public const string PluginVersion = "1.0.0";

	private const float ChaseVolume = 0.32f;

	private const string SonicPatchTypeName = "SonicThumper.Patches.SonicThumperPatch";

	private const string SonicPluginTypeName = "SonicThumper.Plugin";

	private static readonly Dictionary<int, AudioSource> ChaseSources = new Dictionary<int, AudioSource>();

	private static readonly Dictionary<int, AudioClip> OriginalBiteClips = new Dictionary<int, AudioClip>();

	private static FieldInfo angeredAudioField;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Expected O, but got Unknown
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Expected O, but got Unknown
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Expected O, but got Unknown
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Expected O, but got Unknown
		harmony = new Harmony("Dieforl0l.Taborusic.SonicThumperCompat");
		Type type = AccessTools.TypeByName("SonicThumper.Plugin");
		Type type2 = AccessTools.TypeByName("SonicThumper.Patches.SonicThumperPatch");
		if (type == null || type2 == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"SonicThumper types were not found; compatibility patch was not applied.");
			return;
		}
		angeredAudioField = AccessTools.Field(type, "AngeredAudio");
		MethodInfo methodInfo = AccessTools.Method(typeof(CrawlerAI), "BeginChasingPlayerClientRpc", (Type[])null, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(CrawlerAI), "EatPlayerBodyClientRpc", (Type[])null, (Type[])null);
		MethodInfo methodInfo3 = AccessTools.Method(typeof(CrawlerAI), "Start", (Type[])null, (Type[])null);
		RemoveSonicPostfix(methodInfo, type2, "StartSonicSound");
		RemoveSonicPostfix(methodInfo2, type2, "StopSonicSound");
		harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "BeginChasingPostfix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "EatPlayerPostfix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		HarmonyMethod val = new HarmonyMethod(AccessTools.Method(typeof(Plugin), "CrawlerStartPrefix", (Type[])null, (Type[])null));
		val.priority = 800;
		HarmonyMethod val2 = new HarmonyMethod(AccessTools.Method(typeof(Plugin), "CrawlerStartPostfix", (Type[])null, (Type[])null));
		val2.priority = 0;
		harmony.Patch((MethodBase)methodInfo3, val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SonicThumper chase audio is isolated, gain-limited and protected from stacking; BitePlayer remains available to SoundAPI.");
	}

	private void RemoveSonicPostfix(MethodBase original, Type patchType, string methodName)
	{
		if (original == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find a v80 CrawlerAI target for " + methodName + "."));
			return;
		}
		Patches patchInfo = Harmony.GetPatchInfo(original);
		if (patchInfo == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("No Harmony patch information found for " + methodName + "."));
			return;
		}
		List<MethodInfo> list = (from patch in patchInfo.Postfixes
			select patch.PatchMethod into method
			where method != null && method.DeclaringType == patchType && method.Name == methodName
			select method).ToList();
		foreach (MethodInfo item in list)
		{
			harmony.Unpatch(original, item);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Replaced " + list.Count + " SonicThumper postfix(es) for " + methodName + "."));
	}

	private static void CrawlerStartPrefix(CrawlerAI __instance)
	{
		if ((Object)(object)__instance != (Object)null && (Object)(object)__instance.bitePlayerSFX != (Object)null)
		{
			OriginalBiteClips[((Object)__instance).GetInstanceID()] = __instance.bitePlayerSFX;
		}
	}

	private static void CrawlerStartPostfix(CrawlerAI __instance)
	{
		if (!((Object)(object)__instance == (Object)null))
		{
			int instanceID = ((Object)__instance).GetInstanceID();
			if (OriginalBiteClips.TryGetValue(instanceID, out var value) && (Object)(object)value != (Object)null)
			{
				__instance.bitePlayerSFX = value;
				OriginalBiteClips.Remove(instanceID);
			}
		}
	}

	private static void BeginChasingPostfix(CrawlerAI __instance)
	{
		if ((Object)(object)__instance == (Object)null || angeredAudioField == null)
		{
			return;
		}
		object? value = angeredAudioField.GetValue(null);
		AudioClip val = (AudioClip)((value is AudioClip) ? value : null);
		if (!((Object)(object)val == (Object)null))
		{
			AudioSource orCreateChaseSource = GetOrCreateChaseSource(__instance);
			if (!((Object)(object)orCreateChaseSource == (Object)null) && !orCreateChaseSource.isPlaying)
			{
				orCreateChaseSource.clip = val;
				orCreateChaseSource.Play();
				WalkieTalkie.TransmitOneShotAudio(orCreateChaseSource, val, 0.32f);
			}
		}
	}

	private static void EatPlayerPostfix(CrawlerAI __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && ChaseSources.TryGetValue(((Object)__instance).GetInstanceID(), out var value) && (Object)(object)value != (Object)null)
		{
			value.Stop();
			value.clip = null;
		}
	}

	private static AudioSource GetOrCreateChaseSource(CrawlerAI crawler)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		int instanceID = ((Object)crawler).GetInstanceID();
		if (ChaseSources.TryGetValue(instanceID, out var value) && (Object)(object)value != (Object)null)
		{
			return value;
		}
		GameObject val = new GameObject("TaborusicSonicChaseAudio");
		val.transform.SetParent(((Component)crawler).transform, false);
		value = val.AddComponent<AudioSource>();
		value.playOnAwake = false;
		value.loop = false;
		value.volume = 0.32f;
		AudioSource creatureSFX = ((EnemyAI)crawler).creatureSFX;
		if ((Object)(object)creatureSFX != (Object)null)
		{
			value.outputAudioMixerGroup = creatureSFX.outputAudioMixerGroup;
			value.spatialBlend = creatureSFX.spatialBlend;
			value.spatialize = creatureSFX.spatialize;
			value.dopplerLevel = creatureSFX.dopplerLevel;
			value.spread = creatureSFX.spread;
			value.rolloffMode = creatureSFX.rolloffMode;
			value.minDistance = creatureSFX.minDistance;
			value.maxDistance = creatureSFX.maxDistance;
			value.priority = creatureSFX.priority;
		}
		ChaseSources[instanceID] = value;
		return value;
	}
}