Decompiled source of Build Without Stations v1.0.0

plugins/FreeBuild.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FreeBuild")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FreeBuild")]
[assembly: AssemblyTitle("FreeBuild")]
[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 BenjSammy.FreeBuild
{
	[BepInPlugin("benjsammy.freebuild", "Build Without Stations by BenjSammy", "1.0.0")]
	public sealed class FreeBuild : BaseUnityPlugin
	{
		private const string Id = "benjsammy.freebuild";

		private static FieldRef<Piece, CraftingStation> pieceStation;

		private static FieldRef<CraftingStation, bool> needsRoof;

		private static FieldRef<CraftingStation, string> stationName;

		[ThreadStatic]
		private static Player removingPlayer;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			pieceStation = AccessTools.FieldRefAccess<Piece, CraftingStation>("m_craftingStation");
			needsRoof = AccessTools.FieldRefAccess<CraftingStation, bool>("m_craftRequireRoof");
			stationName = AccessTools.FieldRefAccess<CraftingStation, string>("m_name");
			harmony = new Harmony("benjsammy.freebuild");
			try
			{
				harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "HaveRequirements", new Type[2]
				{
					typeof(Piece),
					AccessTools.Inner(typeof(Player), "RequirementMode")
				}, (Type[])null), Hook("BuildPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("RestoreStation"), (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "Repair", new Type[2]
				{
					typeof(ItemData),
					typeof(Piece)
				}, (Type[])null), Hook("RepairPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("RestoreStation"), (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "RemovePiece", Type.EmptyTypes, (Type[])null), Hook("RemovePrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("RestoreRemovingPlayer"), (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "CheckCanRemovePiece", new Type[1] { typeof(Piece) }, (Type[])null), Hook("RemovalStationPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("RestoreStation"), (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.Method(typeof(CraftingStation), "CheckUsable", new Type[2]
				{
					typeof(Player),
					typeof(bool)
				}, (Type[])null), Hook("RoofPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("RestoreRoof"), (HarmonyMethod)null);
			}
			catch
			{
				harmony.UnpatchSelf();
				throw;
			}
		}

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

		private static HarmonyMethod Hook(string method)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			return new HarmonyMethod(typeof(FreeBuild), method, (Type[])null);
		}

		private static void BuildPrefix(Player __instance, Piece __0, out CraftingStation __state)
		{
			__state = null;
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				__state = TemporarilyRemoveStation(__0);
			}
		}

		private static bool RepairPrefix(Player __instance, Piece __1, out CraftingStation __state)
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			__state = null;
			if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || (Object)(object)__1 == (Object)null)
			{
				return true;
			}
			if ((Object)(object)((Component)__1).GetComponentInParent<Ship>() == (Object)null && (Object)(object)((Component)__1).GetComponentInChildren<Ship>(true) == (Object)null && (Object)(object)((Component)__1).GetComponentInParent<Vagon>() == (Object)null && (Object)(object)((Component)__1).GetComponentInChildren<Vagon>(true) == (Object)null)
			{
				__state = TemporarilyRemoveStation(__1);
				return true;
			}
			CraftingStation val = pieceStation.Invoke(__1);
			if ((Object)(object)CraftingStation.HaveBuildStationInRange(((Object)(object)val != (Object)null) ? stationName.Invoke(val) : "$piece_workbench", ((Component)__1).transform.position) != (Object)null)
			{
				return true;
			}
			((Character)__instance).Message((MessageType)2, "$msg_missingstation", 0, (Sprite)null);
			return false;
		}

		private static void RemovePrefix(Player __instance, out Player __state)
		{
			__state = removingPlayer;
			removingPlayer = __instance;
		}

		private static void RestoreRemovingPlayer(Player __state)
		{
			removingPlayer = __state;
		}

		private static void RemovalStationPrefix(Player __instance, Piece __0, out CraftingStation __state)
		{
			__state = null;
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer && (Object)(object)__instance == (Object)(object)removingPlayer)
			{
				__state = TemporarilyRemoveStation(__0);
			}
		}

		private static CraftingStation TemporarilyRemoveStation(Piece piece)
		{
			if ((Object)(object)piece == (Object)null)
			{
				return null;
			}
			CraftingStation result = pieceStation.Invoke(piece);
			pieceStation.Invoke(piece) = null;
			return result;
		}

		private static void RestoreStation(CraftingStation __state, object[] __args)
		{
			if (!((Object)(object)__state == (Object)null))
			{
				object obj = __args[0];
				Piece val = (Piece)(((obj is Piece) ? obj : null) ?? ((object)/*isinst with value type is only supported in some contexts*/));
				if ((Object)(object)val != (Object)null)
				{
					pieceStation.Invoke(val) = __state;
				}
			}
		}

		private static void RoofPrefix(CraftingStation __instance, Player __0, out bool __state)
		{
			__state = (Object)(object)__0 != (Object)null && (Object)(object)__0 == (Object)(object)Player.m_localPlayer && needsRoof.Invoke(__instance);
			if (__state)
			{
				needsRoof.Invoke(__instance) = false;
			}
		}

		private static void RestoreRoof(CraftingStation __instance, bool __state)
		{
			if (__state)
			{
				needsRoof.Invoke(__instance) = true;
			}
		}
	}
}