From f6582b4ee1550c2b3da73aa104719382353adcaa Mon Sep 17 00:00:00 2001 From: Ruthenic Date: Wed, 5 Jan 2022 18:01:11 -0500 Subject: [PATCH] update from demoncord source --- patcher.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/patcher.js b/patcher.js index 559c4d1..e6c05d0 100644 --- a/patcher.js +++ b/patcher.js @@ -1,18 +1,20 @@ function monkeyPatch(name, parentObj, patches) { - const injId = Math.random().toString(36).slice(2).toUpperCase() - const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"]; - const instead = (patches["instead"] === undefined) ? (...args)=>args : patches["instead"]; - const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"]; + const injId = Symbol() + const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"]; + const instead = (patches["instead"] === undefined) ? (res,...args)=>res(...args) : patches["instead"]; + const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"]; const handler = { - apply: (target, thisArg, args) => { - before(args); - res = (patches["instead"] === undefined) ? target(...args) : instead(...args); - return after(...args, res); + apply: (target, thisArg, [ctx, args]) => { + before.apply(ctx, args); + res = instead(target.bind(ctx), args) + return after.apply(ctx, args.concat([res])); } }; const prox = new Proxy(parentObj[name], handler); const orig = parentObj[name]; - parentObj[name] = prox; + parentObj[name] = function() { + return prox(this, [arguments]); + }; const unpatch = () => { parentObj[name] = orig; } @@ -23,6 +25,7 @@ function monkeyPatch(name, parentObj, patches) { }; return unpatch; } + function before(name, parentObj, func) { return monkeyPatch(name, parentObj, {before: func}) } @@ -41,3 +44,4 @@ module.exports = { instead, after } +