diff --git a/R2API/RecalculateStatsAPI.cs b/R2API/RecalculateStatsAPI.cs
index 243761b2..13a38172 100644
--- a/R2API/RecalculateStatsAPI.cs
+++ b/R2API/RecalculateStatsAPI.cs
@@ -90,10 +90,25 @@ public class StatHookEventArgs : EventArgs {
/// An instance of StatHookEventArgs, passed to each subscriber to this event in turn for modification.
public delegate void StatHookEventHandler(CharacterBody sender, StatHookEventArgs args);
+ private static event StatHookEventHandler _getStatCoefficients;
///
/// Subscribe to this event to modify one of the stat hooks which StatHookEventArgs covers. Fired during CharacterBody.RecalculateStats.
///
- public static event StatHookEventHandler GetStatCoefficients;
+ public static event StatHookEventHandler GetStatCoefficients {
+ add {
+ if (!Loaded) {
+ throw new InvalidOperationException($"{nameof(RecalculateStatsAPI)} is not loaded. Please use [{nameof(R2APISubmoduleDependency)}(nameof({nameof(RecalculateStatsAPI)})]");
+ }
+ _getStatCoefficients += value;
+ }
+
+ remove {
+ if (!Loaded) {
+ throw new InvalidOperationException($"{nameof(RecalculateStatsAPI)} is not loaded. Please use [{nameof(R2APISubmoduleDependency)}(nameof({nameof(RecalculateStatsAPI)})]");
+ }
+ _getStatCoefficients -= value;
+ }
+ }
private static StatHookEventArgs StatMods;
@@ -101,10 +116,7 @@ private static void HookRecalculateStats(ILContext il) {
ILCursor c = new ILCursor(il);
c.Emit(OpCodes.Ldarg_0);
- c.EmitDelegate>((cb) => {
- StatMods = new StatHookEventArgs();
- GetStatCoefficients?.Invoke(cb, StatMods);
- });
+ c.EmitDelegate>(GetStatMods);
ModifyHealthStat(c);
ModifyShieldStat(c);
@@ -117,6 +129,21 @@ private static void HookRecalculateStats(ILContext il) {
ModifyArmorStat(c);
}
+ private static void GetStatMods(CharacterBody characterBody) {
+ StatMods = new StatHookEventArgs();
+
+ if (_getStatCoefficients != null) {
+ foreach (StatHookEventHandler @event in _getStatCoefficients.GetInvocationList()) {
+ try {
+ @event(characterBody, StatMods);
+ }
+ catch (Exception e) {
+ R2API.Logger.LogError($"Exception thrown by : {@event.Method.DeclaringType.Name}.{@event.Method.Name}:\n{e}");
+ }
+ }
+ }
+ }
+
private static void ModifyArmorStat(ILCursor c) {
c.Index = 0;
@@ -133,6 +160,9 @@ private static void ModifyArmorStat(ILCursor c) {
return oldArmor + StatMods.armorAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyArmorStat)} failed.");
+ }
}
private static void ModifyAttackSpeedStat(ILCursor c) {
@@ -164,6 +194,9 @@ private static void ModifyAttackSpeedStat(ILCursor c) {
return origSpeedMult + StatMods.attackSpeedMultAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyAttackSpeedStat)} failed.");
+ }
}
private static void ModifyCritStat(ILCursor c) {
@@ -182,6 +215,9 @@ private static void ModifyCritStat(ILCursor c) {
});
c.Emit(OpCodes.Stloc, locOrigCrit);
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyCritStat)} failed.");
+ }
}
private static void ModifyDamageStat(ILCursor c) {
@@ -213,6 +249,9 @@ private static void ModifyDamageStat(ILCursor c) {
return origDamageMult + StatMods.damageMultAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyDamageStat)} failed.");
+ }
}
private static void ModifyJumpStat(ILCursor c) {
@@ -231,6 +270,9 @@ private static void ModifyJumpStat(ILCursor c) {
return origJumpPower * (1 + StatMods.jumpPowerMultAdd);
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyJumpStat)} failed.");
+ }
}
private static void ModifyHealthStat(ILCursor c) {
@@ -262,6 +304,9 @@ private static void ModifyHealthStat(ILCursor c) {
return origHealthMult + StatMods.healthMultAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyHealthStat)} failed.");
+ }
}
private static void ModifyShieldStat(ILCursor c) {
@@ -281,6 +326,9 @@ private static void ModifyShieldStat(ILCursor c) {
return origBaseShield + StatMods.baseShieldAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyShieldStat)} failed.");
+ }
}
private static void ModifyHealthRegenStat(ILCursor c) {
@@ -309,6 +357,9 @@ private static void ModifyHealthRegenStat(ILCursor c) {
return origRegenMult + StatMods.regenMultAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyHealthRegenStat)} failed.");
+ }
}
private static void ModifyMovementSpeedStat(ILCursor c) {
@@ -347,6 +398,9 @@ private static void ModifyMovementSpeedStat(ILCursor c) {
return origMoveSpeedReductionMult + StatMods.moveSpeedReductionMultAdd;
});
}
+ else {
+ R2API.Logger.LogError($"{nameof(ModifyMovementSpeedStat)} failed.");
+ }
}
}
}