Decompiled source of StackSizes v1.0.1

plugins/StackSizes.dll

Decompiled a month 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.Logging;
using EquinoxsModUtils;
using HarmonyLib;
using StackSizes.Patches;
using StackSizesGUI;
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("StackSizes")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StackSizes")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("aa6784b8-391a-42ba-91a1-ee9272ae48af")]
[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 StackSizesGUI
{
	public static class StackSizesGUI
	{
		public static string stackSizeLimit = "";

		public static Texture2D backgroundImage;

		public static float xOffset => 200f;

		public static float yOffset => (float)Screen.height / 2f;

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

		public static void Init()
		{
			LoadImage("StackSizes.Image.StackSizeBox.png", ref backgroundImage);
		}

		public static void DrawGUI()
		{
			//IL_000b: 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_0019: 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_0027: 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_0043: 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_0052: Expected O, but got Unknown
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_0075: Expected O, but got Unknown
			//IL_00d8: 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)
			GUIStyle val = new GUIStyle(GUI.skin.textField)
			{
				fontSize = 16,
				alignment = (TextAnchor)3
			};
			val.normal.textColor = Color.white;
			val.normal.background = CreateTransparentTexture();
			val.border = new RectOffset(0, 0, 0, 0);
			val.margin = new RectOffset(0, 0, 0, 0);
			val.padding = new RectOffset(5, 5, 14, 3);
			GUIStyle val2 = val;
			if ((Object)(object)backgroundImage == (Object)null)
			{
				Debug.LogError((object)"Background image not loaded.");
				return;
			}
			Rect val3 = default(Rect);
			((Rect)(ref val3))..ctor(xPos - (float)(((Texture)backgroundImage).width / 2), yOffset - (float)(((Texture)backgroundImage).height / 2), (float)((Texture)backgroundImage).width, (float)((Texture)backgroundImage).height);
			GUI.DrawTexture(val3, (Texture)(object)backgroundImage);
			Rect val4 = default(Rect);
			((Rect)(ref val4))..ctor(((Rect)(ref val3)).x + 10f, ((Rect)(ref val3)).y + ((Rect)(ref val3)).height / 2f - 15f, ((Rect)(ref val3)).width - 20f, 50f);
			stackSizeLimit = GUI.TextField(val4, stackSizeLimit, val2);
		}

		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);
		}

		private static Texture2D CreateTransparentTexture()
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(1, 1);
			val.SetPixel(0, 0, Color.clear);
			val.Apply();
			return val;
		}
	}
}
namespace StackSizes
{
	public static class SharedState
	{
		public static bool shouldShow = false;

		public static bool disableInput = false;

		public static uint InserterID;

		public static string LastMachineInfo = "";

		public static uint? CurrentSelectedInserterID = null;

		public static int inserterStackSize = 20;

		public static Dictionary<uint, int> stackSizes = new Dictionary<uint, int>();

		public static void UpdateConfiguration(uint instanceId, int newValue)
		{
			stackSizes[instanceId] = newValue;
		}

		public static int GetConfiguration(uint instanceId, int defaultValue = 10)
		{
			int value;
			return stackSizes.TryGetValue(instanceId, out value) ? value : defaultValue;
		}
	}
	[BepInPlugin("com.casper.StackSizes", "StackSizes", "1.0.0")]
	public class StackSizesPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.casper.StackSizes";

		private const string PluginName = "StackSizes";

		private const string VersionString = "1.0.0";

