Decompiled source of ImprovedPackagers v2.0.1

Alternate-ImprovedPackagers.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using FishNet;
using FishNet.Object;
using HarmonyLib;
using ImprovedPackagers;
using MelonLoader;
using MelonLoader.Preferences;
using ScheduleOne.Delivery;
using ScheduleOne.EntityFramework;
using ScheduleOne.ItemFramework;
using ScheduleOne.Management;
using ScheduleOne.NPCs.Behaviour;
using ScheduleOne.ObjectScripts;
using ScheduleOne.Persistence;
using ScheduleOne.Property;
using ScheduleOne.UI.Stations;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(global::ImprovedPackagers.ImprovedPackagers), "Improved Packagers", "2.0.0", "GuysWeForgotDre", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: AssemblyTitle("Improved Packagers")]
[assembly: AssemblyDescription("Enables Packagers to unpack product at Packaging Stations, and load vehicles parked in Loading Bays")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Improved Packagers")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("2.0.0.0")]
public static class StationModeRegistry
{
	private static readonly Dictionary<string, int> _explicit = new Dictionary<string, int>();

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

	private static readonly string FilePath = Path.Combine("UserData", "ImprovedPackagers.json");

	public static void Load()
	{
		if (File.Exists(FilePath))
		{
			return;
		}
		_explicit.Clear();
		_sticky.Clear();
		try
		{
			string[] array = File.ReadAllLines(FilePath);
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length == 0 || text.StartsWith("#"))
				{
					continue;
				}
				string[] array2 = text.Split(new char[1] { ':' });
				if (array2.Length == 2)
				{
					string key = array2[0];
					if (int.TryParse(array2[1], out var result))
					{
						_explicit[key] = result;
					}
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Load data error " + ex.Message);
		}
	}

	public static void Save()
	{
		try
		{
			using StreamWriter streamWriter = new StreamWriter(FilePath, append: false);
			streamWriter.WriteLine("# Improve Packagers: Unpacking Stations");
			foreach (KeyValuePair<string, int> item in _explicit)
			{
				streamWriter.WriteLine($"{item.Key}:{item.Value}");
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Save data error " + ex.Message);
		}
	}

	public static void SetExplicit(PackagingStation station, EMode mode)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Expected I4, but got Unknown
		_explicit[((BuildableItem)station).GUID.ToString()] = (int)mode;
		_sticky.Remove(((BuildableItem)station).GUID.ToString());
	}

	public static bool TryGetMode(PackagingStation station, out EMode mode)
	{
		mode = (EMode)0;
		if (station != null && _explicit.TryGetValue(((BuildableItem)station).GUID.ToString(), out var value))
		{
			mode = (EMode)value;
			return true;
		}
		if (station != null && _sticky.TryGetValue(((BuildableItem)station).GUID.ToString(), out value))
		{
			mode = (EMode)value;
			return true;
		}
		return false;
	}

	public static void SetExplicitIfExists(PackagingStation station)
	{
		if (station != null && _explicit.TryGetValue(((BuildableItem)station).GUID.ToString(), out var value))
		{
			_sticky[((BuildableItem)station).GUID.ToString()] = value;
		}
	}

	public static void SetStickyIfNone(PackagingStation station, EMode mode)
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected I4, but got Unknown
		if (station != null && !_explicit.ContainsKey(((BuildableItem)station).GUID.ToString()) && !_sticky.ContainsKey(((BuildableItem)station).GUID.ToString()))
		{
			_sticky[((BuildableItem)station).GUID.ToString()] = (int)mode;
		}
	}

	public static void Remove(PackagingStation station)
	{
		if (station != null)
		{
			_explicit.Remove(((BuildableItem)station).GUID.ToString());
			_sticky.Remove(((BuildableItem)station).GUID.ToString());
		}
	}

	public static void ClearAll()
	{
		_explicit.Clear();
		_sticky.Clear();
	}
}
namespace ImprovedPackagers;

