Decompiled source of NoMapCartographyTable v1.0.1

NoMapCartographyTable.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using ConditionalConfigSync;
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("NoMapCartographyTable")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("NoMapCartographyTable")]
[assembly: AssemblyTitle("NoMapCartographyTable")]
[assembly: AssemblyVersion("1.0.1.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 NoMapCartographytable
{
	[BepInPlugin("com.dreich.nomapcartographytable", "NoMapCartographytable", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class NoMapCartographytablePlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.dreich.nomapcartographytable";

		public const string PluginName = "NoMapCartographytable";

		public const string PluginVersion = "1.0.1";

		internal const string ConfigSyncGuid = "_shudnal.ConditionalConfigSync";

		internal static bool ConfigSyncAvailable;

		private static ConfigEntry<bool> lockConfigurationSetting;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> LogEvents;

		private static Harmony harmony;

		private static bool sessionActive;

		private static MapTable sessionTable;

		private static Transform sessionPoint;

		private static readonly FieldInfo ReadSwitchField = AccessTools.Field(typeof(MapTable), "m_readSwitch");

		private static readonly FieldInfo InteractDistanceField = AccessTools.Field(typeof(Player), "m_maxInteractDistance");

		private static readonly FieldInfo NoMapField = AccessTools.Field(typeof(Game), "m_noMap");

		internal static bool SessionActive => sessionActive;

		internal static bool NoMap
		{
			get
			{
				if (NoMapField != null)
				{
					return (bool)NoMapField.GetValue(null);
				}
				return false;
			}
			set
			{
				NoMapField?.SetValue(null, value);
			}
		}

		private void Awake()
		{
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ConfigSyncAvailable = Chainloader.PluginInfos.ContainsKey("_shudnal.ConditionalConfigSync");
			if (ConfigSyncAvailable)
			{
				ConfigSyncCompat.Initialize("com.dreich.nomapcartographytable", "NoMapCartographytable", "1.0.1");
			}
			else
			{
				Log.LogInfo((object)"ConditionalConfigSync not found; configuration will not be synced from the server.");
			}
			lockConfigurationSetting = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Lock Configuration", true, "If on, the configuration is locked and can be changed by server admins only. Requires ConditionalConfigSync.");
			if (ConfigSyncAvailable)
			{
				ConfigSyncCompat.AddLockingConfigEntry(lockConfigurationSetting);
			}
			Enabled = BindServerControlled("General", "Enabled", defaultValue: true, "Using a cartography table in a world with the No-Map modifier opens the map while you use the cartography table.");
			LogEvents = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "LogEvents", false, "Log when the table map is opened and closed.");
			harmony = new Harmony("com.dreich.nomapcartographytable");
			harmony.PatchAll();
		}

		private ConfigEntry<T> BindServerControlled<T>(string section, string key, T defaultValue, string description)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return BindServerControlled(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		private ConfigEntry<T> BindServerControlled<T>(string section, string key, T defaultValue, ConfigDescription description)
		{
			ConfigEntry<T> val = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, defaultValue, description);
			if (ConfigSyncAvailable)
			{
				ConfigSyncCompat.AddServerControlled<T>(val);
			}
			return val;
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			harmony = null;
			ClearSession();
		}

		private static void ClearSession()
		{
			sessionActive = false;
			sessionTable = null;
			sessionPoint = null;
		}

		internal static void OpenFromTable(MapTable table)
		{
			if (!((Object)(object)Minimap.instance == (Object)null) && !((Object)(object)table == (Object)null))
			{
				sessionTable = table;
				object? obj = ReadSwitchField?.GetValue(table);
				Component val = (Component)((obj is Component) ? obj : null);
				sessionPoint = (((Object)(object)val != (Object)null) ? val.transform : ((Component)table).transform);
				sessionActive = true;
				Minimap.instance.SetMapMode((MapMode)2);
				if (!Minimap.IsOpen())
				{
					ClearSession();
				}
				else if (LogEvents.Value)
				{
					Log.LogInfo((object)"Opened map from cartography table.");
				}
			}
		}

		internal static void EndSession()
		{
			if (sessionActive)
			{
				ClearSession();
				if (LogEvents.Value)
				{
					Log.LogInfo((object)"Closed table map.");
				}
			}
		}

		internal static bool ShouldCloseSession()
		{
			if (!sessionActive)
			{
				return false;
			}
			if ((Object)(object)sessionTable == (Object)null || (Object)(object)sessionPoint == (Object)null)
			{
				return true;
			}
			if (!Enabled.Value || !NoMap)
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead())
			{
				return true;
			}
			return !InInteractRange(localPlayer);
		}

		private static bool InInteractRange(Player player)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			float num = ((InteractDistanceField != null) ? ((float)InteractDistanceField.GetValue(player)) : 5f);
			return Vector3.Distance(((Character)player).GetEyePoint(), sessionPoint.position) <= num;
		}
	}
	internal static class ConfigSyncCompat
	{
		private static object configSync;

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void Initialize(string guid, string name, string version)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			configSync = (object)new ConfigSync(guid)
			{
				DisplayName = name,
				CurrentVersion = version,
				MinimumRequiredVersion = version,
				ModRequired = false,
				ModRequirementMode = (ModRequirementMode)1
			};
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void AddLockingConfigEntry(ConfigEntry<bool> entry)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			((ConditionalConfigSync)(ConfigSync)configSync).AddLockingConfigEntry<bool>(entry);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void AddServerControlled<T>(ConfigEntry<T> entry)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			((ConditionalConfigSync)(ConfigSync)configSync).AddConfigEntry<T>(entry, (ConfigSyncMode)0);
		}
	}
	[HarmonyPatch(typeof(MapTable), "OnRead", new Type[]
	{
		typeof(Switch),
		typeof(Humanoid),
		typeof(ItemData)
	})]
	internal static class MapTableReadPatch
	{
		private static void Postfix(MapTable __instance, Humanoid user, ItemData item)
		{
			if (NoMapCartographytablePlugin.Enabled.Value && NoMapCartographytablePlugin.NoMap && item == null && !((Object)(object)user == (Object)null) && !((Object)(object)user != (Object)(object)Player.m_localPlayer))
			{
				NoMapCartographytablePlugin.OpenFromTable(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Minimap), "SetMapMode")]
	internal static class MinimapSetMapModePatch
	{
		private static void Prefix(MapMode mode, ref bool __state)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Invalid comparison between Unknown and I4
			__state = false;
			if ((int)mode == 2 && NoMapCartographytablePlugin.SessionActive && NoMapCartographytablePlugin.Enabled.Value && NoMapCartographytablePlugin.NoMap)
			{
				NoMapCartographytablePlugin.NoMap = false;
				__state = true;
			}
		}

		private static void Finalizer(MapMode mode, bool __state)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Invalid comparison between Unknown and I4
			if (__state)
			{
				NoMapCartographytablePlugin.NoMap = true;
			}
			else if (NoMapCartographytablePlugin.SessionActive && (int)mode != 2)
			{
				NoMapCartographytablePlugin.EndSession();
			}
		}
	}
	[HarmonyPatch(typeof(Minimap), "Update")]
	internal static class MinimapUpdatePatch
	{
		private static void Postfix(Minimap __instance)
		{
			if (NoMapCartographytablePlugin.SessionActive && NoMapCartographytablePlugin.ShouldCloseSession())
			{
				NoMapCartographytablePlugin.EndSession();
				__instance.SetMapMode((MapMode)0);
			}
		}
	}
}