Decompiled source of LargerStorages v1.0.1

Mods/LargerStoragesV1.0.0.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using LargerStorages;
using MelonLoader;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(ModMain), "Larger Storages", "1.0.0", "bytefrags", null)]
[assembly: MelonGame(null, "Schedule I")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("SimpleIceHash")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SimpleIceHash")]
[assembly: AssemblyTitle("SimpleIceHash")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LargerStorages;

public class ModMain : MelonMod
{
	private const int SMALL_RACK_SLOTS = 6;

	private const int MEDIUM_RACK_SLOTS = 8;

	private const int LARGE_RACK_SLOTS = 12;

	private const int GAME_MAX_SLOTS = 20;

	private static Type storageEntityType;

	private static bool isInitialized;

	public override void OnInitializeMelon()
	{
		MelonLogger.Msg("Mod loaded");
		TryFindTypes();
		SetupStorageEntityPatch();
	}

	private void TryFindTypes()
	{
		try
		{
			storageEntityType = Type.GetType("Il2CppScheduleOne.Storage.StorageEntity, Assembly-CSharp");
			if (storageEntityType == null)
			{
				MelonLogger.Error("Could not find StorageEntity type!");
			}
			else
			{
				isInitialized = true;
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error in TryFindTypes: " + ex.Message);
		}
	}

	private void SetupStorageEntityPatch()
	{
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Expected O, but got Unknown
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Expected O, but got Unknown
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Expected O, but got Unknown
		try
		{
			if (storageEntityType == null)
			{
				MelonLogger.Error("StorageEntity type not found, cannot patch!");
				return;
			}
			MethodInfo methodInfo = storageEntityType.GetMethod("dll", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			MethodInfo method = storageEntityType.GetMethod("Awake", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (methodInfo == null && method == null)
			{
				MethodInfo[] methods = storageEntityType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo2 in methods)
				{
					if (methodInfo2.Name.Contains("Awake") || methodInfo2.Name.Contains("dll"))
					{
						methodInfo = methodInfo2;
						break;
					}
				}
			}
			if (methodInfo == null && method == null)
			{
				MelonLogger.Error("Could not find methods to patch!");
				return;
			}
			MethodInfo method2 = typeof(ModMain).GetMethod("StorageEntityInitialize_Prefix", BindingFlags.Static | BindingFlags.NonPublic);
			if (method2 == null)
			{
				MelonLogger.Error("Prefix method not found!");
				return;
			}
			Harmony val = new Harmony("com.bytefrags.largerstorages");
			if (methodInfo != null)
			{
				val.Patch((MethodBase)methodInfo, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			if (method != null)
			{
				val.Patch((MethodBase)method, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MelonLogger.Msg("Storage system patched successfully");
			MelonLogger.Msg("Enjoy! If you have any issues or custom mod inquiries, message me on Discord @bytefrags.");
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error patching StorageEntity methods: " + ex.Message);
		}
	}

	private static bool StorageEntityInitialize_Prefix(object __instance)
	{
		try
		{
			if (!isInitialized)
			{
				return true;
			}
			MonoBehaviour val = (MonoBehaviour)((__instance is MonoBehaviour) ? __instance : null);
			if ((Object)(object)val == (Object)null)
			{
				return true;
			}
			string text = ((Object)((Component)val).gameObject).name.ToLower();
			bool flag = text.Contains("storagerack_small");
			bool flag2 = text.Contains("storagerack_medium");
			bool flag3 = text.Contains("storagerack_large");
			if (!flag && !flag2 && !flag3)
			{
				return true;
			}
			int num = 0;
			int num2 = 0;
			PropertyInfo property = storageEntityType.GetProperty("SlotCount");
			FieldInfo field = storageEntityType.GetField("SlotCount");
			PropertyInfo property2 = storageEntityType.GetProperty("DisplayRowCount");
			if (flag)
			{
				num = 6;
				num2 = 2;
			}
			else if (flag2)
			{
				num = 8;
				num2 = 2;
			}
			else if (flag3)
			{
				num = 12;
				num2 = 3;
			}
			num = Mathf.Min(num, 20);
			if (property != null)
			{
				property.SetValue(__instance, num);
			}
			else if (field != null)
			{
				field.SetValue(__instance, num);
			}
			if (property2 != null)
			{
				property2.SetValue(__instance, num2);
			}
			return true;
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error in StorageEntityInitialize_Prefix: " + ex.Message);
			return true;
		}
	}
}