[HarmonyPatch(typeof(PackagingStationCanvas), "SetIsOpen")]
internal static class PSCanvasSetIsOpenPatch
{
	private static void Postfix(PackagingStationCanvas __instance, PackagingStation station, bool open)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && station != null && open)
		{
			StationModeRegistry.SetExplicit(station, __instance.CurrentMode);
		}
	}
}
[HarmonyPatch(typeof(PackagingStationCanvas), "ToggleMode")]
internal static class PSCanvasToggleModePatch
{
	private static void Postfix(PackagingStationCanvas __instance)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && __instance.PackagingStation != null)
		{
			StationModeRegistry.SetExplicit(__instance.PackagingStation, __instance.CurrentMode);
		}
	}
}
[HarmonyPatch(typeof(PackagingStationBehaviour), "IsStationReady")]
internal static class PSBehaviourIsStationReadyPatch
{
	private static bool Prefix(PackagingStationBehaviour __instance, PackagingStation station, ref bool __result)
	{
		//IL_0009: 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_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		if (station == null)
		{
			__result = false;
			return false;
		}
		EMode mode = (EMode)0;
		StationModeRegistry.TryGetMode(station, out mode);
		if ((int)station.GetState(mode) != 0)
		{
			__result = false;
			return false;
		}
		if (station != null && ((IUsable)station).IsInUse && (Object)(object)station.NPCUserObject != (Object)(object)((NetworkBehaviour)((Behaviour)__instance).Npc).NetworkObject)
		{
			__result = false;
			return false;
		}
		Vector3 position = station.StandPoint.position;
		Vector3 val = -station.StandPoint.forward * 0.25f;
		Vector3 val2 = position + val;
		NavMeshHit val3 = default(NavMeshHit);
		if (NavMesh.SamplePosition(val2, ref val3, 0.6f, -1))
		{
			_ = ((NavMeshHit)(ref val3)).position;
		}
		__result = ((Behaviour)__instance).Npc.Movement.CanGetTo(station.StandPoint.position, 1f);
		return false;
	}
}
[HarmonyPatch(typeof(PackagingStation), "Awake")]
internal static class PackaginStationAwakePatch
{
	private static void Postfix(PackagingStation __instance)
	{
		StationModeRegistry.SetExplicitIfExists(__instance);
	}
}
[HarmonyPatch(typeof(PackagingStation), "PackSingleInstance")]
internal static class PackagingStationPackSingleInstancePatch
{
	private static bool Prefix(PackagingStation __instance)
	{
		//IL_000e: 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_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		if (__instance == null || __instance.NPCUserObject == null)
		{
			return true;
		}
		EMode val = (EMode)0;
		if (StationModeRegistry.TryGetMode(__instance, out var mode))
		{
			val = mode;
		}
		if (InstanceFinder.IsServer && (int)val == 1)
		{
			__instance.Unpack();
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(PackagingStation), "SetNPCUser")]
internal static class PackagingStationSetNPCUserPatch
{
	private static void Postfix(PackagingStation __instance, NetworkObject npcObject)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Invalid comparison between Unknown and I4
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && npcObject != null && !StationModeRegistry.TryGetMode(__instance, out var _))
		{
			bool flag = (int)__instance.GetState((EMode)0) == 0;
			EMode mode2 = (EMode)(((int)__instance.GetState((EMode)1) == 0 && !flag) ? 1 : 0);
			StationModeRegistry.SetStickyIfNone(__instance, mode2);
		}
	}
}
[HarmonyPatch(typeof(PackagingStation), "DestroyItem")]
internal static class PackagingStationDestroyPatch
{
	private static void Postfix(PackagingStation __instance)
	{
		StationModeRegistry.Remove(__instance);
	}
}
[HarmonyPatch(typeof(SaveManager), "Save", new Type[] { typeof(string) })]
public class SaveManagerSavePatch
{
	private static void Prefix()
	{
		StationModeRegistry.Save();
	}
}
[HarmonyPatch(typeof(LoadingDock), "SetOccupant")]
internal static class LoadingDockSetOccupantPatch
{
	private static void Postfix(LoadingDock __instance)
	{
		if (!((Object)(object)__instance.DynamicOccupant != (Object)null) || !ImprovedPackagers.GetDockStatus(__instance.ParentProperty.PropertyName, __instance.Name))
		{
			return;
		}
		foreach (ItemSlot itemSlot in __instance.DynamicOccupant.Storage.ItemSlots)
		{
			__instance.InputSlots.Add(itemSlot);
		}
	}
}
[HarmonyPatch(typeof(Property), "Start")]
internal static class PropertyStartPatch
{
	private static void Postfix(Property __instance)
	{
		ImprovedPackagers.AddProperty(__instance);
	}
}
public enum EDirection
{
	Dual_Direction,
	Unload_Only,
	Load_Only
}
public class ImprovedPackagers : MelonMod
{
	public const string ModName = "Improved Packagers";

