chore: 优化部分逻辑

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2021-08-03 17:30:53 +08:00
parent 586b6acbbc
commit 8a2b0d7c2e
6 changed files with 66 additions and 18 deletions

View File

@ -16,15 +16,28 @@ export namespace server {
* Runtime Server Instance * Runtime Server Instance
*/ */
export const ServerInstance = Symbol("ServerInstance") export const ServerInstance = Symbol("ServerInstance")
export interface NativePlugin {
name: string
version: string
authors?: string | string[]
enable: boolean
depends?: string[]
softDepends?: string[]
/**
*
*/
origin: any
[key: string]: any
}
@injectable() @injectable()
export abstract class NativePluginManager { export abstract class NativePluginManager {
list(): any[] { list(): NativePlugin[] {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }
has(name: string): boolean { has(name: string): boolean {
return true return true
} }
get(name: string): any { get(name: string): NativePlugin {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }
load(name: string): boolean { load(name: string): boolean {

View File

@ -1,12 +1,36 @@
import { server } from '@ccms/api' import { server } from '@ccms/api'
const Bukkit = org.bukkit.Bukkit const Bukkit: typeof org.bukkit.Bukkit = Java.type('org.bukkit.Bukkit')
export class BukkitNativePluginManager extends server.NativePluginManager { export class BukkitNativePluginManager extends server.NativePluginManager {
has(name: string) { private bukkitPluginManager: org.bukkit.plugin.PluginManager
return !!this.get(name)
constructor() {
super()
this.bukkitPluginManager = Bukkit.getPluginManager()
}
list(): server.NativePlugin[] {
return Java.from(this.bukkitPluginManager.getPlugins()).map(plugin => this.convert(plugin))
}
has(name: string): boolean {
return !!Bukkit.getPluginManager().getPlugin(name)
}
get(name: string): server.NativePlugin {
return this.convert(Bukkit.getPluginManager().getPlugin(name))
}
private convert(plugin: org.bukkit.plugin.Plugin): server.NativePlugin {
if (!plugin) return plugin as any
let desc = plugin.getDescription()
return {
name: plugin.getName(),
version: desc.getVersion(),
authors: Java.from(desc.getAuthors()),
depends: Java.from(desc.getDepend()),
softDepends: Java.from(desc.getSoftDepend()),
enable: plugin.isEnabled(),
origin: plugin
} }
get(name: string) {
return Bukkit.getPluginManager().getPlugin(name)
} }
} }

View File

@ -21,11 +21,11 @@ export class BukkitTaskManager extends task.TaskManager {
export class BukkitTask extends task.Task { export class BukkitTask extends task.Task {
submit0(...args: any[]): task.Cancelable { submit0(...args: any[]): task.Cancelable {
let run = new BukkitRunnable({ run: () => this.run(...args) }) let run = new BukkitRunnable({ run: () => this.run(...args) })
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}` let suffix = this.isAsync ? 'Asynchronously' : ''
if (this.interval) { if (this.interval) {
return run[funcName](base.getInstance(), this.laterTime, this.interval) return run[`runTaskTimer${suffix}`](base.getInstance(), this.laterTime, this.interval)
} else { } else {
return run[funcName](base.getInstance(), this.laterTime) return run[`runTaskLater${suffix}`](base.getInstance(), this.laterTime)
} }
} }
} }

View File

@ -8,9 +8,8 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
* MiaoScript plugin * MiaoScript plugin
* @param metadata PluginMetadata * @param metadata PluginMetadata
*/ */
export function plugin(metadata: pluginApi.PluginMetadata | any) { export function plugin(metadata: pluginApi.PluginMetadata) {
return function (target: any) { return function (target: any) {
if (!metadata.source) metadata = { souece: metadata }
metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata } metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata }
decorate(injectable(), target) decorate(injectable(), target)
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target) Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target)

View File

@ -19,7 +19,7 @@ export class JSFileScanner implements plugin.PluginScanner {
load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata { load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
if (metadata.type !== this.type) { return } if (metadata.type !== this.type) { return }
this.updatePlugin(metadata.file) this.updatePlugin(metadata.file)
//@ts-ignore //@ts-ignore load plugin not use cache
metadata.instance = require(metadata.file.toString(), { cache: false }) metadata.instance = require(metadata.file.toString(), { cache: false })
return metadata return metadata
} }

View File

@ -39,17 +39,29 @@ class Process extends EventEmitter {
return super.on(event, (...args) => { return super.on(event, (...args) => {
try { try {
listener(...args) listener(...args)
} catch (error) { } catch (origin) {
try { try {
super.emit('error', error) super.emit('error', origin)
} catch (error) { } catch (error) {
console.ex(origin)
console.ex(error) console.ex(error)
} }
} }
}) })
} }
nextTick(func: Function) { nextTick(func: Function, ...args: any[]) {
microTaskPool.execute(func) microTaskPool.execute(() => {
try {
func(args)
} catch (origin) {
try {
super.emit('error', origin)
} catch (error) {
console.ex(origin)
console.ex(error)
}
}
})
} }
exit(code: number) { exit(code: number) {
console.log(`process exit by code ${code}!`) console.log(`process exit by code ${code}!`)
@ -198,7 +210,7 @@ Object.defineProperty(process, require('core-js/es/symbol/to-string-tag'), { val
const eventLoop = new EventLoop() const eventLoop = new EventLoop()
Object.defineProperty(process, 'eventLoop', { value: eventLoop }) Object.defineProperty(process, 'eventLoop', { value: eventLoop })
eventLoop.startEventLoop() eventLoop.startEventLoop()
global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {}) global.setGlobal('queueMicrotask', (func: any, ...args: any[]) => process.nextTick(func, args), {})
global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {}) global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {})
global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {}) global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {})
global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {}) global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})