Decompiled source of SmartInserters v1.0.0

plugins/SmartInserters/SmartInserters.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using HarmonyLib;
using SmartInserters.Patches;
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("SmartInserters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SmartInserters")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6f87a872-92b3-4a7f-b42d-5a160a437260")]
[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 SmartInserters
{
	public static class LimitGUI
	{
		public static bool shouldShow;

		private static Texture2D textBoxNormal;

		private static Texture2D textBoxHover;

		public static string limit;

		public static float xOffset => SmartInsertersPlugin.guiXOffset.Value;

		public static float yOffset => SmartInsertersPlugin.guiYOffset.Value;

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

		public static void DrawGUI()
		{
			//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_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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_0066: 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)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: 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_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			GUIStyle val = new GUIStyle
			{
				fontSize = 16
			};
			val.normal.textColor = Color.white;
			val.normal.background = null;
			GUIStyle val2 = val;
			GUIStyle val3 = new GUIStyle
			{
				fontSize = 16,
				alignment = (TextAnchor)4
			};
			val3.normal.textColor = Color.white;
			val3.normal.background = textBoxNormal;
			val3.hover.textColor = Color.white;
			val3.hover.background = textBoxHover;
			GUIStyle val4 = val3;
			GUI.Label(new Rect(xPos, yOffset, 200f, 40f), "Limit:", val2);
			limit = GUI.TextField(new Rect(xPos, yOffset + 30f, 240f, 40f), limit, val4);
			if (!GUI.Button(new Rect(xPos, yOffset + 80f, 240f, 40f), "Set", val4))
			{
				return;
			}
			if (string.IsNullOrEmpty(limit))
			{
				((FMODAudioSource)(ref Player.instance.audio.buildError)).PlayRandomClip(true);
				Debug.Log((object)"limit is null or emtpy");
				return;
			}
			limit = limit.ToLower();
			if (!limit.EndsWith("s") && !int.TryParse(limit, out var _))
			{
				((FMODAudioSource)(ref Player.instance.audio.buildError)).PlayRandomClip(true);
				Debug.Log((object)("limit: " + limit));
			}
			else
			{
				uint instanceId = GetAimedAtInserter().commonInfo.instanceId;
				SmartInsertersPlugin.inserterLimits[instanceId] = limit;
				((FMODAudioSource)(ref Player.instance.audio.buildClick)).PlayRandomClip(true);
			}
		}

		public static void LoadImages()
		{
			LoadImage("SmartInserters.Images.Border240x40.png", ref textBoxNormal);
			LoadImage("SmartInserters.Images.BorderHover240x40.png", ref textBoxHover);
		}

		public static InserterInstance GetAimedAtInserter()
		{
			//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<InserterInstance, InserterDefinition>(((GenericMachineInstanceRef)(ref val)).index, (MachineTypeEnum)7);
		}

		private static void LoadImage(string path, ref Texture2D output)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			using Stream stream = executingAssembly.GetManifestResourceStream(path);
			if (stream == null)
			{
				Debug.LogError((object)("Could not find image with path '" + path + "'"));
				return;
			}
			using MemoryStream memoryStream = new MemoryStream();
			stream.CopyTo(memoryStream);
			byte[] array = memoryStream.ToArray();
			output = new Texture2D(2, 2);
			ImageConversion.LoadImage(output, array);
		}
	}
	[BepInPlugin("com.equinox.SmartInserters", "SmartInserters", "1.0.0")]
	public class SmartInsertersPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.SmartInserters";

		private const string PluginName = "SmartInserters";

		private const string VersionString = "1.0.0";

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

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

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

		public static ConfigEntry<float> guiXOffset;

		public static ConfigEntry<float> guiYOffset;

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

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: SmartInserters, VersionString: 1.0.0 is loading...");
			Harmony.PatchAll();
			LimitGUI.LoadImages();
			CreateConfigEntries();
			ApplyPatches();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: SmartInserters, VersionString: 1.0.0 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void OnGUI()
		{
			if (LimitGUI.shouldShow)
			{
				LimitGUI.DrawGUI();
			}
		}

		public static void SaveData(string worldName)
		{
			Directory.CreateDirectory(dataFolder);
			Directory.CreateDirectory(dataFolder + "/" + worldName);
			string path = dataFolder + "/" + worldName + "/InserterLimits.txt";
			List<string> list = new List<string>();
			foreach (KeyValuePair<uint, string> inserterLimit in inserterLimits)
			{
				list.Add($"{inserterLimit.Key}|{inserterLimit.Value}");
			}
			File.WriteAllLines(path, list);
		}

		public static void LoadData(string worldName)
		{
			string path = dataFolder + "/" + worldName + "/InserterLimits.txt";
			if (File.Exists(path))
			{
				string[] array = File.ReadAllLines(path);
				string[] array2 = array;
				foreach (string text in array2)
				{
					string[] array3 = text.Split(new char[1] { '|' });
					uint key = uint.Parse(array3[0]);
					inserterLimits[key] = array3[1];
				}
			}
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(FilterInserterUIPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(InserterInstancePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(SaveStatePatch), (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
			guiXOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "GUI X Offset", -350f, new ConfigDescription("Controls the horizontal position of the limit GUI", (AcceptableValueBase)null, Array.Empty<object>()));
			guiYOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "GUI Y Offset", 65f, new ConfigDescription("Controls the vertical position of the limit GUI", (AcceptableValueBase)null, Array.Empty<object>()));
		}
	}
}
namespace SmartInserters.Patches
{
	internal class FilterInserterUIPatch
	{
		[HarmonyPatch(typeof(FilterInserterUI), "OnOpen")]
		[HarmonyPostfix]
		private static void ShowLimitGUI()
		{
			//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)
			LimitGUI.shouldShow = true;
			uint instanceId = LimitGUI.GetAimedAtInserter().commonInfo.instanceId;
			LimitGUI.limit = (SmartInsertersPlugin.inserterLimits.ContainsKey(instanceId) ? SmartInsertersPlugin.inserterLimits[instanceId] : "");
			Debug.Log((object)"Opening FilterInserter GUI");
		}