	public const string Version = "2.0.0";

	public const string ModDesc = "Enables Packagers to unpack product at Packaging Stations, and load vehicles parked in Loading Bays";

	private MelonPreferences_Category PropertyGroup;

	private readonly List<MelonPreferences_Entry<bool>> LoadingBayPrefs = new List<MelonPreferences_Entry<bool>>();

	private const bool prefDefault = true;

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

	public override void OnLateInitializeMelon()
	{
		ReflectionHelper.Initialize();
		StationModeRegistry.Load();
	}

	public override void OnDeinitializeMelon()
	{
		ReflectionHelper.Deinitialize();
		StationModeRegistry.ClearAll();
	}

	public static ImprovedPackagers AddLoadingDocks(Property property)
	{
		string propertyName = property.PropertyName;
		ImprovedPackagers improvedPackagers = new ImprovedPackagers
		{
			PropertyGroup = MelonPreferences.CreateCategory("PackagersLoadVehicles_" + propertyName, propertyName)
		};
		LoadingDock[] loadingDocks = property.LoadingDocks;
		foreach (LoadingDock val in loadingDocks)
		{
			improvedPackagers.LoadingBayPrefs.Add(improvedPackagers.PropertyGroup.CreateEntry<bool>(val.Name.Replace(" ", ""), true, val.Name, (string)null, false, false, (ValueValidator)null, (string)null));
		}
		return improvedPackagers;
	}

	public static void AddProperty(Property property)
	{
		if (property.LoadingDockCount != 0 && !AllProperties.TryGetValue(property.PropertyName, out var _))
		{
			AllProperties.Add(property.PropertyName, AddLoadingDocks(property));
		}
	}

	public static bool GetDockStatus(string property, string dock)
	{
		if (AllProperties.TryGetValue(property, out var value))
		{
			foreach (MelonPreferences_Entry<bool> loadingBayPref in value.LoadingBayPrefs)
			{
				if (((MelonPreferences_Entry)loadingBayPref).DisplayName == dock)
				{
					return loadingBayPref.Value;
				}
			}
		}
		return true;
	}
}
public static class ReflectionHelper
{
	public static MethodInfo SetOccupant;

	private static EventInfo _event;

	private static Delegate _handler;

	private static bool _hooked;

	private static readonly BindingFlags _flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

