Decompiled source of BetterDefib v1.0.0

BepInEx/plugins/HungryMuttis.BetterDefib.dll

Decompiled a year 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BetterDefib.Enums;
using BetterDefib.Features;
using BetterDefib.Patches;
using Microsoft.CodeAnalysis;
using MonoMod.RuntimeDetour.HookGen;
using On;
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("HungryMuttis.BetterDefib")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+47911b9b06b3aaf87b647fac82ab5e35c315f89c")]
[assembly: AssemblyProduct("BetterDefib")]
[assembly: AssemblyTitle("HungryMuttis.BetterDefib")]
[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 BetterDefib
{
	[ContentWarningPlugin("HungryMuttis.BetterDefib", "1.0.0", false)]
	[BepInPlugin("HungryMuttis.BetterDefib", "BetterDefib", "1.0.0")]
	public class BetterDefib : BaseUnityPlugin
	{
		private FeatureManager Manager = null;

		public static BetterDefib Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			if (!((BaseUnityPlugin)this).Config.Bind<bool>("", "Enabled", true, "Enables this mod").Value)
			{
				Logger.LogInfo((object)"HungryMuttis.BetterDefib v1.0.0 is disabled.");
				return;
			}
			Manager = new FeatureManager(Logger, ((BaseUnityPlugin)this).Config);
			Logger.LogDebug((object)"Hooking...");
			Manager.InitializeFeatures();
			Logger.LogDebug((object)"Finished Hooking!");
			Logger.LogInfo((object)"HungryMuttis.BetterDefib v1.0.0 has loaded!");
		}

		internal static void UnhookAll()
		{
			Logger.LogDebug((object)"Unhooking...");
			HookEndpointManager.RemoveAllOwnedBy((object)Assembly.GetExecutingAssembly());
			Logger.LogDebug((object)"Finished Unhooking!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "HungryMuttis.BetterDefib";

		public const string PLUGIN_NAME = "BetterDefib";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BetterDefib.Patches
{
	internal class PlayerPatch
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_RPCA_PlayerRevive <0>__Player_RPCA_PlayerRevive;
		}

		internal static void Init()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			object obj = <>O.<0>__Player_RPCA_PlayerRevive;
			if (obj == null)
			{
				hook_RPCA_PlayerRevive val = Player_RPCA_PlayerRevive;
				<>O.<0>__Player_RPCA_PlayerRevive = val;
				obj = (object)val;
			}
			Player.RPCA_PlayerRevive += (hook_RPCA_PlayerRevive)obj;
		}

		private static void Player_RPCA_PlayerRevive(orig_RPCA_PlayerRevive orig, Player self)
		{
			self.data.remainingOxygen = CalculateOxygen(self.data.remainingOxygen, self.data.maxOxygen);
			orig.Invoke(self);
		}

		private static float CalculateOxygen(float remainingOxygen, float maxOxygen)
		{
			DefibFeature instance = DefibFeature.Instance;
			remainingOxygen = ((remainingOxygen < 0f) ? 0f : remainingOxygen);
			float num = ((instance.Type.Value == global::BetterDefib.Enums.Type.seconds) ? instance.Amount.Value : (maxOxygen * instance.Amount.Value / 100f));
			float num2;
			switch (instance.Setting.Value)
			{
			case Setting.add:
				num2 = remainingOxygen + num;
				break;
			case Setting.cset:
				num2 = Mathf.Max(remainingOxygen, num);
				break;
			case Setting.set:
				num2 = num;
				break;
			default:
				BetterDefib.Logger.LogWarning((object)"Invalid setting: 'Oxygen.Setting'. Review config file.");
				num2 = remainingOxygen;
				break;
			}
			return Mathf.Clamp(num2, 0f, maxOxygen);
		}
	}
}
namespace BetterDefib.Features
{
	[ModFeature]
	internal class DefibFeature : Feature
	{
		public static DefibFeature Instance { get; private set; }

		public ConfigEntry<Setting> Setting { get; private set; } = null;


		public ConfigEntry<float> Amount { get; private set; } = null;


		public ConfigEntry<global::BetterDefib.Enums.Type> Type { get; private set; } = null;


		internal override string FeatureName => "Defib";

		protected override string FeatureDescription => "Restores a configurable amount of oxygen upon revival.";

		public DefibFeature()
		{
			Instance = this;
		}

		public override void CreateConfig(ConfigFile config)
		{
			base.CreateConfig(config);
			Setting = config.Bind<Setting>(FeatureName, "Setting", global::BetterDefib.Enums.Setting.cset, "cset: After reviving, if oxygen is less than <Amount> set it to <Amount>\r\nset:  After reviving, set the oxygen to <Amount> (no matter if the remaining oxygen was more or less than <Amount>)\r\nadd:  After reviving, add <Amount> oxygen (won't give more than 100% of oxygen)");
			Amount = config.Bind<float>(FeatureName, "Amount", 180f, "Amount of oxygen");
			Type = config.Bind<global::BetterDefib.Enums.Type>(FeatureName, "Type", global::BetterDefib.Enums.Type.seconds, "seconds: <Amount> of seconds\r\npercent: <Amount> of percent");
		}

		public override void Initialize()
		{
			PlayerPatch.Init();
		}
	}
	internal abstract class Feature
	{
		public ConfigEntry<bool> Enabled { get; protected set; } = null;


		internal abstract string FeatureName { get; }

		protected abstract string FeatureDescription { get; }

		public abstract void Initialize();

		public virtual void CreateConfig(ConfigFile config)
		{
			Enabled = config.Bind<bool>(FeatureName, "Enabled", true, "Enables feature: " + FeatureDescription);
		}
	}
	internal class FeatureManager
	{
		private readonly List<Feature> Features = new List<Feature>();

		private readonly ManualLogSource Logger;

		private readonly ConfigFile Config;

		public FeatureManager(ManualLogSource logger, ConfigFile config)
		{
			Logger = logger;
			Config = config;
			RegisterFeaturesFromAssembly();
		}

		public void RegisterFeaturesFromAssembly()
		{
			Logger.LogDebug((object)"Scanning for features...");
			IEnumerable<System.Type> enumerable = from t in Assembly.GetExecutingAssembly().GetTypes()
				where t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(Feature)) && t.GetCustomAttribute<ModFeature>() != null
				select t;
			foreach (System.Type item2 in enumerable)
			{
				if (Activator.CreateInstance(item2) is Feature item)
				{
					Features.Add(item);
					Logger.LogDebug((object)("Discovered and registered feature: " + item2.Name));
				}
			}
		}

		public void InitializeFeatures()
		{
			Features.ForEach(delegate(Feature f)
			{
				f.CreateConfig(Config);
				if (f.Enabled.Value)
				{
					Logger.LogInfo((object)("Feature '" + f.FeatureName + "' is enabled. Initializing..."));
					f.Initialize();
				}
				else
				{
					Logger.LogInfo((object)("Feature '" + f.FeatureName + "' is disabled."));
				}
			});
		}

		public bool IsAnyFeatureEnabled()
		{
			return Features.Any((Feature f) => f.Enabled.Value);
		}
	}
	[AttributeUsage(AttributeTargets.Class, Inherited = false)]
	internal class ModFeature : Attribute
	{
	}
}
namespace BetterDefib.Enums
{
	internal enum Setting
	{
		add,
		cset,
		set
	}
	internal enum Type
	{
		seconds,
		percent
	}
}