		[HarmonyPatch(typeof(FilterInserterUI), "OnClose")]
		[HarmonyPostfix]
		private static void HideLimitGUI()
		{
			LimitGUI.shouldShow = false;
			Debug.Log((object)"Closing FilterInserter GUI");
		}
	}
	internal class InserterInstancePatch
	{
		[HarmonyPatch(typeof(InserterInstance), "Give")]
		[HarmonyPrefix]
		private static bool ShouldGiveItems(InserterInstance __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: 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_00ea: Unknown result type (might be due to invalid IL or missing references)
			uint instanceId = __instance.commonInfo.instanceId;
			if (!SmartInsertersPlugin.inserterLimits.ContainsKey(instanceId))
			{
				return true;
			}
			if (__instance.filterType == -1)
			{
				return true;
			}
			ResourceInfo resInfoFromId = SaveState.GetResInfoFromId(__instance.filterType);
			int maxStackCount = resInfoFromId.maxStackCount;
			string text = SmartInsertersPlugin.inserterLimits[instanceId];
			int num = 0;
			if (!text.EndsWith("s"))
			{
				num = int.Parse(text);
			}
			else
			{
				text = text.Replace("s", "");
				num = int.Parse(text) * maxStackCount;
			}
			int num2 = 0;
			Inventory[] inventories = __instance.giveResourceContainer.GetCommonInfo().inventories;
			for (int i = 0; i < inventories.Length; i++)
			{
				Inventory val = inventories[i];
				num2 += ((Inventory)(ref val)).GetResourceCount(__instance.filterType, false);
			}
			if (num2 + __instance.currentHeldStackCount <= num)
			{
				return true;
			}
			return false;
		}
	}
	internal class SaveStatePatch
	{
		[HarmonyPatch(typeof(SaveState), "SaveToFile")]
		[HarmonyPostfix]
		private static void SaveData()
		{
			SmartInsertersPlugin.SaveData(SaveState.instance.metadata.worldName);
		}

		[HarmonyPatch(typeof(SaveState), "LoadFileData", new Type[]
		{
			typeof(SaveMetadata),
			typeof(string)
		})]
		[HarmonyPostfix]
		private static void LoadData(SaveState __instance, SaveMetadata saveMetadata, string replayLocation)
		{
			SmartInsertersPlugin.LoadData(saveMetadata.worldName);
		}
	}
}