Decompiled source of AdjustablePortals v0.1.0

plugins/AdjustablePortals.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using AdjustablePortals.modules;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AdjustablePortals")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AdjustablePortals")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
namespace AdjustablePortals
{
	[BepInPlugin("MidnightsFX.AdjustablePortals", "AdjustablePortals", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class AdjustablePortals : BaseUnityPlugin
	{
		public const string PluginGUID = "MidnightsFX.AdjustablePortals";

		public const string PluginName = "AdjustablePortals";

		public const string PluginVersion = "0.1.0";

		internal static Harmony Harmony = new Harmony("MidnightsFX.AdjustablePortals");

		public static CustomLocalization Localization = LocalizationManager.Instance.GetLocalization();

		internal static ManualLogSource Log;

		public void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			new ValConfig(((BaseUnityPlugin)this).Config);
			TeleportItems.SetupTeleportLists();
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			Harmony.PatchAll(executingAssembly);
		}
	}
	internal class Logger
	{
		public static LogLevel Level = (LogLevel)16;

		public static void enableDebugLogging(object sender, EventArgs e)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if (ValConfig.EnableDebugMode.Value)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void setDebugLogging(bool state)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			if (state)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void LogDebug(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 32)
			{
				AdjustablePortals.Log.LogInfo((object)message);
			}
		}

		public static void LogInfo(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 16)
			{
				AdjustablePortals.Log.LogInfo((object)message);
			}
		}

