Decompiled source of CartographyTableMapRestrict v0.5.0

plugins/CartographyTableMapRestrict.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Cartography Table Map Restrict")]
[assembly: AssemblyProduct("Cartography Table Map Restrict")]
[assembly: AssemblyFileVersion("0.5.0.0")]
[assembly: AssemblyVersion("0.5.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 CartographyTableMapRestrict
{
	[BepInPlugin("aedenthorn.CartographyTableMapRestrict", "Cartography Table Map Restrict", "0.5.0")]
	[BepInProcess("valheim.exe")]
	public class BepInExPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Minimap), "SetMapMode")]
		public static class Minimap_SetMapMode_Patch
		{
			public static void Prefix(ref MapMode mode)
			{
				if (modEnabled.Value && !openingFromTable && (int)mode == 2)
				{
					Dbgl("Blocked the large map: not opened from a cartography table.");
					mode = (MapMode)1;
				}
			}

			public static void Postfix(Minimap __instance)
			{
				if (modEnabled.Value && hideMinimap.Value && (Object)(object)__instance.m_smallRoot != (Object)null && __instance.m_smallRoot.activeSelf)
				{
					__instance.m_smallRoot.SetActive(false);
				}
			}
		}

		[HarmonyPatch(typeof(MapTable), "OnRead", new Type[]
		{
			typeof(Switch),
			typeof(Humanoid),
			typeof(ItemData),
			typeof(bool)
		})]
		public static class MapTable_OnRead_Message_Patch
		{
			public static void Prefix(ref bool showMessage)
			{
				if (modEnabled.Value && suppressMessage.Value)
				{
					showMessage = false;
				}
			}
		}

		[HarmonyPatch(typeof(MapTable), "OnRead", new Type[]
		{
			typeof(Switch),
			typeof(Humanoid),
			typeof(ItemData)
		})]
		public static class MapTable_OnRead_OpenMap_Patch
		{
			public static void Postfix(ItemData item)
			{
				if (modEnabled.Value && !((Object)(object)Player.m_localPlayer == (Object)null) && item == null)
				{
					OpenLargeMap();
				}
			}
		}

		[HarmonyPatch(typeof(Minimap), "ShowPointOnMap")]
		public static class Minimap_ShowPointOnMap_Patch
		{
			public static bool Prefix()
			{
				if (!modEnabled.Value || !preventAutoOpen.Value)
				{
					return true;
				}
				return false;
			}
		}

		public const string pluginGuid = "aedenthorn.CartographyTableMapRestrict";

		public const string pluginName = "Cartography Table Map Restrict";

		public const string pluginVersion = "0.5.0";

		public static ConfigEntry<bool> modEnabled;

		public static ConfigEntry<bool> hideMinimap;

		public static ConfigEntry<bool> suppressMessage;

		public static ConfigEntry<bool> preventAutoOpen;

		public static ConfigEntry<bool> bypassServerNoMap;

		public static ConfigEntry<bool> isDebug;

		public static ConfigEntry<int> nexusID;

		private static ManualLogSource logger;

		private static bool openingFromTable;

		public static void Dbgl(string str)
		{
			if (isDebug != null && isDebug.Value && logger != null)
			{
				logger.LogInfo((object)str);
			}
		}

		public void Awake()
		{
			logger = ((BaseUnityPlugin)this).Logger;
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod.");
			hideMinimap = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideMinimap", true, "Hide the minimap in the corner of the screen.");
			suppressMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SuppressMessage", true, "Suppress the map sync message shown when reading a cartography table.");
			preventAutoOpen = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PreventAutoOpen", true, "Stop the game from force-opening the map when a new location is discovered.");
			bypassServerNoMap = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BypassServerNoMap", true, "Let the cartography table open the map even when the server runs the 'No Map' world modifier.");
			isDebug = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsDebug", false, "Log what the mod blocks and opens.");
			nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1739, "Nexus mod ID for updates");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "aedenthorn.CartographyTableMapRestrict");
		}

		private static void OpenLargeMap()
		{
			Minimap instance = Minimap.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				bool noMap = Game.m_noMap;
				openingFromTable = true;
				if (bypassServerNoMap.Value)
				{
					Game.m_noMap = false;
				}
				try
				{
					instance.SetMapMode((MapMode)2);
				}
				finally
				{
					Game.m_noMap = noMap;
					openingFromTable = false;
				}
				Dbgl("Opened the large map from a cartography table.");
			}
		}
	}
}