Decompiled source of BackPackRedux v0.8.0

BackpackRedux.dll

Decompiled 11 months 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 BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		int result;
		if (!extra)
		{
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				if (instance == null || !instance.HasFocus())
				{
					result = (Menu.IsVisible() ? 1 : 0);
					goto IL_005f;
				}
			}
			result = 1;
			goto IL_005f;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d9;
			}
		}
		result2 = 1;
		goto IL_00d9;
		IL_005f:
		return (byte)result != 0;
		IL_00d9:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value.ToLower());
		}
		catch
		{
			return false;
		}
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			return Input.GetKey(value.ToLower());
		}
		catch
		{
			return !req;
		}
	}
}
namespace BackpackRedux;

[BepInPlugin("aedenthorn.BackpackRedux", "Backpack Redux", "0.8.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(FejdStartup), "LoadMainScene")]
	private static class LoadMainScene_Patch
	{
		private static void Prefix(FejdStartup __instance, List<PlayerProfile> ___m_profiles)
		{
			if (modEnabled.Value && ___m_profiles != null)
			{
				PlayerProfile val = ___m_profiles.Find((PlayerProfile p) => p.GetFilename() == (string)typeof(Game).GetField("m_profileFilename", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null));
				if (val != null)
				{
					backpackFileName = Path.GetFileNameWithoutExtension(val.GetFilename()) + "_backpack";
					LoadBackpackInventory();
				}
			}
		}
	}

	[HarmonyPatch(typeof(Humanoid), "IsTeleportable")]
	private static class Humanoid_IsTeleportable_Patch
	{
		private static void Postfix(Humanoid __instance, ref bool __result)
		{
			if (modEnabled.Value && __result && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && backpackInventory != null && !backpackInventory.IsTeleportable())
			{
				__result = allowTeleportingMetal.Value;
			}
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "Update")]
	private static class InventoryGui_Update_Patch
	{
		private static void Postfix(Animator ___m_animator, ref Container ___m_currentContainer)
		{
			if (modEnabled.Value && AedenthornUtils.CheckKeyDown(hotKey.Value) && Object.op_Implicit((Object)(object)Player.m_localPlayer) && ___m_animator.GetBool("visible"))
			{
				if (opening)
				{
					opening = false;
				}
				else if ((Object)(object)___m_currentContainer != (Object)null && (Object)(object)___m_currentContainer == (Object)(object)backpackContainer)
				{
					backpackInventory = backpackContainer.GetInventory();
					___m_currentContainer = null;
				}
				else if (CanOpenBackpack())
				{
					OpenBackpack();
				}
			}
		}
	}

	[HarmonyPatch(typeof(Inventory), "GetTotalWeight")]
	[HarmonyPriority(0)]
	private static class GetTotalWeight_Patch
	{
		private static void Postfix(Inventory __instance, ref float __result)
		{
			if (!modEnabled.Value || !Object.op_Implicit((Object)(object)backpackContainer) || !Object.op_Implicit((Object)(object)Player.m_localPlayer))
			{
				return;
			}
			if (__instance == ((Humanoid)Player.m_localPlayer).GetInventory())
			{
				if (new StackFrame(2).ToString().IndexOf("OverrideGetTotalWeight") <= -1)
				{
					__result += backpackInventory.GetTotalWeight();
				}
			}
			else if (__instance == backpackInventory)
			{
				__result *= backpackWeightMult.Value;
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	private static class OnDeath_Patch
	{
		private static void Prefix(Player __instance)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer) || backpackInventory == null || __instance.GetPlayerID() != Player.m_localPlayer.GetPlayerID() || backpackInventory.NrOfItems() == 0 || !dropInventoryOnDeath.Value)
			{
				return;
			}
			if (createTombStone.Value)
			{
				GameObject val = Object.Instantiate<GameObject>(__instance.m_tombstone, ((Character)__instance).GetCenterPoint() + Vector3.forward, ((Component)__instance).transform.rotation);
				val.GetComponent<Container>().GetInventory().MoveInventoryToGrave(backpackInventory);
				TombStone component = val.GetComponent<TombStone>();
				PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
				component.Setup(playerProfile.GetName(), playerProfile.GetPlayerID());
				return;
			}
			List<ItemData> allItems = backpackInventory.GetAllItems();
			foreach (ItemData item in allItems)
			{
				Vector3 val2 = ((Component)__instance).transform.position + Vector3.up * 0.5f + Random.insideUnitSphere * 0.3f;
				Quaternion val3 = Quaternion.Euler(0f, (float)Random.Range(0, 360), 0f);
				ItemDrop.DropItem(item, 0, val2, val3);
				backpackInventory.RemoveAll();
			}
		}
	}

	[HarmonyPatch(typeof(Game), "SavePlayerProfile")]
	private static class SavePlayerProfile_Patch
	{
		private static void Prefix()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			if (modEnabled.Value && backpackInventory != null)
			{
				ZPackage val = new ZPackage();
				backpackInventory.Save(val);
				string @base = val.GetBase64();
				File.WriteAllText(Path.Combine(assetPath, backpackFileName), @base);
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				if (Object.op_Implicit((Object)(object)Player.m_localPlayer))
				{
					LoadBackpackInventory();
				}
				__instance.AddString(text);
				__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
				return false;
			}
			return true;
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<string> hotKey;

	public static ConfigEntry<string> backpackName;

	public static ConfigEntry<string> backpackGUID;

	public static ConfigEntry<string> backpackItem;

	public static ConfigEntry<Vector2> backpackSize;

	public static ConfigEntry<float> backpackWeightMult;

	public static ConfigEntry<bool> dropInventoryOnDeath;

	public static ConfigEntry<bool> createTombStone;

	public static ConfigEntry<bool> allowTeleportingMetal;

	private static Container backpackContainer;

	private static Inventory backpackInventory;

	private static string assetPath;

	private static bool opening = false;

	private static string backpackFileName;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1333, "Nexus mod ID for updates");
		hotKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HotKey", "b", "Hotkey to open backpack.");
		allowTeleportingMetal = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowTeleportingMetal", false, "Allow teleporting even if backpack has unteleportable items.");
		backpackName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "BackpackName", "Backpack", "Display name for backpack.");
		backpackItem = ((BaseUnityPlugin)this).Config.Bind<string>("General", "BackpackItem", "", "Required item to equip in order to open backpack. Leave blank to allow opening backpack without item equipped.");
		backpackSize = ((BaseUnityPlugin)this).Config.Bind<Vector2>("General", "BackpackSize", new Vector2(6f, 3f), "Size of backpack (w,h).");
		backpackWeightMult = ((BaseUnityPlugin)this).Config.Bind<float>("General", "BackpackWeightMult", 0.5f, "Multiplier for weight of items in backpack (set to 0 to disable backpack weight).");
		dropInventoryOnDeath = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DropInventoryOnDeath", true, "Drop backpack inventory on death");
		createTombStone = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "CreateTombStone", true, "If DropInventoryOnDeath then create tombstone rather than just dropping inventory.");
		if (modEnabled.Value)
		{
			assetPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), typeof(BepInExPlugin).Namespace);
			if (!Directory.Exists(assetPath))
			{
				Dbgl("Creating mod folder");
				Directory.CreateDirectory(assetPath);
			}
			harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			harmony.PatchAll();
		}
	}

	private void Start()
	{
		try
		{
			Dbgl("Searching for old backpack mod");
			((Behaviour)Chainloader.PluginInfos.First((KeyValuePair<string, PluginInfo> p) => p.Key == "aedenthorn.Backpack").Value.Instance).enabled = false;
			Dbgl("Disabled old backpack");
		}
		catch
		{
			Dbgl("Old plugin not found.");
		}
	}

	private void Update()
	{
		if (modEnabled.Value && Object.op_Implicit((Object)(object)Player.m_localPlayer) && Object.op_Implicit((Object)(object)ZNetScene.instance) && !AedenthornUtils.IgnoreKeyPresses(extra: true) && AedenthornUtils.CheckKeyDown(hotKey.Value) && CanOpenBackpack())
		{
			opening = true;
			OpenBackpack();
		}
	}

	private static bool CanOpenBackpack()
	{
		return backpackItem.Value == "" || ((Humanoid)Player.m_localPlayer).GetInventory().GetEquippedItems().Exists(delegate(ItemData i)
		{
			GameObject dropPrefab = i.m_dropPrefab;
			return ((dropPrefab != null) ? ((Object)dropPrefab).name : null) == backpackItem.Value;
		});
	}

	private static void OpenBackpack()
	{
		backpackContainer = ((Component)Player.m_localPlayer).gameObject.GetComponent<Container>();
		if ((Object)(object)backpackContainer == (Object)null)
		{
			backpackContainer = ((Component)Player.m_localPlayer).gameObject.AddComponent<Container>();
		}
		backpackContainer.m_name = backpackName.Value;
		AccessTools.FieldRefAccess<Container, Inventory>(backpackContainer, "m_inventory") = backpackInventory;
		InventoryGui.instance.Show(backpackContainer, 1);
	}

	private static void LoadBackpackInventory()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: 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_0036: Expected O, but got Unknown
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Expected O, but got Unknown
		backpackInventory = new Inventory(backpackName.Value, (Sprite)null, (int)backpackSize.Value.x, (int)backpackSize.Value.y);
		if (File.Exists(Path.Combine(assetPath, backpackFileName)))
		{
			try
			{
				string text = File.ReadAllText(Path.Combine(assetPath, backpackFileName));
				ZPackage val = new ZPackage(text);
				backpackInventory.Load(val);
			}
			catch (Exception arg)
			{
				Dbgl($"Backpack file corrupt!\n{arg}");
			}
		}
	}
}