Decompiled source of PackagersLoadVehicles v1.0.0

Alternate-PackagersLoadVehicles.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using HarmonyLib;
using MelonLoader;
using MelonLoader.Preferences;
using PackagersLoadVehicles;
using ScheduleOne.Delivery;
using ScheduleOne.ItemFramework;
using ScheduleOne.Property;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(LoadVehicles), "Packagers Load Vehicles", "1.0.0", "GuysWeForgotDre", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: AssemblyTitle("Packagers Load Vehicles")]
[assembly: AssemblyDescription("Enables Schedule I Packagers to load vehicles parked in Loading Bays")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Packagers Load Vehicles")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PackagersLoadVehicles;

public enum EDirection
{
	Dual_Direction,
	Unload_Only,
	Load_Only
}
public class LoadVehicles : MelonMod
{
	public const string ModName = "Packagers Load Vehicles";

	public const string Version = "1.0.0";

	public const string ModDesc = "Enables Schedule I Packagers to 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, LoadVehicles> AllProperties = new Dictionary<string, LoadVehicles>();

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

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

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

	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
{
	private static bool _hooked;

	private static EventInfo _event;

	private static Delegate _handler;

	public static MethodInfo SetOccupant;

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

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

	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 GetLoadingDockMethods()
	{
		SetOccupant = AccessTools.Method(typeof(LoadingDock), "SetOccupant", (Type[])null, (Type[])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);
		}
	}
}
[HarmonyPatch(typeof(LoadingDock), "SetOccupant")]
public class LoadingDockSetOccupantPatch
{
	private static void Postfix(LoadingDock __instance)
	{
		if (!((Object)(object)__instance.DynamicOccupant != (Object)null) || !LoadVehicles.GetDockStatus(__instance.ParentProperty.PropertyName, __instance.Name))
		{
			return;
		}
		foreach (ItemSlot itemSlot in __instance.DynamicOccupant.Storage.ItemSlots)
		{
			__instance.InputSlots.Add(itemSlot);
		}
	}
}
[HarmonyPatch(typeof(Property), "Start")]
public class PropertyStartPatch
{
	private static void Postfix(Property __instance)
	{
		LoadVehicles.AddProperty(__instance);
	}
}

Main-PackagersLoadVehicles.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppScheduleOne.Delivery;
using Il2CppScheduleOne.ItemFramework;
using Il2CppScheduleOne.Property;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using MelonLoader.Preferences;
using PackagersLoadVehicles;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(LoadVehicles), "Packagers Load Vehicles", "1.0.0", "GuysWeForgotDre", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: AssemblyTitle("Packagers Load Vehicles")]
[assembly: AssemblyDescription("Enables Schedule I Packagers to load vehicles parked in Loading Bays")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Packagers Load Vehicles")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PackagersLoadVehicles;

public enum EDirection
{
	Dual_Direction,
	Unload_Only,
	Load_Only
}
public class LoadVehicles : MelonMod
{
	public const string ModName = "Packagers Load Vehicles";

	public const string Version = "1.0.0";

	public const string ModDesc = "Enables Schedule I Packagers to 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, LoadVehicles> AllProperties = new Dictionary<string, LoadVehicles>();

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

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

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

	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;
					MelonLogger.Msg("Hooked ModManagerPhoneApp.OnPreferencesSaved");
				}
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Failed to initialize Mod Manager autuo 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);
		}
	}
}
[HarmonyPatch(typeof(LoadingDock), "SetOccupant")]
public class LoadingDockSetOccupantPatch
{
	private static void Postfix(LoadingDock __instance)
	{
		if (!((Object)(object)__instance.DynamicOccupant != (Object)null))
		{
			return;
		}
		EDirection dockStatus = LoadVehicles.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")]
public class PropertyStartPatch
{
	private static void Postfix(Property __instance)
	{
		LoadVehicles.AddProperty(__instance);
	}
}