feat: optimze plugin system

This commit is contained in:
2023-07-19 17:16:50 +08:00
parent 25a8f35f28
commit 3f1de332fd
39 changed files with 6701 additions and 131 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@ccms/api",
"version": "0.27.3-beta.0",
"version": "0.27.3",
"description": "MiaoScript api package",
"keywords": [
"miaoscript",
@@ -19,9 +19,9 @@
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"@ccms/common": "^0.27.3-beta.0",
"@ccms/container": "^0.27.3-beta.0",
"@ccms/polyfill": "^0.27.3-beta.0",
"@ccms/common": "^0.27.3",
"@ccms/container": "^0.27.3",
"@ccms/polyfill": "^0.27.3",
"base64-js": "^1.5.1",
"source-map-builder": "^0.0.7"
},

View File

@@ -25,7 +25,7 @@ export namespace event {
public EventPriority = EventPriority;
private mapEvent = [];
private listenerMap = [];
private pluginEventMap = [];
private cacheSlowEventKey = {};
protected baseEventDir = '';
@@ -140,16 +140,15 @@ export namespace event {
// @ts-ignore
let executor = exec.name || exec.executor || '[anonymous]'
// noinspection JSUnusedGlobalSymbols
var listener = this.register(
let listener = this.register(
eventCls,
this.createExecute(name, exec, eventCls),
priority,
ignoreCancel
)
var listenerMap = this.listenerMap
// add to cache Be used for close plugin to close event
if (!listenerMap[name]) listenerMap[name] = []
var off = () => {
if (!this.pluginEventMap[name]) this.pluginEventMap[name] = []
let off = () => {
if (off['offed']) return
off['offed'] = true
this.unregister(eventCls, listener)
@@ -159,7 +158,7 @@ export namespace event {
exec: executor
}))
}
listenerMap[name].push(off)
this.pluginEventMap[name].push(off)
// noinspection JSUnresolvedVariable
console.debug(i18n.translate("ms.api.event.register", {
name,
@@ -176,10 +175,10 @@ export namespace event {
* @param plugin 插件
*/
disable(plugin: any) {
var eventCache = this.listenerMap[plugin.description.name]
var eventCache = this.pluginEventMap[plugin.description.name]
if (eventCache) {
eventCache.forEach((off: () => any) => off())
delete this.listenerMap[plugin.description.name]
delete this.pluginEventMap[plugin.description.name]
}
}

View File

@@ -6,6 +6,12 @@ const UUID = Java.type('java.util.UUID')
const Math = Java.type('java.lang.Math')
export namespace particle {
@injectable()
export abstract class ParticleSpawner {
abstract spawn(location: any, particle: Particle)
abstract spawnToPlayer(player: any, location: any, particle: Particle)
}
/**
* 表示一个特效对象
*
@@ -498,10 +504,4 @@ export namespace particle {
}
}
}
@injectable()
export abstract class ParticleSpawner {
abstract spawn(location: any, particle: Particle)
abstract spawnToPlayer(player: any, location: any, particle: Particle)
}
}

View File

@@ -0,0 +1,5 @@
export namespace service {
export class ServiceManager {
}
}

View File

@@ -1,6 +1,8 @@
import { plugin } from './index'
import { EventEmitter } from 'events'
import { injectable } from '@ccms/container'
import { plugin } from './index'
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
export namespace task {
@@ -79,7 +81,7 @@ export namespace task {
/**
* 任务抽象
*/
export abstract class Task implements Cancelable {
export abstract class Task extends EventEmitter implements Cancelable {
protected func: Function
protected isAsync: boolean = false;
protected laterTime: number = 0;
@@ -88,7 +90,10 @@ export namespace task {
protected taskId: number
protected innerTask: any
private cancelled: boolean = false
constructor(owner: plugin.Plugin, func: Function, id: number) {
super()
this.owner = owner
this.func = func
this.taskId = id
@@ -134,20 +139,35 @@ export namespace task {
*/
cancel(): boolean {
let result = this.cancel0()
process.emit('task.finish', this)
this.finish()
this.cancelled = true
return result
}
protected run(...args: any[]): void {
try {
this.emit('before', this)
if (this.cancelled) { return }
this.func(...args)
!this.interval && process.emit('task.finish', this)
} catch (ex: any) {
console.console('§4插件执行任务时发生错误', ex)
console.ex(ex)
this.emit('after', this)
} catch (error: any) {
this.emit('error', error)
if (!error.processed) {
console.console('§4插件执行任务时发生错误', error)
console.ex(error)
this.cancel()
}
} finally {
this.emit('finally', this)
if (!this.interval && !this.cancelled) { this.finish() }
}
}
protected finish() {
process.emit('task.finish', this)
this.emit('finish', this)
}
/**
* 提交任务
* @param args 任务参数