Decompiled source of TrueOSP v1.0.2

TrueOSP.dll

Decompiled 4 months 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 IL.RoR2;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using On.RoR2;
using R2API;
using R2API.Utils;
using RoR2;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Networking;

[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("TrueOSP")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+9510261ea21423d83d64b7a590c4c870e2faf78d")]
[assembly: AssemblyProduct("TrueOSP")]
[assembly: AssemblyTitle("TrueOSP")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace TrueOSP
{
	public class OSPComponent : MonoBehaviour
	{
		public static float ospThreshold = 0.9f;

		public static float ospTimer = 0.5f;

		public static float ospInvuln = 0.5f;

		public CharacterBody characterBody = null;

		public HealthComponent healthComponent = null;

		private float ospStopwatch;

		public void Awake()
		{
			ospStopwatch = 0f;
		}

		public void FixedUpdate()
		{
			if (ospStopwatch > 0f)
			{
				ospStopwatch -= Time.fixedDeltaTime;
				if (ospStopwatch <= 0f && TrueOSPPlugin.enableLogging)
				{
					Debug.Log((object)"OSP timer expired.");
				}
			}
		}

		public void StartOSPTimer()
		{
			if (CanTriggerOSP())
			{
				ospStopwatch = ospTimer;
				if (TrueOSPPlugin.enableLogging)
				{
					Debug.Log((object)"Starting OSP timer.");
				}
			}
		}

		public bool TriggerOSP()
		{
			bool result = false;
			if (CanTriggerOSP() && ospStopwatch > 0f)
			{
				result = true;
				ospStopwatch = 0f;
				characterBody.AddBuff(TrueOSPPlugin.DisableOSPBuff);
				characterBody.AddTimedBuff(Buffs.HiddenInvincibility, ospInvuln);
				characterBody.outOfDangerStopwatch = 0f;
				if (TrueOSPPlugin.enableLogging)
				{
					Debug.Log((object)"Triggering OSP");
				}
			}
			return result;
		}

		public bool CanTriggerOSP()
		{
			return !characterBody.HasBuff(TrueOSPPlugin.DisableOSPBuff);
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.Moffein.TrueOSP", "TrueOSP", "1.0.1")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class TrueOSPPlugin : BaseUnityPlugin
	{
		public static BuffDef DisableOSPBuff;

		public static bool enableLogging = true;

		public void Awake()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			CreateBuff();
			CharacterBody.RecalculateStats += new hook_RecalculateStats(CharacterBody_RecalculateStats);
			HealthComponent.TakeDamageProcess += new Manipulator(HealthComponent_TakeDamageProcess);
		}

		private void HealthComponent_TakeDamageProcess(ILContext il)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			bool flag = true;
			ILCursor val = new ILCursor(il);
			if (val.TryGotoNext(new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterBody>(x, "get_hasOneShotProtection")
			}))
			{
				int index = val.Index;
				val.Index = index + 1;
				val.Emit(OpCodes.Ldarg_0);
				val.EmitDelegate<Func<bool, HealthComponent, bool>>((Func<bool, HealthComponent, bool>)((bool hasOSP, HealthComponent self) => HasShieldOnly(self.body) && hasOSP));
				if (val.TryGotoNext(new Func<Instruction, bool>[1]
				{
					(Instruction x) => ILPatternMatchingExt.MatchCall<HealthComponent>(x, "set_Networkhealth")
				}))
				{
					val.Emit(OpCodes.Ldarg_0);
					val.Emit(OpCodes.Ldarg_1);
					val.EmitDelegate<Func<float, HealthComponent, DamageInfo, float>>((Func<float, HealthComponent, DamageInfo, float>)ProcTrueOSP_Health);
					flag = false;
				}
			}
			if (flag)
			{
				Debug.LogError((object)"TrueOSP IL Hook failed");
			}
		}

		private float ProcTrueOSP_Health(float finalHealth, HealthComponent self, DamageInfo damageInfo)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Invalid comparison between Unknown and I4
			if (!self.body.hasOneShotProtection || (int)DamageTypeCombo.op_Implicit(damageInfo.damageType & DamageTypeCombo.op_Implicit((DamageType)262144)) == 262144)
			{
				return finalHealth;
			}
			if (HasShieldOnly(self.body))
			{
				return finalHealth;
			}
			OSPComponent oSPComponent = ((Component)self.body).gameObject.GetComponent<OSPComponent>();
			if (!Object.op_Implicit((Object)(object)oSPComponent))
			{
				oSPComponent = ((Component)self.body).gameObject.AddComponent<OSPComponent>();
				oSPComponent.healthComponent = self;
				oSPComponent.characterBody = self.body;
				if (enableLogging)
				{
					Debug.Log((object)"Adding OSP Manager to player.");
				}
			}
			float num = self.fullHealth * OSPComponent.ospThreshold;
			if (self.health >= num && finalHealth < num)
			{
				oSPComponent.StartOSPTimer();
			}
			if (finalHealth <= 0f && oSPComponent.TriggerOSP())
			{
				finalHealth = 1f;
			}
			return finalHealth;
		}

		private void CharacterBody_RecalculateStats(orig_RecalculateStats orig, CharacterBody self)
		{
			orig.Invoke(self);
			if (!self.hasOneShotProtection)
			{
				return;
			}
			if (!HasShieldOnly(self))
			{
				self.oneShotProtectionFraction = 0f;
				if (self.cursePenalty > 1f)
				{
					self.hasOneShotProtection = false;
				}
			}
			if (self.HasBuff(DisableOSPBuff))
			{
				if (NetworkServer.active && self.outOfDanger)
				{
					self.RemoveBuff(DisableOSPBuff);
				}
				else
				{
					self.hasOneShotProtection = false;
				}
			}
		}

		private void CreateBuff()
		{
			//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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)DisableOSPBuff))
			{
				BuffDef val = ScriptableObject.CreateInstance<BuffDef>();
				((Object)val).name = "TrueOSP_DisableOSPBuff";
				val.canStack = false;
				val.isCooldown = false;
				val.isDebuff = false;
				val.buffColor = new Color(42f / 85f, 111f / 170f, 0.6741176f);
				val.iconSprite = Addressables.LoadAssetAsync<BuffDef>((object)"RoR2/Base/ArmorReductionOnHit/bdPulverized.asset").WaitForCompletion().iconSprite;
				((Object)val).name = ((Object)val).name;
				ContentAddition.AddBuffDef(val);
				DisableOSPBuff = val;
			}
		}

		public static bool HasShieldOnly(CharacterBody self)
		{
			return self.HasBuff(Buffs.AffixLunar) || (Object.op_Implicit((Object)(object)self.inventory) && self.inventory.GetItemCount(Items.ShieldOnly) > 0);
		}
	}
}