Decompiled source of Asocial Cartography v0.2.1

Plugins/VentureValheim.AsocialCartography.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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("VentureValheim.AsocialCartography")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.AsocialCartography")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("697DC3A2-C8D7-4AED-9A2D-013509E7B1C9")]
[assembly: AssemblyFileVersion("0.2.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.1.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace VentureValheim.AsocialCartography
{
	[BepInPlugin("com.orianaventure.mod.AsocialCartography", "AsocialCartography", "0.2.1")]
	public class AsocialCartographyPlugin : BaseUnityPlugin
	{
		private const string ModName = "AsocialCartography";

		private const string ModVersion = "0.2.1";

		private const string Author = "com.orianaventure.mod";

		private const string ModGUID = "com.orianaventure.mod.AsocialCartography";

		private static string ConfigFileName = "com.orianaventure.mod.AsocialCartography.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony HarmonyInstance = new Harmony("com.orianaventure.mod.AsocialCartography");

		public static readonly ManualLogSource AsocialCartographyLogger;

		private static ConfigEntry<bool> CE_AddPins;

		private static ConfigEntry<bool> CE_ReceivePins;

		private static ConfigEntry<bool> CE_IgnoreBossPins;

		private static ConfigEntry<bool> CE_IgnoreHildirPins;

		public static bool GetAddPins()
		{
			return CE_AddPins.Value;
		}

		public static bool GetIgnoreBossPins()
		{
			return CE_IgnoreBossPins.Value;
		}

		public static bool GetIgnoreHildirPins()
		{
			return CE_IgnoreHildirPins.Value;
		}

		public static bool GetReceivePins()
		{
			return CE_ReceivePins.Value;
		}

		private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
		{
			string extendedDescription = GetExtendedDescription(description, synced);
			configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, extendedDescription);
		}

		public string GetExtendedDescription(string description, bool synchronizedSetting)
		{
			return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
		}

		public void Awake()
		{
			AddConfig("AddPins", "General", "False to disable adding player-placed map pins when adding to a map table (boolean).", synced: false, value: false, ref CE_AddPins);
			AddConfig("ReceivePins", "General", "False to disable taking player-placed map pins when reading a map table (boolean).", synced: false, value: true, ref CE_ReceivePins);
			AddConfig("IgnoreBossPins", "General", "False to include boss map pins in the above configs (boolean).", synced: false, value: true, ref CE_IgnoreBossPins);
			AddConfig("IgnoreHildirPins", "General", "False to include hildir map pins in the above configs (boolean).", synced: false, value: true, ref CE_IgnoreHildirPins);
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				AsocialCartographyLogger.LogDebug((object)"Attempting to reload configuration...");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				AsocialCartographyLogger.LogError((object)("There was an issue loading " + ConfigFileName));
			}
		}

		static AsocialCartographyPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			AsocialCartographyLogger = Logger.CreateLogSource("AsocialCartography");
			CE_AddPins = null;
			CE_ReceivePins = null;
			CE_IgnoreBossPins = null;
			CE_IgnoreHildirPins = null;
		}
	}
	public class AsocialCartography
	{
		[HarmonyPatch(typeof(Minimap), "GetSharedMapData")]
		public static class Patch_Minimap_GetSharedMapData
		{
			private static void Prefix(Minimap __instance, byte[] oldMapData, out List<PinData> __state)
			{
				//IL_002b: Unknown result type (might be due to invalid IL or missing references)
				__state = __instance.m_pins.ToList();
				if (!AsocialCartographyPlugin.GetAddPins())
				{
					__instance.m_pins = new List<PinData>();
					for (int i = 0; i < __state.Count; i++)
					{
						if (AllowedPin(__state[i].m_type))
						{
							__instance.m_pins.Add(__state[i]);
						}
					}
				}
				List<PinData> mapPins = GetMapPins(__instance, oldMapData);
				__instance.m_pins.AddRange(mapPins);
			}

			private static void Postfix(Minimap __instance, List<PinData> __state)
			{
				if (__state != null)
				{
					__instance.m_pins = __state;
				}
			}
		}

		[HarmonyPatch(typeof(Minimap), "AddSharedMapData")]
		public static class Patch_Minimap_AddSharedMapData
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0074: Unknown result type (might be due to invalid IL or missing references)
				//IL_007e: Expected O, but got Unknown
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				MethodInfo obj = AccessTools.Method(typeof(Minimap), "AddPin", (Type[])null, (Type[])null);
				for (int i = 0; i < list.Count; i++)
				{
					if (list[i].opcode == OpCodes.Call)
					{
						object operand = list[i].operand;
						if (operand != null && operand.Equals(obj))
						{
							MethodInfo methodInfo = AccessTools.Method(typeof(AsocialCartography), "AddPinReplacement", (Type[])null, (Type[])null);
							list[i] = new CodeInstruction(OpCodes.Call, (object)methodInfo);
							break;
						}
					}
				}
				return list.AsEnumerable();
			}
		}

		private static bool AllowedPin(PinType pin)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Invalid comparison between Unknown and I4
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Invalid comparison between Unknown and I4
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Invalid comparison between Unknown and I4
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Invalid comparison between Unknown and I4
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Invalid comparison between Unknown and I4
			if ((int)pin == 0 || (int)pin == 1 || (int)pin == 2 || (int)pin == 3 || (int)pin == 6)
			{
				return false;
			}
			if (!AsocialCartographyPlugin.GetIgnoreBossPins() && (int)pin == 9)
			{
				return false;
			}
			if (!AsocialCartographyPlugin.GetIgnoreHildirPins() && ((int)pin == 14 || (int)pin == 15 || (int)pin == 16))
			{
				return false;
			}
			return true;
		}

		private static List<PinData> GetMapPins(Minimap minimap, byte[] mapData)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			//IL_0090: 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)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			List<PinData> list = new List<PinData>();
			if (mapData != null)
			{
				try
				{
					ZPackage val = new ZPackage(mapData);
					int num = val.ReadInt();
					if (minimap.ReadExploredArray(val, num) != null && num >= 2)
					{
						int num2 = val.ReadInt();
						for (int i = 0; i < num2; i++)
						{
							long ownerID = val.ReadLong();
							string name = val.ReadString();
							Vector3 val2 = val.ReadVector3();
							PinType type = (PinType)val.ReadInt();
							bool @checked = val.ReadBool();
							string author = ((num >= 3) ? val.ReadString() : "");
							if (!minimap.HavePinInRange(val2, 1f))
							{
								PinData val3 = new PinData();
								val3.m_type = type;
								val3.m_name = name;
								val3.m_pos = val2;
								val3.m_save = true;
								val3.m_checked = @checked;
								val3.m_ownerID = ownerID;
								val3.m_author = author;
								list.Add(val3);
							}
						}
					}
				}
				catch (Exception ex)
				{
					AsocialCartographyPlugin.AsocialCartographyLogger.LogError((object)"Caught an exception while parsing map data:");
					AsocialCartographyPlugin.AsocialCartographyLogger.LogWarning((object)ex);
				}
			}
			return list;
		}

		public PinData AddPinReplacement(Vector3 pos, PinType type, string name, bool save, bool isChecked, long ownerID, string author)
		{
			//IL_0014: 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_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			if (AsocialCartographyPlugin.GetReceivePins() || AllowedPin(type))
			{
				return Minimap.instance.AddPin(pos, type, name, save, isChecked, ownerID, author);
			}
			return new PinData();
		}
	}
}