Decompiled source of Udderly Easy Backpack Key v1.0.0

BackpackKey.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.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;
		}
	}
}
[BepInPlugin("dairymods.backpackkey", "Backpack Key", "1.0.0")]
public class BackpackPlugin : BaseUnityPlugin
{
	private static ManualLogSource log;

	private static ConfigEntry<Key> key;

	private static bool open;

	private static GUIManager labelled;

	private static TextMeshProUGUI icon;

	private static TextMeshProUGUI label;

	private static ConfigEntry<float> labelOffset;

	private static bool hidFour;

	private void Awake()
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		log = ((BaseUnityPlugin)this).Logger;
		key = ((BaseUnityPlugin)this).Config.Bind<Key>("Keybinds", "Backpack Key", (Key)16, "Hold to open the backpack on your back.");
		labelOffset = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "Label Offset", 40f, "How far right of the 4 the key picture sits. Restart to apply.");
		new Harmony("dairymods.backpackkey").PatchAll(typeof(BackpackPlugin));
	}

	private static bool KeyHeld()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		if ((int)key.Value != 0 && Keyboard.current != null)
		{
			return ((ButtonControl)Keyboard.current[key.Value]).isPressed;
		}
		return false;
	}

	public static bool InteractHeld(CharacterInput input)
	{
		if (!input.interactIsPressed)
		{
			if (open)
			{
				return KeyHeld();
			}
			return false;
		}
		return true;
	}

	[HarmonyPatch(typeof(BackpackWheel), "Update")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> WheelUpdate(IEnumerable<CodeInstruction> instructions)
	{
		FieldInfo field = AccessTools.Field(typeof(CharacterInput), "interactIsPressed");
		MethodInfo helper = AccessTools.Method(typeof(BackpackPlugin), "InteractHeld", (Type[])null, (Type[])null);
		int swapped = 0;
		foreach (CodeInstruction instruction in instructions)
		{
			if (CodeInstructionExtensions.LoadsField(instruction, field, false))
			{
				instruction.opcode = OpCodes.Call;
				instruction.operand = helper;
				swapped++;
			}
			yield return instruction;
		}
		if (swapped == 0)
		{
			log.LogError((object)"BackpackWheel.Update no longer reads interactIsPressed: Backpack Key will not hold the wheel open.");
		}
		else
		{
			log.LogInfo((object)"Backpack Key hooked BackpackWheel.Update");
		}
	}

	private static void LabelHud()
	{
		//IL_0254: Unknown result type (might be due to invalid IL or missing references)
		//IL_025a: Invalid comparison between Unknown and I4
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		GUIManager instance = GUIManager.instance;
		if (!Object.op_Implicit((Object)(object)instance) || !Object.op_Implicit((Object)(object)instance.backpack))
		{
			return;
		}
		if ((Object)(object)instance != (Object)(object)labelled)
		{
			labelled = instance;
			TextMeshProUGUI[] componentsInChildren = ((Component)instance.backpack).GetComponentsInChildren<TextMeshProUGUI>(true);
			icon = ((IEnumerable<TextMeshProUGUI>)componentsInChildren).FirstOrDefault((Func<TextMeshProUGUI, bool>)((TextMeshProUGUI t) => ((Object)t).name == "UI_InputIcon"));
			if (!Object.op_Implicit((Object)(object)icon))
			{
				log.LogWarning((object)("No UI_InputIcon on backpack HUD slot. Texts found: " + string.Join(", ", componentsInChildren.Select((TextMeshProUGUI t) => ((Object)t).name + "='" + ((TMP_Text)t).text + "'"))));
				return;
			}
			label = Object.Instantiate<TextMeshProUGUI>(icon, ((TMP_Text)icon).transform.parent);
			((Object)label).name = "BackpackKeyLabel";
			InputIcon[] components = ((Component)label).GetComponents<InputIcon>();
			for (int num = 0; num < components.Length; num++)
			{
				Object.DestroyImmediate((Object)(object)components[num]);
			}
			foreach (Transform item in ((TMP_Text)label).transform)
			{
				((Component)item).gameObject.SetActive(false);
			}
			((TMP_Text)label).rectTransform.anchoredPosition = ((TMP_Text)icon).rectTransform.anchoredPosition + new Vector2(labelOffset.Value, 0f);
			string value = null;
			(AccessTools.Field(typeof(InputSpriteData), "inputPathToSpriteTagKeyboard")?.GetValue(SingletonAsset<InputSpriteData>.Instance) as Dictionary<string, string>)?.TryGetValue(((object)key.Value/*cast due to .constrained prefix*/).ToString().ToLower(), out value);
			((TMP_Text)label).text = value ?? ((object)key.Value/*cast due to .constrained prefix*/).ToString();
		}
		if (Object.op_Implicit((Object)(object)label))
		{
			BackpackSlot val = ((!Object.op_Implicit((Object)(object)Character.localCharacter)) ? null : Character.localCharacter.player?.backpackSlot);
			bool flag = val != null && !((ItemSlot)val).IsEmpty() && (int)val.backpackType != 4;
			if (((Component)label).gameObject.activeSelf != flag)
			{
				((Component)label).gameObject.SetActive(flag);
			}
			Character localCharacter = Character.localCharacter;
			bool flag2 = !Object.op_Implicit((Object)(object)localCharacter) || (val != null && !((ItemSlot)val).IsEmpty()) || Object.op_Implicit((Object)(object)localCharacter.data.carriedPlayer);
			if (!flag2 && ((Behaviour)icon).enabled)
			{
				((Behaviour)icon).enabled = false;
				hidFour = true;
			}
			else if (flag2 && hidFour)
			{
				((Behaviour)icon).enabled = true;
				hidFour = false;
			}
		}
	}

	private void Update()
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		LabelHud();
		Character localCharacter = Character.localCharacter;
		if (!Object.op_Implicit((Object)(object)localCharacter) || !Application.isFocused || !KeyHeld())
		{
			open = false;
		}
		else if (!open && ((ButtonControl)Keyboard.current[key.Value]).wasPressedThisFrame)
		{
			CharacterData data = localCharacter.data;
			if (!data.dead && !data.passedOut && !data.usingBackpackWheel && !(data.currentItem is Backpack))
			{
				open = TryOpen(localCharacter) || (Object.op_Implicit((Object)(object)data.carriedPlayer) && TryOpen(data.carriedPlayer));
			}
		}
	}

	private static bool TryOpen(Character owner)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Invalid comparison between Unknown and I4
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		BackpackSlot val = owner.player?.backpackSlot;
		if (val == null || ((ItemSlot)val).IsEmpty() || (int)val.backpackType == 4)
		{
			return false;
		}
		BackpackReference fromEquippedBackpack = BackpackReference.GetFromEquippedBackpack(owner);
		BackpackVisuals visuals = ((BackpackReference)(ref fromEquippedBackpack)).GetVisuals();
		BackpackOnBackVisuals val2 = (BackpackOnBackVisuals)(object)((visuals is BackpackOnBackVisuals) ? visuals : null);
		if (!Object.op_Implicit((Object)(object)val2))
		{
			log.LogWarning((object)$"No worn-pack visuals for {val.backpackType}, assuming 4 slots");
		}
		GUIManager.instance.OpenBackpackWheel(fromEquippedBackpack, Object.op_Implicit((Object)(object)val2) ? val2.slotCount : 4, val.backpackType);
		return true;
	}

	[HarmonyPatch(typeof(CharacterBackpackHandler), "AddFuelToJetpack")]
	[HarmonyPrefix]
	private static void FuelAmount(CharacterBackpackHandler __instance, Character interactor, ref float addedFuel)
	{
		if (!Object.op_Implicit((Object)(object)interactor) || (Object)(object)interactor.refs.backpackHandler != (Object)(object)__instance)
		{
			return;
		}
		Item currentItem = interactor.data.currentItem;
		if (Object.op_Implicit((Object)(object)currentItem))
		{
			int num = 25;
			RopeSpool val = default(RopeSpool);
			if (((Component)currentItem).TryGetComponent<RopeSpool>(ref val))
			{
				num = Mathf.CeilToInt(val.RopeFuel);
			}
			else if (currentItem.HasData((DataEntryKey)10))
			{
				num = currentItem.overrideJetpackFuelAmount + Mathf.RoundToInt(currentItem.GetData<FloatItemData>((DataEntryKey)10).Value * currentItem.overrideJetpackFuelPerFuelMult);
			}
			else if (currentItem is Backpack)
			{
				num = currentItem.overrideJetpackFuelAmount;
			}
			else if (currentItem.overrideJetpackFuel)
			{
				num = currentItem.overrideJetpackFuelAmount + (currentItem.HasData((DataEntryKey)2) ? (currentItem.GetData<OptionableIntItemData>((DataEntryKey)2).Value * currentItem.overrideJetpackFuelPerUse) : 0);
			}
			addedFuel = num;
		}
	}
}
internal static class Draw
{
	private static GUIStyle style;

