Decompiled source of CreativeChests v2.1.1

plugins/CreativeChests/CreativeChests.dll

Decompiled 6 days 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.Logging;
using CreativeChests.Patches;
using EquinoxsDebuggingTools;
using EquinoxsModUtils;
using EquinoxsModUtils.Additions;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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 ContentAdder
	{
		public static void AddContent()
		{
			//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_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: 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_00af: 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_00d2: 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)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: 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_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Expected O, but got Unknown
			NewResourceDetails val = new NewResourceDetails
			{
				name = "Creative Chest",
				description = "Fills itself with the item in the first slot.",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				headerTitle = "Logistics",
				subHeaderTitle = "Utility",
				maxStackCount = 50,
				sortPriority = 999,
				unlockName = "Basic Logistics",
				parentName = "Container",
				sprite = Images.LoadSpriteFromFile("CreativeChests.Images.CreativeChest.png", false)
			};
			EMUAdditions.AddNewMachine<ChestInstance, ChestDefinition>((MachineDefinition<ChestInstance, ChestDefinition>)(object)ScriptableObject.CreateInstance<ChestDefinition>(), val, true);
			EMUAdditions.AddNewRecipe(new NewRecipeDetails
			{
				GUID = "com.equinox.CreativeChests",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				duration = 0.1f,
				ingredients = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo
					{
						name = "Iron Ingot",
						quantity = 1
					}
				},
				outputs = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo
					{
						name = "Creative Chest",
						quantity = 1
					}
				},
				sortPriority = 10,
				unlockName = "Basic Logistics"
			}, false);
		}
	}
	[BepInPlugin("com.equinox.CreativeChests", "CreativeChests", "2.1.1")]
	public class CreativeChestsPlugin : BaseUnityPlugin
	{
		internal const string MyGUID = "com.equinox.CreativeChests";

		private const string PluginName = "CreativeChests";

		private const string VersionString = "2.1.1";

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

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

		public static List<uint> creativeChestIDs = new List<uint>();

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeChests, VersionString: 2.1.1 is loading...");
			Harmony.PatchAll();
			Events.GameUnloaded += MachineDefinitionPatch.OnGameUnloaded;
			ApplyPatches();
			CreateConfigEntries();
			ContentAdder.AddContent();
			LoadPrefab();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeChests, VersionString: 2.1.1 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void FixedUpdate()
		{
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			MachineInstanceRef<ChestInstance> val = default(MachineInstanceRef<ChestInstance>);
			foreach (uint creativeChestID in creativeChestIDs)
			{
				if (!MachineManager.instance.GetRefFromId<ChestInstance>(creativeChestID, ref val))
				{
					continue;
				}
				ref Inventory inventory = ref val.GetInventory(0);
				if (inventory.myStacks[0].id == inventory.myStacks[1].id)
				{
					if (!((ResourceStack)(ref inventory.myStacks[0])).isEmpty)
					{
						inventory.myStacks[0].count = inventory.myStacks[0].maxStack;
					}
					continue;
				}
				if (((ResourceStack)(ref inventory.myStacks[0])).isEmpty)
				{
					for (int i = 0; i < inventory.myStacks.Length; i++)
					{
						inventory.myStacks[i] = ResourceStack.CreateEmptyStack();
					}
					continue;
				}
				int id = inventory.myStacks[0].id;
				int maxStackCount = SaveState.GetResInfoFromId(id).maxStackCount;
				EDT.PacedLog("General", $"Filling chest #{creativeChestID} with {maxStackCount} res#{id}", 1f);
				for (int j = 0; j < inventory.myStacks.Length; j++)
				{
					inventory.myStacks[j].id = id;
					inventory.myStacks[j].maxStack = maxStackCount;
					inventory.myStacks[j].count = maxStackCount;
				}
			}
		}

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

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(FlowManagerPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(MachineDefinitionPatch), (string)null);
		}

		private void CreateConfigEntries()
		{
		}

		private void LoadPrefab()
		{
			MachineDefinitionPatch.prefab = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("CreativeChests.CreativeChest")).LoadAsset<GameObject>("assets/creative_box.prefab");
		}

		private void GetCreativeChestList()
		{
		}

		private void SetCreativeChestsList()
		{
		}
	}
}
namespace CreativeChests.Patches
{
	internal class MachineDefinitionPatch
	{
		public static GameObject prefab;

