Decompiled source of SuitSaver v1.2.1

Suit Saver.dll

Decompiled 4 months ago
using System.Collections;
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 GameNetcodeStuff;
using HarmonyLib;
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("Suit Saver")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Suit Saver")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cb7cfb30-b06e-4e41-9de7-03640e1662ea")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SuitSaver
{
	[BepInPlugin("Hexnet.lethalcompany.suitsaver", "Suit Saver", "1.2.1")]
	public class SuitSaver : BaseUnityPlugin
	{
		private const string modGUID = "Hexnet.lethalcompany.suitsaver";

		private const string modName = "Suit Saver";

		private const string modVersion = "1.2.1";

		private readonly Harmony harmony = new Harmony("Hexnet.lethalcompany.suitsaver");

		private void Awake()
		{
			harmony.PatchAll();
			Debug.Log((object)"[SS]: Suit Saver loaded successfully!");
		}
	}
}
namespace SuitSaver.Patches
{
	[HarmonyPatch]
	internal class Patches
	{
		[HarmonyPatch(typeof(StartOfRound))]
		internal class StartPatch
		{
			private static bool ReloadSuit;

			[HarmonyPatch("playersFiredGameOver")]
			[HarmonyPrefix]
			private static void PurchasedSuitCheck()
			{
				string text = LoadFromFile();
				if (text != "-1")
				{
					ReloadSuit = !IsPurchasedSuit(text);
				}
			}

			[HarmonyPatch("ResetShip")]
			[HarmonyPostfix]
			private static void ResetShipPatch()
			{
				if (!ReloadSuit)
				{
					string text = LoadFromFile();
					if (text != "-1")
					{
						Debug.Log((object)("[SS]: Could not reload suit upon ship reset. Perhaps it's locked? (" + text + ")"));
					}
				}
				else
				{
					Debug.Log((object)"[SS]: Ship has been reset!");
					Debug.Log((object)"[SS]: Reloading suit...");
					LoadSuitFromFile();
					ReloadSuit = false;
				}
			}
		}

		[HarmonyPatch(typeof(UnlockableSuit))]
		internal class SuitPatch
		{
			[HarmonyPatch("SwitchSuitClientRpc")]
			[HarmonyPostfix]
			private static void SyncSuit(ref UnlockableSuit __instance, int playerID)
			{
				PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
				int num = (int)localPlayerController.playerClientId;
				if (playerID != num)
				{
					UnlockableSuit.SwitchSuitForPlayer(StartOfRound.Instance.allPlayerScripts[playerID], __instance.syncedSuitID.Value, true);
				}
			}

			[HarmonyPatch("SwitchSuitToThis")]
			[HarmonyPostfix]
			private static void EquipSuitPatch()
			{
				PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
				string unlockableName = StartOfRound.Instance.unlockablesList.unlockables[localPlayerController.currentSuitID].unlockableName;
				SaveToFile(unlockableName);
				Debug.Log((object)("[SS]: Successfully saved current suit. (" + unlockableName + ")"));
			}
		}

		[HarmonyPatch(typeof(PlayerControllerB))]
		internal class JoinGamePatch
		{
			[HarmonyPatch("ConnectClientToPlayerObject")]
			[HarmonyPostfix]
			private static void LoadSuitPatch(ref PlayerControllerB __instance)
			{
				((Component)GameNetworkManager.Instance.localPlayerController).gameObject.AddComponent<EquipAfterSyncPatch>();
			}
		}

		internal class EquipAfterSyncPatch : MonoBehaviour
		{
			private void Start()
			{
				((MonoBehaviour)this).StartCoroutine(LoadSuit());
			}

			private IEnumerator LoadSuit()
			{
				Debug.Log((object)"[SS]: Waiting for suits to sync...");
				string SavedSuit = LoadFromFile();
				int success = -1;
				for (int i = 0; i < 3; i++)
				{
					success = LoadSuitStartup(SavedSuit);
					if (success <= 0)
					{
						if (success == 0)
						{
							Debug.Log((object)("[SS]: Failed to load saved suit. Perhaps it's locked? (" + SavedSuit + ")"));
						}
						break;
					}
					yield return (object)new WaitForSeconds(1f);
				}
				if (success == 1)
				{
					Debug.Log((object)("[SS]: Successfully loaded saved suit. (" + SavedSuit + ")"));
				}
			}
		}

		public static string SavePath = Application.persistentDataPath + "\\suitsaver.txt";

		private static void SaveToFile(string suitName)
		{
			File.WriteAllText(SavePath, suitName);
		}

		private static string LoadFromFile()
		{
			if (File.Exists(SavePath))
			{
				return File.ReadAllText(SavePath);
			}
			return "-1";
		}

		private static UnlockableSuit GetSuitByName(string Name)
		{
			List<UnlockableItem> unlockables = StartOfRound.Instance.unlockablesList.unlockables;
			UnlockableSuit[] array = Resources.FindObjectsOfTypeAll<UnlockableSuit>();
			foreach (UnlockableSuit val in array)
			{
				if (val.syncedSuitID.Value >= 0)
				{
					string unlockableName = unlockables[val.syncedSuitID.Value].unlockableName;
					if (unlockableName == Name)
					{
						return val;
					}
				}
			}
			return null;
		}

		private static bool IsPurchasedSuit(string Name)
		{
			List<UnlockableItem> unlockables = StartOfRound.Instance.unlockablesList.unlockables;
			UnlockableSuit[] array = Resources.FindObjectsOfTypeAll<UnlockableSuit>();
			foreach (UnlockableSuit val in array)
			{
				if (val.syncedSuitID.Value >= 0)
				{
					UnlockableItem val2 = unlockables[val.syncedSuitID.Value];
					string unlockableName = val2.unlockableName;
					if (unlockableName == Name)
					{
						return !val2.alreadyUnlocked && val2.hasBeenUnlockedByPlayer;
					}
				}
			}
			return false;
		}

		private static void LoadSuitFromFile()
		{
			string text = LoadFromFile();
			PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
			if (!(text == "-1"))
			{
				UnlockableSuit suitByName = GetSuitByName(text);
				if ((Object)(object)suitByName != (Object)null)
				{
					UnlockableSuit.SwitchSuitForPlayer(localPlayerController, suitByName.syncedSuitID.Value, false);
					suitByName.SwitchSuitServerRpc((int)localPlayerController.playerClientId);
					Debug.Log((object)("[SS]: Successfully loaded saved suit. (" + text + ")"));
				}
				else
				{
					Debug.Log((object)("[SS]: Failed to load saved suit. Perhaps it's locked? (" + text + ")"));
				}
			}
		}

		private static int LoadSuitStartup(string SavedSuit)
		{
			PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
			if (SavedSuit == "-1")
			{
				return -1;
			}
			UnlockableSuit suitByName = GetSuitByName(SavedSuit);
			if ((Object)(object)suitByName != (Object)null)
			{
				UnlockableSuit.SwitchSuitForPlayer(localPlayerController, suitByName.syncedSuitID.Value, false);
				suitByName.SwitchSuitServerRpc((int)localPlayerController.playerClientId);
				return 1;
			}
			return 0;
		}
	}
}