Decompiled source of WormholeChests Updated v3.0.9

WormholeChests.dll

Decompiled a day 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 BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using EquinoxsModUtils.Additions;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("WormholeChests")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WormholeChests")]
[assembly: AssemblyTitle("WormholeChests")]
[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 WormholeChests
{
	public static class ChestInstanceExtensions
	{
		public static bool IsNull(this ChestInstance chest)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return chest.commonInfo.instanceId == 0;
		}
	}
	[BepInPlugin("com.equinox.WormholeChests", "WormholeChests", "3.0.9")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class WormholeChestsPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.WormholeChests";

		private const string PluginName = "WormholeChests";

		private const string VersionString = "3.0.9";

		private static readonly Harmony Harmony = new Harmony("com.equinox.WormholeChests");

		public static ManualLogSource Log;

		public static bool isUnlockActive = false;

		public static Texture2D wormholeTexture;

		public static ConfigEntry<bool> freeWormholeChests;

		public static ConfigEntry<float> channelBoxXOffset;

		public static ConfigEntry<float> channelBoxYOffset;

		public static ConfigEntry<float> channelBoxWidth;

		public static ConfigEntry<float> createButtonXOffset;

		private void Awake()
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"PluginName: WormholeChests, VersionString: 3.0.9 is loading...");
			Harmony.PatchAll();
			Events.GameDefinesLoaded += OnGameDefinesLoaded;
			Events.TechTreeStateLoaded += OnTechTreeStateLoaded;
			Events.MachineManagerLoaded += OnMachineManagerLoaded;
			CreateConfigEntries();
			ApplyPatches();
			ChestGUI.LoadImages();
			Sprite sprite = Sprite.Create(wormholeTexture, new Rect(0f, 0f, (float)((Texture)wormholeTexture).width, (float)((Texture)wormholeTexture).height), new Vector2(0f, 0f), 512f);
			EMUAdditions.AddNewUnlock(new NewUnlockDetails
			{
				category = (TechCategory)5,
				coreTypeNeeded = (CoreType)2,
				coreCountNeeded = 2000,
				description = "Allow chests on the same channel to share inventories.",
				displayName = "Wormhole Chests",
				requiredTier = (ResearchTier)4,
				treePosition = 0,
				sprite = sprite
			}, false);
			Log.LogInfo((object)"PluginName: WormholeChests, VersionString: 3.0.9 is loaded.");
		}

		private void OnGUI()
		{
			if (ChestGUI.shouldShowGUI)
			{
				try
				{
					ChestGUI.DrawChestGUI();
				}
				catch (Exception ex)
				{
					Log.LogError((object)("OnGUI DrawChestGUI failed: " + ex.Message + "\n" + ex.StackTrace));
					ChestGUI.shouldShowGUI = false;
				}
			}
		}

		private void OnGameDefinesLoaded()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			Unlock unlockByName = Unlocks.GetUnlockByName("Core Boost (Threshing)", false);
			Unlock unlockByName2 = Unlocks.GetUnlockByName("Core Boost (Mining)", false);
			Unlocks.UpdateUnlockTier("Wormhole Chests", unlockByName.requiredTier, false);
			Unlocks.UpdateUnlockTreePosition("Wormhole Chests", unlockByName2.treePosition, false);
		}

		private void OnTechTreeStateLoaded()
		{
			Unlock unlockByName = Unlocks.GetUnlockByName("Wormhole Chests", false);
			if ((Object)(object)unlockByName != (Object)null && TechTreeState.instance != null)
			{
				isUnlockActive = TechTreeState.instance.IsUnlockActive(((UniqueIdScriptableObject)unlockByName).uniqueId);
				Log.LogInfo((object)$"Wormhole Chests unlock active: {isUnlockActive}");
			}
			else
			{
				isUnlockActive = false;
				Log.LogInfo((object)"Wormhole Chests: Could not check unlock status, disabling mod functionality");
			}
		}

		private void OnMachineManagerLoaded()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			if (!isUnlockActive)
			{
				Log.LogInfo((object)"WormholeChests: Unlock not active, skipping data load");
				return;
			}
			try
			{
				WormholeManager.LoadData(SaveState.instance.metadata.worldName);
				Log.LogInfo((object)"WormholeChests Loaded");
				MachineInstanceList<ChestInstance, ChestDefinition> machineList = MachineManager.instance.GetMachineList<ChestInstance, ChestDefinition>((MachineTypeEnum)3);
				if (machineList == null || machineList.myArray == null)
				{
					Log.LogWarning((object)"Could not get chest machine list");
					return;
				}
				for (int i = 0; i < machineList.myArray.Length; i++)
				{
					ChestInstance val = machineList.myArray[i];
					uint instanceId = val.commonInfo.instanceId;
					if (instanceId != 0 && WormholeManager.chestChannelMap.ContainsKey(instanceId))
					{
						Inventory inventoryForChest = WormholeManager.GetInventoryForChest(instanceId);
						if (inventoryForChest.numSlots > 0)
						{
							val.commonInfo.inventories[0] = inventoryForChest;
						}
					}
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("OnMachineManagerLoaded failed: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		private void CreateConfigEntries()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Expected O, but got Unknown
			freeWormholeChests = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Free Wormhole Chests", false, new ConfigDescription("Disables the cost of creating Wormhole Chests. Cheat, not recommended.", (AcceptableValueBase)null, Array.Empty<object>()));
			channelBoxXOffset = ((BaseUnityPlugin)this).Config.Bind<float>("GUI Layout", "Channel Box X Offset", 32f, new ConfigDescription("Controls the horizontal position of the Channel box in a Chest's GUI.", (AcceptableValueBase)null, Array.Empty<object>()));
			channelBoxYOffset = ((BaseUnityPlugin)this).Config.Bind<float>("GUI Layout", "Channel Box Y Offset", -355f, new ConfigDescription("Controls the vertical position of the Channel box in a Chest's GUI.", (AcceptableValueBase)null, Array.Empty<object>()));
			channelBoxWidth = ((BaseUnityPlugin)this).Config.Bind<float>("GUI Layout", "Channel Box Width", 240f, new ConfigDescription("Controls the width of the Channel box in a Chest's GUI.", (AcceptableValueBase)null, Array.Empty<object>()));
			createButtonXOffset = ((BaseUnityPlugin)this).Config.Bind<float>("GUI Layout", "Create Button X Offset", 444f, new ConfigDescription("Controls the horizontal position of the Create / Link button in a Chest's GUI.", (AcceptableValueBase)null, Array.Empty<object>()));
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(ChestDefinitionPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(ChestInstancePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(InventoryNavigatorPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(SaveStatePatch), (string)null);
		}
	}
	public class Wormhole
	{
		public string channel;

		public Inventory inventory;

		public Wormhole()
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			inventory = new Inventory
			{
				myStacks = (ResourceStack[])(object)new ResourceStack[56],
				numSlots = 56
			};
		}

		public Wormhole(string serial)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			inventory = new Inventory
			{
				myStacks = (ResourceStack[])(object)new ResourceStack[56],
				numSlots = 56
			};
			for (int i = 0; i < 56; i++)
			{
				inventory.myStacks[i] = ResourceStack.CreateEmptyStack();
			}
			string[] array = serial.Split(new char[1] { '|' });
			channel = array[0];
			for (int j = 1; j < array.Length; j++)
			{
				string[] array2 = array[j].Split(new char[1] { ',' });
				string text = array2[0];
				string text2 = array2[1];
				if (text != "null" && text2 != "null")
				{
					int num = int.Parse(text);
					int num2 = int.Parse(text2);
					((Inventory)(ref inventory)).AddResources(num, num2, true);
				}
			}
		}

		public string Serialise()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			string text = channel;
			if (inventory.myStacks != null)
			{
				ResourceStack[] myStacks = inventory.myStacks;
				for (int i = 0; i < myStacks.Length; i++)
				{
					ResourceStack val = myStacks[i];
					if (!((ResourceStack)(ref val)).isEmpty)
					{
						text += $"|{((UniqueIdScriptableObject)((ResourceStack)(ref val)).info).uniqueId},{val.count}";
					}
				}
			}
			else
			{
				text += "|null,null";
			}
			return text;
		}
	}
	public static class WormholeManager
	{
		public static Dictionary<string, Wormhole> wormholes = new Dictionary<string, Wormhole>();

		public static Dictionary<uint, string> chestChannelMap = new Dictionary<uint, string>();

		private static string dataFolder => Application.persistentDataPath + "/WormholeChests";

		public static void AddWormhole(Wormhole wormhole)
		{
			wormholes.Add(wormhole.channel, wormhole);
		}

		public static Wormhole GetWormhole(string channel)
		{
			if (!wormholes.TryGetValue(channel, out var value))
			{
				WormholeChestsPlugin.Log.LogWarning((object)("GetWormhole: Channel '" + channel + "' not found in wormholes dictionary"));
				return null;
			}
			return value;
		}

		public static Inventory GetInventoryForChest(uint chestID)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			if (!chestChannelMap.TryGetValue(chestID, out var value))
			{
				WormholeChestsPlugin.Log.LogWarning((object)$"GetInventoryForChest: ChestID {chestID} not found in chestChannelMap");
				return default(Inventory);
			}
			return (Inventory)(((??)GetWormhole(value)?.inventory) ?? default(Inventory));
		}

		public static Inventory GetInventoryForChest(ChestInstance chestInstance)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			return GetInventoryForChest(chestInstance.commonInfo.instanceId);
		}

		public static List<Wormhole> GetAllWormholes()
		{
			return wormholes.Values.ToList();
		}

		public static void CheckForEmptyChannels()
		{
			int num = 0;
			while (num < wormholes.Count)
			{
				string text = wormholes.Keys.ToList()[num];
				if (GetNumChestsInChannel(text) == 0)
				{
					wormholes.Remove(text);
				}
				else
				{
					num++;
				}
			}
		}

		public static bool DoesChannelExist(string channel)
		{
			return wormholes.ContainsKey(channel);
		}

		public static ChestInstance GetAimedAtChest()
		{
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: 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_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Invalid comparison between Unknown and I4
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)Player.instance == (Object)null)
				{
					return default(ChestInstance);
				}
				if ((Object)(object)Player.instance.interaction == (Object)null)
				{
					return default(ChestInstance);
				}
				object privateField = EMU.GetPrivateField<PlayerInteraction>("targetMachineRef", Player.instance.interaction);
				if (privateField == null)
				{
					return default(ChestInstance);
				}
				GenericMachineInstanceRef val = (GenericMachineInstanceRef)privateField;
				if ((int)((GenericMachineInstanceRef)(ref val)).typeIndex != 3)
				{
					return default(ChestInstance);
				}
				if (((GenericMachineInstanceRef)(ref val)).index < 0)
				{
					WormholeChestsPlugin.Log.LogWarning((object)$"GetAimedAtChest: Invalid machineRef index: {((GenericMachineInstanceRef)(ref val)).index}");
					return default(ChestInstance);
				}
				return MachineManager.instance.Get<ChestInstance, ChestDefinition>(((GenericMachineInstanceRef)(ref val)).index, (MachineTypeEnum)3);
			}
			catch (Exception ex)
			{
				WormholeChestsPlugin.Log.LogError((object)("GetAimedAtChest failed: " + ex.Message + "\n" + ex.StackTrace));
				return default(ChestInstance);
			}
		}

		public static bool IsChestWormholeChest(ChestInstance chest)
		{
			//IL_0005: 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)
			return chestChannelMap.ContainsKey(chest.commonInfo.instanceId);
		}

		public static int GetCostToCreateOrLink()
		{
			return Mathf.CeilToInt(100f * Mathf.Pow(1.05f, (float)chestChannelMap.Count));
		}

		public static int GetNumChestsInChannel(string channel)
		{
			int num = 0;
			foreach (string value in chestChannelMap.Values)
			{
				if (value.Equals(channel))
				{
					num++;
				}
			}
			return num;
		}

		public static void SaveData(string worldName)
		{
			Directory.CreateDirectory(dataFolder);
			Directory.CreateDirectory(dataFolder + "/" + worldName);
			string path = dataFolder + "/" + worldName + "/Wormholes.txt";
			List<string> list = new List<string>();
			foreach (Wormhole allWormhole in GetAllWormholes())
			{
				list.Add(allWormhole.Serialise());
			}
			File.WriteAllLines(path, list);
			string path2 = dataFolder + "/" + worldName + "/ChestChannelMap.txt";
			List<string> list2 = new List<string>();
			foreach (KeyValuePair<uint, string> item in chestChannelMap)
			{
				list2.Add($"{item.Key}|{item.Value}");
			}
			File.WriteAllLines(path2, list2);
		}

		public static void LoadData(string worldName)
		{
			try
			{
				string text = dataFolder + "/" + worldName + "/Wormholes.txt";
				if (!File.Exists(text))
				{
					WormholeChestsPlugin.Log.LogInfo((object)("No wormhole data file found at " + text));
					return;
				}
				wormholes.Clear();
				chestChannelMap.Clear();
				string[] array = File.ReadAllLines(text);
				foreach (string text2 in array)
				{
					if (!string.IsNullOrEmpty(text2))
					{
						try
						{
							AddWormhole(new Wormhole(text2));
						}
						catch (Exception ex)
						{
							WormholeChestsPlugin.Log.LogWarning((object)("Failed to parse wormhole line: " + text2 + " - " + ex.Message));
						}
					}
				}
				string text3 = dataFolder + "/" + worldName + "/ChestChannelMap.txt";
				if (!File.Exists(text3))
				{
					WormholeChestsPlugin.Log.LogWarning((object)("ChestChannelMap file not found at " + text3));
					return;
				}
				array = File.ReadAllLines(text3);
				foreach (string text4 in array)
				{
					if (string.IsNullOrEmpty(text4))
					{
						continue;
					}
					try
					{
						string[] array2 = text4.Split(new char[1] { '|' });
						if (array2.Length >= 2)
						{
							uint key = uint.Parse(array2[0]);
							string value = array2[1];
							if (!chestChannelMap.ContainsKey(key))
							{
								chestChannelMap.Add(key, value);
							}
						}
					}
					catch (Exception ex2)
					{
						WormholeChestsPlugin.Log.LogWarning((object)("Failed to parse map line: " + text4 + " - " + ex2.Message));
					}
				}
				WormholeChestsPlugin.Log.LogInfo((object)$"Loaded {wormholes.Count} wormholes and {chestChannelMap.Count} chest mappings");
			}
			catch (Exception ex3)
			{
				WormholeChestsPlugin.Log.LogError((object)("LoadData failed: " + ex3.Message + "\n" + ex3.StackTrace));
			}
		}
	}
	public static class ChestGUI
	{
		public static bool shouldShowGUI;

		public static uint currentChestID;

		public static bool showLinkedLabel;

		public static string lastChannel;

		public static string channel = "";

		private static Texture2D textBoxNormal;

		private static Texture2D textBoxHover;

		public static bool freeChests => WormholeChestsPlugin.freeWormholeChests.Value;

		public static float channelBoxXOffset => WormholeChestsPlugin.channelBoxXOffset.Value;

		public static float channelBoxYOffset => WormholeChestsPlugin.channelBoxYOffset.Value;

		public static float channelBoxWidth => WormholeChestsPlugin.channelBoxWidth.Value;

		public static float createButtonXOffset => WormholeChestsPlugin.createButtonXOffset.Value;

		public static float xPos => (float)Screen.width / 2f + channelBoxXOffset;

		public static float yPos => (float)Screen.height / 2f + channelBoxYOffset;

		public static void LoadImages()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			textBoxNormal = CreateTexture(240, 40, new Color(0.2f, 0.2f, 0.2f, 0.8f), new Color(0.5f, 0.5f, 0.5f, 1f));
			textBoxHover = CreateTexture(240, 40, new Color(0.25f, 0.25f, 0.25f, 0.9f), new Color(0.6f, 0.6f, 0.6f, 1f));
			WormholeChestsPlugin.wormholeTexture = CreateTexture(64, 64, new Color(0.1f, 0f, 0.2f, 1f), new Color(0.3f, 0f, 0.5f, 1f));
		}

		private static Texture2D CreateTexture(int width, int height, Color fillColor, Color borderColor)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(width, height);
			Color[] array = (Color[])(object)new Color[width * height];
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					if (j == 0 || j == width - 1 || i == 0 || i == height - 1)
					{
						array[i * width + j] = borderColor;
					}
					else
					{
						array[i * width + j] = fillColor;
					}
				}
			}
			val.SetPixels(array);
			val.Apply();
			return val;
		}

		private static void HandleKeyPresses()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Invalid comparison between Unknown and I4
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Invalid comparison between Unknown and I4
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			try
			{
				if (((int)Event.current.keyCode == 9 || Event.current.character == '\t') && (int)Event.current.type != 7 && (int)Event.current.type != 8)
				{
					Event.current.Use();
				}
				if ((UnityInput.Current.GetKey((KeyCode)27) || UnityInput.Current.GetKey((KeyCode)9)) && (Object)(object)UIManager.instance != (Object)null && (Object)(object)UIManager.instance.inventoryAndStorageMenu != (Object)null)
				{
					((MachineMenuUI<ChestInstance>)(object)UIManager.instance.inventoryAndStorageMenu).Close();
				}
				if ((Object)(object)InputHandler.instance != (Object)null)
				{
					InputHandler.instance.uiInputBlocked = true;
				}
			}
			catch (Exception ex)
			{
				WormholeChestsPlugin.Log.LogError((object)("HandleKeyPresses failed: " + ex.Message));
			}
		}

		public static void DrawChestGUI()
		{
			HandleKeyPresses();
			DrawChannelBox();
			if (channel != "")
			{
				DrawCreateButton();
				DrawLinkedLabel();
				lastChannel = channel;
			}
		}

		private static void DrawChannelBox()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			GUIStyle val = new GUIStyle
			{
				fontSize = 16,
				alignment = (TextAnchor)3
			};
			val.normal.textColor = Color.white;
			val.normal.background = textBoxNormal;
			val.hover.textColor = Color.white;
			val.hover.background = textBoxHover;
			GUIStyle val2 = new GUIStyle
			{
				fontSize = 16,
				padding = new RectOffset(10, 0, 0, 0),
				alignment = (TextAnchor)3
			};
			val2.normal.textColor = Color.gray;
			channel = GUI.TextField(new Rect(xPos, yPos, channelBoxWidth, 40f), channel, val);
			if (channel == "")
			{
				GUI.Label(new Rect(xPos + 2f, yPos, channelBoxWidth - 10f, 40f), "Channel", val2);
			}
		}

		private static void DrawCreateButton()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			GUIStyle val = new GUIStyle
			{
				fontSize = 16,
				padding = new RectOffset(10, 0, 0, 0),
				alignment = (TextAnchor)(freeChests ? 4 : 3)
			};
			val.normal.textColor = Color.white;
			val.normal.background = textBoxNormal;
			val.hover.textColor = Color.white;
			val.hover.background = textBoxHover;
			ChestInstance aimedAtChest = WormholeManager.GetAimedAtChest();
			if (aimedAtChest.IsNull())
			{
				return;
			}
			bool flag = WormholeManager.DoesChannelExist(channel);
			if (flag && WormholeManager.chestChannelMap.ContainsKey(aimedAtChest.commonInfo.instanceId))
			{
				showLinkedLabel = true;
			}
			string text = (flag ? "Link" : "Create");
			if (GUI.Button(new Rect(xPos + createButtonXOffset, yPos, channelBoxWidth, 40f), text, val))
			{
				if (!freeChests)
				{
					CheckAndRemoveCores();
				}
				Inventory inventory = ((ChestInstance)(ref aimedAtChest)).GetInventory();
				if (!flag)
				{
					Inventory inventory2 = default(Inventory);
					((Inventory)(ref inventory2)).CopyFrom(ref inventory);
					WormholeManager.AddWormhole(new Wormhole
					{
						channel = channel,
						inventory = inventory2
					});
				}
				else
				{
					Inventory inventory3 = ((ChestInstance)(ref aimedAtChest)).GetInventory();
					aimedAtChest.commonInfo.inventories[0] = WormholeManager.GetWormhole(channel).inventory;
					ResourceStack[] myStacks = inventory3.myStacks;
					int num = default(int);
					for (int i = 0; i < myStacks.Length; i++)
					{
						ResourceStack val2 = myStacks[i];
						if (!((ResourceStack)(ref val2)).isEmpty)
						{
							ChestInstance.AddResources(ref aimedAtChest, ((UniqueIdScriptableObject)((ResourceStack)(ref val2)).info).uniqueId, ref num, val2.count);
						}
					}
				}
				showLinkedLabel = true;
				WormholeManager.chestChannelMap[currentChestID] = channel;
				GUI.SetNextControlName(" ");
				GUI.Label(new Rect(-100f, -100f, 1f, 1f), "");
				GUI.FocusControl(" ");
			}
			if (!freeChests)
			{
				DrawCostGUI();
			}
		}

		private static void CheckAndRemoveCores()
		{
			float num = WormholeManager.GetCostToCreateOrLink();
			if ((float)TechTreeState.instance.NumCoresAvailable((CoreType)2) < num)
			{
				((FMODAudioSource)(ref Player.instance.audio.buildError)).PlayRandomClip(true);
				return;
			}
			((FMODAudioSource)(ref Player.instance.audio.buildClick)).PlayRandomClip(true);
			TechTreeState.instance.usedResearchCores[2] += WormholeManager.GetCostToCreateOrLink();
		}

		private static void DrawCostGUI()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			float num = WormholeManager.GetCostToCreateOrLink();
			bool flag = (float)TechTreeState.instance.NumCoresAvailable((CoreType)2) >= num;
			string text = $"Cost: {num}";
			GUIStyle val = new GUIStyle
			{
				fontSize = 16,
				alignment = (TextAnchor)5
			};
			val.normal.textColor = (flag ? Color.green : Color.red);
			GUIStyle val2 = new GUIStyle();
			val2.normal.background = null;
			GUI.Box(new Rect(xPos + createButtonXOffset + channelBoxWidth - 35f, yPos + 5f, 30f, 30f), (Texture)(object)Images.GetImageForResource("Research Core 480nm (Blue)", false), val2);
			GUI.Label(new Rect(xPos + createButtonXOffset + 10f, yPos + 5f, channelBoxWidth - 50f, 30f), text, val);
		}

		private static void DrawLinkedLabel()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			if (!string.IsNullOrEmpty(lastChannel) && lastChannel != channel)
			{
				showLinkedLabel = false;
			}
			if (showLinkedLabel)
			{
				GUIStyle val = new GUIStyle
				{
					fontSize = 16,
					fontStyle = (FontStyle)1,
					alignment = (TextAnchor)4
				};
				val.normal.textColor = Color.green;
				val.normal.background = textBoxNormal;
				GUI.Box(new Rect(xPos + createButtonXOffset, yPos, channelBoxWidth, 40f), "Linked!", val);
			}
		}
	}
	internal class ChestDefinitionPatch
	{
		[HarmonyPatch(typeof(MachineDefinition<ChestInstance, ChestDefinition>), "OnDeconstruct")]
		[HarmonyPostfix]
		private static void UpdateChestMap(ChestDefinition __instance, ref ChestInstance erasedInstance)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			if (!WormholeChestsPlugin.isUnlockActive)
			{
				return;
			}
			uint instanceId = erasedInstance.commonInfo.instanceId;
			if (!WormholeManager.chestChannelMap.ContainsKey(instanceId))
			{
				return;
			}
			if (WormholeManager.GetNumChestsInChannel(WormholeManager.chestChannelMap[instanceId]) > 1)
			{
				ResourceStack[] myStacks = ((ChestInstance)(ref erasedInstance)).GetInventory().myStacks;
				for (int i = 0; i < myStacks.Length; i++)
				{
					ResourceStack val = myStacks[i];
					if (!((ResourceStack)(ref val)).isEmpty)
					{
						((InventoryWrapper)Player.instance.inventory).TryRemoveResources(((UniqueIdScriptableObject)((ResourceStack)(ref val)).info).uniqueId, val.count);
					}
				}
			}
			WormholeManager.chestChannelMap.Remove(instanceId);
			WormholeManager.CheckForEmptyChannels();
		}
	}
	internal class ChestInstancePatch
	{
		[HarmonyPatch(typeof(ChestInstance), "GetInventory")]
		[HarmonyPrefix]
		private static void GetWormholeInsteadOfInventory(ChestInstance __instance)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			if (!WormholeChestsPlugin.isUnlockActive)
			{
				return;
			}
			try
			{
				if (WormholeManager.IsChestWormholeChest(__instance))
				{
					Inventory inventoryForChest = WormholeManager.GetInventoryForChest(__instance);
					if (inventoryForChest.numSlots > 0)
					{
						__instance.commonInfo.inventories[0] = inventoryForChest;
					}
				}
			}
			catch (Exception ex)
			{
				WormholeChestsPlugin.Log.LogError((object)("GetWormholeInsteadOfInventory failed: " + ex.Message));
			}
		}
	}
	internal class InventoryNavigatorPatch
	{
		[HarmonyPatch(typeof(InventoryNavigator), "OnOpen")]
		[HarmonyPrefix]
		private static void ShowGUI(InventoryNavigator __instance)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			if (!WormholeChestsPlugin.isUnlockActive)
			{
				return;
			}
			try
			{
				ChestInstance aimedAtChest = WormholeManager.GetAimedAtChest();
				if (aimedAtChest.IsNull())
				{
					return;
				}
				ChestGUI.shouldShowGUI = true;
				ChestGUI.currentChestID = aimedAtChest.commonInfo.instanceId;
				if (!WormholeManager.chestChannelMap.ContainsKey(aimedAtChest.commonInfo.instanceId))
				{
					return;
				}
				ChestGUI.channel = WormholeManager.chestChannelMap[aimedAtChest.commonInfo.instanceId];
				if (WormholeManager.DoesChannelExist(ChestGUI.channel))
				{
					Wormhole wormhole = WormholeManager.GetWormhole(ChestGUI.channel);
					if (wormhole != null)
					{
						aimedAtChest.commonInfo.inventories[0] = wormhole.inventory;
					}
				}
			}
			catch (Exception ex)
			{
				WormholeChestsPlugin.Log.LogError((object)("ShowGUI failed: " + ex.Message));
				ChestGUI.shouldShowGUI = false;
			}
		}

		[HarmonyPatch(typeof(InventoryNavigator), "OnClose")]
		[HarmonyPrefix]
		private static void HideGui()
		{
			try
			{
				if ((Object)(object)InputHandler.instance != (Object)null)
				{
					InputHandler.instance.uiInputBlocked = false;
				}
				ChestGUI.shouldShowGUI = false;
				ChestGUI.channel = "";
			}
			catch (Exception ex)
			{
				WormholeChestsPlugin.Log.LogError((object)("HideGui failed: " + ex.Message));
			}
		}
	}
	internal class SaveStatePatch
	{
		[HarmonyPatch(typeof(SaveState), "SaveToFile")]
		[HarmonyPostfix]
		private static void SaveWormholes()
		{
			WormholeManager.SaveData(SaveState.instance.metadata.worldName);
			WormholeChestsPlugin.Log.LogInfo((object)"WormholeChests Saved");
		}
	}
}