Decompiled source of UltraMagnet v0.2.1

UltraMagnet.dll

Decompiled 2 weeks ago
using System;
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.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using PluginConfig.API;
using PluginConfig.API.Decorators;
using PluginConfig.API.Fields;
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("UltraMagnet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UltraMagnet")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("75f6899f-9595-4ae3-95d9-6a38a5411b24")]
[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 UltraMagnet;

[HarmonyPatch]
public static class Breakable_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Breakable), "Start")]
	private static void patch_Start(Breakable __instance)
	{
		Harpoon componentInParent = ((Component)__instance).GetComponentInParent<Harpoon>();
		if (Object.op_Implicit((Object)(object)componentInParent) && !componentInParent.drill)
		{
			((Component)__instance).gameObject.SetActive(!ConfigManager.unbreakable.value);
		}
	}
}
[HarmonyPatch]
public static class Harpoon_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Harpoon), "Start")]
	private static void patch_Start(Harpoon __instance)
	{
		if (ConfigManager.magnetPatchPanel.value && !__instance.drill)
		{
			ComponentExtensions.GetOrAddComponent<DirectMagnetScript>((Component)(object)__instance);
		}
		else if (ConfigManager.harpoonPatchPanel.value)
		{
			ComponentExtensions.GetOrAddComponent<DirectMagnetScript>((Component)(object)__instance);
		}
	}

	[HarmonyTranspiler]
	[HarmonyPatch(typeof(Harpoon), "Update")]
	private static IEnumerable<CodeInstruction> Transpiler_Update(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Expected O, but got Unknown
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Expected O, but got Unknown
		if (!ConfigManager.harpoonPatchPanel.value)
		{
			Debug.Log((object)"[UltraMagnet] Harpoon Update transpiler skipped - patch disabled");
			return instructions;
		}
		Debug.Log((object)"[UltraMagnet] Starting Harpoon Update transpiler");
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		for (int i = 0; i < list.Count; i++)
		{
			if (!(list[i].opcode == OpCodes.Ldarg_0) || i + 1 >= list.Count || !CodeInstructionExtensions.LoadsField(list[i + 1], AccessTools.Field(typeof(Harpoon), "stopped"), false))
			{
				continue;
			}
			Debug.Log((object)("[UltraMagnet] Found stopped field check at index " + i));
			int num = i;
			for (int j = i; j < list.Count; j++)
			{
				if (list[j].opcode == OpCodes.Ret && j < list.Count - 1)
				{
					num = j;
					Debug.Log((object)("[UltraMagnet] Found end ret instruction at index " + j));
					break;
				}
			}
			if (num > i)
			{
				Debug.Log((object)("[UltraMagnet] Replacing instruction sequence from index " + i + " to " + num));
				list[i] = new CodeInstruction(OpCodes.Br, (object)list[num + 1].labels[0]);
				for (int k = i + 1; k <= num; k++)
				{
					list[k] = new CodeInstruction(OpCodes.Nop, (object)null);
				}
				break;
			}
		}
		Debug.Log((object)"[UltraMagnet] Completed Harpoon Update transpiler");
		return list.AsEnumerable();
	}

	[HarmonyTranspiler]
	[HarmonyPatch(typeof(Harpoon), "OnTriggerEnter")]
	private static IEnumerable<CodeInstruction> Transpiler_OnTriggerEnter(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Expected O, but got Unknown
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Expected O, but got Unknown
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Expected O, but got Unknown
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Expected O, but got Unknown
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Expected O, but got Unknown
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Expected O, but got Unknown
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		LocalBuilder localBuilder = generator.DeclareLocal(typeof(DirectMagnetScript));
		List<CodeInstruction> list2 = new List<CodeInstruction>();
		list2.Add(new CodeInstruction(OpCodes.Ldarg_0, (object)null));
		MethodInfo methodInfo = AccessTools.Method(typeof(Component), "GetComponent", new Type[0], (Type[])null);
		methodInfo = methodInfo.MakeGenericMethod(typeof(DirectMagnetScript));
		list2.Add(new CodeInstruction(OpCodes.Callvirt, (object)methodInfo));
		list2.Add(new CodeInstruction(OpCodes.Stloc, (object)localBuilder));
		list2.Add(new CodeInstruction(OpCodes.Ldloc, (object)localBuilder));
		Label label = generator.DefineLabel();
		list2.Add(new CodeInstruction(OpCodes.Brfalse_S, (object)label));
		list2.Add(new CodeInstruction(OpCodes.Ldloc, (object)localBuilder));
		MethodInfo methodInfo2 = AccessTools.Method(typeof(Object), "Destroy", new Type[1] { typeof(Object) }, (Type[])null);
		list2.Add(new CodeInstruction(OpCodes.Call, (object)methodInfo2));
		CodeInstruction val = new CodeInstruction(OpCodes.Nop, (object)null);
		val.labels.Add(label);
		list2.Add(val);
		int num = -1;
		for (int i = 0; i < list.Count - 1; i++)
		{
			if ((list[i].opcode == OpCodes.Ldc_I4_S || list[i].opcode == OpCodes.Ldc_I4) && list[i].operand != null && Convert.ToInt32(list[i].operand) == 24 && list[i + 1].opcode == OpCodes.Bne_Un)
			{
				num = i + 2;
				break;
			}
		}
		if (num != -1)
		{
			list.InsertRange(num, list2);
			Debug.Log((object)"[UltraMagnet] Injected DirectMagnetScript removal code into OnTriggerEnter transpiler");
		}
		else
		{
			Debug.LogError((object)"[UltraMagnet] Failed to find injection point in Harpoon.OnTriggerEnter");
		}
		return list.AsEnumerable();
	}
}
[HarmonyPatch]
public static class Cannonball_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Cannonball), "Start")]
	private static void patch_Start(Cannonball __instance)
	{
		if (ConfigManager.cannonballPatchPanel.value)
		{
			ComponentExtensions.GetOrAddComponent<MagnetScript>((Component)(object)__instance);
		}
	}
}
[HarmonyPatch]
public static class Coin_Patch
{
	private static MagnetScript script;

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Coin), "Start")]
	private static void patch_Start(Coin __instance)
	{
		if (ConfigManager.coinPatchPanel.value)
		{
			script = ComponentExtensions.GetOrAddComponent<MagnetScript>((Component)(object)__instance);
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Coin), "Update")]
	private static void patch_Update(Coin __instance)
	{
		if (script.isOnMagnet)
		{
			((MonoBehaviour)__instance).CancelInvoke("GetDeleted");
		}
		else
		{
			((MonoBehaviour)__instance).Invoke("GetDeleted", 5f);
		}
	}
}
public static class ConfigManager
{
	public enum MagnetType
	{
		Rocket,
		Chainsaw
	}

