Skip to content

Commit

Permalink
- throw if submodule is not loaded (#307)
Browse files Browse the repository at this point in the history
- try catch around event invocation
- log if il hooks are failing

Co-authored-by: Tristan McPherson <shredder8910@gmail.com>
  • Loading branch information
xiaoxiao921 and tristanmcpherson authored Oct 25, 2021
1 parent 5722b23 commit 01f0122
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions R2API/RecalculateStatsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,33 @@ public class StatHookEventArgs : EventArgs {
/// <param name="args">An instance of StatHookEventArgs, passed to each subscriber to this event in turn for modification.</param>
public delegate void StatHookEventHandler(CharacterBody sender, StatHookEventArgs args);

private static event StatHookEventHandler _getStatCoefficients;
/// <summary>
/// Subscribe to this event to modify one of the stat hooks which StatHookEventArgs covers. Fired during CharacterBody.RecalculateStats.
/// </summary>
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;

private static void HookRecalculateStats(ILContext il) {
ILCursor c = new ILCursor(il);

c.Emit(OpCodes.Ldarg_0);
c.EmitDelegate<Action<CharacterBody>>((cb) => {
StatMods = new StatHookEventArgs();
GetStatCoefficients?.Invoke(cb, StatMods);
});
c.EmitDelegate<Action<CharacterBody>>(GetStatMods);

ModifyHealthStat(c);
ModifyShieldStat(c);
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -347,6 +398,9 @@ private static void ModifyMovementSpeedStat(ILCursor c) {
return origMoveSpeedReductionMult + StatMods.moveSpeedReductionMultAdd;
});
}
else {
R2API.Logger.LogError($"{nameof(ModifyMovementSpeedStat)} failed.");
}
}
}
}

0 comments on commit 01f0122

Please sign in to comment.