Decompiled source of PocketWorkbench v1.0.0

PocketWorkbench/Toolbox.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("Toolbox")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Toolbox")]
[assembly: AssemblyTitle("Toolbox")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Toolbox
{
	[BepInPlugin("com.jeffehboy.toolbox", "Toolbox", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class ToolboxPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.jeffehboy.toolbox";

		public const string PluginName = "Toolbox";

		public const string PluginVersion = "1.0.0";

		public const string PrefabName = "piece_toolbox";

		private const string WorkbenchPrefab = "piece_workbench";

		private const float BodyLength = 1f;

		private const float BodyHeight = 0.35f;

		private const float BodyDepth = 0.35f;

		private const float LidLineHeight = 0.27f;

		internal static readonly bool EnableToolRepair;

		private const float ModelScale = 1.25f;

		private readonly Harmony harmony = new Harmony("com.jeffehboy.toolbox");

		private void Awake()
		{
			AddLocalizations();
			PrefabManager.OnVanillaPrefabsAvailable += AddToolboxPiece;
			PieceManager.OnPiecesRegistered += PlaceBesideWorkbench;
			harmony.PatchAll(typeof(ToolboxPlugin).Assembly);
			Logger.LogInfo((object)"Toolbox loaded");
		}

		private void AddLocalizations()
		{
			CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
			string text = "English";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				{ "piece_toolbox", "Toolbox" },
				{ "piece_toolbox_desc", "A small portable toolbox. Lets you build, repair and remove workbench pieces nearby. Cannot craft." },
				{ "piece_toolbox_repair", "Repair Tools" },
				{ "piece_toolbox_repaired", "Your basic tools are repaired." },
				{ "piece_toolbox_nothing", "Nothing to repair." }
			});
		}

		private void AddToolboxPiece()
		{
			//IL_0059: 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_006a: 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_0083: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Expected O, but got Unknown
			PrefabManager.OnVanillaPrefabsAvailable -= AddToolboxPiece;
			GameObject val = PrefabManager.Instance.CreateClonedPrefab("piece_toolbox", "piece_workbench");
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"Could not clone the workbench prefab");
				return;
			}
			StripWorkbenchParts(val);
			BuildModel(val);
			val.AddComponent<ToolboxMarker>();
			PieceConfig val2 = new PieceConfig
			{
				Name = "$piece_toolbox",
				Description = "$piece_toolbox_desc",
				PieceTable = PieceTables.Hammer
			};
			val2.AddRequirement(new RequirementConfig("Wood", 1, 0, true));
			Sprite val3 = LoadEmbeddedIcon("toolbox_icon.png") ?? TryRenderIcon(val);
			if ((Object)(object)val3 != (Object)null)
			{
				val2.Icon = val3;
			}
			PieceManager.Instance.AddPiece(new CustomPiece(val, true, val2));
		}

		private static void StripWorkbenchParts(GameObject toolbox)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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_002f: Invalid comparison between Unknown and I4
			EffectArea[] componentsInChildren = toolbox.GetComponentsInChildren<EffectArea>(true);
			foreach (EffectArea val in componentsInChildren)
			{
				val.m_type = (Type)(val.m_type & 0xFB);
				if ((int)val.m_type == 0)
				{
					Collider[] components = ((Component)val).GetComponents<Collider>();
					foreach (Collider val2 in components)
					{
						Object.DestroyImmediate((Object)(object)val2);
					}
					Object.DestroyImmediate((Object)(object)val);
				}
			}
			LODGroup[] componentsInChildren2 = toolbox.GetComponentsInChildren<LODGroup>(true);
			foreach (LODGroup val3 in componentsInChildren2)
			{
				Object.DestroyImmediate((Object)(object)val3);
			}
			MeshRenderer[] componentsInChildren3 = toolbox.GetComponentsInChildren<MeshRenderer>(true);
			foreach (MeshRenderer val4 in componentsInChildren3)
			{
				Object.DestroyImmediate((Object)(object)val4);
			}
			SkinnedMeshRenderer[] componentsInChildren4 = toolbox.GetComponentsInChildren<SkinnedMeshRenderer>(true);
			foreach (SkinnedMeshRenderer val5 in componentsInChildren4)
			{
				Object.DestroyImmediate((Object)(object)val5);
			}
			MeshFilter[] componentsInChildren5 = toolbox.GetComponentsInChildren<MeshFilter>(true);
			foreach (MeshFilter val6 in componentsInChildren5)
			{
				Object.DestroyImmediate((Object)(object)val6);
			}
			Collider[] componentsInChildren6 = toolbox.GetComponentsInChildren<Collider>(true);
			foreach (Collider val7 in componentsInChildren6)
			{
				Object.DestroyImmediate((Object)(object)val7);
			}
			List<GameObject> list = new List<GameObject>();
			Transform[] componentsInChildren7 = toolbox.GetComponentsInChildren<Transform>(true);
			foreach (Transform val8 in componentsInChildren7)
			{
				if ((Object)(object)val8 != (Object)(object)toolbox.transform && ((Component)val8).CompareTag("snappoint"))
				{
					list.Add(((Component)val8).gameObject);
				}
			}
			foreach (GameObject item in list)
			{
				Object.DestroyImmediate((Object)(object)item);
			}
			WearNTear component = toolbox.GetComponent<WearNTear>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_new = null;
				component.m_worn = null;
				component.m_broken = null;
			}
		}

		private static void BuildModel(GameObject toolbox)
		{
			//IL_0068: 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_0080: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00ef: 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_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: 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_0247: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_02da: Unknown result type (might be due to invalid IL or missing references)
			//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0310: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_0366: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
			GameObject beam = PrefabManager.Instance.GetPrefab("wood_beam_1") ?? PrefabManager.Instance.GetPrefab("wood_beam");
			MeshFilter val = FindBeamMesh(beam);
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"Could not find the wood beam mesh; toolbox will be invisible");
				return;
			}
			Mesh sharedMesh = val.sharedMesh;
			Material[] sharedMaterials = ((Renderer)((Component)val).GetComponent<MeshRenderer>()).sharedMaterials;
			Material[] mats = MakeSolid(sharedMaterials, new Color32((byte)52, (byte)50, (byte)48, byte.MaxValue));
			GameObject val2 = new GameObject("model");
			val2.layer = toolbox.layer;
			val2.transform.SetParent(toolbox.transform, false);
			val2.transform.localScale = Vector3.one * 1.25f;
			AddBox(val2.transform, "body", sharedMesh, sharedMaterials, new Vector3(1f, 0.35f, 0.35f), new Vector3(0f, 0.175f, 0f));
			AddBox(val2.transform, "lid_band", sharedMesh, mats, new Vector3(1.02f, 0.025f, 0.37f), new Vector3(0f, 0.27f, 0f));
			float[] array = new float[2] { -0.4f, 0.4f };
			foreach (float num in array)
			{
				AddBox(val2.transform, "strap", sharedMesh, mats, new Vector3(0.05f, 0.37f, 0.37f), new Vector3(num, 0.175f, 0f));
			}
			float num2 = 0.175f;
			AddBox(val2.transform, "latch", sharedMesh, mats, new Vector3(0.07f, 0.09f, 0.03f), new Vector3(0f, 0.26000002f, num2 + 0.01f));
			float[] array2 = new float[2] { -0.25f, 0.25f };
			foreach (float num3 in array2)
			{
				AddBox(val2.transform, "hinge_plate", sharedMesh, mats, new Vector3(0.08f, 0.08f, 0.03f), new Vector3(num3, 0.27f, 0f - (num2 + 0.01f)));
				AddBox(val2.transform, "hinge_knuckle", sharedMesh, mats, new Vector3(0.1f, 0.03f, 0.03f), new Vector3(num3, 0.27f, 0f - (num2 + 0.025f)));
			}
			float num4 = 0.08f;
			float num5 = 0.04f;
			float num6 = 0.35f + num4 / 2f;
			AddBox(val2.transform, "handle_post_l", sharedMesh, sharedMaterials, new Vector3(0.05f, num4, 0.05f), new Vector3(-0.2f, num6, 0f));
			AddBox(val2.transform, "handle_post_r", sharedMesh, sharedMaterials, new Vector3(0.05f, num4, 0.05f), new Vector3(0.2f, num6, 0f));
			AddBox(val2.transform, "handle_bar", sharedMesh, sharedMaterials, new Vector3(0.46f, num5, 0.06f), new Vector3(0f, 0.35f + num4 + num5 / 2f, 0f));
			float num7 = 0.35f + num4 + num5;
			BoxCollider val3 = val2.AddComponent<BoxCollider>();
			val3.size = new Vector3(1f, num7, 0.35f);
			val3.center = new Vector3(0f, num7 / 2f, 0f);
		}

		private static MeshFilter FindBeamMesh(GameObject beam)
		{
			if ((Object)(object)beam == (Object)null)
			{
				return null;
			}
			WearNTear component = beam.GetComponent<WearNTear>();
			GameObject val = (((Object)(object)component != (Object)null && (Object)(object)component.m_new != (Object)null) ? component.m_new : beam);
			MeshFilter val2 = null;
			MeshFilter[] componentsInChildren = val.GetComponentsInChildren<MeshFilter>(true);
			foreach (MeshFilter val3 in componentsInChildren)
			{
				if (!((Object)(object)val3.sharedMesh == (Object)null) && !((Object)(object)((Component)val3).GetComponent<MeshRenderer>() == (Object)null) && ((Object)(object)val2 == (Object)null || val3.sharedMesh.vertexCount > val2.sharedMesh.vertexCount))
				{
					val2 = val3;
				}
			}
			return val2;
		}

		private static void AddBox(Transform parent, string name, Mesh mesh, Material[] mats, Vector3 size, Vector3 center)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0043: 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_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_0052: 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_006e: 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_0060: 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_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: 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_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: 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_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: 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)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(name);
			val.layer = ((Component)parent).gameObject.layer;
			val.transform.SetParent(parent, false);
			val.AddComponent<MeshFilter>().sharedMesh = mesh;
			((Renderer)val.AddComponent<MeshRenderer>()).sharedMaterials = mats;
			Bounds bounds = mesh.bounds;
			Vector3 size2 = ((Bounds)(ref bounds)).size;
			Quaternion val2;
			Vector3 val3 = default(Vector3);
			switch ((!(size2.x >= size2.y) || !(size2.x >= size2.z)) ? ((size2.y >= size2.z) ? 1 : 2) : 0)
			{
			case 1:
				val2 = Quaternion.Euler(0f, 0f, -90f);
				((Vector3)(ref val3))..ctor(size.y / size2.x, size.x / size2.y, size.z / size2.z);
				break;
			case 2:
				val2 = Quaternion.Euler(0f, 90f, 0f);
				((Vector3)(ref val3))..ctor(size.z / size2.x, size.y / size2.y, size.x / size2.z);
				break;
			default:
				val2 = Quaternion.identity;
				((Vector3)(ref val3))..ctor(size.x / size2.x, size.y / size2.y, size.z / size2.z);
				break;
			}
			val.transform.localRotation = val2;
			val.transform.localScale = val3;
			Transform transform = val.transform;
			Quaternion val4 = val2;
			Vector3 val5 = val3;
			bounds = mesh.bounds;
			transform.localPosition = center - val4 * Vector3.Scale(val5, ((Bounds)(ref bounds)).center);
		}

		private static Material[] MakeSolid(Material[] mats, Color32 color)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0017: 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_0067: 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_0088: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(4, 4);
			Color32[] array = (Color32[])(object)new Color32[16];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = color;
			}
			val.SetPixels32(array);
			val.Apply();
			Material[] array2 = (Material[])(object)new Material[mats.Length];
			for (int j = 0; j < mats.Length; j++)
			{
				if ((Object)(object)mats[j] == (Object)null)
				{
					continue;
				}
				Material val2 = new Material(mats[j])
				{
					name = ((Object)mats[j]).name + "_toolbox_iron"
				};
				if (val2.HasProperty("_MainTex"))
				{
					val2.SetTexture("_MainTex", (Texture)(object)val);
				}
				string[] array3 = new string[2] { "_Color", "_MainColor" };
				foreach (string text in array3)
				{
					if (val2.HasProperty(text))
					{
						val2.SetColor(text, Color.white);
					}
				}
				array2[j] = val2;
			}
			return array2;
		}

		private static Sprite LoadEmbeddedIcon(string fileName)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Assembly assembly = typeof(ToolboxPlugin).Assembly;
				string[] manifestResourceNames = assembly.GetManifestResourceNames();
				foreach (string text in manifestResourceNames)
				{
					if (!text.EndsWith(fileName, StringComparison.OrdinalIgnoreCase))
					{
						continue;
					}
					using Stream stream = assembly.GetManifestResourceStream(text);
					using MemoryStream memoryStream = new MemoryStream();
					stream.CopyTo(memoryStream);
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					MethodInfo methodInfo = Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule")?.GetMethod("LoadImage", new Type[2]
					{
						typeof(Texture2D),
						typeof(byte[])
					});
					if (methodInfo == null || !(bool)methodInfo.Invoke(null, new object[2]
					{
						val,
						memoryStream.ToArray()
					}))
					{
						return null;
					}
					return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
				}
				Logger.LogWarning((object)(fileName + " not found in the DLL; using a rendered icon"));
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("Could not load " + fileName + ": " + ex.Message));
			}
			return null;
		}

		private static Sprite TryRenderIcon(GameObject prefab)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				return RenderManager.Instance.Render(prefab, RenderManager.IsometricRotation);
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("Icon render failed, using workbench icon: " + ex.Message));
				return null;
			}
		}

		private static void PlaceBesideWorkbench()
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab("Hammer");
			PieceTable val = ((prefab == null) ? null : prefab.GetComponent<ItemDrop>()?.m_itemData?.m_shared?.m_buildPieces);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			List<GameObject> pieces = val.m_pieces;
			int num = pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == "piece_toolbox");
			if (num >= 0)
			{
				GameObject item = pieces[num];
				pieces.RemoveAt(num);
				int num2 = pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == "piece_workbench");
				pieces.Insert((num2 < 0) ? pieces.Count : (num2 + 1), item);
			}
		}

		internal static bool IsToolbox(Component c)
		{
			return (Object)(object)c != (Object)null && (Object)(object)c.GetComponent<ToolboxMarker>() != (Object)null;
		}
	}
	public class ToolboxMarker : MonoBehaviour
	{
	}
	[HarmonyPatch(typeof(CraftingStation), "Interact")]
	internal static class Patch_Interact
	{
		private static readonly HashSet<string> RepairableItems = new HashSet<string> { "Hammer", "$item_hammer", "Hoe", "$item_hoe" };

		private static bool IsBasicTool(ItemData item)
		{
			if (item?.m_shared == null)
			{
				return false;
			}
			if (RepairableItems.Contains(item.m_shared.m_name))
			{
				return true;
			}
			return (Object)(object)item.m_dropPrefab != (Object)null && RepairableItems.Contains(((Object)item.m_dropPrefab).name);
		}

		private static bool Prefix(CraftingStation __instance, object[] __args, ref bool __result)
		{
			//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_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			if (!ToolboxPlugin.IsToolbox((Component)(object)__instance))
			{
				return true;
			}
			__result = false;
			if (!ToolboxPlugin.EnableToolRepair)
			{
				return false;
			}
			Player val = (Player)((__args.Length != 0) ? /*isinst with value type is only supported in some contexts*/: null);
			bool flag = default(bool);
			int num;
			if (__args.Length > 1)
			{
				object obj = __args[1];
				if (obj is bool)
				{
					flag = (bool)obj;
					num = 1;
				}
				else
				{
					num = 0;
				}
			}
			else
			{
				num = 0;
			}
			bool flag2 = (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			if ((Object)(object)val == (Object)null || flag2 || (Object)(object)val != (Object)(object)Player.m_localPlayer)
			{
				return false;
			}
			int num2 = 0;
			foreach (ItemData allItem in ((Humanoid)val).GetInventory().GetAllItems())
			{
				if (IsBasicTool(allItem) && allItem.m_shared.m_useDurability)
				{
					float maxDurability = allItem.GetMaxDurability();
					if (!(allItem.m_durability >= maxDurability))
					{
						allItem.m_durability = maxDurability;
						num2++;
					}
				}
			}
			if (num2 > 0)
			{
				EffectList repairItemDoneEffects = __instance.m_repairItemDoneEffects;
				if (repairItemDoneEffects != null)
				{
					repairItemDoneEffects.Create(((Component)__instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
				}
				((Character)val).Message((MessageType)2, Localization.instance.Localize("$piece_toolbox_repaired"), 0, (Sprite)null, false);
			}
			else
			{
				((Character)val).Message((MessageType)2, Localization.instance.Localize("$piece_toolbox_nothing"), 0, (Sprite)null, false);
			}
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(CraftingStation), "GetHoverText")]
	internal static class Patch_HoverText
	{
		private static bool Prefix(CraftingStation __instance, ref string __result)
		{
			if (!ToolboxPlugin.IsToolbox((Component)(object)__instance))
			{
				return true;
			}
			__result = Localization.instance.Localize(ToolboxPlugin.EnableToolRepair ? "$piece_toolbox\n[<color=yellow><b>$KEY_Use</b></color>] $piece_toolbox_repair" : "$piece_toolbox");
			return false;
		}
	}
	[HarmonyPatch(typeof(CraftingStation), "GetHoverName")]
	internal static class Patch_HoverName
	{
		private static bool Prefix(CraftingStation __instance, ref string __result)
		{
			if (!ToolboxPlugin.IsToolbox((Component)(object)__instance))
			{
				return true;
			}
			__result = Localization.instance.Localize("$piece_toolbox");
			return false;
		}
	}
	[HarmonyPatch(typeof(CraftingStation), "FindClosestStationInRange")]
	internal static class Patch_FindClosestStation
	{
		private static void Postfix(object[] __args, ref CraftingStation __result)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
			if (!ToolboxPlugin.IsToolbox((Component)(object)__result))
			{
				return;
			}
			string text = (string)__args[0];
			Vector3 val = (Vector3)__args[1];
			float num = (float)__args[2];
			__result = null;
			float num2 = float.MaxValue;
			if (!(AccessTools.Field(typeof(CraftingStation), "m_allStations")?.GetValue(null) is List<CraftingStation> list))
			{
				return;
			}
			foreach (CraftingStation item in list)
			{
				if (!((Object)(object)item == (Object)null) && !(item.m_name != text) && !ToolboxPlugin.IsToolbox((Component)(object)item))
				{
					float num3 = Vector3.Distance(((Component)item).transform.position, val);
					if (num3 < num && num3 < num2)
					{
						num2 = num3;
						__result = item;
					}
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class Patch_FindStationsInRange
	{
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method(typeof(CraftingStation), "FindStationsInRange", (Type[])null, (Type[])null);
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		private static void Postfix(object[] __args)
		{
			foreach (object obj in __args)
			{
				if (obj is List<CraftingStation> list)
				{
					list.RemoveAll((CraftingStation s) => ToolboxPlugin.IsToolbox((Component)(object)s));
				}
			}
		}
	}
}