Decompiled source of BuyResources v1.1.1

BuyResourcesMod.dll

Decompiled a month 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 HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName = ".NET Framework 4.6.1")]
[assembly: AssemblyCompany("BuyResourcesMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BuyResourcesMod")]
[assembly: AssemblyTitle("BuyResourcesMod")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.tawoly.buyres", "Buy Resources", "1.1.0")]
public class BuyResourcesPlugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("com.tawoly.buyres").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"BuyResourcesMod: Loaded and Harmony-Patches active");
	}
}
[HarmonyPatch]
public static class TerminalManagerAwakePatch
{
	public static GameController gameController;

	public static GameObject[] itemsToAdd;

	[HarmonyPostfix]
	[HarmonyPatch(typeof(TerminalManager), "Awake")]
	public static void AfterAwake(TerminalManager __instance)
	{
		gameController = Object.FindObjectOfType<GameController>();
		itemsToAdd = FindItems();
	}

	private static GameObject[] FindItems()
	{
		string[] prefabNames = new string[4] { "hazard_cone", "copper_ingot", "steel_ingot", "water_pouch" };
		return (from go in Resources.FindObjectsOfTypeAll<GameObject>()
			where prefabNames.Contains(((Object)go).name)
			select go).ToArray();
	}
}
[HarmonyPatch]
public static class TerminalInterpreterPatches
{
	private static TerminalInterpreter interpreter;

	[HarmonyPostfix]
	[HarmonyPatch(typeof(TerminalInterpreter), "OpenShopCommand")]
	public static void AfterOpenShopCmd(TerminalInterpreter __instance)
	{
		interpreter = __instance;
		refreshItems();
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(TerminalInterpreter), "BuyItemCommand")]
	public static bool BeforeBuyItemCmd(TerminalInterpreter __instance)
	{
		interpreter = __instance;
		refreshItems();
		return true;
	}

	public static void refreshItems()
	{
		//IL_004d: 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_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected I4, but got Unknown
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		if (interpreter?.shopCatalog?.items == null || (Object)(object)TerminalManagerAwakePatch.gameController == (Object)null)
		{
			return;
		}
		foreach (KeyValuePair<ResourceTypes, int> resourceRate in TerminalManagerAwakePatch.gameController.resourceRates)
		{
			ResourceTypes key = resourceRate.Key;
			GameObject pNewItem;
			switch ((int)key)
			{
			case 3:
				pNewItem = ((IEnumerable<GameObject>)TerminalManagerAwakePatch.itemsToAdd).FirstOrDefault((Func<GameObject, bool>)((GameObject p) => ((Object)p).name == "hazard_cone"));
				break;
			case 1:
				pNewItem = ((IEnumerable<GameObject>)TerminalManagerAwakePatch.itemsToAdd).FirstOrDefault((Func<GameObject, bool>)((GameObject p) => ((Object)p).name == "copper_ingot"));
				break;
			case 0:
				pNewItem = ((IEnumerable<GameObject>)TerminalManagerAwakePatch.itemsToAdd).FirstOrDefault((Func<GameObject, bool>)((GameObject p) => ((Object)p).name == "steel_ingot"));
				break;
			case 2:
				pNewItem = ((IEnumerable<GameObject>)TerminalManagerAwakePatch.itemsToAdd).FirstOrDefault((Func<GameObject, bool>)((GameObject p) => ((Object)p).name == "water_pouch"));
				break;
			default:
				Debug.LogWarning((object)$"BuyResourcesMod: Prefab for '{resourceRate.Key}' not found!");
				continue;
			}
			addItem(interpreter, pNewItem, resourceRate.Value * 2 * 2);
		}
	}

	private static void addItem(TerminalInterpreter interpreter, GameObject pNewItem, int pPrice)
	{
		if (Array.Exists(interpreter.shopCatalog.items, (ShopItem i) => (Object)(object)i != (Object)null && (Object)(object)i.item == (Object)(object)pNewItem))
		{
			removeItem(interpreter, pNewItem);
		}
		if (!((Object)(object)pNewItem == (Object)null) && pPrice >= 0)
		{
			ShopItem val = ScriptableObject.CreateInstance<ShopItem>();
			val.item = pNewItem;
			val.price = pPrice;
			interpreter.shopCatalog.items = interpreter.shopCatalog.items.Concat((IEnumerable<ShopItem>)(object)new ShopItem[1] { val }).ToArray();
		}
	}

	private static void removeItem(TerminalInterpreter interpreter, GameObject pItem)
	{
		interpreter.shopCatalog.items = interpreter.shopCatalog.items.Where((ShopItem i) => (Object)(object)i != (Object)null && ((Object)i.item).name != ((Object)pItem).name).ToArray();
	}
}