Decompiled source of ProtectConductiveItems v1.1.0

ProtectConductiveItems.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LobbyCompatibility.Enums;
using LobbyCompatibility.Features;
using Microsoft.CodeAnalysis;
using ProtectConductiveItems.Dependencies;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("AmazingAssets.TerrainToMesh")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp-firstpass")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("ClientNetworkTransform")]
[assembly: IgnoresAccessChecksTo("DissonanceVoip")]
[assembly: IgnoresAccessChecksTo("Facepunch Transport for Netcode for GameObjects")]
[assembly: IgnoresAccessChecksTo("Facepunch.Steamworks.Win64")]
[assembly: IgnoresAccessChecksTo("Unity.AI.Navigation")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging.DocCodeExamples")]
[assembly: IgnoresAccessChecksTo("Unity.Burst")]
[assembly: IgnoresAccessChecksTo("Unity.Burst.Unsafe")]
[assembly: IgnoresAccessChecksTo("Unity.Collections")]
[assembly: IgnoresAccessChecksTo("Unity.Collections.LowLevel.ILSupport")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem.ForUI")]
[assembly: IgnoresAccessChecksTo("Unity.Jobs")]
[assembly: IgnoresAccessChecksTo("Unity.Mathematics")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.Common")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.MetricTypes")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStats")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Component")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Implementation")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsReporting")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkProfiler.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkSolutionInterface")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Components")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Networking.Transport")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Csg")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.KdTree")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Poly2Tri")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Stl")]
[assembly: IgnoresAccessChecksTo("Unity.Profiling.Core")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.ShaderLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Config.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.ShaderGraph.ShaderGraphLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Authentication")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Analytics")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Device")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Networking")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Registration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Scheduler")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Telemetry")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Threading")]
[assembly: IgnoresAccessChecksTo("Unity.Services.QoS")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Relay")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("Unity.Timeline")]
[assembly: IgnoresAccessChecksTo("Unity.VisualEffectGraph.Runtime")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ARModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.NVIDIAModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TestAccount666.ProtectConductiveItems")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Protects your items from lightning")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+544dc7869d263eb5011d74c61ffc4921c03af1b8")]
[assembly: AssemblyProduct("ProtectConductiveItems")]
[assembly: AssemblyTitle("TestAccount666.ProtectConductiveItems")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ProtectConductiveItems
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("TestAccount666.ProtectConductiveItems", "ProtectConductiveItems", "1.1.0")]
	public class ProtectConductiveItems : BaseUnityPlugin
	{
		internal static readonly List<string> FilterList = new List<string>();

		internal static ConfigEntry<string> filterConfigEntry = null;

		internal static ConfigEntry<bool> protectToolsEntry = null;

		public static ProtectConductiveItems Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		internal static void Patch()
		{
			//IL_000d: 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)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("TestAccount666.ProtectConductiveItems");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		private static void ReadAndApplyFilter()
		{
			ReadFilter();
			ResetMetalObjects();
		}

		private static void ReadFilter()
		{
			FilterList.Clear();
			string value = filterConfigEntry.Value;
			value = value.ToLower().Replace(", ", ",");
			string[] array = value.Split(",");
			foreach (string text in array)
			{
				if (text != null)
				{
					string text2 = text;
					while (text2.StartsWith(" "))
					{
						string text3 = text2;
						text2 = text3.Substring(1, text3.Length - 1);
					}
					while (text2.EndsWith(" "))
					{
						string text4 = text2;
						int length = text4.Length;
						int num = length - 1;
						text2 = text4.Substring(num, length - num);
					}
					FilterList.Add(text2);
				}
			}
			FilterList.RemoveAll(string.IsNullOrWhiteSpace);
		}

		internal static void ResetMetalObjects()
		{
			StormyWeather val = Object.FindObjectOfType<StormyWeather>();
			if (val != null && ((Behaviour)val).isActiveAndEnabled)
			{
				val.metalObjects.Clear();
				((MonoBehaviour)val).StartCoroutine(val.GetMetalObjectsAfterDelay());
			}
		}

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			if (DependencyChecker.IsLobbyCompatibilityInstalled())
			{
				Logger.LogInfo((object)"Found LobbyCompatibility Mod, initializing support :)");
				LobbyCompatibilitySupport.Initialize();
			}
			InitializeConfig();
			ReadFilter();
			Patch();
			Logger.LogInfo((object)"TestAccount666.ProtectConductiveItems v1.1.0 has loaded!");
		}

		private static void InitializeConfig()
		{
			filterConfigEntry = ((BaseUnityPlugin)Instance).Config.Bind<string>("General", "Name Filter", "", "A comma-separated list of items that should no longer be targeted by lightning. This will use 'StartsWith', so only the first characters are needed");
			protectToolsEntry = ((BaseUnityPlugin)Instance).Config.Bind<bool>("General", "Protect Tools", true, "If true, will generally prevent tools from being targeted by lightning");
			filterConfigEntry.SettingChanged += delegate
			{
				ReadAndApplyFilter();
			};
			protectToolsEntry.SettingChanged += delegate
			{
				ResetMetalObjects();
			};
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "TestAccount666.ProtectConductiveItems";

		public const string PLUGIN_NAME = "ProtectConductiveItems";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace ProtectConductiveItems.Patches
{
	[HarmonyPatch(typeof(GrabbableObject))]
	public static class GrabbableObjectPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void RemoveFromMetalObjectsList(GrabbableObject __instance)
		{
			StormyWeather stormyWeather = StormyWeatherPatch.stormyWeather;
			if (stormyWeather != null && StormyWeatherPatch.MatchesFilter(__instance))
			{
				stormyWeather.metalObjects.Remove(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(StormyWeather))]
	public static class StormyWeatherPatch
	{
		public static StormyWeather? stormyWeather;

		[HarmonyPatch("OnEnable")]
		[HarmonyPostfix]
		private static void CacheStormyWeather(StormyWeather __instance)
		{
			stormyWeather = __instance;
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> ReplaceFindObjectsOfTypeCall(IEnumerable<CodeInstruction> instructions)
		{
			ProtectConductiveItems.Logger.LogDebug((object)"Searching for FindObjectsOfType...");
			List<CodeInstruction> codes = new List<CodeInstruction>(instructions);
			foreach (CodeInstruction codeInstruction in codes)
			{
				if (codeInstruction.opcode != OpCodes.Call || !(codeInstruction.operand is MethodInfo methodInfo) || !(methodInfo.Name == "FindObjectsOfType"))
				{
					yield return codeInstruction;
					continue;
				}
				ProtectConductiveItems.Logger.LogDebug((object)"Found!");
				MethodInfo getFilteredObjectsMethod = AccessTools.Method(typeof(StormyWeatherPatch), "GetFilteredObjects", (Type[])null, (Type[])null);
				ProtectConductiveItems.Logger.LogDebug((object)("Replacing with method '" + getFilteredObjectsMethod?.ToString() + "'!"));
				yield return new CodeInstruction(OpCodes.Call, (object)getFilteredObjectsMethod);
			}
		}

		public static GrabbableObject[] GetFilteredObjects()
		{
			ProtectConductiveItems.Logger.LogDebug((object)"Filtering Objects!");
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			if (array == null)
			{
				array = Array.Empty<GrabbableObject>();
			}
			List<GrabbableObject> list = array.ToList();
			ProtectConductiveItems.Logger.LogDebug((object)$"Unfiltered objects count: {list.Count}");
			list.RemoveAll(MatchesFilter);
			ProtectConductiveItems.Logger.LogDebug((object)$"Filtered objects count: {list.Count}");
			return list.ToArray();
		}

		public static bool MatchesFilter(GrabbableObject grabbableObject)
		{
			Item itemProperties = grabbableObject?.itemProperties;
			if (itemProperties == null)
			{
				return true;
			}
			if (ProtectConductiveItems.protectToolsEntry.Value && !itemProperties.isScrap)
			{
				return true;
			}
			return ProtectConductiveItems.FilterList.Any((string filter) => itemProperties.itemName.ToLower().StartsWith(filter));
		}
	}
}
namespace ProtectConductiveItems.Dependencies
{
	internal static class DependencyChecker
	{
		internal static bool IsLobbyCompatibilityInstalled()
		{
			return Chainloader.PluginInfos.Values.Any((PluginInfo metadata) => metadata.Metadata.GUID.Contains("LobbyCompatibility"));
		}
	}
	internal static class LobbyCompatibilitySupport
	{
		internal static void Initialize()
		{
			PluginHelper.RegisterPlugin("TestAccount666.ProtectConductiveItems", new Version("1.1.0"), (CompatibilityLevel)2, (VersionStrictness)2);
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}