Decompiled source of MinimapNorthUp v0.1.0

plugins/MycopunkMinimapNorthUp/MycopunkMinimapNorthUp.dll

Decompiled a week 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 BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MycopunkShared;
using Pigeon.Movement;
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("MycopunkMinimapNorthUp")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+dd4678d16211b3cb4c24457fa75d36322b82a547")]
[assembly: AssemblyProduct("MycopunkMinimapNorthUp")]
[assembly: AssemblyTitle("MycopunkMinimapNorthUp")]
[assembly: AssemblyVersion("0.1.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 MycopunkMinimapNorthUp
{
	internal static class MenuText
	{
		private static readonly Dictionary<string, Dictionary<string, string>> Names = new Dictionary<string, Dictionary<string, string>>
		{
			["en"] = new Dictionary<string, string> { ["minimap.north"] = "North-up minimap" },
			["ja"] = new Dictionary<string, string> { ["minimap.north"] = "ミニマップを北向きに固定" }
		};

		private static readonly Dictionary<string, Dictionary<string, string>> Descriptions = new Dictionary<string, Dictionary<string, string>> { ["ja"] = new Dictionary<string, string> { ["minimap.north"] = "ミニマップの北を上に固定し、マップの代わりにプレイヤーの矢印を回転させます。変更はすぐに反映されます。" } };

		public static object[] Label(string key)
		{
			return ModsMenuTag.Label(key, (IDictionary<string, Dictionary<string, string>>)Names, (IDictionary<string, Dictionary<string, string>>)Descriptions);
		}
	}
	internal static class MinimapNorthUp
	{
		private static readonly MethodInfo Euler = AccessTools.Method(typeof(Quaternion), "Euler", new Type[3]
		{
			typeof(float),
			typeof(float),
			typeof(float)
		}, (Type[])null);

		private static readonly MethodInfo Identity = AccessTools.PropertyGetter(typeof(Quaternion), "identity");

		private static readonly MethodInfo Lerp = AccessTools.Method(typeof(Quaternion), "LerpUnclamped", new Type[3]
		{
			typeof(Quaternion),
			typeof(Quaternion),
			typeof(float)
		}, (Type[])null);

		private static readonly MethodInfo SetLocalRotation = AccessTools.PropertySetter(typeof(Transform), "localRotation");

		private static readonly FieldInfo PlayerPointer = AccessTools.Field(typeof(Minimap), "playerPointer");

		internal static bool Active;

		private static bool On
		{
			get
			{
				if (Plugin.NorthUp != null)
				{
					return Plugin.NorthUp.Value;
				}
				return false;
			}
		}

		public static void Install(Harmony harmony)
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method(typeof(Minimap), "UpdateMinimap", (Type[])null, (Type[])null);
			if (methodInfo == null || PlayerPointer == null || Euler == null || Identity == null || Lerp == null || SetLocalRotation == null)
			{
				Plugin.Log.LogWarning((object)"Minimap.UpdateMinimap or its fields not found; north-up minimap is off.");
				return;
			}
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(MinimapNorthUp), "Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			if (Active)
			{
				Plugin.Log.LogInfo((object)"North-up minimap patch installed.");
			}
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			int num = list.FindIndex((CodeInstruction c) => CodeInstructionExtensions.Calls(c, Euler));
			if (num < 0)
			{
				return Fail(list, "map rotation");
			}
			int num2 = -1;
			int num3 = -1;
			for (int num4 = 0; num4 < list.Count - 3; num4++)
			{
				if (!CodeInstructionExtensions.LoadsField(list[num4], PlayerPointer, false))
				{
					continue;
				}
				if (num2 < 0 && CodeInstructionExtensions.Calls(list[num4 + 2], Identity) && CodeInstructionExtensions.Calls(list[num4 + 3], SetLocalRotation))
				{
					num2 = num4 + 2;
					continue;
				}
				int num5 = num4 + 2;
				while (num3 < 0 && num5 < list.Count - 1 && num5 < num4 + 16)
				{
					if (CodeInstructionExtensions.Calls(list[num5], Lerp) && CodeInstructionExtensions.Calls(list[num5 + 1], SetLocalRotation))
					{
						num3 = num5;
					}
					num5++;
				}
			}
			if (num2 < 0)
			{
				return Fail(list, "player arrow (corner map)");
			}
			if (num3 < 0)
			{
				return Fail(list, "player arrow (full-screen blend)");
			}
			list[num].operand = AccessTools.Method(typeof(MinimapNorthUp), "MapRotation", (Type[])null, (Type[])null);
			list[num3].operand = AccessTools.Method(typeof(MinimapNorthUp), "PointerBlend", (Type[])null, (Type[])null);
			list[num2].operand = AccessTools.Method(typeof(MinimapNorthUp), "PointerIdle", (Type[])null, (Type[])null);
			Active = true;
			return list;
		}

		private static IEnumerable<CodeInstruction> Fail(List<CodeInstruction> list, string what)
		{
			Plugin.Log.LogWarning((object)("North-up minimap: " + what + " not found in Minimap.UpdateMinimap (game update?); north-up is off."));
			return list;
		}

		private static Quaternion MapRotation(float x, float y, float z)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			if (!On)
			{
				return Quaternion.Euler(x, y, z);
			}
			return Quaternion.identity;
		}

		private static Quaternion PointerIdle()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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)
			if (!On)
			{
				return Quaternion.identity;
			}
			return Quaternion.Euler(0f, 0f, 0f - ((Quaternion)(ref PlayerLook.Rotation)).eulerAngles.y);
		}

		private static Quaternion PointerBlend(Quaternion a, Quaternion b, float t)
		{
			//IL_0010: 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_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			if (!On)
			{
				return Quaternion.LerpUnclamped(a, b, t);
			}
			return b;
		}
	}
	[BepInPlugin("t4t5u0.mycopunk.minimapnorthup", "Mycopunk Minimap North Up", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "t4t5u0.mycopunk.minimapnorthup";

		public const string Name = "Mycopunk Minimap North Up";

		public const string Version = "0.1.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> NorthUp;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			NorthUp = ((BaseUnityPlugin)this).Config.Bind<bool>("Minimap", "NorthUp", true, new ConfigDescription("Keep north at the top of the corner minimap and turn the player arrow instead of the map (the way the full-screen map already works). Takes effect at once.", (AcceptableValueBase)null, MenuText.Label("minimap.north")));
			Harmony harmony = new Harmony("t4t5u0.mycopunk.minimapnorthup");
			Try("north-up minimap", delegate
			{
				MinimapNorthUp.Install(harmony);
			});
			Try("MODS tab", delegate
			{
				ModsMenu.Install(harmony, (BaseUnityPlugin)(object)this);
			});
			Try("hotkey report", delegate
			{
				Hotkeys.ReportConflicts((BaseUnityPlugin)(object)this);
			});
			Log.LogInfo((object)("Mycopunk Minimap North Up 0.1.0 loaded (north-up=" + (MinimapNorthUp.Active ? NorthUp.Value.ToString() : "N/A") + ")."));
		}

		private static void Try(string what, Action action)
		{
			try
			{
				action();
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Could not set up " + what + "; it is off: " + ex));
			}
		}
	}
}