Skip to content

Conversation

@doggybootsy
Copy link
Member

Hopefully more memory safe version of the patcher.

Also adds a new function called getOriginal when called it'll return the original function

const foo = {
    bar: () => 123
}

Patcher.after("MyPlugin", foo, "bar", (that, args, res) => res * 2);

foo.bar(); // 246

Patcher.getOriginal(foo.bar)(); // 123

Adds options when patching

  • once: When true (default is false) it will instantly unpatch after the callback is ran
const foo = {
    bar: () => 123
}

Patcher.after("MyPlugin", foo, "bar", (that, args, res) => res * 2, { once: true });

foo.bar(); // 246

foo.bar(); // 123
  • forcePatch: When false (default is true) and when you go to patch a function that doesn't exist on the module, it will not add the function
const foo = {}

Patcher.after("MyPlugin", foo, "bar", (that, args, res) => res * 2, { forcePatch: false });

foo.bar(); // Uncaught TypeError: foo.bar is not a function

Adds 2 new options
* `displayName`: A string that represents the name of that given patch. This will be shown when an exception is thrown within the patch (default is the function name or `Anonymous`)
* `priority`: A number that defines the priority of that patch, higher number increases the priority and lower number number lowers the priority (default is 0)

Also fixes typos and adds the patch details to the handler object of the proxy
Adds a option to prevent the patch being unpatched via `unpatchAll`. To unpatch you must manually unpatch via the returned function or the once option
@zerebos zerebos requested a review from Copilot July 22, 2025 15:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a complete rewrite of the Patcher module with enhanced memory safety and new functionality. The rewrite introduces a new getOriginal function to retrieve unpatched functions and adds options for more granular patching behavior.

  • Complete rewrite of the patcher implementation using WeakMaps for better memory management
  • Added getOriginal function to access original unpatched functions
  • Introduced options system with once, forcePatch, displayName, priority, and detached parameters

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/betterdiscord/modules/patcher.ts Complete rewrite of the core patcher logic with new hook-based system and enhanced type safety
src/betterdiscord/api/patcher.ts Updated API wrapper to support new options parameter and integrate with rewritten patcher

@zerebos
Copy link
Member

zerebos commented Aug 3, 2025

I think this is something that could use more testing before merging

Because the checking is only done on the plugin api, its possible for patcher to throw a exception for bd internally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants