Skip to content

Commit b81450a

Browse files
committed
[Add:API] Traits.StopEffect
1 parent 53e8c5c commit b81450a

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

ExampleMod/TraitAddition.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ void IRegistrable.Register() {
2727
Player.PostUpdateStats.Event += PostUpdateStats_Event;
2828

2929
Traits.ApplyEffect.Event += ApplyEffect_Event;
30+
31+
Traits.StopEffect.Event += StopEffect_Event;
32+
}
33+
34+
private void StopEffect_Event(TraitType type) {
35+
if (type != MyTrait) return;
36+
37+
Mod.Log("TestTrait stopeed!");
3038
}
3139

3240
void Awake() {

RL2.API/Endpoints/Traits.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Reflection;
77
using Rewired.Utils.Libraries.TinyJson;
88
using UnityEngine;
9+
using MonoMod.Cil;
10+
using Mono.Cecil.Cil;
911

1012
namespace RL2.API;
1113

@@ -19,6 +21,7 @@ public static class Traits
1921
LoadData.Hook,
2022
LoadContent.Hook,
2123
ApplyEffect.Hook,
24+
StopEffect.ILHook,
2225
ModifyData.Hook,
2326
ModifyTraitObj.Hook,
2427
ExtendTypeArray.Hook,
@@ -205,6 +208,39 @@ internal static void Method(Action<TraitManager, TraitType, bool> orig, TraitMan
205208
}
206209
}
207210

211+
/// <summary>
212+
/// Triggered when a Trait is disabled on player death
213+
/// </summary>
214+
public static class StopEffect
215+
{
216+
/// <inheritdoc cref="StopEffect"/>
217+
/// <param name="type"></param>
218+
public delegate void Definition(TraitType type);
219+
220+
/// <inheritdoc cref="Definition"/>
221+
public static event Definition? Event;
222+
223+
internal static ILHook ILHook = new ILHook(
224+
typeof(PlayerDeathWindowController).GetMethod(nameof(PlayerDeathWindowController.OnOpen), BindingFlags.NonPublic | BindingFlags.Instance),
225+
Method,
226+
new ILHookConfig() {
227+
ID = "RL2.API::Traits.ApplyEffect",
228+
ManualApply = true,
229+
}
230+
);
231+
232+
internal static void Method(ILContext il) {
233+
ILCursor cursor = new ILCursor(il);
234+
235+
if (cursor.TryGotoNext(MoveType.After, i => i.MatchCallvirt<BaseTrait>(nameof(BaseTrait.DisableOnDeath)))) {
236+
cursor.Emit(OpCodes.Ldloc_3);
237+
cursor.EmitDelegate((List<BaseTrait>.Enumerator enumerator) => {
238+
Event?.Invoke(enumerator.Current.TraitType);
239+
});
240+
}
241+
}
242+
}
243+
208244
/// <summary>
209245
/// Allows extending the <see cref="TraitType_RL.TypeArray"/>
210246
/// </summary>

0 commit comments

Comments
 (0)