-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowerful.patches.ts
More file actions
135 lines (112 loc) · 3.08 KB
/
Copy pathflowerful.patches.ts
File metadata and controls
135 lines (112 loc) · 3.08 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type { LogSource } from "@flowerloader/api";
import type {
FlowerPatch,
Patchable,
PatchFn,
} from "@flowerloader/api/FlowerPatch";
export class flowerPatcher {
Patches: FlowerPatch[] = [];
MyLogSource: LogSource;
//Apply patches
ApplyAllPatches() {
for (const patch of this.Patches) {
this.Apply(patch);
}
}
FindPatch(obj: Patchable, method: string) {
if (!obj[method]) {
console.error(`Method ${method} not found on ${obj}`);
return;
}
for (const patch of this.Patches) {
if (patch.obj === obj && patch.methodName === method) {
return patch;
}
}
const patch: FlowerPatch = {
obj: obj,
methodName: method,
prefixes: [],
postfixes: [],
applied: false,
};
this.Patches.push(patch);
return patch;
}
/**
* Binds a patch to an object. All patches are accumulated even after initial binding.
* @param patch
* @returns false if this patch has already been bound
*/
Apply(patch: FlowerPatch): boolean {
/** Only apply patches once ever */
if (patch.applied) return false;
const orig = patch.obj[patch.methodName] as PatchFn;
const wrapper: PatchFn = (...args) => {
this.MyLogSource.writeDebug(`Running detour for ${patch.methodName}`);
// Re-read the patch on every call so prefixes and postfixes
// registered after binding still run.
const current = this.FindPatch(patch.obj, patch.methodName);
if (!current) return;
this.MyLogSource.writeDebug(`Prefixes ${current.prefixes.length}`);
//Allow ending the detour early
for (const prefix of current.prefixes) {
if (false === prefix.call(current.obj, ...args)) {
this.MyLogSource.writeDebug("Ending detour");
return;
}
}
let origRet: ReturnType<PatchFn>;
try {
origRet = orig.call(current.obj, ...args);
} catch (e) {
this.MyLogSource.write(`Error running orig: ${e}`);
return;
}
/**
* Todo: allow postfixes to modify the return data here
*/
this.MyLogSource.writeDebug(`Postfixes ${current.postfixes.length}`);
for (const postfix of current.postfixes) {
try {
postfix.call(current.obj, ...args);
} catch (e) {
this.MyLogSource.write(
`Failed to run postfix: ${e instanceof Error ? e.message : e}`,
);
}
}
return origRet;
};
patch.obj[patch.methodName] = wrapper;
return true;
}
/**
* Registers a new patch with flower
* @param obj any object that contains a method
* @param methodName the string name of the method to patch
* @param patch a function that runs when the method is called
* @param isPrefix if this should run before the method (afterward otherwise)
* @returns true on success
*/
RegisterPatch(
obj: Patchable,
methodName: string,
patch: PatchFn,
isPrefix: boolean,
) {
this.MyLogSource.writeDebug(`Running RegisterPatch for ${methodName}`);
const accum = this.FindPatch(obj, methodName);
if (!accum) return false;
if (isPrefix) {
accum.prefixes.push(patch);
} else {
accum.postfixes.push(patch);
}
this.Apply(accum);
return true;
}
constructor(LogSource: LogSource) {
this.MyLogSource = LogSource;
}
}