Decompiled source of PoisonPuffer v1.1.0

PoisonPuffer.dll

Decompiled 5 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TestAccount666.PoisonPuffer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("The spore lizard has evolved to be poisonous!")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+3331c3132732146ca4b9477919488f1511e5e570")]
[assembly: AssemblyProduct("PoisonPuffer")]
[assembly: AssemblyTitle("TestAccount666.PoisonPuffer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 PoisonPuffer
{
	[BepInPlugin("TestAccount666.PoisonPuffer", "PoisonPuffer", "1.1.0")]
	public class PoisonPuffer : BaseUnityPlugin
	{
		internal static List<AudioClip>? coughAudioClips;

		public static PoisonPuffer Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Patch();
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			if (directoryName == null)
			{
				Logger.LogError((object)"An error occured while trying to find the assembly directory. Please report this!");
				return;
			}
			coughAudioClips = new List<AudioClip>();
			string text = Path.Combine(directoryName, "coughs");
			text = (Directory.Exists(text) ? text : Path.Combine(directoryName));
			for (int i = 1; i <= 14; i++)
			{
				((MonoBehaviour)this).StartCoroutine(LoadAudioClipFromFile(new Uri(Path.Combine(text, $"cough{i}.wav")), $"cough{i}"));
			}
			Logger.LogInfo((object)"TestAccount666.PoisonPuffer v1.1.0 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("TestAccount666.PoisonPuffer");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}

		private static IEnumerator LoadAudioClipFromFile(Uri filePath, string name)
		{
			UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(filePath, (AudioType)20);
			try
			{
				yield return unityWebRequest.SendWebRequest();
				if ((int)unityWebRequest.result != 1)
				{
					Logger.LogError((object)("Failed to load AudioClip: " + unityWebRequest.error));
					yield break;
				}
				AudioClip clip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
				coughAudioClips?.Add(clip);
				((Object)clip).name = name;
				Logger.LogInfo((object)("Loaded clip '" + name + "'!"));
			}
			finally
			{
				((IDisposable)unityWebRequest)?.Dispose();
			}
		}
	}
	public class PoisonTrigger : MonoBehaviour
	{
		private readonly Random _random = new Random();

		private int? _instanceId;

		private static readonly List<int> _PreviousCoughs = new List<int>(3);

		private void Start()
		{
			((MonoBehaviour)this).StartCoroutine(CheckForPlayers());
		}

		private IEnumerator CheckForPlayers()
		{
			while (true)
			{
				PlayerControllerB localPlayer = StartOfRound.Instance.localPlayerController;
				Vector3 localPlayerPosition = ((Component)localPlayer).transform.position;
				Vector3 poisonPosition = ((Component)this).gameObject.transform.position;
				float distance = Vector3.Distance(poisonPosition, localPlayerPosition);
				if (distance > 8f)
				{
					yield return (object)new WaitForEndOfFrame();
					continue;
				}
				localPlayer.DamagePlayer(_random.Next(3, 9), true, true, (CauseOfDeath)5, 0, false, default(Vector3));
				int? instanceId = _instanceId;
				if (!instanceId.HasValue || Object.FindObjectFromInstanceID(_instanceId.Value) == null)
				{
					GameObject audioObject = new GameObject("TemporaryCoughAudio");
					_instanceId = ((Object)audioObject).GetInstanceID();
					if (PoisonPuffer.coughAudioClips == null)
					{
						yield return (object)new WaitForSeconds(1f);
						continue;
					}
					int soundIndex = _random.Next(0, PoisonPuffer.coughAudioClips.Count);
					while (_PreviousCoughs.Contains(soundIndex))
					{
						soundIndex = _random.Next(0, PoisonPuffer.coughAudioClips.Count);
						yield return (object)new WaitForEndOfFrame();
					}
					if (_PreviousCoughs.Count >= 3)
					{
						_PreviousCoughs.RemoveAt(0);
					}
					_PreviousCoughs.Add(soundIndex);
					AudioClip coughAudio = PoisonPuffer.coughAudioClips[soundIndex];
					if (coughAudio == null)
					{
						yield return (object)new WaitForSeconds(1f);
						continue;
					}
					PoisonPuffer.Logger.LogDebug((object)$"Playing clip '{((Object)coughAudio).name}' ({soundIndex})");
					AudioSource audioSource = audioObject.AddComponent<AudioSource>();
					audioSource.clip = coughAudio;
					audioSource.volume = 1f;
					audioSource.Play();
					Object.Destroy((Object)(object)audioObject, coughAudio.length);
				}
				yield return (object)new WaitForSeconds(1f);
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "TestAccount666.PoisonPuffer";

		public const string PLUGIN_NAME = "PoisonPuffer";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace PoisonPuffer.Patches
{
	[HarmonyPatch]
	public class PufferAIPatch
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> TranspileShakeTailAnimation(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Expected O, but got Unknown
			PoisonPuffer.Logger.LogDebug((object)"Searching for Instantiate...");
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 0; i < list.Count; i++)
			{
				if (!(list[i].opcode != OpCodes.Call) && list[i].operand is MethodInfo methodInfo && methodInfo.Name == "Instantiate")
				{
					PoisonPuffer.Logger.LogDebug((object)"Found!");
					MethodInfo methodInfo2 = AccessTools.Method(typeof(PufferAIPatch), "AddPoisonTrigger", new Type[1] { typeof(GameObject) }, (Type[])null);
					PoisonPuffer.Logger.LogDebug((object)("Injecting method '" + methodInfo2?.ToString() + "'!"));
					list[i + 1] = new CodeInstruction(OpCodes.Call, (object)methodInfo2);
					break;
				}
			}
			return list.AsEnumerable();
		}

		public static void AddPoisonTrigger(GameObject gameObject)
		{
			PoisonPuffer.Logger.LogDebug((object)("Found object: " + (object)gameObject));
			gameObject.AddComponent<PoisonTrigger>();
		}

		[HarmonyPatch(typeof(PufferAI), "DoAIInterval")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> DoAIIntervalTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			FindPattern1(ref instructions);
			FindPattern2(ref instructions);
			return instructions;
		}

		private static void FindPattern1(ref IEnumerable<CodeInstruction> instructions)
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			string[] array = new string[3] { "ldloc.2 NULL", "ldc.r4 5", "bge.un Label13" };
			int num = 0;
			for (int i = 0; i < list.Count(); i++)
			{
				CodeInstruction val = list[i];
				if (!((object)val).ToString().Equals(array[num]))
				{
					num = 0;
					continue;
				}
				num++;
				if (num >= 2)
				{
					list[i] = new CodeInstruction(OpCodes.Ldc_R4, (object)10f);
					num = 0;
					PoisonPuffer.Logger.LogDebug((object)"Found distance!");
				}
			}
			instructions = list;
		}

		private static void FindPattern2(ref IEnumerable<CodeInstruction> instructions)
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			string[] array = new string[3] { "ldfld float PufferAI::timeSinceAlert", "ldc.r4 1.5", "ble.un Label15" };
			int num = 0;
			for (int i = 0; i < list.Count(); i++)
			{
				CodeInstruction val = list[i];
				if (!((object)val).ToString().Equals(array[num]))
				{
					num = 0;
					continue;
				}
				num++;
				if (num >= 2)
				{
					list[i] = new CodeInstruction(OpCodes.Ldc_R4, (object)0.75f);
					num = 0;
					PoisonPuffer.Logger.LogDebug((object)"Found timeSinceAlert!");
				}
			}
			instructions = list;
		}
	}
}