Decompiled source of MySix IronCultivator v1.0.7

MySix.IronCultivator.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using MySix.Core;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace MySix
{
	internal static class ModVersion
	{
		public const string Value = "1.0.7";
	}
}
namespace MySix.IronCultivator
{
	public enum GridKey
	{
		None,
		Shift,
		Ctrl,
		Alt
	}
	public static class GridPlanting
	{
		public struct Cell
		{
			public Vector3 Position;

			public Quaternion Rotation;

			public bool Ok;

			public string Why;
		}

		public static Setting<GridKey> Key;

		public static Setting<bool> ControllerBlock;

		public static Setting<bool> AllOrNothing;

		public const int Size = 5;

		public static readonly Vector2Int[] Order = BuildOrder();

		public static readonly List<Cell> Cells = new List<Cell>();

		private static readonly List<GameObject> Ghosts = new List<GameObject>();

		private static readonly List<bool> GhostRed = new List<bool>();

		private static string _ghostsFor;

		private static readonly FieldRef<Player, GameObject> PlacementGhost = AccessTools.FieldRefAccess<Player, GameObject>("m_placementGhost");

		private static readonly FieldRef<Player, PlacementStatus> PlacementStatus = AccessTools.FieldRefAccess<Player, PlacementStatus>("m_placementStatus");

		private static readonly FieldRef<Player, PieceTable> BuildPieces = AccessTools.FieldRefAccess<Player, PieceTable>("m_buildPieces");

		private static readonly FieldRef<Humanoid, ItemData> RightItem = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_rightItem");

		private static int _spaceMask;

		private static int _roofMask;

		public static bool ForceHeld;

		private const float Buffer = 0.1f;

		private static readonly FieldRef<Player, int> PlaceRotation = AccessTools.FieldRefAccess<Player, int>("m_placeRotation");

		private static bool _turnSet;

		internal static bool PlantedThisPress;

		public const string NoSeeds = "$msg_missingrequirement";

		private const float RayStart = 1.2f;

		private static readonly RaycastHit[] Hits = (RaycastHit[])(object)new RaycastHit[32];

		private static readonly FieldRef<Player, int> PlaceRayMask = AccessTools.FieldRefAccess<Player, int>("m_placeRayMask");

		private const float LookAhead = 4f;

		private static readonly Dictionary<string, float> Reaches = new Dictionary<string, float>();

		private static readonly Collider[] Overlaps = (Collider[])(object)new Collider[64];

		private static readonly int[] Turns = (from _ in Enumerable.Range(0, 25)
			select Random.Range(0, 16)).ToArray();

		private static readonly MethodInfo UpdateGhost = AccessTools.Method(typeof(Player), "UpdatePlacementGhost", (Type[])null, (Type[])null);

		public static bool Active { get; private set; }

		public static float Spacing { get; private set; }

		private static int SpaceMask
		{
			get
			{
				if (_spaceMask == 0)
				{
					return _spaceMask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid" });
				}
				return _spaceMask;
			}
		}

		private static int RoofMask
		{
			get
			{
				if (_roofMask == 0)
				{
					return _roofMask = LayerMask.GetMask(new string[3] { "Default", "static_solid", "piece" });
				}
				return _roofMask;
			}
		}

		public static int TurnSteps { get; private set; }

		public static Quaternion Facing => Quaternion.Euler(0f, (float)TurnSteps * 22.5f, 0f);

		private static Vector2Int[] BuildOrder()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			int num = 2;
			List<Vector2Int> list = new List<Vector2Int>();
			for (int i = -num; i <= num; i++)
			{
				for (int j = -num; j <= num; j++)
				{
					list.Add(new Vector2Int(i, j));
				}
			}
			return (from v in list
				orderby Mathf.Max(Mathf.Abs(((Vector2Int)(ref v)).x), Mathf.Abs(((Vector2Int)(ref v)).y)), ((Vector2Int)(ref v)).x * ((Vector2Int)(ref v)).x + ((Vector2Int)(ref v)).y * ((Vector2Int)(ref v)).y, ((Vector2Int)(ref v)).y, ((Vector2Int)(ref v)).x
				select v).ToArray();
		}

		public static Piece CropInHand(Player player)
		{
			if ((Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)Player.m_localPlayer || !((Character)player).InPlaceMode() || ((Character)player).IsDead())
			{
				return null;
			}
			ItemData val = RightItem.Invoke((Humanoid)(object)player);
			if (val == null || val.m_shared.m_name != "$item_mysix_ironcultivator")
			{
				return null;
			}
			PieceTable val2 = BuildPieces.Invoke(player);
			Piece val3 = (((Object)(object)val2 != (Object)null) ? val2.GetSelectedPiece() : null);
			if ((Object)(object)val3 == (Object)null)
			{
				return null;
			}
			Plant component = ((Component)val3).GetComponent<Plant>();
			if ((Object)(object)component == (Object)null || component.m_attachDistance > 0f)
			{
				return null;
			}
			return val3;
		}

