Decompiled source of GrenadeTurret v1.0.0

GrenadeTurret.dll

Decompiled 10 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GrenadeTurret.Patches;
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("GrenadeTurret")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GrenadeTurret")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("63f51f95-93ff-4c35-8370-a0a372261158")]
[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 GrenadeTurret
{
	[BepInPlugin("f0ur3y3s.GrenadeTurret", "GrenadeTurret", "1.0.0")]
	public class GrenadeTurretBase : BaseUnityPlugin
	{
		private const string modGUID = "f0ur3y3s.GrenadeTurret";

		private const string modName = "GrenadeTurret";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("f0ur3y3s.GrenadeTurret");

		private static GrenadeTurretBase Instance;

		private static ManualLogSource logger;

		internal static ManualLogSource GetLogger()
		{
			return logger;
		}

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			logger = Logger.CreateLogSource("GrenadeTurret");
			logger.LogInfo((object)"GrenadeTurret Loaded!");
			logger.LogInfo((object)"Patching GrenadeTurretBase");
			harmony.PatchAll(typeof(GrenadeTurretBase));
			logger.LogInfo((object)"Patching GrenadeTurretPatch");
			harmony.PatchAll(typeof(GrenadeTurretPatch));
		}
	}
}
namespace GrenadeTurret.Patches
{
	internal class GrenadeTurretPatch
	{
		private static readonly OpCode[] DamageOpcodes = new OpCode[11]
		{
			OpCodes.Ldfld,
			OpCodes.Ldc_I4_S,
			OpCodes.Ldc_I4_1,
			OpCodes.Ldc_I4_1,
			OpCodes.Ldc_I4_7,
			OpCodes.Ldc_I4_0,
			OpCodes.Ldc_I4_0,
			OpCodes.Ldloca_S,
			OpCodes.Initobj,
			OpCodes.Ldloc_3,
			OpCodes.Callvirt
		};

		internal static GameObject mapPropsContainer;

		internal static SpawnableMapObject turretSMO;

		[HarmonyPatch(typeof(Turret), "Update")]
		[HarmonyPostfix]
		private static void UpdatePatch(Turret __instance)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Invalid comparison between Unknown and I4
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			FieldInfo field = typeof(Turret).GetField("turretInterval", BindingFlags.Instance | BindingFlags.NonPublic);
			float num = (float)field.GetValue(__instance);
			if ((int)__instance.turretMode == 2 && 0f == num)
			{
				GrenadeTurretBase.GetLogger().LogInfo((object)"Turret is firing!!!");
				Vector3 position = __instance.aimPoint.position;
				Vector3 forward = __instance.aimPoint.forward;
				float num2 = 50f;
				Ray val = default(Ray);
				((Ray)(ref val))..ctor(position, forward);
				RaycastHit val2 = default(RaycastHit);
				if (Physics.Raycast(val, ref val2, num2, StartOfRound.Instance.collidersAndRoomMaskAndDefault))
				{
					ManualLogSource logger = GrenadeTurretBase.GetLogger();
					Vector3 point = ((RaycastHit)(ref val2)).point;
					logger.LogInfo((object)("Turret bullet hit at " + ((object)(Vector3)(ref point)).ToString()));
					GrenadeTurretBase.GetLogger().LogInfo((object)("Turret bullet hit on " + ((Object)((Component)((RaycastHit)(ref val2)).collider).gameObject).name));
					GrenadeTurretBase.GetLogger().LogInfo((object)("Turret bullet hit on " + ((Component)((RaycastHit)(ref val2)).collider).gameObject.tag));
					GrenadeTurretBase.GetLogger().LogInfo((object)("Turret bullet hit on " + ((Component)((RaycastHit)(ref val2)).collider).gameObject.layer));
					Vector3 point2 = ((RaycastHit)(ref val2)).point;
					Landmine.SpawnExplosion(point2 + Vector3.up, true, 5.7f, 6.4f);
					GrenadeTurretBase.GetLogger().LogInfo((object)"Spawning explosion");
				}
			}
		}

		private static bool IsOpcodesMatch(List<CodeInstruction> instructions, OpCode[] expectedOpcodes, int startIndex)
		{
			for (int i = 0; i < 11; i++)
			{
				if (instructions[startIndex + i].opcode != expectedOpcodes[i])
				{
					return false;
				}
			}
			GrenadeTurretBase.GetLogger().LogInfo((object)"Found a nopper");
			return true;
		}

		private static bool IsCallGNMGetInstance(List<CodeInstruction> instructions, int index)
		{
			if (instructions[index].opcode == OpCodes.Call && instructions[index].operand is MethodInfo methodInfo && methodInfo.DeclaringType == typeof(GameNetworkManager) && methodInfo.Name == "get_Instance")
			{
				return true;
			}
			return false;
		}

		[HarmonyPatch(typeof(Turret), "Update")]
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			for (int i = 0; i < list.Count; i++)
			{
				if (IsCallGNMGetInstance(list, i) && IsOpcodesMatch(list, DamageOpcodes, i + 1))
				{
					for (int j = 0; j < 12; j++)
					{
						list[i + j].opcode = OpCodes.Nop;
						GrenadeTurretBase.GetLogger().LogInfo((object)"Nopped");
					}
				}
			}
			return list.AsEnumerable();
		}

		[HarmonyPatch(typeof(RoundManager), "SpawnMapObjects")]
		[HarmonyPostfix]
		private static void RoundManagerPatch(ref RoundManager __instance)
		{
			mapPropsContainer = GameObject.FindGameObjectWithTag("MapPropsContainer");
			SpawnableMapObject[] spawnableMapObjects = __instance.currentLevel.spawnableMapObjects;
			foreach (SpawnableMapObject val in spawnableMapObjects)
			{
				if (!((Object)(object)val.prefabToSpawn.GetComponentInChildren<Turret>() == (Object)null))
				{
					turretSMO = val;
					if (turretSMO == null)
					{
						GrenadeTurretBase.GetLogger().LogInfo((object)"Turret map object is null");
					}
					GrenadeTurretBase.GetLogger().LogInfo((object)"Turret map object found");
				}
			}
		}
	}
}