Decompiled source of AutoRefillFires v1.0.0

plugins\AutoRefillFires\AutoRefillFires.dll

Decompiled 6 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyMetadata("PluginVersion", "1.0.0")]
[assembly: AssemblyCompany("AutoRefillFires")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+7af5e7e407a99406556b6a44f64d63df91d64377")]
[assembly: AssemblyProduct("AutoRefillFires")]
[assembly: AssemblyTitle("AutoRefillFires")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AutoRefillFires
{
	[BepInPlugin("simplifydave.autorefillfires", "Auto Refill Fires", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "simplifydave.autorefillfires";

		public const string PluginName = "Auto Refill Fires";

		public const string PluginVersion = "1.0.0";

		private ConfigEntry<float> _radius;

		private ConfigEntry<float> _checkInterval;

		private ConfigEntry<float> _refillBelowPercent;

		private ConfigEntry<bool> _useNearbyContainers;

		private ConfigEntry<float> _containerRadius;

		private ConfigEntry<bool> _refillToMax;

		private ConfigEntry<int> _refillAmount;

		private ConfigEntry<bool> _fillCampfires;

		private ConfigEntry<bool> _fillHearths;

		private ConfigEntry<bool> _fillStandingTorches;

		private ConfigEntry<bool> _fillWallTorches;

		private ConfigEntry<bool> _fillBraziers;

		private ConfigEntry<bool> _fillOtherFireplaces;

		private float _nextCheckTime;

		private void Awake()
		{
			_radius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 20f, "Refill fireplaces within this radius around the player.");
			_checkInterval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CheckInterval", 3f, "How often fireplaces are checked, in seconds.");
			_refillBelowPercent = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RefillBelowPercent", 0.75f, "Refill when fuel drops below this percentage.");
			_refillToMax = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RefillToMax", true, "If true, refill fireplaces to maximum fuel. If false, add only RefillAmount fuel.");
			_refillAmount = ((BaseUnityPlugin)this).Config.Bind<int>("General", "RefillAmount", 5, "Amount of fuel to add when RefillToMax is false.");
			_useNearbyContainers = ((BaseUnityPlugin)this).Config.Bind<bool>("Containers", "UseNearbyContainers", false, "Allow plugin to take fuel from nearby containers.");
			_containerRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Containers", "ContainerRadius", 10f, "Maximum distance from the fireplace to search for containers.");
			_fillCampfires = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillCampfires", true, "Automatically refill campfires.");
			_fillHearths = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillHearths", true, "Automatically refill hearths.");
			_fillStandingTorches = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillStandingTorches", true, "Automatically refill all standing torches, including colored variants.");
			_fillWallTorches = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillWallTorches", true, "Automatically refill all wall torches, including colored variants.");
			_fillBraziers = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillBraziers", true, "Automatically refill braziers.");
			_fillOtherFireplaces = ((BaseUnityPlugin)this).Config.Bind<bool>("Fireplace Types", "FillOtherFireplaces", true, "Automatically refill unknown or modded Fireplace-based objects.");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Auto Refill Fires loaded!");
		}

		private void Update()
		{
			if (!(Time.time < _nextCheckTime))
			{
				_nextCheckTime = Time.time + _checkInterval.Value;
				Player localPlayer = Player.m_localPlayer;
				if (!((Object)(object)localPlayer == (Object)null))
				{
					RefillNearbyFireplaces(localPlayer);
				}
			}
		}

		private bool ShouldRefillFireplace(Fireplace fireplace)
		{
			string text = ((Object)((Component)fireplace).gameObject).name.Replace("(Clone)", "").ToLowerInvariant();
			if (text.Contains("firepit"))
			{
				return _fillCampfires.Value;
			}
			if (text.Contains("hearth"))
			{
				return _fillHearths.Value;
			}
			if (text.Contains("groundtorch"))
			{
				return _fillStandingTorches.Value;
			}
			if (text.Contains("walltorch"))
			{
				return _fillWallTorches.Value;
			}
			if (text.Contains("brazier"))
			{
				return _fillBraziers.Value;
			}
			return _fillOtherFireplaces.Value;
		}

		private void RefillNearbyFireplaces(Player player)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			Fireplace[] array = Object.FindObjectsByType<Fireplace>((FindObjectsInactive)0, (FindObjectsSortMode)0);
			Fireplace[] array2 = array;
			foreach (Fireplace val in array2)
			{
				if (!((Object)(object)val == (Object)null))
				{
					float num = Vector3.Distance(((Component)player).transform.position, ((Component)val).transform.position);
					if (!(num > _radius.Value))
					{
						TryRefillFireplace(player, val);
					}
				}
			}
		}

		private void TryRefillFireplace(Player player, Fireplace fireplace)
		{
			if (!ShouldRefillFireplace(fireplace))
			{
				return;
			}
			ZNetView val = ((Component)fireplace).GetComponent<ZNetView>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)fireplace).GetComponentInParent<ZNetView>();
			}
			if ((Object)(object)val == (Object)null || !val.IsValid())
			{
				return;
			}
			ZDO zDO = val.GetZDO();
			if (zDO == null || (Object)(object)fireplace.m_fuelItem == (Object)null)
			{
				return;
			}
			float num = zDO.GetFloat(ZDOVars.s_fuel, 0f);
			float maxFuel = fireplace.m_maxFuel;
			if (maxFuel <= 0f)
			{
				return;
			}
			float num2 = num / maxFuel;
			if (num2 >= _refillBelowPercent.Value)
			{
				return;
			}
			string name = fireplace.m_fuelItem.m_itemData.m_shared.m_name;
			int num3 = Mathf.CeilToInt(maxFuel - num);
			if (num3 <= 0)
			{
				return;
			}
			int num4 = ((!_refillToMax.Value) ? Mathf.Min(Mathf.Max(_refillAmount.Value, 0), num3) : num3);
			if (num4 <= 0)
			{
				return;
			}
			int num5 = 0;
			for (int i = 0; i < num4; i++)
			{
				if (!TryConsumeFuel(player, fireplace, name))
				{
					break;
				}
				val.InvokeRPC("RPC_AddFuel", new object[0]);
				num5++;
			}
			if (num5 > 0)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Refilled " + ((Object)fireplace).name + ": " + $"{num:0.0}/{maxFuel:0.0}, " + "fuel=" + name + ", " + $"added={num5}, " + "mode=" + (_refillToMax.Value ? "max" : "fixed")));
			}
		}

		private bool TryConsumeFuel(Player player, Fireplace fireplace, string fuelName)
		{
			Inventory inventory = ((Humanoid)player).GetInventory();
			if (inventory.CountItems(fuelName, -1, true) > 0)
			{
				inventory.RemoveItem(fuelName, 1, -1, true);
				((BaseUnityPlugin)this).Logger.LogDebug((object)("Fuel " + fuelName + " taken from player inventory."));
				return true;
			}
			if (!_useNearbyContainers.Value)
			{
				return false;
			}
			return TryConsumeFuelFromNearbyContainer(fireplace, fuelName);
		}

		private bool TryConsumeFuelFromNearbyContainer(Fireplace fireplace, string fuelName)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			Container[] array = Object.FindObjectsByType<Container>((FindObjectsInactive)0, (FindObjectsSortMode)0);
			Container val = null;
			float num = float.MaxValue;
			Container[] array2 = array;
			foreach (Container val2 in array2)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				float num2 = Vector3.Distance(((Component)fireplace).transform.position, ((Component)val2).transform.position);
				if (!(num2 > _containerRadius.Value))
				{
					Inventory inventory = val2.GetInventory();
					if (inventory != null && inventory.CountItems(fuelName, -1, true) > 0 && num2 < num)
					{
						num = num2;
						val = val2;
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			Inventory inventory2 = val.GetInventory();
			if (inventory2 == null)
			{
				return false;
			}
			if (inventory2.CountItems(fuelName, -1, true) <= 0)
			{
				return false;
			}
			inventory2.RemoveItem(fuelName, 1, -1, true);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Fuel " + fuelName + " taken from container " + ((Object)val).name + " " + $"({num:0.0}m)"));
			return true;
		}
	}
	internal static class VersionInfo
	{
		public const string Version = "1.0.0";
	}
}