diff --git a/lib/patcher/src/hooks/after.ts b/lib/patcher/src/hooks/after.ts index 1c62568..5749c74 100644 --- a/lib/patcher/src/hooks/after.ts +++ b/lib/patcher/src/hooks/after.ts @@ -32,6 +32,34 @@ function unpatchAfter( hookNode.next = undefined } +/** + * After hooks allow you to modify the return value of the original function, or to perform some action after the original function is called. + * + * ```js + * import { after } from '@revenge-mod/patcher' + * + * const obj = { + * method: (a) => a * 2 + * } + * + * after(obj, 'method', (result) => { + * console.log('After method called with result:', result) + * // Modify return value + * return result + 1 + * }) + * + * console.log(obj.method(2)) // 5 + * // CONSOLE OUTPUT: + * // After method called with result: 4 + * // 5 + * ``` + * + * @param parent The parent object containing the method to patch. + * @param key The key of the method to patch. + * @param hook The hook function to execute after the original method. + * + * @returns A function to unpatch. + */ export function after< Parent extends Record, Key extends keyof Parent, diff --git a/lib/patcher/src/hooks/before.ts b/lib/patcher/src/hooks/before.ts index b58e7f6..79b67f8 100644 --- a/lib/patcher/src/hooks/before.ts +++ b/lib/patcher/src/hooks/before.ts @@ -32,6 +32,34 @@ function unpatchBefore( hookNode.next = undefined } +/** + * Before hooks allow you to modify the arguments passed to the original function, or to perform some action before the original function is called. + * + * ```js + * import { before } from '@revenge-mod/patcher' + * + * const obj = { + * method: (a) => console.log('Original method called with:', a) + * } + * + * before(obj, 'method', ([a]) => { + * console.log('Before method called with:', a) + * // Modify arguments by returning new array + * return [a + 1] + * }) + * + * obj.method(2) + * // CONSOLE OUTPUT: + * // Before method called with: 2 + * // Original method called with: 3 + * ``` + * + * @param parent The parent object containing the method to patch. + * @param key The key of the method to patch. + * @param hook The hook function to execute before the original method. + * + * @returns A function to unpatch. + */ export function before< Parent extends Record, Key extends keyof Parent, diff --git a/lib/patcher/src/hooks/instead.ts b/lib/patcher/src/hooks/instead.ts index 186ece8..9d01689 100644 --- a/lib/patcher/src/hooks/instead.ts +++ b/lib/patcher/src/hooks/instead.ts @@ -70,6 +70,43 @@ function unpatchInstead( hookNode.next = undefined } +/** + * Instead hooks allow you to completely replace the original function with a new one, while still being able to call the original function if needed. + * + * ```js + * import { instead } from '@revenge-mod/patcher' + * + * const obj = { + * method: (a) => { + * console.log('Original method called with:', a) + * return 'original result' + * } + * } + * + * instead(obj, 'method', ([a], original) => { + * console.log('Instead method called with:', a) + * // Call the original function if needed + * const originalResult = original('modified') + * console.log('Original method was called') + * + * // Return a new value + * return 'new value' + * }) + * + * console.log(obj.method('test')) // 'new value' + * // CONSOLE OUTPUT: + * // Instead method called with: test + * // Original method called with: modified + * // Original method was called + * // new value + * ``` + * + * @param parent The parent object containing the method to patch. + * @param key The key of the method to patch. + * @param hook The hook function to execute instead of the original method. + * + * @return A function to unpatch. + */ export function instead< Parent extends Record, Key extends keyof Parent,