Decompiled source of Dimentional Storage v1.0.2

ogatas-Dimentional-Storage.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ogatas_Dimentional_Storage;

internal sealed class Inventory
{
	internal const long Maximum = 2305843009213693951L;

	private readonly Dictionary<int, long> counts = new Dictionary<int, long>();

	internal int KindCount
	{
		get
		{
			lock (this)
			{
				return counts.Count;
			}
		}
	}

	internal IEnumerable<int> ItemIds
	{
		get
		{
			lock (this)
			{
				return new List<int>(counts.Keys);
			}
		}
	}

	internal long Count(int item)
	{
		lock (this)
		{
			long value;
			return counts.TryGetValue(item, out value) ? value : 0;
		}
	}

	internal long Room(int item)
	{
		return (item > 0) ? (2305843009213693951L - Count(item)) : 0;
	}

	internal void Add(int item, long n)
	{
		lock (this)
		{
			if (item <= 0 || n < 0 || n > Room(item))
			{
				throw new ArgumentOutOfRangeException();
			}
			if (n > 0)
			{
				counts[item] = Count(item) + n;
			}
		}
	}

	internal void Remove(int item, long n)
	{
		lock (this)
		{
			if (n < 0 || n > Count(item))
			{
				throw new ArgumentOutOfRangeException();
			}
			if (n > 0)
			{
				counts[item] = Count(item) - n;
			}
		}
	}

	internal int Withdraw(int item, int requested, Func<int, int> putInBackpack)
	{
		lock (this)
		{
			int num = (int)Math.Min(Math.Max(0, requested), Count(item));
			if (num <= 0)
			{
				return 0;
			}
			int num2 = putInBackpack(num);
			if (num2 < 0 || num2 > num)
			{
				throw new InvalidOperationException("Invalid backpack transfer result");
			}
			Remove(item, num2);
			return num2;
		}
	}

	internal void Write(BinaryWriter w)
	{
		lock (this)
		{
			List<int> list = new List<int>();
			foreach (KeyValuePair<int, long> count in counts)
			{
				if (count.Value > 0)
				{
					list.Add(count.Key);
				}
			}
			list.Sort();
			w.Write(list.Count);
			foreach (int item in list)
			{
				w.Write(item);
				w.Write(counts[item]);
			}
		}
	}

	internal static Inventory Read(BinaryReader r)
	{
		int num = r.ReadInt32();
		if (num < 0 || num > 100000)
		{
			throw new InvalidDataException("Invalid RTS item count");
		}
		Inventory inventory = new Inventory();
		for (int i = 0; i < num; i++)
		{
			int num2 = r.ReadInt32();
			long num3 = r.ReadInt64();
			if (num2 <= 0 || num3 <= 0 || num3 > 2305843009213693951L || inventory.counts.ContainsKey(num2))
			{
				throw new InvalidDataException("Invalid RTS inventory entry");
			}
			inventory.Add(num2, num3);
		}
		return inventory;
	}
}
internal enum SensorMode
{
	Normal,
	Input,
	Output
}
internal struct SensorKey : IEquatable<SensorKey>
{
	internal int planet;

	internal int entity;

	internal SensorKey(int p, int e)
	{
		planet = p;
		entity = e;
	}

	public bool Equals(SensorKey o)
	{
		return planet == o.planet && entity == o.entity;
	}

	public override bool Equals(object o)
	{
		return o is SensorKey && Equals((SensorKey)o);
	}

	public override int GetHashCode()
	{
		return (planet * 397) ^ entity;
	}
}
internal sealed class SensorSetting
{
	internal SensorMode mode;

	internal int item;
}
internal sealed class WorldState
{
	internal Inventory inventory = new Inventory();

	internal readonly Dictionary<SensorKey, SensorSetting> sensors = new Dictionary<SensorKey, SensorSetting>();

	internal SensorSetting Sensor(int planet, int entity)
	{
		SensorSetting value;
		return sensors.TryGetValue(new SensorKey(planet, entity), out value) ? value : new SensorSetting();
	}

	internal void SetSensor(int planet, int entity, SensorMode mode, int item)
	{
		SensorKey key = new SensorKey(planet, entity);
		if (mode == SensorMode.Normal)
		{
			sensors.Remove(key);
			return;
		}
		sensors[key] = new SensorSetting
		{
			mode = mode,
			item = item
		};
	}

	internal void Write(BinaryWriter w)
	{
		inventory.Write(w);
		w.Write(sensors.Count);
		foreach (KeyValuePair<SensorKey, SensorSetting> sensor in sensors)
		{
			w.Write(sensor.Key.planet);
			w.Write(sensor.Key.entity);
			w.Write((byte)sensor.Value.mode);
			w.Write(sensor.Value.item);
		}
	}

	internal static WorldState Read(BinaryReader r)
	{
		WorldState worldState = new WorldState
		{
			inventory = Inventory.Read(r)
		};
		int num = r.ReadInt32();
		if (num < 0 || num > 1000000)
		{
			throw new InvalidDataException("Invalid RTS sensor count");
		}
		for (int i = 0; i < num; i++)
		{
			int num2 = r.ReadInt32();
			int num3 = r.ReadInt32();
			SensorMode sensorMode = (SensorMode)r.ReadByte();
			int num4 = r.ReadInt32();
			if (num2 <= 0 || num3 <= 0 || sensorMode < SensorMode.Input || sensorMode > SensorMode.Output || num4 < 0)
			{
				throw new InvalidDataException("Invalid RTS sensor entry");
			}
			SensorKey key = new SensorKey(num2, num3);
			if (worldState.sensors.ContainsKey(key))
			{
				throw new InvalidDataException("Duplicate RTS sensor");
			}
			worldState.sensors.Add(key, new SensorSetting
			{
				mode = sensorMode,
				item = num4
			});
		}
		return worldState;
	}
}
internal static class SaveCodec
{
	private static readonly byte[] Header = Encoding.ASCII.GetBytes("ogatas-Dimentional-Storage");

	internal static void Write(BinaryWriter target, WorldState state)
	{
		byte[] array;
		using (MemoryStream memoryStream = new MemoryStream())
		{
			using BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
			state.Write(binaryWriter);
			binaryWriter.Flush();
			array = memoryStream.ToArray();
		}
		target.Write(Header);
		target.Write(2);
		target.Write(array.Length);
		target.Write(Hash(array));
		target.Write(array);
	}

	internal static WorldState Read(BinaryReader source)
	{
		Stream baseStream = source.BaseStream;
		long position = baseStream.Position;
		if (baseStream.Length - position < 8)
		{
			return new WorldState();
		}
		ulong num = source.ReadUInt64();
		switch (num)
		{
		case 7218552640146335599uL:
			throw new InvalidDataException("Lowercase storage header is unsupported. Save once with the migration edition first.");
		default:
			if (num != 3552576238885357391L)
			{
				if (num != BitConverter.ToUInt64(Header, 0))
				{
					baseStream.Position = position;
					return new WorldState();
				}
				baseStream.Position = position;
				byte[] array = source.ReadBytes(Header.Length);
				if (array.Length != Header.Length)
				{
					throw new InvalidDataException("Truncated ogatas-Dimentional-Storage save header");
				}
				for (int i = 0; i < Header.Length; i++)
				{
					if (array[i] != Header[i])
					{
						throw new InvalidDataException("Invalid ogatas-Dimentional-Storage save header");
					}
				}
				int num2 = source.ReadInt32();
				int num3 = source.ReadInt32();
				uint num4 = source.ReadUInt32();
				if (num2 != 2 || num3 < 8 || num3 > 20000000 || num3 > baseStream.Length - baseStream.Position)
				{
					throw new InvalidDataException("Invalid ogatas-Dimentional-Storage save block");
				}
				byte[] array2 = source.ReadBytes(num3);
				if (Hash(array2) != num4)
				{
					throw new InvalidDataException("ogatas-dimentional-storage save checksum mismatch");
				}
				using MemoryStream memoryStream = new MemoryStream(array2);
				using BinaryReader r = new BinaryReader(memoryStream);
				WorldState result = WorldState.Read(r);
				if (memoryStream.Position != memoryStream.Length)
				{
					throw new InvalidDataException("Trailing ogatas-dimentional-storage save data");
				}
				return result;
			}
			goto case 3554277243486295119uL;
		case 3554277243486295119uL:
			throw new InvalidDataException("Old save format is no longer supported. Use a save created by version 0.1.20 or later.");
		}
	}

	private static uint Hash(byte[] data)
	{
		uint num = 2166136261u;
		foreach (byte b in data)
		{
			num = (num ^ b) * 16777619;
		}
		return num;
	}
}
internal struct Packet
{
	internal int count;

	internal int inc;

	internal Packet(int c, int i)
	{
		count = c;
		inc = i;
	}
}
internal sealed class Network
{
	private readonly PlanetFactory f;

	private readonly Plugin config;

	private readonly Inventory inventory;

	private readonly HashSet<int> outputSorters = new HashSet<int>();

	private readonly List<int> coaters = new List<int>();

	private readonly List<StorageComponent> collectors = new List<StorageComponent>();

	private int topologyAge = 1000;

	private int rotation;

	private long moved;

	private readonly List<ItemProto> fuelsHigh = new List<ItemProto>();

	private readonly List<ItemProto> fuelsLow = new List<ItemProto>();

	private readonly List<int> thermalPriority = new List<int>();

	private readonly Dictionary<int, List<ItemProto>> ammoCandidates = new Dictionary<int, List<ItemProto>>();

	private int selectionKeyCount = -1;

	private string cachedPriority;

	private bool supplyingCoaters;

	private bool coatersNeedRefill = true;

	internal Network(PlanetFactory factory, Plugin settings, Inventory stock)
	{
		f = factory;
		config = settings;
		inventory = stock;
	}

	internal void InvalidateTopology()
	{
		topologyAge = 1000;
	}

	internal long Tick()
	{
		StartTick();
		SupplyTier(0);
		SupplyTier(1);
		SupplyTier(2);
		SupplyTier(3);
		return FinishTick();
	}

