From e004580e3abf2117ee0b56e71f1eb2e9f3c04306 Mon Sep 17 00:00:00 2001 From: SrLicht Date: Tue, 27 Jun 2023 06:51:28 -0300 Subject: [PATCH 1/2] DamageHandlerExtensions * The fact that I had to create an extension to get data from private filds is sad. * Some DamageType are not available because I have no way to get them without modifying the events. * DamageHandlerExtensions will probably be removed when Northwood implements its system. --- .../Extensions/DamageHandlerExtensions.cs | 172 ++++++++++++++++++ .../Core/Extensions/ReflectionExtensions.cs | 13 ++ NwPluginAPI/Enums/DamageType.cs | 20 +- 3 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs diff --git a/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs b/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs new file mode 100644 index 0000000..2edb799 --- /dev/null +++ b/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs @@ -0,0 +1,172 @@ +using PlayerStatsSystem; +using PluginAPI.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PlayerRoles.PlayableScps.Scp939; + +namespace PluginAPI.Core.Extensions +{ + public static class DamageHandlerExtensions + { + /// + /// Gets a of a + /// + /// + /// + public static DamageType GetDamageType(this DamageHandlerBase damageHandler) + { + switch (damageHandler) + { + case CustomReasonDamageHandler: + return DamageType.Custom; + case WarheadDamageHandler: + return DamageType.Warhead; + case ExplosionDamageHandler: + return DamageType.Explosion; + case Scp018DamageHandler: + return DamageType.Scp018; + case Scp096DamageHandler dmg: + return DamageType.Scp096; + case Scp939DamageHandler: + return DamageType.Scp939; + case RecontainmentDamageHandler: + return DamageType.Recontain079; + case MicroHidDamageHandler: + return DamageType.MicroHID; + case DisruptorDamageHandler: + return DamageType.MolecularDisruptor; + case FirearmDamageHandler firearmDamageHandler: + return firearmDamageHandler.GetFirearmDamageType(); + case JailbirdDamageHandler: + return DamageType.Jailbird; + case ScpDamageHandler scpDamageHandler: + { + var translationId = scpDamageHandler.GetPrivateFieldValue("_translationId"); + var deathTranslation = DeathTranslations.TranslationsById[translationId]; + // Scp106 does damage directly with a value of 106106f so if I want DamageType.Scp106 to be activated at least 1 time I have to do this check + // And how floats with more than 3 digits become inaccurate... + return Math.Abs(scpDamageHandler.Damage - 106106) < 0.001 ? DamageType.Scp106 : deathTranslation.GetTranslationDamageType(); + } + case UniversalDamageHandler universalDamage: + { + if (TranslationIdConversion.TryGetValue(universalDamage.TranslationId, out var damageType)) + { + return damageType; + } + + Log.Warning($"{nameof(DamageHandlerExtensions)}.{nameof(GetDamageType)}: Unknown damage detected from {nameof(UniversalDamageHandler)} with the ID {universalDamage.TranslationId}"); + return DamageType.Universal; + } + default: + return DamageType.Unknown; + } + } + + /// + /// Gets a from + /// + /// FireArm damage handler + /// + public static DamageType GetFirearmDamageType(this FirearmDamageHandler dmg) + { + return dmg.WeaponType switch + { + ItemType.GunCrossvec => DamageType.Crossvec, + ItemType.GunLogicer => DamageType.Logicer, + ItemType.GunShotgun => DamageType.Shotgun, + ItemType.GunAK => DamageType.AK, + ItemType.GunCOM15 => DamageType.Com15, + ItemType.GunCom45 => DamageType.Com45, + ItemType.GunCOM18 => DamageType.COM18, + ItemType.GunFSP9 => DamageType.FSP9, + ItemType.GunE11SR => DamageType.E11SR, + ItemType.MicroHID => DamageType.MicroHID, + ItemType.ParticleDisruptor => DamageType.MolecularDisruptor, + ItemType.GunRevolver => DamageType.Revolver, + _ => DamageType.Firearm + }; + } + + /// + /// Gets the specific done by SCP-096. + /// + /// .Scp096GateKill or .Scp096SlapLeft or .Scp096SlapRight or .Scp096Charge + public static DamageType GetScp096DamageType(this Scp096DamageHandler handler) + { + var attackType = handler.GetPrivateFieldValue("_attackType"); + + return attackType switch + { + Scp096DamageHandler.AttackType.Charge => DamageType.Scp096Charge, + Scp096DamageHandler.AttackType.GateKill => DamageType.Scp096GateKill, + Scp096DamageHandler.AttackType.SlapLeft => DamageType.Scp096SlapLeft, + Scp096DamageHandler.AttackType.SlapRight => DamageType.Scp096SlapRight, + _ => DamageType.Scp096 + }; + } + + /// + /// Gets the specific done by SCP-939. + /// + /// .Scp939Claw or .Scp939LungeSecondary or .Scp939LungeTarget + public static DamageType GetScp939DamageType(this Scp939DamageHandler handler) + { + var attackType = handler.GetPrivateFieldValue("_damageType"); + + switch (attackType) + { + case Scp939DamageType.Claw: + return DamageType.Scp939Claw; + case Scp939DamageType.LungeSecondary: + return DamageType.Scp939LungeSecondary; + case Scp939DamageType.LungeTarget: + return DamageType.Scp939LungeTarget; + default: + return DamageType.Scp939; + } + } + + /// + /// Gets a from a + /// + /// + public static DamageType GetTranslationDamageType(this DeathTranslation translation) + { + return TranslationIdConversion.TryGetValue(translation.Id, out var damageType) ? damageType : DamageType.Unknown; + } + + /// + /// Private dictionary of types of damage based on the id of translation. + /// + private static readonly Dictionary TranslationIdConversion = new() + { + { DeathTranslations.Asphyxiated.Id, DamageType.Asphyxiated }, + { DeathTranslations.Bleeding.Id, DamageType.Bleeding }, + { DeathTranslations.Crushed.Id, DamageType.Crushed }, + { DeathTranslations.Decontamination.Id, DamageType.Decontamination }, + { DeathTranslations.Explosion.Id, DamageType.Explosion }, + { DeathTranslations.Falldown.Id, DamageType.Falldown }, + { DeathTranslations.Poisoned.Id, DamageType.Poisoned }, + { DeathTranslations.Recontained.Id, DamageType.Recontainment }, + { DeathTranslations.Scp049.Id, DamageType.Scp049 }, + { DeathTranslations.Scp096.Id, DamageType.Scp096 }, + { DeathTranslations.Scp173.Id, DamageType.Scp173 }, + { DeathTranslations.Scp207.Id, DamageType.Scp207 }, + { DeathTranslations.Scp939Lunge.Id, DamageType.Scp939 }, + { DeathTranslations.Scp939Other.Id, DamageType.Scp939 }, + { DeathTranslations.Tesla.Id, DamageType.Tesla }, + { DeathTranslations.Unknown.Id, DamageType.Unknown }, + { DeathTranslations.Warhead.Id, DamageType.Warhead }, + { DeathTranslations.Zombie.Id, DamageType.Scp0492 }, + { DeathTranslations.BulletWounds.Id, DamageType.Firearm }, + { DeathTranslations.PocketDecay.Id, DamageType.PocketDecay }, + { DeathTranslations.SeveredHands.Id, DamageType.SeveredHands }, + { DeathTranslations.FriendlyFireDetector.Id, DamageType.FriendlyFireDetector }, + { DeathTranslations.MicroHID.Id, DamageType.MicroHID }, + { DeathTranslations.Hypothermia.Id, DamageType.Hypothermia }, + }; + } +} diff --git a/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs b/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs index b8fc290..92e3fcc 100644 --- a/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs +++ b/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs @@ -20,5 +20,18 @@ public static bool IsValidEntrypoint(this Type type) return false; } + + public static T GetPrivateFieldValue(this object obj, string fieldName) + { + Type type = obj.GetType(); + FieldInfo field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); + + if (field != null) + { + return (T)field.GetValue(obj); + } + + throw new ArgumentException($"Field '{fieldName}' not found in type '{type}'."); + } } } \ No newline at end of file diff --git a/NwPluginAPI/Enums/DamageType.cs b/NwPluginAPI/Enums/DamageType.cs index 2224758..2eb6282 100644 --- a/NwPluginAPI/Enums/DamageType.cs +++ b/NwPluginAPI/Enums/DamageType.cs @@ -27,7 +27,7 @@ public enum DamageType : int Com45 = 13, Jailbird = 14, Explosion = 15, - GrenadeExplosion = 16, + //GrenadeExplosion = 16, Recontainment = 17, Recontain079 = 18, Universal = 19, @@ -35,13 +35,15 @@ public enum DamageType : int Bleeding = 21, PocketDecay = 22, Decontamination = 23, - Hemorrhage = 24, + // Its not the same as Bleeding ? + //Hemorrhage = 24, Poisoned = 25, SeveredHands = 26, - Checkpoint = 27, + // Checkpoint can kill you ? + //Checkpoint = 27, FriendlyFireDetector = 28, Hypothermia = 29, - CardiacArrest = 30, + //CardiacArrest = 30, Falldown = 31, Tesla = 32, Scp207 = 33, @@ -58,7 +60,13 @@ public enum DamageType : int Scp939LungeSecondary = 44, Scp018 = 45, Warhead = 46, - PlayerLeft = 47, - ForcedDeath = 48, + //PlayerLeft = 47, + //ForcedDeath = 48, + + // Temporary DamageType until Nortwood implements its own system. + Scp096 = 50, + Scp939 = 51, + Crushed = 52, + Unknown = 53, } } From a3b18130bfbf17abb9d35ddae401a9d69370eae3 Mon Sep 17 00:00:00 2001 From: SrLicht Date: Thu, 29 Jun 2023 09:33:37 -0300 Subject: [PATCH 2/2] Using assembly Publicized * and enabling unsafe code --- .../Core/Extensions/DamageHandlerExtensions.cs | 11 +++-------- .../Core/Extensions/ReflectionExtensions.cs | 16 +--------------- NwPluginAPI/NwPluginAPI.csproj | 3 ++- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs b/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs index 2edb799..cd4d238 100644 --- a/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs +++ b/NwPluginAPI/Core/Extensions/DamageHandlerExtensions.cs @@ -44,8 +44,7 @@ public static DamageType GetDamageType(this DamageHandlerBase damageHandler) return DamageType.Jailbird; case ScpDamageHandler scpDamageHandler: { - var translationId = scpDamageHandler.GetPrivateFieldValue("_translationId"); - var deathTranslation = DeathTranslations.TranslationsById[translationId]; + var deathTranslation = DeathTranslations.TranslationsById[scpDamageHandler._translationId]; // Scp106 does damage directly with a value of 106106f so if I want DamageType.Scp106 to be activated at least 1 time I have to do this check // And how floats with more than 3 digits become inaccurate... return Math.Abs(scpDamageHandler.Damage - 106106) < 0.001 ? DamageType.Scp106 : deathTranslation.GetTranslationDamageType(); @@ -96,9 +95,7 @@ public static DamageType GetFirearmDamageType(this FirearmDamageHandler dmg) /// .Scp096GateKill or .Scp096SlapLeft or .Scp096SlapRight or .Scp096Charge public static DamageType GetScp096DamageType(this Scp096DamageHandler handler) { - var attackType = handler.GetPrivateFieldValue("_attackType"); - - return attackType switch + return handler._attackType switch { Scp096DamageHandler.AttackType.Charge => DamageType.Scp096Charge, Scp096DamageHandler.AttackType.GateKill => DamageType.Scp096GateKill, @@ -114,9 +111,7 @@ public static DamageType GetScp096DamageType(this Scp096DamageHandler handler) /// .Scp939Claw or .Scp939LungeSecondary or .Scp939LungeTarget public static DamageType GetScp939DamageType(this Scp939DamageHandler handler) { - var attackType = handler.GetPrivateFieldValue("_damageType"); - - switch (attackType) + switch (handler._damageType) { case Scp939DamageType.Claw: return DamageType.Scp939Claw; diff --git a/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs b/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs index 92e3fcc..2e0f5b7 100644 --- a/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs +++ b/NwPluginAPI/Core/Extensions/ReflectionExtensions.cs @@ -1,4 +1,4 @@ -namespace PluginAPI.Core.Extensions +namespace PluginAPI.Core.Extensions { using Attributes; using System; @@ -17,21 +17,7 @@ public static bool IsValidEntrypoint(this Type type) if (attr != null) return true; } - return false; } - - public static T GetPrivateFieldValue(this object obj, string fieldName) - { - Type type = obj.GetType(); - FieldInfo field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); - - if (field != null) - { - return (T)field.GetValue(obj); - } - - throw new ArgumentException($"Field '{fieldName}' not found in type '{type}'."); - } } } \ No newline at end of file diff --git a/NwPluginAPI/NwPluginAPI.csproj b/NwPluginAPI/NwPluginAPI.csproj index 77b65d0..fd56534 100644 --- a/NwPluginAPI/NwPluginAPI.csproj +++ b/NwPluginAPI/NwPluginAPI.csproj @@ -43,6 +43,7 @@ false false false + True @@ -57,7 +58,7 @@ - +