	public static Camera Cam
	{
		get
		{
			MainCamera instance = MainCamera.instance;
			Camera val = (Object.op_Implicit((Object)(object)instance) ? ((Component)instance).GetComponent<Camera>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return Camera.main;
			}
			return val;
		}
	}

	public static bool ToGui(Camera cam, Vector3 world, out Vector2 gui)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: 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_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = cam.WorldToViewportPoint(world);
		gui = new Vector2(val.x * (float)Screen.width, (1f - val.y) * (float)Screen.height);
		return val.z > 0f;
	}

	public static void Line(Vector2 a, Vector2 b, Color c, float width)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: 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_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		Matrix4x4 matrix = GUI.matrix;
		Vector2 val = b - a;
		GUI.color = c;
		GUIUtility.RotateAroundPivot(Mathf.Atan2(val.y, val.x) * 57.29578f, a);
		GUI.DrawTexture(new Rect(a.x, a.y - width / 2f, ((Vector2)(ref val)).magnitude, width), (Texture)(object)Texture2D.whiteTexture);
		GUI.matrix = matrix;
		GUI.color = Color.white;
	}

	public static void Dot(Vector2 p, Color c, float size)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = c;
		GUI.DrawTexture(new Rect(p.x - size / 2f, p.y - size / 2f, size, size), (Texture)(object)Texture2D.whiteTexture);
		GUI.color = Color.white;
	}

	public static void Text(Rect r, string s, Color c, int size = 18, TextAnchor anchor = (TextAnchor)0)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: 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)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: 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_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		if (style == null)
		{
			style = new GUIStyle(GUI.skin.label)
			{
				fontStyle = (FontStyle)1,
				wordWrap = false
			};
		}
		style.fontSize = size;
		style.alignment = anchor;
		style.normal.textColor = new Color(0f, 0f, 0f, c.a * 0.8f);
		GUI.Label(new Rect(((Rect)(ref r)).x + 2f, ((Rect)(ref r)).y + 2f, ((Rect)(ref r)).width, ((Rect)(ref r)).height), s, style);
		style.normal.textColor = c;
		GUI.Label(r, s, style);
	}
}