Decompiled source of Tacos Backpack v3.0.0

TacosBackpack.dll

Decompiled 5 minutes ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2CppFishNet.Connection;
using Il2CppFishNet.Object;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppScheduleOne.DevUtilities;
using Il2CppScheduleOne.ItemFramework;
using Il2CppScheduleOne.Networking;
using Il2CppScheduleOne.Persistence.Datas;
using Il2CppScheduleOne.PlayerScripts;
using Il2CppScheduleOne.Storage;
using Il2CppScheduleOne.Tools;
using Il2CppScheduleOne.UI;
using Il2CppScheduleOne.UI.Phone;
using Il2CppSteamworks;
using Il2CppSystem;
using MelonLoader;
using MelonLoader.Preferences;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using TacosBackpack;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Mod), "Tacos Backpack", "3.0.0", "Taco", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("TacosBackpack")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TacosBackpack")]
[assembly: AssemblyTitle("TacosBackpack")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 TacosBackpack
{
	public class Mod : MelonMod
	{
		private const int Slots = 20;

		private const string Title = "Backpack";

		private const float SaveEvery = 5f;

		private const float LoadDelay = 3f;

		private static MelonPreferences_Entry<KeyCode> _key;

		private static StorageEntity _bag;

		private static IntPtr _bagPtr = IntPtr.Zero;

		private bool _inWorld;

		private bool _loaded;

		private bool _shown;

		private float _playerSince = -1f;

		private float _nextSave;

		private string _file;

		private string _lastJson;

		private static string Folder => Path.Combine(MelonEnvironment.UserDataDirectory, "TacosBackpack");

		public override void OnInitializeMelon()
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			MelonPreferences_Category obj = MelonPreferences.CreateCategory("TacosBackpack", "Tacos Backpack");
			obj.SetFilePath(Path.Combine(MelonEnvironment.UserDataDirectory, "TacosBackpack.cfg"));
			_key = obj.CreateEntry<KeyCode>("ToggleKey", (KeyCode)98, "Key that opens and closes the backpack", (string)null, false, false, (ValueValidator)null, (string)null);
			obj.SaveToFile(false);
			Directory.CreateDirectory(Folder);
			Redirect("SetStoredInstance", "LocalStoredInstance");
			Redirect("SetItemSlotQuantity", "LocalQuantity");
			Redirect("SetSlotLocked", "LocalLock");
			Redirect("SetSlotFilter", "LocalFilter");
			((MelonBase)this).LoggerInstance.Msg("Ready. Press " + ((object)_key.Value/*cast due to .constrained prefix*/).ToString() + " in a world to open your backpack.");
		}

		private void Redirect(string method, string prefix)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			try
			{
				((MelonBase)this).HarmonyInstance.Patch((MethodBase)AccessTools.Method(typeof(StorageEntity), method, (Type[])null, (Type[])null), new HarmonyMethod(typeof(Mod), prefix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Error("Could not hook " + method + ": " + ex.Message);
			}
		}

		private static bool IsBag(StorageEntity s)
		{
			if ((Object)(object)s != (Object)null && _bagPtr != IntPtr.Zero)
			{
				return ((Il2CppObjectBase)s).Pointer == _bagPtr;
			}
			return false;
		}

		public static bool LocalStoredInstance(StorageEntity __instance, NetworkConnection conn, int itemSlotIndex, ItemInstance instance)
		{
			if (!IsBag(__instance))
			{
				return true;
			}
			__instance.RpcLogic___SetStoredInstance_Internal_2652194801(conn, itemSlotIndex, instance);
			return false;
		}

		public static bool LocalQuantity(StorageEntity __instance, int itemSlotIndex, int quantity)
		{
			if (!IsBag(__instance))
			{
				return true;
			}
			__instance.RpcLogic___SetItemSlotQuantity_Internal_1692629761(itemSlotIndex, quantity);
			return false;
		}

		public static bool LocalLock(StorageEntity __instance, NetworkConnection conn, int itemSlotIndex, bool locked, NetworkObject lockOwner, string lockReason)
		{
			if (!IsBag(__instance))
			{
				return true;
			}
			__instance.RpcLogic___SetSlotLocked_Internal_3170825843(conn, itemSlotIndex, locked, lockOwner, lockReason);
			return false;
		}

		public static bool LocalFilter(StorageEntity __instance, NetworkConnection conn, int itemSlotIndex, SlotFilter filter)
		{
			if (!IsBag(__instance))
			{
				return true;
			}
			__instance.RpcLogic___SetSlotFilter_Internal_527532783(conn, itemSlotIndex, filter);
			return false;
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			_inWorld = sceneName == "Main";
			_loaded = false;
			_shown = false;
			_playerSince = -1f;
			_file = null;
			_lastJson = null;
		}

		public override void OnSceneWasUnloaded(int buildIndex, string sceneName)
		{
			if (!(sceneName != "Main"))
			{
				_inWorld = false;
				_loaded = false;
				_shown = false;
				_bag = null;
				_bagPtr = IntPtr.Zero;
			}
		}

		public override void OnApplicationQuit()
		{
			try
			{
				if (_inWorld && _loaded)
				{
					WriteFile();
				}
			}
			catch
			{
			}
		}

		private void MakeBag()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			GameObject val = new GameObject("TacosBackpack");
			val.SetActive(false);
			StorageEntity obj = val.AddComponent<StorageEntity>();
			obj.SlotCount = 20;
			obj.DisplayRowCount = 4;
			obj.StorageEntityName = "Backpack";
			obj.EmptyOnSleep = false;
			val.SetActive(true);
			_bag = obj;
			_bagPtr = ((Il2CppObjectBase)obj).Pointer;
		}

		public override void OnUpdate()
		{
			if (!_inWorld || (Object)(object)Player.Local == (Object)null)
			{
				return;
			}
			float unscaledTime = Time.unscaledTime;
			try
			{
				if ((Object)(object)_bag == (Object)null)
				{
					MakeBag();
					_playerSince = unscaledTime;
				}
				if (!_loaded)
				{
					if (unscaledTime - _playerSince >= 3f)
					{
						ReadFile();
					}
					return;
				}
				Toggle();
				if (unscaledTime >= _nextSave)
				{
					_nextSave = unscaledTime + 5f;
					WriteFile();
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning(ex.Message);
				_nextSave = unscaledTime + 30f;
			}
		}

		private void Toggle()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			StorageMenu instance = Singleton<StorageMenu>.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if (_shown && !instance.IsOpen)
			{
				_shown = false;
			}
			if (!Input.GetKeyDown(_key.Value))
			{
				return;
			}
			if (_shown)
			{
				instance.Close();
				_shown = false;
			}
			else
			{
				if (instance.IsOpen || Cursor.visible)
				{
					return;
				}
				Phone instance2 = PlayerSingleton<Phone>.Instance;
				if (!((Object)(object)instance2 != (Object)null) || !instance2.IsOpen)
				{
					ManagementClipboard instance3 = Singleton<ManagementClipboard>.Instance;
					if (!((Object)(object)instance3 != (Object)null) || !instance3.IsEquipped)
					{
						instance.Open(((Il2CppObjectBase)_bag).Cast<IItemSlotOwner>(), "Backpack", string.Empty, (Action)null);
						_shown = true;
					}
				}
			}
		}

		private static string WhoseWorld()
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: 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_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			string text = null;
			try
			{
				Lobby instance = Singleton<Lobby>.Instance;
				if ((Object)(object)instance != (Object)null && instance.IsInLobby && instance.LobbyID != 0L)
				{
					text = SteamMatchmaking.GetLobbyOwner(new CSteamID(instance.LobbyID)).m_SteamID.ToString();
				}
			}
			catch
			{
			}
			if (string.IsNullOrEmpty(text) || text == "0")
			{
				try
				{
					text = SteamUser.GetSteamID().m_SteamID.ToString();
				}
				catch
				{
					text = "me";
				}
			}
			string text2 = "0";
			try
			{
				text2 = GameManager.Seed.ToString();
			}
			catch
			{
			}
			return text + "_" + text2;
		}

		private void ReadFile()
		{
			string text = WhoseWorld();
			_file = Path.Combine(Folder, text + ".json");
			if (!File.Exists(_file))
			{
				string[] files = Directory.GetFiles(Folder, text.Split('_')[0] + "_*.json");
				if (files.Length == 1)
				{
					_file = files[0];
				}
			}
			if (File.Exists(_file))
			{
				string text2 = File.ReadAllText(_file);
				DeserializedItemSet val = default(DeserializedItemSet);
				if (ItemSet.TryDeserialize(text2, ref val))
				{
					val.LoadTo(_bag.ItemSlots);
					_lastJson = text2;
					((MelonBase)this).LoggerInstance.Msg("Backpack loaded (" + _bag.ItemCount + " items).");
				}
				else
				{
					((MelonBase)this).LoggerInstance.Warning("Couldn't read " + _file + "; it is left untouched.");
				}
			}
			else
			{
				((MelonBase)this).LoggerInstance.Msg("Empty backpack for this world.");
			}
			_loaded = true;
			_nextSave = Time.unscaledTime + 5f;
		}

		private void WriteFile()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_bag == (Object)null || _file == null)
			{
				return;
			}
			string jSON = new ItemSet(_bag.ItemSlots).GetJSON();
			if (!string.IsNullOrWhiteSpace(jSON) && !(jSON == _lastJson))
			{
				string text = _file + ".tmp";
				File.WriteAllText(text, jSON);
				if (File.Exists(_file))
				{
					File.Copy(_file, _file + ".bak", overwrite: true);
				}
				File.Copy(text, _file, overwrite: true);
				File.Delete(text);
				_lastJson = jSON;
			}
		}
	}
}