	public static void Initialize()
	{
		if (_hooked)
		{
			return;
		}
		try
		{
			SetOccupant = AccessTools.Method(typeof(LoadingDock), "SetOccupant", (Type[])null, (Type[])null);
			Type type = (from a in AppDomain.CurrentDomain.GetAssemblies()
				select a.GetType("ModManagerPhoneApp.ModSettingsEvents")).FirstOrDefault((Type t) => t != null);
			if (!(type == null))
			{
				_event = type.GetEvent("OnPreferencesSaved", _flags);
				if (!(_event == null))
				{
					_handler = Delegate.CreateDelegate(typeof(Action), typeof(ReflectionHelper).GetMethod("OnPrefsSaved", _flags));
					_event.AddEventHandler(null, _handler);
					_hooked = true;
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Failed to initialize Mod Manager auto refresh " + ex.Message);
		}
	}

	public static void Deinitialize()
	{
		try
		{
			if (_hooked && _event != null && (object)_handler != null)
			{
				_event.RemoveEventHandler(null, _handler);
			}
		}
		catch
		{
		}
		finally
		{
			_hooked = false;
			_event = null;
			_handler = null;
		}
	}

	private static void OnPrefsSaved()
	{
		try
		{
			foreach (Property ownedProperty in Property.OwnedProperties)
			{
				LoadingDock[] loadingDocks = ownedProperty.LoadingDocks;
				foreach (LoadingDock obj in loadingDocks)
				{
					SetOccupant.Invoke(obj, new object[1]);
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("OnPrefsSaved error " + ex.Message);
		}
	}
}

Main-ImprovedPackagers.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2CppFishNet;
using Il2CppFishNet.Object;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppScheduleOne.Delivery;
using Il2CppScheduleOne.EntityFramework;
using Il2CppScheduleOne.ItemFramework;
using Il2CppScheduleOne.Management;
using Il2CppScheduleOne.NPCs.Behaviour;
using Il2CppScheduleOne.ObjectScripts;
using Il2CppScheduleOne.Persistence;
using Il2CppScheduleOne.Property;
using Il2CppScheduleOne.UI.Stations;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using ImprovedPackagers;
using MelonLoader;
using MelonLoader.Preferences;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(global::ImprovedPackagers.ImprovedPackagers), "Improved Packagers", "2.0.0", "GuysWeForgotDre", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: AssemblyTitle("Improved Packagers")]
[assembly: AssemblyDescription("Enables Packagers to unpack product at Packaging Stations, and load vehicles parked in Loading Bays")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Improved Packagers")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("2.0.0.0")]
public static class StationModeRegistry
{
	private static readonly Dictionary<string, int> _explicit = new Dictionary<string, int>();

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

	private static readonly string FilePath = Path.Combine("UserData", "ImprovedPackagers.json");

	public static void Load()
	{
		if (File.Exists(FilePath))
		{
			return;
		}
		_explicit.Clear();
		_sticky.Clear();
		try
		{
			string[] array = File.ReadAllLines(FilePath);
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length == 0 || text.StartsWith("#"))
				{
					continue;
				}
				string[] array2 = text.Split(new char[1] { ':' });
				if (array2.Length == 2)
				{
					string key = array2[0];
					if (int.TryParse(array2[1], out var result))
					{
						_explicit[key] = result;
					}
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Load data error " + ex.Message);
		}
	}

	public static void Save()
	{
		try
		{
			using StreamWriter streamWriter = new StreamWriter(FilePath, append: false);
			streamWriter.WriteLine("# Improve Packagers: Unpacking Stations");
			foreach (KeyValuePair<string, int> item in _explicit)
			{
				streamWriter.WriteLine($"{item.Key}:{item.Value}");
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Save data error " + ex.Message);
		}
	}

	public static void SetExplicit(PackagingStation station, EMode mode)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: 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_001f: Expected I4, but got Unknown
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		Dictionary<string, int> @explicit = _explicit;
		Guid gUID = ((BuildableItem)station).GUID;
		@explicit[((object)(Guid)(ref gUID)).ToString()] = (int)mode;
		Dictionary<string, int> sticky = _sticky;
		gUID = ((BuildableItem)station).GUID;
		sticky.Remove(((object)(Guid)(ref gUID)).ToString());
	}

	public static bool TryGetMode(PackagingStation station, out EMode mode)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		mode = (EMode)0;
		Guid gUID;
		int value;
		if (station != null)
		{
			Dictionary<string, int> @explicit = _explicit;
			gUID = ((BuildableItem)station).GUID;
			if (@explicit.TryGetValue(((object)(Guid)(ref gUID)).ToString(), out value))
			{
				mode = (EMode)value;
				return true;
			}
		}
		if (station != null)
		{
			Dictionary<string, int> sticky = _sticky;
			gUID = ((BuildableItem)station).GUID;
			if (sticky.TryGetValue(((object)(Guid)(ref gUID)).ToString(), out value))
			{
				mode = (EMode)value;
				return true;
			}
		}
		return false;
	}

	public static void SetExplicitIfExists(PackagingStation station)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		if (station != null)
		{
			Dictionary<string, int> @explicit = _explicit;
			Guid gUID = ((BuildableItem)station).GUID;
			if (@explicit.TryGetValue(((object)(Guid)(ref gUID)).ToString(), out var value))
			{
				Dictionary<string, int> sticky = _sticky;
				gUID = ((BuildableItem)station).GUID;
				sticky[((object)(Guid)(ref gUID)).ToString()] = value;
			}
		}
	}

	public static void SetStickyIfNone(PackagingStation station, EMode mode)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected I4, but got Unknown
		if (station == null)
		{
			return;
		}
		Dictionary<string, int> @explicit = _explicit;
		Guid gUID = ((BuildableItem)station).GUID;
		if (!@explicit.ContainsKey(((object)(Guid)(ref gUID)).ToString()))
		{
			Dictionary<string, int> sticky = _sticky;
			gUID = ((BuildableItem)station).GUID;
			if (!sticky.ContainsKey(((object)(Guid)(ref gUID)).ToString()))
			{
				Dictionary<string, int> sticky2 = _sticky;
				gUID = ((BuildableItem)station).GUID;
				sticky2[((object)(Guid)(ref gUID)).ToString()] = (int)mode;
			}
		}
	}

	public static void Remove(PackagingStation station)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		if (station != null)
		{
			Dictionary<string, int> @explicit = _explicit;
			Guid gUID = ((BuildableItem)station).GUID;
			@explicit.Remove(((object)(Guid)(ref gUID)).ToString());
			Dictionary<string, int> sticky = _sticky;
			gUID = ((BuildableItem)station).GUID;
			sticky.Remove(((object)(Guid)(ref gUID)).ToString());
		}
	}