	public static PluginConfigurator config;

	public static FloatField radius;

	public static FloatField spinning;

	public static BoolField unbreakable;

	public static BoolField magnetPatchPanel;

	public static BoolField cannonballPatchPanel;

	public static BoolField coinPatchPanel;

	public static BoolField projectilePatchPanel;

	public static BoolField harpoonPatchPanel;

	public static BoolField rocketPatchPanel;

	public static BoolField grenadePatchPanel;

	public static void Init()
	{
		config = PluginConfigurator.Create("UltraMagnet", "ostia.UltraMagnet");
		SetupIcon();
		SetupPatchToggles();
		SetupMagnetSettings();
	}

	private static void SetupIcon()
	{
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		string text = Path.Combine(directoryName, "icon.png");
		config.SetIconWithURL("file://" + text);
	}

	private static void SetupPatchToggles()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Expected O, but got Unknown
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Expected O, but got Unknown
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Expected O, but got Unknown
		new ConfigHeader(config.rootPanel, "Patches", 24);
		cannonballPatchPanel = new BoolField(config.rootPanel, "Cannonball", "cannonballPatchPanel", true);
		coinPatchPanel = new BoolField(config.rootPanel, "Coin", "coinPatchPanel", true);
		projectilePatchPanel = new BoolField(config.rootPanel, "Projectile", "projectilePatchPanel", true);
		harpoonPatchPanel = new BoolField(config.rootPanel, "Harpoon (Require restart)", "harpoonPatchPanel", true);
		grenadePatchPanel = new BoolField(config.rootPanel, "Grenade", "grenadePatchPanel", true);
		rocketPatchPanel = new BoolField(config.rootPanel, "Rocket (Require restart)", "rocketPatchPanel", true);
	}

	private static void SetupMagnetSettings()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Expected O, but got Unknown
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Expected O, but got Unknown
		new ConfigHeader(config.rootPanel, "Magnet", 24);
		magnetPatchPanel = new BoolField(config.rootPanel, "Magnet", "magnetPatchPanel", true);
		unbreakable = new BoolField(config.rootPanel, "Unbreakable", "unbreakable", true);
		radius = new FloatField(config.rootPanel, "Radius", "radius", 1000f, 0f, float.MaxValue);
		spinning = new FloatField(config.rootPanel, "Spinning", "spinning", 80f, 0f, 85f);
	}
}
[HarmonyPatch]
public static class Grenade_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Grenade), "Start")]
	private static void patch_Start(Grenade __instance)
	{
		if (ConfigManager.grenadePatchPanel.value && !__instance.rocket)
		{
			ComponentExtensions.GetOrAddComponent<MagnetScript>((Component)(object)__instance);
		}
	}
}
public class DirectMagnetScript : MonoBehaviour
{
	public List<Magnet> magnets = new List<Magnet>();

