Decompiled source of FarmGrid v1.0.0

FarmGrid.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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("FarmGrid")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FarmGrid")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("26c187af-1b0d-4b17-9627-fa0c8d81c65b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace FarmGrid;

[BepInPlugin("fr.galathil.FarmGrid", "FarmGrid", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[HarmonyPatch]
public class FarmGrid : BaseUnityPlugin
{
	public const string pluginId = "fr.galathil.FarmGrid";

	public const string pluginName = "FarmGrid";

	public const string pluginVersion = "1.0.0";

	private Harmony _harmony;

	private static ManualLogSource _log;

	private ConfigEntry<float> plantSpacing;

	private ConfigEntry<int> farmGridSections;

	private ConfigEntry<float> farmGridYOffset;

	private ConfigEntry<Color> farmGridColor;

	private ConfigEntry<string> plantObjectMasks;

	private ConfigEntry<string> customPlants;

	private ConfigEntry<int> farmGridFixedRotation;

	private void Awake()
	{
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "fr.galathil.FarmGrid");
		_log = ((BaseUnityPlugin)this).Logger;
		farmGridFixedRotation = ((BaseUnityPlugin)this).Config.Bind<int>("FarmGrid", "farmGridFixedRotation", FarmGrid_Patch.farmGridFixedRotation, "Fixed rotation in degrees, 0 is no fixed rotation (if you want 0 to be the fixed rotation enter 360)");
		plantSpacing = ((BaseUnityPlugin)this).Config.Bind<float>("FarmGrid", "plantSpacing", FarmGrid_Patch.plantSpacing, "Amount of space between plants. This is extra spacing on top of the growthsize needed to grow.");
		farmGridSections = ((BaseUnityPlugin)this).Config.Bind<int>("FarmGrid", "farmGridSections", FarmGrid_Patch.farmGridSections, "Amount of grid sections (on either side of the main axis.)");
		farmGridYOffset = ((BaseUnityPlugin)this).Config.Bind<float>("FarmGrid", "farmGridYOffset", FarmGrid_Patch.farmGridYOffset, "Grid offset from the ground.");
		farmGridColor = ((BaseUnityPlugin)this).Config.Bind<Color>("FarmGrid", "farmGridColor", FarmGrid_Patch.farmGridColor, "Color of the grid.");
		plantObjectMasks = ((BaseUnityPlugin)this).Config.Bind<string>("FarmGrid", "plantObjectMasks", GeneralExtensions.Join<string>((IEnumerable<string>)FarmGrid_Patch.plantObjectMasks, (Func<string, string>)null, ", "), "Masks used by overlapping detection, if you add custom plants that are not in the normal category you may want to add more flags here.\nif you don't know what you are doing, don't touch this.\n\nUse this for Planting Plus:plantObjectMasks = piece,piece_nonsolid,item");
		customPlants = ((BaseUnityPlugin)this).Config.Bind<string>("FarmGrid", "customPlants", "", "Plants that are not actually plants (objects without a plant component), their name and size.\n\nUse this for Planting Plus:\ncustomPlants = RaspberryBush(Clone): 0.5, RaspberryBush: 0.5, BlueberryBush(Clone): 0.5, BlueberryBush: 0.5, Pickable_Mushroom_yellow(Clone): 0.5, Pickable_Mushroom_yellow: 0.5, Pickable_Mushroom_blue(Clone): 0.5, Pickable_Mushroom_blue: 0.5, CloudberryBush: 0.5, CloudberryBush(Clone): 0.5, Pickable_Thistle(Clone): 0.5, Pickable_Thistle: 0.5, Pickable_Dandelion(Clone): 0.5, Pickable_Dandelion: 0.5");
		farmGridFixedRotation.SettingChanged += FarmGridFixedRotation_SettingChanged;
		plantSpacing.SettingChanged += PlantSpacing_SettingChanged;
		farmGridSections.SettingChanged += FarmGridSections_SettingChanged;
		farmGridYOffset.SettingChanged += FarmGridYOffset_SettingChanged;
		farmGridColor.SettingChanged += FarmGridColor_SettingChanged;
		plantObjectMasks.SettingChanged += PlantObjectMasks_SettingChanged;
		customPlants.SettingChanged += CustomPlants_SettingChanged;
		SettingsChanged();
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	private void FarmGridFixedRotation_SettingChanged(object sender, EventArgs e)
	{
		FarmGrid_Patch.farmGridFixedRotation = farmGridFixedRotation.Value;
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void SettingsChanged()
	{
		FarmGridFixedRotation_SettingChanged(null, null);
		PlantSpacing_SettingChanged(null, null);
		FarmGridSections_SettingChanged(null, null);
		FarmGridYOffset_SettingChanged(null, null);
		FarmGridColor_SettingChanged(null, null);
		PlantObjectMasks_SettingChanged(null, null);
		CustomPlants_SettingChanged(null, null);
	}

	private void CustomPlants_SettingChanged(object sender, EventArgs e)
	{
		FarmGrid_Patch.customPlants.Clear();
		string value = customPlants.Value;
		char[] separator = new char[1] { ',' };
		string[] array = value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		foreach (string text in array)
		{
			string[] separator2 = new string[1] { ":" };
			string[] array2 = text.Split(separator2, StringSplitOptions.RemoveEmptyEntries);
			if (array2.Length == 2 && float.TryParse(array2[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var result))
			{
				FarmGrid_Patch.customPlants.Add(array2[0].Trim(), result);
			}
		}
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void PlantObjectMasks_SettingChanged(object sender, EventArgs e)
	{
		string[] array = plantObjectMasks.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
		List<string> list = new List<string>();
		string[] array2 = array;
		foreach (string text in array2)
		{
			list.Add(text.Trim());
		}
		FarmGrid_Patch.plantObjectMasks = list;
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void FarmGridColor_SettingChanged(object sender, EventArgs e)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		FarmGrid_Patch.farmGridColor = farmGridColor.Value;
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void FarmGridYOffset_SettingChanged(object sender, EventArgs e)
	{
		FarmGrid_Patch.farmGridYOffset = farmGridYOffset.Value;
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void FarmGridSections_SettingChanged(object sender, EventArgs e)
	{
		FarmGrid_Patch.farmGridSections = farmGridSections.Value;
		FarmGrid_Patch.DestroyFarmGrid();
	}

	private void PlantSpacing_SettingChanged(object sender, EventArgs e)
	{
		FarmGrid_Patch.plantSpacing = plantSpacing.Value;
		FarmGrid_Patch.DestroyFarmGrid();
	}
}
[HarmonyPatch]
internal class FarmGrid_Patch
{
	private class PlantObject
	{
		public Vector3 position;

		public float growthSize;

		public float colliderRadius = 0f;

		public PlantObject(Vector3 position, float growthSize, Collider collider)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			this.position = position;
			this.growthSize = growthSize;
			if ((Object)(object)collider != (Object)null && collider is CapsuleCollider)
			{
				colliderRadius = ((CapsuleCollider)collider).radius;
			}
		}
	}

	public static float plantSpacing = 0.01f;

	public static int farmGridSections = 2;

	public static float farmGridYOffset = 0.2f;

	public static Color farmGridColor = new Color(0.8f, 0.8f, 0.8f, 0.2f);

	public static Dictionary<string, float> customPlants = new Dictionary<string, float>();

	public static List<string> plantObjectMasks = new List<string> { "piece", "piece_nonsolid" };

	private static Vector3 plantSnapPoint;

	private static GameObject[] farmGrid = null;

	private static bool farmGridVisible = false;

	private static Vector3 plantGhostPosition;

	private static Vector3 otherPlantCollider;

	private static List<PlantObject> otherPlantList;

	private static int plantObjectMask;

	private static Material lineMaterial;

	internal static int farmGridFixedRotation;

	[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> UpdatePlacementGhost(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Expected O, but got Unknown
		List<CodeInstruction> list = instructions.ToList();
		int num = 0;
		bool flag = false;
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].opcode == OpCodes.Brtrue)
			{
				num = i;
			}
			if (CodeInstructionExtensions.Calls(list[i], AccessTools.Method(typeof(Player), "FindClosestSnapPoints", (Type[])null, (Type[])null)))
			{
				list.InsertRange(num + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[2]
				{
					new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(FarmGrid_Patch), "GetFarmSnapPoints", (Type[])null, (Type[])null)),
					new CodeInstruction(OpCodes.Brtrue, list[num].operand)
				});
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			ZLog.LogWarning((object)"FarmGrid: Failed to patch UpdatePlacementGhost");
		}
		return list;
	}

	[HarmonyPatch(typeof(Player), "PlacePiece")]
	[HarmonyPrefix]
	public static void PlacePiece(Piece piece, Vector3 pos, Quaternion rot, bool doAttack = true)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (!GetPlantObject(Player.m_localPlayer.m_placementGhost.GetComponent<Collider>(), out var plantObject) || HasOverlappingPlants(plantObject.position, plantObject.growthSize))
		{
		}
		otherPlantCollider = Vector3.zero;
	}

	[HarmonyPatch(typeof(Humanoid), "SetupVisEquipment")]
	[HarmonyPostfix]
	private static void SetupVisEquipment(ref Humanoid __instance)
	{
		if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer && (!Object.op_Implicit((Object)(object)Player.m_localPlayer) || ((Humanoid)Player.m_localPlayer).m_rightItem == null || ((Humanoid)Player.m_localPlayer).m_rightItem.m_shared == null || ((Humanoid)Player.m_localPlayer).m_rightItem.m_shared.m_name != "$item_cultivator"))
		{
			HideFarmGrid();
		}
	}

	private static void DrawFarmGrid(Vector3 pos, Vector3 gridDir, float gridSize)
	{
		//IL_0024: 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_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: 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)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: 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_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
		if (farmGrid == null || (Object)(object)farmGrid[0] == (Object)null)
		{
			InitFarmGrid();
		}
		Vector3 val = default(Vector3);
		((Vector3)(ref val))..ctor(gridDir.z, gridDir.y, 0f - gridDir.x);
		for (int i = -farmGridSections; i <= farmGridSections; i++)
		{
			Vector3 val2 = pos + gridDir * (float)i * gridSize;
			GameObject val3 = farmGrid[i + farmGridSections];
			val3.transform.position = val2;
			LineRenderer component = val3.GetComponent<LineRenderer>();
			component.widthMultiplier = 0.015f;
			((Renderer)component).enabled = true;
			for (int j = -farmGridSections; j <= farmGridSections; j++)
			{
				Vector3 val4 = val2 + val * (float)j * gridSize;
				float groundHeight = GetGroundHeight(val4);
				val4.y = groundHeight + farmGridYOffset;
				component.SetPosition(j + farmGridSections, val4);
			}
		}
		for (int k = -farmGridSections; k <= farmGridSections; k++)
		{
			Vector3 val5 = pos + val * (float)k * gridSize;
			GameObject val6 = farmGrid[k + farmGridSections + (farmGridSections * 2 + 1)];
			val6.transform.position = val5;
			LineRenderer component2 = val6.GetComponent<LineRenderer>();
			component2.widthMultiplier = 0.015f;
			((Renderer)component2).enabled = true;
			for (int l = -farmGridSections; l <= farmGridSections; l++)
			{
				Vector3 val7 = val5 + gridDir * (float)l * gridSize;
				float groundHeight2 = GetGroundHeight(val7);
				val7.y = groundHeight2 + farmGridYOffset;
				component2.SetPosition(l + farmGridSections, val7);
			}
		}
		farmGridVisible = true;
	}

	private static float GetGroundHeight(Vector3 linePos)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//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_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		float num = ZoneSystem.instance.GetGroundHeight(linePos);
		RaycastHit[] array = Physics.RaycastAll(linePos + Vector3.up, Vector3.down, 2f, Piece.s_pieceRayMask);
		for (int i = 0; i < array.Length; i++)
		{
			RaycastHit val = array[i];
			if (!Object.op_Implicit((Object)(object)((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Plant>()) && (double)((RaycastHit)(ref val)).point.y > (double)num)
			{
				num = ((RaycastHit)(ref val)).point.y;
			}
		}
		return num;
	}

	public static void DestroyFarmGrid()
	{
		if (farmGrid == null)
		{
			return;
		}
		for (int i = 0; i < farmGrid.Length; i++)
		{
			if (Object.op_Implicit((Object)(object)farmGrid[i]))
			{
				Object.Destroy((Object)(object)farmGrid[i]);
			}
		}
		farmGrid = null;
	}

	private static void InitFarmGrid()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)lineMaterial))
		{
			lineMaterial = Resources.FindObjectsOfTypeAll<Material>().First((Material k) => ((Object)k).name == "Default-Line");
		}
		farmGrid = (GameObject[])(object)new GameObject[(farmGridSections * 2 + 1) * 2];
		for (int i = 0; i < farmGrid.Length; i++)
		{
			GameObject val = new GameObject();
			LineRenderer val2 = val.AddComponent<LineRenderer>();
			((Renderer)val2).material = lineMaterial;
			val2.startColor = farmGridColor;
			val2.endColor = farmGridColor;
			val2.positionCount = farmGridSections * 2 + 1;
			farmGrid[i] = val;
		}
	}

	private static void HideFarmGrid()
	{
		if (farmGridVisible && farmGrid != null && (Object)(object)farmGrid[0] != (Object)null)
		{
			for (int i = 0; i < farmGrid.Length; i++)
			{
				((Renderer)farmGrid[i].GetComponent<LineRenderer>()).enabled = false;
			}
			farmGridVisible = false;
		}
	}

	private static PlantObject GetGhostPlant()
	{
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		Collider component = localPlayer.m_placementGhost.GetComponent<Collider>();
		if ((Object)(object)component != (Object)null && GetPlantObject(component, out var plantObject))
		{
			return plantObject;
		}
		if ((Object)(object)component == (Object)null)
		{
			Piece componentInParent = localPlayer.m_placementGhost.GetComponentInParent<Piece>();
			if ((Object)(object)componentInParent != (Object)null && customPlants.TryGetValue(((Object)componentInParent).name, out var value))
			{
				return new PlantObject(((Component)componentInParent).transform.position, value, null);
			}
		}
		return null;
	}

	private static bool GetFarmSnapPoints()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: 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_00ae: 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_006c: 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_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ce: 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)
		//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_021a: Unknown result type (might be due to invalid IL or missing references)
		//IL_021f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0223: Unknown result type (might be due to invalid IL or missing references)
		//IL_0228: 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_0232: 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)
		//IL_025b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_028d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0292: Unknown result type (might be due to invalid IL or missing references)
		//IL_0296: Unknown result type (might be due to invalid IL or missing references)
		//IL_029d: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0402: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03de: Unknown result type (might be due to invalid IL or missing references)
		//IL_041f: Unknown result type (might be due to invalid IL or missing references)
		//IL_043d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0448: Unknown result type (might be due to invalid IL or missing references)
		//IL_044d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0415: Unknown result type (might be due to invalid IL or missing references)
		//IL_041a: Unknown result type (might be due to invalid IL or missing references)
		//IL_031d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0322: Unknown result type (might be due to invalid IL or missing references)
		//IL_0327: Unknown result type (might be due to invalid IL or missing references)
		//IL_032e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0333: Unknown result type (might be due to invalid IL or missing references)
		//IL_0338: Unknown result type (might be due to invalid IL or missing references)
		//IL_033d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0344: Unknown result type (might be due to invalid IL or missing references)
		//IL_0349: Unknown result type (might be due to invalid IL or missing references)
		//IL_034e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0352: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = Player.m_localPlayer?.m_placementGhost;
		if ((Object)(object)val == (Object)null)
		{
			return false;
		}
		PlantObject plantGhost = GetGhostPlant();
		if (plantGhost != null)
		{
			if (plantGhostPosition == val.transform.position)
			{
				if (plantSnapPoint != Vector3.zero)
				{
					val.transform.position = plantSnapPoint;
				}
				return true;
			}
			plantSnapPoint = Vector3.zero;
			plantGhostPosition = plantGhost.position;
			List<PlantObject> otherPlants = GetOtherPlants(plantGhost.position, plantGhost.growthSize * 3.5f);
			otherPlants.OrderBy(delegate(PlantObject k)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				Vector3 val6 = k.position - plantGhost.position;
				return ((Vector3)(ref val6)).sqrMagnitude;
			});
			PlantObject firstPlant = ((otherPlants.Count > 0) ? otherPlants[0] : null);
			if (otherPlants.Count >= 1)
			{
				if (!((Vector3)(ref otherPlantCollider)).Equals(otherPlants[0].position))
				{
					otherPlantCollider = otherPlants[0].position;
					otherPlantList = GetOtherPlants(otherPlants[0].position, plantGhost.growthSize * 4.5f);
					otherPlantList = otherPlantList.OrderBy(delegate(PlantObject k)
					{
						//IL_0001: Unknown result type (might be due to invalid IL or missing references)
						//IL_000c: Unknown result type (might be due to invalid IL or missing references)
						//IL_0011: Unknown result type (might be due to invalid IL or missing references)
						//IL_0016: Unknown result type (might be due to invalid IL or missing references)
						Vector3 val5 = k.position - firstPlant.position;
						return ((Vector3)(ref val5)).sqrMagnitude;
					}).ToList();
				}
				otherPlants = otherPlantList;
			}
			if (otherPlants.Count == 1)
			{
				Vector3 val2 = plantGhost.position.xz() - otherPlants[0].position.xz();
				((Vector3)(ref val2)).Normalize();
				float num = Mathf.Max(plantGhost.growthSize, otherPlants[0].growthSize) * 2f + plantSpacing;
				plantSnapPoint = otherPlants[0].position + val2 * num;
				float groundHeight = GetGroundHeight(plantSnapPoint);
				plantSnapPoint.y = groundHeight;
				val.transform.position = plantSnapPoint;
				DrawFarmGrid(plantSnapPoint, val2, num);
				return true;
			}
			if (otherPlants.Count > 1)
			{
				Vector3 gridDir = GetGridDir(otherPlants, plantGhost);
				Vector3 val3 = default(Vector3);
				((Vector3)(ref val3))..ctor(gridDir.z, gridDir.y, 0f - gridDir.x);
				float num2 = Mathf.Max(Mathf.Max(otherPlants[1].growthSize, otherPlants[0].growthSize), plantGhost.growthSize) * 2f + plantSpacing;
				List<Vector3> list = new List<Vector3>();
				for (int i = -2; i <= 2; i++)
				{
					for (int j = -2; j <= 2; j++)
					{
						if (i != 0 || j != 0)
						{
							Vector3 item = firstPlant.position + gridDir * (float)i * num2 + val3 * (float)j * num2;
							list.Add(item);
						}
					}
				}
				List<Vector3> list2 = list.OrderBy(delegate(Vector3 k)
				{
					//IL_0000: Unknown result type (might be due to invalid IL or missing references)
					//IL_0007: Unknown result type (might be due to invalid IL or missing references)
					//IL_000c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0011: Unknown result type (might be due to invalid IL or missing references)
					Vector3 val4 = k - plantGhost.position;
					return ((Vector3)(ref val4)).sqrMagnitude;
				}).ToList();
				plantSnapPoint = Vector3.zero;
				for (int l = 0; l < list2.Count; l++)
				{
					if (!HasOverlappingPlants(list2[l], plantGhost.growthSize))
					{
						plantSnapPoint = list2[l];
						break;
					}
				}
				if (plantSnapPoint == Vector3.zero)
				{
					plantSnapPoint = list2[0];
				}
				float groundHeight2 = GetGroundHeight(plantSnapPoint);
				plantSnapPoint.y = groundHeight2;
				val.transform.position = plantSnapPoint;
				DrawFarmGrid(plantSnapPoint, gridDir, num2);
				return true;
			}
			HideFarmGrid();
			return true;
		}
		HideFarmGrid();
		return false;
	}

	private static Vector3 GetGridDir(List<PlantObject> plantList, PlantObject plantGhost)
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: 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_0174: 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)
		//IL_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		if (farmGridFixedRotation != 0)
		{
			Vector3 val = default(Vector3);
			val.y = 0f;
			val.x = Mathf.Sin((float)farmGridFixedRotation * ((float)Math.PI / 180f));
			val.z = Mathf.Cos((float)farmGridFixedRotation * ((float)Math.PI / 180f));
			return ((Vector3)(ref val)).normalized;
		}
		Vector3 result;
		for (int i = 0; i < plantList.Count - 1; i++)
		{
			for (int j = 1 + i; j < plantList.Count; j++)
			{
				result = plantList[i].position.xz() - plantList[j].position.xz();
				float num = Mathf.Max(Mathf.Max(plantList[i].growthSize, plantList[j].growthSize), plantGhost.growthSize) * 2f + plantSpacing;
				float magnitude = ((Vector3)(ref result)).magnitude;
				float num2 = Mathf.Floor(magnitude / (num - 0.01f));
				if ((double)Mathf.Abs(magnitude - num2 * num) <= (double)num2 * 0.009999999776482582)
				{
					((Vector3)(ref result)).Normalize();
					return result;
				}
			}
		}
		result = plantList[0].position.xz() - plantList[1].position.xz();
		((Vector3)(ref result)).Normalize();
		return result;
	}

	private static List<PlantObject> GetOtherPlants(Vector3 pos, float collisionRadius)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		List<PlantObject> list = new List<PlantObject>();
		if (plantObjectMask == 0)
		{
			plantObjectMask = LayerMask.GetMask(plantObjectMasks.ToArray());
		}
		int num = Physics.OverlapSphereNonAlloc(pos, collisionRadius, Piece.s_pieceColliders, plantObjectMask);
		for (int i = 0; i < num; i++)
		{
			if (GetPlantObject(Piece.s_pieceColliders[i], out var plantObject))
			{
				list.Add(plantObject);
			}
		}
		return list;
	}

	private static bool HasOverlappingPlants(Vector3 pos, float collisionRadius)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: 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)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		if (plantObjectMask == 0)
		{
			plantObjectMask = LayerMask.GetMask(plantObjectMasks.ToArray());
		}
		int num = Physics.OverlapSphereNonAlloc(pos, collisionRadius, Piece.s_pieceColliders, plantObjectMask);
		for (int i = 0; i < num; i++)
		{
			if (GetPlantObject(Piece.s_pieceColliders[i], out var plantObject))
			{
				Vector3 val = pos - plantObject.position;
				if ((double)((Vector3)(ref val)).magnitude <= (double)collisionRadius + (double)plantObject.growthSize)
				{
					return true;
				}
			}
		}
		return false;
	}

	private static bool GetPlantObject(Collider collider, out PlantObject plantObject)
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)collider == (Object)null)
		{
			plantObject = null;
			return false;
		}
		Plant componentInParent = ((Component)collider).GetComponentInParent<Plant>();
		if ((Object)(object)componentInParent != (Object)null)
		{
			if (!customPlants.TryGetValue(((Object)componentInParent).name, out var value))
			{
				value = componentInParent.m_growRadius;
			}
			plantObject = new PlantObject(((Component)collider).transform.position, value, collider);
			return true;
		}
		Piece componentInParent2 = ((Component)collider).GetComponentInParent<Piece>();
		if ((Object)(object)componentInParent2 != (Object)null && customPlants.TryGetValue(((Object)componentInParent2).name, out var value2))
		{
			if ((double)value2 == 0.0)
			{
				value2 = collider.contactOffset;
			}
			plantObject = new PlantObject(((Component)collider).transform.position, value2, collider);
			return true;
		}
		plantObject = null;
		return false;
	}
}
public static class Vector3Ext
{
	public static Vector3 xz(this Vector3 v)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		return new Vector3(v.x, 0f, v.z);
	}
}