	public static void ClearAll()
	{
		_explicit.Clear();
		_sticky.Clear();
	}
}
namespace ImprovedPackagers;

[HarmonyPatch(typeof(PackagingStationCanvas), "SetIsOpen")]
internal static class PSCanvasSetIsOpenPatch
{
	private static void Postfix(PackagingStationCanvas __instance, PackagingStation station, bool open)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && station != null && open)
		{
			StationModeRegistry.SetExplicit(station, __instance.CurrentMode);
		}
	}
}
[HarmonyPatch(typeof(PackagingStationCanvas), "ToggleMode")]
internal static class PSCanvasToggleModePatch
{
	private static void Postfix(PackagingStationCanvas __instance)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && __instance.PackagingStation != null)
		{
			StationModeRegistry.SetExplicit(__instance.PackagingStation, __instance.CurrentMode);
		}
	}
}
[HarmonyPatch(typeof(PackagingStationBehaviour), "IsStationReady")]
internal static class PSBehaviourIsStationReadyPatch
{
	private static bool Prefix(PackagingStationBehaviour __instance, PackagingStation station, ref bool __result)
	{
		//IL_0009: 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_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		if (station == null)
		{
			__result = false;
			return false;
		}
		EMode mode = (EMode)0;
		StationModeRegistry.TryGetMode(station, out mode);
		if ((int)station.GetState(mode) != 0)
		{
			__result = false;
			return false;
		}
		IUsable val = ((station != null) ? ((Il2CppObjectBase)station).TryCast<IUsable>() : null);
		if (val != null && val.IsInUse && (Object)(object)station.NPCUserObject != (Object)(object)((NetworkBehaviour)((Behaviour)__instance).Npc).NetworkObject)
		{
			__result = false;
			return false;
		}
		Vector3 position = station.StandPoint.position;
		Vector3 val2 = -station.StandPoint.forward * 0.25f;
		Vector3 val3 = position + val2;
		NavMeshHit val4 = default(NavMeshHit);
		if (NavMesh.SamplePosition(val3, ref val4, 0.6f, -1))
		{
			_ = ((NavMeshHit)(ref val4)).position;
		}
		__result = ((Behaviour)__instance).Npc.Movement.CanGetTo(station.StandPoint.position, 1f);
		return false;
	}
}
[HarmonyPatch(typeof(PackagingStation), "Awake")]
internal static class PackaginStationAwakePatch
{
	private static void Postfix(PackagingStation __instance)
	{
		StationModeRegistry.SetExplicitIfExists(__instance);
	}
}
[HarmonyPatch(typeof(PackagingStation), "PackSingleInstance")]
internal static class PackagingStationPackSingleInstancePatch
{
	private static bool Prefix(PackagingStation __instance)
	{
		//IL_000e: 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_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		if (__instance == null || __instance.NPCUserObject == null)
		{
			return true;
		}
		EMode val = (EMode)0;
		if (StationModeRegistry.TryGetMode(__instance, out var mode))
		{
			val = mode;
		}
		if (InstanceFinder.IsServer && (int)val == 1)
		{
			__instance.Unpack();
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(PackagingStation), "SetNPCUser")]
internal static class PackagingStationSetNPCUserPatch
{
	private static void Postfix(PackagingStation __instance, NetworkObject npcObject)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Invalid comparison between Unknown and I4
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		if (__instance != null && npcObject != null && !StationModeRegistry.TryGetMode(__instance, out var _))
		{
			bool flag = (int)__instance.GetState((EMode)0) == 0;
			EMode mode2 = (EMode)(((int)__instance.GetState((EMode)1) == 0 && !flag) ? 1 : 0);
			StationModeRegistry.SetStickyIfNone(__instance, mode2);
		}
	}
}
[HarmonyPatch(typeof(PackagingStation), "DestroyItem")]
internal static class PackagingStationDestroyPatch
{
	private static void Postfix(PackagingStation __instance)
	{
		StationModeRegistry.Remove(__instance);
	}
}
[HarmonyPatch(typeof(SaveManager), "Save", new Type[] { typeof(string) })]
public class SaveManagerSavePatch
{
	private static void Prefix()
	{
		StationModeRegistry.Save();
	}
}
[HarmonyPatch(typeof(LoadingDock), "SetOccupant")]
internal static class LoadingDockSetOccupantPatch
{
	private static void Postfix(LoadingDock __instance)
	{
		if (!((Object)(object)__instance.DynamicOccupant != (Object)null))
		{
			return;
		}
		EDirection dockStatus = ImprovedPackagers.GetDockStatus(__instance.ParentProperty.PropertyName, __instance.Name);
		if (dockStatus != EDirection.Unload_Only)
		{
			Enumerator<ItemSlot> enumerator = __instance.DynamicOccupant.Storage.ItemSlots.GetEnumerator();
			while (enumerator.MoveNext())
			{
				ItemSlot current = enumerator.Current;
				__instance.InputSlots.Add(current);
			}
		}
		if (dockStatus == EDirection.Load_Only)
		{
			__instance.OutputSlots.Clear();
		}
	}
}
[HarmonyPatch(typeof(Property), "Start")]
internal static class PropertyStartPatch
{
	private static void Postfix(Property __instance)
	{
		ImprovedPackagers.AddProperty(__instance);
	}
}
public enum EDirection
{
	Dual_Direction,
	Unload_Only,
	Load_Only
}
public class ImprovedPackagers : MelonMod
{
	public const string ModName = "Improved Packagers";

