Decompiled source of ImprovedStacks v2.0.1

ImprovedStacksMod.dll

Decompiled 2 weeks 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
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("ImprovedStacksMod")]
[assembly: AssemblyDescription("Mod to simply change stack sizes of items")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("AlmanaX21")]
[assembly: AssemblyProduct("ImprovedStacksMod")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3DC357FF-84FD-4BB0-89C7-FA6059583C24")]
[assembly: AssemblyFileVersion("2.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 ImprovedStacksMod
{
	public enum MethodType
	{
		Static,
		Multiplier
	}
	[BepInPlugin("AlmanaX21.ImprovedStacksMod", "ImprovedStacksMod", "2.0.0")]
	public class ImprovedStacksModPlugin : BaseUnityPlugin
	{
		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order = null;

			[UsedImplicitly]
			public bool? Browsable = null;

			[UsedImplicitly]
			public string? Category = null;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer = null;
		}

		internal const string ModName = "ImprovedStacksMod";

		internal const string ModVersion = "2.0.0";

		internal const string Author = "AlmanaX21";

		private const string ModGUID = "AlmanaX21.ImprovedStacksMod";

		private static string ConfigFileName = "improvedstacks.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony _harmony = new Harmony("AlmanaX21.ImprovedStacksMod");

		public static readonly ManualLogSource ImprovedStacksModLogger;

		internal static ConfigEntry<MethodType> StackMethodConfig;

		internal static ConfigEntry<float> StackMultiplierConfig;

		internal static ConfigEntry<string> StackMappingsConfig;

		internal static Dictionary<int, int> StackMappings;

		private void Awake()
		{
			StackMethodConfig = ((BaseUnityPlugin)this).Config.Bind<MethodType>("Stack Settings", "Method", MethodType.Multiplier, "Choose how stack sizes are changed:\n'Static' uses per-item mappings, 'Multiplier' multiplies all stacks by the multiplier value.");
			StackMultiplierConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Stack Settings", "Multiplier Value", 2f, "Only used when Method = Multiplier. Multiplies all stack sizes by this number.");
			StackMappingsConfig = TextEntryConfig("Stack Settings", "Static Mappings", "50=100\n20=50\n10=30", "Only used when Method = Static. Define mappings as 'old=new', one per line.\nExample:\n50=100\n20=50\n10=30");
			ParseStackMappings();
			SetupWatcher();
			_harmony.PatchAll();
			ImprovedStacksModLogger.LogInfo((object)"ImprovedStacksMod loaded successfully.");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				ImprovedStacksModLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
				ParseStackMappings();
			}
			catch
			{
				ImprovedStacksModLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				ImprovedStacksModLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		internal static void ParseStackMappings()
		{
			StackMappings.Clear();
			try
			{
				if (StackMethodConfig.Value == MethodType.Static)
				{
					string text = StackMappingsConfig.Value.Replace("\\n", "\n");
					string[] array = text.Split(new char[2] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
					string[] array2 = array;
					foreach (string text2 in array2)
					{
						string text3 = text2.Trim();
						if (!string.IsNullOrWhiteSpace(text3) && !text3.StartsWith("#"))
						{
							string[] array3 = text3.Split(new char[1] { '=' });
							if (array3.Length == 2 && int.TryParse(array3[0].Trim(), out var result) && int.TryParse(array3[1].Trim(), out var result2))
							{
								StackMappings[result] = result2;
							}
						}
					}
					ImprovedStacksModLogger.LogInfo((object)$"Loaded {StackMappings.Count} static stack mappings.");
				}
				else
				{
					ImprovedStacksModLogger.LogInfo((object)$"Using multiplier mode: x{StackMultiplierConfig.Value}");
				}
			}
			catch (Exception arg)
			{
				ImprovedStacksModLogger.LogError((object)$"Error parsing stack configuration: {arg}");
			}
		}

		internal ConfigEntry<T> TextEntryConfig<T>(string group, string name, T value, string desc)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			ConfigurationManagerAttributes configurationManagerAttributes = new ConfigurationManagerAttributes();
			configurationManagerAttributes.CustomDrawer = TextAreaDrawer;
			ConfigurationManagerAttributes configurationManagerAttributes2 = configurationManagerAttributes;
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, new ConfigDescription(desc, (AcceptableValueBase)null, new object[1] { configurationManagerAttributes2 }));
		}

		internal static void TextAreaDrawer(ConfigEntryBase entry)
		{
			GUILayout.ExpandHeight(true);
			GUILayout.ExpandWidth(true);
			entry.BoxedValue = GUILayout.TextArea((string)entry.BoxedValue, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.ExpandHeight(true)
			});
		}

		static ImprovedStacksModPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			ImprovedStacksModLogger = Logger.CreateLogSource("ImprovedStacksMod");
			StackMethodConfig = null;
			StackMultiplierConfig = null;
			StackMappingsConfig = null;
			StackMappings = new Dictionary<int, int>();
		}
	}
	[HarmonyPatch(typeof(RM), "LoadResources")]
	public static class RM_LoadResources_Patch
	{
		private static void Postfix()
		{
			if (RM.code?.allItems?.items == null)
			{
				ImprovedStacksModPlugin.ImprovedStacksModLogger.LogDebug((object)"Something is null");
				return;
			}
			Item val = default(Item);
			foreach (Transform item in RM.code.allItems.items)
			{
				if (((Component)item).TryGetComponent<Item>(ref val))
				{
					int value;
					if (ImprovedStacksModPlugin.StackMethodConfig.Value == MethodType.Multiplier)
					{
						val.stackAmount = Mathf.RoundToInt((float)val.stackAmount * ImprovedStacksModPlugin.StackMultiplierConfig.Value);
					}
					else if (ImprovedStacksModPlugin.StackMappings.TryGetValue(val.stackAmount, out value))
					{
						val.stackAmount = value;
					}
				}
			}
			ImprovedStacksModPlugin.ImprovedStacksModLogger.LogInfo((object)"Applied stack size adjustments.");
		}
	}
}