Decompiled source of Smeltolingo v1.0.1

SmeltolingoMod.dll

Decompiled 2 hours ago
using System.Collections.Generic;
using System.Diagnostics;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SmeltolingoMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SmeltolingoMod")]
[assembly: AssemblyTitle("SmeltolingoMod")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SmeltolingoMod;

[BepInPlugin("com.smeltolingo.autosmeltolingo", "AutoSmeltolingo", "1.0.0")]
public class AutoSmeltolingoPlugin : BaseUnityPlugin
{
	private Harmony _harmony;

	private void Awake()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		_harmony = new Harmony("sevolingo.autosmelter");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"AutoSmeltolingo plugin is loaded!");
	}
}
[HarmonyPatch(typeof(Smelter), "UpdateSmelter")]
public class SmelterPatch
{
	private static Dictionary<Smelter, float> lastRunMap = new Dictionary<Smelter, float>();

	private static string[] ores = new string[4] { "$item_copperore", "$item_tinore", "$item_ironore", "$item_silverore" };

	private static void Postfix(Smelter __instance)
	{
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance == (Object)null)
		{
			return;
		}
		ZNetView nView = GetNView(__instance);
		if ((Object)(object)nView == (Object)null || !nView.IsValid())
		{
			return;
		}
		ZDO zDO = nView.GetZDO();
		float num = ((zDO != null) ? zDO.GetFloat("fuel", 0f) : 0f);
		if (lastRunMap.TryGetValue(__instance, out var value) && Time.time - value < 2f)
		{
			return;
		}
		lastRunMap[__instance] = Time.time;
		List<Container> list = FindNearbyChests(((Component)__instance).transform.position, 10f);
		foreach (Container item in list)
		{
			Inventory inventory = item.GetInventory();
			if (inventory == null)
			{
				continue;
			}
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (allItem == null)
				{
					continue;
				}
				if (allItem.m_shared.m_name == "$item_coal" && num < 18f)
				{
					inventory.RemoveItem(allItem, 1);
					if (nView.IsOwner())
					{
						nView.InvokeRPC("RPC_AddFuel", new object[1] { 1 });
					}
					return;
				}
				if (!ores.Equals(allItem.m_shared.m_name))
				{
					continue;
				}
				int num2 = (int)typeof(Smelter).GetMethod("GetQueueSize", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(__instance, null);
				if (num2 < 10 && nView.IsValid())
				{
					nView.InvokeRPC(ZNetView.Everybody, "RPC_AddOre", new object[1] { ((Object)allItem.m_dropPrefab).name });
					inventory.RemoveItem(allItem, 1);
				}
				return;
			}
		}
	}

	private static ZNetView GetNView(Smelter smelter)
	{
		object? obj = typeof(Smelter).GetField("m_nview", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(smelter);
		return (ZNetView)((obj is ZNetView) ? obj : null);
	}

	private static List<Container> FindNearbyChests(Vector3 center, float radius)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		List<Container> list = new List<Container>();
		Collider[] array = Physics.OverlapSphere(center, radius);
		Collider[] array2 = array;
		foreach (Collider val in array2)
		{
			if (!((Object)(object)val == (Object)null))
			{
				Container componentInParent = ((Component)val).GetComponentInParent<Container>();
				if (!((Object)(object)componentInParent == (Object)null) && !list.Contains(componentInParent))
				{
					list.Add(componentInParent);
				}
			}
		}
		return list;
	}
}