Decompiled source of ChargingHealEnergy v1.0.0

BepInEx/plugins/ChargingHealEnergy/ChargingHealEnergy.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ChargingHealEnergy")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ChargingHealEnergy")]
[assembly: AssemblyTitle("ChargingHealEnergy")]
[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.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]
	[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]
	[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 ChargingHealEnergy
{
	[BepInPlugin("local.repo.charginghealenergy", "Charging Heal Energy", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "local.repo.charginghealenergy";

		public const string PluginName = "Charging Heal Energy";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<float> EnergyPercentPerHeal { get; private set; }

		private void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			EnergyPercentPerHeal = ((BaseUnityPlugin)this).Config.Bind<float>("General", "EnergyPercentPerHeal", 0.25f, new ConfigDescription("Percentage of total charging station energy spent per GameTools healing tick (5 HP).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			new Harmony("local.repo.charginghealenergy").PatchAll(typeof(GameToolsHealingPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Charging Heal Energy loaded.");
		}
	}
	[HarmonyPatch]
	internal static class GameToolsHealingPatch
	{
		[HarmonyTargetMethod]
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method("Zichen_GameTools.Plugin:AccumulateChargingStationHeal", (Type[])null, (Type[])null) ?? throw new MissingMethodException("GameTools healing method was not found.");
		}

		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo heal = AccessTools.Method(typeof(PlayerHealth), "Heal", (Type[])null, (Type[])null);
			MethodInfo healOther = AccessTools.Method(typeof(PlayerHealth), "HealOther", (Type[])null, (Type[])null);
			MethodInfo healWithEnergy = AccessTools.Method(typeof(GameToolsHealingPatch), "HealWithEnergy", (Type[])null, (Type[])null);
			MethodInfo healOtherWithEnergy = AccessTools.Method(typeof(GameToolsHealingPatch), "HealOtherWithEnergy", (Type[])null, (Type[])null);
			int replacements = 0;
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Callvirt && instruction.operand is MethodInfo methodInfo)
				{
					MethodInfo replacement = null;
					if (methodInfo == heal)
					{
						replacement = healWithEnergy;
					}
					else if (methodInfo == healOther)
					{
						replacement = healOtherWithEnergy;
					}
					if (replacement != null)
					{
						CodeInstruction val = new CodeInstruction(OpCodes.Ldarg_1, (object)null);
						val.labels.AddRange(instruction.labels);
						instruction.labels.Clear();
						yield return val;
						yield return new CodeInstruction(OpCodes.Call, (object)replacement);
						replacements++;
						continue;
					}
				}
				yield return instruction;
			}
			if (replacements != 2)
			{
				throw new InvalidOperationException($"Expected two GameTools healing calls, found {replacements}.");
			}
		}

		private static void HealWithEnergy(PlayerHealth health, int amount, bool effect, ChargingStation station)
		{
			TryHeal(health, amount, effect, station, other: false);
		}

		private static void HealOtherWithEnergy(PlayerHealth health, int amount, bool effect, ChargingStation station)
		{
			TryHeal(health, amount, effect, station, other: true);
		}

		private static void TryHeal(PlayerHealth health, int amount, bool effect, ChargingStation station, bool other)
		{
			if ((Object)(object)station == (Object)null)
			{
				HealOriginal(health, amount, effect, other);
			}
			else if (SemiFunc.IsMasterClientOrSingleplayer())
			{
				Traverse val = Traverse.Create((object)station);
				float value = val.Field("chargeFloat").GetValue<float>();
				float num = Mathf.Max(0f, Plugin.EnergyPercentPerHeal.Value) / 100f;
				if (!(value <= 0f) && !(value + 0.0001f < num))
				{
					HealOriginal(health, amount, effect, other);
					ConsumeEnergy(station, val, num);
				}
			}
		}

		private static void HealOriginal(PlayerHealth health, int amount, bool effect, bool other)
		{
			if (other)
			{
				health.HealOther(amount, effect);
			}
			else
			{
				health.Heal(amount, effect);
			}
		}

		private static void ConsumeEnergy(ChargingStation station, Traverse traverse, float cost)
		{
			float num = Mathf.Max(0f, traverse.Field("chargeFloat").GetValue<float>() - cost);
			int num2 = Mathf.Clamp((int)(num * 100f), 0, 100);
			traverse.Field("chargeFloat").SetValue((object)num);
			traverse.Field("chargeTotal").SetValue((object)num2);
			if ((Object)(object)StatsManager.instance != (Object)null)
			{
				StatsManager.instance.runStats["chargingStationChargeTotal"] = num2;
			}
			traverse.Method("CrystalsItShouldHave", Array.Empty<object>()).GetValue();
			int num3 = Mathf.Clamp(Mathf.CeilToInt(num * (float)station.chargeSegments), 0, 255);
			traverse.Field("chargeSegmentCurrent").SetValue((object)num3);
			traverse.Field("lastSentSegment").SetValue((object)num3);
			if (num <= 0f && traverse.Field("isCharging").GetValue<bool>())
			{
				traverse.Method("StopCharge", Array.Empty<object>()).GetValue();
			}
			if (SemiFunc.IsMultiplayer())
			{
				PhotonView component = ((Component)station).GetComponent<PhotonView>();
				if (!((Object)(object)component == (Object)null))
				{
					PhotonNetwork.RemoveBufferedRPCs(component.ViewID, "ChargingStationSegmentChangedRPC", (int[])null);
					component.RPC("ChargingStationSegmentChangedRPC", (RpcTarget)3, new object[1] { (byte)num3 });
				}
			}
		}
	}
}