Decompiled source of QuickFill v1.1.0

QuickFill_v1.1.0.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("QuickFill")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("QuickFill")]
[assembly: AssemblyTitle("QuickFill")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace QuickFill;

[BepInPlugin("DEEP_FROST.QuickFill", "QuickFill", "1.1.0")]
public class QuickFillPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Smelter), "OnAddOre")]
	private static class FillOrePatch
	{
		private static bool _filling;

		private static readonly Func<Smelter, Switch, Humanoid, ItemData, bool> Original = AccessTools.MethodDelegate<Func<Smelter, Switch, Humanoid, ItemData, bool>>(AccessTools.Method(typeof(Smelter), "OnAddOre", (Type[])null, (Type[])null), (object)null, true);

		[HarmonyPrefix]
		private static bool Prefix(Smelter __instance, Switch sw, Humanoid user, ItemData item, ref bool __result)
		{
			if (_filling || !FillActive)
			{
				return true;
			}
			_filling = true;
			try
			{
				int value = _maxPerPress.Value;
				int i = 0;
				Inventory inventory = user.GetInventory();
				ItemData val = item;
				for (; i < value; i++)
				{
					if (val != null)
					{
						val = inventory.GetItem(val.m_shared.m_name, -1, false);
					}
					if (val == null)
					{
						if (item != null)
						{
							break;
						}
						val = FindCookable(__instance, inventory);
						if (val == null)
						{
							break;
						}
					}
					if (!Original(__instance, sw, user, val))
					{
						break;
					}
				}
				if (i > 0)
				{
					ZLog.Log((object)$"[QuickFill] Ore: added {i} per one press");
					__result = true;
					return false;
				}
				return true;
			}
			finally
			{
				_filling = false;
			}
		}
	}

	[HarmonyPatch(typeof(Smelter), "OnAddFuel")]
	private static class FillFuelPatch
	{
		private static bool _filling;

		private static readonly Func<Smelter, Switch, Humanoid, ItemData, bool> Original = AccessTools.MethodDelegate<Func<Smelter, Switch, Humanoid, ItemData, bool>>(AccessTools.Method(typeof(Smelter), "OnAddFuel", (Type[])null, (Type[])null), (object)null, true);

		[HarmonyPrefix]
		private static bool Prefix(Smelter __instance, Switch sw, Humanoid user, ItemData item, ref bool __result)
		{
			if (_filling || !FillActive)
			{
				return true;
			}
			if ((Object)(object)__instance.m_fuelItem == (Object)null)
			{
				return true;
			}
			_filling = true;
			try
			{
				int value = _maxPerPress.Value;
				int i;
				for (i = 0; i < value; i++)
				{
					if (!Original(__instance, sw, user, item))
					{
						break;
					}
				}
				if (i > 0)
				{
					ZLog.Log((object)$"[QuickFill] Fuel: added {i} per one press");
					__result = true;
					return false;
				}
				return true;
			}
			finally
			{
				_filling = false;
			}
		}
	}

	public const string PluginGuid = "DEEP_FROST.QuickFill";

	public const string PluginName = "QuickFill";

	public const string PluginVersion = "1.1.0";

	private static ConfigEntry<bool> _enabled;

	private static ConfigEntry<int> _maxPerPress;

	private static ConfigEntry<bool> _rightShiftToo;

	public static bool FillActive
	{
		get
		{
			if (_enabled.Value)
			{
				if (!Input.GetKey((KeyCode)304))
				{
					if (_rightShiftToo.Value)
					{
						return Input.GetKey((KeyCode)303);
					}
					return false;
				}
				return true;
			}
			return false;
		}
	}

	private void Awake()
	{
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch / Главный выключатель");
		_maxPerPress = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxPerPress", 100, "Safety cap: max items per one press / Защитный лимит предметов за одно нажатие");
		_rightShiftToo = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RightShiftToo", true, "React to right Shift too / Реагировать и на правый Shift");
		Harmony.CreateAndPatchAll(typeof(QuickFillPlugin).Assembly, "DEEP_FROST.QuickFill");
		ZLog.Log((object)"[QuickFill] v1.1.0 loaded: hold Shift + E on a smelter/kiln/mill = fill to max");
	}

	private static ItemData FindCookable(Smelter smelter, Inventory inventory)
	{
		List<ItemConversion> conversion = smelter.m_conversion;
		for (int i = 0; i < conversion.Count; i++)
		{
			ItemDrop val = conversion[i].m_from;
			if (!((Object)(object)val == (Object)null))
			{
				ItemData item = inventory.GetItem(val.m_itemData.m_shared.m_name, -1, false);
				if (item != null)
				{
					return item;
				}
			}
		}
		return null;
	}
}