Decompiled source of SmarterHoeUnlocks v1.2.0

SmarterHoeUnlocks.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Utils;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("computer.olivia.smarterhoeunlocks", "SmarterHoe Unlocks", "1.2.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public sealed class SmarterHoeUnlocks : BaseUnityPlugin
{
	private static readonly FieldInfo KnownStations = AccessTools.Field(typeof(Player), "m_knownStations");

	private static Func<Piece, GameObject, bool> isTerrainTool;

	private static MethodInfo activeCostMode;

	private static MethodInfo globalCostMode;

	private static FieldInfo requireField;

	private static MethodInfo requireValue;

	private const string Stonecutter = "$piece_stonecutter";

	private void Awake()
	{
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_023b: Expected O, but got Unknown
		//IL_0250: Unknown result type (might be due to invalid IL or missing references)
		//IL_0257: Expected O, but got Unknown
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_028b: Expected O, but got Unknown
		//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Expected O, but got Unknown
		//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02eb: Expected O, but got Unknown
		Type type = AccessTools.TypeByName("SmarterHoe.Plugin");
		Type type2 = AccessTools.TypeByName("SmarterHoe.CustomPieces.CustomPieceManager");
		MethodInfo methodInfo = ((!(type == null)) ? AccessTools.PropertyGetter(type, "RequireCraftingStation") : null);
		MethodInfo checkInRange = ((!(type == null)) ? AccessTools.Method(type, "CheckCraftingStationInRange", (Type[])null, (Type[])null) : null);
		MethodInfo methodInfo2 = ((!(type2 == null)) ? AccessTools.Method(type2, "IsTerrainToolPiece", (Type[])null, (Type[])null) : null);
		activeCostMode = ((!(type == null)) ? AccessTools.Method(type, "GetActiveCostMode", (Type[])null, (Type[])null) : null);
		globalCostMode = ((!(type == null)) ? AccessTools.PropertyGetter(type, "CostMode") : null);
		MethodInfo methodInfo3 = AccessTools.Method(typeof(Player), "HaveRequirements", new Type[2]
		{
			typeof(Piece),
			typeof(RequirementMode)
		}, (Type[])null);
		requireField = ((!(type == null)) ? AccessTools.Field(type, "_requireCraftingStation") : null);
		requireValue = AccessTools.PropertyGetter(typeof(ConfigEntry<bool>), "Value");
		List<MethodInfo> list = ((!(type == null) && !(requireField == null) && !(checkInRange == null)) ? (from m in AccessTools.GetDeclaredMethods(type)
			where m != checkInRange && ReadsSettingThenChecks(m, checkInRange)
			select m).ToList() : new List<MethodInfo>());
		if (KnownStations == null || requireField == null || list.Count == 0 || methodInfo == null || checkInRange == null || methodInfo2 == null || activeCostMode == null || globalCostMode == null || methodInfo3 == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"SmarterHoe methods not found (new SmarterHoe version?). Nothing patched; SmarterHoe keeps its own range checks.");
			return;
		}
		isTerrainTool = AccessTools.MethodDelegate<Func<Piece, GameObject, bool>>(methodInfo2, (object)null, true);
		Harmony val = new Harmony("computer.olivia.smarterhoeunlocks");
		MethodInfo methodInfo4 = methodInfo;
		HarmonyMethod val2 = new HarmonyMethod(typeof(SmarterHoeUnlocks), "NoRangeCheck", (Type[])null);
		val.Patch((MethodBase)methodInfo4, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)checkInRange, new HarmonyMethod(typeof(SmarterHoeUnlocks), "StationUnlocked", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		methodInfo4 = methodInfo3;
		val2 = new HarmonyMethod(typeof(SmarterHoeUnlocks), "PieceStationUnlocked", (Type[])null);
		val.Patch((MethodBase)methodInfo4, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		foreach (MethodInfo item in list)
		{
			methodInfo4 = item;
			val2 = new HarmonyMethod(typeof(SmarterHoeUnlocks), "AlwaysCheck", (Type[])null);
			val.Patch((MethodBase)methodInfo4, (HarmonyMethod)null, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("SmarterHoe station checks now need the station unlocked, never nearby ({0} tools: {1}).", list.Count, string.Join(", ", list.Select((MethodInfo m) => m.Name))));
	}

	private static bool ReadsSettingThenChecks(MethodInfo method, MethodInfo checkInRange)
	{
		try
		{
			List<CodeInstruction> originalInstructions = PatchProcessor.GetOriginalInstructions((MethodBase)method, (ILGenerator)null);
			return originalInstructions.Any((CodeInstruction c) => CodeInstructionExtensions.LoadsField(c, requireField, false)) && originalInstructions.Any((CodeInstruction c) => CodeInstructionExtensions.Calls(c, checkInRange));
		}
		catch
		{
			return false;
		}
	}

	private static IEnumerable<CodeInstruction> AlwaysCheck(IEnumerable<CodeInstruction> instructions)
	{
		List<CodeInstruction> list = instructions.ToList();
		for (int i = 0; i < list.Count - 1; i++)
		{
			if (CodeInstructionExtensions.LoadsField(list[i], requireField, false) && CodeInstructionExtensions.Calls(list[i + 1], requireValue))
			{
				list[i].opcode = OpCodes.Ldc_I4_1;
				list[i].operand = null;
				list[i + 1].opcode = OpCodes.Nop;
				list[i + 1].operand = null;
			}
		}
		return list;
	}

	private static bool Knows(Player player, string station)
	{
		if (string.IsNullOrEmpty(station))
		{
			return true;
		}
		string text = ((!station.StartsWith("$")) ? ("$" + station) : station);
		if (text != "$piece_stonecutter")
		{
			return true;
		}
		return KnownStations.GetValue(player) is Dictionary<string, int> dictionary && dictionary.ContainsKey(text);
	}

	private static bool IsFreebuild(object mode)
	{
		return mode != null && mode.ToString() == "Freebuild";
	}

	private static void NoRangeCheck(ref bool __result)
	{
		__result = false;
	}

	private static bool StationUnlocked(string stationName, Player player, ref bool __result)
	{
		__result = true;
		if ((Object)(object)player == (Object)null || player.NoCostCheat() || IsFreebuild(globalCostMode.Invoke(null, null)))
		{
			return false;
		}
		if (Knows(player, stationName))
		{
			return false;
		}
		string text = ((!stationName.StartsWith("$")) ? ("$" + stationName) : stationName);
		((Character)player).Message((MessageType)2, "$msg_missingstation: " + text, 0, (Sprite)null, false);
		__result = false;
		return false;
	}

	private static void PieceStationUnlocked(Player __instance, Piece piece, ref bool __result)
	{
		if (__result && !((Object)(object)piece == (Object)null) && !((Object)(object)piece.m_craftingStation == (Object)null) && !Knows(__instance, piece.m_craftingStation.m_name) && isTerrainTool(piece, null) && !__instance.NoCostCheat() && !IsFreebuild(activeCostMode.Invoke(null, new object[2] { __instance, false })))
		{
			__result = false;
		}
	}
}