Decompiled source of SenealCapeOnLogin v1.0.4

Cjayride.SenealCapeOnLogin.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.SenealCapeOnLogin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+59020e396c0bc0447c4959230ebf75a37044ce8f")]
[assembly: AssemblyProduct("Cjayride.SenealCapeOnLogin")]
[assembly: AssemblyTitle("Cjayride.SenealCapeOnLogin")]
[assembly: AssemblyVersion("1.0.4.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 Cjayride.SenealCapeOnLogin
{
	[BepInPlugin("cjayride.SenealCapeOnLogin", "SeneaL Cape On Login", "1.0.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "cjayride.SenealCapeOnLogin";

		public const string PluginName = "SeneaL Cape On Login";

		public const string PluginVersion = "1.0.4";

		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("cjayride.SenealCapeOnLogin").PatchAll();
			if (!SenealCapeSlot.Available)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"SeneaL CapeSlot API not found; cape re-apply will not run.");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Re-applies SeneaL UI cape slot after load when a backpack uses the shoulder slot.");
			}
		}
	}
	internal static class SenealInventorySlots
	{
		private static readonly Func<Inventory, bool> Ready;

		private static readonly Action<Inventory, ItemData> Relocate;

		private static readonly Action<Inventory> Changed;

		internal static bool Available
		{
			get
			{
				if (Relocate != null)
				{
					return Ready != null;
				}
				return false;
			}
		}

		static SenealInventorySlots()
		{
			Type type = AccessTools.TypeByName("SeneaLUI.Inv.Slots");
			if (!(type == null))
			{
				Ready = DelegateFor<Func<Inventory, bool>>(AccessTools.Method(type, "Ready", (Type[])null, (Type[])null));
				Relocate = DelegateFor<Action<Inventory, ItemData>>(AccessTools.Method(type, "Relocate", (Type[])null, (Type[])null));
				Changed = DelegateFor<Action<Inventory>>(AccessTools.Method(type, "Changed", (Type[])null, (Type[])null));
			}
		}

		internal static void PlaceInEquipRow(Inventory inventory, ItemData item)
		{
			if (!Available || inventory == null || item == null)
			{
				return;
			}
			try
			{
				if (Ready(inventory))
				{
					Relocate(inventory, item);
					Changed?.Invoke(inventory);
				}
			}
			catch
			{
			}
		}

		private static T DelegateFor<T>(MethodInfo method) where T : Delegate
		{
			if (!(method == null))
			{
				return AccessTools.MethodDelegate<T>(method, (object)null, true);
			}
			return null;
		}
	}
	internal static class SenealBackpacks
	{
		private static readonly Func<ItemData, bool> IsBackpackFn;

		private static readonly FieldRef<bool> On;

		internal static bool Ready
		{
			get
			{
				if (On != null)
				{
					return On.Invoke();
				}
				return false;
			}
		}

		static SenealBackpacks()
		{
			Type type = AccessTools.TypeByName("SeneaLUI.Mods.Backpacks");
			if (!(type == null))
			{
				IsBackpackFn = DelegateFor<Func<ItemData, bool>>(AccessTools.Method(type, "IsBackpack", (Type[])null, (Type[])null));
				FieldInfo fieldInfo = AccessTools.Field(type, "On");
				if (fieldInfo != null)
				{
					On = AccessTools.StaticFieldRefAccess<bool>(fieldInfo);
				}
			}
		}

		internal static bool IsBackpack(ItemData item)
		{
			if (item == null || IsBackpackFn == null)
			{
				return false;
			}
			try
			{
				return IsBackpackFn(item);
			}
			catch
			{
				return false;
			}
		}

		private static T DelegateFor<T>(MethodInfo method) where T : Delegate
		{
			if (!(method == null))
			{
				return AccessTools.MethodDelegate<T>(method, (object)null, true);
			}
			return null;
		}
	}
	internal static class SenealCapeSlot
	{
		private static readonly Action<Humanoid, ItemData, bool> Set;

		private static readonly Action<Player> UpdateModifiers;

		private static readonly Action<Humanoid, float> UpdateEquipment;

		private static readonly Func<ItemData, bool> IsCapeFn;

		private static readonly FieldRef<ItemData> SlotItem;

		private static readonly FieldRef<Humanoid> Owner;

		private static readonly FieldRef<Humanoid, ItemData> ShoulderItem;

		internal static bool Available
		{
			get
			{
				if (Set != null && SlotItem != null)
				{
					return UpdateModifiers != null;
				}
				return false;
			}
		}

		static SenealCapeSlot()
		{
			ShoulderItem = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_shoulderItem");
			Type type = AccessTools.TypeByName("SeneaLUI.Mods.CapeSlot");
			if (!(type == null))
			{
				Set = DelegateFor<Action<Humanoid, ItemData, bool>>(AccessTools.Method(type, "Set", (Type[])null, (Type[])null));
				UpdateModifiers = DelegateFor<Action<Player>>(AccessTools.Method(type, "UpdateModifiers", (Type[])null, (Type[])null));
				UpdateEquipment = DelegateFor<Action<Humanoid, float>>(AccessTools.Method(type, "UpdateEquipment", (Type[])null, (Type[])null));
				IsCapeFn = DelegateFor<Func<ItemData, bool>>(AccessTools.Method(type, "IsCape", (Type[])null, (Type[])null));
				FieldInfo fieldInfo = AccessTools.Field(type, "Item");
				if (fieldInfo != null)
				{
					SlotItem = AccessTools.StaticFieldRefAccess<ItemData>(fieldInfo);
				}
				FieldInfo fieldInfo2 = AccessTools.Field(type, "_owner");
				if (fieldInfo2 != null)
				{
					Owner = AccessTools.StaticFieldRefAccess<Humanoid>(fieldInfo2);
				}
			}
		}

		internal static bool TryReapply(Player player)
		{
			if (!Available || (Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)Player.m_localPlayer)
			{
				return false;
			}
			if (!SenealBackpacks.Ready)
			{
				return false;
			}
			Inventory inventory = ((Humanoid)player).GetInventory();
			if (inventory == null)
			{
				return false;
			}
			ItemData val = ShoulderItem.Invoke((Humanoid)(object)player);
			if (val != null && !SenealBackpacks.IsBackpack(val))
			{
				return false;
			}
			ItemData val2 = SlotItem.Invoke();
			ItemData val3 = ResolveCape(inventory, val2);
			if (val3 == null)
			{
				return false;
			}
			if ((Object)(object)((Owner != null) ? Owner.Invoke() : null) != (Object)(object)player || val2 != val3 || !val3.m_equipped)
			{
				Set((Humanoid)(object)player, val3, arg3: false);
			}
			UpdateModifiers(player);
			UpdateEquipment?.Invoke((Humanoid)(object)player, 0f);
			SenealInventorySlots.PlaceInEquipRow(inventory, val3);
			return SlotItem.Invoke() == val3;
		}

		private static ItemData ResolveCape(Inventory inventory, ItemData slotItem)
		{
			if (slotItem != null && inventory.ContainsItem(slotItem))
			{
				return slotItem;
			}
			ItemData val = null;
			ItemData val2 = null;
			List<ItemData> allItems = inventory.GetAllItems();
			int count = allItems.Count;
			for (int i = 0; i < count; i++)
			{
				ItemData val3 = allItems[i];
				if (IsCape(val3))
				{
					if (val2 == null)
					{
						val2 = val3;
					}
					if (val3.m_equipped)
					{
						val = val3;
						break;
					}
				}
			}
			return val ?? val2;
		}

		private static bool IsCape(ItemData item)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Invalid comparison between Unknown and I4
			if (item?.m_shared == null)
			{
				return false;
			}
			if (IsCapeFn != null)
			{
				try
				{
					return IsCapeFn(item);
				}
				catch
				{
				}
			}
			if ((int)item.m_shared.m_itemType == 17)
			{
				return !SenealBackpacks.IsBackpack(item);
			}
			return false;
		}

		private static T DelegateFor<T>(MethodInfo method) where T : Delegate
		{
			if (!(method == null))
			{
				return AccessTools.MethodDelegate<T>(method, (object)null, true);
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(Player), "Load")]
	internal static class PlayerLoadReapplyCape
	{
		private static void Postfix(Player __instance)
		{
			CapeReapplyScheduler.ScheduleReapply(__instance);
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned", new Type[] { typeof(bool) })]
	internal static class PlayerOnSpawnedReapplyCape
	{
		private static void Postfix(Player __instance)
		{
			CapeReapplyScheduler.ScheduleReapply(__instance);
		}
	}
	[HarmonyPatch(typeof(Player), "OnRespawn")]
	internal static class PlayerOnRespawnReapplyCape
	{
		private static void Postfix(Player __instance)
		{
			CapeReapplyScheduler.ScheduleReapply(__instance);
		}
	}
	internal static class CapeReapplyScheduler
	{
		private static int _generation;

		internal static void ScheduleReapply(Player player)
		{
			if (!((Object)(object)player != (Object)(object)Player.m_localPlayer) && SenealCapeSlot.Available)
			{
				((MonoBehaviour)player).StartCoroutine(ReapplyAfterLoad(player, ++_generation));
			}
		}

		private static IEnumerator ReapplyAfterLoad(Player player, int generation)
		{
			for (int i = 0; i < 4; i++)
			{
				yield return null;
			}
			for (int i = 0; i < 90; i++)
			{
				if (generation != _generation)
				{
					break;
				}
				if ((Object)(object)player == (Object)null)
				{
					break;
				}
				if ((Object)(object)player != (Object)(object)Player.m_localPlayer)
				{
					break;
				}
				if (SenealCapeSlot.TryReapply(player))
				{
					break;
				}
				yield return null;
				yield return null;
			}
		}
	}
}