-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatcher.d.ts
99 lines (84 loc) · 2.78 KB
/
patcher.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Cancel } from "./index";
type FnOrAny<F> = F extends (...args: any) => any ? F : any;
export interface Patcher {
/**
* Patches a function, executing the callback before it was called.
* This allows modifying the arguments being passed to the original.
*/
before<M, K extends keyof M>(
caller: string,
moduleToPatch: M,
functionName: K,
callback: PatchBeforeCallback<FnOrAny<M[K]>>,
): Cancel;
/**
* Patches a function, executing the callback after it was called.
* This allows modifying the return value from the original.
*/
after<M, K extends keyof M>(
caller: string,
moduleToPatch: M,
functionName: K,
callback: PatchAfterCallback<FnOrAny<M[K]>>,
): Cancel;
/**
* Patches a function, executing the callback instead of the original.
* This allows completely replacing the original.
*/
instead<M, K extends keyof M>(
caller: string,
moduleToPatch: M,
functionName: K,
callback: PatchInsteadCallback<FnOrAny<M[K]>>,
): Cancel;
/** Returns all patches for the given caller. */
getPatchesByCaller(caller: string): PatchInfo[];
/** Removes all patches created by the given caller. */
unpatchAll(caller: string): void;
}
export interface BoundPatcher
extends Omit<Patcher, "before" | "after" | "instead"> {
/** @see {@link Patcher.before} */
before<M, K extends keyof M>(
moduleToPatch: M,
functionName: K,
callback: PatchBeforeCallback<FnOrAny<M[K]>>,
): Cancel;
/** @see {@link Patcher.after} */
after<M, K extends keyof M>(
moduleToPatch: M,
functionName: K,
callback: PatchAfterCallback<FnOrAny<M[K]>>,
): Cancel;
/** @see {@link Patcher.instead} */
instead<M, K extends keyof M>(
moduleToPatch: M,
functionName: K,
callback: PatchInsteadCallback<FnOrAny<M[K]>>,
): Cancel;
/** @see {@link Patcher.getPatchesByCaller} */
getPatchesByCaller(): PatchInfo[];
/** @see {@link Patcher.unpatchAll} */
unpatchAll(): void;
}
export type PatchBeforeCallback<O extends (...args: any) => any> = (
thisObject: ThisParameterType<O>,
methodArguments: Parameters<O>,
) => any;
export type PatchAfterCallback<O extends (...args: any) => any> = (
thisObject: ThisParameterType<O>,
methodArguments: Parameters<O>,
returnValue: ReturnType<O>,
) => any;
export type PatchInsteadCallback<O extends (...args: any) => any> = (
thisObject: ThisParameterType<O>,
methodArguments: Parameters<O>,
originalMethod: O,
) => any;
export interface PatchInfo {
callback: (...args: any) => any;
caller: string;
id: number;
type: "before" | "after" | "instead";
unpatch: Cancel;
}