Decompiled source of MassFarming v1.13.0

Xeio-MassFarming/MassFarming.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Splatform;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MassFarming")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MassFarming")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e1da477d-c14c-4355-be8f-c043e789791a")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MassFarming;

[BepInPlugin("xeio.MassFarming", "MassFarming", "1.13")]
public class MassFarming : BaseUnityPlugin
{
	public static ConfigEntry<KeyboardShortcut> MassActionHotkey { get; private set; }

	public static ConfigEntry<KeyboardShortcut> ControllerPickupHotkey { get; private set; }

	public static ConfigEntry<float> MassInteractRange { get; private set; }

	public static ConfigEntry<int> PlantGridWidth { get; private set; }

	public static ConfigEntry<int> PlantGridLength { get; private set; }

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

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

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

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

	public void Awake()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		MassActionHotkey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "MassActionHotkey", new KeyboardShortcut((KeyCode)304, Array.Empty<KeyCode>()), "Mass activation hotkey for multi-pickup/multi-plant.");
		ControllerPickupHotkey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "ControllerPickupHotkey", new KeyboardShortcut((KeyCode)334, Array.Empty<KeyCode>()), "Mass activation hotkey for multi-pickup/multi-plant for controller.");
		MassInteractRange = ((BaseUnityPlugin)this).Config.Bind<float>("Pickup", "MassInteractRange", 5f, "Range of auto-pickup.");
		PlantGridWidth = ((BaseUnityPlugin)this).Config.Bind<int>("PlantGrid", "PlantGridWidth", 5, "Grid width of auto-plant. Recommend odd-number, default is '5' so (5x5).");
		PlantGridLength = ((BaseUnityPlugin)this).Config.Bind<int>("PlantGrid", "PlantGridLength", 5, "Grid length of auto-plant. Recommend odd-number, default is '5' so (5x5).");
		IgnoreStamina = ((BaseUnityPlugin)this).Config.Bind<bool>("Plant", "IgnoreStamina", false, "Ignore stamina requirements when planting extra rows.");
		IgnoreDurability = ((BaseUnityPlugin)this).Config.Bind<bool>("Plant", "IgnoreDurability", false, "Ignore durability when planting extra rows.");
		GridAnchorWidth = ((BaseUnityPlugin)this).Config.Bind<bool>("PlantGrid", "GridAnchorWidth", true, "Planting grid anchor point (width). Default is 'enabled' so the grid will extend left and right from the crosshairs. Disable to set the anchor to the side of the grid. Disable both anchors to set to corner.");
		GridAnchorLength = ((BaseUnityPlugin)this).Config.Bind<bool>("PlantGrid", "GridAnchorLength", true, "Planting grid anchor point (length). Default is 'enabled' so the grid will extend forward and backward from the crosshairs. Disable to set the anchor to the side of the grid. Disable both anchors to set to corner.");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}
}
[HarmonyPatch]
public static class MassPickup
{
	private static FieldInfo m_interactMaskField = AccessTools.Field(typeof(Player), "m_interactMask");

	private static MethodInfo _ExtractMethod = AccessTools.Method(typeof(Beehive), "Extract", (Type[])null, (Type[])null);

