From a47896a97fd5817062e55c7e93dd2a1ca731729e Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 1 Feb 2020 03:37:56 +0800 Subject: [PATCH] feat: update Proxy handle and optimize core index Signed-off-by: MiaoWoo --- packages/common/src/proxy.ts | 31 +++++++++++++++++-------------- packages/core/src/index.ts | 18 +++++++----------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/packages/common/src/proxy.ts b/packages/common/src/proxy.ts index 0db74357..28f6ee09 100644 --- a/packages/common/src/proxy.ts +++ b/packages/common/src/proxy.ts @@ -1,27 +1,30 @@ let createProxy = eval(` function(handle){ return new JSAdapter(handle) } `) -export interface ProxyHandle { - get: (target: any, name: string) => any - set: (target: any, name: string, value: any) => void - construct: (target: any, ...args: any[]) => any - has: (target: any, name: string) => boolean - ownKeys: (target: any) => string[] +export interface ProxyHandle extends ProxyHandler { + // get: (target: any, name: string) => any + // set: (target: any, name: string, value: any) => boolean + // construct: (target: any, ...args: any[]) => any + // has: (target: any, name: string) => boolean + // ownKeys: (target: any) => string[] values: (target: any) => any[]; - call: (target: any, name: string, ...args: any[]) => any - delete: (target: any, name: string) => boolean + // call: (target: any, name: string, ...args: any[]) => any + // deleteProperty: (target: any, name: string) => boolean } export class Proxy { - static newProxy(target: any, handle: Partial) { + static newProxy(target: any, handle: Partial): any { + return new Proxy(target, handle) + } + constructor(target: any, handle: Partial) { return createProxy({ - __get__: (name: string) => handle.get ? handle.get(target, name) : target[name], - __put__: (name: string, value: any) => handle.set ? handle.set(target, name, value) : target[name] = value, - __call__: (name: string, ...args: any) => handle.call ? handle.call(target, name, ...args) : target[name](...args), - __new__: (...args: any) => handle.construct ? handle.construct(target, ...args) : new target(...args), + __get__: (name: string) => handle.get ? handle.get(target, name, undefined) : target[name], + __put__: (name: string, value: any) => handle.set ? handle.set(target, name, value, undefined) : target[name] = value, + __call__: (name: string, ...args: any) => handle.apply ? handle.apply(target[name], target, args) : target[name].apply(target, args), + __new__: (...args: any) => handle.construct ? handle.construct(target, args, target) : new target(...args), __getIds__: () => handle.ownKeys ? handle.ownKeys(target) : Object.keys(target), __getValues__: () => handle.values ? handle.values(target) : Object.values(target), __has__: (name: string) => handle.has ? handle.has(target, name) : Object.getOwnPropertyDescriptor(target, name) != undefined, - __delete__: (name: string) => handle.delete ? handle.delete(target, name) : delete target[name] + __delete__: (name: string) => handle.deleteProperty ? handle.deleteProperty(target, name) : delete target[name] }) } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e1f21cf7..a9d14db0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -51,34 +51,30 @@ class MiaoScriptCore { } function detectServer() { - let type = 'unknow' try { Java.type("org.bukkit.Bukkit"); - type = 'bukkit'; - console.info(`Detect Bukkit Compatible set ServerType to ${type} ...`) + return 'bukkit' } catch (ex) { } try { Java.type("org.spongepowered.api.Sponge"); - type = 'sponge'; - console.info(`Detect Sponge Compatible set ServerType to ${type} ...`) + return 'sponge' } catch (ex) { } try { Java.type("net.md_5.bungee.api.ProxyServer"); - type = 'bungee'; - console.info(`Detect Sponge Compatible set ServerType to ${type} ...`) + return 'bungee' } catch (ex) { } - if (type === 'unknow') { throw Error('Unknow Server Type...') } - return type; + throw Error('Unknow Server Type...') } -function init() { +function initialize() { let corePackageStartTime = new Date().getTime() container.bind(ContainerInstance).toConstantValue(container); container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()); let type = detectServer(); + console.info(`Detect Compatible Server set ServerType to ${type} ...`) container.bind(server.ServerType).toConstantValue(type); console.log(`Initialization MiaoScript Package @ms/core @ms/${type}. Please wait...`) require(`@ms/${type}`).default(container); @@ -90,4 +86,4 @@ function init() { return disable; } -export default init(); +export default initialize();