Decompiled source of CreativeChests v1.0.1

plugins/CreativeChests/CreativeChests.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CreativeChests.Patches;
using EquinoxsModUtils;
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("CreativeChests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CreativeChests")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("fb7689e8-cb54-45b3-b387-52ca40346dfe")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CreativeChests
{
	public static class ChestProperties
	{
		public const string isCreative = "isCreative";

		public const string lastResId = "lastResId";

		public const string creativeChestIndexes = "creativeChestIndexes";
	}
	[BepInPlugin("com.equinox.CreativeChests", "CreativeChests", "1.0.1")]
	public class CreativeChestsPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.CreativeChests";

		private const string PluginName = "CreativeChests";

		private const string VersionString = "1.0.1";

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

		public static ManualLogSource Log = new ManualLogSource("CreativeChests");

		public static bool showGUI;

		public static uint currentChestId;

		public static int currentChestIndex;

		public static List<int> creativeChestIds = new List<int>();

		public static List<int> creativeChestIndexes = new List<int>();

		private static Texture2D border;

		private static Texture2D borderHover;

		public static ConfigEntry<float> buttonXOffset;

		public static ConfigEntry<float> buttonYOffset;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeChests, VersionString: 1.0.1 is loading...");
			Harmony.PatchAll();
			ApplyPatches();
			CreateConfigEntries();
			ModUtils.SaveStateLoaded += OnSaveStateLoaded;
			ModUtils.AddCustomDataForMachine<string>(0u, "creativeChestIndexes", "");
			border = ModUtils.LoadTexture2DFromFile("CreativeChests.Assets.Border240x40.png", false, (Assembly)null);
			borderHover = ModUtils.LoadTexture2DFromFile("CreativeChests.Assets.BorderHover240x40.png", false, (Assembly)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeChests, VersionString: 1.0.1 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void FixedUpdate()
		{
			//IL_006b: 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_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_0228: Unknown result type (might be due to invalid IL or missing references)
			if (!ModUtils.hasGameLoaded)
			{
				return;
			}
			bool flag = false;
			MachineInstanceList<ChestInstance, ChestDefinition> machineList = MachineManager.instance.GetMachineList<ChestInstance, ChestDefinition>((MachineTypeEnum)3);
			foreach (int creativeChestIndex in creativeChestIndexes)
			{
				if (creativeChestIndex >= machineList.curCount || machineList.unusedIndices.Contains(creativeChestIndex))
				{
					flag = true;
					break;
				}
				ChestInstance index = machineList.GetIndex(creativeChestIndex);
				ref Inventory inventory = ref MACHINE_INSTANCE_HELPER_EXTENSIONS.GetInventory<ChestInstance>(ref index, 0);
				if (((ResourceStack)(ref inventory.myStacks[0])).isEmpty)
				{
					for (int i = 0; i < inventory.myStacks.Length; i++)
					{
						inventory.myStacks[i] = ResourceStack.CreateEmptyStack();
					}
					return;
				}
				int id = inventory.myStacks[0].id;
				if (id == ModUtils.GetCustomDataForMachine<int>(index.commonInfo.instanceId, "lastResId"))
				{
					return;
				}
				int maxStackCount = SaveState.GetResInfoFromId(id).maxStackCount;
				for (int j = 0; j < inventory.myStacks.Length; j++)
				{
					inventory.myStacks[j].id = id;
					inventory.myStacks[j].maxStack = maxStackCount;
					inventory.myStacks[j].count = maxStackCount;
				}
			}
			if (!flag)
			{
				return;
			}
			creativeChestIndexes.Clear();
			ChestInstance[] myArray = machineList.myArray;
			foreach (ChestInstance val in myArray)
			{
				if ((val.commonInfo.instanceId != 0 || val.commonInfo.index != 0) && ModUtils.CustomDataExistsForMachine(val.commonInfo.instanceId) && ModUtils.GetCustomDataForMachine<bool>(val.commonInfo.instanceId, "isCreative"))
				{
					creativeChestIndexes.Add(val.commonInfo.index);
				}
			}
			SetCreativeChestsList();
		}

		private void OnGUI()
		{
			//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_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: 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_00b0: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			if (showGUI && ModUtils.hasGameLoaded)
			{
				float num = (float)Screen.width / 2f + buttonXOffset.Value;
				float num2 = (float)Screen.height / 2f + buttonYOffset.Value;
				GUIStyle val = new GUIStyle
				{
					fontSize = 16,
					alignment = (TextAnchor)4
				};
				val.normal.textColor = Color.yellow;
				val.normal.background = border;
				val.hover.textColor = Color.black;
				val.hover.background = borderHover;
				GUIStyle val2 = val;
				if (GUI.Button(new Rect(num, num2, 200f, 40f), "Set To Creative", val2))
				{
					ModUtils.UpdateCustomDataForMachine<bool>(currentChestId, "isCreative", true);
					creativeChestIndexes.Add(currentChestIndex);
					SetCreativeChestsList();
				}
			}
		}

		private void OnSaveStateLoaded(object sender, EventArgs e)
		{
			GetCreativeChestList();
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(ChestDefinitionPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(ChestInstancePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(InventoryNavigatorPatch), (string)null);
		}

		private void CreateConfigEntries()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			buttonXOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Button Horizontal Offset", 120f, new ConfigDescription("Controls the horizontal position of the 'Set To Creative' button", (AcceptableValueBase)null, Array.Empty<object>()));
			buttonYOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Button Vertical Offset", 320f, new ConfigDescription("Controls the vertical position of the 'Set To Creative' button", (AcceptableValueBase)null, Array.Empty<object>()));
		}

		private void GetCreativeChestList()
		{
			string customDataForMachine = ModUtils.GetCustomDataForMachine<string>(0u, "creativeChestIndexes");
			Debug.Log((object)("Creative Chest List: " + customDataForMachine));
			if (!string.IsNullOrEmpty(customDataForMachine))
			{
				List<int> list = new List<int>();
				string[] array = customDataForMachine.Split(new char[1] { ',' });
				foreach (string s in array)
				{
					list.Add(int.Parse(s));
				}
				creativeChestIndexes = list;
			}
		}

		private void SetCreativeChestsList()
		{
			List<string> list = new List<string>();
			foreach (int creativeChestIndex in creativeChestIndexes)
			{
				list.Add(creativeChestIndex.ToString());
			}
			Debug.Log((object)("Creative Chest List: " + string.Join(",", creativeChestIndexes)));
			ModUtils.UpdateCustomDataForMachine<string>(0u, "creativeChestIndexes", string.Join(",", creativeChestIndexes));
		}
	}
}
namespace CreativeChests.Patches
{
	internal class ChestDefinitionPatch
	{
		[HarmonyPatch(typeof(MachineDefinition<ChestInstance, ChestDefinition>), "OnBuild")]
		[HarmonyPostfix]
		private static void AddParameters(MachineInstanceRef<ChestInstance> instRef)
		{
			ModUtils.AddCustomDataForMachine<bool>(instRef.instanceId, "isCreative", false);
			ModUtils.AddCustomDataForMachine<int>(instRef.instanceId, "lastResId", -1);
		}