		public static bool PadHeld()
		{
			if (ControllerBlock != null && ControllerBlock.Value && ZInput.IsGamepadActive())
			{
				return ZInput.GetButton("JoyBlock");
			}
			return false;
		}

		public static bool KeyHeld()
		{
			switch ((Key != null) ? Key.Value : GridKey.Alt)
			{
			case GridKey.Shift:
				if (!Input.GetKey((KeyCode)304))
				{
					return Input.GetKey((KeyCode)303);
				}
				return true;
			case GridKey.Ctrl:
				if (!Input.GetKey((KeyCode)306))
				{
					return Input.GetKey((KeyCode)305);
				}
				return true;
			case GridKey.Alt:
				if (!Input.GetKey((KeyCode)308))
				{
					return Input.GetKey((KeyCode)307);
				}
				return true;
			default:
				return false;
			}
		}

		public static bool Wanted(Player player)
		{
			if ((Object)(object)CropInHand(player) != (Object)null && !Hud.IsPieceSelectionVisible())
			{
				if (!ForceHeld && !KeyHeld())
				{
					return PadHeld();
				}
				return true;
			}
			return false;
		}

		public static float SpacingFor(Piece piece)
		{
			Plant component = ((Component)piece).GetComponent<Plant>();
			float num = Reach(((Component)piece).gameObject, ((Component)piece).transform);
			GameObject[] grownPrefabs = component.m_grownPrefabs;
			foreach (GameObject val in grownPrefabs)
			{
				if ((Object)(object)val != (Object)null)
				{
					num = Mathf.Max(num, Reach(val, val.transform));
				}
			}
			return component.m_growRadius + num + 0.1f;
		}

		private static float Reach(GameObject root, Transform rootTransform)
		{
			float num = 0f;
			Collider[] componentsInChildren = root.GetComponentsInChildren<Collider>(true);
			foreach (Collider val in componentsInChildren)
			{
				if (!val.isTrigger && ((1 << ((Component)val).gameObject.layer) & SpaceMask) != 0 && (((Component)val).gameObject.activeSelf || !((Object)(object)((Component)val).transform != (Object)(object)rootTransform)))
				{
					num = Mathf.Max(num, HorizontalReach(val));
				}
			}
			return num;
		}

		private static float HorizontalReach(Collider c)
		{
			//IL_0006: 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_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			Vector3 lossyScale = ((Component)c).transform.lossyScale;
			float num = Mathf.Max(Mathf.Abs(lossyScale.x), Mathf.Abs(lossyScale.z));
			CapsuleCollider val = (CapsuleCollider)(object)((c is CapsuleCollider) ? c : null);
			Vector2 val4;
			if (val == null)
			{
				SphereCollider val2 = (SphereCollider)(object)((c is SphereCollider) ? c : null);
				if (val2 == null)
				{
					BoxCollider val3 = (BoxCollider)(object)((c is BoxCollider) ? c : null);
					if (val3 != null)
					{
						val4 = new Vector2(val3.center.x, val3.center.z);
						float magnitude = ((Vector2)(ref val4)).magnitude;
						val4 = new Vector2(val3.size.x, val3.size.z);
						return (magnitude + ((Vector2)(ref val4)).magnitude / 2f) * num;
					}
					return 0.2f;
				}
				val4 = new Vector2(val2.center.x, val2.center.z);
				return (((Vector2)(ref val4)).magnitude + val2.radius) * num;
			}
			val4 = new Vector2(val.center.x, val.center.z);
			return (((Vector2)(ref val4)).magnitude + ((val.direction == 1) ? val.radius : Mathf.Max(val.radius, val.height / 2f))) * num;
		}

