Decompiled source of RealisticSpears v0.1.0

plugins/RealisticSpears.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("RealisticSpears")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RealisticSpears")]
[assembly: AssemblyTitle("RealisticSpears")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace RealisticSpears
{
	[HarmonyPatch(typeof(Humanoid), "EquipItem")]
	public static class Patch_Humanoid_EquipItem
	{
		private static void Prefix(Humanoid __instance, ItemData item)
		{
			if (item != null)
			{
				SharedData shared = item.m_shared;
				if (SpearUtils.IsSpearWithPokeAttack_Shared(shared))
				{
					shared.m_attack.m_attackAnimation = "sword_secondary";
					shared.m_attack.m_attackRange = 3f;
					shared.m_attack.m_attackHeight = 1f;
					shared.m_attack.m_attackAngle = 30f;
					shared.m_attack.m_attackRayWidth = 0.4f;
				}
			}
		}
	}
	[HarmonyPatch(typeof(VisEquipment), "CustomUpdate")]
	public static class Patch_VisEquipment_CustomUpdate
	{
		private static readonly FieldInfo RightItemField = AccessTools.Field(typeof(VisEquipment), "m_rightItemInstance");

		public static void Postfix(VisEquipment __instance, float deltaTime, float time)
		{
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || RightItemField == null)
			{
				return;
			}
			Humanoid component = ((Component)__instance).GetComponent<Humanoid>();
			SharedData val = ((component != null) ? component.GetCurrentWeapon() : null)?.m_shared;
			if (val == null || !SpearUtils.IsSpearWithSwordAttack_Shared(val))
			{
				return;
			}
			object? value = RightItemField.GetValue(__instance);
			GameObject val2 = (GameObject)((value is GameObject) ? value : null);
			if (!Object.op_Implicit((Object)(object)val2))
			{
				return;
			}
			Transform val3 = FindBestTargetTransform(val2);
			if ((Object)(object)val3 == (Object)null)
			{
				return;
			}
			bool isFangSpear = SpearUtils.IsFangSpear_Shared(val);
			bool flag = false;
			Player val4 = (Player)(object)((component is Player) ? component : null);
			if ((Object)(object)val4 != (Object)null && ((Character)val4).InAttack())
			{
				object? value2 = AccessTools.Field(typeof(Humanoid), "m_currentAttack").GetValue(component);
				Attack val5 = (Attack)((value2 is Attack) ? value2 : null);
				if (val5 != null && string.Equals(val5.m_attackAnimation, "spear_throw", StringComparison.OrdinalIgnoreCase))
				{
					flag = true;
				}
			}
			SpearRotatedMarker spearRotatedMarker = ((Component)val3).GetComponent<SpearRotatedMarker>();
			if ((Object)(object)spearRotatedMarker == (Object)null)
			{
				spearRotatedMarker = ((Component)val3).gameObject.AddComponent<SpearRotatedMarker>();
				spearRotatedMarker.origLocalPos = val3.localPosition;
				spearRotatedMarker.origLocalRot = val3.localRotation;
				spearRotatedMarker.rotationApplied = false;
				spearRotatedMarker.initialized = true;
			}
			SpearPositioner.ApplyState(((Component)val3).gameObject, spearRotatedMarker, !flag, isFangSpear);
		}

		private static Transform FindBestTargetTransform(GameObject inst)
		{
			Transform val = FindDeepChild_Internal(inst.transform, "attach", exact: true);
			if (Object.op_Implicit((Object)(object)val))
			{
				return val;
			}
			Transform val2 = FindDeepChild_Internal(inst.transform, "attach", exact: false);
			if (Object.op_Implicit((Object)(object)val2))
			{
				return val2;
			}
			MeshRenderer componentInChildren = inst.GetComponentInChildren<MeshRenderer>(true);
			if (Object.op_Implicit((Object)(object)componentInChildren))
			{
				return ((Component)componentInChildren).transform;
			}
			return inst.transform;
		}

		private static Transform FindDeepChild_Internal(Transform root, string name, bool exact)
		{
			Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
			foreach (Transform val in componentsInChildren)
			{
				if (exact)
				{
					if (string.Equals(((Object)val).name, name, StringComparison.OrdinalIgnoreCase))
					{
						return val;
					}
				}
				else if (((Object)val).name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return val;
				}
			}
			return null;
		}
	}
	[BepInPlugin("com.havocmods.realisticspears", "Realistic Spears", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static Harmony HarmonyInstance;

		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			HarmonyInstance = new Harmony("com.havocmods.realisticspears");
			HarmonyInstance.PatchAll();
			AnimationSpeedManager.Add(delegate(Character character, double speed)
			{
				Player val = (Player)(object)((character is Player) ? character : null);
				if ((Object)(object)val == (Object)null || !((Character)val).InAttack())
				{
					return speed;
				}
				object? value = AccessTools.Field(typeof(Humanoid), "m_currentAttack").GetValue(val);
				Attack val2 = (Attack)((value is Attack) ? value : null);
				SharedData val3 = ((Humanoid)val).GetCurrentWeapon()?.m_shared;
				if (val2 == null || val3 == null)
				{
					return speed;
				}
				return (SpearUtils.IsSpearWithSwordAttack_Shared(val3) && string.Equals(val2.m_attackAnimation, "sword_secondary", StringComparison.OrdinalIgnoreCase)) ? (speed * 2.200000047683716) : speed;
			});
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[RealisticSpears] Loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmonyInstance = HarmonyInstance;
			if (harmonyInstance != null)
			{
				harmonyInstance.UnpatchSelf();
			}
		}
	}
	internal sealed class SpearRotatedMarker : MonoBehaviour
	{
		public Vector3 origLocalPos;

		public Quaternion origLocalRot;

		public bool rotationApplied;

		public bool initialized;

		public bool throwFlipped;
	}
	internal static class SpearPositioner
	{
		internal static void FixSpearRotationAndPosition(GameObject go, bool isRevert, bool isFangSpear)
		{
			if (Object.op_Implicit((Object)(object)go))
			{
				if (!isRevert)
				{
					FixSpearRotation(go);
					FixSpearPositionOffset(go, isRevert, isFangSpear);
				}
				else
				{
					FixSpearPositionOffset(go, isRevert, isFangSpear);
					FixSpearRotation(go);
				}
			}
		}

		internal static void FixSpearRotation(GameObject go)
		{
			go.transform.Rotate(180f, 25f, 0f, (Space)1);
		}

		internal static void FixSpearPositionOffset(GameObject go, bool isRevert, bool isFangSpear)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = (isRevert ? Vector3.back : Vector3.forward);
			float num = (isFangSpear ? (-0.72f) : (-0.5f));
			go.transform.Translate(val * num, (Space)1);
		}

		internal static void ApplyState(GameObject go, SpearRotatedMarker marker, bool applyRotation, bool isFangSpear, bool extraFlip = false)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: 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)
			if (!((Object)(object)go == (Object)null) && !((Object)(object)marker == (Object)null) && marker.initialized)
			{
				float num = (isFangSpear ? (-0.72f) : (-0.5f));
				Quaternion val = ((!applyRotation) ? marker.origLocalRot : (marker.origLocalRot * Quaternion.Euler(180f, 25f, 0f)));
				Quaternion val2 = (extraFlip ? (val * Quaternion.Euler(180f, 0f, 0f)) : val);
				go.transform.localRotation = val2;
				go.transform.localPosition = marker.origLocalPos + val2 * Vector3.forward * num;
				marker.rotationApplied = applyRotation;
				marker.throwFlipped = extraFlip;
			}
		}
	}
	internal static class SpearUtils
	{
		internal static bool IsSpearWithPokeAttack_Shared(SharedData shared)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			if (shared != null && (int)shared.m_skillType == 5 && shared.m_attack != null)
			{
				return shared.m_attack.m_attackAnimation == "spear_poke";
			}
			return false;
		}

		internal static bool IsSpearWithSwordAttack_Shared(SharedData shared)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			if (shared != null && (int)shared.m_skillType == 5 && shared.m_attack != null)
			{
				return shared.m_attack.m_attackAnimation == "sword_secondary";
			}
			return false;
		}

		internal static bool IsFangSpear_Shared(SharedData shared)
		{
			if (shared != null && !string.IsNullOrEmpty(shared.m_name))
			{
				return shared.m_name == "$item_spear_wolffang";
			}
			return false;
		}
	}
}
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;
		}
	}
}
internal static class AnimationSpeedManager
{
	public delegate double Handler(Character character, double speed);

