Decompiled source of QuiverRecall v0.1.0

QuiverRecall.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BetterArchery;
using Common;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("QuiverRecall")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("QuiverRecall")]
[assembly: AssemblyTitle("QuiverRecall")]
[assembly: AssemblyVersion("0.1.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 QuiverRecall
{
	[BepInPlugin("Manke.QuiverRecall", "QuiverRecall", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "Manke.QuiverRecall";

		public const string Name = "QuiverRecall";

		public const string Version = "0.1.0";

		private const string BetterArcheryGuid = "ishid4.mods.betterarchery";

		private const string QuiverSlotName = "quiver";

		private const int QuiverSlots = 3;

		private const string RecordKey = "Manke.QuiverRecall";

		private const char Separator = '|';

		private const float RetrySeconds = 5f;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		private static float _restoreUntil = -1f;

		private static readonly Action<Inventory, bool, bool> InventoryChanged = AccessTools.MethodDelegate<Action<Inventory, bool, bool>>(AccessTools.Method(typeof(Inventory), "Changed", (Type[])null, (Type[])null), (object)null, true);

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Equip the quiver and put the arrows back in its slots when you take everything from your tombstone.");
			Harmony val = new Harmony("Manke.QuiverRecall");
			val.Patch((MethodBase)AccessTools.Method(typeof(Character), "CheckDeath", (Type[])null, (Type[])null), new HarmonyMethod(AccessTools.Method(typeof(Plugin), "CheckDeathPrefix", (Type[])null, (Type[])null), 1000, (string[])null, (string[])null, (bool?)null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(TombStone), "OnTakeAllSuccess", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "TombstoneTaken", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(InventoryGui), "OnTakeAll", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnTakeAllPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private void Update()
		{
			if (!(_restoreUntil < 0f))
			{
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || Restore(localPlayer) || Time.time > _restoreUntil)
				{
					_restoreUntil = -1f;
				}
			}
		}

		private static void CheckDeathPrefix(Character __instance)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			if (Enabled.Value && (Object)(object)__instance == (Object)(object)Player.m_localPlayer && !__instance.IsDead() && __instance.GetHealth() <= 0f)
			{
				SaveRecord((Player)__instance);
			}
		}

		private static void TombstoneTaken()
		{
			_restoreUntil = Time.time + 5f;
		}

		private static void OnTakeAllPostfix(Container ___m_currentContainer)
		{
			if ((Object)(object)___m_currentContainer != (Object)null && (Object)(object)((Component)___m_currentContainer).GetComponent<TombStone>() != (Object)null)
			{
				TombstoneTaken();
			}
		}

		private static void SaveRecord(Player player)
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			Inventory inventory = ((Humanoid)player).GetInventory();
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				allItem.m_customData.Remove("Manke.QuiverRecall");
			}
			ItemData slotItem = CustomSlotCreator.GetSlotItem((Humanoid)(object)player, "quiver");
			if (slotItem == null)
			{
				return;
			}
			string[] array = new string[5]
			{
				Game.instance.GetPlayerProfile().GetPlayerID().ToString(),
				"-1",
				null,
				null,
				null
			};
			ItemData ammoItem = ((Humanoid)player).GetAmmoItem();
			for (int i = 0; i < 3; i++)
			{
				Vector2i quiverSlotPosition = BetterArchery.GetQuiverSlotPosition(i);
				ItemData val = ((quiverSlotPosition.x < 0) ? null : inventory.GetItemAt(quiverSlotPosition.x, quiverSlotPosition.y));
				array[i + 2] = val?.m_shared.m_name ?? "";
				if (val != null && val == ammoItem)
				{
					array[1] = i.ToString();
				}
			}
			slotItem.m_customData["Manke.QuiverRecall"] = string.Join('|'.ToString(), array);
		}

		private static bool Restore(Player player)
		{
			Inventory inventory = ((Humanoid)player).GetInventory();
			string text = Game.instance.GetPlayerProfile().GetPlayerID().ToString();
			ItemData val = null;
			string[] array = null;
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (allItem.m_customData.TryGetValue("Manke.QuiverRecall", out var value))
				{
					string[] array2 = value.Split('|');
					if (array2.Length == 5 && array2[0] == text)
					{
						val = allItem;
						array = array2;
						break;
					}
				}
			}
			if (val == null)
			{
				return true;
			}
			if (!BetterArchery.IsQuiverEquipped())
			{
				val.m_equipped = false;
				((Humanoid)player).EquipItem(val, false);
				if (!BetterArchery.IsQuiverEquipped())
				{
					return false;
				}
			}
			if (PlaceArrows(inventory, array))
			{
				InventoryChanged(inventory, arg2: false, arg3: false);
			}
			SelectArrows(player, inventory, array);
			if (array.Skip(2).All(string.IsNullOrEmpty))
			{
				val.m_customData.Remove("Manke.QuiverRecall");
			}
			else
			{
				val.m_customData["Manke.QuiverRecall"] = string.Join('|'.ToString(), array);
			}
			Log.LogDebug((object)("Quiver restored, record now: " + string.Join('|'.ToString(), array)));
			return true;
		}

		private static bool PlaceArrows(Inventory inventory, string[] fields)
		{
			//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_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			bool result = false;
			for (int i = 0; i < 3; i++)
			{
				string name = fields[i + 2];
				Vector2i quiverSlotPosition = BetterArchery.GetQuiverSlotPosition(i);
				if (string.IsNullOrEmpty(name) || quiverSlotPosition.x < 0)
				{
					continue;
				}
				if (inventory.GetItemAt(quiverSlotPosition.x, quiverSlotPosition.y) != null)
				{
					fields[i + 2] = "";
					continue;
				}
				ItemData val = ((IEnumerable<ItemData>)inventory.GetAllItems()).FirstOrDefault((Func<ItemData, bool>)((ItemData item) => item.m_shared.m_name == name && !BetterArchery.IsQuiverSlot(item.m_gridPos)));
				if (val != null)
				{
					val.m_gridPos = quiverSlotPosition;
					fields[i + 2] = "";
					result = true;
				}
			}
			return result;
		}

		private static void SelectArrows(Player player, Inventory inventory, string[] fields)
		{
			//IL_0016: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			if (int.TryParse(fields[1], out var result) && result >= 0 && result < 3)
			{
				Vector2i quiverSlotPosition = BetterArchery.GetQuiverSlotPosition(result);
				ItemData itemAt = inventory.GetItemAt(quiverSlotPosition.x, quiverSlotPosition.y);
				if (itemAt != null && !((Humanoid)player).IsItemEquiped(itemAt))
				{
					((Humanoid)player).EquipItem(itemAt, false);
				}
				if (itemAt != null)
				{
					fields[1] = "-1";
				}
			}
		}
	}
}