Decompiled source of SmarterRail v0.0.3

plugins/SmarterRail/SmarterRail.dll

Decompiled 10 months 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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("RightDrag")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RightDrag")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("64443039-edc2-4adb-8193-a459b7477663")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.2.0")]
namespace RightDrag;

[BepInPlugin("SmarterTransit.nhickling.co.uk", "SmarterTransit", "0.0.0.3")]
public class SmarterTransit : BaseUnityPlugin
{
	public const string pluginGuid = "SmarterTransit.nhickling.co.uk";

	public const string pluginName = "SmarterTransit";

	public const string pluginVersion = "0.0.0.3";

	private static ManualLogSource ModLogger;

	private static List<int> activatedSlots = new List<int>();

	public static ConfigEntry<int> EmptySlotsRequired;

	public static ConfigEntry<int> MinimumLoadPercentage;

	public void Awake()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Expected O, but got Unknown
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Expected O, but got Unknown
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Expected O, but got Unknown
		//IL_0165: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Expected O, but got Unknown
		//IL_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a2: Expected O, but got Unknown
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Expected O, but got Unknown
		((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: started");
		Harmony val = new Harmony("SmarterTransit.nhickling.co.uk");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: Fetching patch references");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Patching Outbound");
		MethodInfo methodInfo = AccessTools.Method(typeof(TransitDepotInstance), "StartPacking", new Type[0], (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(SmarterTransit), "StartPacking_MyPatch", (Type[])null, (Type[])null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: Starting Patch");
		val.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: Patched");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Patching Inbound");
		MethodInfo methodInfo3 = AccessTools.GetDeclaredMethods(typeof(TransitDepotInstance)).First((MethodInfo method) => method.Name == "ValidateAddResources" && !method.IsStatic);
		MethodInfo methodInfo4 = AccessTools.Method(typeof(SmarterTransit), "ValidateAddResources_MyPatch", (Type[])null, (Type[])null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: Starting Patch");
		val.Patch((MethodBase)methodInfo3, new HarmonyMethod(methodInfo4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SmarterTransit: Patched");
		EmptySlotsRequired = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "EmptySlotsRequired", 50, new ConfigDescription("Number of EMPTY slots required at the destination before a pack can be sent. (Currently Disabled)", (AcceptableValueBase)new AcceptableValueRange<int>(1, 55), Array.Empty<object>()));
		MinimumLoadPercentage = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "MinimumLoadPercentage", 100, new ConfigDescription("Minimum percentage of items before a pack will be sent. (0 to always send, 100 to only send full carts)", (AcceptableValueBase)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
		ModLogger = ((BaseUnityPlugin)this).Logger;
	}

	public static bool StartPacking_MyPatch(TransitDepotInstance __instance)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		if (__instance.hasPair)
		{
			ref TransitDepotInstance reference = ref __instance.pairedDepot.Get();
			ref Inventory inputInventory = ref ((TransitDepotInstance)(ref __instance)).GetInputInventory();
			ref Inventory outputInventory = ref ((TransitDepotInstance)(ref reference)).GetOutputInventory();
			float num = ((TransitDepotInstance)(ref __instance)).maxCapacity;
			int value = MinimumLoadPercentage.Value;
			if (!(Math.Ceiling((float)((Inventory)(ref inputInventory)).GetTotalResourceCount() / num * 100f) >= (double)value))
			{
				return false;
			}
			if (((Inventory)(ref outputInventory)).GetNumberOfEmptySlots() < EmptySlotsRequired.Value)
			{
				return false;
			}
		}
		return true;
	}

	public static bool ValidateAddResources_MyPatch(TransitDepotInstance __instance, ref bool __result, int resId, int inventoryIndex, out int slotNum, AddResourceValidationType validationType)
	{
		ref Inventory inputInventory = ref ((TransitDepotInstance)(ref __instance)).GetInputInventory();
		slotNum = -1;
		if (((Inventory)(ref inputInventory)).GetNumberOfEmptySlots() < EmptySlotsRequired.Value)
		{
			__result = false;
			return false;
		}
		return true;
	}
}