Decompiled source of WinterMantisCameraHeightFix v1.0.0

BepInEx/plugins/WinterMantisCameraHeightFix.dll

Decompiled an hour ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ModelReplacement;
using ModelReplacement.AvatarBodyUpdater;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("WinterMantisCameraHeightFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WinterMantisCameraHeightFix")]
[assembly: AssemblyTitle("WinterMantisCameraHeightFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 WinterMantisCameraHeightFix
{
	[BepInPlugin("Loona.WinterMantisCameraHeightFix", "WinterMantisCameraHeightFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "Loona.WinterMantisCameraHeightFix";

		public const string PluginName = "WinterMantisCameraHeightFix";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private static Harmony harmony;

		private static bool patchesApplied;

		public static ConfigEntry<bool> Enabled;

		private static readonly string[] TargetNames = new string[4] { "SYLVUNNY", "ESPUNNY", "FLAREPUNNY", "UMBREEPUNNY" };

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableFix", true, "Enable or disable the vanilla camera height and floor alignment fix for WinterMantis models.");
			harmony = new Harmony("Loona.WinterMantisCameraHeightFix");
			MethodInfo methodInfo = AccessTools.Method(typeof(BodyReplacementBase), "Start", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "OnBodyReplacementStartPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo2 = AccessTools.Method(typeof(BodyReplacementBase), "GetAvatarUpdater", (Type[])null, (Type[])null);
			if (methodInfo2 != null)
			{
				harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "GetAvatarUpdaterPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Log.LogInfo((object)"WinterMantisCameraHeightFix v1.0.0 by Loona loaded!");
		}

		private void Start()
		{
			EnsureTargetPatches();
		}

		private static void OnBodyReplacementStartPrefix()
		{
			EnsureTargetPatches();
		}

		private static bool MatchesTargetName(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return false;
			}
			string[] targetNames = TargetNames;
			foreach (string value in targetNames)
			{
				if (text.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return true;
				}
			}
			return false;
		}

		private static void EnsureTargetPatches()
		{
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			if (patchesApplied)
			{
				return;
			}
			patchesApplied = true;
			int num = 0;
			MethodInfo methodInfo = AccessTools.Method(typeof(Plugin), "DetectCameraPrefix", (Type[])null, (Type[])null);
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				Type[] types;
				try
				{
					types = assembly.GetTypes();
				}
				catch (ReflectionTypeLoadException ex)
				{
					types = ex.Types;
				}
				catch
				{
					continue;
				}
				if (types == null)
				{
					continue;
				}
				bool flag = MatchesTargetName(assembly.GetName().Name);
				Type[] array;
				if (!flag)
				{
					array = types;
					foreach (Type type in array)
					{
						if (type != null && typeof(BodyReplacementBase).IsAssignableFrom(type) && MatchesTargetName(type.Name))
						{
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					continue;
				}
				array = types;
				foreach (Type type2 in array)
				{
					if (type2 != null && type2.FullName == "CamVisuals.CameraVisuals")
					{
						MethodInfo methodInfo2 = AccessTools.Method(type2, "DetectCamera", (Type[])null, (Type[])null);
						if (methodInfo2 != null)
						{
							harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
							num++;
						}
					}
				}
			}
			Log.LogInfo((object)$"Patches applied: {num} CameraVisuals (WinterMantisCameraHeightFix).");
		}

		private static bool DetectCameraPrefix()
		{
			return !Enabled.Value;
		}

		private static void GetAvatarUpdaterPostfix(BodyReplacementBase __instance, ref AvatarUpdater __result)
		{
			if (Enabled.Value && (Object)(object)__instance != (Object)null && MatchesTargetName(((object)__instance).GetType().Name))
			{
				__result = (AvatarUpdater)(object)new FloorAlignedAvatarUpdater();
			}
		}
	}
	public sealed class FloorAlignedAvatarUpdater : AvatarUpdater
	{
		private bool canAlignBones;

		private bool useToes;

		private Transform SafeAvatarBone(string name)
		{
			try
			{
				return ((AvatarUpdater)this).GetAvatarTransformFromBoneName(name);
			}
			catch
			{
				return null;
			}
		}

		private Transform SafePlayerBone(string name)
		{
			try
			{
				return ((AvatarUpdater)this).GetPlayerTransformFromBoneName(name);
			}
			catch
			{
				return null;
			}
		}

		public override void AssignModelReplacement(GameObject player, GameObject replacement)
		{
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			((AvatarUpdater)this).AssignModelReplacement(player, replacement);
			Transform val = SafeAvatarBone("spine");
			bool flag = (Object)(object)SafeAvatarBone("toe.L") != (Object)null && (Object)(object)SafeAvatarBone("toe.R") != (Object)null && (Object)(object)SafePlayerBone("toe.L") != (Object)null && (Object)(object)SafePlayerBone("toe.R") != (Object)null;
			bool flag2 = (Object)(object)SafeAvatarBone("foot.L") != (Object)null && (Object)(object)SafeAvatarBone("foot.R") != (Object)null && (Object)(object)SafePlayerBone("foot.L") != (Object)null && (Object)(object)SafePlayerBone("foot.R") != (Object)null;
			useToes = flag;
			canAlignBones = (Object)(object)val != (Object)null && (flag || flag2);
			base.rootPositionOffset = (Vector3)(canAlignBones ? Vector3.zero : new Vector3(0f, 0.35f, 0f));
		}

		protected override void UpdateModel()
		{
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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_0109: 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)
			((AvatarUpdater)this).UpdateModel();
			if (canAlignBones)
			{
				Transform val = SafeAvatarBone("spine");
				Transform val2 = SafeAvatarBone(useToes ? "toe.L" : "foot.L");
				Transform val3 = SafeAvatarBone(useToes ? "toe.R" : "foot.R");
				Transform val4 = SafePlayerBone(useToes ? "toe.L" : "foot.L");
				Transform val5 = SafePlayerBone(useToes ? "toe.R" : "foot.R");
				if ((Object)(object)val != (Object)null && (Object)(object)val2 != (Object)null && (Object)(object)val3 != (Object)null && (Object)(object)val4 != (Object)null && (Object)(object)val5 != (Object)null)
				{
					float num = (val4.position.y + val5.position.y) * 0.5f;
					float num2 = (val2.position.y + val3.position.y) * 0.5f;
					val.position += Vector3.up * (num - num2);
				}
			}
		}
	}
}