Decompiled source of Prison Pocket v1.0.0

BepInEx\plugins\ThirdPocketMod.dll

Decompiled 6 hours ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ThirdPocketMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ThirdPocketMod")]
[assembly: AssemblyTitle("ThirdPocketMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ThirdPocketMod;

[BepInPlugin("com.remiafterdark.prisonpocket", "Prison Pocket", "1.0.0")]
public class ThirdPocketMod : BaseUnityPlugin
{
	public static ManualLogSource Log;

	private void Awake()
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Log.LogInfo((object)"Prison Pocket loaded.");
		new Harmony("com.remiafterdark.prisonpocket").PatchAll();
	}
}
[HarmonyPatch(typeof(Inventory), "Initialize")]
public class Patch_Inventory_Initialize
{
	private static void Postfix(Inventory __instance, ENT_Player p)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Expected O, but got Unknown
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Expected O, but got Unknown
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		PrisonPocketTrinket.EnsureRegistered();
		if (!PrisonPocketTrinket.IsSelectedForCurrentRun())
		{
			return;
		}
		Transform inventoryTransform = GetInventoryTransform(__instance);
		GameObject val = new GameObject("ThirdPocket_Transform");
		if ((Object)(object)inventoryTransform != (Object)null)
		{
			val.transform.SetParent(inventoryTransform, false);
			val.transform.localPosition = new Vector3(0f, 1.5f, 2f);
		}
		else
		{
			val.transform.position = new Vector3(0f, 1.5f, 2f);
			ThirdPocketMod.Log.LogWarning((object)"[ThirdPocket] Inventory transform not found; third pocket may be misplaced.");
		}
		Pocket val2 = new Pocket();
		val2.transform = val.transform;
		val2.key = "__thirdpocket__";
		val2.targetHandID = 0;
		Bag_Handler bagHandler = GetBagHandler(__instance);
		Bag_Pouch pocketAsset = GetPocketAsset(__instance);
		if ((Object)(object)bagHandler != (Object)null && (Object)(object)pocketAsset != (Object)null)
		{
			Pouch val3 = new Pouch();
			val3.pouchVisual = bagHandler.CreateCustomPouch(val.transform.position, pocketAsset);
			val3.maxCapacity = 1;
			val3.maxLargeCapacity = 0;
			val2.pouch = val3;
			List<Pouch> pouches = GetPouches(__instance);
			if (pouches != null)
			{
				pouches.Add(val3);
			}
			else
			{
				ThirdPocketMod.Log.LogError((object)"[ThirdPocket] pouches list was null! Pouch won't be tracked.");
			}
		}
		else
		{
			ThirdPocketMod.Log.LogError((object)$"[ThirdPocket] Could not create pouch visual. bagHandler={bagHandler} pocketAsset={pocketAsset}");
		}
		ThirdPocketTracker.SetThirdPocket(__instance, val2);
		ThirdPocketMod.Log.LogInfo((object)"[ThirdPocket] Prison Pocket selected — third pocket created.");
		NudgeUp(inventoryTransform, "Inventory Text/Inventory - Button Tooltip Text", 0.35f);
		NudgeUp(inventoryTransform, "Perks/Perk Description", 0.35f);
	}

	private static void NudgeUp(Transform root, string path, float yOffset)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		Transform val = (((Object)(object)root != (Object)null) ? root.Find(path) : null);
		if ((Object)(object)val == (Object)null)
		{
			ThirdPocketMod.Log.LogWarning((object)("[ThirdPocket] Could not find '" + path + "' to reposition."));
		}
		else
		{
			val.localPosition += new Vector3(0f, yOffset, 0f);
		}
	}

	private static Transform GetInventoryTransform(Inventory inv)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		return (Transform)(typeof(Inventory).GetField("inventory", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv));
	}

	private static Bag_Handler GetBagHandler(Inventory inv)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		return (Bag_Handler)(typeof(Inventory).GetField("bagHandler", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv));
	}

	private static Bag_Pouch GetPocketAsset(Inventory inv)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		return (Bag_Pouch)(typeof(Inventory).GetField("pocketAsset", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv));
	}

	private static List<Pouch> GetPouches(Inventory inv)
	{
		return (List<Pouch>)(typeof(Inventory).GetField("pouches", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv));
	}
}
[HarmonyPatch(typeof(Inventory), "Update")]
public class Patch_Inventory_Update
{
	private const string FocusButton = "NoHandholds";

