Refactor BDPatcher: Streamline error handling, optimize patch filtering, and improve code readability with modern JavaScript syntax

This commit is contained in:
deadshumz 2024-02-27 12:34:18 +03:00
parent f986a7fc4d
commit dcc49590b1
1 changed files with 50 additions and 38 deletions

View File

@ -50,44 +50,56 @@
} }
static makeOverride(patch) { static makeOverride(patch) {
return function BDPatcher() { const logError = (type, caller, err) => {
let returnValue; Logger.err("Patcher", `Could not fire ${type} callback of ${patch.functionName} for ${caller}`, err);
if (!patch.children || !patch.children.length) return patch.originalFunction.apply(this, arguments); };
for (const superPatch of patch.children.filter(c => c.type === "before")) {
try { const applyPatch = (context, args, callback, originalFunction) => {
superPatch.callback(this, arguments); try {
} const result = callback(context, args, originalFunction);
catch (err) { return typeof result !== "undefined" ? result : undefined;
Logger.err("Patcher", `Could not fire before callback of ${patch.functionName} for ${superPatch.caller}`, err); } catch (err) {
} logError(callback.type, callback.caller, err);
} return undefined;
}
const insteads = patch.children.filter(c => c.type === "instead"); };
if (!insteads.length) {returnValue = patch.originalFunction.apply(this, arguments);}
else { return function BDPatcher() {
for (const insteadPatch of insteads) { let returnValue;
try {
const tempReturn = insteadPatch.callback(this, arguments, patch.originalFunction.bind(this)); if (!patch.children || !patch.children.length) {
if (typeof(tempReturn) !== "undefined") returnValue = tempReturn; return patch.originalFunction.apply(this, arguments);
} }
catch (err) {
Logger.err("Patcher", `Could not fire instead callback of ${patch.functionName} for ${insteadPatch.caller}`, err); const beforePatches = [];
} const insteadPatches = [];
} const afterPatches = [];
}
patch.children.forEach(child => {
for (const slavePatch of patch.children.filter(c => c.type === "after")) { if (child.type === "before") beforePatches.push(child);
try { else if (child.type === "instead") insteadPatches.push(child);
const tempReturn = slavePatch.callback(this, arguments, returnValue); else if (child.type === "after") afterPatches.push(child);
if (typeof(tempReturn) !== "undefined") returnValue = tempReturn; });
}
catch (err) { beforePatches.forEach(patch => applyPatch(this, arguments, patch.callback));
Logger.err("Patcher", `Could not fire after callback of ${patch.functionName} for ${slavePatch.caller}`, err);
} if (!insteadPatches.length) {
} returnValue = patch.originalFunction.apply(this, arguments);
return returnValue; } else {
}; insteadPatches.forEach(patch => {
} const result = applyPatch(this, arguments, patch.callback, patch.originalFunction.bind(this));
if (result !== undefined) returnValue = result;
});
}
afterPatches.forEach(patch => {
const result = applyPatch(this, arguments, patch.callback, () => returnValue);
if (result !== undefined) returnValue = result;
});
return returnValue;
};
}
static rePatch(patch) { static rePatch(patch) {
patch.proxyFunction = patch.module[patch.functionName] = this.makeOverride(patch); patch.proxyFunction = patch.module[patch.functionName] = this.makeOverride(patch);