Decompiled source of ResourceFeeder v1.0.4

plugins/ResourceFeeder/ResourceFeeder.dll

Decompiled 5 months ago
using System;
using System.Collections;
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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Common;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("ResourceFeeder")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("ResourceFeeder")]
[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 Common
{
	public class OrderedSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable
	{
		private readonly IDictionary<T, LinkedListNode<T>> m_Dictionary;

		private readonly LinkedList<T> m_LinkedList;

		public int Count => m_Dictionary.Count;

		public virtual bool IsReadOnly => m_Dictionary.IsReadOnly;

		public OrderedSet()
			: this((IEqualityComparer<T>)EqualityComparer<T>.Default)
		{
		}

		public OrderedSet(IEqualityComparer<T> comparer)
		{
			m_Dictionary = new Dictionary<T, LinkedListNode<T>>(comparer);
			m_LinkedList = new LinkedList<T>();
		}

		void ICollection<T>.Add(T item)
		{
			Add(item);
		}

		public void Clear()
		{
			m_LinkedList.Clear();
			m_Dictionary.Clear();
		}

		public bool Remove(T item)
		{
			if (!m_Dictionary.TryGetValue(item, out var value))
			{
				return false;
			}
			m_Dictionary.Remove(item);
			m_LinkedList.Remove(value);
			return true;
		}

		public IEnumerator<T> GetEnumerator()
		{
			return m_LinkedList.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public bool Contains(T item)
		{
			return m_Dictionary.ContainsKey(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			m_LinkedList.CopyTo(array, arrayIndex);
		}

		public bool Add(T item)
		{
			if (m_Dictionary.ContainsKey(item))
			{
				return false;
			}
			LinkedListNode<T> value = m_LinkedList.AddLast(item);
			m_Dictionary.Add(item, value);
			return true;
		}

		public bool TryGetFirst(out T result)
		{
			LinkedListNode<T> first = m_LinkedList.First;
			result = ((first == null) ? default(T) : first.Value);
			return first != null;
		}

		public bool TryGetLast(out T result)
		{
			LinkedListNode<T> last = m_LinkedList.Last;
			result = ((last == null) ? default(T) : last.Value);
			return last != null;
		}

		public LinkedListNode<T> GetNode(T item)
		{
			if (!m_Dictionary.ContainsKey(item))
			{
				return null;
			}
			return m_Dictionary[item];
		}

		public LinkedListNode<T> GetLastNode()
		{
			return m_LinkedList.Last;
		}

		public LinkedListNode<T> GetFirstNode()
		{
			return m_LinkedList.First;
		}
	}
	public class UniqueQueue<T> : IEnumerable<T>, IEnumerable where T : Tuple<MachineTypeEnum, int, int>
	{
		private Dictionary<MachineTypeEnum, HashSet<int>> m_Dictionary;

		private Queue<T> queue;

		public int Count => m_Dictionary.Sum((KeyValuePair<MachineTypeEnum, HashSet<int>> x) => x.Value.Count);

		public UniqueQueue()
		{
			m_Dictionary = new Dictionary<MachineTypeEnum, HashSet<int>>();
			queue = new Queue<T>();
		}

		public void Clear()
		{
			m_Dictionary.Clear();
			queue.Clear();
		}

		public bool Contains(T item)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			if (m_Dictionary.TryGetValue(item.Item1, out var value))
			{
				return value.Contains(item.Item2);
			}
			return false;
		}

		public void Enqueue(T item)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			if (!m_Dictionary.TryGetValue(item.Item1, out var value))
			{
				value = new HashSet<int>();
				m_Dictionary[item.Item1] = value;
			}
			if (value.Add(item.Item2))
			{
				queue.Enqueue(item);
			}
		}

		public T Dequeue()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			T val = queue.Dequeue();
			m_Dictionary[val.Item1].Remove(val.Item2);
			return val;
		}

		public T Peek()
		{
			return queue.Peek();
		}

		public IEnumerator<T> GetEnumerator()
		{
			return queue.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return queue.GetEnumerator();
		}
	}
}
namespace ResourceFeeder
{
	[StructLayout(LayoutKind.Sequential, Size = 1)]
	public struct MyPluginInfo
	{
		public const string PLUGIN_GUID = "xyz.deadmoroz.resourcefeeder";

