Decompiled source of KeepInventory v1.1.1

Mods/KeepInventory.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using BoneLib;
using BoneLib.BoneMenu;
using HAHOOS.KeepInventory;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Pool;
using Il2CppSLZ.Marrow.Warehouse;
using Il2CppSystem;
using LabFusion.Data;
using LabFusion.Network;
using LabFusion.Player;
using LabFusion.Utilities;
using MelonLoader;
using MelonLoader.Preferences;
using MelonLoader.Utils;
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: MelonInfo(typeof(Main), "Keep Inventory", "1.1.1", "HAHOOS", "https://thunderstore.io/c/bonelab/p/HAHOOS/KeepInventory/")]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonAuthorColor(0, 255, 165, 0)]
[assembly: MelonColor(0, 255, 72, 59)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("HAHOOS.KeepInventory")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+edca147cf799415b3674d4fe5b531f88097547e3")]
[assembly: AssemblyProduct("HAHOOS.KeepInventory")]
[assembly: AssemblyTitle("HAHOOS.KeepInventory")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace HAHOOS.KeepInventory
{
	public class Main : MelonMod
	{
		public int LightAmmo;

		public int MediumAmmo;

		public int HeavyAmmo;

		private MelonPreferences_Category mp_modCategory;

		private MelonPreferences_Entry mp_itemsaving;

		private MelonPreferences_Entry mp_ammosaving;

		private MelonPreferences_Entry mp_persistentsave;

		private MelonPreferences_Entry mp_blacklistedLevels;

		private MelonPreferences_Category mp_saveCategory;

		private MelonPreferences_Entry mp_itemslots;

		private MelonPreferences_Entry mp_ammolight;

		private MelonPreferences_Entry mp_ammomedium;

		private MelonPreferences_Entry mp_ammoheavy;

		private FunctionElement statusElement;

		private LevelInfo levelInfo;

		private bool HasFusion;

		public Dictionary<string, Barcode> Slots { get; private set; } = new Dictionary<string, Barcode>();


		public override void OnInitializeMelon()
		{
			((MelonBase)this).LoggerInstance.Msg("Setting up KeepInventory");
			SetupPreferences();
			SetupMenu();
			HasFusion = HelperMethods.CheckIfAssemblyLoaded("labfusion");
			Application.quitting += Action.op_Implicit((Action)OnQuit);
			Hooking.OnLevelLoaded += LevelLoadedEvent;
			Hooking.OnLevelUnloaded += LevelUnloadedEvent;
		}

		public void OnQuit()
		{
			if (!(bool)mp_persistentsave.BoxedValue)
			{
				return;
			}
			((MelonBase)this).LoggerInstance.Msg("Saving Preferences");
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (KeyValuePair<string, Barcode> slot in Slots)
			{
				dictionary.Add(slot.Key, slot.Value.ID);
			}
			mp_itemslots.BoxedValue = dictionary;
			mp_ammolight.BoxedValue = LightAmmo;
			mp_ammomedium.BoxedValue = MediumAmmo;
			mp_ammoheavy.BoxedValue = HeavyAmmo;
			mp_saveCategory.SaveToFile(true);
			((MelonBase)this).LoggerInstance.Msg("Saved Preferences successfully");
		}

		private void LevelUnloadedEvent()
		{
			if (((List<string>)mp_blacklistedLevels.BoxedValue).Contains(levelInfo.barcode))
			{
				return;
			}
			Slots.Clear();
			HeavyAmmo = 0;
			MediumAmmo = 0;
			LightAmmo = 0;
			((MelonBase)this).LoggerInstance.Msg("Saving inventory...");
			if ((Object)(object)Player.RigManager == (Object)null)
			{
				((MelonBase)this).LoggerInstance.Msg("RigManager does not exist");
				return;
			}
			if ((Object)(object)Player.RigManager.inventory == (Object)null)
			{
				((MelonBase)this).LoggerInstance.Msg("Inventory does not exist");
				return;
			}
			if ((bool)mp_itemsaving.BoxedValue)
			{
				foreach (SlotContainer item in (Il2CppArrayBase<SlotContainer>)(object)Player.RigManager.inventory.bodySlots)
				{
					if ((Object)(object)item.inventorySlotReceiver != (Object)null && item.inventorySlotReceiver._weaponHost != null)
					{
						Poolee component = ((Component)item.inventorySlotReceiver._weaponHost.GetTransform()).GetComponent<Poolee>();
						if ((Object)(object)component != (Object)null)
						{
							Barcode barcode = ((Scannable)component.SpawnableCrate).Barcode;
							Slots.Add(((Object)item).name, barcode);
						}
					}
				}
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				foreach (KeyValuePair<string, Barcode> slot in Slots)
				{
					dictionary.Add(slot.Key, slot.Value.ID);
				}
				mp_itemslots.BoxedValue = dictionary;
			}
			if ((bool)mp_ammosaving.BoxedValue)
			{
				AmmoInventory instance = AmmoInventory.Instance;
				LightAmmo = instance.GetCartridgeCount("light");
				MediumAmmo = instance.GetCartridgeCount("medium");
				HeavyAmmo = instance.GetCartridgeCount("heavy");
				mp_ammolight.BoxedValue = LightAmmo;
				mp_ammomedium.BoxedValue = MediumAmmo;
				mp_ammoheavy.BoxedValue = HeavyAmmo;
			}
			((MelonBase)this).LoggerInstance.Msg("Successfully saved inventory");
		}

		private void SpawnSavedItems()
		{
			if (Slots.Count < 1)
			{
				return;
			}
			List<SlotContainer> list = ((IEnumerable<SlotContainer>)Player.RigManager.inventory.bodySlots).ToList();
			foreach (KeyValuePair<string, Barcode> item in Slots)
			{
				SlotContainer val = list.Find((SlotContainer slot) => ((Object)slot).name == item.Key);
				if ((Object)(object)val != (Object)null)
				{
					InventorySlotReceiver inventorySlotReceiver = val.inventorySlotReceiver;
					if ((Object)(object)inventorySlotReceiver != (Object)null)
					{
						inventorySlotReceiver.DestroyContents();
						inventorySlotReceiver.SpawnInSlotAsync(item.Value);
					}
				}
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private void RequestItemSpawn(string barcode)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			PooleeUtilities.RequestSpawn(barcode, new SerializedTransform(Player.Head.position + Player.Head.forward * 1.5f, Quaternion.identity), (uint)PlayerIdManager.LocalSmallId);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private void FusionFunction()
		{
			if (NetworkInfo.HasServer)
			{
				foreach (KeyValuePair<string, Barcode> slot in Slots)
				{
					RequestItemSpawn(slot.Value.ID);
				}
				return;
			}
			if ((bool)mp_itemsaving.BoxedValue)
			{
				SpawnSavedItems();
			}
		}

		private async void LevelLoadedEvent(LevelInfo obj)
		{
			//IL_0019: 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)
			levelInfo = obj;
			if (!((List<string>)mp_blacklistedLevels.BoxedValue).Contains(obj.barcode))
			{
				((Element)statusElement).ElementName = "Current level is not blacklisted";
				((Element)statusElement).ElementColor = Color.green;
				((MelonBase)this).LoggerInstance.Msg("Loading inventory...");
				if (HasFusion)
				{
					FusionFunction();
				}
				else if ((bool)mp_itemsaving.BoxedValue)
				{
					SpawnSavedItems();
				}
				if ((bool)mp_ammosaving.BoxedValue)
				{
					while ((Object)(object)AmmoInventory.Instance == (Object)null)
					{
						await Task.Delay(50);
					}
					AmmoInventory ammoInventory = AmmoInventory.Instance;
					ammoInventory.ClearAmmo();
					ammoInventory.AddCartridge(ammoInventory.lightAmmoGroup, LightAmmo);
					ammoInventory.AddCartridge(ammoInventory.mediumAmmoGroup, MediumAmmo);
					ammoInventory.AddCartridge(ammoInventory.heavyAmmoGroup, HeavyAmmo);
				}
				((MelonBase)this).LoggerInstance.Msg("Loaded inventory");
			}
			else
			{
				((MelonBase)this).LoggerInstance.Msg("Not loading inventory because level is blacklisted");
				((Element)statusElement).ElementName = "Current level is blacklisted";
				((Element)statusElement).ElementColor = Color.red;
			}
		}

		private void SetupMenu()
		{
			//IL_000b: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			Page val = Page.Root.CreatePage("HAHOOS", Color.white, 0, true);
			Page val2 = val.CreatePage("KeepInventory", new Color(255f, 72f, 59f), 0, true);
			val2.CreateBool("Save Items", Color.white, (bool)mp_itemsaving.BoxedValue, (Action<bool>)delegate(bool value)
			{
				mp_itemsaving.BoxedValue = value;
				mp_itemsaving.Save();
			});
			val2.CreateBool("Save Ammo", Color.white, (bool)mp_ammosaving.BoxedValue, (Action<bool>)delegate(bool value)
			{
				mp_ammosaving.BoxedValue = value;
				mp_ammosaving.Save();
			});
			val2.CreateBool("Persistent Save", Color.magenta, (bool)mp_persistentsave.BoxedValue, (Action<bool>)delegate(bool value)
			{
				mp_persistentsave.BoxedValue = value;
				mp_persistentsave.Save();
			});
			val2.CreateFunction("Blacklist Level from Saving/Loading", Color.red, (Action)delegate
			{
				//IL_009e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0065: Unknown result type (might be due to invalid IL or missing references)
				List<string> list = (List<string>)mp_blacklistedLevels.BoxedValue;
				if (list.Contains(levelInfo.barcode))
				{
					int num = list.IndexOf(levelInfo.barcode);
					if (num != -1)
					{
						list.RemoveAt(num);
						((Element)statusElement).ElementName = "Current Level is not blacklisted";
						((Element)statusElement).ElementColor = Color.green;
					}
				}
				else
				{
					list.Add(levelInfo.barcode);
					((Element)statusElement).ElementName = "Current Level is blacklisted";
					((Element)statusElement).ElementColor = Color.red;
				}
			});
			statusElement = val2.CreateFunction("Current Level is not blacklisted", Color.green, (Action)delegate
			{
			});
		}

		private void SetupPreferences()
		{
			//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d3: Expected O, but got Unknown
			List<string> list = new List<string>
			{
				"fa534c5a868247138f50c62e424c4144.Level.VoidG114", "SLZ.BONELAB.Content.Level.LevelOutro", "c2534c5a-db71-49cf-b694-24584c657665", "c2534c5a-4197-4879-8cd3-4a695363656e", "c2534c5a-6b79-40ec-8e98-e58c5363656e", "c2534c5a-54df-470b-baaf-741f4c657665", "c2534c5a-56a6-40ab-a8ce-23074c657665", "c2534c5a-7601-4443-bdfe-7f235363656e", "SLZ.BONELAB.Content.Level.LevelBigAnomalyB", "SLZ.BONELAB.Content.Level.LevelStreetPunch",
				"SLZ.BONELAB.Content.Level.SprintBridge04", "SLZ.BONELAB.Content.Level.SceneMagmaGate", "SLZ.BONELAB.Content.Level.MoonBase", "SLZ.BONELAB.Content.Level.LevelKartRace", "c2534c5a-c056-4883-ac79-e051426f6964", "c2534c5a-162f-4661-a04d-975d5363656e", "fa534c5a868247138f50c62e424c4144.Level.LevelArenaMin", "c2534c5a-c180-40e0-b2b7-325c5363656e", "c2534c5a-de61-4df9-8f6c-416954726547", "c2534c5a-4f3b-480e-ad2f-69175363656e",
				"c2534c5a-80e1-4a29-93ca-f3254d656e75"
			};
			mp_modCategory = MelonPreferences.CreateCategory("HAHOOS_KeepInventory_Settings");
			mp_itemsaving = (MelonPreferences_Entry)(object)mp_modCategory.CreateEntry<bool>("ItemSaving", true, "Item Saving", "If true, will save and load items in inventory", false, false, (ValueValidator)null, (string)null);
			mp_ammosaving = (MelonPreferences_Entry)(object)mp_modCategory.CreateEntry<bool>("AmmoSaving", true, "Ammo Saving", "If true, will save and load ammo in inventory", false, false, (ValueValidator)null, (string)null);
			mp_persistentsave = (MelonPreferences_Entry)(object)mp_modCategory.CreateEntry<bool>("PersistentSave", true, "Persistent Save", "If true, will save and load inventory in a KeepInventory_Save.cfg file to be used between sessions", false, false, (ValueValidator)null, (string)null);
			mp_blacklistedLevels = (MelonPreferences_Entry)(object)mp_modCategory.CreateEntry<List<string>>("BlacklistedLevels", list, "Blacklisted Levels", "List of levels that will not save/load inventory", false, false, (ValueValidator)null, (string)null);
			mp_saveCategory = MelonPreferences.CreateCategory("HAHOOS_KeepInventory_Save");
			mp_itemslots = (MelonPreferences_Entry)(object)mp_saveCategory.CreateEntry<Dictionary<string, string>>("ItemSlots", new Dictionary<string, string>(), "Item Slots", "Saved items in the inventory", false, false, (ValueValidator)null, (string)null);
			mp_ammolight = (MelonPreferences_Entry)(object)mp_saveCategory.CreateEntry<int>("AmmoLight", 0, "Ammo Light", "Saved ammo of type Light", false, false, (ValueValidator)null, (string)null);
			mp_ammomedium = (MelonPreferences_Entry)(object)mp_saveCategory.CreateEntry<int>("AmmoMedium", 0, "Ammo Medium", "Saved ammo of type Medium", false, false, (ValueValidator)null, (string)null);
			mp_ammoheavy = (MelonPreferences_Entry)(object)mp_saveCategory.CreateEntry<int>("AmmoHeavy", 0, "Ammo Heavy", "Saved ammo of type Heavy", false, false, (ValueValidator)null, (string)null);
			mp_saveCategory.SetFilePath(Path.Combine(MelonEnvironment.UserDataDirectory, "KeepInventory_Save.cfg"));
			if (!(bool)mp_persistentsave.BoxedValue)
			{
				return;
			}
			Slots.Clear();
			foreach (KeyValuePair<string, string> item in (Dictionary<string, string>)mp_itemslots.BoxedValue)
			{
				Slots.Add(item.Key, new Barcode(item.Value.ToString()));
			}
			try
			{
				LightAmmo = int.Parse(mp_ammolight.BoxedValue.ToString());
				MediumAmmo = int.Parse(mp_ammomedium.BoxedValue.ToString());
				HeavyAmmo = int.Parse(mp_ammoheavy.BoxedValue.ToString());
			}
			catch (Exception)
			{
				((MelonBase)this).LoggerInstance.Error("Could not parse saved data from String to Int, don't tell me you tried to modify the file yourself..");
			}
		}
	}
}