	[HarmonyPatch(typeof(Player), "Interact")]
	public static void Prefix(Player __instance, GameObject go, bool hold, bool alt)
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		if (((Character)__instance).InAttack() || ((Character)__instance).InDodge() || hold)
		{
			return;
		}
		KeyboardShortcut value = MassFarming.ControllerPickupHotkey.Value;
		if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
		{
			value = MassFarming.MassActionHotkey.Value;
			if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
			{
				return;
			}
		}
		Interactable componentInParent = go.GetComponentInParent<Interactable>();
		Pickable val = (Pickable)(object)((componentInParent is Pickable) ? componentInParent : null);
		if (val != null)
		{
			int num = (int)m_interactMaskField.GetValue(__instance);
			Collider[] array = Physics.OverlapSphere(go.transform.position, MassFarming.MassInteractRange.Value, num);
			Collider[] array2 = array;
			foreach (Collider val2 in array2)
			{
				object obj;
				if (val2 == null)
				{
					obj = null;
				}
				else
				{
					GameObject gameObject = ((Component)val2).gameObject;
					obj = ((gameObject != null) ? gameObject.GetComponentInParent<Pickable>() : null);
				}
				Pickable val3 = (Pickable)obj;
				if (val3 != null && (Object)(object)val3 != (Object)(object)val && ((Object)val3.m_itemPrefab).name == ((Object)val.m_itemPrefab).name)
				{
					val3.Interact((Humanoid)(object)__instance, false, alt);
				}
			}
			return;
		}
		Beehive val4 = (Beehive)(object)((componentInParent is Beehive) ? componentInParent : null);
		if (val4 == null)
		{
			return;
		}
		int num2 = (int)m_interactMaskField.GetValue(__instance);
		Collider[] array3 = Physics.OverlapSphere(go.transform.position, MassFarming.MassInteractRange.Value, num2);
		Collider[] array4 = array3;
		foreach (Collider val5 in array4)
		{
			object obj2;
			if (val5 == null)
			{
				obj2 = null;
			}
			else
			{
				GameObject gameObject2 = ((Component)val5).gameObject;
				obj2 = ((gameObject2 != null) ? gameObject2.GetComponentInParent<Beehive>() : null);
			}
			Beehive val6 = (Beehive)obj2;
			if (val6 != null && (Object)(object)val6 != (Object)(object)val4 && PrivateArea.CheckAccess(((Component)val6).transform.position, 0f, true, false))
			{
				_ExtractMethod.Invoke(val6, null);
			}
		}
	}
}
[HarmonyPatch]
public static class MassPlant
{
	private static readonly FieldInfo m_noPlacementCostField = AccessTools.Field(typeof(Player), "m_noPlacementCost");

	private static readonly FieldInfo m_placementGhostField = AccessTools.Field(typeof(Player), "m_placementGhost");

	private static readonly FieldInfo m_buildPiecesField = AccessTools.Field(typeof(Player), "m_buildPieces");

	private static readonly MethodInfo _GetRightItemMethod = AccessTools.Method(typeof(Humanoid), "GetRightItem", (Type[])null, (Type[])null);

	private static Vector3 placedPosition;

	private static Quaternion placedRotation;

	private static Piece placedPiece;

	private static bool placeSuccessful = false;

	private static int? massFarmingRotation = null;

	private static readonly int _plantSpaceMask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid" });

	private static GameObject[] _placementGhosts = (GameObject[])(object)new GameObject[1];

	private static Piece _fakeResourcePiece;

