Decompiled source of FasterItemDropship v1.3.1

FasterItemDropship.dll

Decompiled 4 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FasterItemDropship")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("Mod made by flipf17")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FasterItemDropship")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a5a250fd-b706-48b9-9be9-da360fd939dc")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace FasterItemDropship
{
	public static class ConfigSettings
	{
		public static ConfigEntry<int> dropshipDeliveryTime;

		public static ConfigEntry<int> dropshipMaxStayDuration;

		public static ConfigEntry<int> dropshipLeaveAfterSecondsOpenDoors;

		public static ConfigEntry<bool> addItemsToDropshipDuringDelivery;

		public static ConfigEntry<bool> startDeliveryBeforePlayerShipLanded;

		public static Dictionary<string, ConfigEntryBase> currentConfigEntries = new Dictionary<string, ConfigEntryBase>();

		public static void BindConfigSettings()
		{
			Plugin.Log("BindingConfigs");
			dropshipDeliveryTime = AddConfigEntry<int>(((BaseUnityPlugin)Plugin.instance).Config.Bind<int>("General", "DeliveryTime", 10, "[Host only] How long it takes (in seconds) for the item dropship to arrive."));
			dropshipMaxStayDuration = AddConfigEntry<int>(((BaseUnityPlugin)Plugin.instance).Config.Bind<int>("General", "MaxLandDuration", 40, "[Host only] The max duration (in seconds) the item dropship will stay."));
			dropshipLeaveAfterSecondsOpenDoors = AddConfigEntry<int>(((BaseUnityPlugin)Plugin.instance).Config.Bind<int>("General", "LeaveAfterSecondsOpenDoors", 3, "[Host only] How long (in seconds) the item dropship will stay for after opening its doors."));
			addItemsToDropshipDuringDelivery = AddConfigEntry<bool>(((BaseUnityPlugin)Plugin.instance).Config.Bind<bool>("General", "AddItemsToDropshipDuringDelivery", true, "[Host only] If true, items purchased while a delivery is in progress, but before the item dropship has fully landed, will be added immediately."));
			startDeliveryBeforePlayerShipLanded = AddConfigEntry<bool>(((BaseUnityPlugin)Plugin.instance).Config.Bind<bool>("General", "StartDeliveryBeforePlayerShipLanded", true, "[Host only] If true, the dropship timer will start incrementing when the players start landing on the moon rather than after they finishing landing on the moon.\nDisable this if this causes any mod conflicts of any kind."));
		}

		public static ConfigEntry<T> AddConfigEntry<T>(ConfigEntry<T> configEntry)
		{
			currentConfigEntries.Add(((ConfigEntryBase)configEntry).Definition.Key, (ConfigEntryBase)(object)configEntry);
			return configEntry;
		}

		public static void TryRemoveOldConfigSettings()
		{
			HashSet<string> hashSet = new HashSet<string>();
			HashSet<string> hashSet2 = new HashSet<string>();
			foreach (ConfigEntryBase value in currentConfigEntries.Values)
			{
				hashSet.Add(value.Definition.Section);
				hashSet2.Add(value.Definition.Key);
			}
			try
			{
				ConfigFile config = ((BaseUnityPlugin)Plugin.instance).Config;
				string configFilePath = config.ConfigFilePath;
				if (!File.Exists(configFilePath))
				{
					return;
				}
				string text = File.ReadAllText(configFilePath);
				string[] array = File.ReadAllLines(configFilePath);
				string text2 = "";
				for (int i = 0; i < array.Length; i++)
				{
					array[i] = array[i].Replace("\n", "");
					if (array[i].Length <= 0)
					{
						continue;
					}
					if (array[i].StartsWith("["))
					{
						if (text2 != "" && !hashSet.Contains(text2))
						{
							text2 = "[" + text2 + "]";
							int num = text.IndexOf(text2);
							int num2 = text.IndexOf(array[i]);
							text = text.Remove(num, num2 - num);
						}
						text2 = array[i].Replace("[", "").Replace("]", "").Trim();
					}
					else
					{
						if (!(text2 != ""))
						{
							continue;
						}
						if (i <= array.Length - 4 && array[i].StartsWith("##"))
						{
							int j;
							for (j = 1; i + j < array.Length && array[i + j].Length > 3; j++)
							{
							}
							if (hashSet.Contains(text2))
							{
								int num3 = array[i + j - 1].IndexOf("=");
								string item = array[i + j - 1].Substring(0, num3 - 1);
								if (!hashSet2.Contains(item))
								{
									int num4 = text.IndexOf(array[i]);
									int num5 = text.IndexOf(array[i + j - 1]) + array[i + j - 1].Length;
									text = text.Remove(num4, num5 - num4);
								}
							}
							i += j - 1;
						}
						else if (array[i].Length > 3)
						{
							text = text.Replace(array[i], "");
						}
					}
				}
				if (!hashSet.Contains(text2))
				{
					text2 = "[" + text2 + "]";
					int num6 = text.IndexOf(text2);
					text = text.Remove(num6, text.Length - num6);
				}
				while (text.Contains("\n\n\n"))
				{
					text = text.Replace("\n\n\n", "\n\n");
				}
				File.WriteAllText(configFilePath, text);
				config.Reload();
			}
			catch
			{
			}
		}
	}
	[BepInPlugin("FlipMods.FasterItemDropship", "FasterItemDropship", "1.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		public static Plugin instance;

		private void Awake()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			instance = this;
			ConfigSettings.BindConfigSettings();
			_harmony = new Harmony("FasterItemDropship");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"FasterItemDropship loaded");
		}

		internal static void Log(string message)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)message);
		}

		internal static void LogWarning(string message)
		{
			((BaseUnityPlugin)instance).Logger.LogWarning((object)message);
		}

		internal static void LogError(string message)
		{
			((BaseUnityPlugin)instance).Logger.LogError((object)message);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "FlipMods.FasterItemDropship";

		public const string PLUGIN_NAME = "FasterItemDropship";

		public const string PLUGIN_VERSION = "1.3.1";
	}
}
namespace FasterItemDropship.Patches
{
	[HarmonyPatch]
	internal static class FasterItemDropshipPatcher
	{
		private static Terminal terminalScript;

