Decompiled source of EmeraudeCombat v1.0.0

plugins/EmeraudeCombat.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EmeraudeCombat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EmeraudeCombat")]
[assembly: AssemblyTitle("EmeraudeCombat")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace EmeraudeCombat
{
	[BepInPlugin("emeraude.combat", "Emeraude Combat", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "emeraude.combat";

		public const string Name = "Emeraude Combat";

		public const string Version = "1.0.0";

		internal static ConfigEntry<bool> SplitDamageNumbers;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			SplitDamageNumbers = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "SplitDamageNumbers", true, "Show one damage number per damage type on creatures, each with its own color (yellow: weak, white: normal, grey: resistant or immune), instead of one combined number.");
			new Harmony("emeraude.combat").PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded (split damage numbers: {2})", "Emeraude Combat", "1.0.0", SplitDamageNumbers.Value));
		}
	}
	internal static class DamageSplit
	{
		internal static HitData LastHit;

		internal static DamageTypes LastBase;

		internal static DamageModifiers LastModifiers;

		internal static List<DamagePart> Pending;

		private static bool warned;

		internal static Category CategoryOf(DamageModifier modifier)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Invalid comparison between Unknown and I4
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Invalid comparison between Unknown and I4
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			if ((int)modifier <= 2)
			{
				if ((int)modifier == 0)
				{
					return Category.Normal;
				}
				if ((int)modifier == 2)
				{
					goto IL_0015;
				}
			}
			else if ((int)modifier == 6 || (int)modifier == 8)
			{
				goto IL_0015;
			}
			return Category.Resistant;
			IL_0015:
			return Category.Weak;
		}

		internal static TextType TextTypeOf(Category category)
		{
			return (TextType)(category switch
			{
				Category.Weak => 2, 
				Category.Resistant => 1, 
				_ => 0, 
			});
		}

		internal static IEnumerable<DamagePart> Parts(DamageTypes before, DamageTypes after, DamageModifiers mods)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: 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_0017: Unknown result type (might be due to invalid IL or missing references)
			yield return new DamagePart(Category.Normal, before.m_damage, after.m_damage);
			yield return Part(mods.m_blunt, before.m_blunt, after.m_blunt);
			yield return Part(mods.m_slash, before.m_slash, after.m_slash);
			yield return Part(mods.m_pierce, before.m_pierce, after.m_pierce);
			yield return Part(mods.m_chop, before.m_chop, after.m_chop);
			yield return Part(mods.m_pickaxe, before.m_pickaxe, after.m_pickaxe);
			yield return Part(mods.m_frost, before.m_frost, after.m_frost);
			yield return Part(mods.m_lightning, before.m_lightning, after.m_lightning);
			yield return Part(mods.m_nonPlayer, before.m_nonPlayer, after.m_nonPlayer);
		}

		private static DamagePart Part(DamageModifier modifier, float before, float after)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			if ((int)modifier != 4)
			{
				return new DamagePart(CategoryOf(modifier), before, after);
			}
			return new DamagePart(Category.Normal, 0f, 0f);
		}

		internal static void Fail(Exception e)
		{
			Pending = null;
			LastHit = null;
			if (!warned)
			{
				warned = true;
				Plugin.Log.LogWarning((object)$"Split damage numbers disabled for this hit: {e}");
			}
		}
	}
	[HarmonyPatch(typeof(HitData), "ApplyResistance")]
	internal static class ApplyResistancePatch
	{
		private static void Prefix(HitData __instance, DamageModifiers modifiers)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			DamageSplit.LastHit = __instance;
			DamageSplit.LastBase = __instance.m_damage;
			DamageSplit.LastModifiers = modifiers;
		}
	}
	[HarmonyPatch(typeof(Character), "ApplyDamage")]
	internal static class ApplyDamagePatch
	{
		private static void Prefix(Character __instance, HitData hit, bool showDamageText)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			DamageSplit.Pending = null;
			try
			{
				if (Plugin.SplitDamageNumbers.Value && showDamageText && hit != null && hit == DamageSplit.LastHit && !__instance.IsPlayer())
				{
					DamageSplit.Pending = Rules.Split(DamageSplit.Parts(DamageSplit.LastBase, hit.m_damage, DamageSplit.LastModifiers), hit.GetTotalDamage());
				}
			}
			catch (Exception e)
			{
				DamageSplit.Fail(e);
			}
		}

		private static void Postfix()
		{
			DamageSplit.Pending = null;
			DamageSplit.LastHit = null;
		}
	}
	[HarmonyPatch(typeof(DamageText), "ShowText", new Type[]
	{
		typeof(DamageModifier),
		typeof(Vector3),
		typeof(float),
		typeof(bool)
	})]
	internal static class ShowTextPatch
	{
		private static bool Prefix(DamageText __instance, Vector3 pos, bool player)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			List<DamagePart> pending = DamageSplit.Pending;
			if (pending == null)
			{
				return true;
			}
			DamageSplit.Pending = null;
			try
			{
				for (int i = 0; i < pending.Count; i++)
				{
					__instance.ShowText(DamageSplit.TextTypeOf(pending[i].Category), pos + Vector3.up * (0.4f * (float)i), pending[i].Amount, player);
				}
				return false;
			}
			catch (Exception e)
			{
				DamageSplit.Fail(e);
				return true;
			}
		}
	}
	public enum Category
	{
		Normal,
		Weak,
		Resistant
	}
	public readonly struct DamagePart
	{
		public readonly Category Category;

		public readonly float Base;

		public readonly float Amount;

		public DamagePart(Category category, float baseAmount, float amount)
		{
			Category = category;
			Base = baseAmount;
			Amount = amount;
		}
	}
	public static class Rules
	{
		private const float Present = 0.01f;

		private const float Tolerance = 0.05f;

		public static List<DamagePart> Split(IEnumerable<DamagePart> parts, float shownTotal)
		{
			List<DamagePart> list = new List<DamagePart>();
			float num = 0f;
			foreach (DamagePart part in parts)
			{
				num += part.Amount;
				if (!(part.Base < 0.01f) && !(part.Amount < 0.01f))
				{
					list.Add(part);
				}
			}
			if (list.Count < 2)
			{
				return null;
			}
			if (Math.Abs(num - shownTotal) > 0.05f + Math.Abs(shownTotal) * 0.001f)
			{
				return null;
			}
			return list;
		}
	}
}