		public static void LogWarning(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 4)
			{
				AdjustablePortals.Log.LogWarning((object)message);
			}
		}

		public static void LogError(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 2)
			{
				AdjustablePortals.Log.LogError((object)message);
			}
		}
	}
	internal class ValConfig
	{
		public static ConfigFile cfg;

		public static ConfigEntry<bool> EnableDebugMode;

		public static ConfigEntry<float> PortalPieceActivationDistance;

		public static ConfigEntry<int> PortalNearbyPiecesForActivation;

		public static ConfigEntry<string> DefeatedEikthyrAllowedItems;

		public static ConfigEntry<string> DefeatedElderAllowedItems;

		public static ConfigEntry<string> DefeatedBonemassAllowedItems;

		public static ConfigEntry<string> DefeatedModerAllowItems;

		public static ConfigEntry<string> DefeatedYagluthAllowItems;

		public static ConfigEntry<string> DefeatedQueenAllowItems;

		public static ConfigEntry<string> DefeatedFaderAllowItems;

		public ValConfig(ConfigFile cf)
		{
			cfg = cf;
			cfg.SaveOnConfigSet = true;
			CreateConfigValues(cf);
			Logger.setDebugLogging(EnableDebugMode.Value);
			SetupMainFileWatcher();
		}

		private void CreateConfigValues(ConfigFile Config)
		{
			//IL_001b: 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_002d: Expected O, but got Unknown
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			EnableDebugMode = Config.Bind<bool>("Client config", "EnableDebugMode", false, new ConfigDescription("Enables Debug logging.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdvanced = true
			} }));
			EnableDebugMode.SettingChanged += Logger.enableDebugLogging;
			PortalNearbyPiecesForActivation = BindServerConfig("PortalActivation", "PortalNearbyPiecesForActivation", 2000, "The number of building pieces required nearby in order for a portal to be activated.", advanced: false, 0, 10000);
			PortalPieceActivationDistance = BindServerConfig("PortalActivation", "PortalPieceActivationDistance", 100f, "The distance that will be checked for nearby building pieces to meet the required structures nearby.");
			DefeatedEikthyrAllowedItems = BindServerConfig("PortalProgression", "DefeatedEikthyrAllowedItems", "", "Comma seperated list of prefab items that will be allowed to teleported once Eikthyr is defeated.");
			DefeatedEikthyrAllowedItems.SettingChanged += TeleportItems.EikthyrAllowedTeleportsChanged;
			DefeatedElderAllowedItems = BindServerConfig("PortalProgression", "DefeatedElderAllowedItems", "", "Comma seperated list of prefab items that will be allowed to be teleported once The Elder is defeated.");
			DefeatedElderAllowedItems.SettingChanged += TeleportItems.ElderAllowedTeleportsChanged;
			DefeatedBonemassAllowedItems = BindServerConfig("PortalProgression", "DefeatedBonemassAllowedItems", "Bronze,Copper,Tin,CopperOre,TinOre,BronzeScrap", "Comma seperated list of prefab items that will be allowed to be teleported once Bonemass is defeated.");
			DefeatedBonemassAllowedItems.SettingChanged += TeleportItems.BonemassAllowedTeleportsChanged;
			DefeatedModerAllowItems = BindServerConfig("PortalProgression", "DefeatedModerAllowItems", "Iron,IronOre,Ironpit,IronScrap,chest_hildir1,chest_hildir2,chest_hildir3", "Comma seperated list of prefab items that will be allowed to be teleported once Moder is defeated.");
			DefeatedModerAllowItems.SettingChanged += TeleportItems.ModerAllowedTeleportsChanged;
			DefeatedYagluthAllowItems = BindServerConfig("PortalProgression", "DefeatedYagluthAllowItems", "Silver,SilverOre,DragonEgg", "Comma seperated list of prefab items that will be allowed to be teleported once Yagluth is defeated.");
			DefeatedYagluthAllowItems.SettingChanged += TeleportItems.YagluthAllowedTeleportsChanged;
			DefeatedQueenAllowItems = BindServerConfig("PortalProgression", "DefeatedQueenAllowItems", "MechanicalSpring,BlackMetal,BlackMetalScrap", "Comma seperated list of prefab items that will be allowed to be teleported once The Seeker Queen is defeated.");
			DefeatedQueenAllowItems.SettingChanged += TeleportItems.QueenAllowedTeleportsChanged;
			DefeatedFaderAllowItems = BindServerConfig("PortalProgression", "DefeatedFaderAllowItems", "DvergrNeedle", "Comma seperated list of prefab items that will be allowed to be teleported once Fader is defeated.");
			DefeatedFaderAllowItems.SettingChanged += TeleportItems.FaderAllowedTeleportsChanged;
		}

		internal static void SetupMainFileWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
			fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
			fileSystemWatcher.Path = Path.GetDirectoryName(cfg.ConfigFilePath);
			fileSystemWatcher.Filter = "MidnightsFX.ImpactfulSkills.cfg";
			fileSystemWatcher.Changed += OnConfigFileChanged;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
		{
			if (ZNet.instance.IsServer())
			{
				Logger.LogInfo("Configuration file has been changed, reloading settings.");
				cfg.Reload();
			}
		}

		public static ConfigEntry<List<string>> BindServerConfig(string catagory, string key, List<string> value, string description, bool advanced = false)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			return cfg.Bind<List<string>>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<float[]> BindServerConfig(string catagory, string key, float[] value, string description, bool advanced = false, float valmin = 0f, float valmax = 150f)
		{
			//IL_001b: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<float[]>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<bool> BindServerConfig(string catagory, string key, bool value, string description, AcceptableValueBase acceptableValues = null, bool advanced = false)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			return cfg.Bind<bool>(catagory, key, value, new ConfigDescription(description, acceptableValues, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<int> BindServerConfig(string catagory, string key, int value, string description, bool advanced = false, int valmin = 0, int valmax = 150)
		{
			//IL_001b: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<int>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<int>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<float> BindServerConfig(string catagory, string key, float value, string description, bool advanced = false, float valmin = 0f, float valmax = 150f)
		{
			//IL_001b: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<float>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<string> BindServerConfig(string catagory, string key, string value, string description, AcceptableValueList<string> acceptableValues = null, bool advanced = false)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			return cfg.Bind<string>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)acceptableValues, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}
	}
}
namespace AdjustablePortals.modules
{
	internal static class ActivationRequirements
	{
		[HarmonyPatch(typeof(TeleportWorld))]
		internal static class PortalInstanceActivatable
		{
			private static readonly int pieceMask = LayerMask.GetMask(new string[1] { "piece" });

			private static float update_timer = 0f;

			private static float current_update_time = 0f;

			private static int nearby_pieces = 0;

			[HarmonyPatch(typeof(TeleportWorld), "HaveTarget")]
			[HarmonyPostfix]
			internal static void Activator(TeleportWorld __instance, ref bool __result)
			{
				//IL_0061: Unknown result type (might be due to invalid IL or missing references)
				if (!((Object)(object)__instance.m_nview == (Object)null) && __instance.m_nview.IsValid())
				{
					current_update_time += Time.fixedDeltaTime;
					if (update_timer <= current_update_time)
					{
						update_timer = current_update_time + 10f;
						Collider[] array = Physics.OverlapSphere(((Component)__instance).transform.position, ValConfig.PortalPieceActivationDistance.Value, pieceMask);
						nearby_pieces = array.Length;
					}
					if (nearby_pieces < ValConfig.PortalNearbyPiecesForActivation.Value)
					{
						__result = false;
					}
				}
			}

			[HarmonyPatch(typeof(TeleportWorld), "GetHoverText")]
			[HarmonyPostfix]
			internal static void OnHoverHelp(TeleportWorld __instance, ref string __result)
			{
				if (!((Object)(object)__instance.m_nview == (Object)null) && __instance.m_nview.IsValid() && nearby_pieces <= ValConfig.PortalNearbyPiecesForActivation.Value)
				{
					__result += Localization.instance.Localize($"\nMore nearby structures required ({nearby_pieces}/{ValConfig.PortalNearbyPiecesForActivation.Value})");
				}
			}

			[HarmonyPatch(typeof(TeleportWorld), "TargetFound")]
			[HarmonyPostfix]
			internal static void TargetFoundPrevention(TeleportWorld __instance, ref bool __result)
			{
				if (!((Object)(object)__instance.m_nview == (Object)null) && __instance.m_nview.IsValid() && nearby_pieces <= ValConfig.PortalNearbyPiecesForActivation.Value)
				{
					__result = false;
				}
			}
		}
	}
	internal static class TeleportItems
	{
		[HarmonyPatch(typeof(Humanoid))]
		private static class AllowConfiguredTeleportableItems
		{
			[HarmonyPatch("IsTeleportable")]
			private static void Postfix(Humanoid __instance, ref bool __result)
			{
				if (__result)
				{
					return;
				}
				List<string> list = (from x in __instance.m_inventory.GetAllItems()
					where !x.m_shared.m_teleportable
					select ((Object)x.m_dropPrefab).name).Distinct().ToList();
				if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)33))
				{
					foreach (string eikthyrAllowedTeleport in EikthyrAllowedTeleports)
					{
						if (list.Contains(eikthyrAllowedTeleport))
						{
							list.Remove(eikthyrAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)36))
				{
					foreach (string elderAllowedTeleport in ElderAllowedTeleports)
					{
						if (list.Contains(elderAllowedTeleport))
						{
							list.Remove(elderAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)37))
				{
					foreach (string bonemassAllowedTeleport in BonemassAllowedTeleports)
					{
						if (list.Contains(bonemassAllowedTeleport))
						{
							list.Remove(bonemassAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)34))
				{
					foreach (string moderAllowedTeleport in ModerAllowedTeleports)
					{
						if (list.Contains(moderAllowedTeleport))
						{
							list.Remove(moderAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)35))
				{
					foreach (string yagluthAllowedTeleport in YagluthAllowedTeleports)
					{
						if (list.Contains(yagluthAllowedTeleport))
						{
							list.Remove(yagluthAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey("defeated_queen"))
				{
					foreach (string queenAllowedTeleport in QueenAllowedTeleports)
					{
						if (list.Contains(queenAllowedTeleport))
						{
							list.Remove(queenAllowedTeleport);
						}
					}
				}
				if (ZoneSystem.instance.GetGlobalKey("defeated_fader"))
				{
					foreach (string faderAllowedTeleport in FaderAllowedTeleports)
					{
						if (list.Contains(faderAllowedTeleport))
						{
							list.Remove(faderAllowedTeleport);
						}
					}
				}
				if (list.Count == 0)
				{
					__result = true;
					return;
				}
				Logger.LogDebug("The following items are not teleportable " + string.Join(", ", list));
				__result = false;
			}
		}

		internal static List<string> EikthyrAllowedTeleports = new List<string>();

		internal static List<string> ElderAllowedTeleports = new List<string>();

		internal static List<string> BonemassAllowedTeleports = new List<string>();

		internal static List<string> ModerAllowedTeleports = new List<string>();

		internal static List<string> YagluthAllowedTeleports = new List<string>();

		internal static List<string> QueenAllowedTeleports = new List<string>();

		internal static List<string> FaderAllowedTeleports = new List<string>();

		internal static void SetupTeleportLists()
		{
			ConfigListChanged(EikthyrAllowedTeleports, ValConfig.DefeatedEikthyrAllowedItems.Value);
			ConfigListChanged(ElderAllowedTeleports, ValConfig.DefeatedElderAllowedItems.Value);
			ConfigListChanged(BonemassAllowedTeleports, ValConfig.DefeatedBonemassAllowedItems.Value);
			ConfigListChanged(ModerAllowedTeleports, ValConfig.DefeatedModerAllowItems.Value);
			ConfigListChanged(YagluthAllowedTeleports, ValConfig.DefeatedYagluthAllowItems.Value);
			ConfigListChanged(QueenAllowedTeleports, ValConfig.DefeatedQueenAllowItems.Value);
			ConfigListChanged(FaderAllowedTeleports, ValConfig.DefeatedFaderAllowItems.Value);
		}

		internal static void EikthyrAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(EikthyrAllowedTeleports, ValConfig.DefeatedEikthyrAllowedItems.Value);
		}

		internal static void ElderAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(ElderAllowedTeleports, ValConfig.DefeatedElderAllowedItems.Value);
		}

		internal static void BonemassAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(BonemassAllowedTeleports, ValConfig.DefeatedBonemassAllowedItems.Value);
		}

		internal static void ModerAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(ModerAllowedTeleports, ValConfig.DefeatedModerAllowItems.Value);
		}

		internal static void YagluthAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(YagluthAllowedTeleports, ValConfig.DefeatedYagluthAllowItems.Value);
		}

		internal static void QueenAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(QueenAllowedTeleports, ValConfig.DefeatedQueenAllowItems.Value);
		}

		internal static void FaderAllowedTeleportsChanged(object s, EventArgs e)
		{
			ConfigListChanged(FaderAllowedTeleports, ValConfig.DefeatedFaderAllowItems.Value);
		}

		private static void ConfigListChanged(List<string> targetList, string configValue)
		{
			try
			{
				List<string> list = new List<string>();
				string[] array = configValue.Split(new char[1] { ',' });
				foreach (string item in array)
				{
					list.Add(item);
				}
				if (list.Count > 0)
				{
					targetList.Clear();
					targetList.AddRange(list);
				}
			}
			catch (Exception arg)
			{
				Logger.LogWarning($"Error parsing ConfigList: {arg}");
			}
		}
	}
}