diff --git a/src/bridgeo/plugin/register/plugin-register-logger.ts b/src/bridgeo/plugin/register/plugin-register-logger.ts index 5cf0238..8c302ac 100644 --- a/src/bridgeo/plugin/register/plugin-register-logger.ts +++ b/src/bridgeo/plugin/register/plugin-register-logger.ts @@ -1,4 +1,5 @@ import PluginRegister from "@/bridgeo/plugin/register/plugin-register"; +import { binding } from "@/bridgeo/utils/js/functions"; import { Logger } from "@/bridgeo/utils/js/logger"; export interface IPluginRegisterLogger { @@ -8,10 +9,10 @@ export interface IPluginRegisterLogger { } export class PluginRegisterLogger extends PluginRegister { register(handler: IPluginRegisterLogger) { - const onLoggerRaw = handler.onLoggerRaw?.bind(handler); + const onLoggerRaw = binding(handler).onLoggerRaw; if (onLoggerRaw) handler.logger.on('log.raw', onLoggerRaw); - const onLoggerLogging = handler.onLoggerLogging?.bind(handler); + const onLoggerLogging = binding(handler).onLoggerLogging; if (onLoggerLogging) handler.logger.on('log', onLoggerLogging); this.plugin.lifecycle.on('self.unload', () => { diff --git a/src/bridgeo/plugin/register/plugin-register-relay.ts b/src/bridgeo/plugin/register/plugin-register-relay.ts index b849bac..ad7b4c0 100644 --- a/src/bridgeo/plugin/register/plugin-register-relay.ts +++ b/src/bridgeo/plugin/register/plugin-register-relay.ts @@ -2,6 +2,7 @@ import PluginRegister from "@/bridgeo/plugin/register/plugin-register"; import { PacketOptionsStub, PacketsReceiver, PacketStub } from "@/bridgeo/relay/packet-bus"; import CommonRelay from "@/bridgeo/relay/relay"; import { RelayContext, RelayCreatingMaterial, relays } from "@/bridgeo/relay/starter"; +import { binding } from "@/bridgeo/utils/js/functions"; export interface IPluginRegisterRelay extends PacketsReceiver { onRelayCreating?(material: RelayCreatingMaterial): void; @@ -13,10 +14,10 @@ export interface IPluginRegisterRelay extends PacketsReceiver { } export class PluginRegisterRelay extends PluginRegister { register(handler: IPluginRegisterRelay) { - const onRelayCreating = handler.onRelayCreating?.bind(handler); + const onRelayCreating = binding(handler).onRelayCreating; if (onRelayCreating) this.plugin.lifecycle.on('relay.creating', onRelayCreating); - const onRelayCreated = handler.onRelayCreated?.bind(handler); + const onRelayCreated = binding(handler).onRelayCreated; if (onRelayCreated) { relays.forEach(relay => onRelayCreated(relay, { class: relay.constructor as typeof CommonRelay, @@ -40,13 +41,15 @@ export class PluginRegisterRelay extends PluginRegister { const onUpstream = handler.onUpstream?.bind(context); if (onUpstream) { - context.client.on('upstream', onUpstream); - this.plugin.lifecycle.on('self.unload', () => context.client.off('upstream', onUpstream)); + const args = [ 'upstream', onUpstream ] as const; + context.client.on(...args); + this.plugin.lifecycle.on('self.unload', () => context.client.off(...args)); } const onDownstream = handler.onDownstream?.bind(context); if (onDownstream) { - context.client.on('downstream', onDownstream); - this.plugin.lifecycle.on('self.unload', () => context.client.off('downstream', onDownstream)); + const args = [ 'downstream', onDownstream ] as const; + context.client.on(...args); + this.plugin.lifecycle.on('self.unload', () => context.client.off(...args)); } }; relays.flatMap(relay => Object.values(relay.clients)) diff --git a/src/bridgeo/relay/flow/menu-form.ts b/src/bridgeo/relay/flow/menu-form.ts index c7c392b..56e9eee 100644 --- a/src/bridgeo/relay/flow/menu-form.ts +++ b/src/bridgeo/relay/flow/menu-form.ts @@ -16,6 +16,7 @@ import form, { DialogFormRequest } from "@/bridgeo/relay/packets/form"; import { RelayContext } from "@/bridgeo/relay/starter"; +import { binding } from "@/bridgeo/utils/js/functions"; import { ArrayIndexes } from "@/bridgeo/utils/js/type-utils"; import globalId from "@/bridgeo/utils/mc/global-id"; @@ -31,11 +32,11 @@ abstract class MenuForm { constructor(arg0: RequireQueue | RelayContext | Player, arg1: RequireOnce | undefined = undefined) { if ((arg0 as RelayContext).relay) { const context = arg0 as RelayContext; - this.#queue = context.client.queue.bind(context.client); + this.#queue = binding(context.client).queue; this.#once = listener => context.packets.once('client.modal_form_response', listener); } else if ((arg0 as Player).version) { const player = arg0 as Player; - this.#queue = player.queue.bind(player); + this.#queue = binding(player).queue; this.#once = listener => player.once('modal_form_response', listener); } else { this.#queue = arg0 as RequireQueue; @@ -246,17 +247,17 @@ export class MenuCustomForm extends MenuForm { // noinspection TypeScriptValidateTypes block({ // @ts-expect-error - label: this.addLabel.bind(this), + label: binding(this).addLabel, // @ts-expect-error - input: this.addInput.bind(this), + input: binding(this).addInput, // @ts-expect-error - toggle: this.addToggle.bind(this), + toggle: binding(this).addToggle, // @ts-expect-error - dropdown: this.addDropdown.bind(this), + dropdown: binding(this).addDropdown, // @ts-expect-error - slider: this.addSlider.bind(this), + slider: binding(this).addSlider, // @ts-expect-error - stepSlider: this.addStepSlider.bind(this), + stepSlider: binding(this).addStepSlider, response: () => Promise.reject() }).then().catch(_.noop); /* eslint-enable @typescript-eslint/ban-ts-comment */ diff --git a/src/bridgeo/relay/starter.ts b/src/bridgeo/relay/starter.ts index 3f68a23..96f7cbd 100644 --- a/src/bridgeo/relay/starter.ts +++ b/src/bridgeo/relay/starter.ts @@ -15,6 +15,7 @@ import notification from "@/bridgeo/relay/packets/notification"; import CommonRelay from "@/bridgeo/relay/relay"; import CommonRelayPlayer from "@/bridgeo/relay/relay-player"; import { BridgeoConfig, BridgeoConfigGenerated, generatedBridgeoConfig } from "@/bridgeo/utils/js/bridgeo-config"; +import { binding } from "@/bridgeo/utils/js/functions"; import { Logger } from "@/bridgeo/utils/js/logger"; import { findFreeUdpPort } from "@/bridgeo/utils/js/port-utils"; import { mchalk } from "@/bridgeo/utils/mc/mc-formatter"; @@ -150,15 +151,15 @@ export async function createRelay(options: BridgeoRelayOptions) { playing = false; relay.options.disposable && relay.running && relay.clientCount === 0 && relay.close(); }); - client.on('clientbound', context.packets.onClientBound.bind(context.packets)); - client.on('serverbound', context.packets.onServerBound.bind(context.packets)); + client.on('clientbound', binding(context.packets).onClientBound); + client.on('serverbound', binding(context.packets).onServerBound); context.packets.register(context.local); context.client.context = context; setupRelayPlayer(context); lifecycle.emit('relay.joined', context); }); - relay.on('error', logger.error.bind(logger)); + relay.on('error', binding(logger).error); relays.push(relay); await relay.listen(); diff --git a/src/bridgeo/terminal.ts b/src/bridgeo/terminal.ts index a5f5bab..ef7397e 100644 --- a/src/bridgeo/terminal.ts +++ b/src/bridgeo/terminal.ts @@ -4,6 +4,7 @@ import chalk from "chalk"; import { reloadPlugins, unloadPlugins } from "@/bridgeo/plugin/loader"; import { relays } from "@/bridgeo/relay/starter"; +import { binding } from "@/bridgeo/utils/js/functions"; import { Logger, loggerPool } from "@/bridgeo/utils/js/logger"; import { typeCenter } from "@/bridgeo/utils/js/terminal-typography"; import { toAnsiColorFormat } from "@/bridgeo/utils/mc/mc-formatter"; @@ -23,11 +24,11 @@ const consoleReal = { debug: console.debug, }; const consoleLogger = new Logger('Console').inPool(); -console.log = consoleLogger.info.bind(consoleLogger); -console.info = consoleLogger.info.bind(consoleLogger); -console.warn = consoleLogger.warn.bind(consoleLogger); -console.error = consoleLogger.error.bind(consoleLogger); -console.debug = consoleLogger.debug.bind(consoleLogger); +console.info = binding(consoleLogger).info; +console.warn = binding(consoleLogger).warn; +console.error = binding(consoleLogger).error; +console.debug = binding(consoleLogger).debug; +console.log = console.info; export function initTerminal() { loggerPool.on('log', (_, content) => { diff --git a/src/bridgeo/utils/js/functions.ts b/src/bridgeo/utils/js/functions.ts index 6cc6fdf..3278cae 100644 --- a/src/bridgeo/utils/js/functions.ts +++ b/src/bridgeo/utils/js/functions.ts @@ -86,3 +86,13 @@ export function mixinsClassInstance(clazz: T, objects: any[], exclude: (strin } return clazz; } + +export function binding(receiver: T): T { + if (receiver === null || receiver === undefined) return receiver; + return new Proxy(receiver, { + get(...args) { + const value = Reflect.get(...args); + return typeof value === 'function' ? value.bind(receiver) : value; + } + }); +} diff --git a/src/bridgeo/utils/js/json-binding.ts b/src/bridgeo/utils/js/json-binding.ts index e878079..042a2b0 100644 --- a/src/bridgeo/utils/js/json-binding.ts +++ b/src/bridgeo/utils/js/json-binding.ts @@ -14,26 +14,26 @@ export default function createJsonBinding(path: string, defaul read(); const handler: ProxyHandler = { - get(target, key, receiver) { + get(...args) { if (dynamic) read(); - const value = Reflect.get(target, key, receiver); + const value = Reflect.get(...args); if (value === null) return value; return typeof value === 'object' ? new Proxy(value as object, handler) : value; }, - set(target, key, value) { - const result = Reflect.set(target, key, value); + set(...args) { + const value = Reflect.set(...args); write(); - return result; + return value; }, - defineProperty(target: T, property: string | symbol, attributes: PropertyDescriptor) { - const result = Reflect.defineProperty(target, property, attributes); + defineProperty(...args) { + const value = Reflect.defineProperty(...args); write(); - return result; + return value; }, - deleteProperty(target: T, p: string | symbol) { - const result = Reflect.deleteProperty(target, p); + deleteProperty(...args) { + const value = Reflect.deleteProperty(...args); write(); - return result; + return value; } }; return new Proxy(json, handler);