HookAttribute, DetourAttribute - Trivaxy/Libvaxy GitHub Wiki
tModLoader offers an extremely useful tool named MonoMod built into the API itself. Libvaxy in turn provides you with two attributes: Hook
and Detour
.
Hook
acts as a cleaner alternative to manually subscribing to MonoMod events, otherwise, it is the exact same as using the
On.Terraria
and IL.Terraria
namespaces.
Your own methods that you apply Hook
to must be static. Otherwise, Libvaxy will throw an exception.
For example, if we want to detour the Entity.WithinRange
method, we can do:
[Hook(typeof(On.Terraria.Entity), "WithinRange")]
public static void ReplaceEntityWithinRange(On.Terraria.Entity.orig_WithinRange orig, Entity self, Vector2 Target, float MaxRange)
{
// your awesome code here
}
The same concept applies to IL editing methods:
[Hook(typeof(IL.Terraria.Entity), "WithinRange")]
public static void EditEntityWithinRange(ILContext context)
{
// your still awesome code here
}
The Detour
attribute on the other hand allows you to completely replace another mod's method code with your own (but not edit it. This feature might come soon).
It can be used like so:
[Detour("SomeCoolMod.SomeCoolType", "TheMethodName")]
public void DetourMethod()
{
// is your code still awesome?
}
If you are targetting a method with multiple overloads that have differing parameters, you need to specify an array of the parameter types in order, e.g:
[Detour("SomeCoolMod.SomeCoolType", "MethodWithOverloads", new Type[] { firstType, secondType })]
One note: Libvaxy will apply all Hook
s when it is loaded, and it will apply all Detour
s once all mods have loaded. If your mod is time-sensitive and needs to apply a certain hook as soon as possible (which is bad practice in general, so try to avoid running into such a situation) then you must manually subscribe them.