Decompiled source of LethalSuits v0.1.0

LethalSharedSuits.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LethalSharedSuits")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LethalSharedSuits")]
[assembly: AssemblyTitle("LethalSharedSuits")]
[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 LethalSharedSuits
{
	[BepInPlugin("pattyjogal.LethalSuits", "Lethal Suits", "0.1.0")]
	public class MoreSuitsMod : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(StartOfRound))]
		internal class StartOfRoundPatch
		{
			private static UnlockableItem GetOriginalSuit(ref StartOfRound __instance)
			{
				return __instance.unlockablesList.unlockables.First((UnlockableItem unlockable) => (Object)(object)unlockable.suitMaterial != (Object)null && unlockable.alreadyUnlocked);
			}

			private static List<string> GetSuitsFromConfig(string configPath)
			{
				return File.ReadAllText(configPath).Split(new string[1] { Environment.NewLine }, StringSplitOptions.None).ToList();
			}

			private static List<string> GetUninstalledSuits(List<string> allSuits)
			{
				return allSuits.Except(Directory.GetDirectories(suitsBasePath)).ToList();
			}

			private static void DownloadSuit(string suit)
			{
				string address = "https://lethal-suits.nyc3.digitaloceanspaces.com/" + suit + ".zip";
				string text = Path.Combine(suitsBasePath, suit + ".zip");
				string destinationDirectoryName = Path.Combine(suitsBasePath, suit);
				try
				{
					using (WebClient webClient = new WebClient())
					{
						webClient.DownloadFile(address, Path.Combine(suitsBasePath, text));
					}
					ZipFile.ExtractToDirectory(text, destinationDirectoryName);
				}
				catch (WebException ex)
				{
					Debug.Log((object)("Error downloading the ZIP file: " + ex.Message));
				}
				catch (IOException ex2)
				{
					Debug.Log((object)("Error extracting the ZIP file: " + ex2.Message));
				}
			}

			[HarmonyPatch("Start")]
			[HarmonyPrefix]
			private static void StartPatch(ref StartOfRound __instance)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0018: Expected O, but got Unknown
				//IL_068b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0690: Unknown result type (might be due to invalid IL or missing references)
				//IL_0697: Unknown result type (might be due to invalid IL or missing references)
				//IL_069c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0701: Unknown result type (might be due to invalid IL or missing references)
				//IL_0706: Unknown result type (might be due to invalid IL or missing references)
				//IL_070c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0711: Unknown result type (might be due to invalid IL or missing references)
				//IL_0401: Unknown result type (might be due to invalid IL or missing references)
				//IL_0408: Expected O, but got Unknown
				//IL_0318: Unknown result type (might be due to invalid IL or missing references)
				//IL_031f: Expected O, but got Unknown
				//IL_05a0: Unknown result type (might be due to invalid IL or missing references)
				//IL_0248: Unknown result type (might be due to invalid IL or missing references)
				//IL_024f: Expected O, but got Unknown
				int count = __instance.unlockablesList.unlockables.Count;
				UnlockableItem val = new UnlockableItem();
				try
				{
					if (!Directory.Exists(configBasePath))
					{
						Directory.CreateDirectory(configBasePath);
					}
					if (!Directory.Exists(modBasePath))
					{
						Directory.CreateDirectory(modBasePath);
					}
					if (!Directory.Exists(suitsBasePath))
					{
						Directory.CreateDirectory(suitsBasePath);
					}
					if (!Directory.Exists(friendsSuitsBasePath))
					{
						Directory.CreateDirectory(friendsSuitsBasePath);
					}
					if (!File.Exists(Path.Combine(configBasePath, "suits.txt")))
					{
						File.Create(Path.Combine(configBasePath, "suits.txt"));
					}
					if (!SuitsAdded)
					{
						int num = 0;
						val = GetOriginalSuit(ref __instance);
						string[] files = Directory.GetFiles(friendsSuitsBasePath);
						List<string> list = new List<string>(files.Length);
						string[] array = files;
						foreach (string item in array)
						{
							list.Add(item);
						}
						list.Add(Path.Combine(configBasePath, "suits.txt"));
						List<string> list2 = list.SelectMany(GetSuitsFromConfig).ToList();
						List<string> uninstalledSuits = GetUninstalledSuits(list2);
						uninstalledSuits.ForEach((Action<string>)Debug.Log);
						uninstalledSuits.ForEach(DownloadSuit);
						List<string> list3 = new List<string>();
						List<string> list4 = new List<string>();
						foreach (string item3 in list2.Select((string suit) => Path.Combine(suitsBasePath, suit)))
						{
							if (item3 != "")
							{
								string[] files2 = Directory.GetFiles(item3, "*.png");
								string[] files3 = Directory.GetFiles(item3, "*.matbundle");
								list3.AddRange(files2);
								list4.AddRange(files3);
							}
						}
						list4.Sort();
						list3.Sort();
						try
						{
							foreach (string item4 in list4)
							{
								Object[] array2 = AssetBundle.LoadFromFile(item4).LoadAllAssets();
								foreach (Object val2 in array2)
								{
									if (val2 is Material)
									{
										Material item2 = (Material)val2;
										customMaterials.Add(item2);
									}
								}
							}
						}
						catch (Exception ex)
						{
							Debug.Log((object)("Something went wrong with More Suits! Could not load materials from asset bundle(s). Error: " + ex));
						}
						foreach (string item5 in list3)
						{
							Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
							UnlockableItem val3;
							Material val4;
							if (Path.GetFileNameWithoutExtension(item5).ToLower() == "default")
							{
								val3 = val;
								val4 = val3.suitMaterial;
							}
							else
							{
								val3 = JsonUtility.FromJson<UnlockableItem>(JsonUtility.ToJson((object)val));
								val4 = Object.Instantiate<Material>(val3.suitMaterial);
							}
							byte[] array3 = File.ReadAllBytes(item5);
							Texture2D val5 = new Texture2D(2, 2);
							ImageConversion.LoadImage(val5, array3);
							val4.mainTexture = (Texture)(object)val5;
							val3.unlockableName = Path.GetFileNameWithoutExtension(item5);
							try
							{
								string path = Path.Combine(Path.GetDirectoryName(item5), "advanced", val3.unlockableName + ".json");
								if (File.Exists(path))
								{
									array = File.ReadAllLines(path);
									for (int i = 0; i < array.Length; i++)
									{
										string[] array4 = array[i].Trim().Split(':');
										if (array4.Length != 2)
										{
											continue;
										}
										string text = array4[0].Trim('"', ' ', ',');
										string text2 = array4[1].Trim('"', ' ', ',');
										if (text2.Contains(".png"))
										{
											byte[] array5 = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(item5), "advanced", text2));
											Texture2D val6 = new Texture2D(2, 2);
											ImageConversion.LoadImage(val6, array5);
											val4.SetTexture(text, (Texture)(object)val6);
											continue;
										}
										if (text == "PRICE" && int.TryParse(text2, out var result))
										{
											try
											{
												val3 = AddToRotatingShop(val3, result, __instance.unlockablesList.unlockables.Count);
											}
											catch (Exception ex2)
											{
												Debug.Log((object)("Something went wrong with More Suits! Could not add a suit to the rotating shop. Error: " + ex2));
											}
											continue;
										}
										switch (text2)
										{
										case "KEYWORD":
											val4.EnableKeyword(text);
											continue;
										case "DISABLEKEYWORD":
											val4.DisableKeyword(text);
											continue;
										case "SHADERPASS":
											val4.SetShaderPassEnabled(text, true);
											continue;
										case "DISABLESHADERPASS":
											val4.SetShaderPassEnabled(text, false);
											continue;
										}
										float result2;
										Vector4 vector;
										if (text == "SHADER")
										{
											Shader shader = Shader.Find(text2);
											val4.shader = shader;
										}
										else if (text == "MATERIAL")
										{
											foreach (Material customMaterial in customMaterials)
											{
												if (((Object)customMaterial).name == text2)
												{
													val4 = Object.Instantiate<Material>(customMaterial);
													val4.mainTexture = (Texture)(object)val5;
													break;
												}
											}
										}
										else if (float.TryParse(text2, out result2))
										{
											val4.SetFloat(text, result2);
										}
										else if (TryParseVector4(text2, out vector))
										{
											val4.SetVector(text, vector);
										}
									}
								}
							}
							catch (Exception ex3)
							{
								Debug.Log((object)("Something went wrong with More Suits! Error: " + ex3));
							}
							val3.suitMaterial = val4;
							if (!(val3.unlockableName.ToLower() != "default"))
							{
								continue;
							}
							if (num == MaxSuits)
							{
								Debug.Log((object)"Attempted to add a suit, but you've already reached the max number of suits! Modify the config if you want more.");
								continue;
							}
							bool flag = GetSuitsFromConfig(Path.Combine(configBasePath, "suits.txt")).Contains(val3.unlockableName);
							Debug.Log((object)string.Format("Truth for {0}: {1}", val3.unlockableName, GetSuitsFromConfig(Path.Combine(configBasePath, "suits.txt")).Contains(val3.unlockableName)));
							val3.alreadyUnlocked = flag;
							val3.hasBeenUnlockedByPlayer = flag;
							if (!flag)
							{
								val3.placedPosition = Vector3.zero;
								val3.placedRotation = Vector3.zero;
								val3.unlockableType = 753;
							}
							__instance.unlockablesList.unlockables.Add(val3);
							num++;
						}
						SuitsAdded = true;
					}
					UnlockableItem val7 = JsonUtility.FromJson<UnlockableItem>(JsonUtility.ToJson((object)val));
					val7.alreadyUnlocked = false;
					val7.hasBeenMoved = false;
					val7.placedPosition = Vector3.zero;
					val7.placedRotation = Vector3.zero;
					val7.unlockableType = 753;
					while (__instance.unlockablesList.unlockables.Count < count + MaxSuits)
					{
						__instance.unlockablesList.unlockables.Add(val7);
					}
				}
				catch (Exception ex4)
				{
					Debug.Log((object)("Something went wrong with More Suits! Error: " + ex4));
				}
			}

			[HarmonyPatch("PositionSuitsOnRack")]
			[HarmonyPrefix]
			private static bool PositionSuitsOnRackPatch(ref StartOfRound __instance)
			{
				//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
				//IL_0106: Unknown result type (might be due to invalid IL or missing references)
				//IL_010d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0112: Unknown result type (might be due to invalid IL or missing references)
				//IL_0117: Unknown result type (might be due to invalid IL or missing references)
				//IL_012b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0130: Unknown result type (might be due to invalid IL or missing references)
				GetSuitsFromConfig(Path.Combine(configBasePath, "suits.txt"));
				List<UnlockableSuit> source = Object.FindObjectsOfType<UnlockableSuit>().ToList();
				source = source.OrderBy((UnlockableSuit suit) => suit.syncedSuitID.Value).ToList();
				int num = 0;
				source.ForEach(delegate(UnlockableSuit suit)
				{
					Debug.Log((object)$"XXXXXXXXXXXXXXXXXXXX {suit}");
				});
				foreach (UnlockableSuit item in source)
				{
					Debug.Log((object)$"Placing {item.suitID} suit");
					AutoParentToShip component = ((Component)item).gameObject.GetComponent<AutoParentToShip>();
					component.overrideOffset = true;
					float num2 = 0.18f;
					if (MakeSuitsFitOnRack && source.Count > 13)
					{
						num2 /= (float)Math.Min(source.Count, 20) / 12f;
					}
					component.positionOffset = new Vector3(-2.45f, 2.75f, -8.41f) + __instance.rightmostSuitPosition.forward * num2 * (float)num;
					component.rotationOffset = new Vector3(0f, 90f, 0f);
					num++;
				}
				return false;
			}
		}

		private const string modGUID = "pattyjogal.LethalSuits";

		private const string modName = "Lethal Suits";

		private const string modVersion = "0.1.0";

		private static readonly string configBasePath = Path.Combine(Paths.ConfigPath, "suits");

		private static readonly string modBasePath = Path.Combine(Paths.PluginPath, "lethal-suits");

		private static readonly string suitsBasePath = Path.Combine(modBasePath, "suits");

		private static readonly string friendsSuitsBasePath = Path.Combine(configBasePath, "friends");

		private readonly Harmony harmony = new Harmony("pattyjogal.LethalSuits");

		private static MoreSuitsMod Instance;

		public static bool SuitsAdded = false;

		public static string DisabledSuits;

		public static bool LoadAllSuits;

		public static bool MakeSuitsFitOnRack;

		public static int MaxSuits;

		public static List<Material> customMaterials = new List<Material>();

		private static TerminalNode cancelPurchase;

		private static TerminalKeyword buyKeyword;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			DisabledSuits = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Disabled Suit List", "UglySuit751.png,UglySuit752.png,UglySuit753.png", "Comma-separated list of suits that shouldn't be loaded").Value;
			LoadAllSuits = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ignore !less-suits.txt", false, "If true, ignores the !less-suits.txt file and will attempt to load every suit, except those in the disabled list. This should be true if you're not worried about having too many suits.").Value;
			MakeSuitsFitOnRack = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Make Suits Fit on Rack", true, "If true, squishes the suits together so more can fit on the rack.").Value;
			MaxSuits = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Max Suits", 100, "The maximum number of suits to load. If you have more, some will be ignored.").Value;
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Lethal Suits is loaded!");
		}

		private static UnlockableItem AddToRotatingShop(UnlockableItem newSuit, int price, int unlockableID)
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Expected O, but got Unknown
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Expected O, but got Unknown
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Expected O, but got Unknown
			Terminal val = Object.FindObjectOfType<Terminal>();
			for (int i = 0; i < val.terminalNodes.allKeywords.Length; i++)
			{
				if (((Object)val.terminalNodes.allKeywords[i]).name == "Buy")
				{
					buyKeyword = val.terminalNodes.allKeywords[i];
					break;
				}
			}
			newSuit.alreadyUnlocked = false;
			newSuit.hasBeenMoved = false;
			newSuit.placedPosition = Vector3.zero;
			newSuit.placedRotation = Vector3.zero;
			newSuit.shopSelectionNode = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)newSuit.shopSelectionNode).name = newSuit.unlockableName + "SuitBuy1";
			newSuit.shopSelectionNode.creatureName = newSuit.unlockableName + " suit";
			newSuit.shopSelectionNode.displayText = "You have requested to order " + newSuit.unlockableName + " suits.\nTotal cost of item: [totalCost].\n\nPlease CONFIRM or DENY.\n\n";
			newSuit.shopSelectionNode.clearPreviousText = true;
			newSuit.shopSelectionNode.shipUnlockableID = unlockableID;
			newSuit.shopSelectionNode.itemCost = price;
			newSuit.shopSelectionNode.overrideOptions = true;
			CompatibleNoun val2 = new CompatibleNoun();
			val2.noun = ScriptableObject.CreateInstance<TerminalKeyword>();
			val2.noun.word = "confirm";
			val2.noun.isVerb = true;
			val2.result = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val2.result).name = newSuit.unlockableName + "SuitBuyConfirm";
			val2.result.creatureName = "";
			val2.result.displayText = "Ordered " + newSuit.unlockableName + " suits! Your new balance is [playerCredits].\n\n";
			val2.result.clearPreviousText = true;
			val2.result.shipUnlockableID = unlockableID;
			val2.result.buyUnlockable = true;
			val2.result.itemCost = price;
			val2.result.terminalEvent = "";
			CompatibleNoun val3 = new CompatibleNoun();
			val3.noun = ScriptableObject.CreateInstance<TerminalKeyword>();
			val3.noun.word = "deny";
			val3.noun.isVerb = true;
			if ((Object)(object)cancelPurchase == (Object)null)
			{
				cancelPurchase = ScriptableObject.CreateInstance<TerminalNode>();
			}
			val3.result = cancelPurchase;
			((Object)val3.result).name = "MoreSuitsCancelPurchase";
			val3.result.displayText = "Cancelled order.\n";
			newSuit.shopSelectionNode.terminalOptions = (CompatibleNoun[])(object)new CompatibleNoun[2] { val2, val3 };
			TerminalKeyword val4 = ScriptableObject.CreateInstance<TerminalKeyword>();
			((Object)val4).name = newSuit.unlockableName + "Suit";
			val4.word = newSuit.unlockableName.ToLower() + " suit";
			val4.defaultVerb = buyKeyword;
			CompatibleNoun val5 = new CompatibleNoun();
			val5.noun = val4;
			val5.result = newSuit.shopSelectionNode;
			List<CompatibleNoun> list = buyKeyword.compatibleNouns.ToList();
			list.Add(val5);
			buyKeyword.compatibleNouns = list.ToArray();
			List<TerminalKeyword> list2 = val.terminalNodes.allKeywords.ToList();
			list2.Add(val4);
			list2.Add(val2.noun);
			list2.Add(val3.noun);
			val.terminalNodes.allKeywords = list2.ToArray();
			return newSuit;
		}

		public static bool TryParseVector4(string input, out Vector4 vector)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			vector = Vector4.zero;
			string[] array = input.Split(',');
			if (array.Length == 4 && float.TryParse(array[0], out var result) && float.TryParse(array[1], out var result2) && float.TryParse(array[2], out var result3) && float.TryParse(array[3], out var result4))
			{
				vector = new Vector4(result, result2, result3, result4);
				return true;
			}
			return false;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "LethalSharedSuits";

		public const string PLUGIN_NAME = "LethalSharedSuits";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}