		private static readonly Harmony Harmony = new Harmony("com.casper.StackSizes");

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

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

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: StackSizes, VersionString: 1.0.0 is loading...");
			Harmony.PatchAll();
			ApplyPatches();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: StackSizes, VersionString: 1.0.0 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
			global::StackSizesGUI.StackSizesGUI.Init();
		}

		private void Update()
		{
			//IL_0022: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Invalid comparison between Unknown and I4
			if (ModUtils.hasSaveStateLoaded)
			{
				GenericMachineInstanceRef val = (GenericMachineInstanceRef)ModUtils.GetPrivateField<PlayerInteraction>("targetMachineRef", Player.instance.interaction);
				if (ModUtils.NullCheck((object)val, "aimedMachine", false) && (int)((GenericMachineInstanceRef)(ref val)).typeIndex == 7)
				{
					SharedState.LastMachineInfo = $"Machine Hit ID: {((GenericMachineInstanceRef)(ref val)).instanceId}\n";
					SharedState.InserterID = ((GenericMachineInstanceRef)(ref val)).instanceId;
				}
				else
				{
					SharedState.shouldShow = false;
					SharedState.InserterID = 0u;
					SharedState.LastMachineInfo = "No inserter targeted.\n";
				}
			}
			if (SharedState.shouldShow && (Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305)))
			{
				float axis = Input.GetAxis("Mouse ScrollWheel");
				if (axis != 0f)
				{
					UpdateStackSize((axis > 0f) ? 1 : (-1));
				}
			}
			if (SharedState.shouldShow && (Input.GetKey((KeyCode)303) || Input.GetKey((KeyCode)304)))
			{
				float axis2 = Input.GetAxis("Mouse ScrollWheel");
				if (axis2 != 0f)
				{
					UpdateStackSize((axis2 > 0f) ? 100 : (-100));
				}
			}
		}

		private void UpdateStackSize(int change)
		{
			if (int.TryParse(global::StackSizesGUI.StackSizesGUI.stackSizeLimit, out var result))
			{
				int num = result + change;
				num = Mathf.Max(1, num);
				global::StackSizesGUI.StackSizesGUI.stackSizeLimit = num.ToString();
				SharedState.inserterStackSize = num;
				SharedState.UpdateConfiguration(SharedState.InserterID, num);
				Debug.Log((object)$"Stack size updated to: {num}");
			}
			else
			{
				Debug.LogError((object)"Failed to parse current stack size limit as an integer.");
			}
		}

		private void OnGUI()
		{
			if (SharedState.shouldShow)
			{
				SharedState.disableInput = true;
				global::StackSizesGUI.StackSizesGUI.DrawGUI();
			}
			else
			{
				SharedState.disableInput = false;
			}
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(InserterInstancePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(SaveStatesPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(PlayerToolbarPatch), (string)null);
		}

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

		public static void LoadData(string worldName)
		{
			string path = dataFolder + "/" + worldName + "/stackSizes.txt";
			if (!File.Exists(path))
			{
				return;
			}
			string[] array = File.ReadAllLines(path);
			string[] array2 = array;
			foreach (string text in array2)
			{
				string[] array3 = text.Split(new char[1] { '|' });
				if (array3.Length == 2)
				{
					uint key = uint.Parse(array3[0]);
					int value = int.Parse(array3[1]);
					SharedState.stackSizes[key] = value;
				}
			}
		}
	}
}
namespace StackSizes.Patches
{
	internal class InserterInstancePatch
	{
		[HarmonyPatch(typeof(InserterInstance), "get_maxStackSize")]
		public static class MaxStackSizePatch
		{
			[HarmonyPostfix]
			public static void Postfix(ref int __result, InserterInstance __instance)
			{
				//IL_0001: 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)
				if (__instance.isStack)
				{
					uint instanceId = __instance.commonInfo.instanceId;
					if (instanceId == SharedState.InserterID)
					{
						global::StackSizesGUI.StackSizesGUI.stackSizeLimit = (SharedState.stackSizes.ContainsKey(instanceId) ? SharedState.stackSizes[instanceId].ToString() : __result.ToString());
						SharedState.shouldShow = true;
					}
					else
					{
						SharedState.shouldShow = false;
					}
					__result = SharedState.GetConfiguration(instanceId, __result);
				}
			}
		}
	}
	[HarmonyPatch]
	public class PlayerToolbarPatch
	{
		[HarmonyPatch(typeof(PlayerToolbar), "ProcessInput")]
		[HarmonyPrefix]
		public static bool PrefixProcessInput()
		{
			if (SharedState.disableInput)
			{
				return false;
			}
			return true;
		}
	}
	internal class SaveStatesPatch
	{
		[HarmonyPatch(typeof(SaveState), "SaveToFile")]
		[HarmonyPostfix]
		private static void SaveData()
		{
			StackSizesPlugin.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)
		{
			StackSizesPlugin.LoadData(saveMetadata.worldName);
		}
	}
}