Decompiled source of SmelterQueue v1.0.3

SmelterQueue.dll

Decompiled 2 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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: AssemblyTitle("SmelterQueue")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SmelterQueue")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7a4e200a-49d4-4905-9fc5-86d8f9fc46b6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SmelterQueue;

[BepInPlugin("andrew.smelterqueue", "Smelter Queue", "1.0.3")]
public class SmelterQueuePlugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("andrew.smelterqueue").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Smelter Queue mod loaded OK");
	}

	private void Update()
	{
		if (!Input.GetKeyDown((KeyCode)103))
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)localPlayer == (Object)null))
		{
			Smelter hoveredSmelter = GetHoveredSmelter();
			if (!((Object)(object)hoveredSmelter == (Object)null))
			{
				FilterTools.CycleFilter(hoveredSmelter, localPlayer);
			}
		}
	}

	public static Smelter GetHoveredSmelter()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return null;
		}
		GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
		if ((Object)(object)hoverObject == (Object)null)
		{
			return null;
		}
		return hoverObject.GetComponentInParent<Smelter>();
	}

	public static Smelter GetHoveredOreIntake()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return null;
		}
		GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
		if ((Object)(object)hoverObject == (Object)null)
		{
			return null;
		}
		Smelter componentInParent = hoverObject.GetComponentInParent<Smelter>();
		if ((Object)(object)componentInParent == (Object)null)
		{
			return null;
		}
		Switch componentInParent2 = hoverObject.GetComponentInParent<Switch>();
		if ((Object)(object)componentInParent2 == (Object)null)
		{
			return null;
		}
		if ((Object)(object)componentInParent2 != (Object)(object)componentInParent.m_addOreSwitch)
		{
			return null;
		}
		return componentInParent;
	}

	private void OnGUI()
	{
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Expected O, but got Unknown
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_0277: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		Smelter hoveredOreIntake = GetHoveredOreIntake();
		if ((Object)(object)hoveredOreIntake == (Object)null)
		{
			return;
		}
		ZDO zDO = FilterTools.GetZDO(hoveredOreIntake);
		if (zDO == null)
		{
			return;
		}
		int num = zDO.GetInt("queued", 0);
		float num2 = (float)Screen.height / 1080f;
		int num3 = 10;
		int num4 = Mathf.RoundToInt(48f * num2);
		int num5 = Mathf.RoundToInt(5f * num2);
		int num6 = Mathf.RoundToInt(10f * num2);
		int num7 = Mathf.RoundToInt(28f * num2);
		int fontSize = Mathf.RoundToInt(18f * num2);
		int num8 = num6 * 2 + num3 * num4 + (num3 - 1) * num5;
		int num9 = num6 * 2 + num4 + num7;
		float num10 = (float)(Screen.width - num8) / 2f;
		float num11 = (float)Screen.height * 0.66f;
		GUI.Box(new Rect(num10, num11, (float)num8, (float)num9), "");
		string text = "Mode: " + FilterTools.FilterDisplayName(FilterTools.GetFilter(hoveredOreIntake)) + "   [G to change]";
		GUIStyle val = new GUIStyle(GUI.skin.label);
		val.alignment = (TextAnchor)4;
		val.fontSize = fontSize;
		GUI.Label(new Rect(num10, num11 + 4f * num2, (float)num8, (float)num7), text, val);
		Rect val2 = default(Rect);
		Rect val3 = default(Rect);
		Rect val4 = default(Rect);
		for (int i = 0; i < num3; i++)
		{
			float num12 = num10 + (float)num6 + (float)(i * (num4 + num5));
			float num13 = num11 + (float)num6 + (float)num7;
			((Rect)(ref val2))..ctor(num12, num13, (float)num4, (float)num4);
			GUI.Box(val2, "");
			if (i < num)
			{
				string prefabName = zDO.GetString("item" + i, "");
				Sprite icon = GetIcon(prefabName);
				if ((Object)(object)icon != (Object)null)
				{
					Texture2D texture = icon.texture;
					Rect textureRect = icon.textureRect;
					((Rect)(ref val3))..ctor(((Rect)(ref textureRect)).x / (float)((Texture)texture).width, ((Rect)(ref textureRect)).y / (float)((Texture)texture).height, ((Rect)(ref textureRect)).width / (float)((Texture)texture).width, ((Rect)(ref textureRect)).height / (float)((Texture)texture).height);
					float num14 = 4f * num2;
					((Rect)(ref val4))..ctor(((Rect)(ref val2)).x + num14, ((Rect)(ref val2)).y + num14, ((Rect)(ref val2)).width - num14 * 2f, ((Rect)(ref val2)).height - num14 * 2f);
					GUI.DrawTextureWithTexCoords(val4, (Texture)(object)texture, val3);
				}
			}
		}
	}

	private static Sprite GetIcon(string prefabName)
	{
		if ((Object)(object)ZNetScene.instance == (Object)null)
		{
			return null;
		}
		GameObject prefab = ZNetScene.instance.GetPrefab(prefabName);
		if ((Object)(object)prefab == (Object)null)
		{
			return null;
		}
		ItemDrop component = prefab.GetComponent<ItemDrop>();
		if ((Object)(object)component == (Object)null)
		{
			return null;
		}
		Sprite[] icons = component.m_itemData.m_shared.m_icons;
		if (icons == null || icons.Length == 0)
		{
			return null;
		}
		return icons[0];
	}
}
public static class FilterTools
{
	public static ZDO GetZDO(Smelter smelter)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Expected O, but got Unknown
		ZNetView val = (ZNetView)AccessTools.Field(typeof(Smelter), "m_nview").GetValue(smelter);
		if ((Object)(object)val == (Object)null)
		{
			return null;
		}
		return val.GetZDO();
	}

	public static string GetFilter(Smelter smelter)
	{
		ZDO zDO = GetZDO(smelter);
		if (zDO == null)
		{
			return "";
		}
		return zDO.GetString("SQ_Filter", "");
	}

	public static void SetFilter(Smelter smelter, string value)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Expected O, but got Unknown
		ZNetView val = (ZNetView)AccessTools.Field(typeof(Smelter), "m_nview").GetValue(smelter);
		if (!((Object)(object)val == (Object)null) && val.GetZDO() != null)
		{
			if (!val.IsOwner())
			{
				val.ClaimOwnership();
			}
			val.GetZDO().Set("SQ_Filter", value);
		}
	}

	public static void CycleFilter(Smelter smelter, Player player)
	{
		string filter = GetFilter(smelter);
		List<ItemConversion> conversion = smelter.m_conversion;
		if (conversion == null || conversion.Count == 0)
		{
			return;
		}
		List<string> list = new List<string>();
		list.Add("");
		foreach (ItemConversion item in conversion)
		{
			string name = ((Object)((Component)item.m_from).gameObject).name;
			if (!(name == "IronOre"))
			{
				list.Add(name);
			}
		}
		int num = list.IndexOf(filter);
		if (num < 0)
		{
			num = 0;
		}
		int index = (num + 1) % list.Count;
		string text = list[index];
		SetFilter(smelter, text);
		((Character)player).Message((MessageType)2, "Mode: " + FilterDisplayName(text), 0, (Sprite)null);
	}

	public static string FilterDisplayName(string filter)
	{
		if (filter == "")
		{
			return "All ores";
		}
		return NiceName(filter) + " only";
	}

	public static string NiceName(string prefabName)
	{
		GameObject prefab = ZNetScene.instance.GetPrefab(prefabName);
		if ((Object)(object)prefab == (Object)null)
		{
			return prefabName;
		}
		ItemDrop component = prefab.GetComponent<ItemDrop>();
		if ((Object)(object)component == (Object)null)
		{
			return prefabName;
		}
		return Localization.instance.Localize(component.m_itemData.m_shared.m_name);
	}
}
[HarmonyPatch(typeof(Smelter), "OnAddOre")]
public static class SmelterAddOrePatch
{
	private static bool Prefix(Smelter __instance, Humanoid user, ref ItemData item, ref bool __result)
	{
		string filter = FilterTools.GetFilter(__instance);
		if (filter == "")
		{
			return true;
		}
		if (item == null)
		{
			foreach (ItemData allItem in user.GetInventory().GetAllItems())
			{
				if ((Object)(object)allItem.m_dropPrefab != (Object)null && ((Object)allItem.m_dropPrefab).name == filter)
				{
					item = allItem;
					return true;
				}
			}
			((Character)user).Message((MessageType)2, "This smelter only accepts " + FilterTools.NiceName(filter), 0, (Sprite)null);
			__result = false;
			return false;
		}
		if ((Object)(object)item.m_dropPrefab == (Object)null || ((Object)item.m_dropPrefab).name != filter)
		{
			((Character)user).Message((MessageType)2, "This smelter only accepts " + FilterTools.NiceName(filter), 0, (Sprite)null);
			__result = false;
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Smelter), "RPC_AddOre")]
public static class SmelterRpcAddOrePatch
{
	private static bool Prefix(Smelter __instance, string name)
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		string filter = FilterTools.GetFilter(__instance);
		if (filter == "")
		{
			return true;
		}
		if (name == filter)
		{
			return true;
		}
		GameObject prefab = ZNetScene.instance.GetPrefab(name);
		if ((Object)(object)prefab != (Object)null)
		{
			Vector3 val = ((Component)__instance).transform.position + Vector3.up * 1.5f + Random.insideUnitSphere * 0.3f;
			Object.Instantiate<GameObject>(prefab, val, Quaternion.identity);
		}
		return false;
	}
}