Decompiled source of Cartridge Case Despawn Unlimiter v1.0.0

Mods/CartridgeCaseDespawnUnlimiter.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BoneLib.BoneMenu;
using BoneLib.BoneMenu.Elements;
using CartridgeCaseDespawnUnlimiter;
using HarmonyLib;
using MelonLoader;
using SLZ.Combat;
using SLZ.Interaction;
using SLZ.Marrow.Pool;
using SLZ.Props.Weapons;
using UnhollowerBaseLib;
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(Main), "Cartridge Unlimiter", "1.0.0", "Snake1Byte", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: AssemblyTitle("CartridgeCaseDespawnUnlimiter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CartridgeCaseDespawnUnlimiter")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("376e6019-0f8f-44fc-918f-94c1fbe1b8c7")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CartridgeCaseDespawnUnlimiter;

public class Main : MelonMod
{
	public override void OnInitializeMelon()
	{
		MethodPatches.Initialize();
		UI.Initialize();
	}
}
internal class MethodPatches
{
	[HarmonyPatch(typeof(InventorySlot), "Insert")]
	public static class PatchInventorySlotInsert
	{
		[HarmonyPostfix]
		private static void Postfix(ref GameObject go)
		{
			ObjectGrabbed(go);
		}
	}

	public static void Initialize()
	{
	}

	public static void ObjectGrabbed(GameObject go)
	{
		Il2CppArrayBase<Gun> componentsInChildren = go.GetComponentsInChildren<Gun>();
		foreach (Gun item in componentsInChildren)
		{
			if (!((Object)(object)item == (Object)null) && !((Object)(object)item.defaultCartridge == (Object)null) && item.defaultCartridge.cartridgeCaseSpawnable != null && !((Object)(object)item.defaultCartridge.cartridgeCaseSpawnable.policyData == (Object)null))
			{
				lock (SharedValues.SyncLock)
				{
					AddToDictionary(item);
				}
				if (SharedValues.ModEnabled)
				{
					item.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize = SharedValues.MaxCartridges;
				}
				else if (SharedValues.GrabbedGuns.ContainsKey(item))
				{
					item.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize = SharedValues.GrabbedGuns[item];
				}
				if (SharedValues.Debug)
				{
					Melon<Main>.Logger.Msg("---------------------------------------------------------------------------");
					Melon<Main>.Logger.Msg($"After grabbing it, Gun component: {((Object)item).name} has its spawnPolicData changed to {item.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize}.");
				}
			}
		}
		foreach (Gun item2 in componentsInChildren)
		{
			if (!((Object)(object)item2 == (Object)null) && !((Object)(object)item2.defaultMagazine == (Object)null) && item2.defaultMagazine.spawnable != null && !((Object)(object)item2.defaultMagazine.spawnable.policyData == (Object)null))
			{
				item2.defaultMagazine.spawnable.policyData.maxSize = SharedValues.MaxCartridges;
			}
		}
		if (SharedValues.ModEnabled)
		{
			Array.ForEach(((IEnumerable<FirearmCartridge>)Resources.FindObjectsOfTypeAll<FirearmCartridge>()).ToArray(), delegate(FirearmCartridge item)
			{
				item.despawnDelaySeconds = SharedValues.DespawnTimerInSeconds;
			});
		}
		else
		{
			Array.ForEach(((IEnumerable<FirearmCartridge>)Resources.FindObjectsOfTypeAll<FirearmCartridge>()).ToArray(), delegate(FirearmCartridge item)
			{
				item.despawnDelaySeconds = SharedValues.DefaultDespawnTimer;
			});
		}
	}

	private static void AddToDictionary(Gun item)
	{
		bool flag = false;
		foreach (Gun key in SharedValues.GrabbedGuns.Keys)
		{
			if (((Object)item).GetInstanceID() == ((Object)key).GetInstanceID())
			{
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			SharedValues.GrabbedGuns.Add(item, item.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize);
		}
	}
}
internal class SharedValues
{
	public const string ModName = "Cartridge Unlimiter";

