Decompiled source of WeedKillerFixes v1.2.1

WeedKillerFixes.dll

Decompiled a month 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.Logging;
using DunGen;
using HarmonyLib;
using LobbyCompatibility.Enums;
using LobbyCompatibility.Features;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("WeedKillerFixes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Fixes some major issues with weed killer")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyInformationalVersion("1.2.1+81aa74b928f5aaea70c9884e136c83d8cd99b136")]
[assembly: AssemblyProduct("WeedKillerFixes")]
[assembly: AssemblyTitle("WeedKillerFixes")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.1.0")]
[module: UnverifiableCode]
[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 WeedKillerFixes
{
	internal static class LobbyCompatibility
	{
		internal static void Init()
		{
			PluginHelper.RegisterPlugin("butterystancakes.lethalcompany.weedkillerfixes", Version.Parse("1.2.1"), (CompatibilityLevel)0, (VersionStrictness)0);
		}
	}
	[BepInPlugin("butterystancakes.lethalcompany.weedkillerfixes", "Weed Killer Fixes", "1.2.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal const string PLUGIN_GUID = "butterystancakes.lethalcompany.weedkillerfixes";

		internal const string PLUGIN_NAME = "Weed Killer Fixes";

		internal const string PLUGIN_VERSION = "1.2.1";

		internal static ManualLogSource Logger;

		private const string GUID_LOBBY_COMPATIBILITY = "BMX.LobbyCompatibility";

		private void Awake()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			if (Chainloader.PluginInfos.ContainsKey("BMX.LobbyCompatibility"))
			{
				Logger.LogInfo((object)"CROSS-COMPATIBILITY - Lobby Compatibility detected");
				LobbyCompatibility.Init();
			}
			new Harmony("butterystancakes.lethalcompany.weedkillerfixes").PatchAll();
			Logger.LogInfo((object)"Weed Killer Fixes v1.2.1 loaded");
		}
	}
	[HarmonyPatch]
	internal static class WeedKillerFixesPatches
	{
		private static MoldSpreadManager moldSpreadManager;

		private static VehicleController vehicleController;

		private static CadaverGrowthAI cadaverGrowthAI;

		private static readonly MethodInfo FIND_OBJECT_OF_TYPE_MOLD_SPREAD_MANAGER = AccessTools.Method(typeof(Object), "FindObjectOfType", (Type[])null, new Type[1] { typeof(MoldSpreadManager) });

		private static readonly MethodInfo FIND_OBJECT_OF_TYPE_CADAVER_GROWTH_AI = AccessTools.Method(typeof(Object), "FindObjectOfType", (Type[])null, new Type[1] { typeof(CadaverGrowthAI) });

		private static readonly MethodInfo MOLD_SPREAD_MANAGER_INSTANCE = AccessTools.DeclaredPropertyGetter(typeof(WeedKillerFixesPatches), "MoldSpreadManager");

		private static readonly FieldInfo VEHICLE_CONTROLLER_INSTANCE = AccessTools.Field(typeof(WeedKillerFixesPatches), "vehicleController");

		private static readonly FieldInfo CADAVER_GROWTH_AI_INSTANCE = AccessTools.Field(typeof(WeedKillerFixesPatches), "cadaverGrowthAI");

		private static MoldSpreadManager MoldSpreadManager
		{
			get
			{
				if ((Object)(object)moldSpreadManager == (Object)null)
				{
					moldSpreadManager = Object.FindAnyObjectByType<MoldSpreadManager>();
				}
				return moldSpreadManager;
			}
		}

		[HarmonyPatch(typeof(SprayPaintItem), "TrySprayingWeedKillerBottle")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> SprayPaintItem_Trans_TrySprayingWeedKillerBottle(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			MethodInfo methodInfo = AccessTools.DeclaredPropertyGetter(typeof(Time), "deltaTime");
			MethodInfo methodInfo2 = AccessTools.Method(typeof(Object), "FindObjectOfType", (Type[])null, new Type[1] { typeof(VehicleController) });
			for (int i = 2; i < list.Count - 3; i++)
			{
				if (list[i].opcode == OpCodes.Call)
				{
					MethodInfo methodInfo3 = list[i].operand as MethodInfo;
					if (methodInfo3 == methodInfo)
					{
						list[i].opcode = OpCodes.Ldfld;
						list[i].operand = AccessTools.Field(typeof(SprayPaintItem), "sprayIntervalSpeed");
						list.Insert(i, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
						Plugin.Logger.LogDebug((object)"Transpiler (SprayPaintItem.TrySprayingWeedKillerBottle): Fix addVehicleHPInterval time");
					}
					else if (methodInfo3 == methodInfo2)
					{
						list[i].opcode = OpCodes.Ldsfld;
						list[i].operand = VEHICLE_CONTROLLER_INSTANCE;
						Plugin.Logger.LogDebug((object)"Transpiler (SprayPaintItem.TrySprayingWeedKillerBottle): Cache Cruiser script");
					}
				}
			}
			return list;
		}

		[HarmonyPatch(typeof(SprayPaintItem), "Start")]
		[HarmonyPatch(typeof(SprayPaintItem), "CheckForWeedsInSprayPath")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> CacheMoldSpreadManager(IEnumerable<CodeInstruction> instructions, MethodBase __originalMethod)
		{
			List<CodeInstruction> list = instructions.ToList();
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Call && list[i].operand as MethodInfo == FIND_OBJECT_OF_TYPE_MOLD_SPREAD_MANAGER)
				{
					list[i].operand = MOLD_SPREAD_MANAGER_INSTANCE;
					Plugin.Logger.LogDebug((object)$"Transpiler ({__originalMethod.DeclaringType}.{__originalMethod.Name}): Cache weed script");
				}
			}
			return list;
		}

		[HarmonyPatch(typeof(SprayPaintItem), "LateUpdate")]
		[HarmonyPatch(typeof(SprayPaintItem), "TrySprayingWeedKillerOnLocalPlayer")]
		[HarmonyPatch(typeof(SprayPaintItem), "CheckForCadaverPlantsInSprayPath")]
		[HarmonyPatch(typeof(SprayPaintItem), "KillCadaverPlantRpc")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> CacheCadaverGrowthAI(IEnumerable<CodeInstruction> instructions, MethodBase __originalMethod)
		{
			List<CodeInstruction> list = instructions.ToList();
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Call && list[i].operand as MethodInfo == FIND_OBJECT_OF_TYPE_CADAVER_GROWTH_AI)
				{
					list[i].opcode = OpCodes.Ldsfld;
					list[i].operand = CADAVER_GROWTH_AI_INSTANCE;
					Plugin.Logger.LogDebug((object)$"Transpiler ({__originalMethod.DeclaringType}.{__originalMethod.Name}): Cache Cadaver script");
				}
			}
			return list;
		}

		[HarmonyPatch(typeof(VehicleController), "Awake")]
		[HarmonyPostfix]
		private static void VehicleController_Post_Awake(VehicleController __instance)
		{
			if ((Object)(object)vehicleController == (Object)null)
			{
				vehicleController = __instance;
			}
		}

		[HarmonyPatch(typeof(CadaverGrowthAI), "Start")]
		[HarmonyPostfix]
		private static void CadaverGrowthAI_Post_Start(CadaverGrowthAI __instance)
		{
			if ((Object)(object)cadaverGrowthAI == (Object)null)
			{
				cadaverGrowthAI = __instance;
			}
		}

		[HarmonyPatch(typeof(SprayPaintItem), "LateUpdate")]
		[HarmonyPrefix]
		private static void SprayPaintItem_Pre_LateUpdate(SprayPaintItem __instance)
		{
			if (!__instance.isWeedKillerSprayBottle)
			{
				return;
			}
			if (__instance.killingCadaverPlant.Item1 != -1)
			{
				if ((Object)(object)__instance.cadaverGrowthAI == (Object)null && (Object)(object)cadaverGrowthAI != (Object)null)
				{
					__instance.cadaverGrowthAI = cadaverGrowthAI;
				}
				if ((Object)(object)__instance.cadaverGrowthAI == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy Cadavers but no Cadaver Growths AI exists.");
					__instance.killingCadaverPlant = (-1, -1, -1);
					return;
				}
				if (__instance.killingCadaverPlant.Item2 < 0 || __instance.killingCadaverPlant.Item3 < 0)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy an invalid Cadaver target");
					__instance.killingCadaverPlant = (-1, -1, -1);
					return;
				}
				if (__instance.cadaverGrowthAI.plantBatchers == null || __instance.cadaverGrowthAI.plantBatchers.Length <= __instance.killingCadaverPlant.Item3)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Cadaver with plant type out of range");
					__instance.killingCadaverPlant = (-1, -1, -1);
					return;
				}
				List<List<Matrix4x4>> list = __instance.cadaverGrowthAI.plantBatchers[__instance.killingCadaverPlant.Item3]?.Batches;
				if (list == null || list.Count <= __instance.killingCadaverPlant.Item1)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Cadaver with batch ID out of range");
					__instance.killingCadaverPlant = (-1, -1, -1);
					return;
				}
				List<Matrix4x4> list2 = list[__instance.killingCadaverPlant.Item1];
				if (list2 == null || list2.Count <= __instance.killingCadaverPlant.Item2)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Cadaver with positional index out of range");
					__instance.killingCadaverPlant = (-1, -1, -1);
				}
			}
			else
			{
				if (__instance.killingWeed.Item1 == -1)
				{
					return;
				}
				if ((Object)(object)__instance.moldManager == (Object)null && (Object)(object)moldSpreadManager != (Object)null)
				{
					__instance.moldManager = moldSpreadManager;
				}
				if ((Object)(object)__instance.moldManager == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy Vains but no mold spread manager exists.");
					__instance.killingWeed = (-1, -1);
					return;
				}
				if (__instance.killingWeed.Item2 < 0)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy an invalid Vain target");
					__instance.killingWeed = (-1, -1);
					return;
				}
				if ((Object)(object)__instance.moldManager.grassInstancer == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Vain but no instancer could be found");
					__instance.killingWeed = (-1, -1);
					return;
				}
				List<List<Matrix4x4>> batches = __instance.moldManager.grassInstancer.Batches;
				if (batches == null || batches.Count <= __instance.killingWeed.Item1)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Vain with batch ID out of range");
					__instance.killingWeed = (-1, -1);
					return;
				}
				List<Matrix4x4> list3 = batches[__instance.killingWeed.Item1];
				if (list3 == null || list3.Count <= __instance.killingWeed.Item2)
				{
					Plugin.Logger.LogWarning((object)"Weed killer is trying to destroy a Vain with positional index out of range");
					__instance.killingWeed = (-1, -1);
				}
			}
		}

		[HarmonyPatch(typeof(CadaverGrowthAI), "RemoveWeedFromTile")]
		[HarmonyPrefix]
		private static bool CadaverGrowthAI_Pre_RemoveWeedFromTile(CadaverGrowthAI __instance, int tileIndex)
		{
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Expected O, but got Unknown
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.GrowthTiles == null || __instance.GrowthTiles.Count <= tileIndex || __instance.GrowthTiles[tileIndex] == null)
			{
				Plugin.Logger.LogError((object)"Cadaver Growths trying to remove Cadaver from an invalid tile (expect problems!)");
				return false;
			}
			RoundManager instance = RoundManager.Instance;
			object obj;
			if (instance == null)
			{
				obj = null;
			}
			else
			{
				GameObject mapPropsContainer = instance.mapPropsContainer;
				obj = ((mapPropsContainer != null) ? mapPropsContainer.transform : null);
			}
			Transform val = (Transform)obj;
			Tile tile = __instance.GrowthTiles[tileIndex].tile;
			if ((Object)(object)val != (Object)null && (Object)(object)tile != (Object)null)
			{
				Bounds bounds;
				if ((Object)(object)__instance.GrowthTiles[tileIndex].particlesContainer == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths trying to remove Cadaver from a tile that is missing a particles container");
					__instance.GrowthTiles[tileIndex].particlesContainer = new GameObject("SporesContainer");
					Transform transform = __instance.GrowthTiles[tileIndex].particlesContainer.transform;
					bounds = tile.Bounds;
					transform.position = ((Bounds)(ref bounds)).center;
					__instance.GrowthTiles[tileIndex].particlesContainer.transform.SetParent(val);
					Plugin.Logger.LogDebug((object)"Created a backup particles container for affected tile");
				}
				if ((Object)(object)__instance.GrowthTiles[tileIndex].scanNodesContainer == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths trying to remove Cadaver from a tile that is missing a scan node container");
					__instance.GrowthTiles[tileIndex].scanNodesContainer = new GameObject("ScanNodesContainer");
					Transform transform2 = __instance.GrowthTiles[tileIndex].scanNodesContainer.transform;
					bounds = tile.Bounds;
					transform2.position = ((Bounds)(ref bounds)).center;
					__instance.GrowthTiles[tileIndex].scanNodesContainer.transform.SetParent(val);
					Plugin.Logger.LogDebug((object)"Created a backup scan node container for affected tile");
				}
			}
			else
			{
				if ((Object)(object)__instance.GrowthTiles[tileIndex].particlesContainer == (Object)null)
				{
					Plugin.Logger.LogError((object)"Cadaver Growths trying to remove Cadaver from a tile that is missing a particles container (expect problems!)");
					return false;
				}
				if ((Object)(object)__instance.GrowthTiles[tileIndex].scanNodesContainer == (Object)null)
				{
					Plugin.Logger.LogError((object)"Cadaver Growths trying to remove Cadaver from a tile that is missing a scan node container (expect problems!)");
					return false;
				}
			}
			if (__instance.growingRecentPlant && __instance.growingPlant.Item1 != -1)
			{
				if (__instance.growingPlant.Item2 < 0 || __instance.growingPlant.Item3 < 0)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths are trying to grow an invalid Cadaver target");
					__instance.growingPlant = (-1, -1, -1);
					__instance.growingRecentPlant = false;
					return true;
				}
				if (__instance.plantBatchers == null || __instance.plantBatchers.Length <= __instance.growingPlant.Item3)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths are trying to grow a Cadaver with plant type out of range");
					__instance.growingPlant = (-1, -1, -1);
					__instance.growingRecentPlant = false;
					return true;
				}
				List<List<Matrix4x4>> list = __instance.plantBatchers[__instance.growingPlant.Item3]?.Batches;
				if (list == null || list.Count <= __instance.growingPlant.Item1)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths are trying to grow a Cadaver with batch ID out of range");
					__instance.growingPlant = (-1, -1, -1);
					__instance.growingRecentPlant = false;
					return true;
				}
				List<Matrix4x4> list2 = list[__instance.growingPlant.Item1];
				if (list2 == null || list2.Count <= __instance.growingPlant.Item2)
				{
					Plugin.Logger.LogWarning((object)"Cadaver Growths are trying to grow a Cadaver with positional index out of range");
					__instance.growingPlant = (-1, -1, -1);
					__instance.growingRecentPlant = false;
					return true;
				}
			}
			return true;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "WeedKillerFixes";

		public const string PLUGIN_NAME = "WeedKillerFixes";

		public const string PLUGIN_VERSION = "1.2.1";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}