	public const string Version = "2.0.0";

	public const string ModDesc = "Enables Packagers to unpack product at Packaging Stations, and load vehicles parked in Loading Bays";

	private MelonPreferences_Category PropertyGroup;

	private readonly List<MelonPreferences_Entry<EDirection>> LoadingBayPrefs = new List<MelonPreferences_Entry<EDirection>>();

	private const EDirection prefDefault = EDirection.Dual_Direction;

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

	public override void OnLateInitializeMelon()
	{
		ReflectionHelper.Initialize();
		StationModeRegistry.Load();
	}

	public override void OnDeinitializeMelon()
	{
		ReflectionHelper.Deinitialize();
		StationModeRegistry.ClearAll();
	}

	public static ImprovedPackagers AddLoadingDocks(Property property)
	{
		string propertyName = property.PropertyName;
		ImprovedPackagers improvedPackagers = new ImprovedPackagers
		{
			PropertyGroup = MelonPreferences.CreateCategory("PackagersLoadVehicles_" + propertyName, propertyName)
		};
		foreach (LoadingDock item in (Il2CppArrayBase<LoadingDock>)(object)property.LoadingDocks)
		{
			improvedPackagers.LoadingBayPrefs.Add(improvedPackagers.PropertyGroup.CreateEntry<EDirection>(item.Name.Replace(" ", ""), EDirection.Dual_Direction, item.Name, (string)null, false, false, (ValueValidator)null, (string)null));
		}
		return improvedPackagers;
	}

