Decompiled source of The Mod That Fixes Barter tm v1.0.0

ChessBarterAdvanced.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
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("ChessBarterAdvanced")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("N/A")]
[assembly: AssemblyProduct("ChessBarterAdvanced")]
[assembly: AssemblyCopyright("Copyright © N/A 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("66ae9a35-031b-4ddb-a13e-f1fcbfcc79cd")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ChessBarterAdvanced;

[BepInPlugin("spapi.etg.barterfix", "Barter Fix", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "spapi.etg.barterfix";

	public static MethodInfo ocu = AccessTools.Method(typeof(Plugin), "OverrideCanUse", (Type[])null, (Type[])null);

	public static MethodInfo ode = AccessTools.Method(typeof(Plugin), "OverrideDoEffect", (Type[])null, (Type[])null);

	public static MethodInfo oc = AccessTools.Method(typeof(Plugin), "OverrideCheck", (Type[])null, (Type[])null);

	public static MethodInfo inequality = AccessTools.Method(typeof(Object), "op_Inequality", (Type[])null, (Type[])null);

	public void Awake()
	{
		ETGModMainBehaviour.WaitForGameManagerStart((Action<GameManager>)delegate
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			Type type = AccessTools.TypeByName("FrostAndGunfireItems.Barter");
			if ((object)type != null)
			{
				Harmony val = new Harmony("spapi.etg.barterfix");
				MethodInfo methodInfo = AccessTools.Method(type, "CanBeUsed", (Type[])null, (Type[])null);
				MethodInfo methodInfo2 = AccessTools.Method(type, "DoEffect", (Type[])null, (Type[])null);
				if ((object)methodInfo != null)
				{
					val.Patch((MethodBase)methodInfo, new HarmonyMethod(ocu), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				if ((object)methodInfo2 != null)
				{
					val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(ode), (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		});
	}

	public static bool OverrideCanUse(ref bool __result, PlayerController user)
	{
		if ((Object)(object)user == (Object)null || user.CurrentRoom == null || user.carriedConsumables == null)
		{
			__result = false;
			return false;
		}
		return true;
	}

	public static IEnumerable<CodeInstruction> OverrideDoEffect(IEnumerable<CodeInstruction> instructions)
	{
		bool firstInequality = false;
		Label i = default(Label);
		bool managedToGetLabel = false;
		try
		{
			CodeInstruction instruction2 = instructions.Where((CodeInstruction x) => x.opcode == OpCodes.Ldloca_S).Skip(1).First();
			i = instruction2.labels[0];
			managedToGetLabel = true;
		}
		catch
		{
		}
		foreach (CodeInstruction instruction in instructions)
		{
			yield return instruction;
			if (!firstInequality && CodeInstructionExtensions.Calls(instruction, inequality))
			{
				firstInequality = true;
				yield return new CodeInstruction(OpCodes.Ldloc_S, (object)4);
				yield return new CodeInstruction(OpCodes.Call, (object)oc);
			}
			if (managedToGetLabel && instruction.opcode == OpCodes.Stloc_2)
			{
				yield return new CodeInstruction(OpCodes.Ldloc_2, (object)null);
				yield return new CodeInstruction(OpCodes.Brfalse, (object)i);
			}
		}
	}

	public static bool OverrideCheck(bool curr, ShopItemController sic)
	{
		return curr && (Object)(object)sic != (Object)null && (Object)(object)sic.item != (Object)null;
	}
}