Decompiled source of DropshipItemRecovery v1.0.1

DropshipItemRecovery.dll

Decompiled a month 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.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DropshipItemRecovery")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("This mod solves the problem that occurs when the host player disconnects from the game after ordering items from the store in the terminal.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DropshipItemRecovery")]
[assembly: AssemblyTitle("DropshipItemRecovery")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 DropshipItemRecovery
{
	public static class BuyItemsServerRpcPatch
	{
		private static readonly string SavePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		[HarmonyPatch(typeof(Terminal), "LoadNewNodeIfAffordable")]
		[HarmonyPrefix]
		public static void BeforePurchases(Terminal __instance, out int __state)
		{
			__state = __instance.orderedItemsFromTerminal.Count;
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNodeIfAffordable")]
		[HarmonyPostfix]
		public static void SavePurchases(Terminal __instance, TerminalNode node, int __state)
		{
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null || !singleton.IsHost)
			{
				return;
			}
			int num = __instance.orderedItemsFromTerminal.Count - __state;
			if (node.buyItemIndex != -1 && num > 0)
			{
				List<int> list = new List<int>();
				for (int i = 0; i < num; i++)
				{
					list.Add(node.buyItemIndex);
				}
				List<int> list2 = LoadOrderedItemsFromFile();
				list2.AddRange(list);
				SaveOrderedItemsToFile(list2);
			}
		}

		[HarmonyPatch(typeof(Terminal), "BuyItemsServerRpc")]
		[HarmonyPrefix]
		public static void HandleClientPurchase(Terminal __instance, int[] boughtItems, int newGroupCredits, int numItemsInShip)
		{
			try
			{
				List<int> list = LoadOrderedItemsFromFile();
				list.AddRange(boughtItems);
				SaveOrderedItemsToFile(list);
			}
			catch (Exception arg)
			{
				Plugin.Instance.SendLog($"Failed to save purchases on host: {arg}");
			}
		}

		[HarmonyPatch(typeof(Terminal), "Awake")]
		[HarmonyPostfix]
		public static void AwakePost(Terminal __instance)
		{
			List<int> list = LoadOrderedItemsFromFile();
			__instance.orderedItemsFromTerminal.Clear();
			if (list.Count > 0)
			{
				__instance.numberOfItemsInDropship = list.Count;
				__instance.orderedItemsFromTerminal.AddRange(list);
			}
		}

		[HarmonyPatch(typeof(ItemDropship), "ShipLeave")]
		[HarmonyPostfix]
		public static void ShipLeavePatch(ItemDropship __instance)
		{
			try
			{
				string path = Path.Combine(SavePath, GameNetworkManager.Instance.currentSaveFileName + "_ordItems.dat");
				if (File.Exists(path))
				{
					File.Delete(path);
				}
			}
			catch (Exception ex)
			{
				Plugin.Instance.SendLog(ex.ToString());
			}
		}

		private static void SaveOrderedItemsToFile(List<int> orderedItems)
		{
			try
			{
				if (orderedItems.Count > 12)
				{
					int num = orderedItems.Count - 12;
					orderedItems.RemoveRange(orderedItems.Count - num, num);
					Plugin.Instance.SendLog("Count of ordered items is too big. Extra items were removed.");
				}
				string path = Path.Combine(SavePath, GameNetworkManager.Instance.currentSaveFileName + "_ordItems.dat");
				string contents = string.Join(",", orderedItems);
				File.WriteAllText(path, contents);
			}
			catch (Exception arg)
			{
				Plugin.Instance.SendLog($"Failed to save ordered items: {arg}");
			}
		}

		private static List<int> LoadOrderedItemsFromFile()
		{
			try
			{
				string path = Path.Combine(SavePath, GameNetworkManager.Instance.currentSaveFileName + "_ordItems.dat");
				if (!File.Exists(path))
				{
					Plugin.Instance.SendLog("File " + GameNetworkManager.Instance.currentSaveFileName + "_ordItems.dat not found, returning empty ordered list.");
					return new List<int>();
				}
				string text = File.ReadAllText(path);
				return text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
			}
			catch (Exception arg)
			{
				Plugin.Instance.SendLog($"Failed to load ordered items: {arg}");
				return new List<int>();
			}
		}
	}
	[BepInPlugin("DropshipItemRecovery", "DropshipItemRecovery", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			Instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"DropshipItemRecovery is successfully loaded!");
			try
			{
				Harmony val = new Harmony("DropshipItemRecovery");
				Harmony.CreateAndPatchAll(typeof(BuyItemsServerRpcPatch), (string)null);
				val.PatchAll();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Patches patched!");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		public void SendLog(string text)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("DropshipItemRecovery: " + text));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "DropshipItemRecovery";

		public const string PLUGIN_NAME = "DropshipItemRecovery";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}