	private static bool IsHotKeyPressed
	{
		get
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = MassFarming.ControllerPickupHotkey.Value;
			int result;
			if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
			{
				value = MassFarming.MassActionHotkey.Value;
				result = (Input.GetKey(((KeyboardShortcut)(ref value)).MainKey) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "TryPlacePiece")]
	public static void TryPlacePiecePrefix(int ___m_placeRotation)
	{
		if (IsHotKeyPressed)
		{
			int? num = massFarmingRotation;
			if (!num.HasValue)
			{
				massFarmingRotation = ___m_placeRotation;
			}
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Player), "TryPlacePiece")]
	public static void TryPlacePiecePostfix(Player __instance, ref bool __result, Piece piece, ref int ___m_placeRotation)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Expected O, but got Unknown
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		placeSuccessful = __result;
		if (__result)
		{
			GameObject val = (GameObject)m_placementGhostField.GetValue(__instance);
			placedPosition = val.transform.position;
			placedRotation = val.transform.rotation;
			placedPiece = piece;
		}
		if (IsHotKeyPressed)
		{
			___m_placeRotation = massFarmingRotation.Value;
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "UpdatePlacement")]
	public static void UpdatePlacementPrefix(bool takeInput, float dt, ref int ___m_placeRotation)
	{
		placeSuccessful = false;
		if (IsHotKeyPressed && massFarmingRotation.HasValue)
		{
			___m_placeRotation = massFarmingRotation.Value;
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Player), "UpdatePlacement")]
	public static void UpdatePlacementPostfix(Player __instance, bool takeInput, float dt, int ___m_placeRotation)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		//IL_022d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0298: Unknown result type (might be due to invalid IL or missing references)
		if (IsHotKeyPressed)
		{
			massFarmingRotation = ___m_placeRotation;
		}
		if (!placeSuccessful)
		{
			return;
		}
		Plant component = ((Component)placedPiece).gameObject.GetComponent<Plant>();
		if (!Object.op_Implicit((Object)(object)component) || !IsHotKeyPressed)
		{
			return;
		}
		Heightmap val = Heightmap.FindHeightmap(placedPosition);
		if (!Object.op_Implicit((Object)(object)val))
		{
			return;
		}
		object? value = m_buildPiecesField.GetValue(__instance);
		PieceTable val2 = (PieceTable)((value is PieceTable) ? value : null);
		foreach (Vector3 item in BuildPlantingGridPositions(placedPosition, component, placedRotation))
		{
			if ((placedPiece.m_cultivatedGroundOnly && !val.IsCultivated(item)) || placedPosition == item)
			{
				continue;
			}
			object? obj = _GetRightItemMethod.Invoke(__instance, Array.Empty<object>());
			ItemData val3 = (ItemData)((obj is ItemData) ? obj : null);
			if (val3 == null)
			{
				continue;
			}
			if (!MassFarming.IgnoreStamina.Value && !((Character)__instance).HaveStamina(val3.m_shared.m_attack.m_attackStamina))
			{
				Hud.instance.StaminaBarUppgradeFlash();
				break;
			}
			if (!(bool)m_noPlacementCostField.GetValue(__instance) && !__instance.HaveRequirements(placedPiece, (RequirementMode)0))
			{
				break;
			}
			if (!HasGrowSpace(item, ((Component)placedPiece).gameObject))
			{
				continue;
			}
			GameObject val4 = Object.Instantiate<GameObject>(((Component)placedPiece).gameObject, item, placedRotation);
			Piece component2 = val4.GetComponent<Piece>();
			if (Object.op_Implicit((Object)(object)component2))
			{
				component2.SetCreator(__instance.GetPlayerID(), ((IUser)PlatformManager.DistributionPlatform.LocalUser).PlatformUserID);
			}
			placedPiece.m_placeEffect.Create(item, placedRotation, val4.transform, 1f, -1, default(ZDOID));
			Game.instance.IncrementPlayerStat((PlayerStatType)2, 1f, false);
			__instance.ConsumeResources(placedPiece.m_resources, 0, -1, 1);
			if (!MassFarming.IgnoreStamina.Value)
			{
				((Character)__instance).UseStamina(val3.m_shared.m_attack.m_attackStamina);
			}
			if ((Object)(object)val2 != (Object)null)
			{
				((Character)__instance).RaiseSkill(val2.m_skill, 1f);
			}
			if (!MassFarming.IgnoreDurability.Value && val3.m_shared.m_useDurability)
			{
				val3.m_durability -= val3.m_shared.m_useDurabilityDrain;
				if (val3.m_durability <= 0f)
				{
					break;
				}
			}
		}
	}

	private static List<Vector3> BuildPlantingGridPositions(Vector3 originPos, Plant placedPlant, Quaternion rotation)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: 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)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		float num = placedPlant.m_growRadius * 2f;
		List<Vector3> list = new List<Vector3>(MassFarming.PlantGridWidth.Value * MassFarming.PlantGridLength.Value);
		Vector3 val = rotation * Vector3.left * num;
		Vector3 val2 = rotation * Vector3.forward * num;
		Vector3 val3 = originPos;
		if (MassFarming.GridAnchorLength.Value)
		{
			val3 -= val2 * (float)(MassFarming.PlantGridLength.Value / 2);
		}
		if (MassFarming.GridAnchorWidth.Value)
		{
			val3 -= val * (float)(MassFarming.PlantGridWidth.Value / 2);
		}
		for (int i = 0; i < MassFarming.PlantGridLength.Value; i++)
		{
			Vector3 val4 = val3;
			for (int j = 0; j < MassFarming.PlantGridWidth.Value; j++)
			{
				val4.y = ZoneSystem.instance.GetGroundHeight(val4);
				list.Add(val4);
				val4 += val;
			}
			val3 += val2;
		}
		return list;
	}

	private static bool HasGrowSpace(Vector3 newPos, GameObject go)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		Plant component = go.GetComponent<Plant>();
		if (component != null)
		{
			Collider[] array = Physics.OverlapSphere(newPos, component.m_growRadius, _plantSpaceMask);
			return array.Length == 0;
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "SetupPlacementGhost")]
	public static void SetupPlacementGhostPrefix(Player __instance, int ___m_placeRotation)
	{
		if (IsHotKeyPressed)
		{
			int? num = massFarmingRotation;
			if (!num.HasValue)
			{
				massFarmingRotation = ___m_placeRotation;
			}
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Player), "SetupPlacementGhost")]
	public static void SetupPlacementGhostPostfix(Player __instance, ref int ___m_placeRotation)
	{
		if (IsHotKeyPressed && massFarmingRotation.HasValue)
		{
			___m_placeRotation = massFarmingRotation.Value;
		}
		DestroyGhosts();
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
	public static void UpdatePlacementGhostPostfix(Player __instance, bool flashGuardStone)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0207: Unknown result type (might be due to invalid IL or missing references)
		//IL_0233: Unknown result type (might be due to invalid IL or missing references)
		//IL_0250: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = (GameObject)m_placementGhostField.GetValue(__instance);
		if (!Object.op_Implicit((Object)(object)val) || !val.activeSelf)
		{
			SetGhostsActive(active: false);
			return;
		}
		KeyboardShortcut value = MassFarming.ControllerPickupHotkey.Value;
		if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
		{
			value = MassFarming.MassActionHotkey.Value;
			if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
			{
				SetGhostsActive(active: false);
				return;
			}
		}
		Plant component = val.GetComponent<Plant>();
		if (!Object.op_Implicit((Object)(object)component))
		{
			SetGhostsActive(active: false);
			return;
		}
		if (!EnsureGhostsBuilt(__instance))
		{
			SetGhostsActive(active: false);
			return;
		}
		Requirement val2 = ((IEnumerable<Requirement>)val.GetComponent<Piece>().m_resources).FirstOrDefault((Func<Requirement, bool>)((Requirement r) => Object.op_Implicit((Object)(object)r.m_resItem) && r.m_amount > 0));
		_fakeResourcePiece.m_resources[0].m_resItem = val2.m_resItem;
		_fakeResourcePiece.m_resources[0].m_amount = val2.m_amount;
		float num = __instance.GetStamina();
		object? obj = _GetRightItemMethod.Invoke(__instance, Array.Empty<object>());
		ItemData val3 = (ItemData)((obj is ItemData) ? obj : null);
		if (val3 == null)
		{
			return;
		}
		Heightmap val4 = Heightmap.FindHeightmap(val.transform.position);
		List<Vector3> list = BuildPlantingGridPositions(val.transform.position, component, val.transform.rotation);
		for (int num2 = 0; num2 < _placementGhosts.Length; num2++)
		{
			Vector3 val5 = list[num2];
			if (val.transform.position == val5)
			{
				_placementGhosts[num2].SetActive(false);
				continue;
			}
			Requirement obj2 = _fakeResourcePiece.m_resources[0];
			obj2.m_amount += val2.m_amount;
			_placementGhosts[num2].transform.position = val5;
			_placementGhosts[num2].transform.rotation = val.transform.rotation;
			_placementGhosts[num2].SetActive(true);
			bool invalidPlacementHeightlight = false;
			if (val.GetComponent<Piece>().m_cultivatedGroundOnly && !val4.IsCultivated(val5))
			{
				invalidPlacementHeightlight = true;
			}
			else if (!HasGrowSpace(val5, val.gameObject))
			{
				invalidPlacementHeightlight = true;
			}
			else if (!MassFarming.IgnoreStamina.Value && num < val3.m_shared.m_attack.m_attackStamina)
			{
				Hud.instance.StaminaBarUppgradeFlash();
				invalidPlacementHeightlight = true;
			}
			else if (!(bool)m_noPlacementCostField.GetValue(__instance) && !__instance.HaveRequirements(_fakeResourcePiece, (RequirementMode)0))
			{
				invalidPlacementHeightlight = true;
			}
			num -= val3.m_shared.m_attack.m_attackStamina;
			_placementGhosts[num2].GetComponent<Piece>().SetInvalidPlacementHeightlight(invalidPlacementHeightlight);
		}
	}

	private static bool EnsureGhostsBuilt(Player player)
	{
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Expected O, but got Unknown
		int num = MassFarming.PlantGridWidth.Value * MassFarming.PlantGridLength.Value;
		if (!Object.op_Implicit((Object)(object)_placementGhosts[0]) || _placementGhosts.Length != num)
		{
			DestroyGhosts();
			if (_placementGhosts.Length != num)
			{
				_placementGhosts = (GameObject[])(object)new GameObject[num];
			}
			object? value = m_buildPiecesField.GetValue(player);
			PieceTable val = (PieceTable)((value is PieceTable) ? value : null);
			if (val != null)
			{
				GameObject selectedPrefab = val.GetSelectedPrefab();
				if (selectedPrefab != null)
				{
					if (selectedPrefab.GetComponent<Piece>().m_repairPiece)
					{
						return false;
					}
					for (int i = 0; i < _placementGhosts.Length; i++)
					{
						_placementGhosts[i] = SetupMyGhost(player, selectedPrefab);
					}
					goto IL_00e3;
				}
			}
			return false;
		}
		goto IL_00e3;
		IL_00e3:
		if (!Object.op_Implicit((Object)(object)_fakeResourcePiece))
		{
			_fakeResourcePiece = _placementGhosts[0].GetComponent<Piece>();
			_fakeResourcePiece.m_dlc = string.Empty;
			_fakeResourcePiece.m_resources = (Requirement[])(object)new Requirement[1]
			{
				new Requirement()
			};
		}
		return true;
	}

	private static void DestroyGhosts()
	{
		for (int i = 0; i < _placementGhosts.Length; i++)
		{
			if (Object.op_Implicit((Object)(object)_placementGhosts[i]))
			{
				Object.Destroy((Object)(object)_placementGhosts[i]);
				_placementGhosts[i] = null;
			}
		}
		_fakeResourcePiece = null;
	}

	private static void SetGhostsActive(bool active)
	{
		GameObject[] placementGhosts = _placementGhosts;
		foreach (GameObject val in placementGhosts)
		{
			if (val != null)
			{
				val.SetActive(active);
			}
		}
	}

	private static GameObject SetupMyGhost(Player player, GameObject prefab)
	{
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Expected O, but got Unknown
		ZNetView.m_forceDisableInit = true;
		GameObject val = Object.Instantiate<GameObject>(prefab);
		ZNetView.m_forceDisableInit = false;
		((Object)val).name = ((Object)prefab).name;
		Joint[] componentsInChildren = val.GetComponentsInChildren<Joint>();
		foreach (Joint val2 in componentsInChildren)
		{
			Object.Destroy((Object)(object)val2);
		}
		Rigidbody[] componentsInChildren2 = val.GetComponentsInChildren<Rigidbody>();
		foreach (Rigidbody val3 in componentsInChildren2)
		{
			Object.Destroy((Object)(object)val3);
		}
		int layer = LayerMask.NameToLayer("ghost");
		Transform[] componentsInChildren3 = val.GetComponentsInChildren<Transform>();
		foreach (Transform val4 in componentsInChildren3)
		{
			((Component)val4).gameObject.layer = layer;
		}
		TerrainModifier[] componentsInChildren4 = val.GetComponentsInChildren<TerrainModifier>();
		foreach (TerrainModifier val5 in componentsInChildren4)
		{
			Object.Destroy((Object)(object)val5);
		}
		GuidePoint[] componentsInChildren5 = val.GetComponentsInChildren<GuidePoint>();
		foreach (GuidePoint val6 in componentsInChildren5)
		{
			Object.Destroy((Object)(object)val6);
		}
		Light[] componentsInChildren6 = val.GetComponentsInChildren<Light>();
		foreach (Light val7 in componentsInChildren6)
		{
			Object.Destroy((Object)(object)val7);
		}
		Transform val8 = val.transform.Find("_GhostOnly");
		if (Object.op_Implicit((Object)(object)val8))
		{
			((Component)val8).gameObject.SetActive(true);
		}
		MeshRenderer[] componentsInChildren7 = val.GetComponentsInChildren<MeshRenderer>();
		foreach (MeshRenderer val9 in componentsInChildren7)
		{
			if (!((Object)(object)((Renderer)val9).sharedMaterial == (Object)null))
			{
				Material[] sharedMaterials = ((Renderer)val9).sharedMaterials;
				for (int num2 = 0; num2 < sharedMaterials.Length; num2++)
				{
					Material val10 = new Material(sharedMaterials[num2]);
					val10.SetFloat("_RippleDistance", 0f);
					val10.SetFloat("_ValueNoise", 0f);
					sharedMaterials[num2] = val10;
				}
				((Renderer)val9).sharedMaterials = sharedMaterials;
				((Renderer)val9).shadowCastingMode = (ShadowCastingMode)0;
			}
		}
		return val;
	}
}