Decompiled source of ClearAllPins v1.0.0

plugins/ClearAllPins.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

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

[BepInPlugin("OGTristonMcPherson.ClearAllPins", "ClearAllPins", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "OGTristonMcPherson.ClearAllPins";

	public const string Version = "1.0.0";

	private ConfigEntry<bool> keepDeathPins;

	private ConfigEntry<float> buttonX;

	private ConfigEntry<float> buttonY;

	private float confirmUntil;

	private string status = "";

	private float statusUntil;

	private static FieldInfo pinsField;

	private static FieldInfo modeField;

	private static MethodInfo removePin;

	private void Awake()
	{
		keepDeathPins = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepDeathPins", true, "Leave death (tombstone) markers alone when clearing.");
		buttonX = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "OffsetFromRight", 230f, "Pixels from the right edge of the screen.");
		buttonY = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "OffsetFromTop", 20f, "Pixels from the top edge of the screen.");
		pinsField = AccessTools.Field(typeof(Minimap), "m_pins");
		modeField = AccessTools.Field(typeof(Minimap), "m_mode");
		removePin = AccessTools.Method(typeof(Minimap), "RemovePin", new Type[1] { typeof(PinData) }, (Type[])null);
		if (pinsField == null || removePin == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Could not find Minimap.m_pins / RemovePin - the button will do nothing.");
		}
	}

	private bool LargeMapOpen()
	{
		Minimap instance = Minimap.instance;
		if ((Object)(object)instance == (Object)null || modeField == null)
		{
			return false;
		}
		object value = modeField.GetValue(instance);
		return value != null && value.ToString() == "Large";
	}

	private void OnGUI()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: 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)
		if (!LargeMapOpen())
		{
			return;
		}
		Rect val = default(Rect);
		((Rect)(ref val))..ctor((float)Screen.width - buttonX.Value, buttonY.Value, 200f, 32f);
		bool flag = Time.unscaledTime < confirmUntil;
		string text = ((!flag) ? "Clear all pins" : "Click again to confirm");
		Color backgroundColor = GUI.backgroundColor;
		if (flag)
		{
			GUI.backgroundColor = new Color(1f, 0.35f, 0.35f);
		}
		if (GUI.Button(val, text))
		{
			if (flag)
			{
				confirmUntil = 0f;
				int num = ClearPins();
				status = ((num < 0) ? "Failed - see BepInEx log" : ("Removed " + num + " pins"));
				statusUntil = Time.unscaledTime + 4f;
			}
			else
			{
				confirmUntil = Time.unscaledTime + 3f;
			}
		}
		GUI.backgroundColor = backgroundColor;
		if (Time.unscaledTime < statusUntil)
		{
			GUI.Label(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y + 36f, 300f, 24f), status);
		}
	}

	private int ClearPins()
	{
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Invalid comparison between Unknown and I4
		Minimap instance = Minimap.instance;
		if ((Object)(object)instance == (Object)null || pinsField == null || removePin == null)
		{
			return -1;
		}
		if (!(pinsField.GetValue(instance) is IList list))
		{
			return -1;
		}
		List<PinData> list2 = new List<PinData>();
		foreach (object item in list)
		{
			PinData val = (PinData)((item is PinData) ? item : null);
			if (val != null && (val.m_save || val.m_ownerID != 0) && (!keepDeathPins.Value || (int)val.m_type != 4))
			{
				list2.Add(val);
			}
		}
		int num = 0;
		foreach (PinData item2 in list2)
		{
			try
			{
				removePin.Invoke(instance, new object[1] { item2 });
				num++;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to remove pin '" + item2.m_name + "': " + ex.Message));
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Cleared " + num + " pins."));
		return num;
	}
}