Decompiled source of All Undodgeable Bullets v1.0.2

AllUndodgeableBulletsBIE.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using MonoMod.Cil;
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("AllUndodgeableBulletsBIE")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AllUndodgeableBulletsBIE")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f301fcac-e641-4f73-9242-5de36aff571e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AllUndodgeableBulletsBIE
{
	public static class ILTools
	{
		public static bool JumpToNext(this ILCursor crs, Func<Instruction, bool> match, int times = 1)
		{
			for (int i = 0; i < times; i++)
			{
				if (!crs.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1] { match }))
				{
					return false;
				}
			}
			return true;
		}

		public static bool JumpBeforeNext(this ILCursor crs, Func<Instruction, bool> match, int times = 1)
		{
			for (int i = 0; i < times; i++)
			{
				if (!crs.TryGotoNext((MoveType)((i != times - 1) ? 2 : 0), new Func<Instruction, bool>[1] { match }))
				{
					return false;
				}
			}
			return true;
		}

		public static IEnumerable MatchAfter(this ILCursor crs, Func<Instruction, bool> match)
		{
			Instruction curr = crs.Next;
			crs.Index = 0;
			while (crs.JumpToNext(match))
			{
				yield return null;
			}
			crs.Next = curr;
		}

		public static IEnumerable MatchBefore(this ILCursor crs, Func<Instruction, bool> match)
		{
			Instruction curr = crs.Next;
			crs.Index = 0;
			while (crs.JumpBeforeNext(match))
			{
				Instruction c = crs.Next;
				yield return null;
				crs.Goto(c, (MoveType)2, false);
			}
			crs.Next = curr;
		}

		public static VariableDefinition DeclareLocal<T>(this ILContext ctx)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			VariableDefinition val = new VariableDefinition(ctx.Import(typeof(T)));
			ctx.Body.Variables.Add(val);
			return val;
		}

		public static VariableDefinition DeclareLocal<T>(this ILCursor crs)
		{
			return crs.Context.DeclareLocal<T>();
		}

		public static bool TryGotoArg(this ILCursor crs, Instruction targetInstr, int argIndex, int instance = 0)
		{
			if (argIndex < 0)
			{
				return false;
			}
			if (instance < 0)
			{
				return false;
			}
			if (targetInstr == null)
			{
				return false;
			}
			List<Instruction> argumentInstructions = targetInstr.GetArgumentInstructions(crs.Context, argIndex);
			if (instance >= argumentInstructions.Count)
			{
				return false;
			}
			crs.Goto(argumentInstructions[instance], (MoveType)2, false);
			return true;
		}

		public static bool TryGotoArg(this ILCursor crs, int argIndex, int instance = 0)
		{
			return crs.TryGotoArg(crs.Next, argIndex, instance);
		}

		public static IEnumerable MatchArg(this ILCursor crs, Instruction targetInstr, int argIndex)
		{
			if (argIndex < 0 || targetInstr == null)
			{
				yield break;
			}
			Instruction curr = crs.Next;
			List<Instruction> argumentInstrs = targetInstr.GetArgumentInstructions(crs.Context, argIndex);
			foreach (Instruction arg in argumentInstrs)
			{
				crs.Goto(arg, (MoveType)2, false);
				yield return null;
			}
			crs.Next = curr;
		}

		public static IEnumerable MatchArg(this ILCursor crs, int argIndex)
		{
			return crs.MatchArg(crs.Next, argIndex);
		}

		private static List<Instruction> GetArgumentInstructions(this Instruction instruction, ILContext context, int argumentIndex)
		{
			int num = instruction.InputCount();
			int num2 = num - argumentIndex - 1;
			if (num2 < 0)
			{
				Debug.Log((object)$"Argument index {argumentIndex} is higher than the highest argument index ({num - 1})");
				return new List<Instruction>();
			}
			List<Instruction> list = instruction.PossiblePreviousInstructions(context);
			List<Instruction> list2 = new List<Instruction>();
			foreach (Instruction item in list)
			{
				BacktrackToArg(item, context, num2, list2);
			}
			list2.Sort((Instruction a, Instruction b) => context.IndexOf(a).CompareTo(context.IndexOf(b)));
			return list2;
		}

		private static void BacktrackToArg(Instruction current, ILContext ctx, int remainingMoves, List<Instruction> foundArgs)
		{
			if (remainingMoves <= 0 && current.OutputCount() > 0)
			{
				if (remainingMoves == 0)
				{
					foundArgs.Add(current);
				}
				return;
			}
			remainingMoves -= current.StackDelta();
			List<Instruction> list = current.PossiblePreviousInstructions(ctx);
			foreach (Instruction item in list)
			{
				BacktrackToArg(item, ctx, remainingMoves, foundArgs);
			}
		}

		public static int InputCount(this Instruction instr)
		{
			//IL_0011: 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)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Invalid comparison between Unknown and I4
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Expected I4, but got Unknown
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Invalid comparison between Unknown and I4
			if (instr == null)
			{
				return 0;
			}
			OpCode opCode = instr.OpCode;
			if ((int)((OpCode)(ref opCode)).FlowControl == 2)
			{
				IMethodSignature val = (IMethodSignature)instr.Operand;
				int num = 0;
				if ((int)((OpCode)(ref opCode)).Code != 114 && val.HasThis && !val.ExplicitThis)
				{
					num++;
				}
				if (val.HasParameters)
				{
					num += val.Parameters.Count;
				}
				if ((int)((OpCode)(ref opCode)).Code == 40)
				{
					num++;
				}
				return num;
			}
			StackBehaviour stackBehaviourPop = ((OpCode)(ref opCode)).StackBehaviourPop;
			if (1 == 0)
			{
			}
			int result;
			switch (stackBehaviourPop - 1)
			{
			case 0:
			case 2:
			case 9:
				result = 1;
				break;
			case 1:
			case 3:
			case 4:
			case 5:
			case 7:
			case 8:
			case 10:
			case 11:
				result = 2;
				break;
			case 6:
			case 12:
			case 13:
			case 14:
			case 15:
			case 16:
				result = 3;
				break;
			default:
				result = 0;
				break;
			}
			if (1 == 0)
			{
			}
			return result;
		}

		public static int OutputCount(this Instruction instr)
		{
			//IL_0011: 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)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Invalid comparison between Unknown and I4
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected I4, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Invalid comparison between Unknown and I4
			if (instr == null)
			{
				return 0;
			}
			OpCode opCode = instr.OpCode;
			if ((int)((OpCode)(ref opCode)).FlowControl == 2)
			{
				IMethodSignature val = (IMethodSignature)instr.Operand;
				int num = 0;
				if ((int)((OpCode)(ref opCode)).Code == 114 || (int)val.ReturnType.MetadataType != 1)
				{
					num++;
				}
				return num;
			}
			StackBehaviour stackBehaviourPush = ((OpCode)(ref opCode)).StackBehaviourPush;
			if (1 == 0)
			{
			}
			int result;
			switch (stackBehaviourPush - 20)
			{
			case 0:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				result = 1;
				break;
			case 1:
				result = 2;
				break;
			default:
				result = 0;
				break;
			}
			if (1 == 0)
			{
			}
			return result;
		}

		public static int StackDelta(this Instruction instr)
		{
			return instr.OutputCount() - instr.InputCount();
		}

		public static List<Instruction> PossiblePreviousInstructions(this Instruction instr, ILContext ctx)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			List<Instruction> list = new List<Instruction>();
			Enumerator<Instruction> enumerator = ctx.Instrs.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Instruction current = enumerator.Current;
					if (Array.IndexOf(current.PossibleNextInstructions(), instr) >= 0)
					{
						list.Add(current);
					}
				}
			}
			finally
			{
				((IDisposable)enumerator).Dispose();
			}
			return list;
		}

		public static Instruction[] PossibleNextInstructions(this Instruction instr)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected I4, but got Unknown
			OpCode opCode = instr.OpCode;
			FlowControl flowControl = ((OpCode)(ref opCode)).FlowControl;
			if (1 == 0)
			{
			}
			Instruction[] result;
			switch ((int)flowControl)
			{
			case 2:
			case 5:
				result = (Instruction[])(object)new Instruction[1] { instr.Next };
				break;
			case 0:
			{
				Instruction branchTarget2 = instr.GetBranchTarget();
				result = (Instruction[])(object)((branchTarget2 == null) ? new Instruction[0] : new Instruction[1] { branchTarget2 });
				break;
			}
			case 3:
			{
				Instruction branchTarget = instr.GetBranchTarget();
				result = (Instruction[])(object)((branchTarget == null) ? new Instruction[1] { instr.Next } : new Instruction[2] { instr.Next, branchTarget });
				break;
			}
			default:
				result = (Instruction[])(object)new Instruction[0];
				break;
			}
			if (1 == 0)
			{
			}
			return result;
		}

		public static Instruction GetBranchTarget(this Instruction branch)
		{
			object operand = branch.Operand;
			Instruction val = (Instruction)((operand is Instruction) ? operand : null);
			if (val != null)
			{
				return val;
			}
			object operand2 = branch.Operand;
			return ((ILLabel)(((operand2 is ILLabel) ? operand2 : null)?)).Target;
		}

		public static string InstructionToString(this Instruction c)
		{
			//IL_0012: 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_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Invalid comparison between Unknown and I4
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Invalid comparison between Unknown and I4
			//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_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				return ((object)c).ToString();
			}
			catch
			{
				try
				{
					OpCode opCode = c.OpCode;
					OperandType operandType = ((OpCode)(ref opCode)).OperandType;
					if (((int)operandType == 0 || (int)operandType == 15) ? true : false)
					{
						object operand = c.Operand;
						ILLabel val = (ILLabel)((operand is ILLabel) ? operand : null);
						if (val != null)
						{
							object arg = c.Offset;
							opCode = c.OpCode;
							return $"IL_{arg:x4}: {((OpCode)(ref opCode)).Name} IL_{val.Target.Offset:x4}";
						}
					}
					opCode = c.OpCode;
					if ((int)((OpCode)(ref opCode)).OperandType == 10 && c.Operand is IEnumerable<ILLabel> source)
					{
						object arg2 = c.Offset;
						opCode = c.OpCode;
						return string.Format("IL_{0:x4}: {1} {2}", arg2, ((OpCode)(ref opCode)).Name, string.Join(", ", source.Select((ILLabel x) => x.Target.Offset.ToString("x4")).ToArray()));
					}
				}
				catch
				{
				}
			}
			return "This shouldn't be happening";
		}

		public static T EnumeratorGetField<T>(this object obj, string name)
		{
			return (T)obj.GetType().EnumeratorField(name).GetValue(obj);
		}

		public static FieldInfo EnumeratorField(this MethodBase method, string name)
		{
			return method.DeclaringType.EnumeratorField(name);
		}

		public static FieldInfo EnumeratorField(this Type tp, string name)
		{
			return tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).First((FieldInfo x) => (object)x != null && x.Name != null && (x.Name.Contains("<" + name + ">") || x.Name == name));
		}
	}
	[BepInPlugin("spapi.etg.allundodgeablebullets", "All Undodgeable Bullets", "1.0.2")]
	[HarmonyPatch]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "spapi.etg.allundodgeablebullets";

		public const string NAME = "All Undodgeable Bullets";

		public const string VERSION = "1.0.2";

		public static bool UndodgeableMode;

		public static MethodInfo ub_pc_iif = AccessTools.Method(typeof(Plugin), "UndodgeableBullets_PreCollision_IgnoreInvulnerableFrames", (Type[])null, (Type[])null);

		public static MethodInfo ub_hd_iif = AccessTools.Method(typeof(Plugin), "UndodgeableBullets_HandleDamage_IgnoreInvulnerableFrames", (Type[])null, (Type[])null);

		public static MethodInfo ub_hd_iiffd = AccessTools.Method(typeof(Plugin), "UndodgeableBullets_HandleDamage_IgnoreInvulnerableFramesForDamage", (Type[])null, (Type[])null);

		public void Awake()
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			ETGModConsole.CommandDescriptions["undodgeablemode"] = "Toggles All Undodgeable Bullets mode.";
			ETGModConsole.Commands.AddUnit("undodgeablemode", (Action<string[]>)delegate
			{
				UndodgeableMode = !UndodgeableMode;
				if (!UndodgeableMode)
				{
					ETGModConsole.Log((object)"All Undodgeable Bullets mode disabled.", false);
				}
				else
				{
					ETGModConsole.Log((object)"All Undodgeable Bullets mode enabled.", false);
					List<string> list = new List<string> { "Good luck beating dragun with that on.", "You can go unbind your dodgeroll key now.", "You're lucky that this doesn't work on beams.", "Why?", "Can you beat this mode? I don't think you can." };
					ETGModConsole.Log((object)BraveUtility.RandomElement<string>(list), false);
				}
			});
			new Harmony("spapi.etg.allundodgeablebullets").PatchAll();
		}

		[HarmonyPatch(typeof(Projectile), "OnPreCollision")]
		[HarmonyILManipulator]
		public static void UndodgeableBullets_PreCollision_Transpiler(ILContext ctx)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(ctx);
			if (val.JumpToNext((Instruction x) => ILPatternMatchingExt.MatchCallOrCallvirt<tk2dSpriteAnimator>(x, "QueryInvulnerabilityFrame"), 2))
			{
				val.Emit(OpCodes.Ldarg_3);
				val.Emit(OpCodes.Call, (MethodBase)ub_pc_iif);
			}
		}

		public static bool UndodgeableBullets_PreCollision_IgnoreInvulnerableFrames(bool curr, SpeculativeRigidbody target)
		{
			if (UndodgeableMode && (Object)(object)target != (Object)null && (Object)(object)((BraveBehaviour)target).gameActor != (Object)null && ((BraveBehaviour)target).gameActor is PlayerController)
			{
				return false;
			}
			return curr;
		}

		[HarmonyPatch(typeof(Projectile), "HandleDamage")]
		[HarmonyILManipulator]
		public static void UndodgeableBullets_HandleDamage_Transpiler(ILContext ctx)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_0090: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(ctx);
			if (val.JumpToNext((Instruction x) => ILPatternMatchingExt.MatchCallOrCallvirt<tk2dSpriteAnimator>(x, "QueryInvulnerabilityFrame")))
			{
				val.Emit(OpCodes.Ldarg_1);
				val.Emit(OpCodes.Call, (MethodBase)ub_hd_iif);
				if (val.JumpToNext((Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 0), 7))
				{
					val.Emit(OpCodes.Ldarg_1);
					val.Emit(OpCodes.Call, (MethodBase)ub_hd_iiffd);
				}
			}
		}

		public static bool UndodgeableBullets_HandleDamage_IgnoreInvulnerableFrames(bool curr, SpeculativeRigidbody target)
		{
			if (UndodgeableMode && (Object)(object)target != (Object)null && (Object)(object)((BraveBehaviour)target).gameActor != (Object)null && ((BraveBehaviour)target).gameActor is PlayerController)
			{
				return false;
			}
			return curr;
		}

		public static bool UndodgeableBullets_HandleDamage_IgnoreInvulnerableFramesForDamage(bool curr, SpeculativeRigidbody target)
		{
			if (UndodgeableMode && (Object)(object)target != (Object)null && (Object)(object)((BraveBehaviour)target).gameActor != (Object)null && ((BraveBehaviour)target).gameActor is PlayerController)
			{
				return true;
			}
			return curr;
		}
	}
}