Mod Creation/Mod Context API Reference: Difference between revisions

No edit summary
Line 625: Line 625:
  <nowiki>ctx.accountStorage.clear();</nowiki>
  <nowiki>ctx.accountStorage.clear();</nowiki>


== Game Method Patching/Hooking ==
== Game Object Patching/Hooking ==
=== <code>patch(className: class, methodName: string): Patch</code> ===
 
=== patch(className: class, methodName: string): Patch ===
 
This is the entry-point to the method patching API. The Patch object returned by this method can perform further actions with the specified class and method.
This is the entry-point to the method patching API. The Patch object returned by this method can perform further actions with the specified class and method.


Line 640: Line 642:


'''Example'''
'''Example'''
  <nowiki>ctx.patch(Skill, 'addXP');</nowiki>
  <nowiki>ctx.patch(Skill, 'addXP');</nowiki>


==== <code>Patch.before(hook: (...args: any) => any[] | void): void</code> ====
==== Patch.before(hook: (...args: any) => any[] | void): void ====
 
Execute a callback function immediately before the method body is called. The callback function's parameters are the arguments being passed into the method call. Optionally the callback function can return an array of values to override the arguments being passed to the method body. If no return value is specified (returns <code>undefined</code>), the arguments are left as-is.
Execute a callback function immediately before the method body is called. The callback function's parameters are the arguments being passed into the method call. Optionally the callback function can return an array of values to override the arguments being passed to the method body. If no return value is specified (returns <code>undefined</code>), the arguments are left as-is.


Line 650: Line 654:


'''Example'''
'''Example'''
  <nowiki>// Double all XP gains
  <nowiki>// Double all XP gains
ctx.patch(Skill, 'addXP').before((amount, masteryAction) => [amount * 2, masteryAction]);</nowiki>
ctx.patch(Skill, 'addXP').before((amount, masteryAction) => [amount * 2, masteryAction]);</nowiki>


==== <code>Patch.after(hook: (returnValue: any, ...args: any) => any | void): void</code> ====
==== Patch.after(hook: (returnValue: any, ...args: any) => any | void): void ====
 
Execute a callback function immediate after the method body is finished executing. The callback function's first parameter is the value returned from the method body. The rest of the parameters are the arguments that were passed into the method body. Optionally the callback function can return a new value to override the method's return value. If no return value is specified (returns <code>undefined</code>), the return value is left as-is.
Execute a callback function immediate after the method body is finished executing. The callback function's first parameter is the value returned from the method body. The rest of the parameters are the arguments that were passed into the method body. Optionally the callback function can return a new value to override the method's return value. If no return value is specified (returns <code>undefined</code>), the return value is left as-is.


Line 661: Line 667:


'''Example'''
'''Example'''
  <nowiki>// The player never misses an attack
  <nowiki>// The player never misses an attack
ctx.patch(Player, 'rollToHit').after(willHit => {
ctx.patch(Player, 'rollToHit').after(willHit => {
Line 667: Line 674:
});</nowiki>
});</nowiki>


==== <code>Patch.replace(replacement: (replacedMethod: (...args: any) => any, ...args: any) => any): void</code> ====
==== Patch.replace(replacement: (replacedMethod: (...args: any) => any, ...args: any) => any): void ====
 
Execute a callback function instead of the method's current body. The callback function's first parameter is the replaced method body. The rest of the parameters are the arguments that were to be passed to the method. The callback function's return value is the return value for the method. The replacement function is still subject to argument/return value modifications made in <code>before</code> and <code>after</code> hooks, respectively.
Execute a callback function instead of the method's current body. The callback function's first parameter is the replaced method body. The rest of the parameters are the arguments that were to be passed to the method. The callback function's return value is the return value for the method. The replacement function is still subject to argument/return value modifications made in <code>before</code> and <code>after</code> hooks, respectively.


Line 675: Line 683:


'''Example'''
'''Example'''
  <nowiki>ctx.patch(Skill, 'addXP').replace(function(o, amount, masteryAction) {
  <nowiki>ctx.patch(Skill, 'addXP').replace(function(o, amount, masteryAction) {
   // Prevent any woodcutting XP
   // Prevent any woodcutting XP
Line 687: Line 696:


It's important to note that using the <code>replace</code> method replaces the current method body, meaning multiple calls of the <code>replace</code> method get executed in the reverse order that they were declared:
It's important to note that using the <code>replace</code> method replaces the current method body, meaning multiple calls of the <code>replace</code> method get executed in the reverse order that they were declared:
  <nowiki>const xpPatch = ctx.patch(Skill, 'addXP');
  <nowiki>const xpPatch = ctx.patch(Skill, 'addXP');


Line 704: Line 714:
// Replacement #1</nowiki>
// Replacement #1</nowiki>


=== <code>isPatched(className: class, methodName: string): boolean</code> ===
=== isPatched(className: class, methodName: string): boolean ===
 
Checks whether or not a method has been patched.
Checks whether or not a method has been patched.


Line 718: Line 729:


'''Example'''
'''Example'''
  <nowiki>ctx.isPatched(Skill, 'addXP'); // false
  <nowiki>ctx.isPatched(Skill, 'addXP'); // false
ctx.patch(Skill, 'addXP');
ctx.patch(Skill, 'addXP');
89

edits