Decompiled source of Stay Loaded v1.0.0

BepInEx/plugins/StayLoaded/StayLoaded.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("xNeliox")]
[assembly: AssemblyProduct("Stay Loaded")]
[assembly: AssemblyVersion("1.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;
		}
	}
}
namespace StayLoaded
{
	[BepInPlugin("xNeliox.stayloaded", "Stay Loaded", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private Harmony harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			harmony = new Harmony("xNeliox.stayloaded");
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Stay Loaded 1.0.0 by xNeliox loaded.");
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	internal static class LoadedState
	{
		internal sealed class Context
		{
			internal int UnequipDepth;
		}

		internal const string Key = "xNeliox.stayloaded.loaded";

		internal static readonly ConditionalWeakTable<Player, Context> Contexts = new ConditionalWeakTable<Player, Context>();

		internal static readonly FieldRef<Player, ItemData> Loaded = AccessTools.FieldRefAccess<Player, ItemData>("m_weaponLoaded");

		internal static readonly Action<Player, ItemData> SetLoaded = AccessTools.MethodDelegate<Action<Player, ItemData>>(AccessTools.Method(typeof(Player), "SetWeaponLoaded", (Type[])null, (Type[])null), (object)null, true);

		internal static bool Eligible(ItemData item)
		{
			if (item != null && item.m_shared != null && item.m_shared.m_attack != null)
			{
				return item.m_shared.m_attack.m_requiresReload;
			}
			return false;
		}

		internal static void Restore(Player player, ItemData item)
		{
			if (!((Object)(object)player != (Object)(object)Player.m_localPlayer) && Eligible(item) && ((Humanoid)player).GetCurrentWeapon() == item && item.m_customData.TryGetValue("xNeliox.stayloaded.loaded", out var value) && value == "1")
			{
				SetLoaded(player, item);
			}
		}
	}
	[HarmonyPatch(typeof(Player), "SetWeaponLoaded")]
	internal static class RememberCompletedReload
	{
		private static void Postfix(Player __instance, ItemData weapon)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer && LoadedState.Eligible(weapon))
			{
				weapon.m_customData["xNeliox.stayloaded.loaded"] = "1";
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "UnequipItem")]
	internal static class PreserveWhenUnequipping
	{
		private static void Prefix(Humanoid __instance, out LoadedState.Context __state)
		{
			__state = null;
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (val != null && (Object)(object)val == (Object)(object)Player.m_localPlayer)
			{
				__state = LoadedState.Contexts.GetOrCreateValue(val);
				__state.UnequipDepth++;
			}
		}

		private static Exception Finalizer(Exception __exception, LoadedState.Context __state)
		{
			if (__state != null)
			{
				__state.UnequipDepth--;
			}
			return __exception;
		}
	}
	[HarmonyPatch(typeof(Player), "ResetLoadedWeapon")]
	internal static class ConsumeLoadedState
	{
		private static void Prefix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && LoadedState.Contexts.GetOrCreateValue(__instance).UnequipDepth <= 0)
			{
				LoadedState.Loaded.Invoke(__instance)?.m_customData.Remove("xNeliox.stayloaded.loaded");
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "EquipItem")]
	internal static class RestoreOnEquip
	{
		private static void Postfix(Humanoid __instance, ItemData item, bool __result)
		{
			if (__result)
			{
				Player val = (Player)(object)((__instance is Player) ? __instance : null);
				if (val != null)
				{
					LoadedState.Restore(val, item);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Player), "UpdateWeaponLoading")]
	internal static class RestoreBeforeReloadCheck
	{
		private static void Prefix(Player __instance, ItemData weapon)
		{
			LoadedState.Restore(__instance, weapon);
		}
	}
	[HarmonyPatch(typeof(ItemData), "GetTooltip", new Type[] { typeof(int) })]
	internal static class LoadedTooltip
	{
		private static void Postfix(ItemData __instance, ref string __result)
		{
			if (LoadedState.Eligible(__instance) && __instance.m_customData.TryGetValue("xNeliox.stayloaded.loaded", out var value) && value == "1")
			{
				__result = __result + "\n<color=#a8d98a>" + LoadedText() + "</color>";
			}
		}

		internal static string LoadedText()
		{
			Localization instance = Localization.instance;
			switch (((instance != null) ? instance.GetSelectedLanguage() : null) ?? "English")
			{
			case "German":
				return "Geladen – schussbereit";
			case "French":
				return "Chargée – prête à tirer";
			case "Spanish":
				return "Cargada – lista para disparar";
			case "Italian":
				return "Carica – pronta a sparare";
			case "Portuguese":
			case "Portuguese_Brazilian":
				return "Carregada – pronta para disparar";
			case "Polish":
				return "Załadowana – gotowa do strzału";
			case "Russian":
				return "Заряжено – готово к выстрелу";
			case "Chinese":
				return "已装填 – 可以射击";
			case "Japanese":
				return "装填済み – 発射可能";
			case "Korean":
				return "장전됨 – 발사 준비 완료";
			default:
				return "Loaded – ready to fire";
			}
		}
	}
}