	public static void AddProperty(Property property)
	{
		if (property.LoadingDockCount != 0 && !AllProperties.TryGetValue(property.PropertyName, out var _))
		{
			AllProperties.Add(property.PropertyName, AddLoadingDocks(property));
		}
	}

	public static EDirection GetDockStatus(string property, string dock)
	{
		if (AllProperties.TryGetValue(property, out var value))
		{
			foreach (MelonPreferences_Entry<EDirection> loadingBayPref in value.LoadingBayPrefs)
			{
				if (((MelonPreferences_Entry)loadingBayPref).DisplayName == dock)
				{
					return loadingBayPref.Value;
				}
			}
		}
		return EDirection.Dual_Direction;
	}
}
public static class ReflectionHelper
{
	public static MethodInfo SetOccupant;

	private static EventInfo _event;

	private static Delegate _handler;

	private static bool _hooked;

	private static readonly BindingFlags _flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

	public static void Initialize()
	{
		if (_hooked)
		{
			return;
		}
		try
		{
			SetOccupant = AccessTools.Method(typeof(LoadingDock), "SetOccupant", (Type[])null, (Type[])null);
			Type type = (from a in AppDomain.CurrentDomain.GetAssemblies()
				select a.GetType("ModManagerPhoneApp.ModSettingsEvents")).FirstOrDefault((Type t) => t != null);
			if (!(type == null))
			{
				_event = type.GetEvent("OnPreferencesSaved", _flags);
				if (!(_event == null))
				{
					_handler = Delegate.CreateDelegate(typeof(Action), typeof(ReflectionHelper).GetMethod("OnPrefsSaved", _flags));
					_event.AddEventHandler(null, _handler);
					_hooked = true;
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Failed to initialize Mod Manager auto refresh " + ex.Message);
		}
	}

	public static void Deinitialize()
	{
		try
		{
			if (_hooked && _event != null && (object)_handler != null)
			{
				_event.RemoveEventHandler(null, _handler);
			}
		}
		catch
		{
		}
		finally
		{
			_hooked = false;
			_event = null;
			_handler = null;
		}
	}

	private static void OnPrefsSaved()
	{
		try
		{
			Enumerator<Property> enumerator = Property.OwnedProperties.GetEnumerator();
			while (enumerator.MoveNext())
			{
				foreach (LoadingDock item in (Il2CppArrayBase<LoadingDock>)(object)enumerator.Current.LoadingDocks)
				{
					SetOccupant.Invoke(item, new object[1]);
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("OnPrefsSaved error " + ex.Message);
		}
	}
}