	internal void StartTick()
	{
		moved = 0L;
		if (topologyAge++ >= 60)
		{
			RefreshTopology();
		}
		rotation = ((rotation != int.MaxValue) ? (rotation + 1) : 0);
		CollectStorage();
		RecoverTanks();
		TransferStations(output: false);
		if (config.directRecovery.Value)
		{
			RecoverProducts();
			RecoverExchangers();
			RecoverMiners();
			RecoverFractionators();
			RecoverCollectors();
		}
		TransferSensors(input: true);
		coatersNeedRefill = true;
		SupplyCoaters();
	}

	internal void SupplyTier(int tier)
	{
		switch (tier)
		{
		case 0:
			SupplyAssemblers(selfProducing: true);
			SupplyLabs(selfProducing: true);
			break;
		case 1:
			SupplyLabs(selfProducing: false);
			break;
		case 2:
			SupplyAssemblers(selfProducing: false);
			SupplyFractionators();
			break;
		case 3:
			SupplyOther();
			break;
		}
	}

	internal long FinishTick()
	{
		for (int i = 0; i < 3; i++)
		{
			switch ((rotation + i) % 3)
			{
			case 0:
				TransferStations(output: true);
				break;
			case 1:
				TransferSensors(input: false);
				break;
			case 2:
				SupplyFilteredStorage();
				break;
			}
		}
		return moved;
	}

	internal void CollectDispenserNow()
	{
		RefreshTopology();
		CollectStorage();
	}

	private IEnumerable<int> Cycle(int cursor, int length)
	{
		int count = Math.Min(cursor, length) - 1;
		if (count > 0)
		{
			int start = rotation % count;
			for (int n = 0; n < count; n++)
			{
				yield return 1 + (start + n) % count;
			}
		}
	}

	private bool Entity(int id)
	{
		return id > 0 && id < f.entityPool.Length && f.entityPool[id].id == id;
	}