	public const string Version = "1.0.0";

	public const string Author = "Snake1Byte";

	public static int MaxCartridges = 1000;

	public static float DespawnTimerInSeconds = 300f;

	public static float DefaultDespawnTimer = 30f;

	public static string MainMenuColor = "#ffffb0";

	public static string MenuWarningColor = "#ff6e6e";

	public static string MenuEnableColor = "#642b2b";

	public static Dictionary<Gun, int> GrabbedGuns = new Dictionary<Gun, int>();

	public static readonly object SyncLock = new object();

	public static bool ModEnabled = true;

	public static bool Debug = false;
}
internal class UI
{
	private static FunctionElement toggle;

	public static void Initialize()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: 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_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_020e: Unknown result type (might be due to invalid IL or missing references)
		//IL_022b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_0265: Unknown result type (might be due to invalid IL or missing references)
		//IL_0282: Unknown result type (might be due to invalid IL or missing references)
		//IL_029f: Unknown result type (might be due to invalid IL or missing references)
		MenuCategory val = MenuManager.CreateCategory("Cartridge Unlimiter", SharedValues.MainMenuColor);
		toggle = val.CreateFunctionElement("Disable", Color.white, (Action)delegate
		{
			ToggleClicked();
		});
		MenuCategory val2 = val.CreateCategory("Cartridge Limit", Color.white);
		MenuCategory val3 = val.CreateCategory("Despawn Timer", Color.white);
		IntElement maxCartridgesCounter = val2.CreateIntElement("Max Cartridges", Color.white, SharedValues.MaxCartridges, 1, 0, int.MaxValue, (Action<int>)delegate(int item)
		{
			ValueEdited(ref SharedValues.MaxCartridges, item, null);
		});
		val2.CreateFunctionElement("Add 10", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, SharedValues.MaxCartridges += 10, (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Add 100", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, SharedValues.MaxCartridges += 100, (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Add 1000", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, SharedValues.MaxCartridges += 1000, (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Remove 10", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, Math.Max(0, SharedValues.MaxCartridges - 10), (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Remove 100", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, Math.Max(0, SharedValues.MaxCartridges - 100), (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Remove 1000", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.MaxCartridges, Math.Max(0, SharedValues.MaxCartridges - 1000), (GenericElement<int>)(object)maxCartridgesCounter);
		});
		val2.CreateFunctionElement("Despawn All", SharedValues.MenuWarningColor, (Action)delegate
		{
			DespawnAllClicked();
		});
		val2.CreateSubPanel("NOTE: If you wanna decrease the Cartridge", SharedValues.MenuWarningColor);
		val2.CreateSubPanel("Limit at any given time, you have to despawn all", SharedValues.MenuWarningColor);
		val2.CreateSubPanel("cartridges to see the effect.", SharedValues.MenuWarningColor);
		FloatElement delayTimerCounter = val3.CreateFloatElement("Despawn Timer (Seconds)", Color.white, SharedValues.DespawnTimerInSeconds, 1f, 0f, 86400f, (Action<float>)delegate(float item)
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, item, null);
		});
		val3.CreateFunctionElement("Add 1m", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, SharedValues.DespawnTimerInSeconds += 60f, (GenericElement<float>)(object)delayTimerCounter);
		});
		val3.CreateFunctionElement("Add 10m", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, SharedValues.DespawnTimerInSeconds += 600f, (GenericElement<float>)(object)delayTimerCounter);
		});
		val3.CreateFunctionElement("Add 1h", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, SharedValues.DespawnTimerInSeconds += 3600f, (GenericElement<float>)(object)delayTimerCounter);
		});
		val3.CreateFunctionElement("Remove 1m", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, Math.Max(0f, SharedValues.DespawnTimerInSeconds - 60f), (GenericElement<float>)(object)delayTimerCounter);
		});
		val3.CreateFunctionElement("Remove 10m", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, Math.Max(0f, SharedValues.DespawnTimerInSeconds - 600f), (GenericElement<float>)(object)delayTimerCounter);
		});
		val3.CreateFunctionElement("Remove 1h", Color.white, (Action)delegate
		{
			ValueEdited(ref SharedValues.DespawnTimerInSeconds, Math.Max(0f, SharedValues.DespawnTimerInSeconds - 3600f), (GenericElement<float>)(object)delayTimerCounter);
		});
	}

	private static void ToggleClicked()
	{
		SharedValues.ModEnabled = !SharedValues.ModEnabled;
		((MenuElement)toggle).SetName(SharedValues.ModEnabled ? "Disable" : "Enable");
		lock (SharedValues.SyncLock)
		{
			List<Gun> list = new List<Gun>();
			foreach (Gun key in SharedValues.GrabbedGuns.Keys)
			{
				if ((Object)(object)key == (Object)null)
				{
					list.Add(key);
				}
			}
			list.ForEach(delegate(Gun gun)
			{
				SharedValues.GrabbedGuns.Remove(gun);
			});
			foreach (Gun key2 in SharedValues.GrabbedGuns.Keys)
			{
				if (SharedValues.ModEnabled)
				{
					key2.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize = SharedValues.MaxCartridges;
					Array.ForEach(((IEnumerable<FirearmCartridge>)Resources.FindObjectsOfTypeAll<FirearmCartridge>()).ToArray(), delegate(FirearmCartridge item)
					{
						item.despawnDelaySeconds = SharedValues.DespawnTimerInSeconds;
					});
				}
				else
				{
					key2.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize = SharedValues.GrabbedGuns[key2];
					Array.ForEach(((IEnumerable<FirearmCartridge>)Resources.FindObjectsOfTypeAll<FirearmCartridge>()).ToArray(), delegate(FirearmCartridge item)
					{
						item.despawnDelaySeconds = SharedValues.DefaultDespawnTimer;
					});
				}
			}
			if (!SharedValues.Debug)
			{
				return;
			}
			foreach (Gun key3 in SharedValues.GrabbedGuns.Keys)
			{
				Melon<Main>.Logger.Msg("---------------------------------------------------------------------------");
				Melon<Main>.Logger.Msg(string.Format("Gun {0} has spawnPolicData {1} after {2} this mod.", ((Object)key3).name, key3.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize, SharedValues.ModEnabled ? "enabling" : "disabling"));
			}
		}
	}

	private static void ValueEdited<T>(ref T toEdit, T value, GenericElement<T> menu)
	{
		toEdit = value;
		menu?.SetValue(value);
		Array.ForEach(((IEnumerable<InventorySlot>)Resources.FindObjectsOfTypeAll<InventorySlot>()).ToArray(), delegate(InventorySlot item)
		{
			if (!((Object)(object)item == (Object)null) && !((Object)(object)item.itemGameObject == (Object)null))
			{
				MethodPatches.ObjectGrabbed(item.itemGameObject);
			}
		});
		if (!SharedValues.Debug)
		{
			return;
		}
		foreach (Gun key in SharedValues.GrabbedGuns.Keys)
		{
			Melon<Main>.Logger.Msg("---------------------------------------------------------------------------");
			Melon<Main>.Logger.Msg($"Gun {((Object)key).name} has spawnPolicData {key.defaultCartridge.cartridgeCaseSpawnable.policyData.maxSize} after changing a value in the menu.");
		}
	}

	private static void DespawnAllClicked()
	{
		Array.ForEach(((IEnumerable<FirearmCartridge>)Resources.FindObjectsOfTypeAll<FirearmCartridge>()).ToArray(), delegate(FirearmCartridge item)
		{
			((SpawnEvents)item).Despawn();
		});
	}
}