2024-07-22 00:17:17 +08:00
|
|
|
import { patcher } from "@lib/api";
|
|
|
|
|
import { registerCommand } from "@lib/api/commands";
|
2024-10-13 11:29:42 +08:00
|
|
|
import { createStorage } from "@lib/api/storage";
|
2024-07-22 00:17:17 +08:00
|
|
|
import { logger } from "@lib/utils/logger";
|
|
|
|
|
|
|
|
|
|
import { registeredPlugins } from ".";
|
|
|
|
|
import { BunnyPluginObject } from "./types";
|
|
|
|
|
|
|
|
|
|
type DisposableFn = (...props: any[]) => () => unknown;
|
|
|
|
|
function shimDisposableFn<F extends DisposableFn>(unpatches: (() => void)[], f: F): F {
|
2024-11-10 01:55:18 +08:00
|
|
|
const dummy = ((...props: Parameters<F>) => {
|
2024-07-22 00:17:17 +08:00
|
|
|
const up = f(...props);
|
|
|
|
|
unpatches.push(up);
|
|
|
|
|
return up;
|
|
|
|
|
}) as F;
|
2024-11-10 01:55:18 +08:00
|
|
|
|
|
|
|
|
for (const key in f) if (typeof f[key] === "function") {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
dummy[key] = shimDisposableFn(unpatches, f[key] as DisposableFn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dummy;
|
2024-07-22 00:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 12:07:45 +08:00
|
|
|
export function createBunnyPluginApi(id: string) {
|
2024-07-22 00:17:17 +08:00
|
|
|
const disposers = new Array<DisposableFn>;
|
|
|
|
|
|
|
|
|
|
// proxying this would be a good idea
|
|
|
|
|
const object = {
|
|
|
|
|
...window.bunny,
|
|
|
|
|
api: {
|
|
|
|
|
...window.bunny.api,
|
|
|
|
|
patcher: {
|
|
|
|
|
before: shimDisposableFn(disposers, patcher.before),
|
|
|
|
|
after: shimDisposableFn(disposers, patcher.after),
|
|
|
|
|
instead: shimDisposableFn(disposers, patcher.instead)
|
|
|
|
|
},
|
|
|
|
|
commands: {
|
|
|
|
|
...window.bunny.api.commands,
|
|
|
|
|
registerCommand: shimDisposableFn(disposers, registerCommand)
|
|
|
|
|
},
|
|
|
|
|
flux: {
|
|
|
|
|
...window.bunny.api.flux,
|
|
|
|
|
intercept: shimDisposableFn(disposers, window.bunny.api.flux.intercept)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// Added something in here? Make sure to also update BunnyPluginProperty in ./types
|
|
|
|
|
plugin: {
|
2024-10-13 11:29:42 +08:00
|
|
|
createStorage: <T extends object = any>() => createStorage<T>(`plugins/storage/${id}.json`),
|
2024-07-22 00:17:17 +08:00
|
|
|
manifest: registeredPlugins.get(id),
|
|
|
|
|
logger
|
|
|
|
|
}
|
|
|
|
|
} as unknown as BunnyPluginObject;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
object,
|
|
|
|
|
disposers,
|
|
|
|
|
};
|
|
|
|
|
}
|