		public static int Affordable(Player player, Piece piece)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			if (player.NoCostCheat() || ZoneSystem.instance.GetGlobalKey(piece.FreeBuildKey()))
			{
				return 25;
			}
			int num = 25;
			Requirement[] resources = piece.m_resources;
			foreach (Requirement val in resources)
			{
				if (!((Object)(object)val.m_resItem == (Object)null) && val.m_amount > 0)
				{
					num = Mathf.Min(num, ((Humanoid)player).GetInventory().CountItems(val.m_resItem.m_itemData.m_shared.m_name, -1, true) / val.m_amount);
				}
			}
			return Mathf.Max(0, num);
		}

		public static void Refresh(Player player)
		{
			//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_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: 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)
			if (!((Object)(object)player != (Object)(object)Player.m_localPlayer))
			{
				Piece val = (Wanted(player) ? CropInHand(player) : null);
				GameObject val2 = (((Object)(object)val != (Object)null) ? PlacementGhost.Invoke(player) : null);
				if ((Object)(object)val != (Object)null && !_turnSet)
				{
					Vector3 lookDir = ((Character)player).GetLookDir();
					TurnSteps = (Mathf.RoundToInt(Mathf.Atan2(lookDir.x, lookDir.z) * 57.29578f / 22.5f) % 16 + 16) % 16;
					_turnSet = true;
				}
				if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null || !val2.activeSelf)
				{
					Active = (Object)(object)val != (Object)null;
					Cells.Clear();
					HideGhosts(0);
				}
				else
				{
					Active = true;
					Evaluate(player, val, val2.transform.position, val2.transform.rotation, Facing, PlacementStatus.Invoke(player));
					ShowGhosts(player, val, val2);
				}
			}
		}

		public static void Turned(int before, int after)
		{
			if (after != before)
			{
				TurnSteps = ((TurnSteps + after - before) % 16 + 16) % 16;
			}
		}

		public static void Evaluate(Player player, Piece piece, Vector3 middle, Quaternion middleTurn, Quaternion facing, PlacementStatus? gameVerdict)
		{
			//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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: 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_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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_009a: 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)
			Spacing = SpacingFor(piece);
			int num = Affordable(player, piece);
			Cells.Clear();
			for (int i = 0; i < 25; i++)
			{
				Vector2Int val = Order[i];
				Cell item = new Cell
				{
					Rotation = ((i == 0) ? middleTurn : Quaternion.Euler(0f, (float)Turns[i] * 22.5f, 0f))
				};
				if (val == Vector2Int.zero && gameVerdict.HasValue)
				{
					item.Position = middle;
					item.Why = (((int)gameVerdict.Value != 0) ? MessageFor(gameVerdict.Value) : Check(player, piece, middle));
				}
				else
				{
					Vector3 spot = middle + facing * new Vector3((float)((Vector2Int)(ref val)).x * Spacing, 0f, (float)((Vector2Int)(ref val)).y * Spacing);
					item.Why = Surface(player, piece, middle, spot, out item.Position) ?? Check(player, piece, item.Position);
				}
				if (item.Why == null && num-- <= 0)
				{
					item.Why = "$msg_missingrequirement";
				}
				item.Ok = item.Why == null;
				Cells.Add(item);
			}
			if (AllOrNothing == null || !AllOrNothing.Value || !Cells.Any((Cell c) => !c.Ok))
			{
				return;
			}
			string why = Cells.First((Cell c) => !c.Ok).Why;
			for (int num2 = 1; num2 < Cells.Count; num2++)
			{
				Cell value = Cells[num2];
				value.Ok = false;
				if (value.Why == null)
				{
					value.Why = why;
				}
				Cells[num2] = value;
			}
		}

		private static string Surface(Player player, Piece piece, Vector3 middle, Vector3 spot, out Vector3 at)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: 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_001d: 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_0037: 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)
			//IL_0042: 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_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: 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_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			at = spot;
			float num = default(float);
			if (!ZoneSystem.instance.GetGroundHeight(spot, ref num))
			{
				return "$msg_invalidplacement";
			}
			float num2 = Mathf.Max(middle.y, num) + 1.2f;
			int num3 = Physics.RaycastNonAlloc(new Vector3(spot.x, num2, spot.z), Vector3.down, Hits, num2 - Mathf.Min(middle.y, num) + 2f, PlaceRayMask.Invoke(player));
			RaycastHit? val = null;
			RaycastHit value;
			for (int i = 0; i < num3; i++)
			{
				if ((Object)(object)((RaycastHit)(ref Hits[i])).collider == (Object)null || (Object)(object)((RaycastHit)(ref Hits[i])).collider.attachedRigidbody != (Object)null)
				{
					continue;
				}
				if (val.HasValue)
				{
					float distance = ((RaycastHit)(ref Hits[i])).distance;
					value = val.Value;
					if (!(distance < ((RaycastHit)(ref value)).distance))
					{
						continue;
					}
				}
				val = Hits[i];
			}
			if (!val.HasValue)
			{
				return "$msg_invalidplacement";
			}
			value = val.Value;
			at = ((RaycastHit)(ref value)).point;
			value = val.Value;
			if ((Object)(object)((Component)((RaycastHit)(ref value)).collider).GetComponent<Heightmap>() != (Object)null)
			{
				return null;
			}
			if (!piece.m_cultivatedGroundOnly)
			{
				return "$msg_invalidplacement";
			}
			return "$msg_needcultivated";
		}

		public static string Check(Player player, Piece piece, Vector3 at)
		{
			//IL_0007: 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_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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_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_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: 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_00df: Invalid comparison between Unknown and I4
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Invalid comparison between Unknown and I4
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: 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_0100: Invalid comparison between Unknown and I4
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			Plant component = ((Component)piece).GetComponent<Plant>();
			Heightmap val = Heightmap.FindHeightmap(at);
			if ((piece.m_cultivatedGroundOnly || piece.m_groundOnly || component.m_needCultivatedGround) && (Object)(object)val == (Object)null)
			{
				return "$msg_invalidplacement";
			}
			if ((piece.m_cultivatedGroundOnly || component.m_needCultivatedGround) && !val.IsCultivated(at))
			{
				return "$msg_needcultivated";
			}
			if (!piece.m_allowedInDungeons && ((Character)player).InInterior())
			{
				return "$msg_notindungeon";
			}
			if (Location.IsInsideNoBuildLocation(at))
			{
				return "$msg_nobuildzone";
			}
			if (!PrivateArea.CheckAccess(at, 0f, false, false))
			{
				return "$msg_privatezone";
			}
			if ((int)piece.m_onlyInBiome != 0 && (Heightmap.FindBiome(at) & piece.m_onlyInBiome) == 0)
			{
				return "$msg_wrongbiome";
			}
			if ((Object)(object)val != (Object)null)
			{
				Biome biome = val.GetBiome(at, 0.02f, false);
				if ((biome & component.m_biome) == 0)
				{
					return "$piece_plant_wrongbiome";
				}
				if (!component.m_tolerateHeat && (int)biome == 32 && !ShieldGenerator.IsInsideShield(at))
				{
					return "$piece_plant_toohot";
				}
				if (!component.m_tolerateCold && ((int)biome == 64 || (int)biome == 4) && !ShieldGenerator.IsInsideShield(at))
				{
					return "$piece_plant_toocold";
				}
			}
			if (Physics.Raycast(at, Vector3.up, 100f, RoofMask))
			{
				return "$piece_plant_nosun";
			}
			int num = Physics.OverlapSphereNonAlloc(at, component.m_growRadius, Overlaps, SpaceMask);
			for (int i = 0; i < num; i++)
			{
				Plant component2 = ((Component)Overlaps[i]).GetComponent<Plant>();
				if ((Object)(object)component2 == (Object)null || (int)component2.GetStatus() == 0)
				{
					return "$piece_plant_nospace";
				}
			}
			float num2 = GrownReach(((Component)piece).gameObject);
			num = Physics.OverlapSphereNonAlloc(at, component.m_growRadius + 4f, Overlaps, SpaceMask);
			for (int j = 0; j < num; j++)
			{
				Plant componentInParent = ((Component)Overlaps[j]).GetComponentInParent<Plant>();
				if (!((Object)(object)componentInParent == (Object)null))
				{
					Vector3 val2 = ((Component)componentInParent).transform.position - at;
					Vector2 val3 = new Vector2(val2.x, val2.z);
					float magnitude = ((Vector2)(ref val3)).magnitude;
					if (magnitude < component.m_growRadius + GrownReach(((Component)componentInParent).gameObject) || magnitude < componentInParent.m_growRadius + num2)
					{
						return "$piece_plant_nospace";
					}
				}
			}
			return null;
		}

		private static float GrownReach(GameObject crop)
		{
			string prefabName = Utils.GetPrefabName(crop);
			if (Reaches.TryGetValue(prefabName, out var value))
			{
				return value;
			}
			GameObject val = (((Object)(object)ZNetScene.instance != (Object)null) ? ZNetScene.instance.GetPrefab(prefabName) : null);
			GameObject obj = (((Object)(object)val != (Object)null) ? val : crop);
			float num = Reach(obj, obj.transform);
			Plant component = obj.GetComponent<Plant>();
			if ((Object)(object)component != (Object)null)
			{
				GameObject[] grownPrefabs = component.m_grownPrefabs;
				foreach (GameObject val2 in grownPrefabs)
				{
					if ((Object)(object)val2 != (Object)null)
					{
						num = Mathf.Max(num, Reach(val2, val2.transform));
					}
				}
			}
			Reaches[prefabName] = num;
			return num;
		}

		private static string MessageFor(PlacementStatus status)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected I4, but got Unknown
			return (status - 2) switch
			{
				1 => "$msg_nobuildzone", 
				0 => "$msg_blocked", 
				2 => "$msg_privatezone", 
				3 => "$msg_needspace", 
				6 => "$msg_wrongbiome", 
				7 => "$msg_needcultivated", 
				8 => "$msg_needdirt", 
				9 => "$msg_notindungeon", 
				11 => "$msg_snowtoodeep", 
				12 => "$msg_nosnow", 
				_ => "$msg_invalidplacement", 
			};
		}

		private static void ShowGhosts(Player player, Piece piece, GameObject ghost)
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			if (_ghostsFor != ((Object)ghost).name)
			{
				DestroyGhosts();
				_ghostsFor = ((Object)ghost).name;
			}
			Paint(ghost.GetComponent<Piece>(), !Cells[0].Ok, -1);
			for (int i = 1; i < Cells.Count; i++)
			{
				while (Ghosts.Count < i)
				{
					ZNetView.m_forceDisableInit = true;
					GameObject val;
					try
					{
						val = Object.Instantiate<GameObject>(ghost);
					}
					finally
					{
						ZNetView.m_forceDisableInit = false;
					}
					((Object)val).name = ((Object)ghost).name;
					Ghosts.Add(val);
					GhostRed.Add(item: false);
					Paint(val.GetComponent<Piece>(), red: false, Ghosts.Count - 1, force: true);
				}
				GameObject val2 = Ghosts[i - 1];
				val2.transform.SetPositionAndRotation(Cells[i].Position, Cells[i].Rotation);
				if (!val2.activeSelf)
				{
					val2.SetActive(true);
				}
				Paint(val2.GetComponent<Piece>(), !Cells[i].Ok, i - 1);
			}
			HideGhosts(Cells.Count - 1);
		}

		private static void Paint(Piece piece, bool red, int index, bool force = false)
		{
			if (!((Object)(object)piece == (Object)null) && (index < 0 || force || GhostRed[index] != red))
			{
				piece.SetInvalidPlacementHeightlight(red);
				if (index >= 0)
				{
					GhostRed[index] = red;
				}
			}
		}

		private static void HideGhosts(int keep)
		{
			for (int i = keep; i < Ghosts.Count; i++)
			{
				if ((Object)(object)Ghosts[i] != (Object)null && Ghosts[i].activeSelf)
				{
					Ghosts[i].SetActive(false);
				}
			}
		}

		private static void DestroyGhosts()
		{
			foreach (GameObject ghost in Ghosts)
			{
				if ((Object)(object)ghost != (Object)null)
				{
					Object.Destroy((Object)(object)ghost);
				}
			}
			Ghosts.Clear();
			GhostRed.Clear();
			_ghostsFor = null;
		}

		public static int PlantGrid(Player player, Piece piece)
		{
			UpdateGhost.Invoke(player, new object[1] { true });
			return PlantCells(player, piece);
		}

		public static int PlantCells(Player player, Piece piece)
		{
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			List<Cell> list = Cells.Where((Cell c) => c.Ok).ToList();
			if (list.Count == 0)
			{
				string text = ((Cells.Count > 0) ? Cells[0].Why : null);
				((Character)player).Message((MessageType)2, text ?? "$msg_invalidplacement", 0, (Sprite)null, false);
				return 0;
			}
			Inventory inventory = ((Humanoid)player).GetInventory();
			int num = PlaceRotation.Invoke(player);
			bool flag = (inventory.ItemCheated(piece.m_resources, -1, true) || player.NoCostCheat()) && !PlayerProfile.s_bypassCheatChecks;
			for (int num2 = 0; num2 < list.Count; num2++)
			{
				ZLog.Log((object)("Placed " + ((Object)((Component)piece).gameObject).name));
				Game.instance.IncrementPlayerStat((PlayerStatType)2, 1f, flag);
				Game.instance.GetPlayerProfile().IncrementStatBuildPiecePlaced(piece.m_name, 1f, flag);
				player.PlacePiece(piece, list[num2].Position, list[num2].Rotation, num2 == 0, flag);
			}
			if (list.Count > 1 && !ZoneSystem.instance.GetGlobalKey(piece.FreeBuildKey()))
			{
				player.ConsumeResources(piece.m_resources, 0, -1, list.Count - 1);
			}
			PlaceRotation.Invoke(player) = num;
			PlantedThisPress = true;
			Plugin.Log.LogDebug((object)$"planted {list.Count} of {Cells.Count} {((Object)piece).name}");
			return list.Count;
		}
	}
	[HarmonyPatch(typeof(Player), "UpdatePlacement")]
	public static class FollowTurn
	{
		private static readonly FieldRef<Player, int> PlaceRotation = AccessTools.FieldRefAccess<Player, int>("m_placeRotation");

		private static void Prefix(Player __instance, out int __state)
		{
			__state = PlaceRotation.Invoke(__instance);
			GridPlanting.PlantedThisPress = false;
		}

		private static void Postfix(Player __instance, int __state)
		{
			if (!GridPlanting.PlantedThisPress && (Object)(object)__instance == (Object)(object)Player.m_localPlayer && GridPlanting.Wanted(__instance))
			{
				GridPlanting.Turned(__state, PlaceRotation.Invoke(__instance));
			}
		}
	}
	[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
	public static class RefreshGrid
	{
		private static bool _inside;

		private static void Postfix(Player __instance)
		{
			if (_inside)
			{
				return;
			}
			_inside = true;
			try
			{
				GridPlanting.Refresh(__instance);
			}
			finally
			{
				_inside = false;
			}
		}
	}
	[HarmonyPatch(typeof(Player), "TryPlacePiece")]
	public static class PlaceGrid
	{
		private static bool Prefix(Player __instance, Piece piece, ref bool __result)
		{
			if (!GridPlanting.Wanted(__instance) || (Object)(object)GridPlanting.CropInHand(__instance) != (Object)(object)piece)
			{
				return true;
			}
			__result = GridPlanting.PlantGrid(__instance, piece) > 0;
			return false;
		}
	}
	[HarmonyPatch]
	public static class ControllerInput
	{
		private static readonly MethodInfo GetButton = AccessTools.Method(typeof(ZInput), "GetButton", new Type[1] { typeof(string) }, (Type[])null);

		private static readonly MethodInfo Instead = AccessTools.Method(typeof(ControllerInput), "Button", (Type[])null, (Type[])null);

		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return AccessTools.Method(typeof(Player), "UpdatePlacement", (Type[])null, (Type[])null);
			yield return AccessTools.Method(typeof(Player), "UpdatePlacementGhost", (Type[])null, (Type[])null);
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> code)
		{
			foreach (CodeInstruction item in code)
			{
				if (CodeInstructionExtensions.Calls(item, GetButton))
				{
					item.operand = Instead;
				}
				yield return item;
			}
		}

		public static bool Button(string name)
		{
			if (name == "JoyAltKeys" && GridPlanting.PadHeld() && (Object)(object)GridPlanting.CropInHand(Player.m_localPlayer) != (Object)null)
			{
				return false;
			}
			return ZInput.GetButton(name);
		}
	}
	public static class IronCultivatorItem
	{
		public const string PrefabName = "MySix_IronCultivator";

		public const string RecipeName = "Recipe_MySix_IronCultivator";

		public const string NameToken = "item_mysix_ironcultivator";

		public const string DescriptionToken = "item_mysix_ironcultivator_description";

		public const string SharedName = "$item_mysix_ironcultivator";

		private const string SourcePrefab = "Cultivator";

		private static GameObject _holder;

		private static GameObject _prefab;

		private static readonly Dictionary<Material, Material> Repainted = new Dictionary<Material, Material>();

		private static readonly MethodInfo CloneMethod = AccessTools.Method(typeof(object), "MemberwiseClone", (Type[])null, (Type[])null);

		public static GameObject GetOrBuild(GameObject source)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Invalid comparison between Unknown and I4
			if ((Object)(object)_prefab != (Object)null)
			{
				return _prefab;
			}
			if ((Object)(object)source == (Object)null)
			{
				return null;
			}
			_holder = new GameObject("MySix_IronCultivator_Prefabs");
			_holder.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)_holder);
			GameObject val = Object.Instantiate<GameObject>(source, _holder.transform, false);
			((Object)val).name = "MySix_IronCultivator";
			ItemDrop component = val.GetComponent<ItemDrop>();
			component.m_itemData = CloneData(component.m_itemData);
			SharedData shared = component.m_itemData.m_shared;
			shared.m_name = "$item_mysix_ironcultivator";
			shared.m_description = "$item_mysix_ironcultivator_description";
			component.m_itemData.m_dropPrefab = val;
			if ((int)SystemInfo.graphicsDeviceType != 4)
			{
				Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>(true);
				foreach (Renderer val2 in componentsInChildren)
				{
					if (val2 is MeshRenderer || val2 is SkinnedMeshRenderer)
					{
						val2.sharedMaterials = val2.sharedMaterials.Select(IronMaterial).ToArray();
					}
				}
				shared.m_icons = shared.m_icons.Select(IronRecolour.Sprite).ToArray();
			}
			_prefab = val;
			Plugin.Log.LogInfo((object)("MySix_IronCultivator made from " + ((Object)source).name));
			return _prefab;
		}

		private static Material IronMaterial(Material original)
		{
			//IL_002b: 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_0047: Expected O, but got Unknown
			if ((Object)(object)original == (Object)null || (Object)(object)original.mainTexture == (Object)null)
			{
				return original;
			}
			if (Repainted.TryGetValue(original, out var value))
			{
				return value;
			}
			Material val = new Material(original)
			{
				name = ((Object)original).name + "_iron"
			};
			val.mainTexture = (Texture)(object)IronRecolour.Texture(original.mainTexture);
			Repainted[original] = val;
			return val;
		}

		private static ItemData CloneData(ItemData data)
		{
			//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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_002e: Expected O, but got Unknown
			ItemData val = (ItemData)CloneMethod.Invoke(data, null);
			val.m_shared = (SharedData)CloneMethod.Invoke(data.m_shared, null);
			return val;
		}

		public static void Register(ObjectDB db)
		{
			if ((Object)(object)db == (Object)null || db.m_items == null || db.m_items.Count == 0)
			{
				return;
			}
			GameObject source = db.GetItemPrefab("Cultivator");
			GameObject orBuild = GetOrBuild(source);
			if ((Object)(object)orBuild == (Object)null)
			{
				Plugin.Log.LogWarning((object)"No Cultivator in the game's items, so no MySix_IronCultivator");
				return;
			}
			if (!db.m_items.Contains(orBuild))
			{
				db.m_items.Add(orBuild);
				AccessTools.Method(typeof(ObjectDB), "UpdateRegisters", (Type[])null, (Type[])null).Invoke(db, null);
			}
			if (db.m_recipes.Any((Recipe r) => (Object)(object)r != (Object)null && ((Object)r).name == "Recipe_MySix_IronCultivator"))
			{
				return;
			}
			Recipe val = ((IEnumerable<Recipe>)db.m_recipes).FirstOrDefault((Func<Recipe, bool>)((Recipe r) => (Object)(object)r != (Object)null && (Object)(object)r.m_item != (Object)null && (Object)(object)((Component)r.m_item).gameObject == (Object)(object)source));
			GameObject iron = db.GetItemPrefab("Iron");
			if ((Object)(object)val == (Object)null || (Object)(object)iron == (Object)null)
			{
				Plugin.Log.LogWarning((object)"No Cultivator recipe or no Iron in the game, so no recipe for MySix_IronCultivator");
				return;
			}
			Recipe val2 = Object.Instantiate<Recipe>(val);
			((Object)val2).name = "Recipe_MySix_IronCultivator";
			val2.m_item = orBuild.GetComponent<ItemDrop>();
			val2.m_resources = val.m_resources.Select(delegate(Requirement r)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0005: 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_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0029: 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_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_004e: Expected O, but got Unknown
				Requirement val3 = new Requirement
				{
					m_resItem = r.m_resItem,
					m_amount = r.m_amount,
					m_extraAmountOnlyOneIngredient = r.m_extraAmountOnlyOneIngredient,
					m_amountPerLevel = r.m_amountPerLevel,
					m_recover = r.m_recover,
					m_upgraderResource = r.m_upgraderResource
				};
				if ((Object)(object)r.m_resItem != (Object)null && ((Object)((Component)r.m_resItem).gameObject).name == "Bronze")
				{
					val3.m_resItem = iron.GetComponent<ItemDrop>();
				}
				return val3;
			}).ToArray();
			int num = db.m_recipes.IndexOf(val);
			db.m_recipes.Insert(num + 1, val2);
			Plugin.Log.LogInfo((object)("Recipe_MySix_IronCultivator: " + string.Join(", ", val2.m_resources.Select((Requirement r) => string.Format("{0} {1}", Object.op_Implicit((Object)(object)r.m_resItem) ? ((Object)r.m_resItem).name : "?", r.m_amount))) + string.Format(" at {0} level {1}", Object.op_Implicit((Object)(object)val2.m_craftingStation) ? val2.m_craftingStation.m_name : "no station", val2.m_minStationLevel)));
		}

		public static void Register(ZNetScene scene)
		{
			GameObject orBuild = GetOrBuild(scene.GetPrefab("Cultivator"));
			if (!((Object)(object)orBuild == (Object)null) && !scene.m_prefabs.Contains(orBuild))
			{
				scene.m_prefabs.Add(orBuild);
				Traverse.Create((object)scene).Field("m_namedPrefabs").GetValue<Dictionary<int, GameObject>>()[StringExtensionMethods.GetStableHashCode(((Object)orBuild).name)] = orBuild;
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	public static class ObjectDBAwake
	{
		private static void Postfix(ObjectDB __instance)
		{
			IronCultivatorItem.Register(__instance);
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	public static class ObjectDBCopy
	{
		private static void Postfix(ObjectDB __instance)
		{
			IronCultivatorItem.Register(__instance);
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	public static class ZNetSceneAwake
	{
		private static void Postfix(ZNetScene __instance)
		{
			IronCultivatorItem.Register(__instance);
		}
	}
	[BepInPlugin("com.mysix.ironcultivator", "MySix Iron Cultivator", "1.0.7")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.mysix.ironcultivator";

		public const string PluginName = "MySix Iron Cultivator";

		public const string PluginVersion = "1.0.7";

		private const int RequiredCoreApi = 2;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			if (CorePlugin.RequireApi("MySix Iron Cultivator", 2))
			{
				LoadTexts();
				GridPlanting.Key = Settings.Register<GridKey>("MySix Iron Cultivator", ((BaseUnityPlugin)this).Config.Bind<GridKey>("IronCultivator", "GridKey", GridKey.Alt, "Hold this with the Iron Cultivator out to plant a 5x5 grid of crops instead of one. On a Mac, Alt is the Option key. Alt by default because it is not a movement key, where Shift is sprint and Ctrl is crouch. None leaves only the controller's block button."), (Scope)1, (string)null);
				GridPlanting.ControllerBlock = Settings.Register<bool>("MySix Iron Cultivator", ((BaseUnityPlugin)this).Config.Bind<bool>("IronCultivator", "ControllerBlock", true, "On a controller, holding block (the left trigger) does the same. In placement mode that trigger also rotates a piece and copies the piece you look at; while the Iron Cultivator has a crop chosen, holding it means the grid instead. Crops are planted at a random turn anyway."), (Scope)1, (string)null);
				GridPlanting.AllOrNothing = Settings.Register<bool>("MySix Iron Cultivator", ((BaseUnityPlugin)this).Config.Bind<bool>("IronCultivator", "AllOrNothing", false, "A grid is planted whole or not at all: when some of its 25 spots cannot be planted (no room, not cultivated, too few seeds), only the one crop in the middle goes in, as with the game's own cultivator. Off by default: the grid plants every spot it can."), (Scope)1, (string)null);
				_harmony = new Harmony("com.mysix.ironcultivator");
				_harmony.PatchAll(typeof(Plugin).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"MySix Iron Cultivator 1.0.7 loaded");
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private static void LoadTexts()
		{
			Texts.Load(typeof(Plugin).Assembly, "Texts.txt");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public static class IronRecolour
	{
		private const float BronzeHueFrom = 34f;

		private const float BronzeHueFull = 40f;

		private const float BronzeHueTo = 90f;

		private const float MinSaturation = 0.12f;

		private const float IronHue = 43f / 72f;

		private const float IronSaturation = 0.1f;

		private const float IronValueScale = 0.8f;

		private const float IronValueCurve = 1.15f;

		private static readonly Dictionary<Texture, Texture2D> Done = new Dictionary<Texture, Texture2D>();

		public static float BronzeWeight(Color c)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			float num = default(float);
			float num2 = default(float);
			float num3 = default(float);
			Color.RGBToHSV(c, ref num, ref num2, ref num3);
			float num4 = num * 360f;
			if (num4 > 90f)
			{
				return 0f;
			}
			float num5 = Mathf.Clamp01((num4 - 34f) / 6f);
			float num6 = Mathf.Clamp01((num2 - 0.12f) / 0.1f);
			return num5 * num6;
		}

		public static Color Iron(Color c)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: 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)
			return Iron(c, BronzeWeight(c));
		}

		public static Color Iron(Color c, float w)
		{
			//IL_000a: 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)
			//IL_003e: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if (w <= 0f)
			{
				return c;
			}
			float num = default(float);
			float num2 = default(float);
			float num3 = default(float);
			Color.RGBToHSV(c, ref num, ref num2, ref num3);
			Color val = Color.HSVToRGB(43f / 72f, 0.1f, Mathf.Clamp01(Mathf.Pow(num3, 1.15f) * 0.8f));
			val.a = c.a;
			return Color.Lerp(c, val, w);
		}

		public static Texture2D Texture(Texture source)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if (Done.TryGetValue(source, out var value))
			{
				return value;
			}
			Texture2D val = Read(source, new Rect(0f, 0f, (float)source.width, (float)source.height), mipmaps: true);
			Repaint(val, wholeRegion: true);
			((Object)val).name = ((Object)source).name + "_iron";
			val.Apply(true, true);
			Done[source] = val;
			return val;
		}

		public static Sprite Sprite(Sprite icon)
		{
			//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_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)icon == (Object)null)
			{
				return null;
			}
			Rect textureRect = icon.textureRect;
			Texture2D val = Read((Texture)(object)icon.texture, textureRect, mipmaps: false);
			Repaint(val, wholeRegion: false);
			((Object)val).name = ((Object)icon).name + "_iron";
			val.Apply(false, false);
			Sprite obj = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), icon.pixelsPerUnit);
			((Object)obj).name = ((Object)icon).name + "_iron";
			return obj;
		}

		private static Texture2D Read(Texture source, Rect rect, bool mipmaps)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
			RenderTexture active = RenderTexture.active;
			Graphics.Blit(source, temporary);
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D((int)((Rect)(ref rect)).width, (int)((Rect)(ref rect)).height, (TextureFormat)4, mipmaps);
			val.ReadPixels(rect, 0, 0);
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			return val;
		}

		private static void Repaint(Texture2D tex, bool wholeRegion)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			Color32[] pixels = tex.GetPixels32();
			int width = ((Texture)tex).width;
			int height = ((Texture)tex).height;
			float[] array = new float[pixels.Length];
			for (int i = 0; i < pixels.Length; i++)
			{
				array[i] = BronzeWeight(Color32.op_Implicit(pixels[i]));
			}
			if (wholeRegion)
			{
				float[] array2 = new float[array.Length];
				for (int j = 0; j < height; j++)
				{
					for (int k = 0; k < width; k++)
					{
						float num = 0f;
						for (int l = -1; l <= 1; l++)
						{
							for (int m = -1; m <= 1; m++)
							{
								int num2 = k + m;
								int num3 = j + l;
								if (num2 >= 0 && num3 >= 0 && num2 < width && num3 < height)
								{
									num = Mathf.Max(num, array[num3 * width + num2]);
								}
							}
						}
						array2[j * width + k] = ((num >= 0.33f) ? 1f : num);
					}
				}
				array = array2;
			}
			for (int n = 0; n < pixels.Length; n++)
			{
				pixels[n] = Color32.op_Implicit(Iron(Color32.op_Implicit(pixels[n]), array[n]));
			}
			tex.SetPixels32(pixels);
		}
	}
}