	private void RefreshTopology()
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: 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_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: 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_0133: Unknown result type (might be due to invalid IL or missing references)
		topologyAge = 0;
		outputSorters.Clear();
		coaters.Clear();
		collectors.Clear();
		FactorySystem factorySystem = f.factorySystem;
		if (factorySystem?.inserterPool != null)
		{
			foreach (int item in Cycle(factorySystem.inserterCursor, factorySystem.inserterPool.Length))
			{
				InserterComponent val = factorySystem.inserterPool[item];
				if (val.id == item && val.pickTarget > 0)
				{
					outputSorters.Add(val.pickTarget);
				}
			}
		}
		CargoTraffic cargoTraffic = f.cargoTraffic;
		if (cargoTraffic?.spraycoaterPool != null)
		{
			foreach (int item2 in Cycle(cargoTraffic.spraycoaterCursor, cargoTraffic.spraycoaterPool.Length))
			{
				SpraycoaterComponent val2 = cargoTraffic.spraycoaterPool[item2];
				if (val2.id == item2 && Entity(val2.entityId))
				{
					coaters.Add(item2);
				}
			}
		}
		FactoryStorage factoryStorage = f.factoryStorage;
		if (factoryStorage?.storagePool == null)
		{
			return;
		}
		foreach (int item3 in Cycle(factoryStorage.storageCursor, factoryStorage.storagePool.Length))
		{
			StorageComponent val3 = factoryStorage.storagePool[item3];
			if (val3 != null && val3.id == item3 && Entity(val3.entityId))
			{
				ItemProto val4 = ((ProtoSet<ItemProto>)(object)LDB.items).Select((int)f.entityPool[val3.entityId].protoId);
				if (f.entityPool[val3.entityId].battleBaseId > 0 || (val4 != null && val4.prefabDesc?.isStorage == true))
				{
					collectors.Add(val3);
				}
			}
		}
	}

	private bool HasOutput(int entity)
	{
		if (outputSorters.Contains(entity))
		{
			return true;
		}
		if (Entity(entity))
		{
			bool flag = default(bool);
			int num = default(int);
			int num2 = default(int);
			for (int i = 0; i < 16; i++)
			{
				f.ReadObjectConn(entity, i, ref flag, ref num, ref num2);
				if (flag && Entity(num) && f.entityPool[num].beltId > 0)
				{
					return true;
				}
			}
		}
		return false;
	}

	private bool Has(int item)
	{
		return inventory.Count(item) > 0;
	}

	private static bool CanCoat(int item)
	{
		return item != 2206 && item != 2207;
	}

	private int Supply(int item, int wanted, Func<Packet, bool> accept, bool coat = true)
	{
		if (wanted <= 0)
		{
			return 0;
		}
		bool flag = coat && config.spray.Value && CanCoat(item);
		int i;
		Packet packet;
		for (i = 0; i < wanted; i += packet.count)
		{
			if (flag)
			{
				EnsureCoaters();
			}
			int num = (int)Math.Min(wanted - i, inventory.Count(item));
			if (num <= 0)
			{
				break;
			}
			packet = new Packet(num, 0);
			SprayPlan sprayPlan = (flag ? PlanSpray(packet) : null);
			if (sprayPlan != null)
			{
				int num2 = 0;
				foreach (SprayDebit debit in sprayPlan.debits)
				{
					num2 += debit.baseCount + debit.extraCount;
				}
				packet.count = num2;
				packet.inc = sprayPlan.points;
			}
			if (!accept(packet))
			{
				break;
			}
			inventory.Remove(item, packet.count);
			if (sprayPlan != null)
			{
				CommitSpray(sprayPlan);
			}
			moved += packet.count;
		}
		return i;
	}

	private static int FilterCapacity(GRID grid)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		if (grid.filter <= 0)
		{
			return 0;
		}
		return Math.Max(0, ((ProtoSet<ItemProto>)(object)LDB.items).Select(grid.filter)?.StackSize ?? 0);
	}

	private void CollectStorage()
	{
		//IL_00c6: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_015d: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		HashSet<StorageComponent> hashSet = new HashSet<StorageComponent>();
		foreach (StorageComponent collector in collectors)
		{
			FactoryStorage factoryStorage = f.factoryStorage;
			if (collector.id <= 0 || factoryStorage?.storagePool == null || collector.id >= factoryStorage.storagePool.Length || collector != factoryStorage.storagePool[collector.id] || !Entity(collector.entityId))
			{
				continue;
			}
			StorageComponent val = collector;
			while (val != null && hashSet.Add(val))
			{
				if (val.grids != null)
				{
					bool flag = false;
					lock (val)
					{
						for (int i = 0; i < val.size && i < val.grids.Length; i++)
						{
							GRID val2 = val.grids[i];
							if (val2.itemId <= 0 || val2.count <= 0)
							{
								continue;
							}
							int num = ((val2.itemId == val2.filter) ? FilterCapacity(val2) : 0);
							int num2 = (int)Math.Min(Math.Max(0, val2.count - num), inventory.Room(val2.itemId));
							if (num2 <= 0)
							{
								continue;
							}
							int itemId = val2.itemId;
							int num3 = Rules.SplitInc(val2.count, Math.Max(0, val2.inc), num2);
							val2.count -= num2;
							val2.inc -= num3;
							if (val2.count == 0)
							{
								val2.itemId = val2.filter;
								val2.inc = 0;
								if (val2.filter == 0)
								{
									val2.stackSize = 0;
								}
							}
							val.grids[i] = val2;
							inventory.Add(itemId, num2);
							flag = true;
							moved += num2;
						}
					}
					if (flag)
					{
						val.ResetOptimizationFlags();
						val.NotifyStorageChange();
					}
				}
				val = val.nextStorage;
			}
		}
	}

	private void SupplyFilteredStorage()
	{
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		HashSet<StorageComponent> hashSet = new HashSet<StorageComponent>();
		foreach (StorageComponent collector in collectors)
		{
			StorageComponent s = collector;
			while (s != null && hashSet.Add(s))
			{
				if (s.grids != null)
				{
					bool changed = false;
					lock (s)
					{
						for (int i = 0; i < s.size - s.bans && i < s.grids.Length; i++)
						{
							GRID val = s.grids[i];
							int item = val.filter;
							if (item <= 0 || val.count < 0 || (val.count > 0 && val.itemId != item))
							{
								continue;
							}
							int cap = FilterCapacity(val);
							if (cap > 0)
							{
								int slot = i;
								Supply(item, Rules.Missing(val.count, cap), delegate(Packet packet)
								{
									//IL_001c: Unknown result type (might be due to invalid IL or missing references)
									//IL_0021: Unknown result type (might be due to invalid IL or missing references)
									//IL_0068: 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)
									GRID val2 = s.grids[slot];
									val2.itemId = item;
									val2.count += packet.count;
									val2.stackSize = cap;
									s.grids[slot] = val2;
									changed = true;
									return true;
								}, coat: false);
							}
						}
					}
					if (changed)
					{
						s.ResetOptimizationFlags();
						s.NotifyStorageChange();
					}
				}
				s = s.nextStorage;
			}
		}
	}

	private void RecoverTanks()
	{
		//IL_0054: 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_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: 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_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		FactoryStorage factoryStorage = f.factoryStorage;
		if (factoryStorage?.tankPool == null)
		{
			return;
		}
		foreach (int item in Cycle(factoryStorage.tankCursor, factoryStorage.tankPool.Length))
		{
			TankComponent val = factoryStorage.tankPool[item];
			if (val.id != item || !Entity(val.entityId))
			{
				continue;
			}
			if (val.fluidCount == 0)
			{
				val.fluidId = 0;
				val.fluidInc = 0;
				factoryStorage.tankPool[item] = val;
			}
			else
			{
				if (val.fluidId <= 0 || val.fluidCount < 0 || val.fluidInc < 0)
				{
					continue;
				}
				int num = (int)Math.Min(val.fluidCount, inventory.Room(val.fluidId));
				if (num > 0)
				{
					int fluidId = val.fluidId;
					int num2 = Rules.SplitInc(val.fluidCount, val.fluidInc, num);
					val.fluidCount -= num;
					val.fluidInc -= num2;
					if (val.fluidCount == 0)
					{
						val.fluidId = 0;
						val.fluidInc = 0;
					}
					factoryStorage.tankPool[item] = val;
					inventory.Add(fluidId, num);
					moved += num;
				}
			}
		}
	}

	private void TransferStations(bool output)
	{
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Invalid comparison between Unknown and I4
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Invalid comparison between Unknown and I4
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_023a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0242: Unknown result type (might be due to invalid IL or missing references)
		//IL_0257: Unknown result type (might be due to invalid IL or missing references)
		//IL_0259: Unknown result type (might be due to invalid IL or missing references)
		//IL_028d: Unknown result type (might be due to invalid IL or missing references)
		//IL_028f: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Unknown result type (might be due to invalid IL or missing references)
		PlanetTransport transport = f.transport;
		if (transport?.stationPool == null)
		{
			return;
		}
		foreach (int item in Cycle(transport.stationCursor, transport.stationPool.Length))
		{
			StationComponent s = transport.stationPool[item];
			if (s == null || s.id != item || s.isCollector || s.isVeinCollector || !Entity(s.entityId) || s.storage == null)
			{
				continue;
			}
			lock (s.storage)
			{
				int j;
				for (j = 0; j < s.storage.Length; j++)
				{
					StationStore val = s.storage[j];
					if (val.itemId <= 0)
					{
						continue;
					}
					ELogisticStorage val2 = (s.isStellar ? val.remoteLogic : val.localLogic);
					if (output && (int)val2 == 2)
					{
						int val3 = Rules.StationRoom(val.count, val.max, val.localOrder, val.remoteOrder);
						int wanted = Math.Min(val3, ((ProtoSet<ItemProto>)(object)LDB.items).Select(val.itemId)?.StackSize ?? 100);
						Supply(val.itemId, wanted, delegate(Packet p)
						{
							//IL_0017: Unknown result type (might be due to invalid IL or missing references)
							//IL_001c: Unknown result type (might be due to invalid IL or missing references)
							//IL_0044: Unknown result type (might be due to invalid IL or missing references)
							//IL_0045: Unknown result type (might be due to invalid IL or missing references)
							StationStore val5 = s.storage[j];
							val5.count += p.count;
							s.storage[j] = val5;
							return true;
						}, coat: false);
					}
					else if (!output && (int)val2 == 1)
					{
						int num = (int)Math.Min(Rules.StationAvailable(val.count, val.localOrder, val.remoteOrder), inventory.Room(val.itemId));
						if (num > 0)
						{
							int num2 = Rules.SplitInc(val.count, Math.Max(0, val.inc), num);
							StationStore val4 = val;
							val4.count -= num;
							val4.inc -= num2;
							s.storage[j] = val4;
							inventory.Add(val.itemId, num);
							moved += num;
						}
					}
				}
			}
		}
	}

	private void RecoverCollectors()
	{
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		PlanetTransport transport = f.transport;
		if (transport?.stationPool == null)
		{
			return;
		}
		foreach (int item in Cycle(transport.stationCursor, transport.stationPool.Length))
		{
			StationComponent s = transport.stationPool[item];
			if (s == null || s.id != item || !s.isCollector || s.storage == null || HasOutput(s.entityId))
			{
				continue;
			}
			for (int i = 0; i < s.storage.Length; i++)
			{
				int slot = i;
				StationStore val = s.storage[i];
				if (val.itemId > 0)
				{
					RecoverPacket(val.itemId, () => new Packet(s.storage[slot].count, s.storage[slot].inc), delegate(Packet p)
					{
						//IL_0017: Unknown result type (might be due to invalid IL or missing references)
						//IL_001c: 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)
						StationStore val2 = s.storage[slot];
						val2.count = p.count;
						val2.inc = p.inc;
						s.storage[slot] = val2;
					}, Rules.StationAvailable(val.count, val.localOrder, val.remoteOrder));
				}
			}
		}
	}

	private void SupplyFractionators()
	{
		//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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: 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_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: 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_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		FactorySystem sys = f.factorySystem;
		if (sys?.fractionatorPool == null)
		{
			return;
		}
		foreach (int item in Cycle(sys.fractionatorCursor, sys.fractionatorPool.Length))
		{
			FractionatorComponent val = sys.fractionatorPool[item];
			if (val.id != item || !Entity(val.entityId) || (val.belt1 > 0 && !val.isOutput1) || (val.belt2 > 0 && !val.isOutput2))
			{
				continue;
			}
			if (val.fluidId <= 0)
			{
				RecipeProto[] fractionatorRecipes = RecipeProto.fractionatorRecipes;
				foreach (RecipeProto val2 in fractionatorRecipes)
				{
					if (val2.Items != null && val2.Items.Length != 0 && Has(val2.Items[0]) && f.gameData.history.RecipeUnlocked(((Proto)val2).ID))
					{
						((FractionatorComponent)(ref val)).SetRecipe(val2.Items[0], f.entitySignPool);
						sys.fractionatorPool[item] = val;
						break;
					}
				}
			}
			if (val.fluidId <= 0 || val.fluidInputCargoCount >= (float)val.fluidInputMax)
			{
				continue;
			}
			int id = item;
			bool inputCargoAdded = false;
			Supply(val.fluidId, 4, delegate(Packet p)
			{
				//IL_0017: Unknown result type (might be due to invalid IL or missing references)
				//IL_001c: Unknown result type (might be due to invalid IL or missing references)
				//IL_007b: 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)
				FractionatorComponent val3 = sys.fractionatorPool[id];
				val3.fluidInputCount += p.count;
				if (!inputCargoAdded)
				{
					val3.fluidInputCargoCount += 1f;
					inputCargoAdded = true;
				}
				val3.fluidInputInc += p.inc;
				sys.fractionatorPool[id] = val3;
				return true;
			}, config.sprayFractionator.Value);
		}
	}

	private void RecoverFractionators()
	{
		//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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		FactorySystem sys = f.factorySystem;
		if (sys?.fractionatorPool == null)
		{
			return;
		}
		foreach (int item in Cycle(sys.fractionatorCursor, sys.fractionatorPool.Length))
		{
			FractionatorComponent val = sys.fractionatorPool[item];
			if (val.id != item || !Entity(val.entityId) || outputSorters.Contains(val.entityId))
			{
				continue;
			}
			int id = item;
			if (val.productId > 0 && val.belt0 <= 0)
			{
				Recover(val.productId, () => sys.fractionatorPool[id].productOutputCount, delegate(int n)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: Unknown result type (might be due to invalid IL or missing references)
					//IL_003f: Unknown result type (might be due to invalid IL or missing references)
					//IL_0040: Unknown result type (might be due to invalid IL or missing references)
					FractionatorComponent val2 = sys.fractionatorPool[id];
					val2.productOutputCount -= n;
					sys.fractionatorPool[id] = val2;
				});
			}
			if (val.fluidId > 0 && (val.belt1 <= 0 || !val.isOutput1) && (val.belt2 <= 0 || !val.isOutput2))
			{
				RecoverPacket(val.fluidId, () => new Packet(sys.fractionatorPool[id].fluidOutputCount, sys.fractionatorPool[id].fluidOutputInc), delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: 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)
					FractionatorComponent val2 = sys.fractionatorPool[id];
					val2.fluidOutputCount = p.count;
					val2.fluidOutputInc = p.inc;
					sys.fractionatorPool[id] = val2;
				}, int.MaxValue, applyAcceptanceLimit: false);
			}
		}
	}

	private void TransferSensors(bool input)
	{
		//IL_0054: 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_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		CargoTraffic cargoTraffic = f.cargoTraffic;
		if (cargoTraffic?.monitorPool == null)
		{
			return;
		}
		byte b2 = default(byte);
		byte b3 = default(byte);
		foreach (int item2 in Cycle(cargoTraffic.monitorCursor, cargoTraffic.monitorPool.Length))
		{
			MonitorComponent val = cargoTraffic.monitorPool[item2];
			if (val.id != item2 || !Entity(val.entityId))
			{
				continue;
			}
			SensorSetting sensorSetting = config.State.Sensor(f.planetId, val.entityId);
			if (sensorSetting.mode == SensorMode.Normal || input != (sensorSetting.mode == SensorMode.Input))
			{
				continue;
			}
			int targetBeltId = val.targetBeltId;
			if (targetBeltId <= 0 || targetBeltId >= cargoTraffic.beltPool.Length || cargoTraffic.beltPool[targetBeltId].id != targetBeltId || f.entityAnimPool == null || f.entityAnimPool[val.entityId].power <= 0.1f)
			{
				continue;
			}
			BeltComponent val2 = cargoTraffic.beltPool[targetBeltId];
			CargoPath cargoPath = cargoTraffic.GetCargoPath(val2.segPathId);
			if (cargoPath == null)
			{
				continue;
			}
			int num = val2.segIndex + val2.segPivotOffset + val.offset;
			if (num < 0 || num >= cargoPath.pathLength)
			{
				continue;
			}
			if (sensorSetting.mode == SensorMode.Output)
			{
				int item = sensorSetting.item;
				if (item > 0 && inventory.Count(item) != 0)
				{
					byte b = (byte)Math.Min(4L, inventory.Count(item));
					if (cargoPath.TryInsertItem(num, item, b, (byte)0))
					{
						inventory.Remove(item, b);
						moved += b;
					}
				}
			}
			else
			{
				int num2 = 0;
				int num3 = cargoPath.QueryItemAtIndex(num, ref b2, ref b3);
				if (num3 > 0 && (num2 <= 0 || num3 == num2) && inventory.Room(num3) >= b2 && cargoPath.RemoveCargoAtIndex(num))
				{
					inventory.Add(num3, b2);
					moved += b2;
				}
			}
		}
	}

	private void SupplyAssemblers(bool selfProducing)
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: 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_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		FactorySystem factorySystem = f.factorySystem;
		if (factorySystem == null || factorySystem.assemblerPool == null)
		{
			return;
		}
		foreach (int item in Cycle(factorySystem.assemblerCursor, factorySystem.assemblerPool.Length))
		{
			AssemblerComponent val = factorySystem.assemblerPool[item];
			if (val.id == item && Entity(val.entityId) && val.recipeId > 0 && IsSelfProducing(val.recipeExecuteData) == selfProducing && FeedRecipe(val.recipeExecuteData, val.served, val.incServed))
			{
				((AssemblerComponent)(ref factorySystem.assemblerPool[item])).UpdateNeeds();
			}
		}
	}

	private void SupplyLabs(bool selfProducing)
	{
		//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)
		FactorySystem factorySystem = f.factorySystem;
		if (factorySystem == null || factorySystem.labPool == null)
		{
			return;
		}
		foreach (int item in Cycle(factorySystem.labCursor, factorySystem.labPool.Length))
		{
			LabComponent lab = factorySystem.labPool[item];
			if (lab.id != item || !Entity(lab.entityId))
			{
				continue;
			}
			if (!lab.researchMode)
			{
				if (lab.recipeId > 0 && IsSelfProducing(lab.recipeExecuteData) == selfProducing && FeedRecipe(lab.recipeExecuteData, lab.served, lab.incServed))
				{
					((LabComponent)(ref factorySystem.labPool[item])).UpdateNeedsAssemble();
				}
			}
			else
			{
				if (selfProducing || lab.matrixServed == null || lab.matrixIncServed == null)
				{
					continue;
				}
				TechProto val = ((ProtoSet<TechProto>)(object)LDB.techs).Select(f.gameData.history.currentTech);
				if (val == null)
				{
					continue;
				}
				bool flag = false;
				for (int i = 0; i < lab.matrixServed.Length && i < 6; i++)
				{
					int num = 6001 + i;
					if (Array.IndexOf(val.Items, num) >= 0 && lab.matrixServed[i] < 3600)
					{
						int slot = i;
						if (Supply(num, 1, checked(delegate(Packet p)
						{
							int num2 = lab.matrixServed[slot] + 3600 * p.count;
							int num3 = lab.matrixIncServed[slot] + 3600 * p.inc;
							lab.matrixServed[slot] = num2;
							lab.matrixIncServed[slot] = num3;
							return true;
						})) > 0)
						{
							flag = true;
						}
					}
				}
				if (flag)
				{
					((LabComponent)(ref factorySystem.labPool[item])).UpdateNeedsResearch();
				}
			}
		}
	}

	private static bool IsSelfProducing(RecipeExecuteData recipe)
	{
		if (recipe?.requires == null || recipe.products == null)
		{
			return false;
		}
		int[] requires = recipe.requires;
		foreach (int num in requires)
		{
			if (num > 0 && Array.IndexOf(recipe.products, num) >= 0)
			{
				return true;
			}
		}
		return false;
	}

	private static bool AllResources(int[] items)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Invalid comparison between Unknown and I4
		if (items == null || items.Length == 0)
		{
			return false;
		}
		foreach (int num in items)
		{
			ItemProto obj = ((ProtoSet<ItemProto>)(object)LDB.items).Select(num);
			if (obj == null || (int)obj.Type != 1)
			{
				return false;
			}
		}
		return true;
	}

	private bool FeedRecipe(RecipeExecuteData recipe, int[] served, int[] inc)
	{
		if (recipe == null || recipe.requires == null || recipe.requireCounts == null || served == null || inc == null)
		{
			return false;
		}
		bool coat = (config.sprayResourceOnlyRecipes.Value || (!AllResources(recipe.requires) && !AllResources(recipe.products))) && (recipe.productive || config.spraySpeedOnlyRecipes.Value);
		bool result = false;
		for (int i = 0; i < recipe.requires.Length && i < served.Length && i < inc.Length && i < recipe.requireCounts.Length; i++)
		{
			int slot = i;
			int num = Rules.Missing(served[i], recipe.requireCounts[i]);
			if (num > 0 && Supply(recipe.requires[i], num, checked(delegate(Packet p)
			{
				int num2 = served[slot] + p.count;
				int num3 = inc[slot] + p.inc;
				served[slot] = num2;
				inc[slot] = num3;
				return true;
			}), coat) > 0)
			{
				result = true;
			}
		}
		return result;
	}

	private void RefreshSelections()
	{
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Invalid comparison between Unknown and I4
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Expected I4, but got Unknown
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Expected I4, but got Unknown
		if (selectionKeyCount == inventory.KindCount && cachedPriority == config.fuelPriority.Value)
		{
			return;
		}
		selectionKeyCount = inventory.KindCount;
		cachedPriority = config.fuelPriority.Value;
		fuelsHigh.Clear();
		fuelsLow.Clear();
		thermalPriority.Clear();
		ammoCandidates.Clear();
		foreach (int itemId in inventory.ItemIds)
		{
			ItemProto val = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId);
			if (val == null)
			{
				continue;
			}
			if (val.HeatValue > 0 && val.FuelType != 0)
			{
				fuelsHigh.Add(val);
			}
			if ((int)val.AmmoType > 0)
			{
				if (!ammoCandidates.TryGetValue((int)val.AmmoType, out var value))
				{
					value = (ammoCandidates[(int)val.AmmoType] = new List<ItemProto>());
				}
				value.Add(val);
			}
		}
		fuelsHigh.Sort(delegate(ItemProto a, ItemProto b)
		{
			int num2 = b.HeatValue.CompareTo(a.HeatValue);
			return (num2 != 0) ? num2 : ((Proto)a).ID.CompareTo(((Proto)b).ID);
		});
		fuelsLow.AddRange(fuelsHigh);
		fuelsLow.Sort(delegate(ItemProto a, ItemProto b)
		{
			int num2 = a.HeatValue.CompareTo(b.HeatValue);
			return (num2 != 0) ? num2 : ((Proto)a).ID.CompareTo(((Proto)b).ID);
		});
		foreach (List<ItemProto> value2 in ammoCandidates.Values)
		{
			value2.Sort(delegate(ItemProto a, ItemProto b)
			{
				int num2 = b.Ability.CompareTo(a.Ability);
				return (num2 != 0) ? num2 : ((Proto)a).ID.CompareTo(((Proto)b).ID);
			});
		}
		string[] array = cachedPriority.Split(',');
		foreach (string text in array)
		{
			string text2 = text.Trim();
			if (text2.Length == 0)
			{
				continue;
			}
			foreach (ItemProto item in fuelsHigh)
			{
				if ((int.TryParse(text2, out var result) && result == ((Proto)item).ID) || string.Equals(((Proto)item).name, text2, StringComparison.OrdinalIgnoreCase) || string.Equals(((Proto)item).Name, text2, StringComparison.OrdinalIgnoreCase))
				{
					thermalPriority.Add(((Proto)item).ID);
				}
			}
		}
	}

	private int ChooseFuel(PowerGeneratorComponent gen)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		RefreshSelections();
		bool flag = gen.fuelMask == 1;
		if (flag)
		{
			foreach (int item in thermalPriority)
			{
				ItemProto p = ((ProtoSet<ItemProto>)(object)LDB.items).Select(item);
				if (FuelAllowed(p, gen) && Has(item))
				{
					return item;
				}
			}
		}
		if (flag && config.restrictThermalFuel.Value)
		{
			return 0;
		}
		List<ItemProto> list = ((flag && config.fuelFallbackOrder.Value == FuelFallbackOrder.LowestHeatValue) ? fuelsLow : fuelsHigh);
		foreach (ItemProto item2 in list)
		{
			if (FuelAllowed(item2, gen) && Has(((Proto)item2).ID))
			{
				return ((Proto)item2).ID;
			}
		}
		return 0;
	}

	private bool FuelAllowed(ItemProto p, PowerGeneratorComponent gen)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		return p != null && p.HeatValue > 0 && (p.FuelType & gen.fuelMask) != 0 && (gen.fuelMask != 1 || ((Proto)p).ID < 1141 || ((Proto)p).ID > 1143) && (gen.fuelMask != 1 || ((Proto)p).ID != 1120 || HydrogenPolicy.CanUse(inventory.Count(((Proto)p).ID), config.hydrogenMinimumInventory.Value));
	}

	private int ChooseCatalyst(PowerGeneratorComponent gen)
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		int num = 0;
		int num2 = int.MinValue;
		foreach (int itemId in inventory.ItemIds)
		{
			ItemProto val = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId);
			if (val != null && (val.CatalystType & gen.catalystMask) != 0 && Has(itemId) && (val.Ability > num2 || (val.Ability == num2 && (num == 0 || itemId < num))))
			{
				num = itemId;
				num2 = val.Ability;
			}
		}
		return num;
	}

	private int ChooseAmmo(TurretComponent turret)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected I4, but got Unknown
		RefreshSelections();
		if (ammoCandidates.TryGetValue((int)turret.ammoType, out var value))
		{
			foreach (ItemProto item in value)
			{
				if (Has(((Proto)item).ID))
				{
					return ((Proto)item).ID;
				}
			}
		}
		return 0;
	}

	private void SupplyOther()
	{
		//IL_0087: 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_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_027f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0284: Unknown result type (might be due to invalid IL or missing references)
		//IL_0286: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0292: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b1: Invalid comparison between Unknown and I4
		//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_04e8: Unknown result type (might be due to invalid IL or missing references)
		PowerSystem power = f.powerSystem;
		checked
		{
			if (power?.genPool != null)
			{
				foreach (int item4 in Cycle(power.genCursor, power.genPool.Length))
				{
					PowerGeneratorComponent val = power.genPool[item4];
					if (val.id != item4 || !Entity(val.entityId))
					{
						continue;
					}
					int id = item4;
					if (val.gamma)
					{
						if (val.catalystCount != 0 || !f.gameData.history.useIonLayer)
						{
							continue;
						}
						int item = ChooseCatalyst(val);
						if (item > 0)
						{
							Supply(item, 1, delegate(Packet p)
							{
								//IL_0021: Unknown result type (might be due to invalid IL or missing references)
								//IL_0026: Unknown result type (might be due to invalid IL or missing references)
								//IL_005d: Unknown result type (might be due to invalid IL or missing references)
								//IL_005e: Unknown result type (might be due to invalid IL or missing references)
								PowerGeneratorComponent val5 = power.genPool[id];
								((PowerGeneratorComponent)(ref val5)).SetNewCatalyst(item, (short)1, (short)p.inc);
								power.genPool[id] = val5;
								return true;
							});
						}
					}
					else
					{
						if (val.fuelMask == 0 || val.fuelCount != 0)
						{
							continue;
						}
						int item2 = ChooseFuel(val);
						if (item2 > 0)
						{
							Supply(item2, 1, delegate(Packet p)
							{
								//IL_0021: Unknown result type (might be due to invalid IL or missing references)
								//IL_0026: Unknown result type (might be due to invalid IL or missing references)
								//IL_005d: Unknown result type (might be due to invalid IL or missing references)
								//IL_005e: Unknown result type (might be due to invalid IL or missing references)
								PowerGeneratorComponent val5 = power.genPool[id];
								((PowerGeneratorComponent)(ref val5)).SetNewFuel(item2, (short)1, (short)p.inc);
								power.genPool[id] = val5;
								return true;
							}, val.fuelMask != 1 || config.sprayThermalFuel.Value);
						}
					}
				}
			}
			SupplyExchangers();
			DataPool<TurretComponent> turrets = f.defenseSystem?.turrets;
			if (turrets?.buffer != null)
			{
				foreach (int item5 in Cycle(turrets.cursor, turrets.buffer.Length))
				{
					TurretComponent val2 = turrets.buffer[item5];
					if (val2.id != item5 || !Entity(val2.entityId) || val2.itemCount != 0 || unchecked((int)val2.ammoType) == 0)
					{
						continue;
					}
					int id2 = item5;
					int item3 = ChooseAmmo(val2);
					if (item3 == 0)
					{
						continue;
					}
					Supply(item3, 1, delegate(Packet p)
					{
						//IL_0017: Unknown result type (might be due to invalid IL or missing references)
						//IL_001c: 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_0054: Unknown result type (might be due to invalid IL or missing references)
						TurretComponent val5 = turrets.buffer[id2];
						if (!((TurretComponent)(ref val5)).SetNewItem(item3, (short)1, (short)p.inc))
						{
							return false;
						}
						turrets.buffer[id2] = val5;
						return true;
					});
				}
			}
			FactorySystem sys = f.factorySystem;
			if (sys == null)
			{
				return;
			}
			if (sys.ejectorPool != null)
			{
				foreach (int item6 in Cycle(sys.ejectorCursor, sys.ejectorPool.Length))
				{
					EjectorComponent val3 = sys.ejectorPool[item6];
					if (val3.id != item6 || !Entity(val3.entityId) || val3.bulletCount != 0)
					{
						continue;
					}
					int id3 = item6;
					int bulletId = val3.bulletId;
					if (bulletId > 0)
					{
						Supply(bulletId, 1, delegate(Packet p)
						{
							//IL_0017: Unknown result type (might be due to invalid IL or missing references)
							//IL_001c: 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_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)
							EjectorComponent val5 = sys.ejectorPool[id3];
							val5.bulletCount = 1;
							val5.bulletInc += p.inc;
							sys.ejectorPool[id3] = val5;
							return true;
						});
					}
				}
			}
			if (sys.siloPool == null)
			{
				return;
			}
			foreach (int item7 in Cycle(sys.siloCursor, sys.siloPool.Length))
			{
				SiloComponent val4 = sys.siloPool[item7];
				if (val4.id != item7 || !Entity(val4.entityId) || val4.bulletCount != 0)
				{
					continue;
				}
				int id4 = item7;
				int bulletId2 = val4.bulletId;
				if (bulletId2 > 0)
				{
					Supply(bulletId2, 1, delegate(Packet p)
					{
						//IL_0017: Unknown result type (might be due to invalid IL or missing references)
						//IL_001c: 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_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)
						SiloComponent val5 = sys.siloPool[id4];
						val5.bulletCount = 1;
						val5.bulletInc += p.inc;
						sys.siloPool[id4] = val5;
						return true;
					});
				}
			}
		}
	}

	private void RecoverProducts()
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: 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_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: Unknown result type (might be due to invalid IL or missing references)
		//IL_0233: Unknown result type (might be due to invalid IL or missing references)
		//IL_023d: Unknown result type (might be due to invalid IL or missing references)
		FactorySystem factorySystem = f.factorySystem;
		if (factorySystem?.assemblerPool != null)
		{
			foreach (int item in Cycle(factorySystem.assemblerCursor, factorySystem.assemblerPool.Length))
			{
				AssemblerComponent val = factorySystem.assemblerPool[item];
				if (val.id == item && Entity(val.entityId) && !HasOutput(val.entityId))
				{
					RecoverRecipe(val.recipeExecuteData, val.produced);
				}
			}
		}
		if (factorySystem?.labPool != null)
		{
			foreach (int item2 in Cycle(factorySystem.labCursor, factorySystem.labPool.Length))
			{
				LabComponent val2 = factorySystem.labPool[item2];
				if (val2.id == item2 && Entity(val2.entityId) && !val2.researchMode && !HasOutput(val2.entityId))
				{
					RecoverRecipe(val2.recipeExecuteData, val2.produced);
				}
			}
		}
		PowerSystem power = f.powerSystem;
		if (power?.genPool == null)
		{
			return;
		}
		foreach (int item3 in Cycle(power.genCursor, power.genPool.Length))
		{
			PowerGeneratorComponent val3 = power.genPool[item3];
			if (val3.id == item3 && Entity(val3.entityId) && val3.gamma && !HasOutput(val3.entityId) && val3.productId > 0 && !(val3.productCount < 1f))
			{
				int id = item3;
				Recover(val3.productId, () => (int)power.genPool[id].productCount, delegate(int n)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0040: Unknown result type (might be due to invalid IL or missing references)
					//IL_0041: Unknown result type (might be due to invalid IL or missing references)
					PowerGeneratorComponent val4 = power.genPool[id];
					val4.productCount -= n;
					power.genPool[id] = val4;
				});
			}
		}
	}

	private void RecoverRecipe(RecipeExecuteData recipe, int[] produced)
	{
		if (recipe?.products == null || produced == null)
		{
			return;
		}
		if (!ProductionLimit.MultipleProducts(recipe.products))
		{
			for (int i = 0; i < recipe.products.Length && i < produced.Length; i++)
			{
				if (produced[i] > 0)
				{
					int slot = i;
					Recover(recipe.products[i], () => produced[slot], delegate(int n)
					{
						produced[slot] -= n;
					});
				}
			}
			return;
		}
		lock (inventory)
		{
			if (ProductionLimit.AllProductsOverLimit(inventory, recipe.products, config.buildingStacks.Value, config.matrixStacks.Value, config.itemStackLimits.Value, config.allItemStacks.Value))
			{
				return;
			}
			for (int num = 0; num < recipe.products.Length && num < produced.Length; num++)
			{
				int num2 = recipe.products[num];
				if (num2 > 0 && produced[num] > 0 && ProductionLimit.Maximum(num2, config.buildingStacks.Value, config.matrixStacks.Value, config.itemStackLimits.Value, config.allItemStacks.Value) != 0)
				{
					int num3 = (int)Math.Min(produced[num], inventory.Room(num2));
					if (num3 > 0)
					{
						produced[num] -= num3;
						inventory.Add(num2, num3);
						moved += num3;
					}
				}
			}
		}
	}

	private void Recover(int item, Func<int> stock, Action<int> subtract)
	{
		int num = (int)Math.Min(Math.Max(0, stock()), ProductionLimit.Room(inventory, item, config.buildingStacks.Value, config.matrixStacks.Value, config.itemStackLimits.Value, config.allItemStacks.Value));
		if (num > 0)
		{
			subtract(num);
			inventory.Add(item, num);
			moved += num;
		}
	}

	private void SupplyExchangers()
	{
		//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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		PowerSystem power = f.powerSystem;
		if (power?.excPool == null)
		{
			return;
		}
		foreach (int item in Cycle(power.excCursor, power.excPool.Length))
		{
			PowerExchangerComponent val = power.excPool[item];
			if (val.id != item || !Entity(val.entityId))
			{
				continue;
			}
			int id = item;
			if (val.targetState == 1f && val.emptyCount == 0 && val.emptyId > 0)
			{
				Supply(val.emptyId, 1, delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0045: Unknown result type (might be due to invalid IL or missing references)
					//IL_0046: Unknown result type (might be due to invalid IL or missing references)
					PowerExchangerComponent val2 = power.excPool[id];
					short num = checked((short)p.inc);
					((PowerExchangerComponent)(ref val2)).SetEmptyCount(1, (int)num);
					power.excPool[id] = val2;
					return true;
				}, coat: false);
			}
			else if (val.targetState == -1f && val.fullCount == 0 && val.fullId > 0)
			{
				Supply(val.fullId, 1, delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0045: Unknown result type (might be due to invalid IL or missing references)
					//IL_0046: Unknown result type (might be due to invalid IL or missing references)
					PowerExchangerComponent val2 = power.excPool[id];
					short num = checked((short)p.inc);
					((PowerExchangerComponent)(ref val2)).SetFullCount(1, (int)num);
					power.excPool[id] = val2;
					return true;
				}, coat: false);
			}
		}
	}

	private void RecoverExchangers()
	{
		//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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: 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)
		PowerSystem power = f.powerSystem;
		if (power?.excPool == null)
		{
			return;
		}
		foreach (int item in Cycle(power.excCursor, power.excPool.Length))
		{
			PowerExchangerComponent val = power.excPool[item];
			if (val.id != item || !Entity(val.entityId) || HasOutput(val.entityId))
			{
				continue;
			}
			int id = item;
			if (val.targetState == 1f && val.fullId > 0)
			{
				RecoverPacket(val.fullId, () => new Packet(power.excPool[id].fullCount, power.excPool[id].fullInc), delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: 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_0048: Unknown result type (might be due to invalid IL or missing references)
					PowerExchangerComponent val2 = power.excPool[id];
					((PowerExchangerComponent)(ref val2)).SetFullCount(p.count, p.inc);
					power.excPool[id] = val2;
				});
			}
			else if (val.targetState == -1f && val.emptyId > 0)
			{
				RecoverPacket(val.emptyId, () => new Packet(power.excPool[id].emptyCount, power.excPool[id].emptyInc), delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: 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_0048: Unknown result type (might be due to invalid IL or missing references)
					PowerExchangerComponent val2 = power.excPool[id];
					((PowerExchangerComponent)(ref val2)).SetEmptyCount(p.count, p.inc);
					power.excPool[id] = val2;
				});
			}
		}
	}

	private void RecoverMiners()
	{
		//IL_008b: 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_0092: 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_00ac: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02db: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0306: Unknown result type (might be due to invalid IL or missing references)
		//IL_030d: Unknown result type (might be due to invalid IL or missing references)
		//IL_031c: Unknown result type (might be due to invalid IL or missing references)
		FactorySystem sys = f.factorySystem;
		if (sys?.minerPool == null)
		{
			return;
		}
		foreach (int item in Cycle(sys.minerCursor, sys.minerPool.Length))
		{
			MinerComponent val = sys.minerPool[item];
			if (val.id == item && Entity(val.entityId) && val.productId > 0 && val.insertTarget == 0 && !HasOutput(val.entityId))
			{
				int id = item;
				RecoverPacket(val.productId, () => new Packet(sys.minerPool[id].productCount, 0), delegate(Packet p)
				{
					//IL_0017: Unknown result type (might be due to invalid IL or missing references)
					//IL_001c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0040: Unknown result type (might be due to invalid IL or missing references)
					//IL_0041: Unknown result type (might be due to invalid IL or missing references)
					MinerComponent val3 = sys.minerPool[id];
					val3.productCount = p.count;
					sys.minerPool[id] = val3;
				});
			}
		}
		PlanetTransport transport = f.transport;
		if (transport?.stationPool == null)
		{
			return;
		}
		foreach (int item2 in Cycle(transport.stationCursor, transport.stationPool.Length))
		{
			StationComponent station = transport.stationPool[item2];
			if (station == null || station.id != item2 || !station.isVeinCollector || !Entity(station.entityId) || HasOutput(station.entityId) || station.storage == null)
			{
				continue;
			}
			int minerId = f.entityPool[station.entityId].minerId;
			if (minerId <= 0 || minerId >= sys.minerPool.Length || sys.minerPool[minerId].id != minerId || sys.minerPool[minerId].entityId != station.entityId || sys.minerPool[minerId].insertTarget != 0)
			{
				continue;
			}
			for (int num = 0; num < station.storage.Length; num++)
			{
				int slot = num;
				StationStore val2 = station.storage[slot];
				if (val2.itemId > 0 && val2.count > 0)
				{
					int limit = Rules.StationAvailable(val2.count, val2.localOrder, val2.remoteOrder);
					RecoverPacket(val2.itemId, () => new Packet(station.storage[slot].count, station.storage[slot].inc), delegate(Packet p)
					{
						//IL_0017: Unknown result type (might be due to invalid IL or missing references)
						//IL_001c: 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)
						StationStore val3 = station.storage[slot];
						val3.count = p.count;
						val3.inc = p.inc;
						station.storage[slot] = val3;
					}, limit);
				}
			}
		}
	}

	private void RecoverPacket(int item, Func<Packet> stock, Action<Packet> set, int limit = int.MaxValue, bool applyAcceptanceLimit = true)
	{
		Packet packet = stock();
		int num = (int)Math.Min(Math.Min(Math.Max(0, packet.count), Math.Max(0, limit)), applyAcceptanceLimit ? ProductionLimit.Room(inventory, item, config.buildingStacks.Value, config.matrixStacks.Value, config.itemStackLimits.Value, config.allItemStacks.Value) : inventory.Room(item));
		if (num > 0 && packet.inc >= 0)
		{
			int num2 = Rules.SplitInc(packet.count, packet.inc, num);
			set(new Packet(packet.count - num, packet.inc - num2));
			inventory.Add(item, num);
			moved += num;
		}
	}

	private void EnsureCoaters()
	{
		if (coatersNeedRefill && !supplyingCoaters)
		{
			SupplyCoaters();
		}
	}

	private SprayPlan PlanSpray(Packet p)
	{
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0210: Unknown result type (might be due to invalid IL or missing references)
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0222: Unknown result type (might be due to invalid IL or missing references)
		//IL_0229: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		if (p.count <= 0 || p.inc < 0 || p.inc > (long)p.count * 10L)
		{
			return null;
		}
		int num = 0;
		int num2 = 0;
		foreach (int coater in coaters)
		{
			SpraycoaterComponent val = f.cargoTraffic.spraycoaterPool[coater];
			if (val.id == coater && Entity(val.entityId) && val.incAbility > 0 && val.incAbility <= 10 && val.incSprayTimes > 0 && val.incCount >= 0 && val.extraIncCount >= 0 && (long)val.incCount + (long)val.extraIncCount > 0 && Powered(val) && (val.incAbility > num2 || (val.incAbility == num2 && (num == 0 || coater < num))))
			{
				num = coater;
				num2 = val.incAbility;
			}
		}
		if (num == 0)
		{
			return null;
		}
		SpraycoaterComponent val2 = f.cargoTraffic.spraycoaterPool[num];
		int val3 = (int)Math.Min(2147483647L, (long)val2.incCount + (long)val2.extraIncCount);
		int num3 = Math.Min(val3, Rules.SprayCost(p.count, p.inc, num2));
		if (num3 <= 0)
		{
			return null;
		}
		int num4 = Math.Min(val2.extraIncCount, num3);
		int num5 = num3 - num4;
		SprayPlan sprayPlan = new SprayPlan
		{
			points = Rules.SprayInc(p.count, p.inc, num2, num3)
		};
		sprayPlan.debits.Add(new SprayDebit
		{
			id = num,
			baseCount = num5,
			extraCount = num4,
			item = val2.incItemId,
			consumed = val2.incCount / val2.incSprayTimes - (val2.incCount - num5) / val2.incSprayTimes
		});
		return sprayPlan;
	}

	private bool Powered(SpraycoaterComponent c)
	{
		//IL_000d: 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)
		return f.entityAnimPool != null && c.entityId < f.entityAnimPool.Length && f.entityAnimPool[c.entityId].power > 0.1f;
	}

	private void CommitSpray(SprayPlan plan)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		foreach (SprayDebit debit in plan.debits)
		{
			SpraycoaterComponent val = f.cargoTraffic.spraycoaterPool[debit.id];
			val.incCount -= debit.baseCount;
			val.extraIncCount -= debit.extraCount;
			if (val.incCount == 0 && val.extraIncCount == 0)
			{
				val.incItemId = 0;
				val.incAbility = 0;
				coatersNeedRefill = true;
			}
			f.cargoTraffic.spraycoaterPool[debit.id] = val;
			FactoryProductionStat[] array = f.gameData.statistics?.production?.factoryStatPool;
			if (array != null && f.index < array.Length && array[f.index]?.consumeRegister != null)
			{
				int[] consumeRegister = array[f.index].consumeRegister;
				if (debit.item > 0 && debit.item < consumeRegister.Length)
				{
					consumeRegister[debit.item] += debit.consumed;
				}
			}
		}
	}

	private void SupplyCoaters()
	{
		//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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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_01f1: 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_01f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0202: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0214: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Unknown result type (might be due to invalid IL or missing references)
		if (!config.spray.Value || supplyingCoaters)
		{
			return;
		}
		supplyingCoaters = true;
		coatersNeedRefill = false;
		try
		{
			foreach (int id in coaters)
			{
				SpraycoaterComponent val = f.cargoTraffic.spraycoaterPool[id];
				if (val.id != id || !Entity(val.entityId) || val.incCount != 0 || val.extraIncCount != 0)
				{
					continue;
				}
				int item = 0;
				int[] array = new int[3] { 1143, 1142, 1141 };
				foreach (int num in array)
				{
					if (Has(num))
					{
						item = num;
						break;
					}
				}
				if (item == 0)
				{
					continue;
				}
				ItemProto proto = ((ProtoSet<ItemProto>)(object)LDB.items).Select(item);
				if (proto == null || proto.HpMax <= 0 || proto.Ability <= 0 || proto.Ability >= Cargo.incTable.Length)
				{
					continue;
				}
				int num2 = (int)((double)(proto.HpMax * Cargo.incTable[10]) * 0.001 + 0.1);
				while (Has(item))
				{
					val = f.cargoTraffic.spraycoaterPool[id];
					if (val.incCount < 0 || val.extraIncCount < 0 || (long)val.incCount + (long)val.extraIncCount + proto.HpMax + num2 > val.incCapacity || Supply(item, 1, delegate(Packet p)
					{
						//IL_0026: Unknown result type (might be due to invalid IL or missing references)
						//IL_002b: 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_0077: 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_00e2: Unknown result type (might be due to invalid IL or missing references)
						//IL_011b: Unknown result type (might be due to invalid IL or missing references)
						//IL_011c: Unknown result type (might be due to invalid IL or missing references)
						SpraycoaterComponent val2 = f.cargoTraffic.spraycoaterPool[id];
						int num3 = Math.Min(10, p.inc);
						int num4 = (int)((double)(proto.HpMax * Cargo.incTable[num3]) * 0.001 + 0.1);
						int num5;
						int num6;
						checked
						{
							num5 = val2.incCount + proto.HpMax;
							num6 = val2.extraIncCount + num4;
						}
						if ((long)num5 + (long)num6 > val2.incCapacity)
						{
							return false;
						}
						val2.incItemId = item;
						val2.incAbility = proto.Ability;
						val2.incSprayTimes = proto.HpMax;
						val2.incCount = num5;
						val2.extraIncCount = num6;
						val2.incUsed = val2.incUsed || num4 > 0;
						f.cargoTraffic.spraycoaterPool[id] = val2;
						return true;
					}, coat: false) == 0)
					{
						break;
					}
				}
			}
		}
		finally
		{
			supplyingCoaters = false;
		}
	}
}
internal sealed class StorageWindow
{
	private readonly Plugin plugin;

