Decompiled source of MarkCleared v1.0.0

MarkCleared.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("MarkCleared")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MarkCleared")]
[assembly: AssemblyTitle("MarkCleared")]
[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 MarkCleared
{
	[BepInPlugin("com.dreich.markcleared", "Mark Cleared", "1.0.0")]
	public class MarkClearedPlugin : BaseUnityPlugin
	{
		private const float DefaultEntranceRange = 8f;

		private const float MinEntranceRange = 1f;

		private const float MaxEntranceRange = 30f;

		private const float DefaultPinRange = 6f;

		private const float MinPinRange = 5f;

		private const float MaxPinRange = 150f;

		private const string DefaultRoofCrosses = "$piece_woodwallrooftop,$piece_woodwallrooftop45,$piece_woodwallrooftop67,$piece_ashwoodcross26,$piece_ashwoodcross45,$piece_ashwoodwallrooftop67,$piece_stavecross26,$piece_stavecross45,$piece_stavewallrooftop67";

		public const string PluginGuid = "com.dreich.markcleared";

		public const string PluginName = "Mark Cleared";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<string> RoofCrosses;

		internal static ConfigEntry<float> EntranceRange;

		internal static ConfigEntry<float> PinRange;

		internal static ConfigEntry<bool> ShowMessage;

		internal static ConfigEntry<bool> FoundCrosses;

		private void Awake()
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Placing a roof cross in a dungeon or cave doorway crosses off the map pins there.");
			RoofCrosses = ((BaseUnityPlugin)this).Config.Bind<string>("General", "RoofCrossPieces", "$piece_woodwallrooftop,$piece_woodwallrooftop45,$piece_woodwallrooftop67,$piece_ashwoodcross26,$piece_ashwoodcross45,$piece_ashwoodwallrooftop67,$piece_stavecross26,$piece_stavecross45,$piece_stavewallrooftop67", "Comma-separated pieces that mark a dungeon or cave as cleared. Use the piece's name key, like $piece_woodwallrooftop, or its prefab name, like wood_wall_roof_top. Defaults are every Wood, Ashwood and Timber Roof Cross.");
			EntranceRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "EntranceRange", 8f, new ConfigDescription("The roof cross has to be placed within this many meters of a dungeon or cave entrance.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 30f), Array.Empty<object>()));
			PinRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PinRange", 6f, new ConfigDescription("Map pins within this many meters of the entrance, or of the middle of the dungeon or cave, are crossed off.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 150f), Array.Empty<object>()));
			ShowMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowMessage", true, "Show a message in the top left corner when pins are crossed off.");
			FoundCrosses = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FoundCrosses", true, "Coming across a roof cross someone else placed in a dungeon or cave doorway crosses off the map pins there too. Off: only the roof crosses you built count.");
			new Harmony("com.dreich.markcleared").PatchAll();
		}

		private void Update()
		{
			FoundCross.Tick();
		}
	}
	internal static class RoofCross
	{
		private static string parsedFrom;

		private static readonly HashSet<string> names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		internal static bool Is(Piece piece)
		{
			if ((Object)(object)piece == (Object)null)
			{
				return false;
			}
			string value = MarkClearedPlugin.RoofCrosses.Value;
			if (value != parsedFrom)
			{
				names.Clear();
				string[] array = value.Split(new char[1] { ',' });
				foreach (string text in array)
				{
					if (text.Trim().Length > 0)
					{
						names.Add(text.Trim());
					}
				}
				parsedFrom = value;
			}
			if (!names.Contains(piece.m_name))
			{
				return names.Contains(Utils.GetPrefabName(((Component)piece).gameObject));
			}
			return true;
		}
	}
	internal static class Entrance
	{
		private const float InteriorHeight = 4000f;

		internal static Teleport Near(Vector3 position)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: 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_0049: Unknown result type (might be due to invalid IL or missing references)
			Teleport result = null;
			float num = MarkClearedPlugin.EntranceRange.Value;
			Teleport[] array = Object.FindObjectsByType<Teleport>((FindObjectsSortMode)0);
			foreach (Teleport val in array)
			{
				Vector3 position2 = ((Component)val).transform.position;
				if (!((Object)(object)val.m_targetPoint == (Object)null) && !(position2.y > 4000f))
				{
					float num2 = Vector3.Distance(position, position2);
					if (!(num2 > num))
					{
						result = val;
						num = num2;
					}
				}
			}
			return result;
		}
	}
	internal static class Pins
	{
		private static readonly FieldRef<Minimap, List<PinData>> AllPins = AccessTools.FieldRefAccess<Minimap, List<PinData>>("m_pins");

		internal static int CrossOffAround(Vector3 entrance)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return MarkAround(entrance, crossOff: true);
		}

		internal static int UncrossAround(Vector3 entrance)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return MarkAround(entrance, crossOff: false);
		}

		private static int MarkAround(Vector3 entrance, bool crossOff)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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_0079: Invalid comparison between Unknown and I4
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: 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)
			if ((Object)(object)Minimap.instance == (Object)null)
			{
				return 0;
			}
			Location location = Location.GetLocation(entrance, false);
			Vector3 val = (((Object)(object)location != (Object)null) ? ((Component)location).transform.position : entrance);
			float value = MarkClearedPlugin.PinRange.Value;
			int num = 0;
			foreach (PinData item in AllPins.Invoke(Minimap.instance))
			{
				if (item.m_save && item.m_checked != crossOff && (int)item.m_type != 4 && (!(Utils.DistanceXZ(item.m_pos, entrance) > value) || !(Utils.DistanceXZ(item.m_pos, val) > value)))
				{
					item.m_checked = crossOff;
					num++;
				}
			}
			return num;
		}
	}
	internal static class Report
	{
		internal static void Crossed(int crossed)
		{
			Player localPlayer = Player.m_localPlayer;
			if (crossed > 0 && !((Object)(object)localPlayer == (Object)null) && MarkClearedPlugin.ShowMessage.Value)
			{
				((Character)localPlayer).Message((MessageType)1, (crossed == 1) ? "Crossed off 1 map pin" : $"Crossed off {crossed} map pins", 0, (Sprite)null, false);
			}
		}

		internal static void Uncrossed(int uncrossed)
		{
			Player localPlayer = Player.m_localPlayer;
			if (uncrossed > 0 && !((Object)(object)localPlayer == (Object)null) && MarkClearedPlugin.ShowMessage.Value)
			{
				((Character)localPlayer).Message((MessageType)1, (uncrossed == 1) ? "Uncrossed 1 map pin" : $"Uncrossed {uncrossed} map pins", 0, (Sprite)null, false);
			}
		}
	}
	internal static class FoundCross
	{
		private const float CheckSeconds = 2f;

		private const float SettleSeconds = 5f;

		private const float GiveUpSeconds = 30f;

		private static readonly List<(Piece piece, float seenAt)> waiting = new List<(Piece, float)>();

		private static float checkedAt;

		private static float playerSince = float.MaxValue;

		internal static void Seen(Piece piece)
		{
			if (MarkClearedPlugin.Enabled.Value && RoofCross.Is(piece))
			{
				waiting.Add((piece, Time.time));
			}
		}

		internal static void Tick()
		{
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Player.m_localPlayer == (Object)null || (Object)(object)Minimap.instance == (Object)null)
			{
				playerSince = float.MaxValue;
				return;
			}
			if (playerSince == float.MaxValue)
			{
				playerSince = Time.time;
			}
			if (waiting.Count == 0 || Time.time - checkedAt < 2f || Time.time - playerSince < 5f)
			{
				return;
			}
			checkedAt = Time.time;
			int num = 0;
			for (int num2 = waiting.Count - 1; num2 >= 0; num2--)
			{
				var (val, num3) = waiting[num2];
				if (!Counts(val))
				{
					waiting.RemoveAt(num2);
				}
				else
				{
					Teleport val2 = Entrance.Near(((Component)val).transform.position);
					if ((Object)(object)val2 != (Object)null)
					{
						num += Pins.CrossOffAround(((Component)val2).transform.position);
					}
					if ((Object)(object)val2 != (Object)null || Time.time - Mathf.Max(num3, playerSince) > 30f)
					{
						waiting.RemoveAt(num2);
					}
				}
			}
			Report.Crossed(num);
		}

		private static bool Counts(Piece piece)
		{
			if ((Object)(object)piece == (Object)null)
			{
				return false;
			}
			ZNetView component = ((Component)piece).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return false;
			}
			if (!MarkClearedPlugin.FoundCrosses.Value)
			{
				return piece.IsCreator();
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Piece), "Awake")]
	internal static class PieceAwakePatch
	{
		private static void Postfix(Piece __instance)
		{
			FoundCross.Seen(__instance);
		}
	}
	internal static class BrokenCross
	{
		private static readonly List<Piece> nearby = new List<Piece>();

		internal static void Gone(ZDO zdo)
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			if (zdo == null || !MarkClearedPlugin.Enabled.Value || (Object)(object)ZNetScene.instance == (Object)null || (Object)(object)Minimap.instance == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null)
			{
				return;
			}
			GameObject prefab = ZNetScene.instance.GetPrefab(zdo.GetPrefab());
			if (!((Object)(object)prefab == (Object)null) && RoofCross.Is(prefab.GetComponent<Piece>()) && Counts(zdo))
			{
				Teleport val = Entrance.Near(zdo.GetPosition());
				if (!((Object)(object)val == (Object)null) && !StillMarked(((Component)val).transform.position, zdo))
				{
					Report.Uncrossed(Pins.UncrossAround(((Component)val).transform.position));
				}
			}
		}

		private static bool Counts(ZDO zdo)
		{
			if (MarkClearedPlugin.FoundCrosses.Value)
			{
				return true;
			}
			if ((Object)(object)Game.instance != (Object)null)
			{
				return zdo.GetLong(ZDOVars.s_creator, 0L) == Game.instance.GetPlayerProfile().GetPlayerID();
			}
			return false;
		}

		private static bool StillMarked(Vector3 entrance, ZDO gone)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			nearby.Clear();
			Piece.GetAllPiecesInRadius(entrance, MarkClearedPlugin.EntranceRange.Value, nearby);
			foreach (Piece item in nearby)
			{
				if (RoofCross.Is(item))
				{
					ZNetView component = ((Component)item).GetComponent<ZNetView>();
					if ((Object)(object)component != (Object)null && component.IsValid() && component.GetZDO() != gone)
					{
						return true;
					}
				}
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(ZDOMan), "HandleDestroyedZDO")]
	internal static class DestroyedZdoPatch
	{
		private static void Prefix(ZDOMan __instance, object[] __args)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (__args.Length != 0 && __args[0] is ZDOID val)
			{
				BrokenCross.Gone(__instance.GetZDO(val));
			}
		}
	}
}