Decompiled source of AutoRestock v2.1.0

plugins/Restock/Restock.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Restock")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Restock")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b7c4c260-1c2e-41ff-8694-47980241528e")]
[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 Restock;

[BepInPlugin("com.equinox.Restock", "Restock", "2.1.0")]
public class RestockPlugin : BaseUnityPlugin
{
	private const string MyGUID = "com.equinox.Restock";

	private const string PluginName = "Restock";

	private const string VersionString = "2.1.0";

	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;

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

	private Bounds scanZone;

	private int buildablesMask = 2097512;

	private void Awake()
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: Restock, VersionString: 2.1.0 is loading...");
		Harmony.PatchAll();
		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 ResourceNames.SafeResources)
		{
			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: 2.1.0 is loaded.");
		Log = ((BaseUnityPlugin)this).Logger;
	}

	private void FixedUpdate()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Invalid comparison between Unknown and I4
		//IL_00b0: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		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) => (Object)(object)((ResourceStack)(ref stack)).info != (Object)null).Distinct().ToList())
			{
				ResourceStack current = item;
				if (!((InventoryWrapper)Player.instance.inventory).HasAnyOfResource(((ResourceStack)(ref current)).info))
				{
					continue;
				}
				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))
					{
						Debug.Log((object)"Can't add resources to player inventory");
						continue;
					}
					((Inventory)(ref val4)).TryRemoveResources(((UniqueIdScriptableObject)((ResourceStack)(ref current)).info).uniqueId, num3);
					((InventoryWrapper)Player.instance.inventory).AddResources(uniqueId, num3);
				}
			}
		}
	}

	private bool IsItemBuilding(string name)
	{
		return ResourceNames.SafeResources.IndexOf(name) <= ResourceNames.SafeResources.IndexOf("Sectional Corner (2x2)");
	}
}