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
*/
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()
export abstract class NativePluginManager {
list(): any[] {
list(): NativePlugin[] {
throw new Error("Method not implemented.")
}
has(name: string): boolean {
return true
}
get(name: string): any {
get(name: string): NativePlugin {
throw new Error("Method not implemented.")
}
load(name: string): boolean {

View File

@ -1,12 +1,36 @@
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 {
has(name: string) {
return !!this.get(name)
private bukkitPluginManager: org.bukkit.plugin.PluginManager
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 {
submit0(...args: any[]): task.Cancelable {
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) {
return run[funcName](base.getInstance(), this.laterTime, this.interval)
return run[`runTaskTimer${suffix}`](base.getInstance(), this.laterTime, this.interval)
} 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
* @param metadata PluginMetadata
*/
export function plugin(metadata: pluginApi.PluginMetadata | any) {
export function plugin(metadata: pluginApi.PluginMetadata) {
return function (target: any) {
if (!metadata.source) metadata = { souece: metadata }
metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata }
decorate(injectable(), 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 {
if (metadata.type !== this.type) { return }
this.updatePlugin(metadata.file)
//@ts-ignore
//@ts-ignore load plugin not use cache
metadata.instance = require(metadata.file.toString(), { cache: false })
return metadata
}

View File

@ -39,17 +39,29 @@ class Process extends EventEmitter {
return super.on(event, (...args) => {
try {
listener(...args)
} catch (error) {
} catch (origin) {
try {
super.emit('error', error)
super.emit('error', origin)
} catch (error) {
console.ex(origin)
console.ex(error)
}
}
})
}
nextTick(func: Function) {
microTaskPool.execute(func)
nextTick(func: Function, ...args: any[]) {
microTaskPool.execute(() => {
try {
func(args)
} catch (origin) {
try {
super.emit('error', origin)
} catch (error) {
console.ex(origin)
console.ex(error)
}
}
})
}
exit(code: number) {
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()
Object.defineProperty(process, 'eventLoop', { value: eventLoop })
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('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {})
global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})