	private const string LeftKey = "Pocket-Left";

	private const string RightKey = "Pocket-Right";

	private static void Prefix(Inventory __instance)
	{
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		Pocket thirdPocket = ThirdPocketTracker.GetThirdPocket(__instance);
		if (thirdPocket == null || thirdPocket.pouch == null || CL_GameManager.gMan.lockPlayer || CL_GameManager.gMan.isPaused || CL_GameManager.gMan.lockPlayerInput)
		{
			return;
		}
		ENT_Player player = GetPlayer(__instance);
		if ((Object)(object)player == (Object)null || player.IsLocked() || player.IsInventoryLocked() || !IsFocusModeActive(player))
		{
			return;
		}
		ButtonInput button = InputManager.GetButton("Pocket-Left");
		ButtonInput button2 = InputManager.GetButton("Pocket-Right");
		int num;
		ButtonInput val;
		if (button.Down)
		{
			num = 0;
			val = button;
		}
		else
		{
			if (!button2.Down)
			{
				return;
			}
			num = 1;
			val = button2;
		}
		val.Down = false;
		ItemHand[] itemHands = GetItemHands(__instance);
		if (itemHands == null)
		{
			return;
		}
		ItemHand val2 = itemHands[num];
		bool flag = val2.currentItem != null;
		bool flag2 = thirdPocket.pouch.pouchItems.Count > 0;
		if (!flag && flag2)
		{
			if ((int)player.hands[num].interactState == 0)
			{
				Hand val3 = player.hands[num];
				val3.HideInteractionSprite();
				Item val4 = thirdPocket.pouch.RemoveItemFromPouch();
				__instance.AddItemToHand(val4, val3);
			}
		}
		else if (flag && !flag2 && (val2.currentItem.pocketable || thirdPocket.pouch.maxLargeCapacity > thirdPocket.pouch.pouchItems.Count))
		{
			__instance.DropItemFromHand(thirdPocket.transform.position, num, true, false);
		}
	}

	private static bool IsFocusModeActive(ENT_Player player)
	{
		MethodInfo method = typeof(ENT_Player).GetMethod("IsFocusModeActive", BindingFlags.Instance | BindingFlags.NonPublic);
		if (method == null)
		{
			return InputManager.GetButton("NoHandholds").Pressed;
		}
		return (bool)method.Invoke(player, null);
	}

	private static ItemHand[] GetItemHands(Inventory inv)
	{
		return (ItemHand[])typeof(Inventory).GetField("itemHands", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv);
	}

	private static ENT_Player GetPlayer(Inventory inv)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		return (ENT_Player)(typeof(Inventory).GetField("player", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(inv));
	}
}
public static class ThirdPocketTracker
{
	private static Dictionary<Inventory, Pocket> _pockets = new Dictionary<Inventory, Pocket>();

	public static void SetThirdPocket(Inventory inv, Pocket pocket)
	{
		_pockets[inv] = pocket;
	}

	public static Pocket GetThirdPocket(Inventory inv)
	{
		_pockets.TryGetValue(inv, out var value);
		return value;
	}
}
public static class PrisonPocketTrinket
{
	public const string TrinketName = "Prison Pocket";

	public static Trinket Instance;

	private static bool _registered;

