Decompiled source of RechargeableDefibro v1.0.0

RechargeableDefibro.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
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 Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RechargeableDefibro")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RechargeableDefibro")]
[assembly: AssemblyTitle("RechargeableDefibro")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 RechargeableDefibro
{
	public class DefibroState : MonoBehaviour
	{
		public bool isDepleted = false;

		public bool reviveFired = false;
	}
	internal static class Helpers
	{
		private static readonly FieldInfo _batteryLifeInt = typeof(ItemBattery).GetField("batteryLifeInt", BindingFlags.Instance | BindingFlags.NonPublic);

		private static int GetBatteryLifeInt(ItemBattery b)
		{
			return ((int?)_batteryLifeInt?.GetValue(b)).GetValueOrDefault();
		}

		internal static void CheckAndReactivate(ItemBattery battery)
		{
			ItemReviveItem component = ((Component)battery).GetComponent<ItemReviveItem>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			DefibroState component2 = ((Component)battery).GetComponent<DefibroState>();
			if ((Object)(object)component2 == (Object)null || !component2.isDepleted)
			{
				return;
			}
			int batteryLifeInt = GetBatteryLifeInt(battery);
			int batteryBars = battery.batteryBars;
			Plugin.Log.LogDebug((object)$"[RechargeableDefibro] ChargeTick bars={batteryLifeInt}/{batteryBars}  life={battery.batteryLife:F3}");
			if (batteryLifeInt >= batteryBars)
			{
				component2.isDepleted = false;
				component2.reviveFired = false;
				PhysGrabObject component3 = ((Component)component).GetComponent<PhysGrabObject>();
				if ((Object)(object)component3 != (Object)null)
				{
					component3.dead = false;
				}
				Reflect.CallHardResetVisuals(component);
				Plugin.Log.LogInfo((object)"[RechargeableDefibro] Defibro fully recharged — ready to revive again!");
			}
		}
	}
	internal static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "RechargeableDefibro";

		public const string PLUGIN_NAME = "RechargeableDefibro";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	internal static class Reflect
	{
		private static readonly FieldInfo _isInReleaseSequence = typeof(ItemReviveItem).GetField("isInReleaseSequence", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo _playerTarget = typeof(ItemReviveItem).GetField("playerTarget", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly MethodInfo _batteryInit = typeof(ItemBattery).GetMethod("BatteryInit", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly MethodInfo _hardResetVisuals = typeof(ItemReviveItem).GetMethod("HardResetVisuals", BindingFlags.Instance | BindingFlags.NonPublic);

		public static void ClearReleaseSequence(ItemReviveItem r)
		{
			_isInReleaseSequence?.SetValue(r, false);
		}

		public static void CallBatteryInit(ItemBattery b)
		{
			_batteryInit?.Invoke(b, null);
		}

		public static void CallHardResetVisuals(ItemReviveItem r)
		{
			_hardResetVisuals?.Invoke(r, null);
		}

		public static PlayerAvatar GetPlayerTarget(ItemReviveItem r)
		{
			object? obj = _playerTarget?.GetValue(r);
			return (PlayerAvatar)((obj is PlayerAvatar) ? obj : null);
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "Start")]
	internal static class Patch_Start
	{
		private static void Postfix(ItemReviveItem __instance)
		{
			if (!((Object)(object)((Component)__instance).GetComponent<ItemBattery>() != (Object)null))
			{
				ItemBattery val = ((Component)__instance).gameObject.AddComponent<ItemBattery>();
				val.batteryBars = 1;
				val.batteryLife = 1f;
				val.autoDrain = false;
				val.batteryDrainRate = 0f;
				val.hasBatteryVisuals = true;
				val.isUnchargable = false;
				Reflect.CallBatteryInit(val);
				if ((Object)(object)((Component)__instance).GetComponent<DefibroState>() == (Object)null)
				{
					((Component)__instance).gameObject.AddComponent<DefibroState>();
				}
				Plugin.Log.LogInfo((object)"[RechargeableDefibro] Added ItemBattery to Defibro.");
			}
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "BatteryLookAt")]
	internal static class Patch_BatteryLookAt
	{
		private static bool Prefix(ItemBattery __instance)
		{
			return (Object)(object)((Component)__instance).GetComponent<ItemReviveItem>() == (Object)null;
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "BatteryUpdateBars")]
	internal static class Patch_BatteryUpdateBars
	{
		private static bool Prefix(ItemBattery __instance)
		{
			return (Object)(object)((Component)__instance).GetComponent<ItemReviveItem>() == (Object)null;
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "BatteryChargingVisuals")]
	internal static class Patch_BatteryChargingVisuals
	{
		private static bool Prefix(ItemBattery __instance)
		{
			return (Object)(object)((Component)__instance).GetComponent<ItemReviveItem>() == (Object)null;
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "ReviveAndDestroy")]
	internal static class Patch_ReviveAndDestroy
	{
		private static void Prefix(ItemReviveItem __instance)
		{
			DefibroState defibroState = ((Component)__instance).GetComponent<DefibroState>();
			if ((Object)(object)defibroState == (Object)null)
			{
				defibroState = ((Component)__instance).gameObject.AddComponent<DefibroState>();
			}
			defibroState.reviveFired = true;
			Plugin.Log.LogInfo((object)"[RechargeableDefibro] Defibro fired — reviveFired set.");
		}

		private static void Postfix(ItemReviveItem __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			if (!((Object)(object)component == (Object)null) && component.reviveFired)
			{
				Reflect.ClearReleaseSequence(__instance);
				component.isDepleted = true;
				ItemBattery component2 = ((Component)__instance).GetComponent<ItemBattery>();
				if ((Object)(object)component2 != (Object)null)
				{
					component2.RemoveFullBar(component2.batteryBars);
				}
				Reflect.CallHardResetVisuals(__instance);
				PhysGrabObject component3 = ((Component)__instance).GetComponent<PhysGrabObject>();
				if ((Object)(object)component3 != (Object)null)
				{
					component3.dead = false;
				}
				Plugin.Log.LogInfo((object)"[RechargeableDefibro] Visuals reset, battery drained, grab restored.");
			}
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "TeleportEffectRPC")]
	internal static class Patch_TeleportEffectRPC
	{
		private static void Postfix(ItemReviveItem __instance)
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			if ((Object)(object)component == (Object)null || !component.reviveFired)
			{
				return;
			}
			PlayerAvatar playerTarget = Reflect.GetPlayerTarget(__instance);
			if (!((Object)(object)playerTarget == (Object)null))
			{
				PhysGrabObject component2 = ((Component)__instance).GetComponent<PhysGrabObject>();
				if ((Object)(object)component2 != (Object)null)
				{
					((Component)__instance).transform.position = ((Component)playerTarget).transform.position;
					component2.dead = false;
					Plugin.Log.LogInfo((object)"[RechargeableDefibro] Teleported Defibro to truck with player.");
				}
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabObject), "DestroyPhysGrabObject")]
	internal static class Patch_DestroyPhysGrabObject
	{
		private static bool Prefix(PhysGrabObject __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			if ((Object)(object)component == (Object)null || !component.reviveFired)
			{
				return true;
			}
			__instance.dead = false;
			Plugin.Log.LogInfo((object)"[RechargeableDefibro] Destroy blocked.");
			return false;
		}
	}
	[HarmonyPatch(typeof(PhysGrabObject), "DestroyPhysGrabObjectRPC")]
	internal static class Patch_DestroyPhysGrabObjectRPC
	{
		private static bool Prefix(PhysGrabObject __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			if ((Object)(object)component == (Object)null || !component.reviveFired)
			{
				return true;
			}
			__instance.dead = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "Update")]
	internal static class Patch_Update
	{
		private static bool Prefix(ItemReviveItem __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			return (Object)(object)component == (Object)null || !component.isDepleted;
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "FixedUpdate")]
	internal static class Patch_FixedUpdate
	{
		private static bool Prefix(ItemReviveItem __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			return (Object)(object)component == (Object)null || !component.isDepleted;
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "HeldChanged")]
	internal static class Patch_HeldChanged
	{
		private static bool Prefix(ItemReviveItem __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			return (Object)(object)component == (Object)null || !component.isDepleted;
		}
	}
	[HarmonyPatch(typeof(ItemReviveItem), "EquipChanged")]
	internal static class Patch_EquipChanged
	{
		private static bool Prefix(ItemReviveItem __instance)
		{
			DefibroState component = ((Component)__instance).GetComponent<DefibroState>();
			return (Object)(object)component == (Object)null || !component.isDepleted;
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "ChargeBattery")]
	internal static class Patch_ChargeBattery
	{
		private static void Postfix(ItemBattery __instance)
		{
			Helpers.CheckAndReactivate(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "BatteryFullPercentChange")]
	internal static class Patch_BatteryFullPercentChange
	{
		private static void Postfix(ItemBattery __instance)
		{
			Helpers.CheckAndReactivate(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemBattery), "SetBatteryLife")]
	internal static class Patch_SetBatteryLife
	{
		private static void Postfix(ItemBattery __instance)
		{
			Helpers.CheckAndReactivate(__instance);
		}
	}
	[BepInPlugin("RechargeableDefibro", "RechargeableDefibro", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private readonly Harmony _harmony = new Harmony("RechargeableDefibro");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony.PatchAll();
			Log.LogInfo((object)"RechargeableDefibro v1.0.0 loaded — Defibro is now rechargeable!");
		}

		private void OnDestroy()
		{
			_harmony.UnpatchSelf();
		}
	}
}