Decompiled source of ModerWarehouse v2.0.3

BepInEx/plugins/ModerWarehouse/ModerWarehouse.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using ServersideQoL;
using ServersideQoL.Processors;
using ServersideQoL.Utilities;
using UnityEngine;
using YamlDotNet.Serialization;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Cris Haani")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2026 Cris Haani")]
[assembly: AssemblyDescription("Server-only automatic warehouse for Valheim.")]
[assembly: AssemblyFileVersion("2.0.3.0")]
[assembly: AssemblyInformationalVersion("2.0.3+4net-20261021")]
[assembly: AssemblyProduct("Moder Warehouse")]
[assembly: AssemblyTitle("ModerWarehouse")]
[assembly: AssemblyVersion("2.0.3.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 ModerWarehouse
{
	internal static class GlobalScanPolicy
	{
		internal const double TopologySafetyMinutes = 30.0;

		internal const double WorldStockCooldownSeconds = 10.0;

		internal static bool ShouldValidateTopology(bool unknown, bool signatureChanged, DateTime now, DateTime lastValidation)
		{
			if (!(unknown || signatureChanged))
			{
				return (now - lastValidation).TotalMinutes >= 30.0;
			}
			return true;
		}

		internal static bool ShouldRefreshWorldStock(DateTime now, DateTime lastRefresh)
		{
			return (now - lastRefresh).TotalSeconds >= 10.0;
		}
	}
	internal static class RequestDisplayPolicy
	{
		internal const float MinimumResultSeconds = 2f;

		internal const float MaximumResultSeconds = 8f;

		internal static float ClampResultSeconds(float configuredSeconds)
		{
			if (float.IsNaN(configuredSeconds) || float.IsInfinity(configuredSeconds))
			{
				return 8f;
			}
			return Math.Max(2f, Math.Min(8f, configuredSeconds));
		}
	}
	internal static class RequestPolicy
	{
		internal static bool TryStripKeyword(string text, IEnumerable<string> keywords, out string rest)
		{
			rest = string.Empty;
			string text2 = (text ?? string.Empty).TrimStart(Array.Empty<char>());
			if (text2.Length == 0 || keywords == null)
			{
				return false;
			}
			foreach (string keyword in keywords)
			{
				string text3 = (keyword ?? string.Empty).Trim();
				if (text3.Length != 0 && text2.Length > text3.Length && text2.StartsWith(text3, StringComparison.OrdinalIgnoreCase) && char.IsWhiteSpace(text2[text3.Length]))
				{
					rest = text2.Substring(text3.Length).Trim();
					return true;
				}
			}
			return false;
		}

		internal static int MatchRank(bool localizedName, bool prefabOrToken)
		{
			if (!localizedName)
			{
				if (!prefabOrToken)
				{
					return 2;
				}
				return 1;
			}
			return 0;
		}

		internal static string ChooseToken(IEnumerable<(string Token, int Rank, int Amount)> candidates)
		{
			string text = null;
			int num = int.MaxValue;
			int num2 = -1;
			foreach (var (text2, num3, num4) in candidates)
			{
				if (!string.IsNullOrEmpty(text2) && (num3 < num || (num3 == num && num4 > num2) || (num3 == num && num4 == num2 && string.CompareOrdinal(text2, text) < 0)))
				{
					text = text2;
					num = num3;
					num2 = num4;
				}
			}
			return text;
		}
	}
	internal static class SleepDisplayPolicy
	{
		internal sealed class Layout
		{
			internal string Text { get; }

			internal int LabelLength { get; }

			internal Layout(string text, int labelLength)
			{
				Text = text;
				LabelLength = labelLength;
			}
		}

		internal static Layout Create(string localizedLabel, int cellCount)
		{
			if (cellCount <= 0)
			{
				return new Layout(string.Empty, 0);
			}
			string text = (localizedLabel ?? string.Empty).Trim().TrimEnd('Z', 'z');
			return new Layout(labelLength: Math.Min(text.Length, cellCount), text: (text + new string('Z', cellCount)).Substring(0, cellCount));
		}
	}
	internal sealed class SpatialLookup<T> where T : class
	{
		private sealed class Entry
		{
			internal T Item;

			internal float X;

			internal float Y;

			internal float Z;

			internal int Order;
		}

		private const float CellSize = 16f;

		private readonly Dictionary<long, List<Entry>> cells = new Dictionary<long, List<Entry>>();

		internal SpatialLookup(IEnumerable<T> items, Func<T, float> x, Func<T, float> y, Func<T, float> z)
		{
			int num = 0;
			foreach (T item in items)
			{
				Entry entry = new Entry
				{
					Item = item,
					X = x(item),
					Y = y(item),
					Z = z(item),
					Order = num++
				};
				long key = Pack(Cell(entry.X), Cell(entry.Y), Cell(entry.Z));
				if (!cells.TryGetValue(key, out var value))
				{
					value = (cells[key] = new List<Entry>());
				}
				value.Add(entry);
			}
		}

		private static int Cell(float value)
		{
			return (int)Math.Floor(value / 16f);
		}

		private static long Pack(int x, int y, int z)
		{
			return ((long)(x & 0x1FFFFF) << 42) | ((long)(y & 0x1FFFFF) << 21) | (z & 0x1FFFFF);
		}

		internal T Nearest(float x, float y, float z, float radiusSquared, Func<T, bool> include = null, bool preferLastOnTie = false)
		{
			if (radiusSquared < 0f || float.IsNaN(radiusSquared))
			{
				return null;
			}
			if (radiusSquared > 4096f)
			{
				Entry entry = null;
				float num = radiusSquared;
				foreach (List<Entry> value2 in cells.Values)
				{
					foreach (Entry item in value2)
					{
						float num2 = item.X - x;
						float num3 = item.Y - y;
						float num4 = item.Z - z;
						float num5 = num2 * num2 + num3 * num3 + num4 * num4;
						if (!(num5 > num) && (num5 != num || entry == null || !(preferLastOnTie ? (item.Order < entry.Order) : (item.Order > entry.Order))) && (include == null || include(item.Item)))
						{
							entry = item;
							num = num5;
						}
					}
				}
				if (entry == null)
				{
					return null;
				}
				return entry.Item;
			}
			float num6 = (float)Math.Sqrt(radiusSquared);
			Entry entry2 = null;
			float num7 = radiusSquared;
			for (int i = Cell(x - num6); i <= Cell(x + num6); i++)
			{
				for (int j = Cell(y - num6); j <= Cell(y + num6); j++)
				{
					for (int k = Cell(z - num6); k <= Cell(z + num6); k++)
					{
						if (!cells.TryGetValue(Pack(i, j, k), out var value))
						{
							continue;
						}
						foreach (Entry item2 in value)
						{
							float num8 = item2.X - x;
							float num9 = item2.Y - y;
							float num10 = item2.Z - z;
							float num11 = num8 * num8 + num9 * num9 + num10 * num10;
							if (!(num11 > num7) && (num11 != num7 || entry2 == null || !(preferLastOnTie ? (item2.Order < entry2.Order) : (item2.Order > entry2.Order))) && (include == null || include(item2.Item)))
							{
								entry2 = item2;
								num7 = num11;
							}
						}
					}
				}
			}
			if (entry2 == null)
			{
				return null;
			}
			return entry2.Item;
		}
	}
	internal static class TranslationGate
	{
		internal sealed class Report
		{
			internal readonly List<string> Missing = new List<string>();

			internal readonly List<string> Unknown = new List<string>();

			internal readonly List<string> InvalidPlaceholders = new List<string>();

			internal bool IsValid => InvalidPlaceholders.Count == 0;
		}

		private static readonly Regex LanguageCode = new Regex("^[a-z]{2,3}(?:-[a-z0-9]{2,8})*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

		private static readonly Regex Placeholder = new Regex("\\{([0-9]+)(?:,[^{}]+)?(?::[^{}]+)?\\}", RegexOptions.CultureInvariant);

		internal static bool IsValidLanguageCode(string value)
		{
			string input = (value ?? string.Empty).Trim().Replace('_', '-');
			return LanguageCode.IsMatch(input);
		}

		internal static Report Validate(IDictionary<string, string> reference, IDictionary<string, string> supplied)
		{
			Report report = new Report();
			if (reference == null || supplied == null)
			{
				return report;
			}
			foreach (KeyValuePair<string, string> item in reference)
			{
				if (!supplied.TryGetValue(item.Key, out var value) || string.IsNullOrWhiteSpace(value))
				{
					report.Missing.Add(item.Key);
				}
				else if (!PlaceholderSet(item.Value).SequenceEqual(PlaceholderSet(value)))
				{
					report.InvalidPlaceholders.Add(item.Key);
				}
			}
			foreach (string key in supplied.Keys)
			{
				if (!reference.ContainsKey(key))
				{
					report.Unknown.Add(key);
				}
			}
			report.Missing.Sort(StringComparer.OrdinalIgnoreCase);
			report.Unknown.Sort(StringComparer.OrdinalIgnoreCase);
			report.InvalidPlaceholders.Sort(StringComparer.OrdinalIgnoreCase);
			return report;
		}

		private static string[] PlaceholderSet(string value)
		{
			return (from Match match in Placeholder.Matches(value ?? string.Empty)
				select match.Groups[1].Value).OrderBy<string, string>((string index) => index, StringComparer.Ordinal).ToArray();
		}
	}
	internal static class WarehouseCategories
	{
		internal enum Category
		{
			None,
			Ore,
			Bars,
			Food,
			Meads,
			Ammo,
			Trophies,
			Weapons,
			Armor,
			Tools,
			Fish,
			Misc
		}

		private static readonly Dictionary<string, Category> Keywords = new Dictionary<string, Category>(StringComparer.Ordinal)
		{
			["erz"] = Category.Ore,
			["erze"] = Category.Ore,
			["ore"] = Category.Ore,
			["ores"] = Category.Ore,
			["barren"] = Category.Bars,
			["bars"] = Category.Bars,
			["ingots"] = Category.Bars,
			["essen"] = Category.Food,
			["nahrung"] = Category.Food,
			["food"] = Category.Food,
			["met"] = Category.Meads,
			["mead"] = Category.Meads,
			["meads"] = Category.Meads,
			["tränke"] = Category.Meads,
			["traenke"] = Category.Meads,
			["potions"] = Category.Meads,
			["munition"] = Category.Ammo,
			["ammo"] = Category.Ammo,
			["trophäen"] = Category.Trophies,
			["trophaeen"] = Category.Trophies,
			["trophies"] = Category.Trophies,
			["trophy"] = Category.Trophies,
			["waffen"] = Category.Weapons,
			["weapons"] = Category.Weapons,
			["rüstung"] = Category.Armor,
			["ruestung"] = Category.Armor,
			["armor"] = Category.Armor,
			["armour"] = Category.Armor,
			["werkzeug"] = Category.Tools,
			["werkzeuge"] = Category.Tools,
			["tools"] = Category.Tools,
			["fisch"] = Category.Fish,
			["fische"] = Category.Fish,
			["fish"] = Category.Fish,
			["sonstiges"] = Category.Misc,
			["diverses"] = Category.Misc,
			["misc"] = Category.Misc,
			["overflow"] = Category.Misc
		};

		private static readonly HashSet<string> BarTokens = new HashSet<string>(StringComparer.Ordinal) { "copper", "tin", "bronze", "iron", "silver", "blackmetal", "flametal" };

		internal static bool TryParse(string normalizedLabel, out Category category)
		{
			return Keywords.TryGetValue(normalizedLabel ?? string.Empty, out category);
		}

		internal static bool Contains(Category category, string token, string itemType, float food, float foodStamina)
		{
			string text = itemType ?? string.Empty;
			string text2 = BaseName(token);
			switch (category)
			{
			case Category.Ore:
				if (text == "Material")
				{
					if (!text2.EndsWith("ore", StringComparison.Ordinal) || text2.EndsWith("core", StringComparison.Ordinal))
					{
						return text2.EndsWith("scrap", StringComparison.Ordinal);
					}
					return true;
				}
				return false;
			case Category.Bars:
				if (text == "Material")
				{
					return BarTokens.Contains(text2);
				}
				return false;
			case Category.Food:
				if (text == "Consumable")
				{
					if (!(food > 0f))
					{
						return foodStamina > 0f;
					}
					return true;
				}
				return false;
			case Category.Meads:
				if (text == "Consumable" && food <= 0f)
				{
					return foodStamina <= 0f;
				}
				return false;
			case Category.Ammo:
				if (!(text == "Ammo"))
				{
					return text == "AmmoNonEquipable";
				}
				return true;
			case Category.Trophies:
				return text == "Trophy";
			case Category.Weapons:
				switch (text)
				{
				default:
					return text == "Attach_Atgeir";
				case "OneHandedWeapon":
				case "TwoHandedWeapon":
				case "TwoHandedWeaponLeft":
				case "Bow":
					return true;
				}
			case Category.Armor:
				switch (text)
				{
				default:
					return text == "Trinket";
				case "Helmet":
				case "Chest":
				case "Legs":
				case "Hands":
				case "Shoulder":
				case "Utility":
				case "Shield":
					return true;
				}
			case Category.Tools:
				if (!(text == "Tool"))
				{
					return text == "Torch";
				}
				return true;
			case Category.Fish:
				return text == "Fish";
			default:
				return false;
			}
		}

		private static string BaseName(string token)
		{
			string text = (token ?? string.Empty).ToLowerInvariant();
			if (text.StartsWith("$item_", StringComparison.Ordinal))
			{
				text = text.Substring(6);
			}
			if (text.EndsWith("_old", StringComparison.Ordinal))
			{
				text = text.Substring(0, text.Length - 4);
			}
			return text;
		}
	}
	public static class WarehouseContainerPolicy
	{
		public static bool IsPrivatePrefab(string prefabName)
		{
			string text = (prefabName ?? string.Empty).ToLowerInvariant();
			if (!text.Contains("chest_private"))
			{
				return text.Contains("personal");
			}
			return true;
		}

		public static bool CanManage(bool endpoint, bool ignored, bool isPrivate, bool inUse, bool hasStorageSign)
		{
			return !endpoint && !ignored && !isPrivate && !inUse && hasStorageSign;
		}

		public static bool IsExplicitLabelReset(string plainText)
		{
			return string.Equals((plainText ?? string.Empty).Trim(), "...", StringComparison.Ordinal);
		}

		public static bool TryParseNumberedLabel(string plainText, out string itemLabel, out int priority)
		{
			string text = (plainText ?? string.Empty).Trim();
			int num = text.Length;
			while (num > 0 && char.IsDigit(text[num - 1]))
			{
				num--;
			}
			itemLabel = text.Substring(0, num).TrimEnd(Array.Empty<char>());
			priority = 0;
			if (num == text.Length || itemLabel.Length == 0 || !int.TryParse(text.Substring(num), out priority))
			{
				return false;
			}
			if (priority >= 1)
			{
				return priority <= 99;
			}
			return false;
		}

		public static int EffectiveFillPriority(int storedPriority)
		{
			if (storedPriority < 1 || storedPriority > 99)
			{
				return 1;
			}
			return storedPriority;
		}

		public static int TransferAmount(int sourceStack, int wanted, int maxStack, int matchingStackSpace, int emptySlots)
		{
			int num = Math.Max(1, maxStack);
			int val = Math.Max(0, matchingStackSpace) + Math.Max(0, emptySlots) * num;
			return Math.Min(Math.Min(Math.Max(0, sourceStack), Math.Max(0, wanted)), val);
		}
	}
	public static class WarehouseDistancePolicy
	{
		public static bool IsTooClose(float distance, float minimum)
		{
			return distance < minimum;
		}
	}
	public static class WarehouseDuplicatePolicy
	{
		public static int Compare(bool leftCanonical, string leftStableId, bool rightCanonical, string rightStableId)
		{
			int num = rightCanonical.CompareTo(leftCanonical);
			if (num != 0)
			{
				return num;
			}
			return StringComparer.Ordinal.Compare(leftStableId ?? string.Empty, rightStableId ?? string.Empty);
		}
	}
	internal static class WarehouseLocalization
	{
		public sealed class TranslationFile
		{
			public string language { get; set; }

			public string language_name { get; set; }

			public string translator { get; set; }

			public Dictionary<string, string> strings { get; set; }
		}

		private static readonly Dictionary<string, Dictionary<string, string>> Languages = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);

		private static readonly Dictionary<string, string> Aliases = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		private static Action<string> _log;

		public static void Initialize(Action<string> log)
		{
			_log = log;
			Languages.Clear();
			Aliases.Clear();
			AddBuiltIn("en", English());
			AddBuiltIn("de", German());
			Aliases["english"] = "en";
			Aliases["eng"] = "en";
			Aliases["german"] = "de";
			Aliases["deutsch"] = "de";
			Aliases["ger"] = "de";
			string text = Path.Combine(Paths.ConfigPath, "ModerWarehouse", "localization");
			Directory.CreateDirectory(text);
			WriteTemplate(Path.Combine(text, "TranslationTemplate.json"));
			foreach (string item in Directory.GetFiles(text, "*.json").OrderBy<string, string>((string x) => x, StringComparer.OrdinalIgnoreCase))
			{
				if (!Path.GetFileName(item).Equals("TranslationTemplate.json", StringComparison.OrdinalIgnoreCase))
				{
					LoadFile(item);
				}
			}
			_log?.Invoke($"Moder Warehouse localization: {Languages.Count} language(s) available: " + string.Join(", ", Languages.Keys.OrderBy((string x) => x)) + ".");
		}

		public static string Resolve(string requested)
		{
			string text = (requested ?? string.Empty).Trim();
			if (Aliases.TryGetValue(text, out var value))
			{
				text = value;
			}
			text = text.Replace('_', '-').ToLowerInvariant();
			if (Languages.ContainsKey(text))
			{
				return text;
			}
			string text2 = text.Split(new char[1] { '-' })[0];
			if (Languages.ContainsKey(text2))
			{
				return text2;
			}
			return "en";
		}

		public static string Text(string language, string key)
		{
			language = Resolve(language);
			if (Languages.TryGetValue(language, out var value) && value.TryGetValue(key, out var value2) && !string.IsNullOrWhiteSpace(value2))
			{
				return value2;
			}
			if (Languages["en"].TryGetValue(key, out value2))
			{
				return value2;
			}
			return "[" + key + "]";
		}

		public static string Format(string language, string key, params object[] args)
		{
			try
			{
				return string.Format(CultureInfo.InvariantCulture, Text(language, key), args);
			}
			catch (FormatException)
			{
				string value;
				return Languages["en"].TryGetValue(key, out value) ? string.Format(CultureInfo.InvariantCulture, value, args) : ("[" + key + "]");
			}
		}

		public static IEnumerable<string> TargetKeywords()
		{
			HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "SOLL", "TARGET" };
			foreach (Dictionary<string, string> value2 in Languages.Values)
			{
				if (value2.TryGetValue("request.target", out var value) && !string.IsNullOrWhiteSpace(value))
				{
					hashSet.Add(value.Trim());
				}
			}
			return hashSet;
		}

		public static bool StartsWithAnyTranslation(string text, params string[] keys)
		{
			string text2 = (text ?? string.Empty).TrimStart(Array.Empty<char>());
			if (text2.Length == 0 || keys == null || keys.Length == 0)
			{
				return false;
			}
			foreach (Dictionary<string, string> value2 in Languages.Values)
			{
				foreach (string text3 in keys)
				{
					if (!string.IsNullOrWhiteSpace(text3) && value2.TryGetValue(text3, out var value) && !string.IsNullOrWhiteSpace(value))
					{
						int num = value.IndexOfAny(new char[3] { '{', '\n', '\r' });
						string text4 = ((num >= 0) ? value.Substring(0, num) : value).Trim();
						if (text4.Length != 0 && text2.StartsWith(text4, StringComparison.OrdinalIgnoreCase) && (text2.Length == text4.Length || char.IsWhiteSpace(text2[text4.Length]) || text2[text4.Length] == ':'))
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		private static void AddBuiltIn(string code, Dictionary<string, string> strings)
		{
			Languages[code] = strings;
		}

		private static void LoadFile(string path)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				TranslationFile translationFile = new DeserializerBuilder().IgnoreUnmatchedProperties().Build().Deserialize<TranslationFile>(File.ReadAllText(path));
				string text = (translationFile?.language ?? string.Empty).Trim().Replace('_', '-').ToLowerInvariant();
				if (string.IsNullOrWhiteSpace(text) || translationFile.strings == null || translationFile.strings.Count == 0)
				{
					throw new InvalidDataException("'language' and a non-empty 'strings' object are required");
				}
				if (!TranslationGate.IsValidLanguageCode(text))
				{
					throw new InvalidDataException("'" + text + "' is not a valid language code; use a code such as 'es' or 'sr-Latn'");
				}
				if (string.IsNullOrWhiteSpace(translationFile.language_name))
				{
					throw new InvalidDataException("'language_name' is required (for example 'Español')");
				}
				if (text == "en" || text == "de")
				{
					throw new InvalidDataException("'" + text + "' is built in and cannot be replaced by an external file");
				}
				if (Languages.ContainsKey(text))
				{
					throw new InvalidDataException("language code '" + text + "' is already loaded; keep one file per language code");
				}
				if (!string.IsNullOrWhiteSpace(translationFile.language_name) && Aliases.TryGetValue(translationFile.language_name.Trim(), out var value) && !string.Equals(value, text, StringComparison.OrdinalIgnoreCase))
				{
					throw new InvalidDataException("language_name '" + translationFile.language_name + "' is already used by '" + value + "'");
				}
				Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
				foreach (KeyValuePair<string, string> @string in translationFile.strings)
				{
					if (!string.IsNullOrWhiteSpace(@string.Key) && @string.Value != null)
					{
						dictionary[@string.Key] = @string.Value;
					}
				}
				TranslationGate.Report report = TranslationGate.Validate(Languages["en"], dictionary);
				if (!report.IsValid)
				{
					throw new InvalidDataException("placeholder mismatch in key(s): " + string.Join(", ", report.InvalidPlaceholders) + "; preserve every {0}, {1}, etc. exactly as in TranslationTemplate.json");
				}
				foreach (string item in report.Unknown)
				{
					dictionary.Remove(item);
				}
				Languages[text] = dictionary;
				Aliases[text] = text;
				if (!string.IsNullOrWhiteSpace(translationFile.language_name))
				{
					Aliases[translationFile.language_name.Trim()] = text;
				}
				_log?.Invoke(string.Format("Loaded warehouse translation '{0}' ({1}/{2} strings) from {3}.", text, dictionary.Count, Languages["en"].Count, Path.GetFileName(path)) + GateSummary(report));
			}
			catch (Exception ex)
			{
				_log?.Invoke("Ignored invalid warehouse translation '" + Path.GetFileName(path) + "': " + ex.Message);
			}
		}

		private static string GateSummary(TranslationGate.Report report)
		{
			List<string> list = new List<string>();
			if (report.Missing.Count > 0)
			{
				list.Add($" {report.Missing.Count} missing/empty key(s) use English fallback: " + string.Join(", ", report.Missing.Take(8)) + ((report.Missing.Count > 8) ? ", …" : string.Empty) + ".");
			}
			if (report.Unknown.Count > 0)
			{
				list.Add($" Ignored {report.Unknown.Count} unknown key(s): " + string.Join(", ", report.Unknown.Take(8)) + ((report.Unknown.Count > 8) ? ", …" : string.Empty) + ".");
			}
			if (list.Count != 0)
			{
				return string.Join(" ", list);
			}
			return " Translation gate: complete; placeholders valid.";
		}

		private static void WriteTemplate(string path)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			TranslationFile translationFile;
			if (File.Exists(path))
			{
				try
				{
					translationFile = new DeserializerBuilder().IgnoreUnmatchedProperties().Build().Deserialize<TranslationFile>(File.ReadAllText(path));
					if (translationFile?.strings == null)
					{
						_log?.Invoke("TranslationTemplate.json was not updated because its strings object could not be read.");
						return;
					}
					int num = 0;
					foreach (KeyValuePair<string, string> item in English())
					{
						if (!translationFile.strings.ContainsKey(item.Key))
						{
							translationFile.strings[item.Key] = item.Value;
							num++;
						}
					}
					if (num == 0)
					{
						return;
					}
					_log?.Invoke($"Updated TranslationTemplate.json with {num} new key(s); existing entries were preserved.");
				}
				catch (Exception ex)
				{
					_log?.Invoke("TranslationTemplate.json was not updated: " + ex.Message);
					return;
				}
			}
			else
			{
				translationFile = new TranslationFile
				{
					language = "xx",
					language_name = "Your language",
					translator = "Your name",
					strings = English()
				};
			}
			WriteTemplateFile(path, translationFile);
		}

		private static void WriteTemplateFile(string path, TranslationFile file)
		{
			StringBuilder stringBuilder = new StringBuilder("{\n  \"language\": \"").Append(JsonEscape(file.language)).Append("\",\n  \"language_name\": \"").Append(JsonEscape(file.language_name))
				.Append("\",\n  \"translator\": \"")
				.Append(JsonEscape(file.translator))
				.Append("\",\n  \"strings\": {\n");
			List<KeyValuePair<string, string>> list = file.strings.OrderBy((KeyValuePair<string, string> x) => x.Key).ToList();
			for (int num = 0; num < list.Count; num++)
			{
				KeyValuePair<string, string> keyValuePair = list[num];
				stringBuilder.Append("    \"").Append(JsonEscape(keyValuePair.Key)).Append("\": \"")
					.Append(JsonEscape(keyValuePair.Value))
					.Append('"');
				if (num + 1 < list.Count)
				{
					stringBuilder.Append(',');
				}
				stringBuilder.AppendLine();
			}
			stringBuilder.Append("  }\n}\n");
			File.WriteAllText(path, stringBuilder.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
		}

		private static string JsonEscape(string value)
		{
			return (value ?? string.Empty).Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "\\r")
				.Replace("\n", "\\n");
		}

		private static Dictionary<string, string> English()
		{
			return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
			{
				["help.title"] = "WAREHOUSE HELP",
				["help.in"] = "IN: DEPOSIT ALL",
				["help.req"] = "REQ: WOOD 100",
				["help.out"] = "OUT: COLLECT",
				["help.board"] = "BOARD: E = FILTER",
				["request.title"] = "REQUEST",
				["request.example_item"] = "Wood",
				["request.updating"] = "UPDATING",
				["request.target"] = "TARGET",
				["request.error_example"] = "ERROR: e.g. Tin 20",
				["request.done"] = "DONE {0} x{1}",
				["request.partial"] = "PARTIAL {0} x{1}/{2}",
				["request.output_full"] = "OUTPUT FULL {0}",
				["request.no_output"] = "NO OUT CHEST: ADD AN OUT SIGN",
				["request.not_found"] = "NOT FOUND {0}",
				["request.error"] = "ERROR: {0}",
				["request.suggestions"] = "AVAILABLE",
				["request.shown_on_board"] = "SEARCH {0}\n{1} HIT(S) ON MAIN BOARD",
				["request.stock_live"] = "LIVE",
				["request.stock_seen"] = "AS OF {0}",
				["request.stock_unknown"] = "UNKNOWN",
				["request.idle"] = "REQ",
				["shopping.title"] = "NEEDED",
				["shopping.fulfilled"] = "TARGETS MET",
				["board.warehouse"] = "WAREHOUSE",
				["board.stock"] = "STOCK",
				["board.page"] = "PAGE",
				["board.chests"] = "CHESTS",
				["board.display_sleeping"] = "DISPLAY\nSLEEPING",
				["board.inactive"] = "INACTIVE",
				["board.no_space"] = "NO ROOM",
				["board.no_input"] = "IN SIGN\nMISSING",
				["board.locked"] = "WAREHOUSE\nLOCKED",
				["board.input_unavailable"] = "IN CHEST\nUNAVAILABLE",
				["board.wake"] = "{0} METRES\nWAKE UP",
				["board.sleep_word"] = "SLEEPMODE",
				["board.expand"] = "EXPAND!",
				["board.deposited"] = "DEPOSITED",
				["board.since_visit"] = "SINCE VISIT",
				["board.rejected"] = "REJECTED AT IN",
				["board.internal_item"] = "INTERNAL / CHEAT ITEM",
				["board.positions"] = "ITEMS",
				["board.no_matches"] = "NO MATCHES",
				["info.free"] = "FREE",
				["info.empty_reserved"] = "EMPTY RESERVED",
				["info.occupied"] = "OCCUPIED",
				["common.auto"] = "AUTO",
				["stats.new"] = "NEW",
				["network.active"] = "NETWORK\nACTIVE",
				["network.local"] = "LOCAL",
				["network.statistics"] = "NETWORK\nSTATISTICS",
				["network.warehouses"] = "WAREHOUSES",
				["network.chests_world"] = "CHESTS WORLD",
				["network.stock_world"] = "STOCK WORLD",
				["network.types_world"] = "ITEM TYPES WORLD",
				["stats.title"] = "STATISTICS",
				["stats.period"] = "PERIOD",
				["stats.today"] = "TODAY",
				["stats.days"] = "{0} DAYS",
				["stats.sleep_word"] = "STATISTICS",
				["stats.input"] = "INPUT",
				["stats.output"] = "OUTPUT",
				["stats.balance"] = "BALANCE",
				["stats.per_day"] = "MOVES / DAY",
				["stats.top_input"] = "TOP INPUT",
				["stats.top_output"] = "TOP OUTPUT",
				["stats.current"] = "CURRENT TOTAL",
				["stats.item_types"] = "ITEM TYPES",
				["stats.chests_now"] = "CHESTS NOW",
				["stats.avg_use"] = "AVG USE",
				["stats.peak"] = "PEAK",
				["stats.low"] = "LOW",
				["stats.rising"] = "RISING",
				["stats.falling"] = "FALLING",
				["stats.empty_first"] = "EMPTY FIRST",
				["stats.ratio"] = "IN / OUT",
				["stats.busy_in"] = "BUSIEST INPUT",
				["stats.busy_out"] = "BUSIEST OUTPUT",
				["stats.tracked_since"] = "TRACKED SINCE",
				["stats.full_in"] = "FULL IN",
				["stats.chest_trend"] = "CHESTS USED",
				["stats.week"] = "WEEK",
				["stats.days_tracked"] = "DAYS TRACKED",
				["stats.days_short"] = "DAYS",
				["stats.eta"] = "DAYS LEFT",
				["stats.input_change"] = "INPUT VS 7D",
				["stats.output_change"] = "OUTPUT VS 7D",
				["stats.confidence"] = "DATA BASIS",
				["stats.not_enough"] = "TOO LITTLE DATA",
				["stats.net_day"] = "NET / DAY",
				["stats.date"] = "DATE",
				["stats.amount"] = "AMOUNT",
				["stats.previous"] = "PREVIOUS",
				["stats.control"] = "STAT CONTROL",
				["stats.manual"] = "MANUAL",
				["stats.auto_in"] = "AUTO IN {0}s",
				["stats.page_1"] = "OVERVIEW",
				["stats.page_2"] = "COMPARE",
				["stats.page_3"] = "TREND",
				["stats.page_4"] = "RECORDS"
			};
		}

		private static Dictionary<string, string> German()
		{
			return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
			{
				["help.title"] = "LAGERHILFE",
				["help.in"] = "IN: ALLES REIN",
				["help.req"] = "REQ: HOLZ 100",
				["help.out"] = "OUT: ABHOLEN",
				["help.board"] = "TAFEL: E = FILTER",
				["request.title"] = "BESTELLUNG",
				["request.example_item"] = "Holz",
				["request.updating"] = "WIRD AKTUALISIERT",
				["request.target"] = "SOLL",
				["request.error_example"] = "FEHLER: z.B. Zinn 20",
				["request.done"] = "FERTIG {0} x{1}",
				["request.partial"] = "TEILWEISE {0} x{1}/{2}",
				["request.output_full"] = "AUSGABE VOLL {0}",
				["request.no_output"] = "KEINE OUT-KISTE: OUT-SCHILD SETZEN",
				["request.not_found"] = "NICHT GEFUNDEN {0}",
				["request.error"] = "FEHLER: {0}",
				["request.suggestions"] = "VERFÜGBAR",
				["request.shown_on_board"] = "SUCHE {0}\n{1} TREFFER AM LAGERPANEL",
				["request.stock_live"] = "LIVE",
				["request.stock_seen"] = "STAND {0}",
				["request.stock_unknown"] = "UNBEKANNT",
				["request.idle"] = "FRAGE",
				["shopping.title"] = "BEDARF",
				["shopping.fulfilled"] = "SOLL ERFÜLLT",
				["board.warehouse"] = "LAGER",
				["board.stock"] = "BESTAND",
				["board.page"] = "SEITE",
				["board.chests"] = "KISTEN",
				["board.display_sleeping"] = "ANZEIGE\nSCHLÄFT",
				["board.inactive"] = "INAKTIV",
				["board.no_space"] = "KEIN PLATZ",
				["board.no_input"] = "IN-SCHILD\nFEHLT",
				["board.locked"] = "LAGER\nGESPERRT",
				["board.input_unavailable"] = "IN-KISTE NICHT\nVERFÜGBAR",
				["board.wake"] = "{0} METER\nAUFWECKEN",
				["board.sleep_word"] = "RUHEMODUS",
				["board.expand"] = "AUSBAUEN!",
				["board.deposited"] = "EINGELAGERT",
				["board.since_visit"] = "SEIT BESUCH",
				["board.rejected"] = "BEI IN ABGEWIESEN",
				["board.internal_item"] = "INTERNES / CHEAT-OBJEKT",
				["board.positions"] = "POSITIONEN",
				["board.no_matches"] = "KEINE TREFFER",
				["info.free"] = "FREI",
				["info.empty_reserved"] = "LEER RESERVIERT",
				["info.occupied"] = "BELEGT",
				["common.auto"] = "AUTO",
				["stats.new"] = "NEU",
				["network.active"] = "LAGERNETZ\nAKTIV",
				["network.local"] = "LOKAL",
				["network.statistics"] = "LAGERNETZ\nSTATISTIK",
				["network.warehouses"] = "LAGER",
				["network.chests_world"] = "KISTEN WELT",
				["network.stock_world"] = "BESTAND WELT",
				["network.types_world"] = "ARTEN WELT",
				["stats.title"] = "STATISTIK",
				["stats.period"] = "ZEITRAUM",
				["stats.today"] = "HEUTE",
				["stats.days"] = "{0} TAGE",
				["stats.sleep_word"] = "ZAHLENRUHE",
				["stats.input"] = "EINGANG",
				["stats.output"] = "AUSGANG",
				["stats.balance"] = "SALDO",
				["stats.per_day"] = "BEWEGT / TAG",
				["stats.top_input"] = "TOP EINGANG",
				["stats.top_output"] = "TOP AUSGANG",
				["stats.current"] = "BESTAND TOTAL",
				["stats.item_types"] = "ARTIKELARTEN",
				["stats.chests_now"] = "KISTEN JETZT",
				["stats.avg_use"] = "Ø AUSLASTUNG",
				["stats.peak"] = "HÖCHSTSTAND",
				["stats.low"] = "TIEFSTSTAND",
				["stats.rising"] = "STEIGEND",
				["stats.falling"] = "FALLEND",
				["stats.empty_first"] = "ZUERST LEER",
				["stats.ratio"] = "EIN / AUS",
				["stats.busy_in"] = "MAX EINGANG",
				["stats.busy_out"] = "MAX AUSGANG",
				["stats.tracked_since"] = "ERFASST SEIT",
				["stats.full_in"] = "VOLL IN",
				["stats.chest_trend"] = "KISTEN BELEGT",
				["stats.week"] = "WOCHE",
				["stats.days_tracked"] = "TAGE ERFASST",
				["stats.days_short"] = "TAGE",
				["stats.eta"] = "NOCH TAGE",
				["stats.input_change"] = "EIN VS 7T",
				["stats.output_change"] = "AUS VS 7T",
				["stats.confidence"] = "DATENBASIS",
				["stats.not_enough"] = "ZU WENIG DATEN",
				["stats.net_day"] = "NETTO / TAG",
				["stats.date"] = "DATUM",
				["stats.amount"] = "MENGE",
				["stats.previous"] = "VORPERIODE",
				["stats.control"] = "STAT-STEUERUNG",
				["stats.manual"] = "MANUELL",
				["stats.auto_in"] = "AUTO IN {0}s",
				["stats.page_1"] = "ÜBERSICHT",
				["stats.page_2"] = "VERGLEICH",
				["stats.page_3"] = "TREND",
				["stats.page_4"] = "REKORDE"
			};
		}
	}
	[BepInPlugin("ch.cris.valheim.moderwarehouse", "Moder Warehouse", "2.0.3")]
	public sealed class WarehousePlugin : ServersideQoLPluginBase<WarehousePlugin, WarehouseConfig>
	{
		public const string Guid = "ch.cris.valheim.moderwarehouse";

		public const string Name = "Moder Warehouse";

		public const string Version = "2.0.3";

		public const string BuildId = "4net-20261021";

		protected override WarehouseConfig CreateConfigSingleton(ConfigFile file, Logger logger)
		{
			return new WarehouseConfig(file, logger);
		}

		protected override void RegisterProcessors(IProcessorCollection processors)
		{
			processors.Add<WarehouseProcessor>();
		}
	}
	public sealed class WarehouseConfig : ConfigBase<WarehouseConfig>
	{
		public override ConfigEntry<bool> Enabled { get; }

		public ConfigEntry<string> Language { get; }

		public ConfigEntry<float> Radius { get; }

		public ConfigEntry<float> MinimumRadius { get; }

		public ConfigEntry<float> MinimumWarehouseDistance { get; }

		public ConfigEntry<float> MarkerDistance { get; }

		public ConfigEntry<float> ScanInterval { get; }

		public ConfigEntry<int> MaxMoves { get; }

		public ConfigEntry<bool> AutoCreateLabels { get; }

		public ConfigEntry<float> LabelDistance { get; }

		public ConfigEntry<float> RequestResultSeconds { get; }

		public ConfigEntry<float> BoardPageSeconds { get; }

		public ConfigEntry<float> BoardWakeDistance { get; }

		public ConfigEntry<float> DeliveryNoticeSeconds { get; }

		public ConfigEntry<float> VisitSummarySeconds { get; }

		public ConfigEntry<int> CapacityWarningPercent { get; }

		public ConfigEntry<int> CapacityCriticalPercent { get; }

		public WarehouseConfig(ConfigFile file, Logger logger)
			: base(file, logger)
		{
			Enabled = ConfigBase<WarehouseConfig>.BindEx<bool>(file, "Warehouse", true, "Enable the warehouse.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "Enabled");
			Language = ConfigBase<WarehouseConfig>.BindEx<string>(file, "Localization", "English", "Default display language. Built in: English, German. External JSON translations may add more.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "Language");
			Radius = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Warehouse", 100f, "Storage radius around the input chest.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "Radius");
			MinimumRadius = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Warehouse", 10f, "Smallest radius selectable on an IN sign.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "MinimumRadius");
			MinimumWarehouseDistance = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Warehouse", 210f, "Minimum distance between warehouse IN chests.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "MinimumWarehouseDistance");
			MarkerDistance = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Warehouse", 3f, "Maximum sign-to-chest distance.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "MarkerDistance");
			ScanInterval = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Warehouse", 3f, "Seconds between scans.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "ScanInterval");
			MaxMoves = ConfigBase<WarehouseConfig>.BindEx<int>(file, "Safety", 250, "Maximum stack moves per scan.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "MaxMovesPerCycle");
			AutoCreateLabels = ConfigBase<WarehouseConfig>.BindEx<bool>(file, "Labels", true, "Create persistent localized white labels for homogeneous storage chests.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "AutoCreateLabels");
			LabelDistance = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Labels", 1f, "Maximum distance for ordinary storage labels and blank adoption signs.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "LabelDistance");
			RequestResultSeconds = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Requests", 8f, "Seconds withdrawal results remain visible before the REQ sign returns to its target-stock view (maximum 8 seconds).", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "ResultDisplaySeconds");
			BoardPageSeconds = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Display", 3f, "Seconds each warehouse board page remains visible.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "BoardPageSeconds");
			BoardWakeDistance = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Display", 5f, "Player distance in metres at which the stock board wakes up.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "BoardWakeDistance");
			DeliveryNoticeSeconds = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Display", 6f, "Seconds a completed delivery remains visible on the stock board.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "DeliveryNoticeSeconds");
			VisitSummarySeconds = ConfigBase<WarehouseConfig>.BindEx<float>(file, "Display", 12f, "Seconds stock changes since the last visit remain visible after waking the board.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "VisitSummarySeconds");
			CapacityWarningPercent = ConfigBase<WarehouseConfig>.BindEx<int>(file, "Display", 80, "Occupied chest percentage at which the capacity display becomes orange.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "CapacityWarningPercent");
			CapacityCriticalPercent = ConfigBase<WarehouseConfig>.BindEx<int>(file, "Display", 95, "Occupied chest percentage at which the capacity display becomes red and requests expansion.", (AcceptableValueBase)null, (Deprecated<WarehouseConfig>)null, "CapacityCriticalPercent");
		}
	}
	[Processor("1f3823e7-1db0-4857-b8b8-e377bd8cd191")]
	[RunAfter<ContainerRegistryProcessor>]
	public sealed class WarehouseProcessor : Processor<ProcessorPrefabInfo<Sign>>
	{
		private sealed class FillOrder
		{
			public ServersideQoLZDO Sign { get; }

			public ServersideQoLZDO Chest { get; }

			public string Payload { get; }

			public FillOrder(ServersideQoLZDO sign, ServersideQoLZDO chest, string payload)
			{
				Sign = sign;
				Chest = chest;
				Payload = payload;
			}
		}

		private sealed class StatsCacheEntry
		{
			public string Raw;

			public WarehouseStatsHistory History;

			public DateTime WrittenAt;
		}

		private sealed class PersistedContainerSpatialLookup
		{
			private readonly SpatialLookup<ZDO> lookup;

			private readonly HashSet<ZDO> checkedObjects = new HashSet<ZDO>();

			private readonly HashSet<ZDO> containers = new HashSet<ZDO>();

			internal PersistedContainerSpatialLookup(List<ZDO> objects)
			{
				lookup = new SpatialLookup<ZDO>(objects, (ZDO zdo) => zdo.GetPosition().x, (ZDO zdo) => zdo.GetPosition().y, (ZDO zdo) => zdo.GetPosition().z);
			}

			internal ZDO Nearest(ZDO sign, float range2)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				Vector3 position = sign.GetPosition();
				return lookup.Nearest(position.x, position.y, position.z, range2, IsContainer, preferLastOnTie: true);
			}

			private bool IsContainer(ZDO candidate)
			{
				if (containers.Contains(candidate))
				{
					return true;
				}
				if (checkedObjects.Contains(candidate))
				{
					return false;
				}
				checkedObjects.Add(candidate);
				ZNetScene instance = ZNetScene.instance;
				GameObject obj = ((instance != null) ? instance.GetPrefab(candidate.GetPrefab()) : null);
				if ((Object)(object)((obj != null) ? obj.GetComponent<Container>() : null) == (Object)null)
				{
					return false;
				}
				containers.Add(candidate);
				return true;
			}
		}

		private sealed class Request
		{
			public ServersideQoLZDO Sign { get; }

			public string Payload { get; }

			public Request(ServersideQoLZDO sign, string payload)
			{
				Sign = sign;
				Payload = payload;
			}
		}

		private sealed class RequestSuggestion
		{
			public string Token { get; set; }

			public string Name { get; set; }

			public int Score { get; set; }

			public Dictionary<string, int> Amounts { get; } = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

			public Dictionary<string, string> Statuses { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
		}

		private sealed class RequestSearchNotice
		{
			public string Query { get; }

			public List<RequestSuggestion> Matches { get; }

			public DateTime StartedAt { get; }

			public DateTime Until { get; }

			public RequestSearchNotice(string query, List<RequestSuggestion> matches, DateTime startedAt, DateTime until)
			{
				Query = query;
				Matches = matches;
				StartedAt = startedAt;
				Until = until;
			}
		}

		private sealed class PersistedSign
		{
			public ZDO Zdo { get; }

			public string Text { get; }

			public PersistedSign(ZDO zdo, string text)
			{
				Zdo = zdo;
				Text = text;
			}
		}

		private sealed class PersistedContainer
		{
			public ZDO Zdo { get; }

			public List<ItemData> Items { get; }

			public PersistedContainer(ZDO zdo, List<ItemData> items)
			{
				Zdo = zdo;
				Items = items ?? new List<ItemData>();
			}
		}

		private sealed class PersistedWarehouse
		{
			public string Id { get; }

			public ZDO Sign { get; }

			public ZDO Input { get; }

			public float Radius { get; }

			public PersistedWarehouse(string id, ZDO sign, ZDO input, float radius)
			{
				Id = id;
				Sign = sign;
				Input = input;
				Radius = radius;
			}
		}

		private sealed class PersistedInputCandidate
		{
			public string Id { get; }

			public ZDO Sign { get; }

			public ZDO Input { get; }

			public bool Canonical { get; }

			public PersistedInputCandidate(string id, ZDO sign, ZDO input, bool canonical)
			{
				Id = id;
				Sign = sign;
				Input = input;
				Canonical = canonical;
			}
		}

		private sealed class WarehouseNode
		{
			public string Id { get; }

			public ServersideQoLZDO Sign { get; }

			public ServersideQoLZDO Input { get; }

			public float Radius { get; }

			public string Language { get; }

			public bool Valid { get; set; } = true;

			public string Error { get; set; } = string.Empty;

			public WarehouseNode(string id, ServersideQoLZDO sign, ServersideQoLZDO input, float radius, string language)
			{
				Id = id;
				Sign = sign;
				Input = input;
				Radius = radius;
				Language = language;
			}
		}

		private sealed class NetworkOverview
		{
			public string Signature { get; }

			public List<BoardMessage> Rows { get; }

			public NetworkOverview(string signature, List<BoardMessage> rows)
			{
				Signature = signature;
				Rows = rows;
			}
		}

		private sealed class PerfWindow
		{
			public DateTime Since { get; set; }

			public int Cycles { get; set; }

			public int Skipped { get; set; }

			public int AwakeCycles { get; set; }

			public int SleepingCycles { get; set; }

			public long TotalMs { get; set; }

			public long MaxMs { get; set; }

			public long TopologyMs { get; set; }

			public long ClassifyMs { get; set; }

			public long DisplayMs { get; set; }

			public long TransferMs { get; set; }

			public int Containers { get; set; }

			public int Signs { get; set; }

			public int Stores { get; set; }

			public int InventoryReads { get; set; }

			public int Peers { get; set; }

			public int PingAverage { get; set; }

			public int PingMaximum { get; set; }

			public float LocalQualityMinimum { get; set; } = 1f;

			public float RemoteQualityMinimum { get; set; } = 1f;

			public float OutBytesPerSecond { get; set; }

			public float InBytesPerSecond { get; set; }
		}

		private sealed class StockEntry
		{
			public string Token { get; }

			public string Name { get; }

			public int Amount { get; }

			public StockEntry(string token, string name, int amount)
			{
				Token = token;
				Name = name;
				Amount = amount;
			}
		}

		private sealed class BoardControl
		{
			public string Prefix { get; set; }

			public int? Page { get; set; }

			public DateTime ChangedAt { get; set; }
		}

		private sealed class StatsControl
		{
			public int? Days { get; set; }

			public int? Page { get; set; }

			public DateTime ChangedAt { get; set; }
		}

		private sealed class TargetStatus
		{
			public string Name { get; }

			public int Current { get; }

			public int Target { get; }

			public TargetStatus(string name, int current, int target)
			{
				Name = name;
				Current = current;
				Target = target;
			}
		}

		private sealed class DeliveryNotice
		{
			public Dictionary<string, int> Items { get; }

			public DateTime Until { get; }

			public DeliveryNotice(Dictionary<string, int> items, DateTime until)
			{
				Items = items;
				Until = until;
			}
		}

		private sealed class BoardVisitState
		{
			public bool Awake { get; set; }

			public Dictionary<string, StockEntry> Baseline { get; set; } = new Dictionary<string, StockEntry>(StringComparer.OrdinalIgnoreCase);

			public List<StockChange> Changes { get; set; } = new List<StockChange>();

			public DateTime SummaryUntil { get; set; }
		}

		private sealed class StockChange
		{
			public string Name { get; }

			public int Amount { get; }

			public StockChange(string name, int amount)
			{
				Name = name;
				Amount = amount;
			}
		}

		private sealed class BoardMessage
		{
			public string Name { get; }

			public string Value { get; }

			public string Color { get; }

			public BoardMessage(string name, string value, string color)
			{
				Name = name;
				Value = value;
				Color = color;
			}
		}

		private static readonly Regex MarkerRegex = new Regex("^\\s*\\[WH:(?<id>[A-Za-z0-9_-]+):(?<role>IN|OUT|REQ|INFO|BOARD|STATS|FILL)\\](?<payload>[\\s\\S]*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);

		private static readonly WarehouseText.MatchCache Marker = new WarehouseText.MatchCache(MarkerRegex);

		private static readonly Regex IgnoreMarker = new Regex("^\\s*\\[WH:IGNORE\\]\\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);

		private static readonly Regex DeleteMarker = new Regex("^\\s*\\[WH:DELETE\\]\\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);

		private static readonly Regex CleanMarker = new Regex("^\\s*\\[WH:CLEAN\\]\\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);

		private static readonly int RequestWarehouseKey = KeyHash("ModerWarehouse.RequestWarehouse");

		private static readonly int InfoWarehouseKey = KeyHash("ModerWarehouse.InfoWarehouse");

		private static readonly int BoardWarehouseKey = KeyHash("ModerWarehouse.BoardWarehouse");

		private static readonly int BoardGroupKey = KeyHash("ModerWarehouse.BoardGroup");

		private static readonly int BoardCellKey = KeyHash("ModerWarehouse.BoardCell");

		private static readonly int BoardRenderedTextKey = KeyHash("ModerWarehouse.BoardRenderedText");

		private static readonly int StatsWarehouseKey = KeyHash("ModerWarehouse.StatsWarehouse");

		private static readonly int StatsGroupKey = KeyHash("ModerWarehouse.StatsGroup");

		private static readonly int StatsCellKey = KeyHash("ModerWarehouse.StatsCell");

		private static readonly int StatsHistoryKey = KeyHash("ModerWarehouse.StatsHistory");

		private static readonly int StatsControlKey = KeyHash("ModerWarehouse.StatsControl");

		private static readonly int RequestRenderedTextKey = KeyHash("ModerWarehouse.RequestRenderedText");

		private static readonly int RequestTargetsKey = KeyHash("ModerWarehouse.RequestTargets");

		private static readonly int HelpGroupKey = KeyHash("ModerWarehouse.HelpGroup");

		private static readonly int HelpChildKey = KeyHash("ModerWarehouse.HelpChild");

		private static readonly int HelpWarehouseKey = KeyHash("ModerWarehouse.HelpWarehouse");

		private const int BoardColumns = 4;

		private const int BoardRows = 4;

		private const int BoardCellCount = 16;

		private const int BoardPageSize = 12;

		private const int BoardAnchorCell = 12;

		private const int StatsColumns = 4;

		private const int StatsRows = 4;

		private const int StatsCellCount = 16;

		private const int StatsAnchorCell = 12;

		private static readonly int AutoChestXKey = KeyHash("ModerWarehouse.AutoChestX");

		private static readonly int AutoChestYKey = KeyHash("ModerWarehouse.AutoChestY");

		private static readonly int AutoChestZKey = KeyHash("ModerWarehouse.AutoChestZ");

		private static readonly int AutoLabelItemKey = KeyHash("ModerWarehouse.AutoLabelItem");

		private static readonly int AutoLabelManagedKey = KeyHash("ModerWarehouse.AutoLabelManaged");

		private static readonly int AutoLabelWarehouseKey = KeyHash("ModerWarehouse.AutoLabelWarehouse");

		private static readonly int AutoLabelPriorityKey = KeyHash("ModerWarehouse.AutoLabelPriority");

		private static readonly int InputRadiusKey = KeyHash("ModerWarehouse.InputRadius");

		private static readonly int InputWarehouseKey = KeyHash("ModerWarehouse.InputWarehouse");

		private static readonly int InputCanonicalKey = KeyHash("ModerWarehouse.InputCanonical");

		private readonly HashSet<ServersideQoLZDO> _signs = new HashSet<ServersideQoLZDO>();

		private static readonly Dictionary<string, string> GermanNames = new Dictionary<string, string>();

		private static readonly Dictionary<string, string> GermanDisplayNames = new Dictionary<string, string>();

		private static readonly Dictionary<string, string> EnglishNames = new Dictionary<string, string>();

		private static readonly Dictionary<string, string> EnglishDisplayNames = new Dictionary<string, string>();

		private static readonly Dictionary<string, string> PrefabNames = new Dictionary<string, string>();

		private static readonly Dictionary<string, HashSet<string>> AliasOwners = new Dictionary<string, HashSet<string>>(StringComparer.Ordinal);

		private static bool _germanNamesLoaded;

		private SectorDictionary<HashSet<ServersideQoLZDO>> _containers;

		private readonly Dictionary<ServersideQoLZDO, string> _labelItemTokens = new Dictionary<ServersideQoLZDO, string>();

		private readonly Dictionary<ServersideQoLZDO, string> _labelWarehouseIds = new Dictionary<ServersideQoLZDO, string>();

		private readonly Dictionary<ServersideQoLZDO, int> _labelPriorities = new Dictionary<ServersideQoLZDO, int>();

		private readonly Dictionary<ServersideQoLZDO, DateTime> _requestClearAt = new Dictionary<ServersideQoLZDO, DateTime>();

		private readonly Dictionary<string, DateTime> _orphanBoardSeenAt = new Dictionary<string, DateTime>(StringComparer.Ordinal);

		private readonly Dictionary<string, BoardControl> _boardControls = new Dictionary<string, BoardControl>(StringComparer.Ordinal);

		private readonly Dictionary<ServersideQoLZDO, DateTime> _helpPendingAt = new Dictionary<ServersideQoLZDO, DateTime>();

		private readonly Dictionary<string, DateTime> _boardEnsurePendingAt = new Dictionary<string, DateTime>(StringComparer.Ordinal);

		private readonly HashSet<string> _boardReadyGroups = new HashSet<string>(StringComparer.Ordinal);

		private readonly Dictionary<string, DateTime> _statsEnsurePendingAt = new Dictionary<string, DateTime>(StringComparer.Ordinal);

		private readonly HashSet<string> _statsReadyGroups = new HashSet<string>(StringComparer.Ordinal);

		private readonly Dictionary<string, StatsControl> _statsControls = new Dictionary<string, StatsControl>(StringComparer.Ordinal);

		private readonly Dictionary<string, DeliveryNotice> _deliveryNotices = new Dictionary<string, DeliveryNotice>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, BoardVisitState> _boardVisits = new Dictionary<string, BoardVisitState>(StringComparer.Ordinal);

		private NetworkOverview _networkOverview;

		private DateTime _networkOverviewAt;

		private bool _networkOverviewDirty = true;

		private readonly Dictionary<string, DateTime> _warehouseNextRun = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly HashSet<string> _warehousesRunning = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private readonly HashSet<string> _warehousesDirty = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, WarehouseNode> _knownNodes = new Dictionary<string, WarehouseNode>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, bool> _lastAwake = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DateTime> _lastCompleted = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<List<ServersideQoLZDO>, SpatialLookup<ServersideQoLZDO>> _spatialLookups = new Dictionary<List<ServersideQoLZDO>, SpatialLookup<ServersideQoLZDO>>();

		private List<ServersideQoLZDO> _storageSigns;

		private HashSet<ServersideQoLZDO> _localStorage;

		private readonly Dictionary<string, DateTime> _slowCycleLoggedAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DateTime> _slowSampleAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DateTime> _transferDiagnosticAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, PerfWindow> _performanceWindows = new Dictionary<string, PerfWindow>(StringComparer.OrdinalIgnoreCase);

		private List<string> _acceptedWarehouseIds = new List<string>();

		private readonly Dictionary<string, Vector3[]> _panelPositions = new Dictionary<string, Vector3[]>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, Dictionary<string, int>> _warehouseStockSnapshots = new Dictionary<string, Dictionary<string, int>>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DateTime> _warehouseStockTimes = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly HashSet<string> _freshlyReadWarehouses = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, string> _globalInputErrors = new Dictionary<string, string>(StringComparer.Ordinal);

		private DateTime _globalInputValidationAt;

		private DateTime _worldStockRefreshAt;

		private readonly Dictionary<string, string> _validatedTopologyParts = new Dictionary<string, string>(StringComparer.Ordinal);

		private readonly Dictionary<string, RequestSearchNotice> _requestSearchNotices = new Dictionary<string, RequestSearchNotice>(StringComparer.OrdinalIgnoreCase);

		private Dictionary<ServersideQoLZDO, IInventory> _cycleInventories;

		private readonly Dictionary<ServersideQoLZDO, IInventory> _inventoryIndex = new Dictionary<ServersideQoLZDO, IInventory>();

		private string _performanceLogPath;

		private string _stockCachePath;

		private bool _performanceLogDisabled;

		private int _cycleContainers;

		private int _cycleSigns;

		private int _cycleStores;

		private int _cycleInventoryReads;

		private long _cycleTopologyMs;

		private long _cycleClassifyMs;

		private long _cycleDisplayMs;

		private long _cycleTransferMs;

		private int _cyclePeers;

		private int _cyclePingAverage;

		private int _cyclePingMaximum;

		private float _cycleLocalQualityMinimum;

		private float _cycleRemoteQualityMinimum;

		private float _cycleOutBytesPerSecond;

		private float _cycleInBytesPerSecond;

		private readonly Dictionary<string, ServersideQoLZDO> _boardAnchors = new Dictionary<string, ServersideQoLZDO>(StringComparer.Ordinal);

		private readonly StringBuilder _cycleDetail = new StringBuilder();

		private Stopwatch _markWatch;

		private double _markLast;

		private readonly Dictionary<string, DateTime> _compactAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, Dictionary<string, int>> _noSpace = new Dictionary<string, Dictionary<string, int>>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DeliveryNotice> _noSpaceNotices = new Dictionary<string, DeliveryNotice>(StringComparer.OrdinalIgnoreCase);

		private const double StatsWriteSeconds = 60.0;

		private readonly Dictionary<ServersideQoLZDO, StatsCacheEntry> _statsCache = new Dictionary<ServersideQoLZDO, StatsCacheEntry>();

		private readonly Dictionary<string, string> _skipReported = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<List<ServersideQoLZDO>, Dictionary<(ServersideQoLZDO, float), ServersideQoLZDO>> _nearestMemo = new Dictionary<List<ServersideQoLZDO>, Dictionary<(ServersideQoLZDO, float), ServersideQoLZDO>>();

		private readonly Dictionary<string, DateTime> _miscJanitorAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, string> _quarantineReported = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		private readonly HashSet<ZDOID> _indexSigns = new HashSet<ZDOID>();

		private readonly HashSet<ZDOID> _indexContainers = new HashSet<ZDOID>();

		private DateTime _indexBuiltAt = DateTime.MinValue;

		private readonly Dictionary<int, bool> _containerPrefabs = new Dictionary<int, bool>();

		private readonly Dictionary<ZDOID, KeyValuePair<uint, PersistedContainer>> _decodedContainers = new Dictionary<ZDOID, KeyValuePair<uint, PersistedContainer>>();

		private const double WorldIndexMaxAgeMinutes = 30.0;

		private readonly Queue<ServersideQoLZDO> _warmQueue = new Queue<ServersideQoLZDO>();

		private readonly HashSet<ServersideQoLZDO> _warmQueued = new HashSet<ServersideQoLZDO>();

		private readonly Dictionary<string, DateTime> _warmDeadline = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private readonly Dictionary<string, DateTime> _warmArmedAt = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);

		private int _warmFrame = -1;

		private double _warmSpentMs;

		private Type _warmStateType;

		private FieldInfo _warmInventoryField;

		private bool _warmReflectionFailed;

		private const int WarmUpThreshold = 48;

		private const double WarmUpBudgetMs = 4.0;

		private const double WarmUpMaxSeconds = 3.0;

		private const double WarmUpRearmSeconds = 60.0;

		private const string LabelSeparator = "\u001f";

		private static readonly Dictionary<string, string[]> AliasCache = new Dictionary<string, string[]>(StringComparer.Ordinal);

		private IEnumerable<ServersideQoLZDO> StorageSigns
		{
			get
			{
				IEnumerable<ServersideQoLZDO> storageSigns = _storageSigns;
				return storageSigns ?? _signs;
			}
		}

		private static int KeyHash(string key)
		{
			return StringExtensionMethods.GetStableHashCode(key);
		}

		protected override void Initialize()
		{
			_signs.Clear();
			_requestClearAt.Clear();
			_orphanBoardSeenAt.Clear();
			_boardControls.Clear();
			_helpPendingAt.Clear();
			_boardEnsurePendingAt.Clear();
			_boardReadyGroups.Clear();
			_statsEnsurePendingAt.Clear();
			_statsReadyGroups.Clear();
			_statsControls.Clear();
			_deliveryNotices.Clear();
			_boardVisits.Clear();
			_networkOverview = null;
			_networkOverviewDirty = true;
			_warehouseNextRun.Clear();
			_warehousesRunning.Clear();
			_warehousesDirty.Clear();
			_knownNodes.Clear();
			_validatedTopologyParts.Clear();
			_lastAwake.Clear();
			_lastCompleted.Clear();
			_spatialLookups.Clear();
			_storageSigns = null;
			_localStorage = null;
			_slowCycleLoggedAt.Clear();
			_slowSampleAt.Clear();
			_performanceWindows.Clear();
			_panelPositions.Clear();
			_acceptedWarehouseIds = new List<string>();
			_boardAnchors.Clear();
			_quarantineReported.Clear();
			_noSpace.Clear();
			_noSpaceNotices.Clear();
			_skipReported.Clear();
			_statsCache.Clear();
			_warehouseStockSnapshots.Clear();
			_warehouseStockTimes.Clear();
			_freshlyReadWarehouses.Clear();
			_globalInputErrors.Clear();
			_globalInputValidationAt = DateTime.MinValue;
			_worldStockRefreshAt = DateTime.MinValue;
			_requestSearchNotices.Clear();
			_inventoryIndex.Clear();
			_warmQueue.Clear();
			_warmQueued.Clear();
			_warmDeadline.Clear();
			_warmArmedAt.Clear();
			_decodedContainers.Clear();
			_indexSigns.Clear();
			_indexContainers.Clear();
			_indexBuiltAt = DateTime.MinValue;
			InitializePerformanceLog();
			InitializeStockCache();
			ContainerRegistryProcessor val = Processor.Instance<ContainerRegistryProcessor>();
			_containers = val.GetContainers(100f);
			val.ContainerChanged -= OnContainerChanged;
			val.ContainerChanged += OnContainerChanged;
			ZNet.WorldSaveStarted = (Action)Delegate.Remove(ZNet.WorldSaveStarted, new Action(OnWorldSaveStarted));
			ZNet.WorldSaveStarted = (Action)Delegate.Combine(ZNet.WorldSaveStarted, new Action(OnWorldSaveStarted));
			WarehouseLocalization.Initialize(delegate(string message)
			{
				((Processor)this).Logger.LogInfo((object)message);
			});
			LoadGermanNames();
			((Processor)this).Logger.LogInfo((object)("Moder Warehouse 2.0.3 (build 4net-20261021): localization, configurable capacity alerts, player-safe input ownership, solo-friendly visit deltas, target stock shopping list, delivery confirmation, automatic IN help sign, interactive rotating 4x4 stock board, rolling 4x4 statistics board, persistent linked automatic signs, safe transfers and multi-item withdrawal initialized; " + $"{GermanNames.Count} EN/DE item names loaded."));
			((Processor)this).Logger.LogInfo((object)"Performance revision 4net-20261021: memoized sign/label text, per-type item aliases, warehouse-scoped change events, cached panel positions, throttled statistics writes, world index for read-only stock queries, inventory warm-up.");
		}

		private void OnContainerChanged(ServersideQoLZDO changed, ContainerState state)
		{
			_networkOverviewDirty = true;
			if (changed != null)
			{
				_inventoryIndex.Remove(changed);
				_cycleInventories?.Remove(changed);
			}
			if (_knownNodes.Count > 0)
			{
				WarehouseNode[] array = _knownNodes.Values.ToArray();
				foreach (WarehouseNode warehouseNode in array)
				{
					if ((changed == null || !(Distance2(changed, warehouseNode.Input) > warehouseNode.Radius * warehouseNode.Radius)) && _warehousesDirty.Add(warehouseNode.Id))
					{
						((Processor)this).ScheduleReprocessing(warehouseNode.Sign, 0.5f);
					}
				}
				return;
			}
			HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
			foreach (ServersideQoLZDO sign in _signs)
			{
				string text = WarehouseId(sign);
				if (!string.IsNullOrWhiteSpace(text) && hashSet.Add(text) && _warehousesDirty.Add(text))
				{
					((Processor)this).ScheduleReprocessing(sign, 0.5f);
				}
			}
		}

		private void InitializePerformanceLog()
		{
			_performanceLogDisabled = false;
			string text = Path.Combine(Paths.PluginPath, "ModerWarehouse");
			_performanceLogPath = Path.Combine(text, "ModerWarehouse-performance.log");
			try
			{
				Directory.CreateDirectory(text);
				File.WriteAllText(_performanceLogPath, "Moder Warehouse 2.0.3 performance log\r\n" + $"Started UTC: {DateTime.UtcNow:O}\r\n" + "Revision: 4net-20261021; SUMMARY phase times are independent maxima; SLOW sampled at most once per 10s; skipped means throttled callbacks, not failed work.\r\nUTC | kind | warehouse | state | cycles | skipped | avg_ms | max_ms | topology_ms | classify_ms | display_ms | transfer_ms | containers | signs | stores | inventory_reads | peers | ping_avg_ms | ping_max_ms | local_quality_min | remote_quality_min | out_Bps | in_Bps\r\n", Encoding.UTF8);
				((Processor)this).Logger.LogInfo((object)("Performance log reset: " + _performanceLogPath));
			}
			catch (Exception ex)
			{
				_performanceLogDisabled = true;
				((Processor)this).Logger.LogWarning((object)("Could not create warehouse performance log: " + ex.Message));
			}
		}

		private void InitializeStockCache()
		{
			string text = Path.Combine(Paths.PluginPath, "ModerWarehouse");
			_stockCachePath = Path.Combine(text, "ModerWarehouse-stock-cache.tsv");
			try
			{
				Directory.CreateDirectory(text);
				if (!File.Exists(_stockCachePath))
				{
					return;
				}
				string[] array = File.ReadAllLines(_stockCachePath, Encoding.UTF8);
				for (int i = 0; i < array.Length; i++)
				{
					string[] array2 = array[i].Split(new char[1] { '\t' });
					if (array2.Length >= 3 && int.TryParse(array2[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) && result > 0)
					{
						string key = Encoding.UTF8.GetString(Convert.FromBase64String(array2[0]));
						string key2 = Encoding.UTF8.GetString(Convert.FromBase64String(array2[1]));
						if (!_warehouseStockSnapshots.TryGetValue(key, out var value))
						{
							value = new Dictionary<string, int>(StringComparer.Ordinal);
							_warehouseStockSnapshots[key] = value;
						}
						value[key2] = result;
						if (array2.Length >= 4 && DateTime.TryParse(array2[3], CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out var result2))
						{
							_warehouseStockTimes[key] = result2.ToUniversalTime();
						}
					}
				}
				((Processor)this).Logger.LogInfo((object)$"Loaded last-known stock for {_warehouseStockSnapshots.Count} warehouse(s).");
			}
			catch (Exception ex)
			{
				((Processor)this).Logger.LogWarning((object)("Could not read warehouse stock cache: " + ex.Message));
			}
		}

		private void SaveStockCache()
		{
			if (string.IsNullOrWhiteSpace(_stockCachePath))
			{
				return;
			}
			try
			{
				string text = _stockCachePath + ".tmp";
				DateTime value;
				IEnumerable<string> contents = _warehouseStockSnapshots.OrderBy<KeyValuePair<string, Dictionary<string, int>>, string>((KeyValuePair<string, Dictionary<string, int>> pair) => pair.Key, StringComparer.OrdinalIgnoreCase).SelectMany((KeyValuePair<string, Dictionary<string, int>> warehouse) => from stock in warehouse.Value.Where((KeyValuePair<string, int> stock) => stock.Value > 0).OrderBy<KeyValuePair<string, int>, string>((KeyValuePair<string, int> stock) => stock.Key, StringComparer.Ordinal)
					select Convert.ToBase64String(Encoding.UTF8.GetBytes(warehouse.Key)) + "\t" + Convert.ToBase64String(Encoding.UTF8.GetBytes(stock.Key)) + "\t" + stock.Value.ToString(CultureInfo.InvariantCulture) + "\t" + (_warehouseStockTimes.TryGetValue(warehouse.Key, out value) ? value.ToUniversalTime().ToString("O", CultureInfo.InvariantCulture) : string.Empty));
				File.WriteAllLines(text, contents, Encoding.UTF8);
				if (File.Exists(_stockCachePath))
				{
					File.Delete(_stockCachePath);
				}
				File.Move(text, _stockCachePath);
			}
			catch (Exception ex)
			{
				((Processor)this).Logger.LogWarning((object)("Could not save warehouse stock cache: " + ex.Message));
			}
		}

		private static bool StockEqual(Dictionary<string, int> left, Dictionary<string, int> right)
		{
			if (left == right)
			{
				return true;
			}
			if (left == null || right == null || left.Count != right.Count)
			{
				return false;
			}
			int value;
			return left.All((KeyValuePair<string, int> pair) => right.TryGetValue(pair.Key, out value) && value == pair.Value);
		}

		private static string WarehouseId(ServersideQoLZDO sign)
		{
			//IL_0063: 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)
			int[] array = new int[4] { RequestWarehouseKey, InfoWarehouseKey, BoardWarehouseKey, StatsWarehouseKey };
			foreach (int num in array)
			{
				string text = sign.ZDO.GetString(num, string.Empty);
				if (!string.IsNullOrWhiteSpace(text))
				{
					return text.Trim();
				}
			}
			WarehouseText.MatchCache marker = Marker;
			ZDOVars vars = sign.Vars;
			Match match = marker.Match(PlainText(((ZDOVars)(ref vars)).GetText("")));
			if (!match.Success)
			{
				return string.Empty;
			}
			return match.Groups["id"].Value.Trim();
		}

		protected override bool ClaimExclusive(ServersideQoLZDO zdo)
		{
			return false;
		}

		protected override ProcessResult Process(ServersideQoLZDO zdo, IReadOnlyList<Peer> peers, ProcessorPrefabInfo<Sign> prefabInfo)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b1f: Unknown result type (might be due to invalid IL or missing references)
			//IL_07fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0cff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0920: Unknown result type (might be due to invalid IL or missing references)
			//IL_0ded: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d87: Unknown result type (might be due to invalid IL or missing references)
			//IL_08ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0aec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0ab1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a06: Unknown result type (might be due to invalid IL or missing references)
			zdo.Fields<Piece>().Set((Func<Expression<Func<Piece, bool>>>)(() => (Piece x) => x.m_canBeRemoved), true, "/_/ModerWarehouse/src/WarehousePlugin.cs", 433);
			ZDOVars vars = zdo.Vars;
			string text = PlainText(((ZDOVars)(ref vars)).GetText(""));
			if (CleanMarker.IsMatch(text))
			{
				List<ServersideQoLZDO> list = _signs.Where((ServersideQoLZDO other) => other != zdo && other.IsModCreator() && Distance2(other, zdo) < 0.0025f).ToList();
				foreach (ServersideQoLZDO item in list)
				{
					item.Destroy();
				}
				_signs.Remove(zdo);
				((Processor)this).Logger.LogInfo((object)$"Cleaned warehouse sign stack: removed {list.Count + 1} sign(s).");
				return (ProcessResult)4;
			}
			if (DeleteMarker.IsMatch(text))
			{
				string boardGroup = zdo.ZDO.GetString(BoardGroupKey, string.Empty);
				if (!string.IsNullOrWhiteSpace(boardGroup))
				{
					foreach (ServersideQoLZDO item2 in _signs.Where((ServersideQoLZDO x) => x.ZDO.GetString(BoardGroupKey, string.Empty) == boardGroup).ToList())
					{
						_signs.Remove(item2);
						item2.Destroy();
					}
					((Processor)this).Logger.LogInfo((object)("Deleted warehouse board group '" + boardGroup + "'."));
					return (ProcessResult)4;
				}
				string statsGroup = zdo.ZDO.GetString(StatsGroupKey, string.Empty);
				if (!string.IsNullOrWhiteSpace(statsGroup))
				{
					foreach (ServersideQoLZDO item3 in _signs.Where((ServersideQoLZDO x) => x.ZDO.GetString(StatsGroupKey, string.Empty) == statsGroup).ToList())
					{
						_signs.Remove(item3);
						item3.Destroy();
					}
					((Processor)this).Logger.LogInfo((object)("Deleted warehouse statistics group '" + statsGroup + "'."));
					return (ProcessResult)4;
				}
				_signs.Remove(zdo);
				((Processor)this).Logger.LogInfo((object)"Deleted warehouse sign marked [WH:DELETE].");
				return (ProcessResult)4;
			}
			bool num = _signs.Add(zdo);
			if (num)
			{
				zdo.Destroyed += delegate(ServersideQoLZDO destroyed)
				{
					_signs.Remove(destroyed);
				};
			}
			Match match = Marker.Match(text);
			if (num && match.Success)
			{
				_warehousesDirty.Add(match.Groups["id"].Value);
			}
			string text2 = zdo.ZDO.GetString(RequestWarehouseKey, string.Empty);
			string text3 = zdo.ZDO.GetString(InfoWarehouseKey, string.Empty);
			string text4 = zdo.ZDO.GetString(BoardWarehouseKey, string.Empty);
			string text5 = zdo.ZDO.GetString(StatsWarehouseKey, string.Empty);
			if (match.Success && match.Groups["role"].Value.Equals("REQ", StringComparison.OrdinalIgnoreCase))
			{
				text2 = match.Groups["id"].Value;
				zdo.ZDO.Set(RequestWarehouseKey, text2);
			}
			if (match.Success && match.Groups["role"].Value.Equals("INFO", StringComparison.OrdinalIgnoreCase))
			{
				text3 = match.Groups["id"].Value;
				zdo.ZDO.Set(InfoWarehouseKey, text3);
			}
			if (match.Success && match.Groups["role"].Value.Equals("BOARD", StringComparison.OrdinalIgnoreCase))
			{
				text4 = match.Groups["id"].Value;
				zdo.ZDO.Set(BoardWarehouseKey, text4);
				if (string.IsNullOrWhiteSpace(zdo.ZDO.GetString(BoardGroupKey, string.Empty)))
				{
					zdo.ZDO.Set(BoardGroupKey, Guid.NewGuid().ToString("N"));
				}
				zdo.ZDO.Set(BoardCellKey, 12, false);
				EnsureBoard(zdo, text4);
			}
			if (match.Success && match.Groups["role"].Value.Equals("STATS", StringComparison.OrdinalIgnoreCase))
			{
				text5 = match.Groups["id"].Value;
				zdo.ZDO.Set(StatsWarehouseKey, text5);
				if (string.IsNullOrWhiteSpace(zdo.ZDO.GetString(StatsGroupKey, string.Empty)))
				{
					zdo.ZDO.Set(StatsGroupKey, Guid.NewGuid().ToString("N"));
				}
				zdo.ZDO.Set(StatsCellKey, 12, false);
				EnsureStatsBoard(zdo, text5);
			}
			if (match.Success && match.Groups["role"].Value.Equals("IN", StringComparison.OrdinalIgnoreCase))
			{
				string text6 = match.Groups["id"].Value.Trim();
				float radius = ParseInputRadius(match.Groups["payload"].Value);
				WarehouseRenamePolicy.TryResolvePreviousId(zdo.ZDO.GetString(InputWarehouseKey, string.Empty), text6, out var previousId);
				bool flag = HasOtherInputWithId(zdo, text6);
				if (WarehouseRenamePolicy.CanApplyRename(previousId, text6, flag))
				{
					MigrateWarehouseSigns(zdo, previousId, text6, radius);
					zdo.ZDO.Set(InputWarehouseKey, text6);
				}
				else if (string.IsNullOrWhiteSpace(previousId) && !flag)
				{
					zdo.ZDO.Set(InputWarehouseKey, text6);
				}
				else if (WarehouseRenamePolicy.ShouldLogRenameConflict(previousId, flag))
				{
					((Processor)this).Logger.LogWarning((object)("Warehouse rename to '" + text6 + "' was not applied: another IN marker already uses that name."));
				}
				zdo.ZDO.Set(InputRadiusKey, ParseInputRadius(match.Groups["payload"].Value));
				EnsureHelpSign(zdo, text6, ResolveLanguage(match.Groups["payload"].Value));
			}
			if (!string.IsNullOrWhiteSpace(text2))
			{
				string text7 = (match.Success ? match.Groups["payload"].Value.Trim() : text.Trim());
				string text8 = zdo.ZDO.GetString(RequestRenderedTextKey, string.Empty);
				if (string.IsNullOrWhiteSpace(text7))
				{
					_requestClearAt.Remove(zdo);
					WriteRequestPrompt(zdo, LanguageForWarehouse(text2));
					return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
				}
				if (RequestPolicy.TryStripKeyword(text7, WarehouseLocalization.TargetKeywords(), out var rest))
				{
					if (TryParseTargets(rest, out var targets))
					{
						zdo.ZDO.Set(RequestTargetsKey, SerializeTargets(targets));
						((Processor)this).Logger.LogInfo((object)$"Warehouse '{text2}': saved {targets.Count} target stock value(s).");
						ProcessWarehouse(text2, peers, force: true);
					}
					else
					{
						string language = LanguageForWarehouse(text2);
						WriteRequestText(zdo, "<color=#ff6666>" + WarehouseLocalization.Text(language, "request.error_example") + "\n<color=white>" + WarehouseLocalization.Text(language, "request.target") + " " + WarehouseLocalization.Text(language, "request.example_item") + " 1000");
					}
					return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
				}
				if (IsRequestPrompt(text7))
				{
					ProcessWarehouse(text2, peers);
					return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
				}
				if (!string.IsNullOrWhiteSpace(text8) && string.Equals(text7, text8, StringComparison.Ordinal))
				{
					if (IsStatus(text7) && !_requestClearAt.ContainsKey(zdo))
					{
						_requestClearAt[zdo] = DateTime.UtcNow.AddSeconds(RequestDisplayPolicy.ClampResultSeconds(ConfigBase<WarehouseConfig>.Instance.RequestResultSeconds.Value));
					}
					if (_requestClearAt.TryGetValue(zdo, out var value) && DateTime.UtcNow >= value)
					{
						_requestClearAt.Remove(zdo);
						WriteRequestPrompt(zdo, LanguageForWarehouse(text2));
						ProcessWarehouse(text2, peers, force: true);
					}
					else
					{
						ProcessWarehouse(text2, peers);
					}
					return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
				}
				if (IsStatus(text7))
				{
					if (!_requestClearAt.TryGetValue(zdo, out var value2))
					{
						value2 = DateTime.UtcNow.AddSeconds(RequestDisplayPolicy.ClampResultSeconds(ConfigBase<WarehouseConfig>.Instance.RequestResultSeconds.Value));
						_requestClearAt[zdo] = value2;
					}
					if (DateTime.UtcNow >= value2)
					{
						WriteRequestPrompt(zdo, LanguageForWarehouse(text2));
						_requestClearAt.Remove(zdo);
					}
					return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
				}
				_requestClearAt.Remove(zdo);
				ProcessWarehouse(text2, peers, force: true);
				return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
			}
			if (!string.IsNullOrWhiteSpace(text3))
			{
				ProcessWarehouse(text3, peers);
				return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
			}
			if (!string.IsNullOrWhiteSpace(text4))
			{
				string group = zdo.ZDO.GetString(BoardGroupKey, string.Empty);
				int num2 = zdo.ZDO.GetInt(BoardCellKey, -1);
				CaptureBoardControlInput(zdo, group, num2);
				if (!string.IsNullOrWhiteSpace(group))
				{
					DateTime value3;
					if (HasBoardAnchor(group))
					{
						_orphanBoardSeenAt.Remove(group);
					}
					else if (!_orphanBoardSeenAt.TryGetValue(group, out value3))
					{
						_orphanBoardSeenAt[group] = DateTime.UtcNow;
					}
					else if ((DateTime.UtcNow - value3).TotalSeconds >= 10.0)
					{
						List<ServersideQoLZDO> list2 = _signs.Where((ServersideQoLZDO sign) => sign.ZDO.GetString(BoardGroupKey, string.Empty) == group).ToList();
						foreach (ServersideQoLZDO item4 in list2)
						{
							_signs.Remove(item4);
							item4.Destroy();
						}
						_orphanBoardSeenAt.Remove(group);
						((Processor)this).Logger.LogInfo((object)$"Removed {list2.Count} orphaned stock-board sign(s) from group '{group}'.");
						return (ProcessResult)4;
					}
				}
				if (num2 == 12)
				{
					EnsureBoard(zdo, text4);
				}
				if (num2 == 12)
				{
					ProcessWarehouse(text4, peers);
				}
				return ((Processor)this).ScheduleReprocessing(Mathf.Min(1f, Mathf.Max(0.5f, ConfigBase<WarehouseConfig>.Instance.BoardPageSeconds.Value / 2f)));
			}
			if (!string.IsNullOrWhiteSpace(text5))
			{
				int num3 = zdo.ZDO.GetInt(StatsCellKey, -1);
				if (num3 == 12)
				{
					EnsureStatsBoard(zdo, text5);
				}
				if (num3 == 12 || zdo.ZDO.GetBool(StatsControlKey, false))
				{
					ProcessWarehouse(text5, peers);
				}
				return ((Processor)this).ScheduleReprocessing(Mathf.Min(1f, Mathf.Max(0.5f, ConfigBase<WarehouseConfig>.Instance.BoardPageSeconds.Value / 2f)));
			}
			if (match.Success || IgnoreMarker.IsMatch(text))
			{
				WriteWhite(zdo, text);
			}
			if (match.Success)
			{
				ProcessWarehouse(match.Groups["id"].Value, peers);
			}
			return ((Processor)this).ScheduleReprocessing(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value));
		}

		private bool HasBoardAnchor(string group)
		{
			if (_boardAnchors.TryGetValue(group, out var value) && _signs.Contains(value) && value.ZDO.GetString(BoardGroupKey, string.Empty) == group && value.ZDO.GetInt(BoardCellKey, -1) == 12)
			{
				return true;
			}
			ServersideQoLZDO val = ((IEnumerable<ServersideQoLZDO>)_signs).FirstOrDefault((Func<ServersideQoLZDO, bool>)((ServersideQoLZDO sign) => sign.ZDO.GetString(BoardGroupKey, string.Empty) == group && sign.ZDO.GetInt(BoardCellKey, -1) == 12));
			if (val != null)
			{
				_boardAnchors[group] = val;
			}
			else
			{
				_boardAnchors.Remove(group);
			}
			return val != null;
		}

		private string LanguageForWarehouse(string id)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			foreach (ServersideQoLZDO sign in _signs)
			{
				WarehouseText.MatchCache marker = Marker;
				ZDOVars vars = sign.Vars;
				Match match = marker.Match(PlainText(((ZDOVars)(ref vars)).GetText("")));
				if (match.Success && match.Groups["id"].Value.Equals(id, StringComparison.OrdinalIgnoreCase) && match.Groups["role"].Value.Equals("IN", StringComparison.OrdinalIgnoreCase))
				{
					return ResolveLanguage(match.Groups["payload"].Value);
				}
			}
			return ResolveLanguage(string.Empty);
		}

		private static string ResolveLanguage(string payload)
		{
			Match match = Regex.Match(payload ?? string.Empty, "(?:^|[\\r\\n])\\s*LANG(?:UAGE)?\\s*=\\s*(?<language>[A-Za-z][A-Za-z0-9_-]*)\\s*(?:$|[\\r\\n])", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
			return WarehouseLocalization.Resolve(match.Success ? match.Groups["language"].Value : ConfigBase<WarehouseConfig>.Instance.Language.Value);
		}

		private void ProcessWarehouse(string id, IReadOnlyList<Peer> peers, bool force = false)
		{
			if (string.IsNullOrWhiteSpace(id))
			{
				return;
			}
			WarmInventories();
			string text = CanonicalWarehouseId(id.Trim());
			DateTime utcNow = DateTime.UtcNow;
			if (_warehousesRunning.Contains(text))
			{
				PerformanceWindow(text).Skipped++;
				return;
			}
			bool flag = HasAwakeBoard(text, peers);
			bool value;
			bool woke = flag && (!_lastAwake.TryGetValue(text, out value) || !value);
			_lastAwake[text] = flag;
			_warehouseNextRun.TryGetValue(text, out var value2);
			_lastCompleted.TryGetValue(text, out var value3);
			if (!WarehouseSchedule.CanRun(utcNow, value2, value3, force, woke, _warehousesDirty.Contains(text)))
			{
				PerformanceWindow(text).Skipped++;
				return;
			}
			_warehousesDirty.Remove(text);
			double num = Math.Max(1.0, ConfigBase<WarehouseConfig>.Instance.ScanInterval.Value);
			double value4 = (flag ? 1.0 : Math.Max(30.0, num * 10.0));
			_warehouseNextRun[text] = utcNow.AddSeconds(value4);
			_warehousesRunning.Add(text);
			_cycleInventories = new Dictionary<ServersideQoLZDO, IInventory>();
			_cycleContainers = (_cycleSigns = (_cycleStores = (_cycleInventoryReads = 0)));
			_cycleTopologyMs = (_cycleClassifyMs = (_cycleDisplayMs = (_cycleTransferMs = 0L)));
			CapturePeerMetrics();
			Stopwatch stopwatch = Stopwatch.StartNew();
			try
			{
				ProcessWarehouseCore(text, peers);
			}
			finally
			{
				stopwatch.Stop();
				_cycleInventories = null;
				_spatialLookups.Clear();
				_nearestMemo.Clear();
				_storageSigns = null;
				_localStorage = null;
				_warehousesRunning.Remove(text);
				_lastCompleted[text] = DateTime.UtcNow;
				_warehouseNextRun[text] = DateTime.UtcNow.AddSeconds(value4);
				RecordPerformance(text, flag, stopwatch.ElapsedMilliseconds);
				long num2 = (flag ? 150 : 250);
				if (stopwatch.ElapsedMilliseconds >= num2 && (!_slowCycleLoggedAt.TryGetValue(text, out var value5) || (DateTime.UtcNow - value5).TotalSeconds >= 120.0))
				{
					_slowCycleLoggedAt[text] = DateTime.UtcNow;
					((Processor)this).Logger.LogWarning((object)($"Warehouse '{text}': slow coordinated cycle {stopwatch.ElapsedMilliseconds} ms " + "(board " + (flag ? "awake" : "sleeping") + ")."));
				}
			}
		}

		private bool HasAwakeBoard(string id, IReadOnlyList<Peer> peers)
		{
			//IL_008d: 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_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: 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)
			if (peers == null || peers.Count == 0)
			{
				return false;
			}
			float distance2 = Mathf.Pow(Mathf.Max(1f, ConfigBase<WarehouseConfig>.Instance.BoardWakeDistance.Value), 2f);
			if (_panelPositions.TryGetValue(id, out var value))
			{
				foreach (Peer peer in peers)
				{
					if (peer == null)
					{
						continue;
					}
					Vector3[] array = value;
					foreach (Vector3 val in array)
					{
						Vector3 val2 = peer.RefPos - val;
						if (((Vector3)(ref val2)).sqrMagnitude <= distance2)
						{
							return true;
						}
					}
				}
				return false;
			}
			return _signs.Any(delegate(ServersideQoLZDO sign)
			{
				string text = sign.ZDO.GetString(BoardWarehouseKey, string.Empty);
				string text2 = sign.ZDO.GetString(StatsWarehouseKey, string.Empty);
				return (text.Equals(id, StringComparison.OrdinalIgnoreCase) || text2.Equals(id, StringComparison.OrdinalIgnoreCase)) && peers.Any(delegate(Peer peer)
				{
					//IL_0004: 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_0019: Unknown result type (might be due to invalid IL or missing references)
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					if (peer != null)
					{
						Vector3 val3 = peer.RefPos - sign.ZDO.GetPosition();
						return ((Vector3)(ref val3)).sqrMagnitude <= distance2;
					}
					return false;
				});
			});
		}

		private void MarkStart()
		{
			_cycleDetail.Clear();
			_markWatch = Stopwatch.StartNew();
			_markLast = 0.0;
		}

		private void Mark(string name)
		{
			if (_markWatch != null)
			{
				double totalMilliseconds = _markWatch.Elapsed.TotalMilliseconds;
				_cycleDetail.Append(name).Append('=').Append((totalMilliseconds - _markLast).ToString("0.0", CultureInfo.InvariantCulture))
					.Append(' ');
				_markLast = totalMilliseconds;
			}
		}

		private void ProcessWarehouseCore(string id, IReadOnlyList<Peer> peers)
		{
			//IL_0311: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_04c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b98: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b9d: Unknown result type (might be due to invalid IL or missing references)
			_cycleDetail.Clear();
			Stopwatch stopwatch = Stopwatch.StartNew();
			if (!_germanNamesLoaded)
			{
				LoadGermanNames();
			}
			string language = LanguageForWarehouse(id);
			List<ServersideQoLZDO> list = AllContainers();
			MergeLoadedIntoWorldIndex(list);
			_cycleContainers = list.Count;
			_cycleSigns = _signs.Count;
			float num = ConfigBase<WarehouseConfig>.Instance.MarkerDistance.Value * ConfigBase<WarehouseConfig>.Instance.MarkerDistance.Value;
			MarkStart();
			List<WarehouseNode> list2 = DiscoverWarehouseNodes(list, num);
			Mark("nodes");
			ValidateWarehouseNodes(list2, num);
			Mark("validate");
			_knownNodes.Clear();
			foreach (WarehouseNode item2 in list2.Where((WarehouseNode x) => x.Valid))
			{
				_knownNodes[item2.Id] = item2;
			}
			UpdateInputHelpSigns(list2);
			UpdateUnpairedInputSigns(list2);
			_cycleTopologyMs = stopwatch.ElapsedMilliseconds;
			Mark("helpSigns");
			WarehouseNode localNode = list2.FirstOrDefault((WarehouseNode x) => x.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
			if (localNode == null || !localNode.Valid)
			{
				ReportSkippedCycle(id, (localNode == null) ? ("no IN marker with a reachable input chest (network: " + string.Join(", ", list2.Select((WarehouseNode n) => n.Id + (n.Valid ? "" : "!"))) + ")") : ("warehouse locked (" + localNode.Error + ")"), (localNode == null) ? "board.no_input" : "board.locked", language);
				return;
			}
			_localStorage = new HashSet<ServersideQoLZDO>(list.Where((ServersideQoLZDO x) => Distance2(x, localNode.Input) <= localNode.Radius * localNode.Radius));
			float signRadius = localNode.Radius + Math.Max(ConfigBase<WarehouseConfig>.Instance.LabelDistance.Value, ConfigBase<WarehouseConfig>.Instance.MarkerDistance.Value) + 2f;
			_storageSigns = _signs.Where((ServersideQoLZDO sign) => Distance2(sign, localNode.Input) <= signRadius * signRadius || LinkedInside(sign, localNode)).ToList();
			Mark("localSets");
			HashSet<ServersideQoLZDO> endpoints = new HashSet<ServersideQoLZDO>();
			ServersideQoLZDO input = localNode.Input;
			ServersideQoLZDO outputZdo = null;
			List<Request> list3 = new List<Request>();
			List<ServersideQoLZDO> list4 = new List<ServersideQoLZDO>();
			List<ServersideQoLZDO> list5 = new List<ServersideQoLZDO>();
			List<ServersideQoLZDO> list6 = new List<ServersideQoLZDO>();
			List<FillOrder> list7 = new List<FillOrder>();
			float num2 = ConfigBase<WarehouseConfig>.Instance.LabelDistance.Value * ConfigBase<WarehouseConfig>.Instance.LabelDistance.Value;
			ZDOVars vars;
			foreach (ServersideQoLZDO sign in _signs)
			{
				WarehouseText.MatchCache marker = Marker;
				vars = sign.Vars;
				Match match = marker.Match(PlainText(((ZDOVars)(ref vars)).GetText("")));
				string text = sign.ZDO.GetString(RequestWarehouseKey, string.Empty);
				string text2 = sign.ZDO.GetString(InfoWarehouseKey, string.Empty);
				string text3 = sign.ZDO.GetString(BoardWarehouseKey, string.Empty);
				string text4 = sign.ZDO.GetString(StatsWarehouseKey, string.Empty);
				bool flag = !match.Success && text.Equals(id, StringComparison.OrdinalIgnoreCase);
				bool flag2 = !match.Success && text2.Equals(id, StringComparison.OrdinalIgnoreCase);
				bool flag3 = !match.Success && text3.Equals(id, StringComparison.OrdinalIgnoreCase);
				bool flag4 = !match.Success && text4.Equals(id, StringComparison.OrdinalIgnoreCase);
				if (!flag && !flag2 && !flag3 && !flag4 && (!match.Success || !match.Groups["id"].Value.Equals(id, StringComparison.OrdinalIgnoreCase)))
				{
					continue;
				}
				string text5 = (flag ? "REQ" : (flag2 ? "INFO" : (flag3 ? "BOARD" : (flag4 ? "STATS" : match.Groups["role"].Value.ToUpperInvariant()))));
				switch (text5)
				{
				case "REQ":
				{
					string text6;
					if (!flag)
					{
						text6 = match.Groups["payload"].Value.Trim();
					}
					else
					{
						vars = sign.Vars;
						text6 = PlainText(((ZDOVars)(ref vars)).GetText("")).Trim();
					}
					string payload = text6;
					list3.Add(new Request(sign, payload));
					continue;
				}
				case "INFO":
					list4.Add(sign);
					continue;
				case "BOARD":
					list5.Add(sign);
					continue;
				case "STATS":
					list6.Add(sign);
					continue;
				}
				ServersideQoLZDO val = NearestContainer(sign, list, num);
				if (val != null)
				{
					endpoints.Add(val);
					if (text5 == "IN" && val == localNode.Input)
					{
						input = val;
					}
					if (text5 == "OUT")
					{
						outputZdo = val;
					}
					if (text5 == "FILL")
					{
						list7.Add(new FillOrder(sign, val, match.Groups["payload"].Value));
					}
				}
			}
			_panelPositions[id] = (from sign in list5.Concat(list6)
				select sign.ZDO.GetPosition()).ToArray();
			Mark("signLoop");
			if (input == null)
			{
				ReportSkippedCycle(id, "input chest not resolved", "board.no_input", language);
				return;
			}
			ContainerRegistryProcessor registry = Processor.Instance<ContainerRegistryProcessor>();
			ContainerState state = registry.GetState(input);
			if (state == null)
			{
				ReportSkippedCycle(id, "input chest has no container state", "board.input_unavailable", language);
				return;
			}
			IInventory val2 = InventoryFor(registry, input);
			if (val2 == null)
			{
				ReportSkippedCycle(id, "input chest inventory unavailable", "board.input_unavailable", language);
				return;
			}
			ReportSkippedCycle(id, null, null, language);
			float radius2 = localNode.Radius * localNode.Radius;
			Mark("source");
			HashSet<ServersideQoLZDO> ignored = DiscoverIgnored(list, num);
			Mark("ignored");
			RefreshAutomaticLabels(list, num2, registry, language, id);
			Mark("refreshLabels");
			Dictionary<ServersideQoLZDO, string> labels = DiscoverLabels(list, num2, null, rememberItemTokens: true, id);
			Mark("discoverLabels");
			Dictionary<ServersideQoLZDO, ServersideQoLZDO> dictionary = DiscoverBlankSigns(list, num2, null, id);
			Mark("blankSigns");
			HashSet<ServersideQoLZDO> optedIn = new HashSet<ServersideQoLZDO>(labels.Keys);
			optedIn.UnionWith(dictionary.Keys);
			if (DeferForInventoryWarmUp(id, list.Where((ServersideQoLZDO x) => optedIn.Contains(x) && Distance2(x, input) <= radius2)))
			{
				return;
			}
			List<ServersideQoLZDO> quarantined = new List<ServersideQoLZDO>();
			List<ServersideQoLZDO> list8 = (from x in (from x in list
					where optedIn.Contains(x) && WarehouseContainerPolicy.CanManage(endpoints.Contains(x), ignored.Contains(x), IsPrivateContainer(x), inUse: false, hasStorageSign: true)
					where Distance2(x, input) <= radius2
					select x).Where(delegate(ServersideQoLZDO x)
				{
					if (ContainsOnlyWarehouseItems(InventoryFor(registry, x)))
					{
						return true;
					}
					if (InventoryFor(registry, x) != null)
					{
						quarantined.Add(x);
					}
					return false;
				})
				orderby Distance2(x, input)
				select x).ToList();
			List<ServersideQoLZDO> list9 = list8.Where(delegate(ServersideQoLZDO x)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				ZDOVars vars2 = x.Vars;
				return !((ZDOVars)(ref vars2)).GetInUse(false);
			}).ToList();
			ReportQuarantinedStorage(id, quarantined, registry);
			Mark("storesFilter");
			_cycleStores = list9.Count;
			int moves = Math.Max(1, ConfigBase<WarehouseConfig>.Instance.MaxMoves.Value);
			int num3 = CorrectMisfiledItems(id, list9, labels, registry, ref moves);
			if (num3 > 0)
			{
				((Processor)this).Logger.LogInfo((object)$"Warehouse '{id}' janitor refiled {num3} valid misplaced item(s) in total.");
			}
			Mark("janitor");
			ServersideQoLZDO val3 = ((IEnumerable<ServersideQoLZDO>)list6).FirstOrDefault((Func<ServersideQoLZDO, bool>)((ServersideQoLZDO sign) => sign.ZDO.GetInt(StatsCellKey, -1) == 12));
			StatsCacheEntry statsCacheEntry = ((val3 == null) ? null : TakeStatsHistory(val3));
			WarehouseStatsHistory warehouseStatsHistory = statsCacheEntry?.History;
			if (warehouseStatsHistory != null)
			{
				SnapshotStatistics(warehouseStatsHistory, list8, registry);
			}
			Mark("statsLoad");
			Dictionary<string, int> dictionary2 = (from val5 in list8.SelectMany(delegate(ServersideQoLZDO store)
				{
					IInventory obj = InventoryFor(registry, store);
					IEnumerable<ItemData> enumerable = ((obj != null) ? obj.Items : null);
					return enumerable ?? Enumerable.Empty<ItemData>();
				})
				where val5 != null && val5.m_stack > 0 && val5.m_shared != null
				group val5 by val5.m_shared.m_name).ToDictionary<IGrouping<string, ItemData>, string, int>((IGrouping<string, ItemData> group) => group.Key, (IGrouping<string, ItemData> group) => group.Sum((ItemData val5) => val5.m_stack), StringComparer.Ordinal);
			if (!_warehouseStockSnapshots.TryGetValue(id, out var value) || !StockEqual(value, dictionary2))
			{
				_warehouseStockSnapshots[id] = dictionary2;
				_warehouseStockTimes[id] = DateTime.UtcNow;
				SaveStockCache();
			}
			Mark("stockSnapshot");
			_cycleClassifyMs = stopwatch.ElapsedMilliseconds - _cycleTopologyMs;
			WhitenWarehouseLabels(list8.Concat(endpoints).Concat(ignored).Distinct()
				.ToList(), list, num2);
			Mark("whiten");
			LabelHomogeneousStores(id, list9, labels, dictionary, registry, language);
			Mark("autoLabel");
			WriteWarehouseInfo(id, language, list4, list8, labels, registry);
			WriteShoppingLists(language, list3.Select((Request x) => x.Sign), list8, registry);
			Mark("infoShopping");
			WriteWarehouseBoard(id, language, list5, list8, registry, peers, list2, list, num);
			Mark("board");
			_cycleDisplayMs = stopwatch.ElapsedMilliseconds - _cycleTopologyMs - _cycleClassifyMs;
			int num4 = 0;
			HashSet<ServersideQoLZDO> hashSet = new HashSet<ServersideQoLZDO>();
			Dictionary<string, int> dictionary3 = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
			bool flag5 = val2.Items.Any((ItemData val5) => IsWarehouseItem(val5) && val5.m_stack > 0);
			vars = input.Vars;
			bool inUse = ((ZDOVars)(ref vars)).GetInUse(false);
			bool flag6 = state.ZDO.IsOwnerOrUnassigned();
			bool flag7 = flag5 && !inUse && Own(state);
			int num5 = 0;
			int num6 = 0;
			int num7 = 0;
			int num8 = 0;
			List<ServersideQoLZDO> second = list9.Where((ServersideQoLZDO x) => IsMiscChest(x, labels)).ToList();
			Dictionary<string, int> dictionary4 = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
			bool flag8 = false;
			if (flag5 && !inUse && flag7)
			{
				flag8 = true;
				foreach (ItemData item in val2.Items.ToList())
				{
					if (moves <= 0 || item == null || item.m_stack <= 0)
					{
						break;
					}
					if (!IsWarehouseItem(item))
					{
						continue;
					}
					int num9 = item.m_stack;
					string value5;
					foreach (ServersideQoLZDO item3 in (from x in list9
						where (!labels.TryGetValue(x, out value5)) ? IsSingleItemType(InventoryFor(registry, x), item) : LabelMatches(x, value5, item, registry)
						orderby FillPriority(x, id, item.m_shared.m_name), (!HasItem(InventoryFor(registry, x), item)) ? ((labels.TryGetValue(x, out value5) && LabelMatches(x, value5, item, registry)) ? 1 : 0) : 2 descending
						select x).Concat(second))
					{
						if (moves <= 0 || num9 <= 0)
						{
							break;
						}
						num5++;
						ContainerState state2 = registry.GetState(item3);
						if (state2 == null)
						{
							num6++;
							continue;
						}
						if (!Own(state2))
						{
							num7++;
							continue;
						}
						IInventory val4 = InventoryFor(registry, item3);
						bool flag9 = !labels.ContainsKey(item3) && IsSingleItemType(val4, item);
						int num10 = Move(val2, val4, item, num9);
						if (num10 <= 0)
						{
							num8++;
						}
						if (num10 <= 0)
						{
							continue;
						}
						string key = LocalizedItemName(item, language);
						dictionary3[key] = (dictionary3.TryGetValue(key, out var value2) ? (value2 + num10) : num10);
						num4 += num10;
						warehouseStatsHistory?.AddInput(item.m_shared.m_name, num10);
						num9 -= num10;
						moves--;
						hashSet.Add(item3);
						if (flag9 && ConfigBase<WarehouseConfig>.Instance.AutoCreateLabels.Value)
						{
							string value3 =