	public static void EnsureRegistered()
	{
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		if (!_registered)
		{
			_registered = true;
			Sprite val = LoadEmbeddedIcon("Prison Pocket.png");
			if ((Object)(object)val == (Object)null)
			{
				ThirdPocketMod.Log.LogWarning((object)"[PrisonPocket] Could not load embedded icon. Trinket will show without an icon.");
			}
			Trinket val2 = ScriptableObject.CreateInstance<Trinket>();
			((Object)val2).name = "Prison Pocket";
			val2.title = "Prison Pocket";
			val2.description = "Grants a hidden third pocket. Hold Focus and tap a pocket key to stash or retrieve an item from it.";
			val2.flavorText = "ITAA policy prohibits keistering contraband.";
			val2.icon = val;
			val2.cost = 1;
			val2.isBinding = false;
			val2.settingBlacklist = new List<string>();
			val2.progressionUnlock = null;
			val2.scoreMultiplierBonus = -0.15f;
			Perk val3 = ScriptableObject.CreateInstance<Perk>();
			((Object)val3).name = "Perk_Prison Pocket";
			val3.id = "thirdpocketmod_prison_pocket";
			val3.title = "Prison Pocket";
			val3.description = val2.description;
			val3.flavorText = val2.flavorText;
			val3.icon = val;
			val3.perkType = (PerkType)8;
			val3.useBuff = false;
			val3.modules = new List<PerkModule>();
			val3.playerTag = new List<string>();
			val3.canStack = false;
			val2.perksToGrant = new List<Perk> { val3 };
			WKAssetDatabase val4 = ScriptableObject.CreateInstance<WKAssetDatabase>();
			val4.trinketAssets = new List<Trinket> { val2 };
			val4.perkAssets = new List<Perk> { val3 };
			CL_AssetManager.AddNewDatabase("ThirdPocketMod_PrisonPocket", val4);
			Instance = val2;
		}
	}

	public static bool IsSelectedForCurrentRun()
	{
		M_Gamemode baseGamemode = CL_GameManager.GetBaseGamemode();
		if ((Object)(object)baseGamemode == (Object)null)
		{
			return false;
		}
		return StatManager.saveData.GetGamemodeTrinkets(baseGamemode.GetGamemodeName(true))?.Contains("Prison Pocket") ?? false;
	}

	private static Sprite LoadEmbeddedIcon(string fileName)
	{
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string text = null;
		string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
		foreach (string text2 in manifestResourceNames)
		{
			if (text2.EndsWith(fileName))
			{
				text = text2;
				break;
			}
		}
		if (text == null)
		{
			return null;
		}
		byte[] array;
		using (Stream stream = executingAssembly.GetManifestResourceStream(text))
		{
			if (stream == null)
			{
				return null;
			}
			array = new byte[stream.Length];
			int num;
			for (int j = 0; j < array.Length; j += num)
			{
				num = stream.Read(array, j, array.Length - j);
				if (num <= 0)
				{
					break;
				}
			}
		}
		Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false, false);
		((Texture)val).filterMode = (FilterMode)0;
		if (!ImageConversion.LoadImage(val, array))
		{
			return null;
		}
		return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
	}
}
[HarmonyPatch(typeof(CL_AssetManager), "InitializeAssetManager")]
public class Patch_CL_AssetManager_InitializeAssetManager
{
	private static void Postfix()
	{
		PrisonPocketTrinket.EnsureRegistered();
	}
}
[HarmonyPatch(typeof(UI_TrinketPicker), "ReloadTrinkets")]
public class Patch_UI_TrinketPicker_ReloadTrinkets
{
	private static void Prefix(UI_TrinketPicker __instance)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Expected O, but got Unknown
		PrisonPocketTrinket.EnsureRegistered();
		if (!((Object)(object)PrisonPocketTrinket.Instance == (Object)null))
		{
			M_Gamemode val = (M_Gamemode)(typeof(UI_TrinketPicker).GetField("currentGamemode", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(__instance));
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val.availableTrinkets == (Object)null) && !val.availableTrinkets.trinkets.Contains(PrisonPocketTrinket.Instance))
			{
				val.availableTrinkets.trinkets.Add(PrisonPocketTrinket.Instance);
			}
		}
	}
}