Decompiled source of Restock Updated v3.0.8

Restock.dll

Decompiled 5 days 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Restock")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Restock")]
[assembly: AssemblyTitle("Restock")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 Restock
{
	[BepInPlugin("com.equinox.Restock", "Restock", "3.0.8")]
	public class RestockPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.Restock";

		private const string PluginName = "Restock";

		private const string VersionString = "3.0.8";

		private static readonly Harmony Harmony = new Harmony("com.equinox.Restock");

		public static ManualLogSource Log = new ManualLogSource("Restock");

		public static string restockRadiusKey = "Restock Radius";

		public static ConfigEntry<int> restockRadius;

		public static ConfigEntry<float> restockCooldown;

		private float lastRestockTime;

		internal static Dictionary<string, ConfigEntry<int>> stacksDictionary = new Dictionary<string, ConfigEntry<int>>();

		private Bounds scanZone;

		private int buildablesMask = 2097512;

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: Restock, VersionString: 3.0.8 is loading...");
			Harmony.PatchAll();
			restockCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Restock Cooldown", 0.5f, new ConfigDescription("Seconds between restock checks (reduces CPU usage)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 5f), Array.Empty<object>()));
			restockRadius = ((BaseUnityPlugin)this).Config.Bind<int>("General", restockRadiusKey, 5, new ConfigDescription("The radius around the player to scan for chests", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
			foreach (string safeResource in Resources.SafeResources)
			{
				if (!stacksDictionary.ContainsKey(safeResource))
				{
					bool num = IsItemBuilding(safeResource);
					string text = (num ? "Buildings" : "Items");
					int num2 = (num ? 1 : 0);
					stacksDictionary.Add(safeResource, ((BaseUnityPlugin)this).Config.Bind<int>(text, safeResource, num2, new ConfigDescription("The number of stacks of " + safeResource + " to restock up to", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, int.MaxValue), Array.Empty<object>())));
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: Restock, VersionString: 3.0.8 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void FixedUpdate()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: 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_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Invalid comparison between Unknown and I4
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkServer.active || Time.time - lastRestockTime < restockCooldown.Value)
			{
				return;
			}
			lastRestockTime = Time.time;
			if ((Object)(object)Player.instance == (Object)null)
			{
				return;
			}
			int value = restockRadius.Value;
			Bounds val = default(Bounds);
			((Bounds)(ref val)).center = ((Component)Player.instance).transform.position;
			((Bounds)(ref val)).extents = new Vector3((float)value, 1f, (float)value);
			scanZone = val;
			Collider[] array = Physics.OverlapBox(((Bounds)(ref scanZone)).center, ((Bounds)(ref scanZone)).extents, Quaternion.identity, buildablesMask);
			for (int i = 0; i < array.Length; i++)
			{
				GenericMachineInstanceRef val2 = FHG_Utils.FindMachineRef(((Component)array[i]).gameObject);
				if (!((GenericMachineInstanceRef)(ref val2)).IsValid() || (int)((GenericMachineInstanceRef)(ref val2)).typeIndex != 3)
				{
					continue;
				}
				ChestInstance val3 = ((GenericMachineInstanceRef)(ref val2)).Get<ChestInstance>();
				Inventory val4 = MACHINE_INSTANCE_HELPER_EXTENSIONS.GetCommonInfo<ChestInstance>(ref val3).inventories[0];
				foreach (ResourceStack item in val4.myStacks.Where((ResourceStack stack) => !((ResourceStack)(ref stack)).isEmpty).Distinct().ToList())
				{
					ResourceStack current = item;
					int maxStack = current.maxStack;
					int resourceCount = ((InventoryWrapper)Player.instance.inventory).GetResourceCount(((ResourceStack)(ref current)).info, false);
					int num = 0;
					if (stacksDictionary.ContainsKey(((ResourceStack)(ref current)).info.displayName))
					{
						num = stacksDictionary[((ResourceStack)(ref current)).info.displayName].Value * maxStack;
					}
					if (resourceCount >= num || num == 0)
					{
						continue;
					}
					int num2 = num - resourceCount;
					int num3 = ((num2 > current.count) ? (current.count - 1) : num2);
					if (num3 != 0)
					{
						int uniqueId = ((UniqueIdScriptableObject)((ResourceStack)(ref current)).info).uniqueId;
						if (((InventoryWrapper)Player.instance.inventory).CanAddResources(uniqueId, num3, (List<NetworkInventorySlot>)null))
						{
							((Inventory)(ref val4)).TryRemoveResources(((UniqueIdScriptableObject)((ResourceStack)(ref current)).info).uniqueId, num3);
							((InventoryWrapper)Player.instance.inventory).AddResources(uniqueId, num3);
						}
					}
				}
			}
		}

		private bool IsItemBuilding(string name)
		{
			int num = Resources.SafeResources.IndexOf(name);
			int num2 = Resources.SafeResources.IndexOf("Biobrick");
			int num3 = Resources.SafeResources.IndexOf("Power Floor");
			int num4 = Resources.SafeResources.IndexOf("Sectional Corner (2x2)");
			if (num < num2)
			{
				return true;
			}
			if (num >= num3 && num <= num4)
			{
				return true;
			}
			return false;
		}
	}
}