		private static List<int> itemsToDeliver;

		private static float previousShipTimer = 0f;

		private static bool previousFirstOrder = true;

		[HarmonyPatch(typeof(ItemDropship), "Start")]
		[HarmonyPrefix]
		private static void InitializeDropship(ItemDropship __instance)
		{
			itemsToDeliver = (List<int>)Traverse.Create((object)__instance).Field("itemsToDeliver").GetValue();
		}

		[HarmonyPatch(typeof(Terminal), "Start")]
		[HarmonyPrefix]
		private static void InitializeTerminal(Terminal __instance)
		{
			terminalScript = __instance;
		}

		[HarmonyPatch(typeof(ItemDropship), "Update")]
		[HarmonyPrefix]
		private static void DropshipUpdatePrefix(ItemDropship __instance)
		{
			if (NetworkManager.Singleton.IsServer)
			{
				previousShipTimer = __instance.shipTimer;
				previousFirstOrder = __instance.playersFirstOrder;
				if (!__instance.deliveringOrder && (terminalScript.orderedItemsFromTerminal.Count > 0 || terminalScript.orderedVehicleFromTerminal != -1) && !StartOfRound.Instance.shipHasLanded && ConfigSettings.startDeliveryBeforePlayerShipLanded.Value)
				{
					__instance.shipTimer += Time.deltaTime;
				}
			}
		}

		[HarmonyPatch(typeof(ItemDropship), "Update")]
		[HarmonyPostfix]
		private static void DropshipUpdatePostfix(ItemDropship __instance)
		{
			if (NetworkManager.Singleton.IsServer && !__instance.deliveringOrder && (__instance.shipTimer < previousShipTimer || (previousFirstOrder && !__instance.playersFirstOrder)) && (terminalScript.orderedItemsFromTerminal.Count > 0 || terminalScript.orderedVehicleFromTerminal != -1))
			{
				__instance.shipTimer = 40 - ConfigSettings.dropshipDeliveryTime.Value;
			}
		}

		[HarmonyPatch(typeof(ItemDropship), "OpenShipDoorsOnServer")]
		[HarmonyPostfix]
		private static void OnOpenShipDoors(ItemDropship __instance)
		{
			if (NetworkManager.Singleton.IsServer)
			{
				__instance.shipTimer = Mathf.Max(__instance.shipTimer, (float)(30 - ConfigSettings.dropshipLeaveAfterSecondsOpenDoors.Value));
			}
		}

		[HarmonyPatch(typeof(ItemDropship), "ShipLandedAnimationEvent")]
		[HarmonyPostfix]
		private static void OnLandShipFinished(ItemDropship __instance)
		{
			if (!NetworkManager.Singleton.IsServer)
			{
				return;
			}
			__instance.shipTimer = 30 - ConfigSettings.dropshipMaxStayDuration.Value;
			if (ConfigSettings.addItemsToDropshipDuringDelivery.Value)
			{
				while (terminalScript.orderedItemsFromTerminal.Count > 0 && itemsToDeliver.Count < 12)
				{
					itemsToDeliver.Add(terminalScript.orderedItemsFromTerminal[0]);
					terminalScript.orderedItemsFromTerminal.RemoveAt(0);
				}
			}
		}

		[HarmonyPatch(typeof(ItemDropship), "ShipLeave")]
		[HarmonyPatch(typeof(ItemDropship), "FinishDeliveringVehicleOnServer")]
		[HarmonyPostfix]
		private static void OnShipLeave(ItemDropship __instance)
		{
			__instance.shipTimer = 40 - ConfigSettings.dropshipDeliveryTime.Value;
		}
	}
}