Decompiled source of MinimapTuner v1.0.0

MinimapTuner.dll

Decompiled 3 weeks ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace MinimapTuner;

[BepInPlugin("dark.minimaptuner", "Minimap Tuner", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string ModGuid = "dark.minimaptuner";

	public const string ModName = "Minimap Tuner";

	public const string ModVersion = "1.0.0";

	internal const float VanillaMinZoom = 0.01f;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<float> Scale;

	internal static ConfigEntry<float> ScaleStep;

	internal static ConfigEntry<KeyboardShortcut> ScaleUpKey;

	internal static ConfigEntry<KeyboardShortcut> ScaleDownKey;

	internal static ConfigEntry<bool> MoveStatusEffects;

	internal static ConfigEntry<float> CurrentZoom;

	internal static ConfigEntry<float> MinZoom;

	internal static ConfigEntry<float> MaxZoom;

	internal static ConfigEntry<float> ZoomStep;

	internal static ConfigEntry<KeyboardShortcut> ZoomInKey;

	internal static ConfigEntry<KeyboardShortcut> ZoomOutKey;

	internal static ConfigEntry<bool> ShowMessages;

	internal static ManualLogSource Log;

	private Harmony _harmony;

	private bool _hotkeysBroken;

	private static Minimap _sizeAppliedTo;

	private static RectTransform _root;

	private static Vector2 _origPivot;

	private static Vector2 _origPos;

	private static Vector3 _origScale;

	private static RectTransform _statusRoot;

	private static Vector2 _statusOrigPos;

	private static bool _statusChecked;

	private static Minimap _zoomAppliedTo;

	private static float _origMinZoom = 0.01f;

	private static float _lastSeenZoom = -1f;

	private static float _lastZoomChange;

	private static bool _savingZoom;

	private void Awake()
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Expected O, but got Unknown
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Expected O, but got Unknown
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Expected O, but got Unknown
		//IL_01be: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Expected O, but got Unknown
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0205: Expected O, but got Unknown
		//IL_0238: Unknown result type (might be due to invalid IL or missing references)
		//IL_0242: Expected O, but got Unknown
		//IL_0257: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0358: Unknown result type (might be due to invalid IL or missing references)
		//IL_0362: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable the mod. When disabled, the minimap looks and behaves like in the vanilla game");
		ShowMessages = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowMessages", true, "Show a message in the top left corner when the minimap size is changed with hotkeys");
		Scale = ((BaseUnityPlugin)this).Config.Bind<float>("Size", "Scale", 1f, new ConfigDescription("Minimap size. 1 = vanilla, 1.5 = one and a half times bigger", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 3f), Array.Empty<object>()));
		ScaleStep = ((BaseUnityPlugin)this).Config.Bind<float>("Size", "ScaleStep", 0.05f, new ConfigDescription("How much the size changes per hotkey press", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 0.5f), Array.Empty<object>()));
		ScaleUpKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Size", "ScaleUpKey", new KeyboardShortcut((KeyCode)273, (KeyCode[])(object)new KeyCode[1] { (KeyCode)308 }), "Hotkey to make the minimap bigger");
		ScaleDownKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Size", "ScaleDownKey", new KeyboardShortcut((KeyCode)274, (KeyCode[])(object)new KeyCode[1] { (KeyCode)308 }), "Hotkey to make the minimap smaller");
		MoveStatusEffects = ((BaseUnityPlugin)this).Config.Bind<bool>("Size", "MoveStatusEffects", true, "Move the status effect icons to the left together with the minimap edge, so a bigger minimap does not cover them");
		CurrentZoom = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "CurrentZoom", 0.01f, new ConfigDescription("Current minimap zoom: the part of the world map shown on the minimap. Smaller = closer. Changes when you zoom in game and is remembered between sessions. Vanilla default: 0.01", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.001f, 1f), Array.Empty<object>()));
		MinZoom = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "MinZoom", 0.0025f, new ConfigDescription("Closest allowed zoom. Vanilla: 0.01. Smaller values let you zoom in further", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.001f, 0.05f), Array.Empty<object>()));
		MaxZoom = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "MaxZoom", 1f, new ConfigDescription("Farthest allowed zoom. Vanilla: 1 (the whole world)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 1f), Array.Empty<object>()));
		ZoomStep = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "ZoomStep", 0.25f, new ConfigDescription("Zoom change per key press. 0.25 = 25%. Vanilla: 0.5", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 1f), Array.Empty<object>()));
		ZoomInKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Zoom", "ZoomInKey", KeyboardShortcut.Empty, "Extra hotkey to zoom the minimap in. The vanilla keys (, and .) keep working");
		ZoomOutKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Zoom", "ZoomOutKey", KeyboardShortcut.Empty, "Extra hotkey to zoom the minimap out. The vanilla keys (, and .) keep working");
		Enabled.SettingChanged += delegate
		{
			ApplySize();
			ApplyZoomLimits(restoreZoom: true);
		};
		MinZoom.SettingChanged += delegate
		{
			ApplyZoomLimits(restoreZoom: false);
		};
		CurrentZoom.SettingChanged += delegate
		{
			if (!_savingZoom)
			{
				ApplyZoomLimits(restoreZoom: true);
			}
		};
		Scale.SettingChanged += delegate
		{
			ApplySize();
		};
		MoveStatusEffects.SettingChanged += delegate
		{
			ApplySize();
		};
		_harmony = new Harmony("dark.minimaptuner");
		TryPatch(typeof(UpdateMapPatch));
		TryPatch(typeof(SmallZoomSetterPatch));
		TryPatch(typeof(LargeZoomSetterPatch));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Minimap Tuner 1.0.0 loaded. Scale: " + Scale.Value + ", zoom: " + CurrentZoom.Value));
	}

	private void TryPatch(Type patchClass)
	{
		try
		{
			_harmony.CreateClassProcessor(patchClass).Patch();
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Patch " + patchClass.Name + " failed, this part of the mod will not work: " + ex.Message));
		}
	}

	private void Update()
	{
		Minimap instance = Minimap.instance;
		if (!((Object)(object)instance == (Object)null))
		{
			if ((Object)(object)_sizeAppliedTo != (Object)(object)instance || (!_statusChecked && (Object)(object)Hud.instance != (Object)null))
			{
				ApplySize();
			}
			if ((Object)(object)_zoomAppliedTo != (Object)(object)instance)
			{
				ApplyZoomLimits(restoreZoom: true);
			}
			HandleSizeHotkeys();
		}
	}

	private void OnDestroy()
	{
		RestoreSize();
		if ((Object)(object)_zoomAppliedTo != (Object)null)
		{
			_zoomAppliedTo.m_minZoom = _origMinZoom;
		}
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}

	private void HandleSizeHotkeys()
	{
		//IL_0032: 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)
		if (_hotkeysBroken || !Enabled.Value || (Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		try
		{
			int num = 0;
			if (IsDown(ScaleUpKey.Value))
			{
				num = 1;
			}
			else if (IsDown(ScaleDownKey.Value))
			{
				num = -1;
			}
			if (num == 0)
			{
				HandleZoomHotkeys(Minimap.instance);
			}
			else if (!IsTyping())
			{
				AcceptableValueRange<float> val = (AcceptableValueRange<float>)(object)((ConfigEntryBase)Scale).Description.AcceptableValues;
				float num2 = Mathf.Clamp(Scale.Value + (float)num * ScaleStep.Value, val.MinValue, val.MaxValue);
				Scale.Value = Mathf.Round(num2 * 100f) / 100f;
				if (ShowMessages.Value)
				{
					((Character)Player.m_localPlayer).Message((MessageType)1, "Minimap size: " + Mathf.RoundToInt(Scale.Value * 100f) + "%", 0, (Sprite)null, false);
				}
			}
		}
		catch (Exception ex)
		{
			_hotkeysBroken = true;
			((BaseUnityPlugin)this).Logger.LogError((object)("Size hotkeys disabled due to an error: " + ex));
		}
	}

	internal static void ApplySize()
	{
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: 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_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_017e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_024c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0251: Unknown result type (might be due to invalid IL or missing references)
		//IL_0285: Unknown result type (might be due to invalid IL or missing references)
		//IL_028a: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.m_smallRoot == (Object)null)
			{
				return;
			}
			if ((Object)(object)_sizeAppliedTo != (Object)(object)instance)
			{
				Transform transform = instance.m_smallRoot.transform;
				_root = (RectTransform)(object)((transform is RectTransform) ? transform : null);
				if ((Object)(object)_root == (Object)null)
				{
					return;
				}
				_origPivot = _root.pivot;
				_origPos = _root.anchoredPosition;
				_origScale = ((Transform)_root).localScale;
				_sizeAppliedTo = instance;
				_statusRoot = null;
				_statusChecked = false;
			}
			if (!_statusChecked && (Object)(object)Hud.instance != (Object)null)
			{
				_statusChecked = true;
				_statusRoot = Hud.instance.m_statusEffectListRoot;
				if ((Object)(object)_statusRoot != (Object)null)
				{
					_statusOrigPos = _statusRoot.anchoredPosition;
				}
			}
			_root.pivot = _origPivot;
			_root.anchoredPosition = _origPos;
			((Transform)_root).localScale = _origScale;
			if ((Object)(object)_statusRoot != (Object)null)
			{
				_statusRoot.anchoredPosition = _statusOrigPos;
			}
			float num = ((!Enabled.Value) ? 1f : Scale.Value);
			if (Mathf.Approximately(num, 1f))
			{
				return;
			}
			Rect rect = _root.rect;
			Vector2 val = Vector2.Scale(((Rect)(ref rect)).size, new Vector2(_origScale.x, _origScale.y));
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(1f, 1f);
			_root.pivot = val2;
			_root.anchoredPosition = _origPos + Vector2.Scale(val2 - _origPivot, val);
			((Transform)_root).localScale = _origScale * num;
			if ((Object)(object)_statusRoot != (Object)null && MoveStatusEffects.Value)
			{
				float num2 = val.x * (num - 1f);
				float num3 = ((!((Object)(object)((Transform)_root).parent != (Object)null)) ? 1f : ((Transform)_root).parent.lossyScale.x);
				float num4 = ((!((Object)(object)((Transform)_statusRoot).parent != (Object)null)) ? 1f : ((Transform)_statusRoot).parent.lossyScale.x);
				if (num4 > 0.0001f)
				{
					num2 *= num3 / num4;
				}
				_statusRoot.anchoredPosition = _statusOrigPos + new Vector2(0f - num2, 0f);
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Failed to apply minimap size: " + ex));
		}
	}

	private static void RestoreSize()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_root != (Object)null)
		{
			_root.pivot = _origPivot;
			_root.anchoredPosition = _origPos;
			((Transform)_root).localScale = _origScale;
		}
		if ((Object)(object)_statusRoot != (Object)null)
		{
			_statusRoot.anchoredPosition = _statusOrigPos;
		}
		_sizeAppliedTo = null;
	}

	internal static float ClampZoom(float zoom)
	{
		float num = Mathf.Min(MinZoom.Value, MaxZoom.Value);
		float num2 = Mathf.Max(MinZoom.Value, MaxZoom.Value);
		return Mathf.Clamp(zoom, num, num2);
	}

	private static void SetSmallZoomField(Minimap minimap, float zoom)
	{
		Traverse.Create((object)minimap).Field("m_smallZoom").SetValue((object)zoom);
	}

	internal static void ApplyZoomLimits(bool restoreZoom)
	{
		try
		{
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if ((Object)(object)_zoomAppliedTo != (Object)(object)instance)
			{
				_origMinZoom = instance.m_minZoom;
				_zoomAppliedTo = instance;
				restoreZoom = true;
			}
			if (!Enabled.Value)
			{
				instance.m_minZoom = _origMinZoom;
				return;
			}
			instance.m_minZoom = Mathf.Min(_origMinZoom, MinZoom.Value);
			if (restoreZoom)
			{
				SetSmallZoomField(instance, ClampZoom(CurrentZoom.Value));
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Failed to apply minimap zoom: " + ex));
		}
	}

	internal static void ObserveZoom(float zoom)
	{
		if (!Mathf.Approximately(zoom, _lastSeenZoom))
		{
			_lastSeenZoom = zoom;
			_lastZoomChange = Time.unscaledTime;
		}
		else if (!Mathf.Approximately(zoom, CurrentZoom.Value) && !(Time.unscaledTime - _lastZoomChange < 0.5f))
		{
			_savingZoom = true;
			try
			{
				CurrentZoom.Value = zoom;
			}
			finally
			{
				_savingZoom = false;
			}
		}
	}

	private void HandleZoomHotkeys(Minimap minimap)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Invalid comparison between Unknown and I4
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		if (Enabled.Value && !((Object)(object)Player.m_localPlayer == (Object)null) && (int)minimap.m_mode == 1)
		{
			int num = 0;
			if (IsDown(ZoomOutKey.Value))
			{
				num = 1;
			}
			else if (IsDown(ZoomInKey.Value))
			{
				num = -1;
			}
			if (num != 0 && !IsTyping())
			{
				minimap.SmallZoom *= ((num <= 0) ? 0.5f : 2f);
			}
		}
	}

	internal static bool IsDown(KeyboardShortcut shortcut)
	{
		//IL_0002: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: 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)
		if ((int)((KeyboardShortcut)(ref shortcut)).MainKey == 0)
		{
			return false;
		}
		if (!UnityInput.Current.GetKeyDown(((KeyboardShortcut)(ref shortcut)).MainKey))
		{
			return false;
		}
		foreach (KeyCode modifier in ((KeyboardShortcut)(ref shortcut)).Modifiers)
		{
			if (!UnityInput.Current.GetKey(modifier))
			{
				return false;
			}
		}
		return true;
	}

	private static bool IsTyping()
	{
		return Console.IsVisible() || ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus()) || TextInput.IsVisible() || Menu.IsVisible() || Minimap.InTextInput();
	}
}
[HarmonyPatch(typeof(Minimap), "UpdateMap")]
internal static class UpdateMapPatch
{
	private static void Postfix(float ___m_smallZoom)
	{
		if (Plugin.Enabled.Value)
		{
			Plugin.ObserveZoom(___m_smallZoom);
		}
	}
}
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
internal static class SmallZoomSetterPatch
{
	[HarmonyPriority(800)]
	private static void Prefix(float ___m_smallZoom, ref float value)
	{
		if (Plugin.Enabled.Value)
		{
			if (___m_smallZoom > 0f && !Mathf.Approximately(value, ___m_smallZoom))
			{
				float num = 1f + Plugin.ZoomStep.Value;
				value = ((!(value > ___m_smallZoom)) ? (___m_smallZoom / num) : (___m_smallZoom * num));
			}
			value = Plugin.ClampZoom(value);
		}
	}
}
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
internal static class LargeZoomSetterPatch
{
	[HarmonyPriority(800)]
	private static void Prefix(ref float value)
	{
		if (Plugin.Enabled.Value && value < 0.01f)
		{
			value = 0.01f;
		}
	}
}