Decompiled source of UCH Knockback Mod v1.0.1

KnockbackMod.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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 KnockbackMod
{
	[BepInPlugin("com.uchknockback.mod", "UCH Knockback Mod", "1.0.0")]
	public class KnockbackPlugin : BaseUnityPlugin
	{
		private Harmony harmony;

		private static KnockbackPlugin instance;

		public static ManualLogSource Logger { get; private set; }

		public static ConfigEntry<float> KnockbackForce { get; private set; }

		public static ConfigEntry<float> KnockbackRadius { get; private set; }

		public static ConfigEntry<bool> EnableMod { get; private set; }

		public static ConfigEntry<KeyCode> KnockbackKey { get; private set; }

		private void Awake()
		{
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			EnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable/Disable the knockback mod");
			KnockbackForce = ((BaseUnityPlugin)this).Config.Bind<float>("Knockback", "Force", 15f, "Force of the knockback (higher = stronger push)");
			KnockbackRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Knockback", "Radius", 3f, "Radius of knockback effect");
			KnockbackKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "KnockbackKey", (KeyCode)323, "Key to trigger knockback (Mouse0 = Left Click)");
			Logger.LogInfo((object)"UCH Knockback Mod v1.0.0 is loaded!");
			harmony = new Harmony("com.uchknockback.mod");
			harmony.PatchAll();
			Logger.LogInfo((object)"Harmony patches applied successfully!");
		}

		private void Update()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if (EnableMod.Value && Input.GetKeyDown(KnockbackKey.Value))
			{
				ApplyKnockbackFromLocalPlayer();
			}
		}

		private void ApplyKnockbackFromLocalPlayer()
		{
			//IL_0016: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				GameObject val = FindLocalPlayer();
				if ((Object)(object)val != (Object)null)
				{
					Vector3 position = val.transform.position;
					ApplyKnockbackToNearbyPlayers(position, val);
					Logger.LogInfo((object)$"Knockback triggered from player at position {position}");
				}
				else
				{
					Logger.LogWarning((object)"Could not find local player for knockback");
				}
			}
			catch (Exception arg)
			{
				Logger.LogError((object)$"Error in ApplyKnockbackFromLocalPlayer: {arg}");
			}
		}

		private GameObject FindLocalPlayer()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			GameObject[] array = Object.FindObjectsOfType<GameObject>();
			foreach (GameObject val in array)
			{
				string text = ((Object)val).name.ToLower();
				if ((text.Contains("player") || text.Contains("character")) && val.activeInHierarchy && val.transform.position != Vector3.zero && HasPlayerComponents(val))
				{
					return val;
				}
			}
			return null;
		}

		private bool HasPlayerComponents(GameObject obj)
		{
			if (!((Object)(object)obj.GetComponent<Rigidbody2D>() != (Object)null) && !((Object)(object)obj.GetComponent<Rigidbody>() != (Object)null) && !((Object)(object)obj.GetComponent<CharacterController>() != (Object)null) && !((Object)(object)obj.GetComponent<Collider2D>() != (Object)null))
			{
				return (Object)(object)obj.GetComponent<Collider>() != (Object)null;
			}
			return true;
		}

		private void ApplyKnockbackToNearbyPlayers(Vector3 sourcePosition, GameObject sourcePlayer)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				GameObject[] array = Object.FindObjectsOfType<GameObject>();
				int num = 0;
				GameObject[] array2 = array;
				foreach (GameObject val in array2)
				{
					if (!((Object)(object)val == (Object)(object)sourcePlayer) && IsLikelyPlayer(val))
					{
						float num2 = Vector3.Distance(sourcePosition, val.transform.position);
						if (num2 <= KnockbackRadius.Value && num2 > 0.1f && ApplyKnockbackToPlayer(val, sourcePosition))
						{
							num++;
						}
					}
				}
				Logger.LogInfo((object)$"Applied knockback to {num} nearby players");
			}
			catch (Exception arg)
			{
				Logger.LogError((object)$"Error in ApplyKnockbackToNearbyPlayers: {arg}");
			}
		}

		private bool IsLikelyPlayer(GameObject obj)
		{
			if (!obj.activeInHierarchy)
			{
				return false;
			}
			string text = ((Object)obj).name.ToLower();
			if (text.Contains("player") || text.Contains("character"))
			{
				return HasPlayerComponents(obj);
			}
			return false;
		}

		private bool ApplyKnockbackToPlayer(GameObject player, Vector3 sourcePosition)
		{
			//IL_0006: 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_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: 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_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: 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_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Vector3 val = player.transform.position - sourcePosition;
				Vector3 normalized = ((Vector3)(ref val)).normalized;
				Rigidbody2D component = player.GetComponent<Rigidbody2D>();
				if ((Object)(object)component != (Object)null)
				{
					Vector2 val2 = new Vector2(normalized.x, normalized.y) * KnockbackForce.Value;
					component.AddForce(val2, (ForceMode2D)1);
					Logger.LogInfo((object)$"Applied 2D knockback to {((Object)player).name} with force {val2}");
					return true;
				}
				Rigidbody component2 = player.GetComponent<Rigidbody>();
				if ((Object)(object)component2 != (Object)null)
				{
					Vector3 val3 = normalized * KnockbackForce.Value;
					component2.AddForce(val3, (ForceMode)1);
					Logger.LogInfo((object)$"Applied 3D knockback to {((Object)player).name} with force {val3}");
					return true;
				}
				Vector3 val4 = normalized * (KnockbackForce.Value * 0.1f);
				Transform transform = player.transform;
				transform.position += val4;
				Logger.LogInfo((object)$"Applied position knockback to {((Object)player).name} with offset {val4}");
				return true;
			}
			catch (Exception arg)
			{
				Logger.LogError((object)$"Error applying knockback to {((Object)player).name}: {arg}");
				return false;
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
}