Decompiled source of RestockAnywhere v1.1.3

BepInEx/plugins/RestockAnywhere/RestockAnywhere.dll

Decompiled 2 weeks 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 HarmonyLib;
using Microsoft.CodeAnalysis;
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("RestockAnywhere")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyInformationalVersion("1.1.3")]
[assembly: AssemblyProduct("Restock Anywhere")]
[assembly: AssemblyTitle("RestockAnywhere")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.3.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 RestockAnywhere
{
	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "AirenElias.RestockAnywhere";

		public const string PLUGIN_NAME = "Restock Anywhere";

		public const string PLUGIN_VERSION = "1.1.3";
	}
	[BepInPlugin("AirenElias.RestockAnywhere", "Restock Anywhere", "1.1.3")]
	public class RestockAnywhere : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		private readonly Harmony harmony = new Harmony("AirenElias.RestockAnywhere");

		public static ConfigEntry<bool> PrioritizeClutter;

		public static ConfigEntry<bool> IgnoreRotten;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			PrioritizeClutter = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Prioritize Clutter", true, "Make employees prefer goods outside of the cellar when restocking");
			IgnoreRotten = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ignore Rotten", false, "Make employees ignore spoiled goods when restocking");
			harmony.PatchAll();
			Logger.LogInfo((object)"Plugin Restock Anywhere is loaded!");
		}
	}
	[HarmonyPatch]
	public class RestockerWaitPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(RestockerWait), "FindItemToRestock")]
		private static bool FindItemToRestock(RestockerController ___rc, ref bool __result)
		{
			//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_029e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			foreach (Item targetItem in (from x in Object.FindObjectsOfType<Item>()
				where x.onStand.Value && x.amount.Value <= 0 && !x.IsRestockTarget()
				select x into go
				orderby Vector3.Distance(((Component)go).transform.position, ((Component)___rc).transform.position)
				select go).ToList())
			{
				ManualLogSource logger = RestockAnywhere.Logger;
				string[] obj = new string[6]
				{
					"Found restock target '",
					targetItem.itemSO.itemName,
					"' amount ",
					targetItem.amount.Value.ToString(),
					" at ",
					null
				};
				Vector3 position = ((Component)targetItem).transform.position;
				obj[5] = ((object)(Vector3)(ref position)).ToString();
				logger.LogDebug((object)string.Concat(obj));
				List<Item> list = (from x in Object.FindObjectsOfType<Item>()
					where (Object)(object)x.itemSO == (Object)(object)targetItem.itemSO && !x.onStand.Value && (!RestockAnywhere.IgnoreRotten.Value || !x.IsExpired()) && x.amount.Value > 0 && !x.IsRestockSource()
					select x into go
					orderby Vector3.Distance(((Component)go).transform.position, ((Component)___rc).transform.position)
					select go).ToList();
				if (list.Count <= 0)
				{
					continue;
				}
				if (RestockAnywhere.PrioritizeClutter.Value)
				{
					Item val = ((IEnumerable<Item>)list).FirstOrDefault((Func<Item, bool>)((Item x) => !x.IsInFreezer()));
					if ((Object)(object)val != (Object)null)
					{
						___rc.SetItems(targetItem, val);
						__result = true;
						ManualLogSource logger2 = RestockAnywhere.Logger;
						string[] obj2 = new string[6]
						{
							"Found outside restock source '",
							val.itemSO.itemName,
							"' amount ",
							val.amount.Value.ToString(),
							" at ",
							null
						};
						position = ((Component)val).transform.position;
						obj2[5] = ((object)(Vector3)(ref position)).ToString();
						logger2.LogDebug((object)string.Concat(obj2));
						return false;
					}
				}
				___rc.SetItems(targetItem, list[0]);
				__result = true;
				ManualLogSource logger3 = RestockAnywhere.Logger;
				string[] obj3 = new string[6]
				{
					"Found closest restock source '",
					list[0].itemSO.itemName,
					"' amount ",
					list[0].amount.Value.ToString(),
					" at ",
					null
				};
				position = ((Component)list[0]).transform.position;
				obj3[5] = ((object)(Vector3)(ref position)).ToString();
				logger3.LogDebug((object)string.Concat(obj3));
				return false;
			}
			__result = false;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(RestockerTakeSource), "OnUpdate")]
		private static void OnUpdate(RestockerController ___rc)
		{
			if ((Object)(object)___rc.sourceItem == (Object)null)
			{
				RestockAnywhere.Logger.LogDebug((object)"Failed to reach restock source, clearing item states");
				___rc.targetItem.SetRestockTarget(false);
				___rc.sourceItem.SetRestockSource(false);
			}
		}
	}
}