Decompiled source of UnlockMomentum v0.1.6

UnlockMomentum.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using EntityStates;
using EntityStates.Loader;
using EntityStates.Toolbot;
using HG.Reflection;
using HarmonyLib;
using RoR2;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.6.0")]
namespace Local.Unlock.Momentum;

[BepInPlugin("local.unlock.momentum", "UnlockMomentum", "0.1.6")]
public class Plugin : BaseUnityPlugin
{
	public const string identifier = "local.unlock.momentum";

	public const string version = "0.1.6";

	private static Harmony instance;

	public void Awake()
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		Patch();
	}

	[ConCommand(commandName = "toggle_momentum", helpText = "Select air control behavior.")]
	private static void Patch(ConCommandArgs args = default(ConCommandArgs))
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		if (args.userArgs != null && ((ConCommandArgs)(ref args)).Count != 0)
		{
			throw new ConCommandException("No value required.");
		}
		if (instance == null)
		{
			instance = Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
			Debug.Log((object)"Unlocked air control.");
		}
		else
		{
			instance.UnpatchSelf();
			instance = null;
			Debug.Log((object)"Restored default behavior.");
		}
	}

	[HarmonyPatch(typeof(CharacterMotor), "PreMove")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> codeInstructions)
	{
		foreach (CodeInstruction codeInstruction in codeInstructions)
		{
			if (CodeInstructionExtensions.Calls(codeInstruction, typeof(Vector3).GetMethod("MoveTowards")))
			{
				yield return new CodeInstruction(OpCodes.Ldarg_0, (object)null);
				yield return CodeInstruction.Call(typeof(Plugin), "Accelerate", (Type[])null, (Type[])null);
			}
			else if (CodeInstructionExtensions.LoadsField(codeInstruction, typeof(CharacterMotor).GetField("disableAirControlUntilCollision"), false))
			{
				yield return new CodeInstruction(OpCodes.Pop, (object)null);
				yield return new CodeInstruction(OpCodes.Ldc_I4_0, (object)null);
			}
			else
			{
				yield return codeInstruction;
			}
		}
	}

	private static Vector3 Accelerate(Vector3 original, Vector3 target, float delta, CharacterMotor character)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0210: Unknown result type (might be due to invalid IL or missing references)
		//IL_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: 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_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		if (character.acceleration <= 0f)
		{
			return original;
		}
		if (!character.isGrounded || character.isAirControlForced)
		{
			Vector3 val = original;
			if (!character.isFlying)
			{
				target.y = (val.y = 0f);
			}
			CharacterBody body = character.body;
			float num = Mathf.Max(((Vector3)(ref target)).magnitude, body.moveSpeed);
			float magnitude = ((Vector3)(ref val)).magnitude;
			float num2 = magnitude / num;
			if (!body.isSprinting)
			{
				num *= body.sprintingSpeedMultiplier;
			}
			SkillLocator skillLocator = body.skillLocator;
			GenericSkill val2 = ((skillLocator != null) ? skillLocator.FindSkillByFamilyName("LoaderBodySecondaryFamily") : null);
			if (character.disableAirControlUntilCollision && !character.isFlying)
			{
				if (!body.isPlayerControlled || target == Vector3.zero)
				{
					return original;
				}
				if (Object.op_Implicit((Object)(object)val2) && Object.op_Implicit((Object)(object)val2.stateMachine))
				{
					EntityState state = val2.stateMachine.state;
					if (((FireHook)(((state is FireHook) ? state : null)?)).isStuck ?? false)
					{
						return original;
					}
					delta *= 0.75f / Mathf.Pow(character.acceleration, 0.25f);
				}
				else
				{
					delta /= 1.5f;
					if (original.y > 0f)
					{
						delta /= Mathf.Pow(1.25f, original.y * ((Vector3)(ref original)).magnitude / 750f);
					}
				}
			}
			else
			{
				num2 = (1f + num2) / 2f;
			}
			if (num != 0f && num2 > 1f)
			{
				target *= num2;
			}
			num2 = magnitude - num;
			if (num2 > 0f && val2 == null)
			{
				float num3 = 0.5f;
				if (target != Vector3.zero)
				{
					num3 *= 1f - Vector3.Dot(((Vector3)(ref val)).normalized, ((Vector3)(ref target)).normalized);
				}
				delta *= 1f + num2 / (character.acceleration * 0.15f) * num3;
			}
			if (!character.isFlying)
			{
				target.y = original.y;
			}
		}
		return Vector3.MoveTowards(original, target, delta);
	}

	[HarmonyPatch(typeof(ToolbotDash), "OnEnter")]
	[HarmonyPrefix]
	private static void Unlock(EntityState __instance)
	{
		__instance.characterMotor.disableAirControlUntilCollision = false;
	}
}