Decompiled source of BuildRestrictionTweaksSync v1.0.3

BuildRestrictionTweaksSync.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using BuildRestrictionTweaksSync.Configs;
using BuildRestrictionTweaksSync.Extensions;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("JotunnModTemplate1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("JotunnModTemplate1")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
namespace BuildRestrictionTweaksSync
{
	internal static class Patches
	{
		[HarmonyPatch(typeof(Location))]
		private static class Location_IsInsideNoBuildLocation_Patch
		{
			[HarmonyPostfix]
			[HarmonyPatch("IsInsideNoBuildLocation")]
			private static void Postfix(ref bool __result)
			{
				if (RestrictionTweaks.IgnoreBuildZone.Value || RestrictionTweaks.DisableAllRestrictions.Value)
				{
					__result = false;
				}
			}
		}

		[HarmonyPatch(typeof(CraftingStation))]
		private static class CraftingStation_HaveBuildStationInRange_Patch
		{
			[HarmonyPostfix]
			[HarmonyPatch("HaveBuildStationInRange")]
			private static void Postfix(ref CraftingStation __result, string name)
			{
				if ((RestrictionTweaks.IgnoreMissingStation.Value || RestrictionTweaks.DisableAllRestrictions.Value) && (Object)(object)__result == (Object)null)
				{
					__result = GetCraftingStation();
				}
			}
		}

		[HarmonyPatch(typeof(Player))]
		private static class Player_UpdatePlacementGhost_Patch
		{
			[HarmonyPostfix]
			[HarmonyPatch("UpdatePlacementGhost")]
			private static void Postfix(Player __instance)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: 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_001b: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Invalid comparison between Unknown and I4
				//IL_00da: Unknown result type (might be due to invalid IL or missing references)
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Invalid comparison between Unknown and I4
				//IL_0042: Unknown result type (might be due to invalid IL or missing references)
				//IL_0044: Invalid comparison between Unknown and I4
				//IL_0055: Unknown result type (might be due to invalid IL or missing references)
				//IL_0057: Invalid comparison between Unknown and I4
				//IL_0065: Unknown result type (might be due to invalid IL or missing references)
				//IL_0067: Invalid comparison between Unknown and I4
				//IL_0075: Unknown result type (might be due to invalid IL or missing references)
				//IL_0077: Invalid comparison between Unknown and I4
				//IL_0085: Unknown result type (might be due to invalid IL or missing references)
				//IL_0087: Invalid comparison between Unknown and I4
				//IL_0095: Unknown result type (might be due to invalid IL or missing references)
				//IL_0097: Invalid comparison between Unknown and I4
				//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a8: Invalid comparison between Unknown and I4
				//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: Invalid comparison between Unknown and I4
				//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ca: Invalid comparison between Unknown and I4
				if ((Object)(object)__instance.m_placementGhost != (Object)null)
				{
					PlacementStatus placementStatus = __instance.m_placementStatus;
					if ((int)placementStatus != 0 && (int)placementStatus != 4 && (RestrictionTweaks.DisableAllRestrictions.Value || ((int)placementStatus == 1 && RestrictionTweaks.IgnoreInvalid.Value) || ((int)placementStatus == 2 && RestrictionTweaks.IgnoreBlockedbyPlayer.Value) || ((int)placementStatus == 3 && RestrictionTweaks.IgnoreBuildZone.Value) || ((int)placementStatus == 5 && RestrictionTweaks.IgnoreSpaceRestrictions.Value) || ((int)placementStatus == 6 && RestrictionTweaks.IgnoreTeleportAreaRestrictions.Value) || ((int)placementStatus == 7 && RestrictionTweaks.IgnoreMissingStationExtension.Value) || ((int)placementStatus == 8 && RestrictionTweaks.IgnoreBiomeRestrictions.Value) || ((int)placementStatus == 9 && RestrictionTweaks.IgnoreCultivationRestrictions.Value) || ((int)placementStatus == 10 && RestrictionTweaks.IgnoreDirtRestrictions.Value) || ((int)placementStatus == 11 && RestrictionTweaks.IgnoreDungeonRestrictions.Value)))
					{
						__instance.m_placementStatus = (PlacementStatus)0;
						__instance.SetPlacementGhostValid(true);
					}
				}
			}
		}

		private static GameObject _gameObject;

		private static CraftingStation _craftingStation;

		private static CraftingStation GetCraftingStation()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if ((Object)(object)_gameObject == (Object)null)
			{
				_gameObject = new GameObject();
				Object.DontDestroyOnLoad((Object)(object)_gameObject);
			}
			if ((Object)(object)_craftingStation == (Object)null)
			{
				_craftingStation = _gameObject.AddComponent<CraftingStation>();
			}
			return _craftingStation;
		}
	}
	[BepInPlugin("Searica.Valheim.BuildRestrictionTweaksSync", "BuildRestrictionTweaksSync", "1.0.3")]
	[BepInDependency("com.jotunn.jotunn", "2.18.1")]
	internal sealed class RestrictionTweaks : BaseUnityPlugin
	{
		internal const string Author = "Searica";

		public const string PluginName = "BuildRestrictionTweaksSync";

		public const string PluginGUID = "Searica.Valheim.BuildRestrictionTweaksSync";

		public const string PluginVersion = "1.0.3";

		private const string MainSection = "Global";

		public static ConfigEntry<bool> DisableAllRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreInvalid { get; private set; }

		public static ConfigEntry<bool> IgnoreBlockedbyPlayer { get; private set; }

		public static ConfigEntry<bool> IgnoreBuildZone { get; private set; }

		public static ConfigEntry<bool> IgnoreSpaceRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreTeleportAreaRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreMissingStation { get; private set; }

		public static ConfigEntry<bool> IgnoreMissingStationExtension { get; private set; }

		public static ConfigEntry<bool> IgnoreBiomeRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreCultivationRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreDirtRestrictions { get; private set; }

		public static ConfigEntry<bool> IgnoreDungeonRestrictions { get; private set; }

		public void Awake()
		{
			Log.Init(((BaseUnityPlugin)this).Logger);
			ConfigManager.Init("Searica.Valheim.BuildRestrictionTweaksSync", ((BaseUnityPlugin)this).Config);
			Initialize();
			ConfigManager.SaveOnConfigSet(value: true);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "Searica.Valheim.BuildRestrictionTweaksSync");
			Game.isModded = true;
			ConfigManager.SetupWatcher();
		}

		public void OnDestroy()
		{
			ConfigManager.Save();
		}

		internal static void Initialize()
		{
			DisableAllRestrictions = ConfigManager.BindConfig("Global", "\u200b\u200b\u200b\u200b\u200bDisableAllRestrictions", value: false, "Remove all build restrictions.");
			IgnoreBlockedbyPlayer = ConfigManager.BindConfig("Global", "ignoreBlockedbyPlayer", value: false, "Ignore player blocking build.");
			IgnoreInvalid = ConfigManager.BindConfig("Global", "IgnoreInvalid", value: false, "Prevent misc build restrictions.");
			IgnoreBuildZone = ConfigManager.BindConfig("Global", "IgnoreBuildZone", value: false, "Ignore zone restrictions.");
			IgnoreSpaceRestrictions = ConfigManager.BindConfig("Global", "IgnoreSpaceRestrictions", value: false, "Ignore space restrictions.");
			IgnoreTeleportAreaRestrictions = ConfigManager.BindConfig("Global", "IgnoreTeleportAreaRestrictions", value: false, "Ignore teleport area restrictions.");
			IgnoreMissingStationExtension = ConfigManager.BindConfig("Global", "IignoreMissingStationExtension", value: false, "Ignore missing station extension.");
			IgnoreMissingStation = ConfigManager.BindConfig("Global", "IgnoreMissingStation", value: false, "Ignore missing station.");
			IgnoreBiomeRestrictions = ConfigManager.BindConfig("Global", "IgnoreBiomeRestrictions", value: false, "Ignore biome restrictions.");
			IgnoreCultivationRestrictions = ConfigManager.BindConfig("Global", "IgnoreCultivationRestrictions", value: false, "Ignore need for cultivated ground.");
			IgnoreDirtRestrictions = ConfigManager.BindConfig("Global", "IgnoreDirtRestrictions", value: false, "Ignore need for dirt.");
			IgnoreDungeonRestrictions = ConfigManager.BindConfig("Global", "IgnoreDungeonRestrictions", value: false, "Ignore indoor restrictions.");
		}
	}
	internal enum LogLevel
	{
		Low,
		Medium,
		High
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		private const BindingFlags AllBindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty;

		internal static ConfigEntry<LogLevel> Verbosity { get; set; }

		internal static LogLevel VerbosityLevel => Verbosity.Value;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void LogDebug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void LogError(object data)
		{
			_logSource.LogError(data);
		}

		internal static void LogFatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void LogInfo(object data, LogLevel level = LogLevel.Low)
		{
			if (Verbosity == null || VerbosityLevel >= level)
			{
				_logSource.LogInfo(data);
			}
		}

		internal static void LogMessage(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void LogWarning(object data)
		{
			_logSource.LogWarning(data);
		}

		internal static void LogGameObject(GameObject prefab, bool includeChildren = false)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			LogInfo("***** " + ((Object)prefab).name + " *****");
			Component[] components = prefab.GetComponents<Component>();
			for (int i = 0; i < components.Length; i++)
			{
				LogComponent(components[i]);
			}
			if (!includeChildren)
			{
				return;
			}
			LogInfo("***** " + ((Object)prefab).name + " (children) *****");
			foreach (Transform item in prefab.transform)
			{
				Transform val = item;
				LogInfo(" - " + ((Object)((Component)val).gameObject).name);
				components = ((Component)val).gameObject.GetComponents<Component>();
				for (int i = 0; i < components.Length; i++)
				{
					LogComponent(components[i]);
				}
			}
		}

		internal static void LogComponent(Component compo)
		{
			LogInfo($"--- {((object)compo).GetType().Name}: {((Object)compo).name} ---");
			PropertyInfo[] properties = ((object)compo).GetType().GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty);
			foreach (PropertyInfo propertyInfo in properties)
			{
				LogInfo($" - {propertyInfo.Name} = {propertyInfo.GetValue(compo)}");
			}
			FieldInfo[] fields = ((object)compo).GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty);
			foreach (FieldInfo fieldInfo in fields)
			{
				LogInfo($" - {fieldInfo.Name} = {fieldInfo.GetValue(compo)}");
			}
		}
	}
}
namespace BuildRestrictionTweaksSync.Extensions
{
	internal static class EventExtensions
	{
		public static void SafeInvoke(this Action events)
		{
			if (events == null)
			{
				return;
			}
			Delegate[] invocationList = events.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action action = (Action)invocationList[i];
				try
				{
					action();
				}
				catch (Exception value)
				{
					Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{value}");
				}
			}
		}