	private GameObject panel;

	private GameObject monitorPanel;

	private CanvasGroup nativeGroup;

	private float nativeAlpha;

	private bool nativeInteractable;

	private bool nativeRaycasts;

	private bool ownsNativeGroup;

	private RectTransform grid;

	private Text statusText;

	private Text monitorText;

	private Image itemTab;

	private Image facilityTab;

	private Image normalSensorButton;

	private Image outputSensorButton;

	private Image inputSensorButton;

	private readonly Vector3[] monitorCorners = (Vector3[])(object)new Vector3[4];

	private readonly Text[] amounts = (Text[])(object)new Text[112];

	private readonly Image[] icons = (Image[])(object)new Image[112];

	private readonly ItemProto[] items = (ItemProto[])(object)new ItemProto[112];

	private readonly List<RaycastResult> hits = new List<RaycastResult>();

	private int splitItem;

	private int currentType = 1;

	private bool splitting;

	private bool suppressLeft;

	private bool suppressRight;

	private UIMonitorWindow monitorWindow;

	private readonly Dictionary<Text, string> localizedLabels = new Dictionary<Text, string>();

	private bool hoverDisplayed;

	private bool lastJapanese;

	private float messageUntil;

	private void MaskNative(UIInventoryWindow native, bool mask)
	{
		if (!mask)
		{
			if ((Object)(object)nativeGroup != (Object)null)
			{
				if (ownsNativeGroup)
				{
					Object.Destroy((Object)(object)nativeGroup);
				}
				else
				{
					nativeGroup.alpha = nativeAlpha;
					nativeGroup.interactable = nativeInteractable;
					nativeGroup.blocksRaycasts = nativeRaycasts;
				}
			}
			nativeGroup = null;
		}
		else
		{
			if ((Object)(object)native == (Object)null)
			{
				return;
			}
			if ((Object)(object)nativeGroup == (Object)null)
			{
				nativeGroup = ((Component)native).GetComponent<CanvasGroup>();
				ownsNativeGroup = (Object)(object)nativeGroup == (Object)null;
				if (ownsNativeGroup)
				{
					nativeGroup = ((Component)native).gameObject.AddComponent<CanvasGroup>();
				}
				nativeAlpha = nativeGroup.alpha;
				nativeInteractable = nativeGroup.interactable;
				nativeRaycasts = nativeGroup.blocksRaycasts;
			}
			nativeGroup.alpha = 0f;
			nativeGroup.interactable = false;
			nativeGroup.blocksRaycasts = false;
		}
	}

