Decompiled source of TripleBoxEmployees v1.0.11

BepInEx/plugins/TripleBoxEmployees.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace TripleBoxEmployees
{
	[BepInPlugin("com.earlj.supermarkettogether.tripleboxemployees", "Triple Box Employees", "1.0.11")]
	public sealed class TripleBoxEmployeesPlugin : BaseUnityPlugin
	{
		private sealed class Cargo
		{
			internal int ProductId;

			internal int Count;

			internal int[] Task;
		}

		private sealed class Batch
		{
			internal readonly Queue<Cargo> Extras = new Queue<Cargo>();

			internal readonly Queue<Cargo> EmptyBoxes = new Queue<Cargo>();

			internal readonly Queue<Cargo> LeftoverBoxes = new Queue<Cargo>();

			internal int VisualCount = 1;

			internal int Role;

			internal bool CleaningUp;

			internal Cargo ActiveCleanup;

			internal Vector3 LastPosition;
		}

		[HarmonyPatch(typeof(NPC_Manager), "EmployeeNPCControl")]
		private static class EmployeeControlPatch
		{
			private static bool Prefix(NPC_Manager __instance, int __0, out int __state)
			{
				NPC_Info val = (NetworkServer.active ? GetEmployee(__instance, __0) : null);
				__state = (((Object)(object)val == (Object)null) ? (-999) : val.state);
				if ((Object)(object)val != (Object)null && !val.employeeDismissed && !__instance.employeesOnRiot && val.taskPriority == 2 && val.state == 5 && Batches.TryGetValue(val, out var value) && !value.CleaningUp)
				{
					FinishRestockingBox(__instance, val, value);
					return false;
				}
				return true;
			}

			private static void Postfix(NPC_Manager __instance, int __0, int __state)
			{
				if (!NetworkServer.active || __state < 0)
				{
					return;
				}
				NPC_Info employee = GetEmployee(__instance, __0);
				if (!((Object)(object)employee == (Object)null) && employee.equippedItem == 1 && !Batches.ContainsKey(employee))
				{
					if (employee.taskPriority == 3 && __state == 1 && (employee.state == 18 || employee.state == 2))
					{
						CollectGroundBoxes(__instance, employee);
					}
					else if (employee.taskPriority == 2 && __state == 2 && employee.state == 3)
					{
						CollectStorageBoxes(__instance, employee);
					}
				}
			}
		}

		[HarmonyPatch(typeof(NPC_Manager), "UnequipBox")]
		private static class UnequipPatch
		{
			private static bool Prefix(NPC_Manager __instance, NPC_Info __0)
			{
				if (!NetworkServer.active || (Object)(object)__0 == (Object)null || !Batches.TryGetValue(__0, out var value))
				{
					return true;
				}
				if (__0.employeeDismissed || __0.taskPriority != value.Role || ((value.Role != 3 || (__0.state != 5 && __0.state != 33)) && (value.Role != 2 || (__0.state != 6 && __0.state != 30))))
				{
					ReleaseExtras(__0, value);
					Batches.Remove(__0);
					return true;
				}
				if (value.Role == 2)
				{
					if (!value.CleaningUp)
					{
						ReleaseExtras(__0, value);
						Batches.Remove(__0);
						return true;
					}
					if (value.EmptyBoxes.Count == 0 && value.LeftoverBoxes.Count == 0)
					{
						Batches.Remove(__0);
						return true;
					}
					SendVisual(__0, 6, (value.ActiveCleanup == null) ? __0.boxProductID : value.ActiveCleanup.ProductId);
					value.VisualCount--;
					StartNextCleanup(__0, value);
					return false;
				}
				if (value.Extras.Count == 0)
				{
					Batches.Remove(__0);
					return true;
				}
				Cargo cargo = value.Extras.Dequeue();
				SendVisual(__0, 6, __0.boxProductID);
				value.VisualCount--;
				__0.NetworkboxProductID = cargo.ProductId;
				__0.NetworkboxNumberOfProducts = cargo.Count;
				__0.state = 18;
				return false;
			}
		}

		[HarmonyPatch(typeof(NPC_Manager), "DropBoxOnGround")]
		private static class DropPatch
		{
			private static void Postfix(NPC_Info __0)
			{
				if (NetworkServer.active && (Object)(object)__0 != (Object)null && Batches.TryGetValue(__0, out var value))
				{
					ReleaseExtras(__0, value);
					Batches.Remove(__0);
				}
			}
		}

		[HarmonyPatch(typeof(SaveBehaviour), "SavePersistentValues")]
		private static class SavePatch
		{
			private static void Prefix()
			{
				if (NetworkServer.active)
				{
					ReleaseAll();
				}
			}
		}

		[HarmonyPatch(typeof(NPC_Info), "UserCode_RpcEquipNPCItem__Int32__Int32")]
		private static class BoxVisualPatch
		{
			private static bool Prefix(NPC_Info __instance, int __0, int __1)
			{
				switch (__0)
				{
				case 6:
					ConsumeVisual(__instance, __1);
					return false;
				case 7:
					AppendExtraVisual(__instance, __1);
					return false;
				default:
				{
					if (__instance.taskPriority != 2 && __instance.taskPriority != 3)
					{
						return true;
					}
					Transform val = ((Component)__instance).transform.Find("EquippedItem");
					if ((Object)(object)val == (Object)null)
					{
						return true;
					}
					if (__0 == 0)
					{
						while (val.childCount > 0)
						{
							Transform child = val.GetChild(0);
							child.SetParent((Transform)null, true);
							Object.Destroy((Object)(object)((Component)child).gameObject);
						}
						__instance.instantiatedOBJ = null;
					}
					return true;
				}
				}
			}

			private static void AppendExtraVisual(NPC_Info employee, int productId)
			{
				//IL_0057: 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)
				Transform val = ((Component)employee).transform.Find("EquippedItem");
				if (!((Object)(object)val == (Object)null) && val.childCount != 0 && !((Object)(object)employee.dummyBoxPrefab == (Object)null))
				{
					Transform child = val.GetChild(0);
					Transform transform = Object.Instantiate<GameObject>(employee.dummyBoxPrefab, child).transform;
					((Object)transform).name = "TripleBoxExtra";
					transform.localRotation = Quaternion.identity;
					transform.localScale = Vector3.one;
					Transform val2 = transform.Find("Canvas");
					if ((Object)(object)val2 != (Object)null)
					{
						((Component)val2).gameObject.SetActive(false);
					}
					Transform val3 = transform.Find("BoxMesh");
					if ((Object)(object)val3 != (Object)null)
					{
						((Component)val3).gameObject.SetActive(true);
					}
					if ((Object)(object)ProductListing.Instance != (Object)null)
					{
						ProductListing.Instance.SetBoxColor(((Component)transform).gameObject, productId);
					}
					((Component)transform).gameObject.AddComponent<BoxVisualProduct>().ProductId = productId;
					LayoutExtras(child);
				}
			}

			private static void Postfix(NPC_Info __instance, int __0, int __1)
			{
				//IL_0059: Unknown result type (might be due to invalid IL or missing references)
				//IL_0064: Unknown result type (might be due to invalid IL or missing references)
				//IL_006e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0073: Unknown result type (might be due to invalid IL or missing references)
				if (__0 == 1 && (__instance.taskPriority == 2 || __instance.taskPriority == 3) && !((Object)(object)__instance.instantiatedOBJ == (Object)null))
				{
					BoxVisualProduct boxVisualProduct = __instance.instantiatedOBJ.GetComponent<BoxVisualProduct>();
					if ((Object)(object)boxVisualProduct == (Object)null)
					{
						boxVisualProduct = __instance.instantiatedOBJ.AddComponent<BoxVisualProduct>();
					}
					boxVisualProduct.ProductId = __1;
					Transform transform = __instance.instantiatedOBJ.transform;
					transform.position += ((Component)__instance).transform.forward * 0.22f;
				}
			}

			private static void ConsumeVisual(NPC_Info employee, int productId)
			{
				Transform val = ((Component)employee).transform.Find("EquippedItem");
				if ((Object)(object)val == (Object)null || val.childCount == 0)
				{
					return;
				}
				Transform child = val.GetChild(0);
				List<Transform> list = new List<Transform>();
				Transform val2 = null;
				for (int i = 0; i < child.childCount; i++)
				{
					Transform child2 = child.GetChild(i);
					if (!(((Object)child2).name != "TripleBoxExtra"))
					{
						list.Add(child2);
						BoxVisualProduct component = ((Component)child2).GetComponent<BoxVisualProduct>();
						if ((Object)(object)val2 == (Object)null && (Object)(object)component != (Object)null && component.ProductId == productId)
						{
							val2 = child2;
						}
					}
				}
				if ((Object)(object)val2 != (Object)null)
				{
					val2.SetParent((Transform)null, true);
					Object.Destroy((Object)(object)((Component)val2).gameObject);
				}
				else if (list.Count > 0)
				{
					Transform obj = list[0];
					BoxVisualProduct component2 = ((Component)obj).GetComponent<BoxVisualProduct>();
					if ((Object)(object)component2 != (Object)null)
					{
						BoxVisualProduct boxVisualProduct = ((Component)child).GetComponent<BoxVisualProduct>();
						if ((Object)(object)boxVisualProduct == (Object)null)
						{
							boxVisualProduct = ((Component)child).gameObject.AddComponent<BoxVisualProduct>();
						}
						boxVisualProduct.ProductId = component2.ProductId;
						if ((Object)(object)ProductListing.Instance != (Object)null)
						{
							ProductListing.Instance.SetBoxColor(((Component)child).gameObject, component2.ProductId);
						}
					}
					obj.SetParent((Transform)null, true);
					Object.Destroy((Object)(object)((Component)obj).gameObject);
				}
				LayoutExtras(child);
				employee.instantiatedOBJ = ((Component)child).gameObject;
			}

			private static void LayoutExtras(Transform baseBox)
			{
				//IL_0025: Unknown result type (might be due to invalid IL or missing references)
				//IL_0030: 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: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: 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_005e: Unknown result type (might be due to invalid IL or missing references)
				int num = 0;
				for (int i = 0; i < baseBox.childCount; i++)
				{
					Transform child = baseBox.GetChild(i);
					if (!(((Object)child).name != "TripleBoxExtra"))
					{
						num++;
						child.localPosition = Vector3.zero;
						child.localRotation = Quaternion.identity;
						child.localScale = Vector3.one;
						child.position = baseBox.position + Vector3.up * (0.62f * (float)num);
					}
				}
			}
		}

		private const int Restocker = 2;

		private const int Storage = 3;

		private const float PickupRadius = 25f;

		private const float StackStep = 0.62f;

		private static readonly Dictionary<NPC_Info, Batch> Batches = new Dictionary<NPC_Info, Batch>();

		private static readonly MethodInfo RpcEquip = typeof(NPC_Info).GetMethod("RpcEquipNPCItem", BindingFlags.Instance | BindingFlags.NonPublic);

		private Harmony harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			harmony = new Harmony("com.earlj.supermarkettogether.tripleboxemployees");
			harmony.PatchAll(typeof(TripleBoxEmployeesPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Triple-box storage and restocking enabled on the host.");
		}

		private void Update()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkServer.active)
			{
				return;
			}
			foreach (KeyValuePair<NPC_Info, Batch> item in new List<KeyValuePair<NPC_Info, Batch>>(Batches))
			{
				if ((Object)(object)item.Key != (Object)null)
				{
					item.Value.LastPosition = ((Component)item.Key).transform.position;
				}
				if ((Object)(object)item.Key == (Object)null || item.Key.employeeDismissed || item.Key.taskPriority != item.Value.Role)
				{
					ReleaseExtras(item.Key, item.Value);
					Batches.Remove(item.Key);
				}
			}
		}

		private void OnDestroy()
		{
			if (NetworkServer.active)
			{
				ReleaseAll();
			}
			Batches.Clear();
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private static NPC_Info GetEmployee(NPC_Manager manager, int index)
		{
			if ((Object)(object)manager == (Object)null || (Object)(object)manager.employeeParentOBJ == (Object)null)
			{
				return null;
			}
			Transform transform = manager.employeeParentOBJ.transform;
			if (index < 0 || index >= transform.childCount)
			{
				return null;
			}
			return ((Component)transform.GetChild(index)).GetComponent<NPC_Info>();
		}

		private static void CollectGroundBoxes(NPC_Manager manager, NPC_Info employee)
		{
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)manager.boxesOBJ == (Object)null || employee.boxNumberOfProducts <= 0)
			{
				return;
			}
			Transform transform = manager.boxesOBJ.transform;
			List<GameObject> list = new List<GameObject>();
			for (int i = 0; i < transform.childCount; i++)
			{
				GameObject gameObject = ((Component)transform.GetChild(i)).gameObject;
				if (!((Object)(object)gameObject == (Object)null) && !((Object)(object)gameObject == (Object)(object)employee.randomBox) && !IsTargetedByAnotherEmployee(manager, employee, gameObject))
				{
					BoxData component = gameObject.GetComponent<BoxData>();
					if (!((Object)(object)component == (Object)null) && component.productID >= 0 && component.numberOfProducts > 0 && Vector3.Distance(((Component)employee).transform.position, gameObject.transform.position) <= 25f)
					{
						list.Add(gameObject);
					}
				}
			}
			list.Sort((GameObject a, GameObject b) => Vector3.SqrMagnitude(a.transform.position - ((Component)employee).transform.position).CompareTo(Vector3.SqrMagnitude(b.transform.position - ((Component)employee).transform.position)));
			Batch batch = new Batch
			{
				Role = 3,
				LastPosition = ((Component)employee).transform.position
			};
			NetworkSpawner component2 = ((Component)GameData.Instance).GetComponent<NetworkSpawner>();
			foreach (GameObject item in list)
			{
				if (batch.Extras.Count == 2)
				{
					break;
				}
				if (!((Object)(object)item == (Object)null))
				{
					BoxData component3 = item.GetComponent<BoxData>();
					if (!((Object)(object)component3 == (Object)null))
					{
						Cargo cargo = new Cargo
						{
							ProductId = component3.productID,
							Count = component3.numberOfProducts
						};
						item.transform.SetParent((Transform)null, true);
						component2.EmployeeDestroyBox(item);
						batch.Extras.Enqueue(cargo);
						SendVisual(employee, 7, cargo.ProductId);
						batch.VisualCount++;
					}
				}
			}
			if (batch.Extras.Count > 0)
			{
				Batches[employee] = batch;
			}
		}

		private static bool IsTargetedByAnotherEmployee(NPC_Manager manager, NPC_Info current, GameObject box)
		{
			Transform transform = manager.employeeParentOBJ.transform;
			for (int i = 0; i < transform.childCount; i++)
			{
				NPC_Info component = ((Component)transform.GetChild(i)).GetComponent<NPC_Info>();
				if ((Object)(object)component != (Object)null && (Object)(object)component != (Object)(object)current && component.taskPriority == 3 && component.state == 1 && (Object)(object)component.randomBox == (Object)(object)box)
				{
					return true;
				}
			}
			return false;
		}

		private static void CollectStorageBoxes(NPC_Manager manager, NPC_Info employee)
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)manager.storageOBJ == (Object)null || employee.productAvailableArray == null || employee.productAvailableArray.Length < 5 || employee.boxProductID < 0 || employee.boxNumberOfProducts <= 0)
			{
				return;
			}
			int num = employee.productAvailableArray[2];
			Transform transform = manager.storageOBJ.transform;
			if (num < 0 || num >= transform.childCount)
			{
				return;
			}
			Data_Container component = ((Component)transform.GetChild(num)).GetComponent<Data_Container>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.standSpot == (Object)null)
			{
				return;
			}
			Batch batch = new Batch
			{
				Role = 2,
				LastPosition = ((Component)employee).transform.position
			};
			List<string>[] array = new List<string>[4] { manager.lowProductCountList, manager.mediumProductCountList, manager.highProductCountList, manager.veryHighProductCountList };
			foreach (List<string> list in array)
			{
				if (list == null)
				{
					continue;
				}
				foreach (string item in list)
				{
					if (batch.Extras.Count == 2)
					{
						break;
					}
					if (!TryParseTask(item, out var task))
					{
						continue;
					}
					int num2 = task[2];
					int num3 = task[3];
					int num4 = task[4];
					if (num2 >= 0 && num2 < transform.childCount && num3 >= 0 && (num3 & 1) == 0 && num4 >= 0 && (num2 != num || num3 != employee.productAvailableArray[3]))
					{
						Data_Container component2 = ((Component)transform.GetChild(num2)).GetComponent<Data_Container>();
						if (!((Object)(object)component2 == (Object)null) && component2.productInfoArray != null && !((Object)(object)component2.standSpot == (Object)null) && num3 + 1 < component2.productInfoArray.Length && component2.productInfoArray[num3] == num4 && component2.productInfoArray[num3 + 1] > 0 && !(Vector3.Distance(component.standSpot.position, component2.standSpot.position) > 25f) && ShelfNeedsProduct(manager, task))
						{
							Cargo cargo = new Cargo
							{
								ProductId = num4,
								Count = component2.productInfoArray[num3 + 1],
								Task = task
							};
							int num5 = (((Object)(object)((Component)component2).transform.Find("CanvasSigns") != (Object)null) ? num4 : (-1));
							component2.EmployeeUpdateArrayValuesStorage(num3, num5, -1);
							batch.Extras.Enqueue(cargo);
							SendVisual(employee, 7, cargo.ProductId);
							batch.VisualCount++;
						}
					}
				}
				if (batch.Extras.Count == 2)
				{
					break;
				}
			}
			if (batch.Extras.Count > 0)
			{
				Batches[employee] = batch;
			}
		}

		private static bool TryParseTask(string entry, out int[] task)
		{
			task = null;
			if (string.IsNullOrEmpty(entry))
			{
				return false;
			}
			string[] array = entry.Split('|');
			if (array.Length < 6)
			{
				return false;
			}
			int[] array2 = new int[6];
			for (int i = 0; i < array2.Length; i++)
			{
				if (!int.TryParse(array[i], out array2[i]))
				{
					return false;
				}
			}
			task = array2;
			return true;
		}

		private static bool ShelfNeedsProduct(NPC_Manager manager, int[] task)
		{
			Transform val = (((Object)(object)manager.shelvesOBJ == (Object)null) ? null : manager.shelvesOBJ.transform);
			if ((Object)(object)val == (Object)null || task[0] < 0 || task[0] >= val.childCount || task[1] < 0)
			{
				return false;
			}
			Data_Container component = ((Component)val.GetChild(task[0])).GetComponent<Data_Container>();
			if ((Object)(object)component == (Object)null || component.productInfoArray == null || task[1] + 1 >= component.productInfoArray.Length || component.productInfoArray[task[1]] != task[4])
			{
				return false;
			}
			return component.productInfoArray[task[1] + 1] < manager.GetMaxProductsPerRow(task[0], task[4]);
		}

		private static void ClearVisuals(NPC_Info employee, int count)
		{
			if ((Object)(object)employee != (Object)null && count > 0)
			{
				SendVisual(employee, 0, 0);
			}
		}

		private static void SendVisual(NPC_Info employee, int item, int productId)
		{
			if (RpcEquip != null)
			{
				RpcEquip.Invoke(employee, new object[2] { item, productId });
			}
		}

		private static void ReleaseExtras(NPC_Info employee, Batch batch)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_0038: 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_0051: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)GameData.Instance != (Object)null)
			{
				ManagerBlackboard component = ((Component)GameData.Instance).GetComponent<ManagerBlackboard>();
				Vector3 position = (((Object)(object)employee == (Object)null) ? batch.LastPosition : ((Component)employee).transform.position);
				ReleaseQueue(component, position, batch.Extras);
				ReleaseQueue(component, position, batch.EmptyBoxes);
				ReleaseQueue(component, position, batch.LeftoverBoxes);
				if ((Object)(object)employee != (Object)null && batch.VisualCount > 1)
				{
					ClearVisuals(employee, batch.VisualCount);
					if (employee.equippedItem == 1)
					{
						SendVisual(employee, 1, employee.boxProductID);
					}
				}
			}
			else
			{
				batch.Extras.Clear();
				batch.EmptyBoxes.Clear();
				batch.LeftoverBoxes.Clear();
			}
			batch.VisualCount = 1;
		}

		private static void ReleaseQueue(ManagerBlackboard board, Vector3 position, Queue<Cargo> queue)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			while (queue.Count > 0)
			{
				Cargo cargo = queue.Dequeue();
				if ((Object)(object)board != (Object)null)
				{
					board.SpawnBoxFromEmployee(position + Vector3.right * (0.5f + (float)queue.Count * 0.5f), cargo.ProductId, cargo.Count);
				}
			}
		}

		private static bool TryStartNextRestock(NPC_Manager manager, NPC_Info employee, Batch batch)
		{
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			Transform val = (((Object)(object)manager.shelvesOBJ == (Object)null) ? null : manager.shelvesOBJ.transform);
			NavMeshAgent component = ((Component)employee).GetComponent<NavMeshAgent>();
			while (batch.Extras.Count > 0)
			{
				Cargo cargo = batch.Extras.Dequeue();
				int[] task = cargo.Task;
				if ((Object)(object)val == (Object)null || (Object)(object)component == (Object)null || task == null || task.Length < 5 || task[0] < 0 || task[0] >= val.childCount)
				{
					batch.LeftoverBoxes.Enqueue(cargo);
					continue;
				}
				Data_Container component2 = ((Component)val.GetChild(task[0])).GetComponent<Data_Container>();
				if ((Object)(object)component2 == (Object)null || !ShelfNeedsProduct(manager, task))
				{
					batch.LeftoverBoxes.Enqueue(cargo);
					continue;
				}
				employee.NetworkboxProductID = cargo.ProductId;
				employee.NetworkboxNumberOfProducts = cargo.Count;
				employee.productAvailableArray = task;
				component.destination = (((Object)(object)component2.standSpot == (Object)null) ? ((Component)component2).transform.position : component2.standSpot.position);
				employee.state = 3;
				return true;
			}
			return false;
		}

		private static void StartNextCleanup(NPC_Info employee, Batch batch)
		{
			Cargo cargo = null;
			if (batch.EmptyBoxes.Count > 0)
			{
				cargo = batch.EmptyBoxes.Dequeue();
			}
			else if (batch.LeftoverBoxes.Count > 0)
			{
				cargo = batch.LeftoverBoxes.Dequeue();
			}
			if (cargo != null)
			{
				batch.ActiveCleanup = cargo;
				employee.NetworkboxProductID = cargo.ProductId;
				employee.NetworkboxNumberOfProducts = cargo.Count;
				employee.state = 5;
			}
		}

		private static void FinishRestockingBox(NPC_Manager manager, NPC_Info employee, Batch batch)
		{
			Cargo cargo = new Cargo
			{
				ProductId = employee.boxProductID,
				Count = employee.boxNumberOfProducts
			};
			if (cargo.Count <= 0)
			{
				batch.EmptyBoxes.Enqueue(cargo);
			}
			else
			{
				batch.LeftoverBoxes.Enqueue(cargo);
			}
			if (!TryStartNextRestock(manager, employee, batch))
			{
				batch.CleaningUp = true;
				StartNextCleanup(employee, batch);
			}
		}

		private static void ReleaseAll()
		{
			foreach (KeyValuePair<NPC_Info, Batch> batch in Batches)
			{
				ReleaseExtras(batch.Key, batch.Value);
			}
			Batches.Clear();
		}
	}
	public sealed class BoxVisualProduct : MonoBehaviour
	{
		public int ProductId;
	}
}