		public static void SafeInvoke<TArg1>(this Action<TArg1> events, TArg1 arg1)
		{
			if (events == null)
			{
				return;
			}
			Delegate[] invocationList = events.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action<TArg1> action = (Action<TArg1>)invocationList[i];
				try
				{
					action(arg1);
				}
				catch (Exception value)
				{
					Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{value}");
				}
			}
		}

		public static void SafeInvoke<TArg1, TArg2>(this Action<TArg1, TArg2> events, TArg1 arg1, TArg2 arg2)
		{
			if (events == null)
			{
				return;
			}
			Delegate[] invocationList = events.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action<TArg1, TArg2> action = (Action<TArg1, TArg2>)invocationList[i];
				try
				{
					action(arg1, arg2);
				}
				catch (Exception value)
				{
					Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{value}");
				}
			}
		}

		public static void SafeInvoke<TEventArg>(this EventHandler<TEventArg> events, object sender, TEventArg arg1)
		{
			if (events == null)
			{
				return;
			}
			Delegate[] invocationList = events.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				EventHandler<TEventArg> eventHandler = (EventHandler<TEventArg>)invocationList[i];
				try
				{
					eventHandler(sender, arg1);
				}
				catch (Exception value)
				{
					Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {eventHandler.Method.DeclaringType.Name}.{eventHandler.Method.Name}:\n{value}");
				}
			}
		}
	}
}
namespace BuildRestrictionTweaksSync.Configs
{
	internal static class ConfigManager
	{
		private static string ConfigFileName;