	internal void SyncNativeInventory()
	{
		UIInventoryWindow val = UIRoot.instance?.uiGame?.inventoryWindow;
		bool enabled = PlayerInventoryPatches.Enabled;
		MaskNative(val, enabled);
		if (!enabled || (Object)(object)val == (Object)null || !((ManualBehaviour)val).active)
		{
			HideInventory();
			return;
		}
		ShowInventory();
		AlignNativeLayer(val);
	}

	private void AlignNativeLayer(UIInventoryWindow native)
	{
		if (!((Object)(object)panel == (Object)null) && !((Object)(object)native == (Object)null) && !((Object)(object)((Component)native).transform.parent == (Object)null))
		{
			if ((Object)(object)panel.transform.parent != (Object)(object)((Component)native).transform.parent)
			{
				panel.transform.SetParent(((Component)native).transform.parent, false);
			}
			int num = ((Component)native).transform.GetSiblingIndex();
			if (panel.transform.GetSiblingIndex() < num)
			{
				num--;
			}
			panel.transform.SetSiblingIndex(num + 1);
		}
	}

	private void ShowInventory()
	{
		if ((Object)(object)panel == (Object)null)
		{
			Build();
		}
		if (!((Object)(object)panel == (Object)null) && !panel.activeSelf)
		{
			panel.SetActive(true);
			RefreshItems();
		}
	}