		private static Dictionary<uint, GameObject> visualsMap = new Dictionary<uint, GameObject>();

		[HarmonyPatch(typeof(MachineDefinition<ChestInstance, ChestDefinition>), "OnBuild")]
		[HarmonyPostfix]
		private static void AddCreativeChestToList(MachineInstanceRef<ChestInstance> instRef)
		{
			//IL_0080: 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_00bc: Unknown result type (might be due to invalid IL or missing references)
			if (((ResourceInfo)((ChestInstance)(ref instRef.Get())).myDef).displayName != "Creative Chest")
			{
				return;
			}
			CreativeChestsPlugin.creativeChestIDs.Add(instRef.GetCommonInfo().instanceId);
			EDT.Log("General", $"Added chest #{instRef.GetCommonInfo().instanceId} to creativeChestIDs");
			if (instRef.gridInfo.strata == GameState.instance.GetStrata())
			{
				GameObject val = Object.Instantiate<GameObject>(prefab, ((GridInfo)(ref instRef.gridInfo)).BottomCenter, Quaternion.Euler(0f, (float)instRef.gridInfo.yawRot, 0f));
				val.transform.localScale = new Vector3(1.01f, 1.01f, 1.01f);
				if (visualsMap.ContainsKey(instRef.instanceId))
				{
					Object.Destroy((Object)(object)visualsMap[instRef.instanceId]);
				}
				visualsMap[instRef.instanceId] = val;
			}
		}

		[HarmonyPatch(typeof(MachineDefinition<ChestInstance, ChestDefinition>), "OnDeconstruct")]
		[HarmonyPrefix]
		private static void RemoveVisuals(ref InserterInstance erasedInstance)
		{
			if (!(((ResourceInfo)((InserterInstance)(ref erasedInstance)).myDef).displayName != "Creative Chest"))
			{
				Object.Destroy((Object)(object)visualsMap[erasedInstance.commonInfo.instanceId]);
				visualsMap.Remove(erasedInstance.commonInfo.instanceId);
			}
		}

		public static void RedoVisualsOnStrataChanged()
		{
			//IL_005c: 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_0097: Unknown result type (might be due to invalid IL or missing references)
			if (!LoadingStates.hasGameLoaded)
			{
				return;
			}
			ClearVisuals();
			IMachineInstanceRef val = default(IMachineInstanceRef);
			foreach (uint creativeChestID in CreativeChestsPlugin.creativeChestIDs)
			{
				if (MachineManager.instance.GetRefFromId(creativeChestID, ref val) && val.gridInfo.strata == GameState.instance.GetStrata())
				{
					GameObject val2 = Object.Instantiate<GameObject>(prefab, ((GridInfo)(ref val.gridInfo)).BottomCenter, Quaternion.Euler(0f, (float)val.gridInfo.yawRot, 0f));
					val2.transform.localScale = new Vector3(1.01f, 1.01f, 1.01f);
					if (visualsMap.ContainsKey(val.instanceId))
					{
						Object.Destroy((Object)(object)visualsMap[val.instanceId]);
					}
					visualsMap[val.instanceId] = val2;
				}
			}
		}

		internal static void OnGameUnloaded()
		{
			ClearVisuals();
		}

		private static void ClearVisuals()
		{
			foreach (GameObject value in visualsMap.Values)
			{
				Object.Destroy((Object)(object)value);
			}
			visualsMap.Clear();
		}
	}
	internal class FlowManagerPatch
	{
		[HarmonyPatch(typeof(FlowManager), "SwitchStrataTrampoline")]
		[HarmonyPostfix]
		private static void RedoVisuals()
		{
			MachineDefinitionPatch.RedoVisualsOnStrataChanged();
		}
	}
}