Decompiled source of Medic v1.0.0

Medic.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
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.Configuration;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("headclef")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+a79070a0ced3589b0a77e82f0ca23b59356fcde4")]
[assembly: AssemblyProduct("Medic")]
[assembly: AssemblyTitle("Medic")]
[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 Medic
{
	[BepInPlugin("headclef.Medic", "Medic", "1.0.0")]
	public class Medic : BaseUnityPlugin
	{
		private const string PluginGuid = "headclef.Medic";

		private const string PluginName = "Medic";

		private const string PluginVersion = "1.0.0";

		internal static ConfigEntry<KeyboardShortcut> ReviveKey;

		internal static ConfigEntry<float> ReviveRange;

		internal static ConfigEntry<float> ReviveCooldown;

		internal static ConfigEntry<int> ReviveHealth;

		internal static Medic Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_0054: Expected O, but got Unknown
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			BindConfiguration();
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		private void OnDestroy()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void BindConfiguration()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Expected O, but got Unknown
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Expected O, but got Unknown
			ReviveKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Revive", "Revive Key", new KeyboardShortcut((KeyCode)114, Array.Empty<KeyCode>()), "Key to press to revive a nearby dead player.");
			ReviveRange = ((BaseUnityPlugin)this).Config.Bind<float>("Revive", "Revive Range", 3f, new ConfigDescription("Maximum distance to a dead player to revive them.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 15f), Array.Empty<object>()));
			ReviveCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Revive", "Revive Cooldown", 5f, new ConfigDescription("Cooldown in seconds between revive attempts.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 60f), Array.Empty<object>()));
			ReviveHealth = ((BaseUnityPlugin)this).Config.Bind<int>("Revive", "Revive Health", 100, new ConfigDescription("Health the revived player starts with.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 200), Array.Empty<object>()));
		}
	}
}
namespace Medic.Patches
{
	[HarmonyPatch]
	internal static class RevivePatch
	{
		private static float _lastReviveTime;

		[HarmonyPatch(typeof(PlayerController), "Update")]
		[HarmonyPostfix]
		private static void PlayerController_Update_Postfix(PlayerController __instance)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)__instance != (Object)(object)PlayerController.instance)
				{
					return;
				}
				KeyboardShortcut value = Medic.ReviveKey.Value;
				if (!((KeyboardShortcut)(ref value)).IsDown())
				{
					return;
				}
				float value2 = Medic.ReviveCooldown.Value;
				if (Time.time - _lastReviveTime < value2)
				{
					Medic.Logger.LogDebug((object)$"Revive on cooldown ({value2 - (Time.time - _lastReviveTime):F1}s remaining)");
					return;
				}
				PlayerAvatar playerAvatarScript = __instance.playerAvatarScript;
				if ((Object)(object)playerAvatarScript == (Object)null || playerAvatarScript.deadSet)
				{
					return;
				}
				float value3 = Medic.ReviveRange.Value;
				Vector3 position = ((Component)__instance).transform.position;
				PlayerAvatar val = null;
				float num = float.MaxValue;
				List<PlayerAvatar> list = SemiFunc.PlayerGetAll();
				if (list == null)
				{
					return;
				}
				foreach (PlayerAvatar item in list)
				{
					if (!((Object)(object)item == (Object)(object)playerAvatarScript) && item.deadSet)
					{
						float num2 = Vector3.Distance(position, ((Component)item).transform.position);
						if (num2 <= value3 && num2 < num)
						{
							val = item;
							num = num2;
						}
					}
				}
				if ((Object)(object)val == (Object)null)
				{
					Medic.Logger.LogDebug((object)"No dead players in range to revive.");
					return;
				}
				RevivePlayer(val);
				_lastReviveTime = Time.time;
			}
			catch (Exception ex)
			{
				Medic.Logger.LogError((object)("Revive exception: " + ex.Message));
			}
		}

		private static void RevivePlayer(PlayerAvatar deadPlayer)
		{
			try
			{
				int value = Medic.ReviveHealth.Value;
				string text = deadPlayer.playerName ?? "Unknown";
				Medic.Logger.LogInfo((object)$"Reviving {text} with {value} HP...");
				deadPlayer.Revive(false);
				if ((Object)(object)deadPlayer.playerHealth != (Object)null)
				{
					deadPlayer.playerHealth.health = value;
				}
				Medic.Logger.LogInfo((object)("Revived " + text + " successfully!"));
			}
			catch (Exception ex)
			{
				Medic.Logger.LogError((object)("RevivePlayer exception: " + ex.Message));
			}
		}
	}
}