	private void HideInventory()
	{
		CancelSplit();
		if ((Object)(object)panel != (Object)null)
		{
			panel.SetActive(false);
		}
	}

	private void RequestClose()
	{
		if (PlayerInventoryPatches.Enabled)
		{
			UIRoot instance = UIRoot.instance;
			if (instance != null)
			{
				UIGame uiGame = instance.uiGame;
				if (uiGame != null)
				{
					uiGame.ShutPlayerInventory();
				}
			}
		}
		else
		{
			HideInventory();
		}
	}

	private void Status(string text)
	{
		statusText.text = text;
		messageUntil = Time.unscaledTime + 2f;
		hoverDisplayed = false;
	}

	private void RefreshLanguage()
	{
		bool isJAJA = Localization.isJAJA;
		if (isJAJA == lastJapanese)
		{
			return;
		}
		lastJapanese = isJAJA;
		foreach (KeyValuePair<Text, string> localizedLabel in localizedLabels)
		{
			if ((Object)(object)localizedLabel.Key != (Object)null)
			{
				localizedLabel.Key.text = UiText.Label(localizedLabel.Value);
			}
		}
		if ((Object)(object)statusText != (Object)null)
		{
			statusText.text = "";
		}
		hoverDisplayed = false;
		messageUntil = 0f;
	}