		[HarmonyPatch(typeof(MachineDefinition<ChestInstance, ChestDefinition>), "OnDeconstruct")]
		[HarmonyPrefix]
		private static void RemoveFromCreativeList(ref ChestInstance erasedInstance)
		{
			//IL_002f: 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)
			if (CreativeChestsPlugin.creativeChestIndexes.Contains(erasedInstance.commonInfo.index))
			{
				ref Inventory inventory = ref MACHINE_INSTANCE_HELPER_EXTENSIONS.GetInventory<ChestInstance>(ref erasedInstance, 0);
				for (int i = 0; i < inventory.myStacks.Length; i++)
				{
					inventory.myStacks[i] = ResourceStack.CreateEmptyStack();
				}
				CreativeChestsPlugin.creativeChestIndexes.Remove(erasedInstance.commonInfo.index);
				ModUtils.UpdateCustomDataForMachine<bool>(erasedInstance.commonInfo.instanceId, "isCreative", false);
			}
		}
	}
	internal class ChestInstancePatch
	{
	}
	internal class InventoryNavigatorPatch
	{
		[HarmonyPatch(typeof(InventoryNavigator), "OnOpen")]
		[HarmonyPrefix]
		private static void ShowGUI(InventoryNavigator __instance)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			CreativeChestsPlugin.showGUI = true;
			ChestInstance aimedAtChest = GetAimedAtChest();
			CreativeChestsPlugin.currentChestId = aimedAtChest.commonInfo.instanceId;
			CreativeChestsPlugin.currentChestIndex = aimedAtChest.commonInfo.index;
		}

		[HarmonyPatch(typeof(InventoryNavigator), "OnClose")]
		[HarmonyPrefix]
		private static void HideGui()
		{
			CreativeChestsPlugin.showGUI = false;
		}

		private static ChestInstance GetAimedAtChest()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			GenericMachineInstanceRef val = (GenericMachineInstanceRef)ModUtils.GetPrivateField<PlayerInteraction>("targetMachineRef", Player.instance.interaction);
			return MachineManager.instance.Get<ChestInstance, ChestDefinition>(((GenericMachineInstanceRef)(ref val)).index, (MachineTypeEnum)3);
		}
	}
}