	private static readonly Harmony harmony = new Harmony("AnimationSpeedManager");

	private static bool hasMarkerPatch = false;

	private static readonly MethodInfo method = AccessTools.DeclaredMethod(typeof(CharacterAnimEvent), "CustomFixedUpdate", (Type[])null, (Type[])null);

	private static int index = 0;

	private static bool changed = false;

	private static Handler[][] handlers = Array.Empty<Handler[]>();

	private static readonly Dictionary<int, List<Handler>> handlersPriorities = new Dictionary<int, List<Handler>>();

	[PublicAPI]
	public static void Add(Handler handler, int priority = 400)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Expected O, but got Unknown
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		if (!hasMarkerPatch)
		{
			harmony.Patch((MethodBase)method, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(AccessTools.DeclaredMethod(typeof(AnimationSpeedManager), "markerPatch", (Type[])null, (Type[])null)), (HarmonyMethod)null);
			hasMarkerPatch = true;
		}
		if (!handlersPriorities.TryGetValue(priority, out List<Handler> value))
		{
			handlersPriorities.Add(priority, value = new List<Handler>());
			harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(AccessTools.DeclaredMethod(typeof(AnimationSpeedManager), "wrapper", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		value.Add(handler);
		handlers = (from kv in handlersPriorities
			orderby kv.Key
			select kv.Value.ToArray()).ToArray();
	}

	private static void wrapper(Character ___m_character, Animator ___m_animator)
	{
		Character ___m_character2 = ___m_character;
		double num = (double)___m_animator.speed * 10000000.0 % 100.0;
		if ((!(num > 10.0) || !(num < 30.0)) && !(___m_animator.speed <= 0.001f))
		{
			double num2 = ___m_animator.speed;
			double num3 = handlers[index++].Aggregate(num2, (double current, Handler handler) => handler(___m_character2, current));
			if (num3 != num2)
			{
				___m_animator.speed = (float)(num3 - num3 % 1E-05);
				changed = true;
			}
		}
	}

	private static void markerPatch(Animator ___m_animator)
	{
		if (changed)
		{
			double num = (double)___m_animator.speed * 10000000.0 % 100.0;
			if ((num < 10.0 || num > 30.0) ? true : false)
			{
				___m_animator.speed += 1.9E-06f;
			}
			changed = false;
		}
		index = 0;
	}
}