Decompiled source of MoreCupboards v1.4.0

ScienceBird.MoreCupboards.dll

Decompiled 13 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalLib.Extras;
using LethalLib.Modules;
using LobbyCompatibility.Enums;
using LobbyCompatibility.Features;
using Microsoft.CodeAnalysis;
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("ScienceBird.MoreCupboards")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.4.0.0")]
[assembly: AssemblyInformationalVersion("1.4.0+a578d6baecc22e67b4a07d986a17712b1a479926")]
[assembly: AssemblyProduct("MoreCupboards")]
[assembly: AssemblyTitle("ScienceBird.MoreCupboards")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.4.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 MoreCupboards
{
	public class AddCupboards
	{
		public static List<UnlockableItemDef> cupboardUnlockables = new List<UnlockableItemDef>();

		private static readonly string[] colourNames = new string[5] { "Cupboard Orange", "Cupboard Yellow", "Cupboard Green", "Cupboard Blue", "Cupboard Purple" };

		private static int price = 300;

		public static void RegisterCupboards()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Expected O, but got Unknown
			if (MoreCupboards.cupboardPrice.Value >= 0)
			{
				price = MoreCupboards.cupboardPrice.Value;
			}
			TerminalNode val = (TerminalNode)MoreCupboards.CupboardAssets.LoadAsset("CupboardInfo");
			string text = "";
			if (MoreCupboards.noDoors.Value)
			{
				text = "Alt";
			}
			for (int i = 1; i <= MoreCupboards.maximumCupboards.Value; i++)
			{
				cupboardUnlockables.Add((UnlockableItemDef)MoreCupboards.CupboardAssets.LoadAsset("Cupboard" + i + text + "UnlockableItemDef"));
			}
			foreach (UnlockableItemDef cupboardUnlockable in cupboardUnlockables)
			{
				NetworkPrefabs.RegisterNetworkPrefab(cupboardUnlockable.unlockable.prefabObject);
				Unlockables.RegisterUnlockable(cupboardUnlockable, (StoreType)1, (TerminalNode)null, (TerminalNode)null, val, price);
			}
		}
	}
	public class LobbyCompatibility
	{
		public static void RegisterCompatibility()
		{
			PluginHelper.RegisterPlugin("ScienceBird.MoreCupboards", Version.Parse("1.4.0"), (CompatibilityLevel)2, (VersionStrictness)0);
		}
	}
	[BepInPlugin("ScienceBird.MoreCupboards", "MoreCupboards", "1.4.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class MoreCupboards : BaseUnityPlugin
	{
		public static AssetBundle CupboardAssets;

		public static PluginInfo pluginInfo;

		public static ConfigFile config;

		public static ConfigEntry<int> cupboardPrice;

		public static ConfigEntry<int> maximumCupboards;

		public static ConfigEntry<bool> noDoors;

		public static ConfigEntry<bool> separateCupboardEntries;

		public static ConfigEntry<bool> useColourNames;

		public static ConfigEntry<bool> autoParent;

		public static bool mattyPresent;

		public static bool mrovPresent;

		public static MoreCupboards Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			pluginInfo = ((BaseUnityPlugin)this).Info;
			cupboardPrice = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Cupboard Price", 250, "How much it costs to buy cupboards from the store.");
			maximumCupboards = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Maximum Cupboards", 5, new ConfigDescription("Maximum extra cupboards you're allowed to buy for your ship. This is in addition to the vanilla cupboard, so the actual number of cupboards you can have on the ship will be one higher than this.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			noDoors = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "No Doors", false, "Remove doors from purchased cupboards (does not affect vanilla cupboard).");
			separateCupboardEntries = ((BaseUnityPlugin)this).Config.Bind<bool>("Terminal", "Separate Cupboard Entries", false, "Rather than the shop listing a single cupboard, it and the storage menu will list numbered cupboards (1Cupboard, 2Cupboard, etc.) so you can request specific cupboards (if you're experiencing unusal behaviour in the terminal, try enabling this).");
			useColourNames = ((BaseUnityPlugin)this).Config.Bind<bool>("Terminal", "Use Colour Names", false, "If using the above separate entries option, instead of names like 1Cupboard and 2Cupboard, they will be named based on their colour, like Orange Cupboard and Yellow Cupboard.");
			autoParent = ((BaseUnityPlugin)this).Config.Bind<bool>("Technical", "Auto Parent", true, "If Matty Fixes is found, attempt to parent items to cupboards (only try turning this off if you're having any major errors or other issues with the mod).");
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			CupboardAssets = AssetBundle.LoadFromFile(Path.Combine(directoryName, "morecupboardsassets"));
			AddCupboards.RegisterCupboards();
			Patch();
			Logger.LogInfo((object)"ScienceBird.MoreCupboards v1.4.0 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("ScienceBird.MoreCupboards");
			}
			bool flag = Chainloader.PluginInfos.ContainsKey("BMX.LobbyCompatibility");
			mattyPresent = Chainloader.PluginInfos.ContainsKey("mattymatty.MattyFixes");
			mrovPresent = Chainloader.PluginInfos.ContainsKey("TerminalFormatter");
			if (flag)
			{
				LobbyCompatibility.RegisterCompatibility();
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ScienceBird.MoreCupboards";

		public const string PLUGIN_NAME = "MoreCupboards";

		public const string PLUGIN_VERSION = "1.4.0";
	}
}
namespace MoreCupboards.Patches
{
	[HarmonyPatch]
	internal class CupboardPatches
	{
		[HarmonyPatch(typeof(StartOfRound), "SceneManager_OnLoadComplete1")]
		[HarmonyPostfix]
		private static void DestroyCupboardDoors(StartOfRound __instance, string sceneName)
		{
			if (!(sceneName == "SampleSceneRelay") || !MoreCupboards.noDoors.Value)
			{
				return;
			}
			GameObject val = GameObject.Find("StorageCloset");
			Animator[] componentsInChildren = val.GetComponentsInChildren<Animator>();
			Animator[] array = componentsInChildren;
			foreach (Animator val2 in array)
			{
				if (((Object)((Component)val2).gameObject).name == "Cube.000" || ((Object)((Component)val2).gameObject).name == "Cube.002")
				{
					Object.Destroy((Object)(object)((Component)val2).gameObject);
				}
			}
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNode")]
		[HarmonyPrefix]
		[HarmonyBefore(new string[] { "TerminalFormatter" })]
		private static void TerminalTextCheck(Terminal __instance, TerminalNode node, out bool __state)
		{
			__state = false;
			if ((!MoreCupboards.separateCupboardEntries.Value || MoreCupboards.useColourNames.Value) && node.displayText.Contains("[buyableItemsList]"))
			{
				__state = true;
			}
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNode")]
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		[HarmonyAfter(new string[] { "TerminalFormatter" })]
		private static void TerminalTextOverride(Terminal __instance, bool __state)
		{
			if (MoreCupboards.separateCupboardEntries.Value)
			{
				if (MoreCupboards.useColourNames.Value)
				{
					__instance.currentText = __instance.currentText.Replace("1Cupboard", "Orange Cupboard").Replace("2Cupboard", "Yellow Cupboard").Replace("3Cupboard", "Green Cupboard")
						.Replace("4Cupboard", "Blue Cupboard")
						.Replace("5Cupboard", "Purple Cupboard");
					__instance.screenText.text = __instance.currentText;
				}
				return;
			}
			string text = "";
			if (MoreCupboards.mrovPresent && __state)
			{
				text = " ";
			}
			for (int i = 1; i <= MoreCupboards.maximumCupboards.Value; i++)
			{
				string text2 = i + "Cupboard";
				if (__instance.currentText.Contains(text2))
				{
					__instance.currentText = __instance.currentText.Replace(text2, "Cupboard" + text);
				}
			}
			if (__state)
			{
				int num = __instance.currentText.Split("Cupboard").Length - 1;
				if (MoreCupboards.maximumCupboards.Value > 1 && __instance.currentText.IndexOf("Cupboard") > 0 && num > 1)
				{
					string text3 = __instance.currentText.Substring(__instance.currentText.IndexOf("Cupboard"));
					string text4 = text3.Substring(text3.IndexOf("\n") + 2);
					string text5 = text4.Substring(text4.LastIndexOf("Cupboard"));
					string text6 = text4.Substring(0, text5.IndexOf("\n") + text4.LastIndexOf("Cupboard") + 2);
					if (num == 2)
					{
						__instance.currentText = __instance.currentText.Remove(__instance.currentText.LastIndexOf(text6), text6.Length);
					}
					else
					{
						__instance.currentText = __instance.currentText.Replace(text6, "");
					}
				}
			}
			__instance.screenText.text = __instance.currentText;
		}

		[HarmonyPatch(typeof(Terminal), "OnSubmit")]
		[HarmonyPrefix]
		private static void CupboardCommandOverride(Terminal __instance)
		{
			string text = __instance.screenText.text.Substring(__instance.screenText.text.Length - __instance.textAdded).ToLower();
			if (MoreCupboards.separateCupboardEntries.Value)
			{
				if (MoreCupboards.useColourNames.Value && text.Contains("cupb"))
				{
					if (text.StartsWith("orange cupb"))
					{
						text = "1Cupboard";
					}
					else if (text.StartsWith("yellow cupb"))
					{
						text = "2Cupboard";
					}
					else if (text.StartsWith("green cupb"))
					{
						text = "3Cupboard";
					}
					else if (text.StartsWith("blue cupb"))
					{
						text = "4Cupboard";
					}
					else if (text.StartsWith("purple cupb"))
					{
						text = "5Cupboard";
					}
					__instance.screenText.text = __instance.screenText.text.Substring(0, __instance.screenText.text.Length - __instance.textAdded) + text;
				}
			}
			else
			{
				if (!(text == "cupboard") && text.IndexOf("cupb") != 0 && text.IndexOf("cupb") != 1)
				{
					return;
				}
				text = "cupboard";
				UnlockableItem[] array = StartOfRound.Instance.unlockablesList.unlockables.ToArray();
				int num = 0;
				int num2 = 0;
				for (int i = 1; i <= MoreCupboards.maximumCupboards.Value; i++)
				{
					string text2 = i + "Cupboard";
					UnlockableItem[] array2 = array;
					foreach (UnlockableItem val in array2)
					{
						if (val.unlockableName == "Cupboard" && val.inStorage)
						{
							num = -1;
							break;
						}
						if (val.unlockableName == text2 && val.inStorage)
						{
							num = i;
							break;
						}
					}
					if (num2 == 0 && num == 0)
					{
						UnlockableItem[] array3 = array;
						foreach (UnlockableItem val2 in array3)
						{
							if (val2.unlockableName == text2 && !val2.hasBeenUnlockedByPlayer)
							{
								num2 = i;
								break;
							}
						}
					}
					if (num != 0)
					{
						break;
					}
				}
				switch (num)
				{
				default:
					MoreCupboards.Logger.LogInfo((object)$"Retrieving cupboard{num}");
					text = num + text;
					break;
				case 0:
					if (num2 != 0)
					{
						MoreCupboards.Logger.LogInfo((object)$"Buying cupboard{num2}");
						text = num2 + text;
						break;
					}
					MoreCupboards.Logger.LogInfo((object)"Unable to find cupboards!");
					return;
				case -1:
					break;
				}
				if (num != -1)
				{
					__instance.screenText.text = __instance.screenText.text.Substring(0, __instance.screenText.text.Length - __instance.textAdded) + text;
				}
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "SceneManager_OnLoadComplete1")]
		[HarmonyPostfix]
		private static void ParentCupboardsOnLoad(StartOfRound __instance)
		{
			MoreCupboards.Logger.LogDebug((object)"Parenting existing cupboards to ship...");
			GameObject val = GameObject.Find("/Environment/HangarShip");
			for (int i = 1; i <= MoreCupboards.maximumCupboards.Value; i++)
			{
				GameObject val2 = GameObject.Find($"StorageCloset{i}(Clone)");
				GameObject val3 = GameObject.Find($"StorageCloset{i}Alt(Clone)");
				if ((Object)(object)val2 != (Object)null && (Object)(object)val != (Object)null)
				{
					val2.transform.SetParent(val.transform, true);
				}
				else if ((Object)(object)val3 != (Object)null && (Object)(object)val != (Object)null)
				{
					val3.transform.SetParent(val.transform, true);
				}
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "SpawnUnlockable")]
		[HarmonyPostfix]
		private static void ParentNewCupboard(StartOfRound __instance, int unlockableIndex)
		{
			UnlockableItem val = __instance.unlockablesList.unlockables[unlockableIndex];
			if (!val.unlockableName.Contains("Cupboard") || !(val.unlockableName != "Cupboard"))
			{
				return;
			}
			try
			{
				MoreCupboards.Logger.LogDebug((object)"Parenting cupboard to ship...");
				int num = ((!string.IsNullOrEmpty(val.unlockableName.Replace("Cupboard", ""))) ? int.Parse(val.unlockableName.Replace("Cupboard", "")) : 0);
				if (num <= MoreCupboards.maximumCupboards.Value && num > 0)
				{
					GameObject val2 = GameObject.Find("/Environment/HangarShip");
					GameObject val3 = GameObject.Find($"StorageCloset{num}(Clone)");
					GameObject val4 = GameObject.Find($"StorageCloset{num}Alt(Clone)");
					if ((Object)(object)val3 != (Object)null && (Object)(object)val2 != (Object)null)
					{
						val3.transform.SetParent(val2.transform, true);
					}
					else if ((Object)(object)val4 != (Object)null && (Object)(object)val2 != (Object)null)
					{
						val4.transform.SetParent(val2.transform, true);
					}
				}
			}
			catch (FormatException)
			{
				MoreCupboards.Logger.LogWarning((object)"Incorrect cupboard name string passed!");
			}
		}
	}
	[HarmonyPatch]
	internal class CupboardSaveStoredItems
	{
		private static bool doSync = false;

		private static int syncTimer = 30;

		private static void ApplyCupboardParents()
		{
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			if (!MoreCupboards.mattyPresent || !MoreCupboards.autoParent.Value)
			{
				return;
			}
			MoreCupboards.Logger.LogDebug((object)"Finding objects in cupboards...");
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			List<BoxCollider> list = new List<BoxCollider>();
			for (int i = 1; i <= MoreCupboards.maximumCupboards.Value; i++)
			{
				string text = i.ToString();
				GameObject val = GameObject.Find("StorageCloset" + text + "(Clone)/PlacementCollider");
				if ((Object)(object)val == (Object)null)
				{
					val = GameObject.Find("StorageCloset" + text + "Alt(Clone)/PlacementCollider");
				}
				if ((Object)(object)val == (Object)null)
				{
					MoreCupboards.Logger.LogDebug((object)("Unable to find specified cupboard! StorageCloset" + text));
					continue;
				}
				BoxCollider component = val.GetComponent<BoxCollider>();
				if ((Object)(object)component != (Object)null)
				{
					list.Add(component);
				}
			}
			GrabbableObject[] array2 = array;
			foreach (GrabbableObject val2 in array2)
			{
				foreach (BoxCollider item in list)
				{
					Transform parent = ((Component)item).gameObject.transform.parent;
					Bounds bounds = ((Collider)item).bounds;
					if (((Bounds)(ref bounds)).Contains(((Component)val2).transform.position) && (Object)(object)parent != (Object)null)
					{
						((Component)val2).transform.parent = parent;
						val2.targetFloorPosition = ((Component)val2).transform.localPosition;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "LateUpdate")]
		private static void OnUpdate(StartOfRound __instance)
		{
			if (MoreCupboards.mattyPresent && MoreCupboards.autoParent.Value && doSync)
			{
				syncTimer--;
				if (syncTimer <= 0)
				{
					ApplyCupboardParents();
					doSync = false;
					syncTimer = 30;
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "LoadShipGrabbableItems")]
		private static void LoadOnServerSpawn(StartOfRound __instance)
		{
			if (MoreCupboards.mattyPresent && MoreCupboards.autoParent.Value)
			{
				doSync = true;
				syncTimer = 30;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "SyncShipUnlockablesClientRpc")]
		private static void LoadAfterCupboardSync(StartOfRound __instance)
		{
			if (MoreCupboards.mattyPresent && MoreCupboards.autoParent.Value)
			{
				doSync = true;
				syncTimer = 30;
			}
		}
	}
}