Anonymous

Mod Creation/Mod Context API Reference: Difference between revisions

From Melvor Idle
Line 740: Line 740:
== Game Object Patching/Hooking ==
== Game Object Patching/Hooking ==


=== patch(className: class, methodName: string): Patch ===
=== patch(className: class, methodOrPropertyName: string): MethodPatch | PropertyPatch ===


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 and getter/setter patching API. Depending on if the second parameter is a method or getter/setter property, a <code>MethodPatch</code> or <code>PropertyPatch</code> object will be returned, respectively. The MethodPatch/PropertyPatch object should then be used to perform further actions with the specified class and method/property.


'''Parameters'''
'''Parameters'''


<code>className: class</code> Class containing the method you want to patch. Should be the actual class reference, not a string, e.g. <code>Skill</code>, not <code>'Skill'</code>.
<code>className: class</code> Class containing the method or getter/setter you want to patch. Should be the actual class reference, not a string, e.g. <code>Skill</code>, not <code>'Skill'</code>.


<code>methodName: string</code> Name of the method to patch.
<code>methodOrPropertyName: string</code> Name of the method or getter/setter property to patch.


'''Returns'''
'''Returns'''


<code>Patch</code> A patch object for the specified class and method. See below for usage.
<code>MethodPatch | PropertyPatcch</code> A patch object for the specified class and method or getter/setter property. See below for usage.


'''Example'''
'''Example'''


  <nowiki>ctx.patch(Skill, 'addXP');</nowiki>
  <nowiki>ctx.patch(Skill, 'addXP'); // Returns a MethodPatch
ctx.patch(Skill, 'level'); // Returns a PropertyPatch</nowiki>


==== Patch.before(hook: (...args: any) => any[] | void): void ====
==== MethodPatch.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 771: Line 772:
ctx.patch(Skill, 'addXP').before((amount, masteryAction) => [amount * 2, masteryAction]);</nowiki>
ctx.patch(Skill, 'addXP').before((amount, masteryAction) => [amount * 2, masteryAction]);</nowiki>


==== Patch.after(hook: (returnValue: any, ...args: any) => any | void): void ====
==== MethodPatch.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 787: Line 788:
});</nowiki>
});</nowiki>


==== Patch.replace(replacement: (replacedMethod: (...args: any) => any, ...args: any) => any): void ====
==== MethodPatch.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 827: Line 828:
// Replacement #1</nowiki>
// Replacement #1</nowiki>


=== isPatched(className: class, methodName: string): boolean ===
==== PropertyPatch.get(getter: () => any): void ====


Checks whether or not a method has been patched.
Execute the provided function and return the return value when a getter property is accessed.


'''Parameters'''
'''Parameters'''


<code>className: class</code> Class containing the method to check for having been patched. Should be the actual class reference, not a string, e.g. <code>Skill</code>, not <code>'Skill'</code>.
<code>getter: () => any</code> The getter function to be executed.


<code>methodName: string</code> Name of the method to check.
'''Example'''
 
<nowiki>// Unlimited township resources
ctx.patch(TownshipResource, 'amount').get(() => 999999);</nowiki>
 
==== PropertyPatch.set(setter: (o: undefined, value: any) => void): void ====
 
Execute the provided function when a setter property is accessed.
 
'''Parameters'''
 
<code>setter: (o: undefined, value: any) => any</code> The getter function to be executed, with the <code>value</code> argument containing the value being set. The <code>o</code> argument is currently a placeholder and is unused.
 
'''Example'''
 
<nowiki>// Also unlimited township resources, in a roundabout way
// Sorry, there aren't many setters in the game to use for a good example
ctx.patch(TownshipResource, 'amount').set(function(amount) {
  if (amount < 0) this._amount = 999999;
  else this._amount = amount;
});</nowiki>
 
==== PropertyPatch.replace(getter?: () => any, setter?: (o: undefined, value: any) => void): void ====
 
Alias for calling <code>get</code> and <code>set</code> at the same time.
 
'''Parameters'''
 
<code>getter: () => any</code> The getter function to be executed.
 
<code>setter: (o: undefined, value: any) => any</code> The getter function to be executed, with the <code>value</code> argument containing the value being set. The <code>o</code> argument is currently a placeholder and is unused.
 
'''Example'''
 
See above examples for <code>get</code> and <code>set</code>.
 
=== isPatched(className: class, methodOrPropertyName: string): boolean ===
 
Checks whether or not a method or getter/setter property has been patched.
 
'''Parameters'''
 
<code>className: class</code> Class containing the method or property to check for having been patched. Should be the actual class reference, not a string, e.g. <code>Skill</code>, not <code>'Skill'</code>.
 
<code>methodOrPropertyName: string</code> Name of the method or property to check.


'''Returns'''
'''Returns'''


<code>boolean</code> Whether or not the given class method is patched.
<code>boolean</code> Whether or not the given class method or property is patched.


'''Example'''
'''Example'''
91

edits