		private static string ConfigFileFullPath;

		private static ConfigFile configFile;

		private static BaseUnityPlugin ConfigurationManager;

		private const string ConfigManagerGUID = "com.bepis.bepinex.configurationmanager";

		private static readonly ConfigurationManagerAttributes AdminConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};

		private static readonly ConfigurationManagerAttributes ClientConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = false
		};

		private const char ZWS = '\u200b';

		internal static event Action OnConfigWindowClosed;

		internal static event Action OnConfigFileReloaded;

		private static void InvokeOnConfigWindowClosed()
		{
			ConfigManager.OnConfigWindowClosed?.SafeInvoke();
		}

		private static void InvokeOnConfigFileReloaded()
		{
			ConfigManager.OnConfigFileReloaded?.SafeInvoke();
		}

		internal static ConfigEntry<T> BindConfig<T>(string section, string name, T value, string description, AcceptableValueBase acceptVals = null, bool synced = true)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			string extendedDescription = GetExtendedDescription(description, synced);
			return configFile.Bind<T>(section, name, value, new ConfigDescription(extendedDescription, acceptVals, new object[1] { synced ? AdminConfig : ClientConfig }));
		}

		internal static string SetStringPriority(string sectionName, int priority)
		{
			if (priority == 0)
			{
				return sectionName;
			}
			return new string('\u200b', priority) + sectionName;
		}

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

		internal static void Init(string GUID, ConfigFile config, bool saveOnConfigSet = false)
		{
			configFile = config;
			configFile.SaveOnConfigSet = saveOnConfigSet;
			ConfigFileName = GUID + ".cfg";
			ConfigFileFullPath = Path.Combine(Paths.ConfigPath, ConfigFileName);
		}

		internal static bool DisableSaveOnConfigSet()
		{
			bool saveOnConfigSet = configFile.SaveOnConfigSet;
			configFile.SaveOnConfigSet = false;
			return saveOnConfigSet;
		}

		internal static void SaveOnConfigSet(bool value)
		{
			configFile.SaveOnConfigSet = value;
		}

		internal static void Save()
		{
			configFile.Save();
		}

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

		private static void ReloadConfigFile(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				Log.LogInfo("Reloading config file");
				bool value = DisableSaveOnConfigSet();
				configFile.Reload();
				SaveOnConfigSet(value);
				InvokeOnConfigFileReloaded();
			}
			catch
			{
				Log.LogError("There was an issue loading your " + ConfigFileName);
				Log.LogError("Please check your config entries for spelling and format!");
			}
		}

		internal static void CheckForConfigManager()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Invalid comparison between Unknown and I4
			if ((int)SystemInfo.graphicsDeviceType != 4 && Chainloader.PluginInfos.TryGetValue("com.bepis.bepinex.configurationmanager", out var value) && Object.op_Implicit((Object)(object)value.Instance))
			{
				ConfigurationManager = value.Instance;
				Log.LogDebug("Configuration manager found, hooking DisplayingWindowChanged");
				EventInfo @event = ((object)ConfigurationManager).GetType().GetEvent("DisplayingWindowChanged");
				if (@event != null)
				{
					Action<object, object> action = OnConfigManagerDisplayingWindowChanged;
					Delegate handler = Delegate.CreateDelegate(@event.EventHandlerType, action.Target, action.Method);
					@event.AddEventHandler(ConfigurationManager, handler);
				}
			}
		}

		private static void OnConfigManagerDisplayingWindowChanged(object sender, object e)
		{
			if (!(bool)((object)ConfigurationManager).GetType().GetProperty("DisplayingWindow").GetValue(ConfigurationManager, null))
			{
				InvokeOnConfigWindowClosed();
			}
		}
	}
}