	internal StorageWindow(Plugin p)
	{
		plugin = p;
	}

	internal void Hide()
	{
		HideInventory();
		if ((Object)(object)monitorPanel != (Object)null)
		{
			monitorPanel.SetActive(false);
		}
	}

	internal void Destroy()
	{
		MaskNative(null, mask: false);
		CancelSplit();
		if ((Object)(object)panel != (Object)null)
		{
			Object.Destroy((Object)(object)panel);
		}
		if ((Object)(object)monitorPanel != (Object)null)
		{
			Object.Destroy((Object)(object)monitorPanel);
		}
	}

	private void CancelSplit()
	{
		if (splitting && (Object)(object)UIRoot.instance?.uiGame != (Object)null)
		{
			UIRoot.instance.uiGame.CloseGridSplit();
		}
		splitting = false;
		splitItem = 0;
	}

	private void Build()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: 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_007f: Expected O, but got Unknown
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Expected O, but got Unknown
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Expected O, but got Unknown
		//IL_015d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0208: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_024e: Expected O, but got Unknown
		//IL_0263: Unknown result type (might be due to invalid IL or missing references)
		//IL_026d: Expected O, but got Unknown
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_028c: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0304: Unknown result type (might be due to invalid IL or missing references)
		//IL_030b: Expected O, but got Unknown
		//IL_0326: Unknown result type (might be due to invalid IL or missing references)
		//IL_0338: Unknown result type (might be due to invalid IL or missing references)
		//IL_0347: Unknown result type (might be due to invalid IL or missing references)
		//IL_0351: Expected O, but got Unknown
		//IL_036d: Unknown result type (might be due to invalid IL or missing references)
		//IL_039d: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a4: Expected O, but got Unknown
		//IL_03c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_03de: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e8: Expected O, but got Unknown
		//IL_0420: Unknown result type (might be due to invalid IL or missing references)
		//IL_042f: Unknown result type (might be due to invalid IL or missing references)
		//IL_045c: Unknown result type (might be due to invalid IL or missing references)
		//IL_049f: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0502: Unknown result type (might be due to invalid IL or missing references)
		//IL_0521: Unknown result type (might be due to invalid IL or missing references)
		//IL_0530: Unknown result type (might be due to invalid IL or missing references)
		UIGame val = UIRoot.instance?.uiGame;
		if (!((Object)(object)val?.itemPicker == (Object)null))
		{
			panel = Box("Dimensional Storage Inventory", ((Component)val.itemPicker).transform.parent, new Vector2(680f, 530f), new Vector2(-280f, 0f));
			RectTransform val2 = (RectTransform)panel.transform;
			((Graphic)panel.GetComponent<Image>()).color = new Color(0.37f, 0.36f, 0.35f, 0.98f);
			Frame((Transform)(object)val2, new Vector2(680f, 530f));
			TextAt((Transform)(object)val2, "次元ストレージ", new Vector2(16f, -10f), new Vector2(400f, 30f), 20);
			GameObject val3 = new GameObject("Title drag area", new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(StorageWindowDrag)
			});
			val3.transform.SetParent((Transform)(object)val2, false);
			Place((RectTransform)val3.transform, Vector2.zero, new Vector2(570f, 42f));
			((Graphic)val3.GetComponent<Image>()).color = Color.clear;
			val3.GetComponent<StorageWindowDrag>().window = val2;
			ButtonAt((Transform)(object)val2, "閉じる", new Vector2(582f, -10f), new Vector2(82f, 28f), delegate
			{
				RequestClose();
			});
			itemTab = ButtonAt((Transform)(object)val2, "アイテム", new Vector2(16f, -50f), new Vector2(105f, 30f), delegate
			{
				currentType = 1;
				RefreshItems();
			});
			facilityTab = ButtonAt((Transform)(object)val2, "施設", new Vector2(131f, -50f), new Vector2(105f, 30f), delegate
			{
				currentType = 2;
				RefreshItems();
			});
			GameObject val4 = new GameObject("Dimensional Storage Item Grid", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			val4.transform.SetParent((Transform)(object)val2, false);
			grid = (RectTransform)val4.transform;
			Place(grid, new Vector2(18f, -94f), new Vector2(644f, 368f));
			((Graphic)val4.GetComponent<Image>()).color = new Color(0.2f, 0.2f, 0.2f, 1f);
			for (int num = 0; num < 112; num++)
			{
				int num2 = num % 14;
				int num3 = num / 14;
				GameObject val5 = new GameObject("Dimensional Storage Cell " + num, new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				});
				val5.transform.SetParent((Transform)(object)grid, false);
				Place((RectTransform)val5.transform, new Vector2((float)(num2 * 46), (float)(-num3 * 46)), new Vector2(45f, 45f));
				((Graphic)val5.GetComponent<Image>()).color = new Color(0.29f, 0.29f, 0.28f, 1f);
				GameObject val6 = new GameObject("Icon", new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				});
				val6.transform.SetParent(val5.transform, false);
				Place((RectTransform)val6.transform, new Vector2(3f, -3f), new Vector2(40f, 40f));
				icons[num] = val6.GetComponent<Image>();
				icons[num].preserveAspect = true;
				Text val7 = TextAt(val5.transform, "", new Vector2(1f, -26f), new Vector2(44f, 20f), 12);
				val7.alignment = (TextAnchor)5;
				((Graphic)val7).color = new Color(0.62f, 0.95f, 1f, 1f);
				val7.horizontalOverflow = (HorizontalWrapMode)1;
				val7.verticalOverflow = (VerticalWrapMode)1;
				((Behaviour)val7).enabled = true;
				((Component)val7).transform.SetAsLastSibling();
				Shadow val8 = ((Component)val7).gameObject.AddComponent<Shadow>();
				val8.effectColor = Color.black;
				val8.effectDistance = new Vector2(1f, -1f);
				amounts[num] = val7;
			}
			TextAt((Transform)(object)val2, "右ドラッグ:取り出し / 持ったアイテムをドロップ:入庫", new Vector2(16f, -471f), new Vector2(648f, 23f), 14);
			statusText = TextAt((Transform)(object)val2, "", new Vector2(16f, -499f), new Vector2(648f, 23f), 14);
			panel.SetActive(false);
		}
	}

	private void RefreshItems()
	{
		//IL_004f: 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_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)itemTab != (Object)null)
		{
			((Graphic)itemTab).color = ((currentType == 1) ? new Color(0.4f, 0.44f, 0.44f, 1f) : new Color(0.31f, 0.31f, 0.3f, 1f));
		}
		if ((Object)(object)facilityTab != (Object)null)
		{
			((Graphic)facilityTab).color = ((currentType == 2) ? new Color(0.4f, 0.44f, 0.44f, 1f) : new Color(0.31f, 0.31f, 0.3f, 1f));
		}
		Array.Clear(items, 0, items.Length);
		ItemProto[] dataArray = ((ProtoSet<ItemProto>)(object)LDB.items).dataArray;
		foreach (ItemProto val in dataArray)
		{
			int num = GridLayout.Index(val.GridIndex, currentType);
			if (num >= 0)
			{
				items[num] = val;
			}
		}
		for (int j = 0; j < 112; j++)
		{
			Image obj = icons[j];
			ItemProto obj2 = items[j];
			obj.sprite = ((obj2 != null) ? obj2.iconSprite : null);
			((Behaviour)icons[j]).enabled = items[j] != null;
		}
		RefreshNumbers();
	}

	private void RefreshNumbers()
	{
		for (int i = 0; i < 112; i++)
		{
			if ((Object)(object)amounts[i] != (Object)null)
			{
				Text val = amounts[i];
				((Behaviour)val).enabled = true;
				((Component)val).transform.SetAsLastSibling();
				val.text = ((items[i] == null) ? "" : ((plugin.State.inventory.Count(((Proto)items[i]).ID) == 0L) ? "0" : GridLayout.Amount(plugin.State.inventory.Count(((Proto)items[i]).ID))));
			}
		}
	}

	private int MouseCell()
	{
		//IL_0030: 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)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)grid == (Object)null || (Object)(object)EventSystem.current == (Object)null)
		{
			return -1;
		}
		PointerEventData val = new PointerEventData(EventSystem.current)
		{
			position = Vector2.op_Implicit(Input.mousePosition)
		};
		hits.Clear();
		EventSystem.current.RaycastAll(val, hits);
		if (hits.Count != 0)
		{
			RaycastResult val2 = hits[0];
			if (((RaycastResult)(ref val2)).gameObject.transform.IsChildOf((Transform)(object)grid))
			{
				Vector2 val3 = default(Vector2);
				if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(grid, Vector2.op_Implicit(Input.mousePosition), UIRoot.instance.overlayCanvas.worldCamera, ref val3))
				{
					return -1;
				}
				int num = Mathf.FloorToInt(val3.x / 46f);
				int num2 = Mathf.FloorToInt((0f - val3.y) / 46f);
				return (num >= 0 && num < 14 && num2 >= 0 && num2 < 8) ? (num2 * 14 + num) : (-1);
			}
		}
		return -1;
	}

	internal void Update()
	{
		RefreshLanguage();
		if ((Object)(object)panel != (Object)null && panel.activeSelf)
		{
			if (splitting && !((ManualBehaviour)UIRoot.instance.uiGame.gridSplit).active)
			{
				splitting = false;
				splitItem = 0;
				suppressRight = true;
			}
			if (splitting && Input.GetMouseButtonDown(0))
			{
				CancelSplit();
				suppressLeft = true;
				suppressRight = true;
				Status(UiText.T("取り出しをキャンセルしました", "Withdrawal cancelled"));
			}
			bool flag = suppressLeft || suppressRight;
			if (suppressLeft)
			{
				VFInput.UseMouseLeft();
				if (!Input.GetMouseButton(0))
				{
					suppressLeft = false;
				}
			}
			if (suppressRight)
			{
				VFInput.UseMouseRight();
				if (!Input.GetMouseButton(1))
				{
					suppressRight = false;
				}
			}
			if (!flag && splitting && (Input.GetMouseButtonUp(1) || Input.GetKeyDown((KeyCode)13) || Input.GetKeyDown((KeyCode)271)))
			{
				EndSplit();
			}
			int num = MouseCell();
			if (!flag && !splitting && num >= 0)
			{
				if (Input.GetMouseButtonDown(1) && items[num] != null)
				{
					BeginSplit(((Proto)items[num]).ID);
					VFInput.UseMouseRight();
				}
				if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0))
				{
					Deposit();
					VFInput.UseMouseLeft();
				}
				if (!splitting && items[num] != null && Time.unscaledTime >= messageUntil)
				{
					statusText.text = UiText.ItemName(items[num]) + ": " + plugin.State.inventory.Count(((Proto)items[num]).ID).ToString("N0") + UiText.T(" 個", "");
					hoverDisplayed = true;
				}
			}
			if (!splitting && (num < 0 || items[num] == null))
			{
				if (hoverDisplayed || Time.unscaledTime >= messageUntil)
				{
					statusText.text = "";
				}
				hoverDisplayed = false;
			}
			RefreshNumbers();
		}
		if ((Object)(object)monitorPanel != (Object)null && ((Object)(object)monitorWindow == (Object)null || !((ManualBehaviour)monitorWindow).active))
		{
			monitorPanel.SetActive(false);
		}
	}

	internal void RaiseHandOverlay()
	{
		if ((Object)(object)panel == (Object)null || !panel.activeSelf)
		{
			return;
		}
		UIHandTip val = UIRoot.instance?.uiGame?.handTip;
		if ((Object)(object)val == (Object)null || (Object)(object)val.rtrans == (Object)null)
		{
			return;
		}
		Transform rtrans = (Transform)(object)val.rtrans;
		Transform transform = panel.transform;
		Transform parent = transform.parent;
		while ((Object)(object)parent != (Object)null)
		{
			Transform val2 = rtrans;
			while ((Object)(object)val2 != (Object)null && (Object)(object)val2.parent != (Object)(object)parent)
			{
				val2 = val2.parent;
			}
			if (!((Object)(object)val2 == (Object)null))
			{
				Transform val3 = transform;
				while ((Object)(object)val3.parent != (Object)(object)parent)
				{
					val3 = val3.parent;
				}
				if (!((Object)(object)val2 == (Object)(object)val3))
				{
					if (val2.GetSiblingIndex() <= val3.GetSiblingIndex())
					{
						val2.SetAsLastSibling();
					}
					break;
				}
			}
			parent = parent.parent;
		}
		((Graphic)val.icon).raycastTarget = false;
		((Graphic)val.iconBlack).raycastTarget = false;
		((Graphic)val.iconbg).raycastTarget = false;
		((Graphic)val.tipText).raycastTarget = false;
		if (GameMain.mainPlayer != null && GameMain.mainPlayer.inhandItemId > 0 && MouseCell() >= 0)
		{
			((Behaviour)val.icon).enabled = true;
			((Behaviour)val.iconBlack).enabled = true;
			((Behaviour)val.iconbg).enabled = true;
		}
	}

	private void Deposit()
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		if (!splitting)
		{
			int inhandItemId = GameMain.mainPlayer.inhandItemId;
			int num = plugin.DepositHand();
			if (num > 0)
			{
				VFAudio.Create("transfer-item", (Transform)null, Vector3.zero, true, 0, -1, -1L);
				Status(UiText.T((UiText.ItemName(((ProtoSet<ItemProto>)(object)LDB.items).Select(inhandItemId)) ?? inhandItemId.ToString()) + " を " + num.ToString("N0") + " 個入庫しました", "Deposited " + num.ToString("N0") + " " + (UiText.ItemName(((ProtoSet<ItemProto>)(object)LDB.items).Select(inhandItemId)) ?? inhandItemId.ToString())));
				RefreshNumbers();
			}
		}
	}

	internal void BeginSplit(int item)
	{
		//IL_007b: 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)
		if (plugin.Ready && !splitting && GameMain.mainPlayer.inhandItemId == 0 && !((ManualBehaviour)UIRoot.instance.uiGame.gridSplit).active)
		{
			int num = (int)Math.Min(2147483647L, plugin.State.inventory.Count(item));
			if (num > 0)
			{
				UIRoot.instance.uiGame.OpenGridSplit(item, num, Vector2.op_Implicit(Input.mousePosition));
				((Component)UIRoot.instance.uiGame.gridSplit).transform.SetAsLastSibling();
				splitItem = item;
				splitting = true;
			}
		}
	}

	internal void EndSplit()
	{
		if (splitting)
		{
			int requested = UIRoot.instance.uiGame.CloseGridSplit();
			int num = splitItem;
			splitting = false;
			splitItem = 0;
			int num2 = plugin.Withdraw(num, requested);
			Status((num2 > 0) ? UiText.T((UiText.ItemName(((ProtoSet<ItemProto>)(object)LDB.items).Select(num)) ?? num.ToString()) + " を " + num2.ToString("N0") + " 個取り