	public Rigidbody rb;

	private void Awake()
	{
		rb = ((Component)this).GetComponent<Rigidbody>();
	}

	private void FixedUpdate()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		rb.velocity = ((Component)this).transform.forward * 150f;
		if (magnets.Count <= 0)
		{
			return;
		}
		int num = magnets.Count - 1;
		while (num >= 0)
		{
			if ((Object)(object)magnets[num] == (Object)null)
			{
				magnets.RemoveAt(num);
				num--;
				continue;
			}
			((Component)this).transform.rotation = Quaternion.RotateTowards(((Component)this).transform.rotation, Quaternion.LookRotation(((Component)magnets[num]).transform.position - ((Component)this).transform.position), Time.fixedDeltaTime * 180f);
			break;
		}
	}
}
public class MagnetScript : MonoBehaviour
{
	public List<Magnet> magnets = new List<Magnet>();

	public bool isOnMagnet;

	private int magnetRotationDirection;

	private Rigidbody rb;

	public event Action<bool> onMagnet;

	private Magnet GetTargetMagnet()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		Magnet result = null;
		float num = float.PositiveInfinity;
		for (int i = 0; i < magnets.Count; i++)
		{
			Vector3 val = ((Component)magnets[i]).transform.position - ((Component)this).transform.position;
			float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
			if (sqrMagnitude < num)
			{
				num = sqrMagnitude;
				result = magnets[i];
				Vector3 val2 = new Vector3(rb.velocity.z, rb.velocity.y, 0f - rb.velocity.x);
				Vector3 normalized = ((Vector3)(ref val2)).normalized;
				if (Vector3.Dot(val, normalized) > 0f)
				{
					magnetRotationDirection = -1;
				}
				else
				{
					magnetRotationDirection = 1;
				}
			}
		}
		return result;
	}

	private void Start()
	{
		rb = ((Component)this).GetComponent<Rigidbody>();
	}

	private void LateUpdate()
	{
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		if (magnets.Count > 0)
		{
			magnets.RemoveAll((Magnet magnet) => (Object)(object)magnet == (Object)null);
			if (magnets.Count != 0)
			{
				Magnet targetMagnet = GetTargetMagnet();
				if (Object.op_Implicit((Object)(object)targetMagnet))
				{
					isOnMagnet = true;
					Rigidbody obj = rb;
					Vector3 velocity = rb.velocity;
					Quaternion val = Quaternion.Euler(0f, ConfigManager.spinning.value * (float)magnetRotationDirection, 0f);
					Vector3 val2 = ((Component)targetMagnet).transform.position - ((Component)this).transform.position;
					Vector3 val3 = val * ((Vector3)(ref val2)).normalized;
					val2 = rb.velocity;
					Vector3 val4 = val3 * ((Vector3)(ref val2)).magnitude;
					val2 = rb.velocity;
					obj.velocity = Vector3.RotateTowards(velocity, val4, float.PositiveInfinity, ((Vector3)(ref val2)).magnitude);
				}
			}
		}
		else
		{
			isOnMagnet = false;
		}
	}
}
[HarmonyPatch]
public static class Magnet_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Magnet), "Start")]
	private static void patch_Start(Magnet __instance)
	{
		((Component)__instance).GetComponent<SphereCollider>().radius = ConfigManager.radius.value;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Magnet), "OnTriggerEnter")]
	private static void patch_OnTriggerEnter(Magnet __instance, ref Collider other)
	{
		MagnetScript magnetScript = default(MagnetScript);
		if (((Component)other).TryGetComponent<MagnetScript>(ref magnetScript) && !magnetScript.magnets.Contains(__instance))
		{
			magnetScript.magnets.Add(__instance);
		}
		DirectMagnetScript directMagnetScript = default(DirectMagnetScript);
		if (((Component)other).TryGetComponent<DirectMagnetScript>(ref directMagnetScript) && !directMagnetScript.magnets.Contains(__instance))
		{
			directMagnetScript.magnets.Add(__instance);
		}
	}

	[HarmonyTranspiler]
	[HarmonyPatch(typeof(Magnet), "OnTriggerEnter")]
	private static IEnumerable<CodeInstruction> Transpiler_OnTriggerEnter(IEnumerable<CodeInstruction> instructions)
	{
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Expected O, but got Unknown
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Expected O, but got Unknown
		if (!ConfigManager.rocketPatchPanel.value)
		{
			return instructions;
		}
		Debug.Log((object)"[UltraMagnet] Starting OnTriggerEnter transpiler");
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		for (int i = 0; i < list.Count; i++)
		{
			if (!(list[i].opcode == OpCodes.Ldarg_0) || i + 1 >= list.Count || !CodeInstructionExtensions.LoadsField(list[i + 1], AccessTools.Field(typeof(Magnet), "onEnemy"), false))
			{
				continue;
			}
			Debug.Log((object)("[UltraMagnet] Found onEnemy check sequence at index " + i));
			int num = i;
			for (int j = i; j < list.Count; j++)
			{
				if (CodeInstructionExtensions.LoadsField(list[j], AccessTools.Field(typeof(Grenade), "enemy"), false))
				{
					num = j;
					Debug.Log((object)("[UltraMagnet] Found grenade.enemy check at index " + j));
					break;
				}
			}
			if (num > i)
			{
				Debug.Log((object)("[UltraMagnet] Replacing instruction sequence from index " + i + " to " + num));
				list[i] = new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
				for (int k = i + 1; k <= num; k++)
				{
					list[k] = new CodeInstruction(OpCodes.Nop, (object)null);
				}
				break;
			}
		}
		Debug.Log((object)"[UltraMagnet] Completed OnTriggerEnter transpiler");
		return list.AsEnumerable();
	}
}
[BepInPlugin("ostia.UltraMagnet", "UltraMagnet", "0.2.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public const string PLUGIN_GUID = "ostia.UltraMagnet";

	public const string PLUGIN_NAME = "UltraMagnet";

	public const string PLUGIN_VERSION = "0.2.1";

	private static Harmony harmony;

	private void Awake()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		ConfigManager.Init();
		harmony = new Harmony("ostia.UltraMagnet");
		harmony.PatchAll();
	}
}
[HarmonyPatch]
public static class Projectile_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Projectile), "Start")]
	private static void patch_Start(Projectile __instance)
	{
		if (ConfigManager.projectilePatchPanel.value)
		{
			ComponentExtensions.GetOrAddComponent<MagnetScript>((Component)(object)__instance);
		}
	}
}