From 28a647eaeac58f498f93ab5c05a6d6ed7922f161 Mon Sep 17 00:00:00 2001 From: Robert Delaney Date: Wed, 22 Jun 2022 17:24:58 -0700 Subject: [PATCH] long needed `undoAll` --- package.json | 2 +- src/main/patcher.js | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4763896..a907280 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "drdiscord", - "version": "1.0.2", + "version": "1.0.3", "dependencies": { "asar": "^3.1.0", "electron-devtools-installer": "^3.2.0", diff --git a/src/main/patcher.js b/src/main/patcher.js index 9e6d1be..ce08140 100644 --- a/src/main/patcher.js +++ b/src/main/patcher.js @@ -1,5 +1,6 @@ module.exports = new class rawPatcher { Symbol = Symbol("DrApi") + patches = {} hook(module, fn) { if (!module[fn]) module[fn] = function() {} const original = module[fn] @@ -45,18 +46,34 @@ module.exports = new class rawPatcher { const hook = this.hook(mod, fn) const obj = { callback, id } hook.before.add(obj) - return () => hook.after.delete(obj) + + this.patches[id] ??= [] + this.patches[id].push(() => hook.before.delete(obj)) + + return () => hook.before.delete(obj) } instead(id, mod, fn, callback) { const hook = this.hook(mod, fn) const obj = { callback, id } hook.instead.add(obj) - return () => hook.after.delete(obj) + + this.patches[id] ??= [] + this.patches[id].push(() => hook.instead.delete(obj)) + + return () => hook.instead.delete(obj) } after(id, mod, fn, callback) { const hook = this.hook(mod, fn) const obj = { callback, id } hook.after.add(obj) + + this.patches[id] ??= [] + this.patches[id].push(() => hook.after.delete(obj)) + return () => hook.after.delete(obj) } + unpatchAll(id) { + this.patches[id] ??= [] + for (const undo of this.patches[id]) undo() + } } \ No newline at end of file