		public const string PLUGIN_NAME = "Resource Feeder";

		public const string PLUGIN_VERSION = "1.0.4";
	}
	[BepInPlugin("xyz.deadmoroz.resourcefeeder", "Resource Feeder", "1.0.4")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(FactorySimManager), "PostLoadInit")]
		private class FactorySimManager_PostLoadInit
		{
			private static void Postfix()
			{
				Logger.LogInfo((object)"FactorySimManager init done!");
				instance.initDone = true;
			}
		}

		[HarmonyPatch(typeof(AssemblerInstance), "SetRecipe")]
		private class AssemblerInstance_SetRecipe
		{
			private static void Postfix(AssemblerInstance __instance)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_004f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0050: Unknown result type (might be due to invalid IL or missing references)
				//IL_0055: 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_005b: Unknown result type (might be due to invalid IL or missing references)
				//IL_006a: 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_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0034: Unknown result type (might be due to invalid IL or missing references)
				//IL_0039: Unknown result type (might be due to invalid IL or missing references)
				//IL_003a: 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_008a: Unknown result type (might be due to invalid IL or missing references)
				//IL_008f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0094: 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)
				if (!((CommonMachineInfo)(ref __instance.commonInfo)).canErase)
				{
					return;
				}
				if ((Object)(object)__instance.targetRecipe == (Object)null && instance.initDone)
				{
					instance.RemoveMachine(__instance.commonInfo.typeIndex, __instance.commonInfo.index);
					return;
				}
				instance.RemoveMachine(__instance.commonInfo.typeIndex, __instance.commonInfo.index);
				ResourceInfo[] ingTypes = __instance.targetRecipe.ingTypes;
				foreach (ResourceInfo val in ingTypes)
				{
					instance.AddMachine(((UniqueIdScriptableObject)val).uniqueId, __instance.commonInfo.typeIndex, __instance.commonInfo.index);
				}
			}
		}

		[HarmonyPatch(typeof(InserterInstance), "SimUpdate")]
		private class InserterInstance_SimUpdate
		{
			private static bool Prefix(InserterInstance __instance, ref float dt)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				if (__instance.isFilter && !__instance.hasTakeObj)
				{
					dt *= 2f;
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(InserterInstance), "SetFilter")]
		private class InserterInstance_SetFilter
		{
			private static void Postfix(InserterInstance __instance)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: 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)
				//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_0053: Unknown result type (might be due to invalid IL or missing references)
				//IL_0058: 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_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_0028: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				//IL_0033: Unknown result type (might be due to invalid IL or missing references)
				if (__instance.isFilter && __instance.commonInfo.index > 0)
				{
					if (instance.initDone)
					{
						instance.RemoveMachine(__instance.commonInfo.typeIndex, __instance.commonInfo.index);
					}
					instance.AddMachine(__instance.filterType, __instance.commonInfo.typeIndex, __instance.commonInfo.index);
				}
			}
		}

		[HarmonyPatch]
		private class AssemblerInstance_Deconstruct
		{
			[HarmonyPatch(typeof(MachineInstanceRef<AssemblerInstance>), "Deconstruct")]
			[HarmonyPrefix]
			private static bool AssemblerDeconstruct(MachineInstanceRef<AssemblerInstance> __instance)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				instance.RemoveMachine(__instance.typeIndex, __instance.index);
				return true;
			}

			[HarmonyPatch(typeof(MachineInstanceRef<InserterInstance>), "Deconstruct")]
			[HarmonyPrefix]
			private static bool InserterDeconstruct(MachineInstanceRef<InserterInstance> __instance)
			{
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				if (__instance.GetTyped<InserterInstance>().isFilter)
				{
					instance.RemoveMachine(__instance.typeIndex, __instance.index);
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(MachineInstanceList<AssemblerInstance, AssemblerDefinition>), "UpdateAll")]
		private class AssemblerMachineList_UpdateAll
		{
			private static bool Prefix(bool shouldElapseTime, bool isRefresh)
			{
				if (instance.initDone)
				{
					MachineManager.instance.typeBasedLists[3].UpdateAll(shouldElapseTime, isRefresh);
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(ChestInstance), "SimUpdate")]
		private class ChestInstance_SimUpdate
		{
			private static void Postfix(ref ChestInstance __instance)
			{
				//IL_0077: Unknown result type (might be due to invalid IL or missing references)
				//IL_007c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0080: 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_013f: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
				//IL_0179: Unknown result type (might be due to invalid IL or missing references)
				//IL_017f: Invalid comparison between Unknown and I4
				//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
				//IL_020b: Unknown result type (might be due to invalid IL or missing references)
				if (!((CommonMachineInfo)(ref __instance.commonInfo)).canErase)
				{
					return;
				}
				foreach (ResourceInfo item in from item in ((Inventory)(ref ((ChestInstance)(ref __instance)).GetInventory())).GetAllResourceTypes()
					where instance.resToMachine.ContainsKey(((UniqueIdScriptableObject)item).uniqueId)
					select item)
				{
					IMachineInstanceRef nextMachine = instance.GetNextMachine(((UniqueIdScriptableObject)item).uniqueId);
					if (nextMachine == null)
					{
						continue;
					}
					if (nextMachine is MachineInstanceRef<AssemblerInstance>)
					{
						AssemblerInstance typed = nextMachine.GetTyped<AssemblerInstance>();
						int num = 0;
						if ((Object)(object)typed.targetRecipe != (Object)null)
						{
							int ingredientIndex = typed.targetRecipe.GetIngredientIndex(((UniqueIdScriptableObject)item).uniqueId);
							if (ingredientIndex >= 0)
							{
								num = typed.targetRecipe.runtimeIngQuantities[ingredientIndex] * 2 - ((Inventory)(ref ((AssemblerInstance)(ref typed)).GetInputInventory())).GetResourceCount(((UniqueIdScriptableObject)item).uniqueId, false);
							}
						}
						if (num > 0)
						{
							int num2 = ((Inventory)(ref ((ChestInstance)(ref __instance)).GetInventory())).TryRemoveResources(((UniqueIdScriptableObject)item).uniqueId, Math.Min(2, num));
							if (num2 > 0)
							{
								((Inventory)(ref ((AssemblerInstance)(ref typed)).GetInputInventory())).AddResources(((UniqueIdScriptableObject)item).uniqueId, num2, true);
							}
						}
					}
					else
					{
						if (!(nextMachine is MachineInstanceRef<InserterInstance>))
						{
							continue;
						}
						int num3 = 30;
						ref InserterInstance typed2 = ref nextMachine.GetTyped<InserterInstance>();
						if (!typed2.hasGiveObj || ((object)(CommonMachineInfo)(ref typed2.giveResourceContainer.GetCommonInfo())).Equals((object?)__instance.commonInfo) || !typed2.isFilter || typed2.hasTakeObj || (int)typed2.armState != 2 || (typed2.currentHeldResType >= 0 && typed2.currentHeldResType != ((UniqueIdScriptableObject)item).uniqueId))
						{
							continue;
						}
						if (typed2.currentHeldStackCount < num3)
						{
							int num4 = ((Inventory)(ref ((ChestInstance)(ref __instance)).GetInventory())).TryRemoveResources(((UniqueIdScriptableObject)item).uniqueId, 2);
							if (num4 > 0)
							{
								typed2.currentHeldStackCount += num4;
								typed2.resourceTakenThisFrame = ((UniqueIdScriptableObject)item).uniqueId;
								((InserterInstance)(ref typed2)).SetHeldItem(item);
							}
						}
						else if (typed2.currentHeldResType == ((UniqueIdScriptableObject)item).uniqueId)
						{
							typed2.armState = (ArmState)3;
							typed2.animState = 0f;
							typed2.queuedSound = (InserterSound)1;
						}
					}
				}
			}
		}

		internal static ManualLogSource Logger;

		private static Plugin instance;

		private bool initDone;

		private Dictionary<int, OrderedSet<Tuple<MachineTypeEnum, int>>> resToMachine;

		private Dictionary<int, LinkedListNode<Tuple<MachineTypeEnum, int>>> lastIndex;

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			resToMachine = new Dictionary<int, OrderedSet<Tuple<MachineTypeEnum, int>>>();
			lastIndex = new Dictionary<int, LinkedListNode<Tuple<MachineTypeEnum, int>>>();
			Logger = ((BaseUnityPlugin)this).Logger;
			instance = this;
			new Harmony("xyz.deadmoroz.resourcefeeder").PatchAll();
			Logger.LogInfo((object)"Plugin xyz.deadmoroz.resourcefeeder is loaded!");
		}

		private void AddMachine(int res, MachineTypeEnum type, int index)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (res >= 0)
			{
				if (!resToMachine.TryGetValue(res, out var value))
				{
					value = new OrderedSet<Tuple<MachineTypeEnum, int>>();
					resToMachine[res] = value;
				}
				value.Add(new Tuple<MachineTypeEnum, int>(type, index));
			}
		}

		private void RemoveMachine(MachineTypeEnum type, int index)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			CollectionExtensions.Do<KeyValuePair<int, OrderedSet<Tuple<MachineTypeEnum, int>>>>((IEnumerable<KeyValuePair<int, OrderedSet<Tuple<MachineTypeEnum, int>>>>)resToMachine, (Action<KeyValuePair<int, OrderedSet<Tuple<MachineTypeEnum, int>>>>)delegate(KeyValuePair<int, OrderedSet<Tuple<MachineTypeEnum, int>>> item)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				Tuple<MachineTypeEnum, int> item2 = new Tuple<MachineTypeEnum, int>(type, index);
				LinkedListNode<Tuple<MachineTypeEnum, int>> node = item.Value.GetNode(item2);
				if (lastIndex.TryGetValue(item.Key, out var value) && node != null && value == node)
				{
					value = ((value.Previous != null) ? value.Previous : value.List.Last);
					if (value == null || value == node)
					{
						lastIndex.Remove(item.Key);
					}
					else
					{
						lastIndex[item.Key] = value;
					}
				}
				item.Value.Remove(item2);
			});
		}

		private IMachineInstanceRef GetNextMachine(int res)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: 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_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Invalid comparison between Unknown and I4
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Invalid comparison between Unknown and I4
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			OrderedSet<Tuple<MachineTypeEnum, int>> value;
			while (resToMachine.TryGetValue(res, out value))
			{
				LinkedListNode<Tuple<MachineTypeEnum, int>> value2 = ((!lastIndex.TryGetValue(res, out value2)) ? value.GetFirstNode() : ((value2.Next != null) ? value2.Next : value2.List.First));
				if (value2 == null)
				{
					break;
				}
				IMachineInstanceRef val = null;
				IMachineList machineList = MachineManager.instance.GetMachineList(value2.Value.Item1);
				int item = value2.Value.Item2;
				if (machineList.IsValidIndex(ref item))
				{
					MachineTypeEnum item2 = value2.Value.Item1;
					if ((int)item2 != 1)
					{
						if ((int)item2 == 7)
						{
							val = (IMachineInstanceRef)(object)MACHINE_INSTANCE_HELPER_EXTENSIONS.CreateRef<InserterInstance>(MachineManager.instance.Get<InserterInstance, InserterDefinition>(value2.Value.Item2, (MachineTypeEnum)7));
						}
					}
					else
					{
						val = (IMachineInstanceRef)(object)MACHINE_INSTANCE_HELPER_EXTENSIONS.CreateRef<AssemblerInstance>(MachineManager.instance.Get<AssemblerInstance, AssemblerDefinition>(value2.Value.Item2, (MachineTypeEnum)1));
					}
				}
				if (val == null)
				{
					RemoveMachine(value2.Value.Item1, value2.Value.Item2);
					continue;
				}
				lastIndex[res] = value2;
				return val;
			}
			return null;
		}
	}
}