Compare commits
94 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ad290bd48 | ||
| 611d4e5425 | |||
| 894d5d43e6 | |||
| cd57944cb8 | |||
| 75b34bfc48 | |||
| 47478e13aa | |||
|
|
17af1fd49b | ||
| b5423cd26c | |||
| aca864500f | |||
| f86e1a8c94 | |||
| a76f9e8b50 | |||
| 6dcc56b0e5 | |||
| 6f8ea8290b | |||
| 1f574695a1 | |||
| 090c0b2668 | |||
| db8f2d9bc2 | |||
| e02f673c82 | |||
| 47c85f70eb | |||
| da9e2063ca | |||
| 0568a7abbe | |||
| 7375e8e2e9 | |||
| 41d3923d94 | |||
| f111b9891c | |||
| 2e4fdcb798 | |||
| ab7ca932a3 | |||
| e8b3d43cdf | |||
| eaaa0ef139 | |||
| 94840ae59d | |||
| c31eaeb8f6 | |||
| 469a66a277 | |||
| cd6a2023dc | |||
| 498b0ee017 | |||
| 9bfaf984c1 | |||
| 3c33ebf4e6 | |||
| 8af69c98f3 | |||
| 72277f4d1a | |||
| d85f75ba1f | |||
| ed14d317f4 | |||
| fb8a11ea05 | |||
| eae80fa30b | |||
| 3e69fc42d9 | |||
| c5595a65fb | |||
| df266905f2 | |||
| f8046e38be | |||
| 51fb5aece3 | |||
| 7a1dd3e7b3 | |||
| 8f3c799531 | |||
| 3389dbd566 | |||
| 54604e10b8 | |||
| d7247bae19 | |||
| 3ee2dcad0b | |||
| bc19021c63 | |||
| e9a4f2f351 | |||
| 9be9c400ad | |||
| 8eaf789bce | |||
| 7d6f6124b1 | |||
| e06fcc29e9 | |||
| 79ece8db63 | |||
| 66c1d25888 | |||
| ca8d4778bb | |||
| 7dcc5ed45a | |||
| 91d2d836fc | |||
| e5e1bb70a1 | |||
| e93a53aa9f | |||
| e3bceda06b | |||
| 9367e8c2c9 | |||
| 31c7ea2a29 | |||
| 2589633069 | |||
| bc17e2f376 | |||
| 20749d3381 | |||
| 6261b85413 | |||
| da9d590374 | |||
| 0af3ca8c79 | |||
| 561d5b60e8 | |||
| 878a5368af | |||
| 018b972aea | |||
| 4138cf9448 | |||
| 646176b43c | |||
| 506184a104 | |||
| 392f01d509 | |||
| 75b892e1c0 | |||
| c2d825eed0 | |||
| 393e63c017 | |||
| 46d6748f31 | |||
| b6803ed01c | |||
| bbfc70fdd6 | |||
| e28af3fbbc | |||
| 14eef46e67 | |||
| 1c4f512c89 | |||
| a8ae60625b | |||
| e7bc14294e | |||
| 4750f31b6e | |||
| 18df821049 | |||
| ef98c49e1d |
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
||||
# http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
@@ -21,3 +21,4 @@ wallaby.js
|
||||
.theia
|
||||
type_definitions
|
||||
tsconfig.tsbuildinfo
|
||||
*.tsbuildinfo
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"useWorkspaces": true,
|
||||
"npmClient": "yarn",
|
||||
"packages": [
|
||||
@@ -10,6 +10,7 @@
|
||||
"stream": true
|
||||
},
|
||||
"publish": {
|
||||
"access": "public",
|
||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@
|
||||
"build": "lerna run build --scope=\"@ccms/!(plugins)\"",
|
||||
"build:plugins": "lerna run build --scope=\"@ccms/plugins\"",
|
||||
"ug": "yarn upgrade-interactive --latest",
|
||||
"np": "lerna exec \"npm publish --registry https://registry.npmjs.org\" --scope=\"@ccms/!(client|plugins)\"",
|
||||
"np": "lerna exec \"npm publish --access=public --registry https://registry.npmjs.org\" --scope=\"@ccms/!(client|plugins)\"",
|
||||
"lsp": "npm login --registry=https://registry.npmjs.org --scope=@ccms",
|
||||
"lp": "lerna publish"
|
||||
"lp": "lerna publish --registry https://registry.npmjs.org"
|
||||
},
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
"devDependencies": {
|
||||
"lerna": "^3.21.0"
|
||||
"lerna": "^3.22.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/amqp",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript amqp package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,14 +19,17 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/nashorn": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"@javatypes/amqp-client": "^0.0.3",
|
||||
"@javatypes/spring-amqp": "^0.0.3",
|
||||
"@javatypes/spring-rabbit": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { ConnectionFactoryAdapter } from "./connection"
|
||||
import { RabbitTemplateAdapter } from "./template"
|
||||
import { JSClass } from "@ccms/container"
|
||||
|
||||
export const RabbitAdmin = Java.type('org.springframework.amqp.rabbit.core.RabbitAdmin')
|
||||
|
||||
export class RabbitAdminAdapter {
|
||||
@JSClass('org.springframework.amqp.rabbit.core.RabbitAdmin')
|
||||
private RabbitAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin
|
||||
private _Handler: org.springframework.amqp.rabbit.core.RabbitAdmin
|
||||
constructor(config: RabbitTemplateAdapter | org.springframework.amqp.rabbit.core.RabbitTemplate | ConnectionFactoryAdapter | org.springframework.amqp.rabbit.connection.ConnectionFactory) {
|
||||
this._Handler = new RabbitAdmin((config instanceof RabbitTemplateAdapter || config instanceof ConnectionFactoryAdapter) ? config.getHandler() : config)
|
||||
|
||||
@@ -27,4 +27,4 @@ export class MessageListenerContainerAdapter {
|
||||
getHandler() {
|
||||
return this._Handler
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { JSClass } from "@ccms/container"
|
||||
import { RabbitAdminAdapter, MessageListenerContainerAdapter, AcknowledgeMode, MessageHandler, ChannelAwareMessageListenerAdapter } from "./adapter"
|
||||
|
||||
const RabbitAdmin = Java.type('org.springframework.amqp.rabbit.core.RabbitAdmin')
|
||||
|
||||
export class AmqpAdmin {
|
||||
@JSClass('org.springframework.amqp.core.TopicExchange')
|
||||
private TopicExchange: typeof org.springframework.amqp.core.TopicExchange
|
||||
@@ -10,14 +8,15 @@ export class AmqpAdmin {
|
||||
private Queue: typeof org.springframework.amqp.core.Queue
|
||||
@JSClass('org.springframework.amqp.core.Binding')
|
||||
private Binding: typeof org.springframework.amqp.core.Binding
|
||||
|
||||
@JSClass('org.springframework.amqp.rabbit.core.RabbitAdmin')
|
||||
private RabbitAdmin: typeof org.springframework.amqp.rabbit.core.RabbitAdmin
|
||||
@JSClass('org.springframework.amqp.core.Binding.DestinationType')
|
||||
private DestinationType: typeof org.springframework.amqp.core.Binding.DestinationType
|
||||
private DestinationType: org.springframework.amqp.core.Binding.DestinationType
|
||||
|
||||
private amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin
|
||||
|
||||
constructor(amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin | any) {
|
||||
if (amqpAdmin instanceof RabbitAdmin) {
|
||||
if (amqpAdmin instanceof this.RabbitAdmin) {
|
||||
this.amqpAdmin = amqpAdmin
|
||||
} else if (amqpAdmin instanceof RabbitAdminAdapter) {
|
||||
this.amqpAdmin = amqpAdmin.getHandler()
|
||||
@@ -47,7 +46,7 @@ export class AmqpAdmin {
|
||||
}
|
||||
|
||||
declareBinding(queue: string, exchange: string, routerKey: string, argument: any = null) {
|
||||
let binding = new this.Binding(queue, this.DestinationType.QUEUE, exchange, routerKey, argument)
|
||||
let binding = new this.Binding(queue, org.springframework.amqp.core.Binding.DestinationType.QUEUE, exchange, routerKey, argument)
|
||||
this.amqpAdmin.declareBinding(binding)
|
||||
return this
|
||||
}
|
||||
@@ -87,4 +86,4 @@ export class SimpleMessageHandler extends ChannelAwareMessageListenerAdapter {
|
||||
onMessage(content: any, message: org.springframework.amqp.core.Message, channel: com.rabbitmq.client.Channel) {
|
||||
return this.handler(content, message, channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@ccms/types/dist/typings/jdk" />
|
||||
/// <reference types="@ccms/types/dist/typings/spring" />
|
||||
/// <reference types="@ccms/types/dist/typings/spring/amqp" />
|
||||
/// <reference types="@ccms/types/dist/typings/rabbitmq" />
|
||||
/// <reference types="@javatypes/jdk" />
|
||||
/// <reference types="@javatypes/spring-amqp" />
|
||||
/// <reference types="@javatypes/spring-rabbit" />
|
||||
/// <reference types="@javatypes/amqp-client" />
|
||||
|
||||
import { amqp } from '@ccms/api'
|
||||
import { getContainer, reduceMetadata } from '@ccms/container'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/api",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,16 +19,16 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/container": "^0.7.0",
|
||||
"@ccms/ployfill": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"base64-js": "^1.3.1",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0",
|
||||
"@ccms/ployfill": "^0.11.0",
|
||||
"base64-js": "^1.5.1",
|
||||
"source-map-builder": "^0.0.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/base64-js": "^1.2.5",
|
||||
"@types/base64-js": "^1.3.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { injectable } from "@ccms/container";
|
||||
import { injectable } from "@ccms/container"
|
||||
|
||||
export namespace channel {
|
||||
/**
|
||||
@@ -17,32 +17,32 @@ export namespace channel {
|
||||
* @param exec 执行器
|
||||
*/
|
||||
listen(plugin: any, channel: string, exec: ChannelListener) {
|
||||
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError('Plugin can\'t be undefiend!');
|
||||
let name = plugin.description.name;
|
||||
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError('Plugin can\'t be undefiend!')
|
||||
let name = plugin.description.name
|
||||
let listener = this.register(channel, exec)
|
||||
if (!this.listenerMap[name]) this.listenerMap[name] = [];
|
||||
if (!this.listenerMap[name]) this.listenerMap[name] = []
|
||||
let offExec = () => {
|
||||
this.unregister(channel, listener);
|
||||
console.debug(`[${name}] unregister channel ${channel}`);
|
||||
};
|
||||
this.unregister(channel, listener)
|
||||
console.debug(`[${name}] unregister channel ${channel}`)
|
||||
}
|
||||
var off = {
|
||||
channel,
|
||||
listener,
|
||||
off: offExec
|
||||
};
|
||||
this.listenerMap[name].push(off);
|
||||
console.debug(`[${name}] register channel ${channel} => ${exec.name || '[anonymous]'}`);
|
||||
return off;
|
||||
}
|
||||
this.listenerMap[name].push(off)
|
||||
console.debug(`[${name}] register channel ${channel} => ${exec.name || '[anonymous]'}`)
|
||||
return off
|
||||
}
|
||||
/**
|
||||
* 关闭插件注册的通道
|
||||
* @param plugin 插件
|
||||
*/
|
||||
disable(plugin: any) {
|
||||
var channelCache = this.listenerMap[plugin.description.name];
|
||||
var channelCache = this.listenerMap[plugin.description.name]
|
||||
if (channelCache) {
|
||||
channelCache.forEach(t => t.off());
|
||||
delete this.listenerMap[plugin.description.name];
|
||||
channelCache.forEach(t => t.off())
|
||||
delete this.listenerMap[plugin.description.name]
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
31
packages/api/src/chat.ts
Normal file
31
packages/api/src/chat.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { injectable } from '@ccms/container'
|
||||
|
||||
export namespace chat {
|
||||
@injectable()
|
||||
export abstract class Chat {
|
||||
/**
|
||||
* sendJsonChat
|
||||
* @param sender reciver
|
||||
* @param json json
|
||||
* @param type chat Type 0: chat 1: system 2: actionBar
|
||||
*/
|
||||
sendJson(sender: any, json: string | object, type = 0) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
sendMessage(sender: any, message: string) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
sendActionBar(sender: any, message: string) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
clearActionBar(sender: any) {
|
||||
this.sendActionBar(sender, '')
|
||||
}
|
||||
sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
clearTitle(sender: any) {
|
||||
this.sendTitle(sender, '', '', 0, 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import i18n from '@ccms/i18n'
|
||||
import { injectable } from "@ccms/container"
|
||||
import { plugin } from './interfaces'
|
||||
import { plugin } from './plugin'
|
||||
|
||||
export namespace command {
|
||||
@injectable()
|
||||
@@ -13,7 +13,7 @@ export namespace command {
|
||||
*/
|
||||
on(plugin: plugin.Plugin, name: string, exec: { cmd: Function, tab?: Function }) {
|
||||
var cmd = this.create(plugin, name)
|
||||
if (!cmd) { throw Error("") }
|
||||
if (!cmd) { throw Error(`Plugin ${plugin.description.name} can't create Command ${name}!`) }
|
||||
console.debug(i18n.translate("ms.api.command.register", { plugin: plugin.description.name, name, cmd }))
|
||||
if (exec.cmd && typeof exec.cmd === "function") {
|
||||
this.onCommand(plugin, cmd, exec.cmd)
|
||||
@@ -24,6 +24,9 @@ export namespace command {
|
||||
this.onTabComplete(plugin, cmd, exec.tab)
|
||||
}
|
||||
}
|
||||
public tabComplete(sender: any, input: string, index?: number): string[] {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
/**
|
||||
* 取消命令注册
|
||||
* @param plugin 插件
|
||||
@@ -59,9 +62,9 @@ export namespace command {
|
||||
var complete = tabCompleter(sender, command, Java.from(args)) || []
|
||||
return this.copyPartialMatches(complete, token)
|
||||
} catch (ex) {
|
||||
console.i18n("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
|
||||
console.i18n("ms.api.command.tab.completer.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
|
||||
console.ex(ex)
|
||||
console.sender(sender, [i18n.translate("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
|
||||
console.sender(sender, [i18n.translate("ms.api.command.tab.completer.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,10 +97,11 @@ export class MiaoScriptConsole implements Console {
|
||||
MiaoScriptConsole.sourceMaps[fileName] = null
|
||||
let sourceLine = base.read(fileName).split('\n')
|
||||
let lastLine = sourceLine[sourceLine.length - 1]
|
||||
// lastLine is similar //# sourceMappingURL=data:application/json;base64,
|
||||
if (lastLine.startsWith('//# sourceMappingURL=')) {
|
||||
let sourceContent = null
|
||||
let sourceMappingURL = lastLine.split('sourceMappingURL=', 2)[1]
|
||||
if (sourceMappingURL.startsWith('data:application/jsonbase64,')) {
|
||||
if (sourceMappingURL.startsWith('data:application/json;base64,')) {
|
||||
sourceContent = String.fromCharCode(...Array.from(base64.toByteArray(sourceMappingURL.split(',', 2)[1])))
|
||||
} else if (sourceMappingURL.startsWith('http')) {
|
||||
// TODO
|
||||
@@ -122,6 +123,9 @@ export class MiaoScriptConsole implements Console {
|
||||
}
|
||||
} catch (error) {
|
||||
console.debug('search source map', fileName, 'line', lineNumber, 'error:', error)
|
||||
if (global.debug) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
return {
|
||||
fileName,
|
||||
@@ -129,6 +133,7 @@ export class MiaoScriptConsole implements Console {
|
||||
}
|
||||
}
|
||||
stack(ex: Error, color: boolean = true): string[] {
|
||||
if (!ex) return []
|
||||
let stack = ex.getStackTrace()
|
||||
let cache = [(color ? '§c' : '') + ex]
|
||||
//@ts-ignore
|
||||
@@ -153,9 +158,11 @@ export class MiaoScriptConsole implements Console {
|
||||
var { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
|
||||
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
||||
} else {
|
||||
for (let prefix in ignoreLogPrefix) {
|
||||
if (className.startsWith(ignoreLogPrefix[prefix])) {
|
||||
return
|
||||
if (!global.debug) {
|
||||
for (let prefix in ignoreLogPrefix) {
|
||||
if (className.startsWith(ignoreLogPrefix[prefix])) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
export namespace constants {
|
||||
export namespace ServiceIdentifier {
|
||||
/**
|
||||
* Runtime Server NettyPipeline
|
||||
*/
|
||||
export const NettyPipeline = Symbol("NettyPipeline")
|
||||
/**
|
||||
* Runtime Server RootLogger
|
||||
*/
|
||||
export const RootLogger = Symbol("RootLogger")
|
||||
}
|
||||
export namespace Reflect {
|
||||
export const Method = {
|
||||
getServerConnection: [/*spigot 1.8.8*/'aq',/*spigot 1.12.2*/ 'an', /*spigot 1.14.4+*/'getServerConnection', /*catserver 1.12.2*/'func_147137_ag']
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import i18n from '@ccms/i18n'
|
||||
import { injectable, unmanaged } from '@ccms/container'
|
||||
|
||||
const Thread = Java.type('java.lang.Thread');
|
||||
const Thread = Java.type('java.lang.Thread')
|
||||
|
||||
export namespace event {
|
||||
/**
|
||||
@@ -30,7 +30,7 @@ export namespace event {
|
||||
protected baseEventDir = '';
|
||||
|
||||
constructor(@unmanaged() baseEventDir: string) {
|
||||
this.baseEventDir = baseEventDir;
|
||||
this.baseEventDir = baseEventDir
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,74 +39,74 @@ export namespace event {
|
||||
* org.spongepowered.api.event.game.GameRegistryEvent.Register => gameregistryevent$register
|
||||
*/
|
||||
mapEventName() {
|
||||
if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')); }
|
||||
let count = 0;
|
||||
let jar = this.getJarFile(this.baseEventDir);
|
||||
let entries = jar.entries();
|
||||
if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')) }
|
||||
let count = 0
|
||||
let jar = this.getJarFile(this.baseEventDir)
|
||||
let entries = jar.entries()
|
||||
while (entries.hasMoreElements()) {
|
||||
let entry = entries.nextElement();
|
||||
let name = entry.name;
|
||||
let entry = entries.nextElement()
|
||||
let name = entry.name
|
||||
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
||||
// replace name to qualifiedName
|
||||
let qualifiedName = name.replaceAll('/', '.');
|
||||
let qualifiedName = name.replaceAll('/', '.')
|
||||
try {
|
||||
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6));
|
||||
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6))
|
||||
if (this.isValidEvent(clazz)) {
|
||||
let simpleName = this.class2Name(clazz).toLowerCase();
|
||||
console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName }));
|
||||
this.mapEvent[simpleName] = clazz;
|
||||
count++;
|
||||
let simpleName = this.class2Name(clazz).toLowerCase()
|
||||
console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName }))
|
||||
this.mapEvent[simpleName] = clazz
|
||||
count++
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
return count
|
||||
}
|
||||
|
||||
getJarFile(resource: string, loader?: any) {
|
||||
let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource);
|
||||
let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource)
|
||||
if (dirs.hasMoreElements()) {
|
||||
let url = dirs.nextElement();
|
||||
if (url.protocol === "jar") { return url.openConnection().jarFile; }
|
||||
let url = dirs.nextElement()
|
||||
if (url.protocol === "jar") { return url.openConnection().jarFile }
|
||||
}
|
||||
throw new Error(i18n.translate("ms.api.event.resource.not.found", { resource }))
|
||||
}
|
||||
|
||||
class2Name(clazz: any) {
|
||||
return clazz.simpleName;
|
||||
return clazz.simpleName
|
||||
}
|
||||
|
||||
name2Class(name: any, event: string) {
|
||||
let eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
|
||||
let eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event']
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
this.mapEvent[event] = eventCls;
|
||||
eventCls = base.getClass(eventCls)
|
||||
this.mapEvent[event] = eventCls
|
||||
} catch (ex) {
|
||||
console.i18n("ms.api.event.not.found", { name, event })
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
return eventCls;
|
||||
return eventCls
|
||||
}
|
||||
|
||||
execute(name, exec, eventCls) {
|
||||
return (...args: any[]) => {
|
||||
try {
|
||||
let event = args[args.length - 1];
|
||||
let event = args[args.length - 1]
|
||||
if (eventCls.isAssignableFrom(event.getClass())) {
|
||||
let time = Date.now()
|
||||
exec(event);
|
||||
let cost = Date.now() - time;
|
||||
exec(event)
|
||||
let cost = Date.now() - time
|
||||
if (cost > 20) {
|
||||
console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost })
|
||||
}
|
||||
}
|
||||
} catch (ex) {
|
||||
console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex })
|
||||
console.ex(ex);
|
||||
console.ex(ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,34 +120,33 @@ export namespace event {
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
listen(plugin: any, event: string, exec: (event: any) => void, priority: EventPriority = EventPriority.NORMAL, ignoreCancel = false) {
|
||||
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"));
|
||||
var name = plugin.description.name;
|
||||
var eventCls = this.name2Class(name, event);
|
||||
if (!eventCls) { return; }
|
||||
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"))
|
||||
var name = plugin.description.name
|
||||
var eventCls = this.name2Class(name, event)
|
||||
if (!eventCls) { return }
|
||||
if (typeof priority === 'boolean') {
|
||||
ignoreCancel = priority;
|
||||
priority = EventPriority.NORMAL;
|
||||
ignoreCancel = priority
|
||||
priority = EventPriority.NORMAL
|
||||
}
|
||||
priority = priority || EventPriority.NORMAL;
|
||||
ignoreCancel = ignoreCancel || false;
|
||||
priority = priority || EventPriority.NORMAL
|
||||
ignoreCancel = ignoreCancel || false
|
||||
// @ts-ignore
|
||||
let executor = exec.name || exec.executor || '[anonymous]'
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel);
|
||||
var listenerMap = this.listenerMap;
|
||||
var listener = this.register(eventCls, this.execute(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 offExec = () => {
|
||||
this.unregister(eventCls, listener);
|
||||
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls) }));
|
||||
};
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: offExec
|
||||
};
|
||||
listenerMap[name].push(off);
|
||||
if (!listenerMap[name]) listenerMap[name] = []
|
||||
var off = () => {
|
||||
if (off['offed']) return
|
||||
off['offed'] = true
|
||||
this.unregister(eventCls, listener)
|
||||
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls), exec: executor }))
|
||||
}
|
||||
listenerMap[name].push(off)
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug(i18n.translate("ms.api.event.register", { name, event: this.class2Name(eventCls) }));
|
||||
return off;
|
||||
console.debug(i18n.translate("ms.api.event.register", { name, event: this.class2Name(eventCls), exec: executor }))
|
||||
return off
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,10 +154,10 @@ export namespace event {
|
||||
* @param plugin 插件
|
||||
*/
|
||||
disable(plugin: any) {
|
||||
var eventCache = this.listenerMap[plugin.description.name];
|
||||
var eventCache = this.listenerMap[plugin.description.name]
|
||||
if (eventCache) {
|
||||
eventCache.forEach(t => t.off());
|
||||
delete this.listenerMap[plugin.description.name];
|
||||
eventCache.forEach(off => off())
|
||||
delete this.listenerMap[plugin.description.name]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +165,7 @@ export namespace event {
|
||||
* 判断
|
||||
* @param clazz 事件类
|
||||
*/
|
||||
abstract isValidEvent(clazz: any): boolean;
|
||||
abstract isValidEvent(clazz: any): boolean
|
||||
/**
|
||||
* 注册事件
|
||||
* @param eventCls 事件类
|
||||
@@ -174,12 +173,12 @@ export namespace event {
|
||||
* @param priority 优先级
|
||||
* @param ignoreCancel 是否忽略已取消的事件
|
||||
*/
|
||||
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any;
|
||||
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any
|
||||
/**
|
||||
* 取消监听事件
|
||||
* @param event 事件
|
||||
* @param listener 监听器
|
||||
*/
|
||||
abstract unregister(event: any, listener: any): void;
|
||||
abstract unregister(event: any, listener: any): void
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
import "@ccms/nashorn"
|
||||
|
||||
export * from './web'
|
||||
export * from './amqp'
|
||||
export * from './chat'
|
||||
export * from './task'
|
||||
export * from './event'
|
||||
export * from './proxy'
|
||||
export * from './plugin'
|
||||
export * from './server'
|
||||
export * from './console'
|
||||
export * from './channel'
|
||||
export * from './command'
|
||||
export * from './database'
|
||||
export * from './constants'
|
||||
export * from './interfaces'
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export * from './web'
|
||||
export * from './amqp'
|
||||
export * from './plugin'
|
||||
export * from './server'
|
||||
export * from './database'
|
||||
@@ -1,6 +0,0 @@
|
||||
export interface NativePluginManager {
|
||||
load(name: string): boolean;
|
||||
unload(name: string): boolean;
|
||||
reload(name: string): boolean;
|
||||
delete(name: string): boolean;
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { injectable } from '@ccms/container'
|
||||
|
||||
export namespace plugin {
|
||||
/**
|
||||
* MiaoScript Plugin
|
||||
@@ -11,25 +13,56 @@ export namespace plugin {
|
||||
* Runtime Plugin Instance
|
||||
*/
|
||||
export const PluginInstance = Symbol("PluginInstance")
|
||||
/**
|
||||
* MiaoScript Plugin Manager Symbol
|
||||
*/
|
||||
export const PluginManager = Symbol("PluginManager")
|
||||
/**
|
||||
* MiaoScript Plugin Manager Interface
|
||||
*/
|
||||
export interface PluginManager {
|
||||
scan(folder: string): void
|
||||
build(): void
|
||||
loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin
|
||||
load(...args: any[]): void
|
||||
enable(...args: any[]): void
|
||||
disable(...args: any[]): void
|
||||
reload(...args: any[]): void
|
||||
getPlugin(name: string): plugin.Plugin
|
||||
getPlugins(): Map<string, plugin.Plugin>
|
||||
@injectable()
|
||||
export abstract class PluginManager {
|
||||
abstract scan(folder: string): void
|
||||
abstract build(): void
|
||||
abstract loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin
|
||||
abstract load(...args: any[]): void
|
||||
abstract enable(...args: any[]): void
|
||||
abstract disable(...args: any[]): void
|
||||
abstract reload(...args: any[]): void
|
||||
abstract getPlugin(name: string): plugin.Plugin
|
||||
abstract getPlugins(): Map<string, plugin.Plugin>
|
||||
}
|
||||
export const PluginScanner = Symbol("PluginScanner")
|
||||
/**
|
||||
* 插件加载元信息
|
||||
*/
|
||||
export interface PluginLoadMetadata {
|
||||
/**
|
||||
* 插件加载类型
|
||||
*/
|
||||
type: string
|
||||
/**
|
||||
* 插件文件
|
||||
*/
|
||||
file?: string
|
||||
/**
|
||||
* 插件实例
|
||||
*/
|
||||
instance?: any
|
||||
/**
|
||||
* 插件元信息
|
||||
*/
|
||||
metadata?: PluginMetadata
|
||||
/**
|
||||
* 插件扫描器
|
||||
*/
|
||||
scanner: PluginScanner
|
||||
/**
|
||||
* 插件加载器
|
||||
*/
|
||||
loader?: PluginLoader
|
||||
/**
|
||||
* 是否已加载
|
||||
*/
|
||||
loaded?: boolean
|
||||
[key: string]: any
|
||||
}
|
||||
/**
|
||||
* 插件扫描器
|
||||
*/
|
||||
@@ -39,15 +72,20 @@ export namespace plugin {
|
||||
*/
|
||||
type: string
|
||||
/**
|
||||
* 扫描插件 返回插件加载列表
|
||||
* 扫描插件目录 返回插件加载元信息列表
|
||||
* @param target 扫描目标
|
||||
*/
|
||||
scan(target: any): string[]
|
||||
scan(target: any): PluginLoadMetadata[]
|
||||
/**
|
||||
* 读取插件 返回插件加载元信息
|
||||
* @param target
|
||||
*/
|
||||
read(target: any): PluginLoadMetadata
|
||||
/**
|
||||
* 加载扫描到的目标
|
||||
* @param target 加载目标
|
||||
*/
|
||||
load(target: any): any
|
||||
load(target: PluginLoadMetadata): PluginLoadMetadata
|
||||
}
|
||||
export const PluginLoader = Symbol("PluginLoader")
|
||||
/**
|
||||
@@ -63,32 +101,32 @@ export namespace plugin {
|
||||
* @param target 加载目标
|
||||
* @param result 扫描器加载的结果
|
||||
*/
|
||||
require(target: any, result: any): PluginMetadata
|
||||
require(loadMetadata: PluginLoadMetadata): PluginLoadMetadata
|
||||
/**
|
||||
* 构建插件
|
||||
* @param metadata 插件元信息
|
||||
*/
|
||||
build(metadata: any): Plugin
|
||||
build(metadata: PluginMetadata): Plugin
|
||||
/**
|
||||
* Load 阶段
|
||||
* @param plugin 插件
|
||||
*/
|
||||
load(plugin: Plugin): void
|
||||
load?(plugin: Plugin): void
|
||||
/**
|
||||
* Enable 阶段
|
||||
* @param plugin 插件
|
||||
*/
|
||||
enable(plugin: Plugin): void
|
||||
enable?(plugin: Plugin): void
|
||||
/**
|
||||
* Disable 阶段
|
||||
* @param plugin 插件
|
||||
*/
|
||||
disable(plugin: Plugin): void
|
||||
disable?(plugin: Plugin): void
|
||||
/**
|
||||
* Reload 阶段
|
||||
* @param plugin 插件
|
||||
*/
|
||||
reload(plugin: Plugin): void
|
||||
reload?(plugin: Plugin): void
|
||||
}
|
||||
export interface Plugin {
|
||||
description: PluginMetadata
|
||||
@@ -109,21 +147,29 @@ export namespace plugin {
|
||||
}
|
||||
export interface PluginMetadata extends BaseMetadata {
|
||||
/**
|
||||
* 插件名称
|
||||
* 插件名称 不填默认为类名
|
||||
*/
|
||||
name: string
|
||||
name?: string
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
prefix?: string
|
||||
/**
|
||||
* 插件版本
|
||||
* 插件版本 不填默认为 1.0.0
|
||||
*/
|
||||
version: string
|
||||
version?: string
|
||||
/**
|
||||
* 插件版本
|
||||
* 插件作者 不填默认为 Unknow
|
||||
*/
|
||||
author: string | string[]
|
||||
author?: string | string[]
|
||||
/**
|
||||
* 脚本依赖
|
||||
*/
|
||||
depends?: string[]
|
||||
/**
|
||||
* 插件依赖
|
||||
*/
|
||||
nativeDepends?: string[]
|
||||
/**
|
||||
* 插件源文件 必须指定为 __filename
|
||||
*/
|
||||
@@ -133,13 +179,9 @@ export namespace plugin {
|
||||
*/
|
||||
type?: string
|
||||
/**
|
||||
* 插件扫描器
|
||||
* 插件加载信息
|
||||
*/
|
||||
scanner?: PluginScanner
|
||||
/**
|
||||
* 插件加载器
|
||||
*/
|
||||
loader?: PluginLoader
|
||||
loadMetadata?: PluginLoadMetadata
|
||||
/**
|
||||
* 插件本体
|
||||
*/
|
||||
161
packages/api/src/proxy.ts
Normal file
161
packages/api/src/proxy.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import { provideSingleton, Autowired, optional } from '@ccms/container'
|
||||
import { channel } from './channel'
|
||||
|
||||
export namespace proxy {
|
||||
const ByteArrayOutputStream = Java.type('java.io.ByteArrayOutputStream')
|
||||
const DataOutputStream = Java.type('java.io.DataOutputStream')
|
||||
namespace bungeecord {
|
||||
const CHANNEL_NAME = "BungeeCord"
|
||||
export class SubChannelBuilder {
|
||||
private channel: channel.Channel
|
||||
private player: any
|
||||
private params: string[]
|
||||
constructor(channel: channel.Channel, player: any) {
|
||||
this.channel = channel
|
||||
this.player = player
|
||||
this.params = []
|
||||
}
|
||||
connect(server: string) {
|
||||
this.params.push("Connect")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
connectOther(player: string, server: string) {
|
||||
this.params.push("ConnectOther")
|
||||
this.params.push(player)
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
ip() {
|
||||
this.params.push("IP")
|
||||
return this.finalSend()
|
||||
}
|
||||
ipOther(player: string) {
|
||||
this.params.push("IPOther")
|
||||
this.params.push(player)
|
||||
return this.finalSend()
|
||||
}
|
||||
playerCount(server: string | "ALL") {
|
||||
this.params.push("PlayerCount")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get a list of players connected on a certain server, or on ALL the servers.
|
||||
* @param server count server
|
||||
* Response:
|
||||
* String server = in.readUTF(); // The name of the server you got the player list of, as given in args.
|
||||
* String[] playerList = in.readUTF().split(", ");
|
||||
*/
|
||||
playerList(server: string | "ALL") {
|
||||
this.params.push("PlayerList")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get a list of server name strings, as defined in BungeeCord's config.yml
|
||||
* Response:
|
||||
* String[] serverList = in.readUTF().split(", ");
|
||||
*/
|
||||
getServers() {
|
||||
this.params.push("GetServers")
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get this server's name, as defined in BungeeCord's config.yml
|
||||
*/
|
||||
getServer() {
|
||||
this.params.push("GetServer")
|
||||
return this.finalSend()
|
||||
}
|
||||
broadcast(message: string) {
|
||||
this.message("ALL", message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a message (as in, a chat message) to the specified player.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
message(player: string | "ALL", message: string) {
|
||||
this.params.push("Message")
|
||||
this.params.push(player)
|
||||
this.params.push(message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a raw message (as in, a chat message) to the specified player. The advantage of this method over Message is that you can include click events and hover events.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
messageRaw(player: string | "ALL", json: string) {
|
||||
this.params.push("MessageRaw")
|
||||
this.params.push(player)
|
||||
this.params.push(json)
|
||||
return this.finalSend()
|
||||
}
|
||||
forwardAll(channel: string, data: any) {
|
||||
return this.forward("ALL", channel, data)
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forward(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forwardToPlayer(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
generic(...args: string[]) {
|
||||
args && this.params.concat(...args)
|
||||
return this.finalSend()
|
||||
}
|
||||
private send(...middlewares: ((out: java.io.DataOutputStream) => void)[]) {
|
||||
let byteArray = new ByteArrayOutputStream()
|
||||
let out = new DataOutputStream(byteArray)
|
||||
this.params.forEach(utf => out.writeUTF(utf))
|
||||
for (let middleware of middlewares) {
|
||||
middleware(out)
|
||||
}
|
||||
return this.channel.send(this.player, CHANNEL_NAME, byteArray.toByteArray())
|
||||
}
|
||||
private finalSend() {
|
||||
return {
|
||||
send: this.send.bind(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@provideSingleton(BungeeCord)
|
||||
export class BungeeCord {
|
||||
@optional()
|
||||
@Autowired()
|
||||
private channel: channel.Channel
|
||||
/**
|
||||
* 获得代理
|
||||
* @param player 玩家
|
||||
*/
|
||||
for(player: any): bungeecord.SubChannelBuilder {
|
||||
return new bungeecord.SubChannelBuilder(this.channel, player)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
import { injectable } from '@ccms/container'
|
||||
import { injectable, Autowired, ContainerInstance, Container, postConstruct } from '@ccms/container'
|
||||
|
||||
import { NativePluginManager } from './native_plugin'
|
||||
import { constants } from '../../constants'
|
||||
import { constants } from './constants'
|
||||
|
||||
export namespace server {
|
||||
/**
|
||||
@@ -13,58 +12,49 @@ export namespace server {
|
||||
* Runtime Console
|
||||
*/
|
||||
export const Console = Symbol("Console")
|
||||
/**
|
||||
* MiaoScript Server
|
||||
*/
|
||||
export const Server = Symbol("Server")
|
||||
/**
|
||||
* Runtime Server Instance
|
||||
*/
|
||||
export const ServerInstance = Symbol("ServerInstance")
|
||||
@injectable()
|
||||
export abstract class NativePluginManager {
|
||||
has(name: string): boolean {
|
||||
return true
|
||||
}
|
||||
load(name: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
unload(name: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
reload(name: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
delete(name: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
/**
|
||||
* MiaoScript Server
|
||||
*/
|
||||
export interface Server {
|
||||
getVersion(): string
|
||||
getPlayer(name: string): any
|
||||
getOnlinePlayers(): any[]
|
||||
getConsoleSender(): any
|
||||
getService(service: string): any
|
||||
dispatchCommand(sender: string | any, command: string): boolean
|
||||
dispatchConsoleCommand(command: string): boolean
|
||||
getPluginsFolder(): string
|
||||
getNativePluginManager(): NativePluginManager
|
||||
getDedicatedServer?(): any
|
||||
getNettyPipeline(): any
|
||||
getRootLogger(): any
|
||||
sendJson(sender: string | any, json: object | string): void
|
||||
tabComplete?(sender: string | any, input: string, index?: number): string[]
|
||||
}
|
||||
@injectable()
|
||||
export abstract class ReflectServer implements server.Server {
|
||||
protected pipeline: any
|
||||
protected rootLogger: any
|
||||
|
||||
constructor() {
|
||||
this.reflect()
|
||||
}
|
||||
|
||||
export abstract class Server {
|
||||
getVersion(): string {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getPlayer(name: string) {
|
||||
getPlayer(name: string): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getOnlinePlayers(): any[] {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getConsoleSender() {
|
||||
getConsoleSender(): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getService(service: string) {
|
||||
getService(service: string): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dispatchCommand(sender: any, command: string): boolean {
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dispatchConsoleCommand(command: string): boolean {
|
||||
@@ -73,24 +63,47 @@ export namespace server {
|
||||
getPluginsFolder(): string {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getNativePluginManager(): NativePluginManager {
|
||||
getDedicatedServer?(): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getDedicatedServer() {
|
||||
getNettyPipeline(): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getNettyPipeline() {
|
||||
getRootLogger(): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getRootLogger() {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
@injectable()
|
||||
export class ServerChecker {
|
||||
@Autowired(ServerType)
|
||||
private serverType: string
|
||||
|
||||
check(servers: string[]) {
|
||||
// Not set servers -> allow
|
||||
if (!servers || !servers.length) return true
|
||||
// include !type -> deny
|
||||
let denyServers = servers.filter(svr => svr.startsWith("!"))
|
||||
if (denyServers.length !== 0) {
|
||||
return !denyServers.includes(`!${this.serverType}`)
|
||||
} else {
|
||||
// only include -> allow
|
||||
return servers.includes(this.serverType)
|
||||
}
|
||||
}
|
||||
sendJson(sender: any, json: string | object): void {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
tabComplete?(sender: any, input: string, index?: number): string[] {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
@injectable()
|
||||
export abstract class ReflectServer extends server.Server {
|
||||
@Autowired(ContainerInstance)
|
||||
private container: Container
|
||||
|
||||
protected pipeline: any
|
||||
protected rootLogger: any
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
protected reflect() {
|
||||
try {
|
||||
let consoleServer = this.getDedicatedServer()
|
||||
@@ -110,7 +123,11 @@ export namespace server {
|
||||
if (connection.class.name.indexOf('ServerConnection') !== -1
|
||||
|| connection.class.name.indexOf('NetworkSystem') !== -1) { break }
|
||||
connection = undefined
|
||||
} catch (error) { }
|
||||
} catch (error) {
|
||||
if (global.debug) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!connection) { console.error("Can't found ServerConnection!"); return }
|
||||
for (const field of constants.Reflect.Field.listeningChannels) {
|
||||
@@ -118,16 +135,30 @@ export namespace server {
|
||||
promise = reflect.on(connection).get(field).get().get(0)
|
||||
if (promise.class.name.indexOf('Promise') !== -1) { break }
|
||||
promise = undefined
|
||||
} catch (error) { }
|
||||
} catch (error) {
|
||||
if (global.debug) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!promise) { console.error("Can't found listeningChannels!"); return }
|
||||
this.pipeline = reflect.on(promise).get('channel').get().pipeline()
|
||||
this.container.bind(constants.ServiceIdentifier.NettyPipeline).toConstantValue(this.pipeline)
|
||||
}
|
||||
protected reflectRootLogger(consoleServer: any) {
|
||||
try {
|
||||
this.rootLogger = reflect.on(consoleServer).get('LOGGER').get().parent
|
||||
} catch (error) {
|
||||
try { this.rootLogger = reflect.on(consoleServer).get(0).get().parent } catch (error) { }
|
||||
if (global.debug) {
|
||||
console.ex(error)
|
||||
}
|
||||
try {
|
||||
this.rootLogger = reflect.on(consoleServer).get(0).get().parent
|
||||
} catch (error) {
|
||||
if (global.debug) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.rootLogger && this.rootLogger.class.name.indexOf('Logger') === -1) {
|
||||
console.error('Error Logger Class: ' + this.rootLogger.class.name)
|
||||
@@ -138,6 +169,7 @@ export namespace server {
|
||||
this.rootLogger = this.rootLogger.parent
|
||||
}
|
||||
if (!this.rootLogger) { console.error("Can't found rootLogger!") }
|
||||
this.container.bind(constants.ServiceIdentifier.RootLogger).toConstantValue(this.rootLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,105 @@
|
||||
import { plugin } from './index'
|
||||
import { injectable } from '@ccms/container'
|
||||
|
||||
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
|
||||
|
||||
export namespace task {
|
||||
export const TaskManager = Symbol('TaskManager')
|
||||
export interface TaskManager {
|
||||
@injectable()
|
||||
export abstract class TaskManager {
|
||||
protected taskId: java.util.concurrent.atomic.AtomicInteger
|
||||
protected cacheTasks = new Map<number, task.Task>()
|
||||
protected pluginCacheTasks = new Map<string, Map<number, task.Task>>()
|
||||
|
||||
constructor() {
|
||||
this.taskId = new AtomicInteger(0)
|
||||
process.on('task.finish', (task: task.Task) => {
|
||||
let taskId = task.getTaskId()
|
||||
this.cacheTasks.delete(taskId)
|
||||
let ownerName = task.getOwner()?.description.name
|
||||
if (ownerName && this.pluginCacheTasks.has(ownerName)) {
|
||||
this.pluginCacheTasks.get(ownerName).delete(taskId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected pluginCreate(plugin: plugin.Plugin, task: task.Task) {
|
||||
if (!this.pluginCacheTasks.has(plugin.description.name)) {
|
||||
this.pluginCacheTasks.set(plugin.description.name, new Map())
|
||||
}
|
||||
this.pluginCacheTasks.get(plugin.description.name).set(task.getTaskId(), task)
|
||||
return task
|
||||
}
|
||||
|
||||
protected pluginDisable(plugin: plugin.Plugin) {
|
||||
if (this.pluginCacheTasks.has(plugin.description.name)) {
|
||||
this.pluginCacheTasks.get(plugin.description.name).forEach((task) => {
|
||||
task.cancel()
|
||||
})
|
||||
this.pluginCacheTasks.delete(plugin.description.name)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得自增的任务ID
|
||||
*/
|
||||
protected nextId() {
|
||||
return this.taskId.incrementAndGet()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建任务
|
||||
* @param func 任务内容
|
||||
*/
|
||||
create(func: Function): task.Task;
|
||||
create(func: Function, plugin?: plugin.Plugin): task.Task {
|
||||
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !') };
|
||||
let task = this.create0(plugin, func, this.nextId())
|
||||
this.cacheTasks.set(task.getTaskId(), task)
|
||||
if (plugin) { return this.pluginCreate(plugin, task) }
|
||||
return task
|
||||
}
|
||||
cancel(taskId: number) {
|
||||
if (!this.cacheTasks.has(taskId)) { throw new Error(`Task ${taskId} not found!`) }
|
||||
this.cacheTasks.get(taskId).cancel()
|
||||
}
|
||||
/**
|
||||
* 在主线程执行代码
|
||||
* @param func 执行内容
|
||||
*/
|
||||
callSyncMethod(func: Function): any;
|
||||
abstract callSyncMethod(func: Function): any
|
||||
/**
|
||||
* 关闭任务管理器
|
||||
*/
|
||||
disable();
|
||||
disable(plugin?: plugin.Plugin) {
|
||||
if (plugin) { return this.pluginDisable(plugin) }
|
||||
this.disable0()
|
||||
}
|
||||
protected abstract create0(owner: plugin.Plugin, func: Function, id: number): task.Task
|
||||
protected abstract disable0()
|
||||
}
|
||||
/**
|
||||
* 任务抽象
|
||||
*/
|
||||
export abstract class Task {
|
||||
protected plugin: any;
|
||||
protected func: Function;
|
||||
export abstract class Task implements Cancelable {
|
||||
protected func: Function
|
||||
protected isAsync: boolean = false;
|
||||
protected laterTime: number = 0;
|
||||
protected interval: number = 0;
|
||||
protected owner: plugin.Plugin
|
||||
protected taskId: number
|
||||
protected innerTask: any
|
||||
|
||||
constructor(plugin: any, func: Function) {
|
||||
this.plugin = plugin;
|
||||
this.func = func;
|
||||
constructor(owner: plugin.Plugin, func: Function, id: number) {
|
||||
this.owner = owner
|
||||
this.func = func
|
||||
this.taskId = id
|
||||
}
|
||||
|
||||
getOwner() {
|
||||
return this.owner
|
||||
}
|
||||
|
||||
getTaskId() {
|
||||
return this.taskId
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,8 +107,8 @@ export namespace task {
|
||||
* @param isAsync 是否异步
|
||||
*/
|
||||
async(isAsync: boolean = true): task.Task {
|
||||
this.isAsync = isAsync;
|
||||
return this;
|
||||
this.isAsync = isAsync
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,8 +116,8 @@ export namespace task {
|
||||
* @param tick 延时 Tick
|
||||
*/
|
||||
later(tick: number): task.Task {
|
||||
this.laterTime = tick;
|
||||
return this;
|
||||
this.laterTime = tick
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,16 +125,26 @@ export namespace task {
|
||||
* @param tick 循环时间 Tick
|
||||
*/
|
||||
timer(tick: number): task.Task {
|
||||
this.interval = tick;
|
||||
return this;
|
||||
this.interval = tick
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消任务
|
||||
*/
|
||||
cancel(): boolean {
|
||||
let result = this.cancel0()
|
||||
process.emit('task.finish', this)
|
||||
return result
|
||||
}
|
||||
|
||||
protected run(...args: any[]): void {
|
||||
try {
|
||||
this.func(...args);
|
||||
this.func(...args)
|
||||
!this.interval && process.emit('task.finish', this)
|
||||
} catch (ex) {
|
||||
console.console('§4插件执行任务时发生错误', ex)
|
||||
console.ex(ex);
|
||||
console.ex(ex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,12 +152,27 @@ export namespace task {
|
||||
* 提交任务
|
||||
* @param args 任务参数
|
||||
*/
|
||||
abstract submit(...args: any[]): Cancelable;
|
||||
submit(...args: any[]): Cancelable {
|
||||
this.innerTask = this.submit0(...args)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交任务
|
||||
* @param args 任务参数
|
||||
*/
|
||||
protected abstract submit0(...args: any[]): any
|
||||
/**
|
||||
* 取消任务
|
||||
*/
|
||||
protected cancel0(): boolean {
|
||||
return this.innerTask?.cancel()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 返可取消的对象
|
||||
*/
|
||||
export interface Cancelable {
|
||||
cancel(): boolean;
|
||||
cancel(): boolean
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/bukkit",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript bukkit package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,13 +19,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@javatypes/spigot-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,21 +8,47 @@ const Messenger = Bukkit.getMessenger()
|
||||
@provideSingleton(channel.Channel)
|
||||
export class BukkitChannel extends channel.Channel {
|
||||
@inject(plugin.PluginInstance)
|
||||
private pluginInstance: any;
|
||||
|
||||
send(player: any, channel: string, data: any) {
|
||||
player.sendPluginMessage(this.pluginInstance, channel, data);
|
||||
private pluginInstance: any
|
||||
private cacheChannel = new Map<string, any[]>()
|
||||
/**
|
||||
* 给玩家发送通道消息
|
||||
* @param player 接受消息的玩家
|
||||
* @param channel 通道
|
||||
* @param data 数据
|
||||
*/
|
||||
send(player: any, channel: string, data: number[]) {
|
||||
player.sendPluginMessage(this.pluginInstance, channel, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册通道监听器
|
||||
* @param channel 通道
|
||||
* @param listener 监听器
|
||||
*/
|
||||
register(channel: string, listener: channel.ChannelListener) {
|
||||
Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, new PluginMessageListener({
|
||||
if (!this.cacheChannel.has(channel)) this.cacheChannel.set(channel, [])
|
||||
this.cacheChannel.get(channel).push(listener)
|
||||
let pluginMessageListener = new PluginMessageListener({
|
||||
onPluginMessageReceived: (/**String */ channel, /**Player */ player, /**byte[] */data) => {
|
||||
listener(data, { channel, player, data })
|
||||
try {
|
||||
listener(data, { channel, player, data })
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}));
|
||||
Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel);
|
||||
})
|
||||
Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, pluginMessageListener)
|
||||
Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel)
|
||||
return pluginMessageListener
|
||||
}
|
||||
unregister(channel: string, listener: any) {
|
||||
Messenger.unregisterIncomingPluginChannel(this.pluginInstance, channel)
|
||||
Messenger.unregisterOutgoingPluginChannel(this.pluginInstance, channel)
|
||||
if (!this.cacheChannel.has(channel)) return
|
||||
let cacheListener = this.cacheChannel.get(channel)
|
||||
cacheListener = cacheListener.filter(l => l != listener)
|
||||
Messenger.unregisterIncomingPluginChannel(this.pluginInstance, channel, listener)
|
||||
if (cacheListener.length == 0) {
|
||||
this.cacheChannel.delete(channel)
|
||||
Messenger.unregisterOutgoingPluginChannel(this.pluginInstance, channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
packages/bukkit/src/chat.ts
Normal file
23
packages/bukkit/src/chat.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { chat } from '@ccms/api'
|
||||
import { provideSingleton } from '@ccms/container'
|
||||
import bukkitChat from './enhance/chat'
|
||||
|
||||
@provideSingleton(chat.Chat)
|
||||
export class BukkitChat extends chat.Chat {
|
||||
sendJson(sender: any, json: string | object, type = 0) {
|
||||
bukkitChat.send(sender, typeof json === "string" ? json : JSON.stringify(json), type)
|
||||
}
|
||||
sendMessage(sender: any, message: string) {
|
||||
this.sendJson(sender, { text: message }, 0)
|
||||
}
|
||||
sendActionBar(sender: any, message: string) {
|
||||
this.sendJson(sender, { text: message }, 2)
|
||||
}
|
||||
sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
|
||||
try {
|
||||
sender.sendTitle(title, subtitle, fadeIn, time, fadeOut)
|
||||
} catch (error) {
|
||||
sender.sendTitle(title, subtitle)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,44 +4,47 @@ import { command, plugin } from '@ccms/api'
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
import { provideSingleton, postConstruct, inject } from '@ccms/container'
|
||||
|
||||
let Bukkit = org.bukkit.Bukkit;
|
||||
let TabCompleter = Java.type('org.bukkit.command.TabCompleter');
|
||||
let PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
||||
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor');
|
||||
let Bukkit = org.bukkit.Bukkit
|
||||
let TabCompleter = Java.type('org.bukkit.command.TabCompleter')
|
||||
let PluginCommand = Java.type('org.bukkit.command.PluginCommand')
|
||||
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor')
|
||||
|
||||
@provideSingleton(command.Command)
|
||||
export class BukkitCommand extends command.Command {
|
||||
@inject(plugin.PluginInstance)
|
||||
private pluginInstance: any
|
||||
private commandMap: any;
|
||||
private commandMap: any
|
||||
|
||||
@postConstruct()
|
||||
init() {
|
||||
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get();
|
||||
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get()
|
||||
}
|
||||
create(plugin: any, command: string) {
|
||||
var cmd = this.commandMap.getCommand(command)
|
||||
if (cmd && cmd instanceof PluginCommand) { return cmd };
|
||||
cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get();
|
||||
this.commandMap.register(plugin.description.name, cmd);
|
||||
return cmd;
|
||||
cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get()
|
||||
this.commandMap.register(plugin.description.name, cmd)
|
||||
return cmd
|
||||
}
|
||||
remove(plugin: any, command: string) {
|
||||
var cmd = this.commandMap.getCommand(command)
|
||||
if (cmd && cmd instanceof PluginCommand) {
|
||||
cmd.unregister(this.commandMap);
|
||||
cmd.unregister(this.commandMap)
|
||||
}
|
||||
}
|
||||
tabComplete(sender: any, input: string, index?: number): string[] {
|
||||
return Java.from(this.commandMap.tabComplete(sender, input))
|
||||
}
|
||||
onCommand(plugin: any, command: any, executor: Function) {
|
||||
// 必须指定需要实现的接口类型 否则MOD服会报错
|
||||
command.setExecutor(new CommandExecutor({
|
||||
onCommand: super.setExecutor(plugin, command, executor)
|
||||
}));
|
||||
}))
|
||||
}
|
||||
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
|
||||
// 必须指定需要实现的接口类型 否则MOD服会报错
|
||||
command.setTabCompleter(new TabCompleter({
|
||||
onTabComplete: super.setTabCompleter(plugin, command, tabCompleter)
|
||||
}));
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,28 @@
|
||||
/*global Java, base, module, exports, require*/
|
||||
var nmsChatSerializerClass;
|
||||
var nmsChatSerializerMethod;
|
||||
var packetTypeConstructor;
|
||||
var nmsChatMessageTypeClass;
|
||||
var chatMessageTypes;
|
||||
let ChatSerializer: any
|
||||
let nmsChatSerializerMethodName: string
|
||||
let PacketPlayOutChat: any
|
||||
let chatMessageTypes: any
|
||||
|
||||
var RemapUtils;
|
||||
let RemapUtils: any
|
||||
|
||||
var playerConnectionFieldName;
|
||||
var sendPacketMethod;
|
||||
let playerConnectionFieldName: string
|
||||
let sendPacketMethodName: string
|
||||
|
||||
var downgrade = false;
|
||||
let above_1_16 = false
|
||||
let downgrade = false
|
||||
/**
|
||||
* 获取NMS版本
|
||||
*/
|
||||
//@ts-ignore
|
||||
var nmsVersion = org.bukkit.Bukkit.server.class.name.split('.')[3];
|
||||
let nmsVersion = undefined
|
||||
/**
|
||||
* 获取NMS类
|
||||
*/
|
||||
function nmsCls(name) {
|
||||
function nmsCls(name: string) {
|
||||
return base.getClass(['net.minecraft.server', nmsVersion, name].join('.'))
|
||||
}
|
||||
|
||||
function remapMethod(clazz: any, origin: string, test: string, params) {
|
||||
function remapMethod(clazz: any, origin: string, test: string, params: any) {
|
||||
try {
|
||||
return clazz.getMethod(origin, params)
|
||||
} catch (ex) {
|
||||
@@ -48,67 +47,72 @@ function remapFieldName(clazz: any, origin: string, test: string) {
|
||||
}
|
||||
|
||||
function init() {
|
||||
//@ts-ignore
|
||||
nmsVersion = org.bukkit.Bukkit.server.class.name.split('.')[3]
|
||||
try {
|
||||
RemapUtils = Java.type('catserver.server.remapper.RemapUtils');
|
||||
RemapUtils = Java.type('catserver.server.remapper.RemapUtils')
|
||||
} catch (ex) {
|
||||
}
|
||||
nmsChatSerializerClass = nmsCls(nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
|
||||
nmsChatSerializerMethod = remapMethod(nmsChatSerializerClass, 'a', 'func_150699_a', base.getClass('java.lang.String'))
|
||||
var packetTypeClass = nmsCls("PacketPlayOutChat");
|
||||
Java.from(packetTypeClass.constructors).forEach(function(c) {
|
||||
let nmsChatSerializerClass = nmsCls(nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer")
|
||||
let nmsChatSerializerMethod = remapMethod(nmsChatSerializerClass, 'a', 'func_150699_a', base.getClass('java.lang.String'))
|
||||
nmsChatSerializerMethodName = nmsChatSerializerMethod.getName()
|
||||
ChatSerializer = Java.type(nmsChatSerializerClass.getName())
|
||||
let packetTypeClass = nmsCls("PacketPlayOutChat")
|
||||
PacketPlayOutChat = Java.type(packetTypeClass.getName())
|
||||
let packetTypeConstructor: { parameterTypes: any[] }
|
||||
let constructors = packetTypeClass.constructors
|
||||
Java.from(constructors).forEach(function (c) {
|
||||
if (c.parameterTypes.length === 2) {
|
||||
packetTypeConstructor = c
|
||||
}
|
||||
});
|
||||
nmsChatMessageTypeClass = packetTypeConstructor.parameterTypes[1];
|
||||
if (nmsChatMessageTypeClass.isEnum()) {
|
||||
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
|
||||
} else {
|
||||
switch (nmsChatMessageTypeClass.name) {
|
||||
case "int":
|
||||
//@ts-ignore
|
||||
nmsChatMessageTypeClass = java.lang.Integer;
|
||||
break;
|
||||
case "byte":
|
||||
//@ts-ignore
|
||||
nmsChatMessageTypeClass = java.lang.Byte;
|
||||
break;
|
||||
if (c.parameterTypes.length === 3) {
|
||||
packetTypeConstructor = c
|
||||
above_1_16 = true
|
||||
}
|
||||
})
|
||||
let parameterTypes = packetTypeConstructor.parameterTypes
|
||||
let nmsChatMessageTypeClass = parameterTypes[1]
|
||||
if (nmsChatMessageTypeClass.isEnum()) {
|
||||
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants()
|
||||
}
|
||||
var entityPlayerClass = nmsCls('EntityPlayer');
|
||||
var packetClass = nmsCls('Packet');
|
||||
var playerConnectionField = remapFieldName(entityPlayerClass, 'playerConnection', 'field_71135_a')
|
||||
let playerConnectionField = remapFieldName(nmsCls('EntityPlayer'), 'playerConnection', 'field_71135_a')
|
||||
playerConnectionFieldName = playerConnectionField.getName()
|
||||
sendPacketMethod = remapMethod(playerConnectionField.getType(), 'sendPacket', 'func_179290_a', packetClass)
|
||||
sendPacketMethodName = remapMethod(playerConnectionField.getType(), 'sendPacket', 'func_179290_a', nmsCls('Packet')).getName()
|
||||
}
|
||||
|
||||
function json(sender, json) {
|
||||
function json(sender: { name: string }, json: string) {
|
||||
if (downgrade) {
|
||||
return '/tellraw ' + sender.name + ' ' + json
|
||||
} else {
|
||||
send(sender, json, 0);
|
||||
return false;
|
||||
send(sender, json, 0)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function send(sender, json, type) {
|
||||
//@ts-ignore
|
||||
sendPacket(sender, packetTypeConstructor.newInstance(nmsChatSerializerMethod.invoke(null, json), chatMessageTypes == null ? nmsChatMessageTypeClass.valueOf(java.lang.String.valueOf(type)) : chatMessageTypes[type]))
|
||||
function send(sender: any, json: any, type: number) {
|
||||
let packet
|
||||
if (above_1_16) {
|
||||
packet = new PacketPlayOutChat(ChatSerializer[nmsChatSerializerMethodName](json), chatMessageTypes == null ? type : chatMessageTypes[type], sender.getUniqueId())
|
||||
} else {
|
||||
packet = new PacketPlayOutChat(ChatSerializer[nmsChatSerializerMethodName](json), chatMessageTypes == null ? type : chatMessageTypes[type])
|
||||
}
|
||||
sendPacket(sender, packet)
|
||||
}
|
||||
|
||||
function sendPacket(player, p) {
|
||||
sendPacketMethod.invoke(player.handle[playerConnectionFieldName], p)
|
||||
function sendPacket(player: { handle: { [x: string]: { [x: string]: (arg0: any) => void } } }, p: any) {
|
||||
player.handle[playerConnectionFieldName][sendPacketMethodName](p)
|
||||
}
|
||||
|
||||
try {
|
||||
init();
|
||||
init()
|
||||
} catch (ex) {
|
||||
org.bukkit.Bukkit.getConsoleSender().sendMessage(`§6[§cMS§6][§bbukkit§6][§achat§6] §cNMS Inject Error §4${ex} §cDowngrade to Command Mode...`)
|
||||
downgrade = true;
|
||||
downgrade = true
|
||||
}
|
||||
|
||||
let chat = {
|
||||
json
|
||||
json,
|
||||
send
|
||||
}
|
||||
|
||||
export default chat
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
/// <reference types="@ccms/types/dist/typings/bukkit" />
|
||||
/// <reference types="@javatypes/spigot-api" />
|
||||
|
||||
import { server } from '@ccms/api'
|
||||
import { Container } from '@ccms/container'
|
||||
|
||||
import { BukkitConsole } from './console';
|
||||
import './event';
|
||||
import './server';
|
||||
import './command';
|
||||
import './channel';
|
||||
import './task';
|
||||
import { BukkitConsole } from './console'
|
||||
import './chat'
|
||||
import './task'
|
||||
import './event'
|
||||
import './server'
|
||||
import './command'
|
||||
import './channel'
|
||||
|
||||
export default function BukkitImpl(container: Container) {
|
||||
container.bind(server.Console).toConstantValue(BukkitConsole);
|
||||
container.bind(server.Console).toConstantValue(BukkitConsole)
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { server, constants } from '@ccms/api'
|
||||
import { provideSingleton } from '@ccms/container';
|
||||
import { provideSingleton } from '@ccms/container'
|
||||
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
import chat from './enhance/chat'
|
||||
|
||||
let Bukkit = org.bukkit.Bukkit;
|
||||
let Bukkit = org.bukkit.Bukkit
|
||||
|
||||
@provideSingleton(server.Server)
|
||||
export class BukkitServer extends server.ReflectServer {
|
||||
private pluginsFolder: string;
|
||||
private pluginsFolder: string
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.pluginsFolder = Bukkit.getUpdateFolderFile().getParentFile().getCanonicalPath();
|
||||
super()
|
||||
this.pluginsFolder = Bukkit.getUpdateFolderFile().getParentFile().getCanonicalPath()
|
||||
}
|
||||
|
||||
getPlayer(name: string) {
|
||||
return Bukkit.getPlayer(name)
|
||||
return Bukkit.getPlayerExact(name)
|
||||
}
|
||||
getVersion(): string {
|
||||
return Bukkit.getVersion()
|
||||
}
|
||||
getOnlinePlayers() {
|
||||
return Bukkit.getOnlinePlayers() as unknown as any[]
|
||||
return Bukkit.getOnlinePlayers()
|
||||
}
|
||||
getConsoleSender() {
|
||||
return Bukkit.getConsoleSender()
|
||||
@@ -40,27 +40,18 @@ export class BukkitServer extends server.ReflectServer {
|
||||
return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)
|
||||
}
|
||||
getPluginsFolder(): string {
|
||||
return this.pluginsFolder;
|
||||
return this.pluginsFolder
|
||||
}
|
||||
getNativePluginManager() {
|
||||
return Bukkit.getPluginManager() as any;
|
||||
return Bukkit.getPluginManager() as any
|
||||
}
|
||||
getDedicatedServer() {
|
||||
return reflect.on(Bukkit.getServer()).get('console').get()
|
||||
}
|
||||
getNettyPipeline() {
|
||||
return this.pipeline;
|
||||
return this.pipeline
|
||||
}
|
||||
getRootLogger() {
|
||||
return this.rootLogger;
|
||||
}
|
||||
sendJson(sender: string | any, json: object | string): void {
|
||||
if (typeof sender === "string") {
|
||||
sender = this.getPlayer(sender)
|
||||
}
|
||||
let result = chat.json(sender, json)
|
||||
if (result !== false) {
|
||||
this.dispatchConsoleCommand(result)
|
||||
}
|
||||
return this.rootLogger
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,31 @@
|
||||
import { task, plugin } from '@ccms/api'
|
||||
import { inject, provideSingleton } from '@ccms/container';
|
||||
import { inject, provideSingleton } from '@ccms/container'
|
||||
|
||||
const Bukkit = Java.type('org.bukkit.Bukkit');
|
||||
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
|
||||
const Bukkit = Java.type('org.bukkit.Bukkit')
|
||||
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable')
|
||||
const Callable = Java.type('java.util.concurrent.Callable')
|
||||
|
||||
@provideSingleton(task.TaskManager)
|
||||
export class BukkitTaskManager implements task.TaskManager {
|
||||
@inject(plugin.PluginInstance)
|
||||
private pluginInstance: any;
|
||||
|
||||
create(func: Function): task.Task {
|
||||
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
|
||||
return new BukkitTask(this.pluginInstance, func);
|
||||
export class BukkitTaskManager extends task.TaskManager {
|
||||
create0(owner: plugin.Plugin, func: Function, id: number): task.Task {
|
||||
return new BukkitTask(owner, func, id)
|
||||
}
|
||||
callSyncMethod(func: Function): any {
|
||||
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
|
||||
return Bukkit.getScheduler().callSyncMethod(base.getInstance(), new Callable({ call: () => func() })).get()
|
||||
}
|
||||
disable() {
|
||||
Bukkit.getScheduler().cancelTasks(this.pluginInstance);
|
||||
disable0() {
|
||||
Bukkit.getScheduler().cancelTasks(base.getInstance())
|
||||
}
|
||||
}
|
||||
|
||||
export class BukkitTask extends task.Task {
|
||||
submit(...args: any[]): task.Cancelable {
|
||||
submit0(...args: any[]): task.Cancelable {
|
||||
let run = new BukkitRunnable({ run: () => this.run(...args) })
|
||||
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
||||
if (this.interval) {
|
||||
return run[funcName](this.plugin, this.laterTime, this.interval)
|
||||
return run[funcName](base.getInstance(), this.laterTime, this.interval)
|
||||
} else {
|
||||
return run[funcName](this.plugin, this.laterTime)
|
||||
return run[funcName](base.getInstance(), this.laterTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/bungee",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript bungee package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,13 +19,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@javatypes/bungee-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,20 +6,20 @@ const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
||||
@provideSingleton(channel.Channel)
|
||||
export class BungeeChannel extends channel.Channel {
|
||||
@inject(event.Event)
|
||||
private eventManager: event.Event;
|
||||
private eventManager: event.Event
|
||||
|
||||
send(player: any, channel: string, data: any) {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
register(channel: string, listener: channel.ChannelListener) {
|
||||
Bungee.registerChannel(channel);
|
||||
Bungee.registerChannel(channel)
|
||||
// console.console('§6[§eWARN§6] §eMiaoScript channel in BungeeCord only register. you need self hanler PluginMessageEvent!')
|
||||
return this.eventManager.listen({ description: { name: channel } }, "PluginMessageEvent", (event: net.md_5.bungee.api.event.PluginMessageEvent) => {
|
||||
listener(event.getData(), event)
|
||||
})
|
||||
}
|
||||
unregister(channel: string, listener: any) {
|
||||
Bungee.unregisterChannel(channel);
|
||||
listener.off();
|
||||
unregister(channel: string, off: any) {
|
||||
Bungee.unregisterChannel(channel)
|
||||
off()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/// <reference types="@ccms/types/dist/typings/bungee" />
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
|
||||
import { server } from '@ccms/api'
|
||||
import { Container } from '@ccms/container'
|
||||
|
||||
@@ -3,29 +3,29 @@ import { provideSingleton, inject, postConstruct } from '@ccms/container'
|
||||
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
|
||||
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy();
|
||||
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
||||
|
||||
@provideSingleton(server.Server)
|
||||
export class BungeeServer implements server.Server {
|
||||
private pluginsFolder: string;
|
||||
private pipeline: any;
|
||||
private rootLogger: any;
|
||||
private pluginsFolder: string
|
||||
private pipeline: any
|
||||
private rootLogger: any
|
||||
|
||||
@inject(task.TaskManager)
|
||||
private task: task.TaskManager
|
||||
|
||||
constructor() {
|
||||
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath();
|
||||
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath()
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
initialize() {
|
||||
let count = 0;
|
||||
let count = 0
|
||||
let wait = this.task.create(() => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline()
|
||||
wait.cancel();
|
||||
wait.cancel()
|
||||
} catch (ex) {
|
||||
count++
|
||||
if (count > 50) {
|
||||
@@ -44,7 +44,7 @@ export class BungeeServer implements server.Server {
|
||||
}
|
||||
|
||||
getPlayer(name: string) {
|
||||
return Bungee.getPlayer(name);
|
||||
return Bungee.getPlayer(name)
|
||||
}
|
||||
getVersion(): string {
|
||||
return Bungee.getVersion()
|
||||
@@ -56,7 +56,7 @@ export class BungeeServer implements server.Server {
|
||||
return Bungee.getConsole()
|
||||
}
|
||||
getService(service: string) {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
if (typeof sender === 'string') {
|
||||
@@ -68,18 +68,15 @@ export class BungeeServer implements server.Server {
|
||||
return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
|
||||
}
|
||||
getPluginsFolder(): string {
|
||||
return this.pluginsFolder;
|
||||
return this.pluginsFolder
|
||||
}
|
||||
getNativePluginManager() {
|
||||
return Bungee.getPluginManager() as any
|
||||
}
|
||||
getNettyPipeline() {
|
||||
return this.pipeline;
|
||||
return this.pipeline
|
||||
}
|
||||
getRootLogger() {
|
||||
return this.rootLogger;
|
||||
}
|
||||
sendJson(sender: string | any, json: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
return this.rootLogger
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,32 @@
|
||||
import { task, plugin } from '@ccms/api'
|
||||
import { inject, provideSingleton } from '@ccms/container';
|
||||
import { inject, provideSingleton } from '@ccms/container'
|
||||
|
||||
var Runnable = Java.type('java.lang.Runnable')
|
||||
let TimeUnit = Java.type('java.util.concurrent.TimeUnit')
|
||||
|
||||
@provideSingleton(task.TaskManager)
|
||||
export class BungeeTaskManager implements task.TaskManager {
|
||||
@inject(plugin.PluginInstance)
|
||||
private pluginInstance: any;
|
||||
|
||||
create(func: Function): task.Task {
|
||||
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
|
||||
return new BungeeTask(this.pluginInstance, func);
|
||||
export class BungeeTaskManager extends task.TaskManager {
|
||||
create0(owner: plugin.Plugin, func: Function, id: number): task.Task {
|
||||
return new BungeeTask(owner, func, id)
|
||||
}
|
||||
callSyncMethod(func: Function): any {
|
||||
return func();
|
||||
return func()
|
||||
}
|
||||
disable() {
|
||||
this.pluginInstance.getProxy().getScheduler().cancel(this.pluginInstance)
|
||||
disable0() {
|
||||
base.getInstance().getProxy().getScheduler().cancel(base.getInstance())
|
||||
}
|
||||
}
|
||||
|
||||
export class BungeeTask extends task.Task {
|
||||
submit(...args: any[]): task.Cancelable {
|
||||
submit0(...args: any[]): task.Cancelable {
|
||||
let run = new Runnable({ run: () => this.run(...args) })
|
||||
if (this.isAsync) {
|
||||
return this.plugin.getProxy().getScheduler().runAsync(this.plugin, run)
|
||||
return base.getInstance().getProxy().getScheduler().runAsync(base.getInstance(), run)
|
||||
}
|
||||
if (this.interval) {
|
||||
return this.plugin.getProxy().getScheduler().schedule(this.plugin, run, this.laterTime * 50, this.interval * 50, TimeUnit.MILLISECONDS)
|
||||
return base.getInstance().getProxy().getScheduler().schedule(base.getInstance(), run, this.laterTime * 50, this.interval * 50, TimeUnit.MILLISECONDS)
|
||||
} else {
|
||||
return this.plugin.getProxy().getScheduler().schedule(this.plugin, run, this.laterTime * 50, TimeUnit.MILLISECONDS)
|
||||
return base.getInstance().getProxy().getScheduler().schedule(base.getInstance(), run, this.laterTime * 50, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "@ccms/client",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript client package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,10 +22,10 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"minecraft-protocol": "^1.11.0"
|
||||
"minecraft-protocol": "^1.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ import { $ } from './color'
|
||||
export function attachEvents(client) {
|
||||
client.on('chat', (packet) => {
|
||||
// Listen for chat messages and echo them back.
|
||||
var jsonMsg = JSON.parse(packet.message);
|
||||
var jsonMsg = JSON.parse(packet.message)
|
||||
console.log($(jsonMsg))
|
||||
})
|
||||
client.on('state', (newState, oldState) => {
|
||||
console.log('Client Change State', oldState, 'to', newState)
|
||||
let targetServer = process.argv[3]
|
||||
let targetServer = process.argv[6]
|
||||
if (newState == "play" && targetServer) {
|
||||
setTimeout(() => {
|
||||
client.write('chat', {
|
||||
@@ -24,4 +24,10 @@ export function attachEvents(client) {
|
||||
} else if (packet.health > 0) {
|
||||
}
|
||||
})
|
||||
client.on('kick_disconnect', (packet) => {
|
||||
console.log($(packet.reason))
|
||||
})
|
||||
client.on('disconnect', (packet) => {
|
||||
console.log($(packet.reason))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,33 +5,52 @@ import { attachForge } from './forge'
|
||||
import { attachEvents } from './event'
|
||||
|
||||
let username = process.argv[2] || 'Mr_jtb'
|
||||
let version = process.argv[3] || '1.12.2'
|
||||
let address = process.argv[4] || '192.168.2.5:25577'
|
||||
let realAddress = address.split(":");
|
||||
let client = createConnection(realAddress[0], parseInt(realAddress[1] || "25565"), username)
|
||||
let password = process.argv[3] || ''
|
||||
let version = process.argv[4] || '1.12.2'
|
||||
let readAddress = process.argv[5] || '192.168.2.5:25577'
|
||||
let realAddress = readAddress.split(":")
|
||||
let address = realAddress[0]
|
||||
let port = parseInt(realAddress[1] || "25565")
|
||||
let client = commandLineCreateClient()
|
||||
|
||||
function createConnection(host: string, port: number, username: string) {
|
||||
function commandLineCreateClient() {
|
||||
return createConnection(address, port, username, password)
|
||||
}
|
||||
|
||||
function createConnection(host: string, port: number, username: string, password: string) {
|
||||
let client = createClient({
|
||||
version,
|
||||
host,
|
||||
port,
|
||||
username,
|
||||
skipValidation: true
|
||||
password,
|
||||
clientToken: 'd02c7f39-2376-45da-a5a5-50e24fa8b185',
|
||||
//@ts-ignore
|
||||
authServer: 'https://mcsso.yumc.pw/api/yggdrasil/authserver',
|
||||
sessionServer: 'https://mcsso.yumc.pw/api/yggdrasil/sessionserver'
|
||||
})
|
||||
|
||||
attachCommon(client)
|
||||
attachForge(client)
|
||||
attachEvents(client)
|
||||
return client;
|
||||
return client
|
||||
}
|
||||
|
||||
client.on('error', (error) => {
|
||||
console.log("Client Error", error)
|
||||
})
|
||||
|
||||
client.on('end', (resone) => {
|
||||
console.log("Client End Resone:", resone)
|
||||
client = createConnection('192.168.2.5', 25577, username)
|
||||
})
|
||||
function attachCommon(client) {
|
||||
client.on('error', (error) => {
|
||||
console.log("Client Error", error)
|
||||
})
|
||||
client.on('end', (resone) => {
|
||||
console.log("Client End Resone:", resone)
|
||||
if (`${resone}` != "SocketClosed") {
|
||||
setTimeout(() => {
|
||||
client = commandLineCreateClient()
|
||||
}, 500)
|
||||
} else {
|
||||
process.exit(0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const rl = createInterface({
|
||||
input: process.stdin,
|
||||
@@ -54,22 +73,22 @@ const rl = createInterface({
|
||||
rl.on('line', function (line) {
|
||||
switch (line) {
|
||||
case "":
|
||||
break;
|
||||
break
|
||||
case "eval":
|
||||
break;
|
||||
break
|
||||
case "write":
|
||||
break;
|
||||
break
|
||||
case "/respawn":
|
||||
client.write('client_command', { payload: 0 })
|
||||
break;
|
||||
break
|
||||
case "//reco":
|
||||
client.end("")
|
||||
client = createConnection('192.168.2.5', 25577, username)
|
||||
break;
|
||||
client = commandLineCreateClient()
|
||||
break
|
||||
case "//quit":
|
||||
console.info('Disconnected')
|
||||
client.end("")
|
||||
break;
|
||||
break
|
||||
case "//end":
|
||||
console.info('Forcibly ended client')
|
||||
process.exit(0)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/common",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,12 +19,11 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"@javatypes/jdk": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/nashorn": "^0.7.0"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"gitHead": "562e2d00175c9d3a99c8b672aa07e6d92706a027"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import * as querystring from 'querystring'
|
||||
|
||||
const URL = Java.type('java.net.URL')
|
||||
const Paths = Java.type('java.nio.file.Paths');
|
||||
const Files = Java.type('java.nio.file.Files');
|
||||
const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption');
|
||||
const Paths = Java.type('java.nio.file.Paths')
|
||||
const Files = Java.type('java.nio.file.Files')
|
||||
const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption')
|
||||
|
||||
export type Method =
|
||||
| 'get' | 'GET'
|
||||
@@ -13,36 +15,49 @@ export type Method =
|
||||
| 'patch' | 'PATCH'
|
||||
|
||||
interface RequestConfig {
|
||||
url?: string;
|
||||
method?: Method;
|
||||
headers?: { [key: string]: string };
|
||||
params?: { [key: string]: string };
|
||||
data?: any;
|
||||
url?: string
|
||||
method?: Method
|
||||
headers?: { [key: string]: string }
|
||||
params?: { [key: string]: string }
|
||||
data?: any
|
||||
}
|
||||
|
||||
function request(config: RequestConfig) {
|
||||
// @ts-ignore
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open(config.method, config.url, false);
|
||||
let xhr = new XMLHttpRequest()
|
||||
xhr.open(config.method, config.url, false)
|
||||
for (const header in config.headers) {
|
||||
xhr.setRequestHeader(header, config.headers[header]);
|
||||
xhr.setRequestHeader(header, config.headers[header])
|
||||
}
|
||||
xhr.send(typeof config.data === "string" ? config.data : JSON.stringify(config.data));
|
||||
if ((xhr.getResponseHeader("Content-Type") + '').indexOf('application/json') != -1) {
|
||||
let body = config.data
|
||||
if (body && typeof body !== "string") {
|
||||
switch (config.headers['Content-Type']) {
|
||||
case "application/json":
|
||||
body = JSON.stringify(body)
|
||||
break
|
||||
case "application/x-www-form-urlencoded":
|
||||
body = querystring.encode(body)
|
||||
break
|
||||
}
|
||||
}
|
||||
xhr.send(body)
|
||||
if (xhr.getResponseHeader("Content-Type").indexOf('application/json') != -1) {
|
||||
xhr.responseType = "json"
|
||||
}
|
||||
return xhr.get();
|
||||
return xhr.get()
|
||||
}
|
||||
|
||||
function download(url: string, target: string) {
|
||||
console.debug(`Start Download file ${target} from ${url}....`)
|
||||
Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING)
|
||||
console.debug(`File ${target} Download Complate...`)
|
||||
}
|
||||
|
||||
function _proxy(method: Method) {
|
||||
return function (url: string, data?: any, config?: RequestConfig) {
|
||||
return request({ url, method, data, ...config });
|
||||
return function (url: string, data?: any, config: RequestConfig = {}) {
|
||||
if (!config.headers) { config.headers = {} }
|
||||
config.headers['Content-Type'] = config.headers['Content-Type'] ?? 'application/json'
|
||||
return request({ url, method, data, ...config })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1243
packages/common/src/qrcode.ts
Normal file
1243
packages/common/src/qrcode.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,18 @@
|
||||
/// <reference types="@ccms/types/dist/typings/jdk" />
|
||||
/// <reference types="@javatypes/jdk" />
|
||||
|
||||
const JavaClass = Java.type('java.lang.Class')
|
||||
const JavaObject = Java.type('java.lang.Object')
|
||||
const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException')
|
||||
const fieldCache = new Map<string, java.lang.reflect.Field>()
|
||||
const methodCache = new Map<string, java.lang.reflect.Method>()
|
||||
|
||||
/**
|
||||
* 反射工具类
|
||||
* Created by MiaoWoo on 2017/2/9 0009.
|
||||
*/
|
||||
const JavaClass = Java.type('java.lang.Class')
|
||||
const JavaObject = Java.type('java.lang.Object')
|
||||
const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException')
|
||||
const methodCache = []
|
||||
|
||||
class Reflect {
|
||||
private obj: java.lang.Object
|
||||
private class: java.lang.Class
|
||||
private class: java.lang.Class<any>
|
||||
|
||||
constructor(obj: any) {
|
||||
// if (obj === undefined || obj === null) { throw Error(`reflect object can't be ${obj}!`) }
|
||||
@@ -26,23 +27,34 @@ class Reflect {
|
||||
}
|
||||
}
|
||||
|
||||
method(name: string, ...args: any[]) {
|
||||
return declaredMethod(this.class, name, args)
|
||||
method(name: string, ...args: java.lang.Class<any>[]) {
|
||||
return declaredMethod(this.class, name, ...args)
|
||||
}
|
||||
|
||||
methods() {
|
||||
return Java.from(declaredMethods(this.class))
|
||||
}
|
||||
|
||||
field(name: string | java.lang.String): Reflect {
|
||||
try {
|
||||
// Try getting a public field
|
||||
let field = this.class.getField(name)
|
||||
return on(field.get(this.obj))
|
||||
} catch (ex) {
|
||||
// Try again, getting a non-public field
|
||||
return on(accessible(declaredField(this.class, name)).get(this.obj))
|
||||
field(nameOrIndex: string | java.lang.String | number, declared = false): java.lang.reflect.Field {
|
||||
if (nameOrIndex == undefined || nameOrIndex == null) throw new Error(`reflect field name can't be ${nameOrIndex} from ${this.class.getName()}!`)
|
||||
let key = this.class.getName() + ':' + nameOrIndex + ':' + declared
|
||||
if (fieldCache.has(key)) {
|
||||
return fieldCache.get(key)
|
||||
}
|
||||
let field = null
|
||||
if (typeof nameOrIndex == "number") {
|
||||
field = this.fields(declared)[nameOrIndex]
|
||||
} else {
|
||||
try {
|
||||
// Try getting a public field
|
||||
field = this.class.getField(nameOrIndex)
|
||||
} catch (ex) {
|
||||
// Try again, getting a non-public field
|
||||
field = declaredField(this.class, nameOrIndex)
|
||||
}
|
||||
}
|
||||
if (!field) throw new Error(`can't reflect field ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} from ${this.class.getName()}!`)
|
||||
return accessible(field)
|
||||
}
|
||||
|
||||
fields(declared = false): java.lang.reflect.Field[] {
|
||||
@@ -50,7 +62,7 @@ class Reflect {
|
||||
}
|
||||
|
||||
values(declared = false) {
|
||||
return this.fields(declared).reduce((cache, field) => { return cache[field.getName()] = this.field(field.getName()).get() }, {}) as any
|
||||
return this.fields(declared).reduce((cache, field) => { return cache[field.getName()] = this.get(field.getName()).get() }, {}) as any
|
||||
}
|
||||
|
||||
call(...args: any[]): Reflect {
|
||||
@@ -64,17 +76,12 @@ class Reflect {
|
||||
get(index: number, declared?: boolean): Reflect
|
||||
get(prop: string): Reflect
|
||||
get(param?: string | number, declared: boolean = true): Reflect | any {
|
||||
if (param == undefined || param == null) return this.obj
|
||||
if (typeof param == "number") {
|
||||
return on(accessible(this.fields(declared)[param]).get(this.obj))
|
||||
}
|
||||
if (typeof param == "string") {
|
||||
return this.field(param)
|
||||
}
|
||||
if (arguments.length === 0) return this.obj
|
||||
return on(this.field(param, declared).get(this.obj))
|
||||
}
|
||||
|
||||
set(name: any, value: any): Reflect {
|
||||
accessible(declaredField(this.class, name)).set(this.obj, value)
|
||||
set(param: string | number, value: any, declared: boolean = true): Reflect {
|
||||
this.field(param, declared).set(this.obj, value)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -90,16 +97,13 @@ function types(values: any[], def?: any) {
|
||||
if (values === null) {
|
||||
return []
|
||||
}
|
||||
let result: java.lang.Class[] = []
|
||||
let result: java.lang.Class<any>[] = []
|
||||
values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class))
|
||||
return result
|
||||
}
|
||||
|
||||
function accessible<T extends java.lang.reflect.AccessibleObject>(accessible: T): T {
|
||||
if (accessible === null) {
|
||||
return null
|
||||
}
|
||||
if (!accessible.isAccessible()) {
|
||||
if (!accessible?.isAccessible()) {
|
||||
accessible.setAccessible(true)
|
||||
}
|
||||
return accessible
|
||||
@@ -119,7 +123,7 @@ function declaredConstructor(clazz, param) {
|
||||
return accessible(constructor)
|
||||
}
|
||||
|
||||
function declaredField(clazz: java.lang.Class, name: string | java.lang.String) {
|
||||
function declaredField(clazz: java.lang.Class<any>, name: string | java.lang.String) {
|
||||
if (!clazz) { throw Error(`target class can't be ${clazz}!`) }
|
||||
let target = clazz
|
||||
let field = null
|
||||
@@ -139,63 +143,43 @@ function declaredField(clazz: java.lang.Class, name: string | java.lang.String)
|
||||
return field
|
||||
}
|
||||
|
||||
function declaredMethod(clazz: java.lang.Class, name: string, ...clazzs: any[]): java.lang.reflect.Method {
|
||||
let key = clazz.getName() + '.' + name + ':' + (clazzs || []).join(':')
|
||||
if (!methodCache[key]) {
|
||||
function declaredMethod(clazz: java.lang.Class<any>, nameOrIndex: string | number, ...clazzs: java.lang.Class<any>[]): java.lang.reflect.Method {
|
||||
let key = clazz.getName() + '.' + nameOrIndex + ':' + (clazzs || []).map(c => c.getName()).join(':')
|
||||
if (methodCache.has(key)) { return methodCache.get(key) }
|
||||
if (typeof nameOrIndex === "number") {
|
||||
methodCache.set(key, declaredMethods(clazz)[nameOrIndex])
|
||||
} else {
|
||||
try {
|
||||
methodCache[key] = clazz.getMethod(name, clazzs as any)
|
||||
methodCache.set(key, clazz.getMethod(nameOrIndex, clazzs as any))
|
||||
} catch (ex) {
|
||||
try {
|
||||
methodCache[key] = clazz.getDeclaredMethod(name, clazzs as any)
|
||||
methodCache.set(key, clazz.getDeclaredMethod(nameOrIndex, clazzs as any))
|
||||
} catch (ex) {
|
||||
for (const m of Java.from(declaredMethods(clazz))) {
|
||||
if (m.name == name) {
|
||||
methodCache[key] = m
|
||||
if (m.getName() == nameOrIndex) {
|
||||
methodCache.set(key, m)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return methodCache[key]
|
||||
if (!methodCache.has(key)) throw new Error(`can't reflect method ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} from ${clazz.getName()}!`)
|
||||
return methodCache.get(key)
|
||||
}
|
||||
|
||||
function declaredMethods(clazz) {
|
||||
return clazz.declaredMethods
|
||||
function declaredMethods(clazz: java.lang.Class<any>) {
|
||||
return clazz.getDeclaredMethods()
|
||||
}
|
||||
|
||||
let classMethodsCache: any[] = []
|
||||
|
||||
function mapToObject(javaObj) {
|
||||
if (!javaObj || !javaObj.class) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) }
|
||||
let target = {}
|
||||
getJavaObjectMethods(javaObj).forEach(t => mapMethod(target, javaObj, t))
|
||||
if (!Java.isJavaObject(javaObj)) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) }
|
||||
let target = Proxy.newProxy(javaObj, {
|
||||
apply: (target, name, args) => { return args ? javaObj[name](args) : javaObj[name]() }
|
||||
})
|
||||
return target
|
||||
}
|
||||
|
||||
function getJavaObjectMethods(javaObj) {
|
||||
let className = javaObj.class.name
|
||||
if (!classMethodsCache[className]) {
|
||||
let names: any[] = []
|
||||
let methods = javaObj.class.methods
|
||||
for (let i in methods) {
|
||||
names.push(methods[i].name)
|
||||
}
|
||||
classMethodsCache[className] = names
|
||||
}
|
||||
return classMethodsCache[className]
|
||||
}
|
||||
|
||||
function mapMethod(target, source, name) {
|
||||
target[name] = function __SimpleDynamicMethod__(...args) {
|
||||
if (args.length > 0) {
|
||||
return source[name](args)
|
||||
} else {
|
||||
return source[name]()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function on(obj) {
|
||||
// if (!obj || !obj.class) { throw new TypeError(`参数 ${obj} 不是一个Java对象!`) }
|
||||
return new Reflect(obj)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/compile",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript compile package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -21,6 +21,6 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/container",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript container package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,13 +19,13 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"inversify": "^5.0.1",
|
||||
"inversify-binding-decorators": "^4.0.0",
|
||||
"@ccms/nashorn": "^0.7.0"
|
||||
"inversify-binding-decorators": "^4.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import "reflect-metadata"
|
||||
import { initContainer, getContainer } from './decorators'
|
||||
import { interfaces, Container } from 'inversify'
|
||||
import { interfaces, Container, inject, named } from 'inversify'
|
||||
import { fluentProvide } from 'inversify-binding-decorators'
|
||||
import { ioc } from "./constants"
|
||||
|
||||
@@ -36,10 +36,10 @@ export const provideSingletonNamed = (identifier: interfaces.ServiceIdentifier<a
|
||||
* @param className Java全类名
|
||||
*/
|
||||
export const JavaClass = (className: string) => {
|
||||
return function (target: any, propertyKey: string, index?: number) {
|
||||
return function (target: object, propertyKey: string, index?: number) {
|
||||
try { target[propertyKey] = Java.type(className).class; return } catch (error) { }
|
||||
try { target[propertyKey] = base.getClass(className); return } catch (error) { }
|
||||
console.warn('JavaClass Inject target', target, 'propertyKey', propertyKey, 'failed!')
|
||||
console.warn('JavaClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,10 +48,10 @@ export const JavaClass = (className: string) => {
|
||||
* @param className Java 全类名
|
||||
*/
|
||||
export const JSClass = (className: string) => {
|
||||
return function (target: any, propertyKey: string, index?: number) {
|
||||
return function (target: object, propertyKey: string, index?: number) {
|
||||
try { target[propertyKey] = Java.type(className); return } catch (error) { }
|
||||
try { target[propertyKey] = base.getClass(className).static; return } catch (error) { }
|
||||
console.warn('JSClass Inject target', target, 'propertyKey', propertyKey, 'failed!')
|
||||
console.warn('JSClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,22 @@ export const JSClass = (className: string) => {
|
||||
* 自动注入实例由平台实现
|
||||
* @param className 类名
|
||||
*/
|
||||
export const Autowired = (className?: string | any) => {
|
||||
return function (target: any, propertyKey: string) {
|
||||
target[propertyKey] = getContainer().getNamed(ioc.Autowired, className || propertyKey)
|
||||
export const Autowired = (className?: any) => {
|
||||
return function (target: any, propertyKey: string, index?: number) {
|
||||
let container = getContainer()
|
||||
if (className && (className instanceof Symbol || className instanceof Function)) {
|
||||
return inject(className)(target, propertyKey, index)
|
||||
}
|
||||
let type = Reflect.getMetadata('design:type', target, propertyKey)
|
||||
if (type && type !== Object && !Java.isJavaObject(type)) {
|
||||
inject(type)(target, propertyKey, index)
|
||||
named(className || propertyKey)(target, propertyKey, index)
|
||||
} else if (container.isBound(ioc.Autowired)) {
|
||||
console.debug('Autowired', 'ioc.Autowired', 'named', className || propertyKey)
|
||||
target[propertyKey] = container.getNamed(ioc.Autowired, className || propertyKey)
|
||||
} else {
|
||||
throw new Error(`No matching bindings found for target: ${target.constructor.name} type: ${type} named: ${className || propertyKey}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +83,7 @@ export const Autowired = (className?: string | any) => {
|
||||
* @param className 类名
|
||||
*/
|
||||
export const Resource = (resourceName?: string | any) => {
|
||||
return function (target: any, propertyKey: string) {
|
||||
return function (target: any, propertyKey: string, index?: number) {
|
||||
target[propertyKey] = getContainer().getNamed(ioc.Resource, resourceName || propertyKey)
|
||||
}
|
||||
}
|
||||
@@ -82,8 +95,18 @@ export const reduceMetadata = (ctx: interfaces.Context): any => {
|
||||
}, {})
|
||||
}
|
||||
|
||||
function initAutowired(container: Container) {
|
||||
container.bind(ioc.Autowired).toDynamicValue((ctx) => {
|
||||
var metadata: any = reduceMetadata(ctx)
|
||||
let key = Object.toString.call(metadata.named)
|
||||
if (key === "[object Function]" || key === "[object Symbol]") { return container.get(metadata.named) }
|
||||
return undefined
|
||||
})
|
||||
}
|
||||
|
||||
export const DefaultContainer = new Container()
|
||||
initContainer(DefaultContainer)
|
||||
initAutowired(DefaultContainer)
|
||||
|
||||
export * from 'inversify'
|
||||
export * from './constants'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/core",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -21,11 +21,11 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
},
|
||||
"gitHead": "781524f83e52cad26d7c480513e3c525df867121"
|
||||
}
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
let containerStartTime = Date.now()
|
||||
console.i18n("ms.core.ioc.initialize", { scope: global.scope })
|
||||
import { plugin, server, task, constants } from '@ccms/api'
|
||||
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container'
|
||||
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule, Autowired } from '@ccms/container'
|
||||
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
|
||||
import http from '@ccms/common/dist/http'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
@provideSingleton(MiaoScriptCore)
|
||||
class MiaoScriptCore {
|
||||
@inject(server.Console)
|
||||
@Autowired(server.Console)
|
||||
private Console: Console
|
||||
@inject(task.TaskManager)
|
||||
private taskManager: task.TaskManager
|
||||
@inject(plugin.PluginFolder)
|
||||
@Autowired(plugin.PluginFolder)
|
||||
private pluginFolder: string
|
||||
@inject(plugin.PluginManager)
|
||||
@Autowired()
|
||||
private taskManager: task.TaskManager
|
||||
@Autowired()
|
||||
private pluginManager: plugin.PluginManager
|
||||
|
||||
enable() {
|
||||
this.loadServerConsole()
|
||||
this.loadTaskFunction()
|
||||
global.level = "TRACE"
|
||||
this.loadPlugins()
|
||||
return () => this.disable()
|
||||
}
|
||||
@@ -30,15 +27,6 @@ class MiaoScriptCore {
|
||||
global.setGlobal('console', new this.Console(), { writable: false, configurable: false })
|
||||
}
|
||||
|
||||
loadTaskFunction() {
|
||||
global.setGlobal('setTimeout', (func: Function, tick: number, ...args: any[]) => {
|
||||
return this.taskManager.create(func).later(tick).submit(...args)
|
||||
}, { writable: false, configurable: false })
|
||||
global.setGlobal('setInterval', (func: Function, tick: number, ...args: any[]) => {
|
||||
return this.taskManager.create(func).timer(tick).submit(...args)
|
||||
}, { writable: false, configurable: false })
|
||||
}
|
||||
|
||||
loadPlugins() {
|
||||
let loadPluginStartTime = new Date().getTime()
|
||||
console.i18n("ms.core.plugin.initialize")
|
||||
@@ -50,11 +38,12 @@ class MiaoScriptCore {
|
||||
}
|
||||
|
||||
disable() {
|
||||
let disableStartTime = Date.now()
|
||||
console.i18n("ms.core.engine.disable")
|
||||
this.pluginManager.disable(this.pluginManager.getPlugins())
|
||||
this.taskManager.disable()
|
||||
process.emit('exit', 0)
|
||||
process.exit(0)
|
||||
console.i18n("ms.core.engine.disable.finish", { version: 'v' + global.ScriptEngineVersion, time: (new Date().getTime() - disableStartTime) / 1000 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +77,7 @@ function detectServer(): constants.ServerType {
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
// @ts-ignore
|
||||
global.ScriptEngineVersion = require('../package.json').version
|
||||
try { engineLoad({ script: http.get("http://ms.yumc.pw/api/plugin/download/name/initialize"), name: 'core/initialize.js' }) } catch (error) { console.debug(error) }
|
||||
try {
|
||||
let corePackageStartTime = new Date().getTime()
|
||||
@@ -98,13 +87,15 @@ function initialize() {
|
||||
let type = detectServer()
|
||||
console.i18n("ms.core.initialize.detect", { scope: global.scope, type })
|
||||
container.bind(server.ServerType).toConstantValue(type)
|
||||
container.bind(server.ServerChecker).toSelf().inSingletonScope()
|
||||
container.bind(server.NativePluginManager).toSelf().inSingletonScope()
|
||||
console.i18n("ms.core.package.initialize", { scope: global.scope, type })
|
||||
require(`${global.scope}/${type}`).default(container)
|
||||
require(`${global.scope}/plugin`)
|
||||
container.load(buildProviderModule())
|
||||
console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 })
|
||||
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable()
|
||||
console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 })
|
||||
console.i18n("ms.core.engine.completed", { version: 'v' + global.ScriptEngineVersion, time: (Date.now() - global.ScriptEngineStartTime) / 1000 })
|
||||
return disable
|
||||
} catch (error) {
|
||||
console.i18n("ms.core.initialize.error", { error })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/database",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript database package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,12 +19,13 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@javatypes/spring-jdbc": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
/// <reference types="@ccms/types/dist/typings/spring" />
|
||||
|
||||
import { Model } from './model'
|
||||
|
||||
const HikariDataSource = Java.type('com.zaxxer.hikari.HikariDataSource')
|
||||
@@ -69,7 +67,7 @@ export class DataBase {
|
||||
*/
|
||||
query<T>(sql: string, ...args: any[]): Array<T> {
|
||||
let startTime = Date.now()
|
||||
let result = Java.from(this.jdbcTemplate.queryForList(sql, args))
|
||||
let result = Java.from<any>(this.jdbcTemplate.queryForList(sql, args))
|
||||
console.debug(java.lang.String.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@ccms/types/dist/typings/jdk/index" />
|
||||
/// <reference types="@javatypes/jdk" />
|
||||
/// <reference types="@javatypes/spring-jdbc" />
|
||||
|
||||
export * from './database'
|
||||
export * from './manager'
|
||||
@@ -10,8 +10,9 @@ ms.core.package.initialize: "初始化 MiaoScript 扩展 {scope}/core {scope}/{t
|
||||
ms.core.package.completed: "MiaoScript 扩展 {scope}/core {scope}/{type} {scope}/plugin 加载完成 耗时({time}s)"
|
||||
ms.core.plugin.initialize: "MiaoScript 开始引导插件系统. 请稍候..."
|
||||
ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!"
|
||||
ms.core.engine.completed: "MiaoScript 脚本引擎 加载完毕... 耗时({time}s)!"
|
||||
ms.core.engine.disable: "关闭 MiaoScript 引擎..."
|
||||
ms.core.engine.completed: "MiaoScript 脚本引擎 {version} 加载完毕... 耗时({time}s)!"
|
||||
ms.core.engine.disable: "MiaoScript 关闭脚本引擎 请稍候..."
|
||||
ms.core.engine.disable.finish: "MiaoScript 脚本引擎 {version} 关闭完成... 耗时({time}s)!"
|
||||
ms.core.engine.disable.abnormal: "引擎异常启动或初始化未完成 跳过关闭流程..."
|
||||
|
||||
ms.api.event.resource.not.found: "无法映射事件 未找到资源文件 {resource}!"
|
||||
@@ -21,8 +22,8 @@ ms.api.event.not.found: "§6插件 §b{name} §6注册事件 §c{event} §6失
|
||||
ms.api.event.execute.slow: "§c注意! §6插件 §b{name} §6处理 §d{event} §6事件 §c耗时 §4{cost}ms !"
|
||||
ms.api.event.execute.error: "§6插件 §b{name} §6处理 §d{event} §6事件时发生异常 §4{ex}"
|
||||
ms.api.event.listen.plugin.name.empty: "插件名称为空 请检查传入参数!"
|
||||
ms.api.event.register: "[{name}] 注册事件 {event}"
|
||||
ms.api.event.unregister: "[{name}] 注销事件 {event}"
|
||||
ms.api.event.register: "[{name}] 注册事件 {event} => 执行器 {exec}"
|
||||
ms.api.event.unregister: "[{name}] 注销事件 {event} => 执行器 {exec}"
|
||||
ms.api.command.register.input.error: "CommandExec 必须为一个函数... 输入: {exec}"
|
||||
ms.api.command.register: "[{plugin}] 注册命令 {name}({cmd})..."
|
||||
ms.api.command.unregister: "[{plugin}] 注销命令 {name}..."
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/i18n",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript i18n package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,14 +19,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-yaml": "^3.12.3",
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"@types/js-yaml": "^3.12.5",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/nashorn": "^0.7.0",
|
||||
"js-yaml": "^3.13.1"
|
||||
"js-yaml": "^3.14.0"
|
||||
},
|
||||
"gitHead": "781524f83e52cad26d7c480513e3c525df867121"
|
||||
}
|
||||
|
||||
1
packages/keyvalue/.npmignore
Symbolic link
1
packages/keyvalue/.npmignore
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.npmignore
|
||||
36
packages/keyvalue/package.json
Normal file
36
packages/keyvalue/package.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "@ccms/keyvalue",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript keyvalue package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
"minecraft",
|
||||
"bukkit",
|
||||
"sponge"
|
||||
],
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/circlecloud/ms.git",
|
||||
"license": "ISC",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"clean": "rimraf dist",
|
||||
"watch": "tsc --watch",
|
||||
"build": "yarn clean && tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"@javatypes/amqp-client": "^0.0.3",
|
||||
"@javatypes/spring-amqp": "^0.0.3",
|
||||
"@javatypes/spring-rabbit": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"gitHead": "2589633069d24f646ac09261b1b2304c21d4ea75"
|
||||
}
|
||||
3
packages/keyvalue/src/index.ts
Normal file
3
packages/keyvalue/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@javatypes/jdk" />
|
||||
export { }
|
||||
7
packages/keyvalue/tsconfig.json
Normal file
7
packages/keyvalue/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nashorn",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,6 +22,6 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,11 @@ declare global {
|
||||
|
||||
namespace Java {
|
||||
function type<T = any>(clazz: string): T
|
||||
function from<T>(javaObj: T[]): T[]
|
||||
function to<T>(array: T[], type?: T): T[]
|
||||
function from<T = any>(javaObj: T[]): T[]
|
||||
function to<T = any>(array: T[], type?: T): T[]
|
||||
function extend(...parentTypes: any[]): any
|
||||
function synchronized(func: () => void, lock: any): Function
|
||||
function isJavaObject(obj: any): boolean
|
||||
function asJSONCompatible<T = any>(obj: T): T
|
||||
//@ts-ignore
|
||||
// function super(type: any);
|
||||
@@ -48,7 +49,8 @@ declare global {
|
||||
logger: any
|
||||
debug: boolean
|
||||
level: string
|
||||
NashornEngineStartTime: number
|
||||
ScriptEngineVersion: string
|
||||
ScriptEngineStartTime: number
|
||||
setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType<any>) => void
|
||||
noop: () => void
|
||||
console: Console
|
||||
@@ -65,6 +67,7 @@ declare global {
|
||||
interface Core {
|
||||
getClass(name: String): any
|
||||
getProxyClass(): any
|
||||
getJavaScriptTaskClass(): any
|
||||
getInstance(): any
|
||||
read(path: string): string
|
||||
save(path: string, content: string): void
|
||||
@@ -73,13 +76,24 @@ declare global {
|
||||
interface Console {
|
||||
ex(err: Error): void
|
||||
stack(err: Error, color?: boolean): string[]
|
||||
sender(sender: any, ...args: any): void
|
||||
sender(...args: any): void
|
||||
console(...args: any): void
|
||||
i18n(name: string, ...params: any[]): void
|
||||
}
|
||||
interface ProxyConstructor {
|
||||
newProxy<T extends object>(target: T, handler: ProxyHandler<T>): T
|
||||
newProxy<T extends object>(target: T, handler: ProxyHandle<T>): T
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProxyHandle<T = any> extends ProxyHandler<any> {
|
||||
setPrototypeOf?(target: T, v: any): any
|
||||
// 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: T) => any[]
|
||||
// call: (target: any, name: string, ...args: any[]) => any
|
||||
// deleteProperty: (target: any, name: string) => boolean
|
||||
}
|
||||
export { }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nodejs",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript nodejs package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,12 +19,10 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/nashorn": "^0.7.0"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"gitHead": "781524f83e52cad26d7c480513e3c525df867121"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nukkit",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript nukkit package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,13 +19,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@javatypes/nukkit-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
import { MiaoScriptConsole } from '@ccms/api'
|
||||
|
||||
let Nukkit = base.getInstance().getServer();
|
||||
let Nukkit = base.getInstance().getServer()
|
||||
let LogLevel = Java.type('cn.nukkit.utils.LogLevel')
|
||||
let CommandSender = Java.type('cn.nukkit.command.CommandSender')
|
||||
|
||||
export class NukkitConsole extends MiaoScriptConsole {
|
||||
sender(sender, ...args) {
|
||||
if (!(sender instanceof CommandSender)) {
|
||||
this.error(`First parameter ${sender} not instanceof cn.nukkit.command.CommandSender can't send message!`)
|
||||
return;
|
||||
return
|
||||
}
|
||||
if (Object.prototype.toString.call(args[0]) === "[object Array]") {
|
||||
args[0].forEach(line => sender.sendMessage(this.prefix + line))
|
||||
} else {
|
||||
sender.sendMessage(this.prefix + args.join(' '));
|
||||
sender.sendMessage(this.prefix + args.join(' '))
|
||||
}
|
||||
}
|
||||
console(...args: string[]): void {
|
||||
this.sender(Nukkit.getConsoleSender(), args.join(' '));
|
||||
this.sender(Nukkit.getConsoleSender(), args.join(' '))
|
||||
}
|
||||
error(...args: any[]) {
|
||||
this.logger.log(LogLevel.ERROR, this.name + args.join(' '))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/// <reference types="@ccms/types/dist/typings/nukkit" />
|
||||
/// <reference types="@javatypes/nukkit-api" />
|
||||
|
||||
import { server } from '@ccms/api'
|
||||
import { Container } from '@ccms/container'
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
import { server } from '@ccms/api'
|
||||
import { provideSingleton } from '@ccms/container';
|
||||
import { provideSingleton } from '@ccms/container'
|
||||
|
||||
let Nukkit: cn.nukkit.Server = base.getInstance().getServer();
|
||||
const File = Java.type("java.io.File");
|
||||
let Nukkit: cn.nukkit.Server = base.getInstance().getServer()
|
||||
const File = Java.type("java.io.File")
|
||||
|
||||
@provideSingleton(server.Server)
|
||||
export class NukkitServer implements server.Server {
|
||||
private pluginsFolder: string;
|
||||
private pluginsFolder: string
|
||||
|
||||
constructor() {
|
||||
this.pluginsFolder = new File(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getCanonicalPath()
|
||||
}
|
||||
|
||||
getPlayer(name: string) {
|
||||
return Nukkit.getPlayer(name)
|
||||
return Nukkit.getPlayerExact(name)
|
||||
}
|
||||
getVersion(): string {
|
||||
return Nukkit.getVersion()
|
||||
}
|
||||
getOnlinePlayers() {
|
||||
return Nukkit.getOnlinePlayers() as unknown as any[]
|
||||
return Nukkit.getOnlinePlayers().values()
|
||||
}
|
||||
getConsoleSender() {
|
||||
return Nukkit.getConsoleSender()
|
||||
@@ -37,18 +37,15 @@ export class NukkitServer implements server.Server {
|
||||
return Nukkit.dispatchCommand(Nukkit.getConsoleSender(), command)
|
||||
}
|
||||
getPluginsFolder(): string {
|
||||
return this.pluginsFolder;
|
||||
return this.pluginsFolder
|
||||
}
|
||||
getNativePluginManager() {
|
||||
return Nukkit.getPluginManager() as any;
|
||||
return Nukkit.getPluginManager() as any
|
||||
}
|
||||
getNettyPipeline() {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
getRootLogger() {
|
||||
return Packages.org.apache.logging.log4j.LogManager.getRootLogger()
|
||||
}
|
||||
sendJson(sender: string | any, json: object | string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,29 @@
|
||||
import { task, plugin } from '@ccms/api'
|
||||
import { inject, provideSingleton } from '@ccms/container';
|
||||
import { inject, provideSingleton } from '@ccms/container'
|
||||
|
||||
const NukkitRunnable = Java.type('cn.nukkit.scheduler.NukkitRunnable');
|
||||
const NukkitRunnable = Java.type('cn.nukkit.scheduler.NukkitRunnable')
|
||||
|
||||
@provideSingleton(task.TaskManager)
|
||||
export class NukkitTaskManager implements task.TaskManager {
|
||||
@inject(plugin.PluginInstance)
|
||||
private pluginInstance: any;
|
||||
|
||||
create(func: Function): task.Task {
|
||||
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
|
||||
return new NukkitTask(this.pluginInstance, func);
|
||||
export class NukkitTaskManager extends task.TaskManager {
|
||||
create0(owner: plugin.Plugin, func: Function, id: number): task.Task {
|
||||
return new NukkitTask(owner, func, id)
|
||||
}
|
||||
callSyncMethod(func: Function): any {
|
||||
return func()
|
||||
}
|
||||
disable() {
|
||||
base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
|
||||
disable0() {
|
||||
base.getInstance().getServer().getScheduler().cancelTask(base.getInstance())
|
||||
}
|
||||
}
|
||||
|
||||
export class NukkitTask extends task.Task {
|
||||
submit(...args: any[]): task.Cancelable {
|
||||
submit0(...args: any[]): task.Cancelable {
|
||||
let run = new NukkitRunnable({ run: () => this.run(...args) })
|
||||
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
||||
if (this.interval) {
|
||||
run[funcName](this.plugin, this.laterTime, this.interval);
|
||||
return run[funcName](base.getInstance(), this.laterTime, this.interval)
|
||||
} else {
|
||||
run[funcName](this.plugin, this.laterTime);
|
||||
return run[funcName](base.getInstance(), this.laterTime)
|
||||
}
|
||||
return run;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/ployfill",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript ployfill package",
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/circlecloud/ms.git",
|
||||
@@ -14,13 +14,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/i18n": "^0.7.0",
|
||||
"@ccms/nashorn": "^0.7.0",
|
||||
"core-js": "^3.6.5"
|
||||
"@ccms/i18n": "^0.11.0",
|
||||
"@ccms/nodejs": "^0.11.0",
|
||||
"core-js": "^3.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.11.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
|
||||
import '@ccms/nodejs'
|
||||
import i18n from '@ccms/i18n'
|
||||
let ployfillStartTime = new Date().getTime()
|
||||
i18n.initialize()
|
||||
|
||||
@@ -5,16 +5,18 @@ const ThreadGroup = Java.type("java.lang.ThreadGroup")
|
||||
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
|
||||
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
|
||||
const LinkedBlockingQueue = Java.type("java.util.concurrent.LinkedBlockingQueue")
|
||||
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')
|
||||
const DelayQueue = Java.type('java.util.concurrent.DelayQueue')
|
||||
const JavaScriptTask = Java.type(base.getJavaScriptTaskClass().name)
|
||||
|
||||
const threadCount = new AtomicInteger(0)
|
||||
const threadGroup = new ThreadGroup("@ccms/ployfill-micro-task")
|
||||
const microTaskPool = new ThreadPoolExecutor(
|
||||
10, 100, 60, Packages.java.util.concurrent.TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue(500),
|
||||
100, 200, 60, Packages.java.util.concurrent.TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue(300),
|
||||
(run: any) => new Thread(threadGroup, run, "@ccms/micro-task-" + threadCount.incrementAndGet()),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy()
|
||||
)
|
||||
|
||||
class Process extends EventEmitter {
|
||||
env = {
|
||||
__noSuchProperty__: (prop) => {
|
||||
@@ -22,15 +24,121 @@ class Process extends EventEmitter {
|
||||
}
|
||||
}
|
||||
platform = Packages.java.lang.System.getProperty("os.name")
|
||||
on(event: string | symbol, listener: (...args: any[]) => void) {
|
||||
return super.on(event, (...args) => {
|
||||
try {
|
||||
listener(...args)
|
||||
} catch (error) {
|
||||
try {
|
||||
super.emit('error', error)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
nextTick(func: Function) {
|
||||
microTaskPool.execute(func)
|
||||
}
|
||||
queueMicrotask(func: Function) {
|
||||
microTaskPool.execute(func)
|
||||
exit(code: number) {
|
||||
process.emit('exit', code)
|
||||
microTaskPool.shutdown()
|
||||
console.log('process exit await microTaskPool termination...')
|
||||
microTaskPool.awaitTermination(5000, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
exit() {
|
||||
microTaskPool.shutdown();
|
||||
}
|
||||
|
||||
class EventLoop {
|
||||
private eventLoopMainThread = undefined
|
||||
private eventLoopTaskQueue = new DelayQueue()
|
||||
|
||||
constructor() {
|
||||
this.eventLoopMainThread = new Thread(() => {
|
||||
let task = undefined
|
||||
try {
|
||||
while ((task = this.eventLoopTaskQueue.take()) != undefined) {
|
||||
try {
|
||||
task.getTask()()
|
||||
} catch (error) {
|
||||
try {
|
||||
process.emit('error', error)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`EventLoop Thread isInterrupted exit! remainTask: ${this.eventLoopTaskQueue.size()}`)
|
||||
this.eventLoopTaskQueue.clear()
|
||||
this.eventLoopTaskQueue = undefined
|
||||
this.timeoutCount = undefined
|
||||
this.timeoutTasks = undefined
|
||||
this.intervalCount = undefined
|
||||
this.intervalTasks = undefined
|
||||
this.eventLoopMainThread = undefined
|
||||
}
|
||||
}, "@ccms/node-shim/event-loop")
|
||||
this.eventLoopMainThread.setDaemon(true)
|
||||
process.on('exit', () => this.eventLoopMainThread.interrupt())
|
||||
}
|
||||
|
||||
startEventLoop() {
|
||||
this.eventLoopMainThread.start()
|
||||
}
|
||||
|
||||
private putDelayTask(callback: Function, ms: number) {
|
||||
this.eventLoopTaskQueue.put(new JavaScriptTask(callback, ms))
|
||||
}
|
||||
|
||||
private timeoutCount = new AtomicInteger(0)
|
||||
private timeoutTasks = []
|
||||
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]) {
|
||||
let taskId = this.timeoutCount.incrementAndGet()
|
||||
this.timeoutTasks[taskId] = callback
|
||||
console.trace(`create setTimeout task ${taskId} => ${callback}`)
|
||||
this.putDelayTask(() => {
|
||||
if (this.timeoutTasks[taskId]) {
|
||||
callback(...args)
|
||||
} else {
|
||||
console.trace(`ignore setTimeout ${ms}ms task ${taskId} because it's cancelled!`)
|
||||
}
|
||||
}, ms)
|
||||
return taskId
|
||||
}
|
||||
clearTimeout(taskId: number) {
|
||||
delete this.timeoutTasks[taskId]
|
||||
}
|
||||
|
||||
private intervalCount = new AtomicInteger(0)
|
||||
private intervalTasks = []
|
||||
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]) {
|
||||
let taskId = this.intervalCount.incrementAndGet()
|
||||
this.intervalTasks[taskId] = callback
|
||||
console.trace(`create setInterval ${ms}ms task ${taskId} => ${callback}`)
|
||||
let intervalTask = () => {
|
||||
if (this.intervalTasks[taskId]) {
|
||||
callback(...args)
|
||||
this.putDelayTask(intervalTask, ms)
|
||||
} else {
|
||||
console.trace(`ignore setInterval task ${taskId} because it's cancelled!`)
|
||||
}
|
||||
}
|
||||
this.putDelayTask(intervalTask, ms)
|
||||
return taskId
|
||||
}
|
||||
clearInterval(taskId: number) {
|
||||
delete this.intervalTasks[taskId]
|
||||
}
|
||||
}
|
||||
global.setGlobal('process', new Process(), {})
|
||||
const eventLoop = new EventLoop()
|
||||
global.setGlobal('eventLoop', eventLoop, {})
|
||||
eventLoop.startEventLoop()
|
||||
global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {})
|
||||
global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {})
|
||||
global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {})
|
||||
global.setGlobal('setImmediate', (callback: (...args: any[]) => void, ...args: any[]) => eventLoop.setTimeout(callback, 0, ...args), {})
|
||||
global.setGlobal('clearImmediate ', eventLoop.clearTimeout.bind(eventLoop), {})
|
||||
global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})
|
||||
global.setGlobal('clearInterval', eventLoop.clearInterval.bind(eventLoop), {})
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import { ProxyHandle } from '@ccms/nashorn'
|
||||
|
||||
// Nashorn JSAdapter See https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-JSAdapterconstructor
|
||||
let createProxy = eval(`
|
||||
function(handle){ return new JSAdapter(handle) }
|
||||
`)
|
||||
export interface ProxyHandle extends ProxyHandler<any> {
|
||||
// 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
|
||||
// deleteProperty: (target: any, name: string) => boolean
|
||||
}
|
||||
export class Proxy {
|
||||
static newProxy(target: any, handle: Partial<ProxyHandle>): any {
|
||||
return new Proxy(target, handle)
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
import '@ccms/nashorn'
|
||||
|
||||
const URL = Java.type("java.net.URL");
|
||||
const Files = Java.type("java.nio.file.Files");
|
||||
const StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
|
||||
const JavaString = Java.type("java.lang.String");
|
||||
const SecureRandom = Java.type("java.security.SecureRandom");
|
||||
const SSLContext = Java.type("javax.net.ssl.SSLContext");
|
||||
const HttpsURLConnection = Java.type("javax.net.ssl.HttpsURLConnection");
|
||||
const HostnameVerifier = Java.type("javax.net.ssl.HostnameVerifier");
|
||||
const X509TrustManager = Java.type("javax.net.ssl.X509TrustManager");
|
||||
const URL = Java.type("java.net.URL")
|
||||
const Files = Java.type("java.nio.file.Files")
|
||||
const StandardCopyOption = Java.type("java.nio.file.StandardCopyOption")
|
||||
const JavaString = Java.type("java.lang.String")
|
||||
const SecureRandom = Java.type("java.security.SecureRandom")
|
||||
const SSLContext = Java.type("javax.net.ssl.SSLContext")
|
||||
const HttpsURLConnection = Java.type("javax.net.ssl.HttpsURLConnection")
|
||||
const HostnameVerifier = Java.type("javax.net.ssl.HostnameVerifier")
|
||||
const X509TrustManager = Java.type("javax.net.ssl.X509TrustManager")
|
||||
|
||||
const SocketTimeoutException = Java.type('java.net.SocketTimeoutException');
|
||||
const SocketTimeoutException = Java.type('java.net.SocketTimeoutException')
|
||||
|
||||
const Callable = Java.type('java.util.concurrent.Callable');
|
||||
const Callable = Java.type('java.util.concurrent.Callable')
|
||||
const Executors = Java.type('java.util.concurrent.Executors')
|
||||
|
||||
const UTF_8 = "UTF-8"
|
||||
|
||||
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true });
|
||||
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
|
||||
|
||||
const SSLSocketFactory = function initSSLSocketFactory() {
|
||||
let sslContext = SSLContext.getInstance("TLS");
|
||||
let sslContext = SSLContext.getInstance("TLS")
|
||||
sslContext.init(null, [new X509TrustManager({
|
||||
getAcceptedIssuers: () => null,
|
||||
checkClientTrusted: () => { },
|
||||
checkServerTrusted: () => { }
|
||||
})], new SecureRandom());
|
||||
return sslContext.getSocketFactory();
|
||||
}();
|
||||
})], new SecureRandom())
|
||||
return sslContext.getSocketFactory()
|
||||
}()
|
||||
|
||||
interface Future<T> {
|
||||
cancel(): boolean;
|
||||
isCancelled(): boolean;
|
||||
isDone(): boolean;
|
||||
get(): T;
|
||||
cancel(): boolean
|
||||
isCancelled(): boolean
|
||||
isDone(): boolean
|
||||
get(): T
|
||||
}
|
||||
|
||||
enum ReadyState {
|
||||
@@ -50,14 +50,14 @@ type RequestMethod =
|
||||
| 'options' | 'OPTIONS'
|
||||
| 'post' | 'POST'
|
||||
| 'put' | 'PUT'
|
||||
| 'patch' | 'PATCH';
|
||||
| 'patch' | 'PATCH'
|
||||
type ResponseType =
|
||||
| 'arraybuffer'
|
||||
| 'blob'
|
||||
| 'document'
|
||||
| 'json'
|
||||
| 'text'
|
||||
| 'stream';
|
||||
| 'stream'
|
||||
type EventType =
|
||||
| 'load'
|
||||
| 'error'
|
||||
@@ -65,191 +65,191 @@ type EventType =
|
||||
| 'progress'
|
||||
| 'timeout'
|
||||
| 'loadend'
|
||||
| 'loadstart';
|
||||
type HttpHeader = { [key: string]: string };
|
||||
| 'loadstart'
|
||||
type HttpHeader = { [key: string]: string }
|
||||
|
||||
|
||||
const executor = Executors.newCachedThreadPool();
|
||||
const executor = Executors.newCachedThreadPool()
|
||||
|
||||
export class XMLHttpRequest {
|
||||
private _timeout: number;
|
||||
private _timeout: number
|
||||
private _responseType: ResponseType = 'text';
|
||||
private _withCredentials: boolean;
|
||||
private _withCredentials: boolean
|
||||
|
||||
private _readyState: ReadyState = ReadyState.UNSENT;
|
||||
|
||||
private _method: RequestMethod;
|
||||
private _url: string;
|
||||
private _async: boolean;
|
||||
private _mimeType: string;
|
||||
private _method: RequestMethod
|
||||
private _url: string
|
||||
private _async: boolean
|
||||
private _mimeType: string
|
||||
private _requestHeaders: HttpHeader = {};
|
||||
|
||||
private _status: number = 0;
|
||||
private _statusText: string = null;
|
||||
private _response: any;
|
||||
private _responseText: any;
|
||||
private _responseURL: string;
|
||||
private _response: any
|
||||
private _responseText: any
|
||||
private _responseURL: string
|
||||
private _responseHeaders: HttpHeader = {};
|
||||
|
||||
private _connection = null;
|
||||
|
||||
get timeout() {
|
||||
return this._timeout;
|
||||
return this._timeout
|
||||
}
|
||||
set timeout(timeout: number) {
|
||||
this._timeout = timeout
|
||||
}
|
||||
get readyState() {
|
||||
return this._readyState;
|
||||
return this._readyState
|
||||
}
|
||||
set responseType(type: ResponseType) {
|
||||
this._responseType = type;
|
||||
this._responseType = type
|
||||
}
|
||||
get responseType() {
|
||||
return this._responseType;
|
||||
return this._responseType
|
||||
}
|
||||
|
||||
get status() {
|
||||
return this._status;
|
||||
return this._status
|
||||
}
|
||||
get statusText() {
|
||||
return this._statusText;
|
||||
return this._statusText
|
||||
}
|
||||
get response() {
|
||||
return this._response || this.get();
|
||||
return this._response || this.get()
|
||||
}
|
||||
get responseText() {
|
||||
return this._responseText;
|
||||
return this._responseText
|
||||
}
|
||||
get responseXML() {
|
||||
return this._response;
|
||||
return this._response
|
||||
}
|
||||
get responseURL() {
|
||||
return this._responseURL;
|
||||
return this._responseURL
|
||||
}
|
||||
|
||||
public onload: () => void;
|
||||
public onerror: (ex: Error) => void;
|
||||
public onabort: () => void;
|
||||
public onprogress: () => void;
|
||||
public ontimeout: (ex: Error) => void;
|
||||
public onloadend: () => void;
|
||||
public onloadstart: () => void;
|
||||
public onreadystatechange: () => void;
|
||||
public onload: () => void
|
||||
public onerror: (ex: Error) => void
|
||||
public onabort: () => void
|
||||
public onprogress: () => void
|
||||
public ontimeout: (ex: Error) => void
|
||||
public onloadend: () => void
|
||||
public onloadstart: () => void
|
||||
public onreadystatechange: () => void
|
||||
|
||||
setRequestHeader(key: string, val: string) {
|
||||
this._requestHeaders[key] = val;
|
||||
this._requestHeaders[key] = val
|
||||
}
|
||||
getResponseHeader(key: string): string {
|
||||
return this._responseHeaders[key];
|
||||
return this._responseHeaders[key]
|
||||
}
|
||||
getAllResponseHeaders(): any {
|
||||
return this._responseHeaders;
|
||||
return this._responseHeaders
|
||||
}
|
||||
addEventListener(event: EventType, callback: Function) {
|
||||
this[`on${event.toLowerCase()}`] = callback;
|
||||
this[`on${event.toLowerCase()}`] = callback
|
||||
}
|
||||
overrideMimeType(mimeType: string) {
|
||||
this._mimeType = mimeType;
|
||||
this._mimeType = mimeType
|
||||
}
|
||||
open(method: RequestMethod, url: string, async: boolean = true, user?: string, password?: string) {
|
||||
if (this._readyState !== ReadyState.UNSENT) { throw new Error(`Error Status ${this._readyState}!`) }
|
||||
|
||||
this._method = method;
|
||||
this._url = url;
|
||||
this._async = async;
|
||||
this._method = method
|
||||
this._url = url
|
||||
this._async = async
|
||||
|
||||
this._connection = new URL(this._url).openConnection()
|
||||
if (this._connection instanceof HttpsURLConnection) {
|
||||
this._connection.setHostnameVerifier(TrustAnyHostnameVerifier);
|
||||
this._connection.setSSLSocketFactory(SSLSocketFactory);
|
||||
this._connection.setHostnameVerifier(TrustAnyHostnameVerifier)
|
||||
this._connection.setSSLSocketFactory(SSLSocketFactory)
|
||||
}
|
||||
this._connection.setRequestMethod(this._method);
|
||||
this._connection.setDoOutput(true);
|
||||
this._connection.setDoInput(true);
|
||||
this._connection.setConnectTimeout(this._timeout);
|
||||
this._connection.setReadTimeout(this._timeout);
|
||||
this._connection.setRequestMethod(this._method)
|
||||
this._connection.setDoOutput(true)
|
||||
this._connection.setDoInput(true)
|
||||
this._connection.setConnectTimeout(this._timeout)
|
||||
this._connection.setReadTimeout(this._timeout)
|
||||
|
||||
this.setReadyState(ReadyState.OPENED);
|
||||
this.setReadyState(ReadyState.OPENED)
|
||||
}
|
||||
send(body?: string | object): Future<string> {
|
||||
for (const header in this._requestHeaders) {
|
||||
this._connection.setRequestProperty(header, this._requestHeaders[header]);
|
||||
this._connection.setRequestProperty(header, this._requestHeaders[header])
|
||||
}
|
||||
if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) }
|
||||
let future = executor.submit(new Callable({ call: () => this._send(body) }));
|
||||
let future = executor.submit(new Callable({ call: () => this._send(body) }))
|
||||
if (!this._async) { future.get() }
|
||||
return future;
|
||||
return future
|
||||
}
|
||||
get() {
|
||||
if (this._response === undefined) {
|
||||
switch (this._responseType) {
|
||||
case "json":
|
||||
return this._response = JSON.parse(this._responseText);
|
||||
return this._response = JSON.parse(this._responseText)
|
||||
case "text":
|
||||
return this._response = this._responseText;
|
||||
return this._response = this._responseText
|
||||
default:
|
||||
throw Error(`Unsupport ResponseType: ${this._responseType} !`)
|
||||
}
|
||||
}
|
||||
return this._response;
|
||||
return this._response
|
||||
}
|
||||
abort() {
|
||||
this._connection.disconnect();
|
||||
this.onabort && this.onabort();
|
||||
this._connection.disconnect()
|
||||
this.onabort && this.onabort()
|
||||
}
|
||||
|
||||
private _send(body?: string | object) {
|
||||
try {
|
||||
this._connection.connect();
|
||||
this.onloadstart && this.onloadstart();
|
||||
this._connection.connect()
|
||||
this.onloadstart && this.onloadstart()
|
||||
if (body) {
|
||||
let bodyType = Object.prototype.toString.call(body);
|
||||
let bodyType = Object.prototype.toString.call(body)
|
||||
if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) }
|
||||
var out = this._connection.getOutputStream();
|
||||
out.write(new JavaString(body).getBytes(UTF_8));
|
||||
out.flush();
|
||||
out.close();
|
||||
var out = this._connection.getOutputStream()
|
||||
out.write(new JavaString(body).getBytes(UTF_8))
|
||||
out.flush()
|
||||
out.close()
|
||||
}
|
||||
this.setReadyState(ReadyState.LOADING);
|
||||
this._status = this._connection.getResponseCode();
|
||||
this._statusText = this._connection.getResponseMessage();
|
||||
this.setReadyState(ReadyState.LOADING)
|
||||
this._status = this._connection.getResponseCode()
|
||||
this._statusText = this._connection.getResponseMessage()
|
||||
if (this._status >= 0 && this._status < 300) {
|
||||
this._responseText = this.readOutput(this._connection.getInputStream());
|
||||
this._responseText = this.readOutput(this._connection.getInputStream())
|
||||
} else if (this._status >= 300 && this._status < 400) {
|
||||
this._responseURL = this.getResponseHeader('Location');
|
||||
this._responseURL = this.getResponseHeader('Location')
|
||||
} else {
|
||||
this._responseText = this.readOutput(this._connection.getErrorStream());
|
||||
this._responseText = this.readOutput(this._connection.getErrorStream())
|
||||
}
|
||||
this.setResponseHeaders(this._connection.getHeaderFields());
|
||||
this.onloadend && this.onloadend();
|
||||
this.setResponseHeaders(this._connection.getHeaderFields())
|
||||
this.onloadend && this.onloadend()
|
||||
} catch (ex) {
|
||||
if (ex instanceof SocketTimeoutException && this.ontimeout) {
|
||||
return this.ontimeout(ex)
|
||||
} else if (this.onerror) {
|
||||
return this.onerror(ex);
|
||||
return this.onerror(ex)
|
||||
}
|
||||
throw ex;
|
||||
throw ex
|
||||
} finally {
|
||||
this._connection.disconnect();
|
||||
this.setReadyState(ReadyState.DONE);
|
||||
this._connection.disconnect()
|
||||
this.setReadyState(ReadyState.DONE)
|
||||
}
|
||||
}
|
||||
|
||||
private setResponseHeaders(header: any) {
|
||||
header.forEach((key: string | number, value: string | any[]) => {
|
||||
this._responseHeaders[key] = value[value.length - 1]
|
||||
});
|
||||
this._responseHeaders[key + ''] = value[value.length - 1] + ''
|
||||
})
|
||||
}
|
||||
|
||||
private setReadyState(state: ReadyState) {
|
||||
this._readyState = state;
|
||||
this.onreadystatechange && this.onreadystatechange();
|
||||
this._readyState = state
|
||||
this.onreadystatechange && this.onreadystatechange()
|
||||
}
|
||||
|
||||
private readOutput(input: any) {
|
||||
var tempFile = Files.createTempFile('xhr', '.response');
|
||||
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit();
|
||||
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8');
|
||||
var tempFile = Files.createTempFile('xhr', '.response')
|
||||
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit()
|
||||
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/plugin",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,16 +19,16 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-yaml": "^3.12.3",
|
||||
"@types/js-yaml": "^3.12.5",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0",
|
||||
"@ccms/i18n": "^0.7.0",
|
||||
"js-yaml": "^3.13.1"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/common": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0",
|
||||
"@ccms/i18n": "^0.11.0",
|
||||
"js-yaml": "^3.14.0"
|
||||
}
|
||||
}
|
||||
|
||||
40
packages/plugin/src/command.ts
Normal file
40
packages/plugin/src/command.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { command, plugin, server } from '@ccms/api'
|
||||
import { provideSingleton, Autowired } from '@ccms/container'
|
||||
import { getPluginCommandMetadata, getPluginTabCompleterMetadata } from './utils'
|
||||
|
||||
@provideSingleton(PluginCommandManager)
|
||||
export class PluginCommandManager {
|
||||
@Autowired()
|
||||
private CommandManager: command.Command
|
||||
@Autowired()
|
||||
private ServerChecker: server.ServerChecker
|
||||
|
||||
constructor() {
|
||||
process.on('plugin.before.enable', this.registryCommand.bind(this))
|
||||
process.on('plugin.after.disable', this.unregistryCommand.bind(this))
|
||||
}
|
||||
|
||||
private registryCommand(pluginInstance: plugin.Plugin) {
|
||||
let cmds = getPluginCommandMetadata(pluginInstance)
|
||||
let tabs = getPluginTabCompleterMetadata(pluginInstance)
|
||||
for (const cmd of cmds) {
|
||||
if (!this.ServerChecker.check(cmd.servers)) {
|
||||
console.debug(`[${pluginInstance.description.name}] ${cmd.target.constructor.name} incompatible command ${cmd.name} server(${cmd.servers}) ignore.`)
|
||||
continue
|
||||
}
|
||||
for (let command of [cmd.name, ...cmd.alias]) {
|
||||
this.CommandManager.on(pluginInstance, command, {
|
||||
cmd: pluginInstance[cmd.executor].bind(pluginInstance),
|
||||
tab: tabs.has(command) ?
|
||||
pluginInstance[tabs.get(command).executor].bind(pluginInstance) :
|
||||
console.debug(`[${pluginInstance.description.name}] command ${cmd.name} is not registry tabCompleter`)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unregistryCommand(pluginInstance: plugin.Plugin) {
|
||||
let cmds = getPluginCommandMetadata(pluginInstance)
|
||||
cmds.forEach(cmd => this.CommandManager.off(pluginInstance, cmd.name))
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,116 @@
|
||||
import * as yaml from 'js-yaml'
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import { plugin } from '@ccms/api'
|
||||
import { provideSingleton } from '@ccms/container'
|
||||
|
||||
import { interfaces } from './interfaces'
|
||||
import { getPluginConfigMetadata } from './utils'
|
||||
|
||||
export interface PluginConfigLoader {
|
||||
load(content: string): any;
|
||||
dump(variable: any): string;
|
||||
load(content: string): any
|
||||
dump(variable: any): string
|
||||
}
|
||||
|
||||
export class YamlPluginConfig implements PluginConfigLoader {
|
||||
load(content: string) {
|
||||
return yaml.safeLoad(content);
|
||||
return yaml.safeLoad(content)
|
||||
}
|
||||
dump(variable: any): string {
|
||||
return yaml.safeDump(variable, { skipInvalid: true });
|
||||
return yaml.safeDump(variable, { skipInvalid: true, lineWidth: 120 })
|
||||
}
|
||||
}
|
||||
|
||||
export class JsonPluginConfig implements PluginConfigLoader {
|
||||
load(content: string) {
|
||||
return JSON.parse(content);
|
||||
return JSON.parse(content)
|
||||
}
|
||||
dump(variable: any): string {
|
||||
return JSON.stringify(variable);
|
||||
return JSON.stringify(variable, undefined, 4)
|
||||
}
|
||||
}
|
||||
|
||||
const configLoaderMap = new Map<string, PluginConfigLoader>();
|
||||
|
||||
export function getConfigLoader(format: string) {
|
||||
if (!configLoaderMap.has(format)) { throw new Error(`Unsupport config format ${format} !`) }
|
||||
return configLoaderMap.get(format);
|
||||
export interface PluginConfig {
|
||||
/**
|
||||
* Save Config to File
|
||||
*/
|
||||
readonly save?: () => void
|
||||
/**
|
||||
* Reload Config from File
|
||||
*/
|
||||
readonly reload?: () => void
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
function init() {
|
||||
configLoaderMap.set("json", new JsonPluginConfig())
|
||||
let yaml = new YamlPluginConfig()
|
||||
configLoaderMap.set("yml", yaml)
|
||||
configLoaderMap.set("yaml", yaml)
|
||||
}
|
||||
@provideSingleton(PluginConfigManager)
|
||||
export class PluginConfigManager {
|
||||
private configLoaderMap = new Map<string, PluginConfigLoader>()
|
||||
|
||||
init()
|
||||
constructor() {
|
||||
this.configLoaderMap.set("json", new JsonPluginConfig())
|
||||
let yaml = new YamlPluginConfig()
|
||||
this.configLoaderMap.set("yml", yaml)
|
||||
this.configLoaderMap.set("yaml", yaml)
|
||||
process.on('plugin.before.load', this.loadConfig.bind(this))
|
||||
process.on('plugin.after.disable', this.saveConfig.bind(this))
|
||||
}
|
||||
|
||||
getConfigLoader(format: string) {
|
||||
if (!this.configLoaderMap.has(format)) { throw new Error(`Unsupport config format ${format} !`) }
|
||||
return this.configLoaderMap.get(format)
|
||||
}
|
||||
|
||||
loadConfig(plugin: plugin.Plugin) {
|
||||
let configs = getPluginConfigMetadata(plugin)
|
||||
for (let [_, config] of configs) {
|
||||
this.loadConfig0(plugin, config)
|
||||
}
|
||||
}
|
||||
|
||||
saveConfig(plugin: plugin.Plugin) {
|
||||
let configs = getPluginConfigMetadata(plugin)
|
||||
for (let [_, config] of configs) {
|
||||
config.autosave && this.saveConfig0(plugin, config)
|
||||
}
|
||||
}
|
||||
|
||||
private defienConfigProp(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata, value: any) {
|
||||
Object.defineProperties(value, {
|
||||
'save': { value: () => this.saveConfig0(plugin, metadata) },
|
||||
'reload': { value: () => this.loadConfig0(plugin, metadata) }
|
||||
})
|
||||
Object.defineProperty(plugin, metadata.variable, { value })
|
||||
}
|
||||
|
||||
private loadConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) {
|
||||
try {
|
||||
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
|
||||
let configLoader = this.getConfigLoader(metadata.format)
|
||||
let value = plugin[metadata.variable]
|
||||
if (!fs.exists(metadata.file)) {
|
||||
base.save(metadata.file, configLoader.dump(value))
|
||||
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: metadata.name, format: metadata.format })
|
||||
} else {
|
||||
value = configLoader.load(base.read(metadata.file))
|
||||
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>\n${JSON.stringify(value, undefined, 4)}`)
|
||||
}
|
||||
this.defienConfigProp(plugin, metadata, value)
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.config.load.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
|
||||
private saveConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) {
|
||||
try {
|
||||
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
|
||||
let result = this.getConfigLoader(metadata.format).dump(plugin[metadata.variable])
|
||||
base.save(metadata.file, result)
|
||||
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>\n${result}`)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.config.save.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
|
||||
console.ex(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export const METADATA_KEY = {
|
||||
plugin: Symbol.for("@ccms/plugin:plugin"),
|
||||
souece: Symbol.for("@ccms/plugin:souece"),
|
||||
source: Symbol.for("@ccms/plugin:source"),
|
||||
cmd: Symbol.for("@ccms/plugin:cmd"),
|
||||
tab: Symbol.for("@ccms/plugin:tab"),
|
||||
listener: Symbol.for("@ccms/plugin:listener"),
|
||||
@@ -10,4 +10,4 @@ export const METADATA_KEY = {
|
||||
enable: Symbol.for("@ccms/plugin:stage:enable"),
|
||||
disable: Symbol.for("@ccms/plugin:stage:disable")
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
|
||||
* MiaoScript plugin
|
||||
* @param metadata PluginMetadata
|
||||
*/
|
||||
export function plugin(metadata: pluginApi.PluginMetadata) {
|
||||
export function plugin(metadata: pluginApi.PluginMetadata | any) {
|
||||
return function (target: any) {
|
||||
metadata.target = target
|
||||
metadata.type = "ioc"
|
||||
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)
|
||||
const previousMetadata: Map<string, pluginApi.PluginMetadata> = getPluginMetadatas()
|
||||
@@ -19,7 +19,7 @@ export function plugin(metadata: pluginApi.PluginMetadata) {
|
||||
Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect)
|
||||
const previousSources: Map<string, pluginApi.PluginMetadata> = getPluginSources()
|
||||
previousSources.set(metadata.source.toString(), metadata)
|
||||
Reflect.defineMetadata(METADATA_KEY.souece, previousSources, Reflect)
|
||||
Reflect.defineMetadata(METADATA_KEY.source, previousSources, Reflect)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ export function plugin(metadata: pluginApi.PluginMetadata) {
|
||||
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
||||
return function (target: any, key: string, value: any) {
|
||||
metadata.name = metadata.name || key
|
||||
metadata.alias = metadata.alias || []
|
||||
metadata.target = target
|
||||
metadata.executor = key
|
||||
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
||||
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginCommandMetadata(target)
|
||||
previousMetadata.set(metadata.name, metadata)
|
||||
Reflect.defineMetadata(METADATA_KEY.cmd, previousMetadata, target.constructor)
|
||||
Reflect.defineMetadata(METADATA_KEY.cmd, [metadata, ...getPluginCommandMetadata(target)], target.constructor)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,10 +46,13 @@ export function tab(metadata: interfaces.CommandMetadata = {}) {
|
||||
return function (target: any, key: string, value: any) {
|
||||
metadata.name = metadata.name || (key.startsWith('tab') ? key.split('tab', 2)[1] : key)
|
||||
if (!metadata.name) { return }
|
||||
metadata.alias = metadata.alias || []
|
||||
metadata.target = target
|
||||
metadata.executor = key
|
||||
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
||||
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginTabCompleterMetadata(target)
|
||||
let previousMetadata = getPluginTabCompleterMetadata(target)
|
||||
previousMetadata.set(metadata.name, metadata)
|
||||
metadata.alias?.forEach((name) => previousMetadata.set(name, metadata))
|
||||
Reflect.defineMetadata(METADATA_KEY.tab, previousMetadata, target.constructor)
|
||||
}
|
||||
}
|
||||
@@ -61,9 +64,10 @@ export function tab(metadata: interfaces.CommandMetadata = {}) {
|
||||
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
||||
return function (target: any, key: string, value: any) {
|
||||
metadata.name = metadata.name || key
|
||||
metadata.target = target
|
||||
metadata.executor = key
|
||||
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
|
||||
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor)
|
||||
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...getPluginListenerMetadata(target)], target.constructor)
|
||||
Reflect.defineMetadata(METADATA_KEY.listener, metadata, target[key])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,29 +77,23 @@ export function config(metadata: interfaces.ConfigMetadata = {}) {
|
||||
metadata.variable = key
|
||||
metadata.version = metadata.version ?? 1
|
||||
metadata.format = metadata.format ?? 'yml'
|
||||
const previousMetadata: Map<string, interfaces.ConfigMetadata> = getPluginConfigMetadata(target)
|
||||
metadata.autosave = metadata.autosave ?? false
|
||||
metadata.filename = metadata.filename ?? metadata.name + '.' + metadata.format
|
||||
let previousMetadata = getPluginConfigMetadata(target)
|
||||
previousMetadata.set(metadata.name, metadata)
|
||||
Reflect.defineMetadata(METADATA_KEY.config, previousMetadata, target.constructor)
|
||||
}
|
||||
}
|
||||
|
||||
function stage(metadata: interfaces.ExecMetadata = {}, stage: string) {
|
||||
return function (target: any, key: string, value: any) {
|
||||
metadata.name = metadata.name || key
|
||||
metadata.executor = key
|
||||
const previousMetadata: interfaces.ExecMetadata[] = getPluginStageMetadata(target, stage)
|
||||
Reflect.defineMetadata(METADATA_KEY.stage[stage], [metadata, ...previousMetadata], target.constructor)
|
||||
function stage(stage: string) {
|
||||
return (metadata: interfaces.ExecMetadata = {}) => {
|
||||
return function (target: any, key: string, value: any) {
|
||||
metadata.name = metadata.name || key
|
||||
metadata.executor = key
|
||||
Reflect.defineMetadata(METADATA_KEY.stage[stage], [metadata, ...getPluginStageMetadata(target, stage)], target.constructor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function load(metadata: interfaces.ExecMetadata = {}) {
|
||||
return stage(metadata, 'load')
|
||||
}
|
||||
|
||||
export function enable(metadata: interfaces.ExecMetadata = {}) {
|
||||
return stage(metadata, 'enable')
|
||||
}
|
||||
|
||||
export function disable(metadata: interfaces.ExecMetadata = {}) {
|
||||
return stage(metadata, 'disable')
|
||||
}
|
||||
export const load = stage('load')
|
||||
export const enable = stage('enable')
|
||||
export const disable = stage('disable')
|
||||
|
||||
40
packages/plugin/src/event.ts
Normal file
40
packages/plugin/src/event.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { event, plugin, server } from '@ccms/api'
|
||||
import { provideSingleton, Autowired } from '@ccms/container'
|
||||
import { getPluginListenerMetadata } from './utils'
|
||||
|
||||
@provideSingleton(PluginEventManager)
|
||||
export class PluginEventManager {
|
||||
@Autowired()
|
||||
private EventManager: event.Event
|
||||
@Autowired()
|
||||
private ServerChecker: server.ServerChecker
|
||||
|
||||
constructor() {
|
||||
process.on('plugin.before.enable', this.registryListener.bind(this))
|
||||
process.on('plugin.after.disable', this.unregistryListener.bind(this))
|
||||
}
|
||||
|
||||
mapEventName() {
|
||||
return this.EventManager.mapEventName().toFixed(0)
|
||||
}
|
||||
|
||||
private registryListener(pluginInstance: plugin.Plugin) {
|
||||
let events = getPluginListenerMetadata(pluginInstance)
|
||||
for (const event of events) {
|
||||
// ignore space listener
|
||||
if (!this.ServerChecker.check(event.servers)) {
|
||||
console.debug(`[${pluginInstance.description.name}] ${event.target.constructor.name} incompatible event ${event.name} server(${event.servers}) ignore.`)
|
||||
continue
|
||||
}
|
||||
// here must bind this to pluginInstance
|
||||
let exec = event.target[event.executor]
|
||||
let execBinded = exec.bind(pluginInstance)
|
||||
execBinded.executor = event.executor
|
||||
exec.off = this.EventManager.listen(pluginInstance, event.name, execBinded, event.priority, event.ignoreCancel)
|
||||
}
|
||||
}
|
||||
|
||||
private unregistryListener(pluginInstance: plugin.Plugin) {
|
||||
this.EventManager.disable(pluginInstance)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ export * from './manager'
|
||||
export * from './decorators'
|
||||
export * from './interfaces'
|
||||
|
||||
export { PluginConfig } from './config'
|
||||
|
||||
export {
|
||||
plugin as JSPlugin,
|
||||
cmd as Cmd,
|
||||
|
||||
@@ -25,12 +25,20 @@ export namespace interfaces {
|
||||
public disable() { }
|
||||
}
|
||||
export interface ExecMetadata extends plugin.BaseMetadata {
|
||||
/**
|
||||
* 执行器所在对象 用于绑定this
|
||||
*/
|
||||
target?: any
|
||||
/**
|
||||
* 执行器
|
||||
*/
|
||||
executor?: string
|
||||
}
|
||||
export interface CommandMetadata extends ExecMetadata {
|
||||
/**
|
||||
* 命令别名
|
||||
*/
|
||||
alias?: string[]
|
||||
/**
|
||||
* 命令描述
|
||||
*/
|
||||
@@ -64,9 +72,16 @@ export namespace interfaces {
|
||||
*/
|
||||
format?: string
|
||||
/**
|
||||
* 是否为只读(关闭时将不会自动保存)
|
||||
* 自动保存 默认为 false
|
||||
*/
|
||||
readonly?: boolean
|
||||
autosave?: boolean
|
||||
/**
|
||||
* 配置文件名称
|
||||
*/
|
||||
filename?: string
|
||||
/**
|
||||
* 配置文件全路径
|
||||
*/
|
||||
file?: any
|
||||
}
|
||||
export type PluginLike = Plugin | string
|
||||
}
|
||||
|
||||
@@ -1,24 +1,27 @@
|
||||
import { plugin } from "@ccms/api"
|
||||
import { provideSingleton } from "@ccms/container"
|
||||
import { provideSingletonNamed } from "@ccms/container"
|
||||
|
||||
@provideSingleton(plugin.PluginLoader)
|
||||
const LOADER_TYPE_NAME = 'basic'
|
||||
|
||||
@provideSingletonNamed(plugin.PluginLoader, LOADER_TYPE_NAME)
|
||||
export class BasicLoader implements plugin.PluginLoader {
|
||||
type: string = 'basic'
|
||||
type: string = LOADER_TYPE_NAME
|
||||
|
||||
private pluginRequireMap: Map<string, any>
|
||||
|
||||
constructor() {
|
||||
this.pluginRequireMap = new Map()
|
||||
}
|
||||
require(target: any, result: any) {
|
||||
this.pluginRequireMap.set(target.toString(), result)
|
||||
return result
|
||||
require(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
let metadata = loadMetadata.instance?.description
|
||||
if (metadata?.type == this.type) {
|
||||
loadMetadata.metadata = metadata
|
||||
loadMetadata.loaded = true
|
||||
this.pluginRequireMap.set(metadata.source.toString(), loadMetadata.instance)
|
||||
}
|
||||
return loadMetadata
|
||||
}
|
||||
build(metadata: plugin.PluginMetadata) {
|
||||
return this.pluginRequireMap.get(metadata.source.toString())
|
||||
}
|
||||
load(plugin: plugin.Plugin): void { }
|
||||
enable(plugin: plugin.Plugin): void { }
|
||||
disable(plugin: plugin.Plugin): void { }
|
||||
reload(plugin: plugin.Plugin): void { }
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
import { plugin, server } from "@ccms/api"
|
||||
import { inject, ContainerInstance, Container, provideSingleton } from "@ccms/container"
|
||||
import { inject, ContainerInstance, Container, provideSingletonNamed, Autowired } from "@ccms/container"
|
||||
|
||||
import { interfaces } from "../interfaces"
|
||||
import { getPluginStageMetadata, getPluginSources } from "../utils"
|
||||
|
||||
@provideSingleton(plugin.PluginLoader)
|
||||
const LOADER_TYPE_NAME = 'ioc'
|
||||
|
||||
@provideSingletonNamed(plugin.PluginLoader, LOADER_TYPE_NAME)
|
||||
export class IocLoader implements plugin.PluginLoader {
|
||||
type: string = 'ioc'
|
||||
type: string = LOADER_TYPE_NAME
|
||||
@inject(ContainerInstance)
|
||||
private container: Container
|
||||
@inject(server.ServerType)
|
||||
private serverType: string
|
||||
@Autowired()
|
||||
private serverChecker: server.ServerChecker
|
||||
|
||||
private pluginMetadataMap: Map<string, plugin.PluginMetadata>
|
||||
|
||||
@@ -18,12 +20,16 @@ export class IocLoader implements plugin.PluginLoader {
|
||||
this.pluginMetadataMap = getPluginSources()
|
||||
}
|
||||
|
||||
require(target: any, result: any) {
|
||||
return this.pluginMetadataMap.get(target.toString())
|
||||
require(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
let metadata = this.pluginMetadataMap.get(loadMetadata.file.toString())
|
||||
if (metadata && metadata.type == this.type) {
|
||||
loadMetadata.metadata = metadata
|
||||
loadMetadata.loaded = true
|
||||
}
|
||||
return loadMetadata
|
||||
}
|
||||
|
||||
build(metadata: plugin.PluginMetadata) {
|
||||
if (!this.allowProcess(metadata.servers)) { return }
|
||||
let pluginInstance: plugin.Plugin
|
||||
try {
|
||||
this.bindPlugin(metadata)
|
||||
@@ -63,28 +69,15 @@ export class IocLoader implements plugin.PluginLoader {
|
||||
console.i18n('ms.plugin.manager.build.duplicate', { exists: pluginInstance.description.source, source: metadata.source })
|
||||
}
|
||||
this.container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
|
||||
} catch{
|
||||
} catch {
|
||||
this.container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
|
||||
}
|
||||
}
|
||||
|
||||
private allowProcess(servers: string[]) {
|
||||
// Not set servers -> allow
|
||||
if (!servers || !servers.length) return true
|
||||
// include !type -> deny
|
||||
let denyServers = servers.filter(svr => svr.startsWith("!"))
|
||||
if (denyServers.length !== 0) {
|
||||
return !denyServers.includes(`!${this.serverType}`)
|
||||
} else {
|
||||
// only include -> allow
|
||||
return servers.includes(this.serverType)
|
||||
}
|
||||
}
|
||||
|
||||
private stage(pluginInstance: plugin.Plugin, stageName: string) {
|
||||
let stages = getPluginStageMetadata(pluginInstance, stageName)
|
||||
for (const stage of stages) {
|
||||
if (!this.allowProcess(stage.servers)) { continue }
|
||||
if (!this.serverChecker.check(stage.servers)) { continue }
|
||||
console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers })
|
||||
try {
|
||||
pluginInstance[stage.executor].apply(pluginInstance)
|
||||
|
||||
@@ -1,28 +1,38 @@
|
||||
import i18n from '@ccms/i18n'
|
||||
import { plugin, server, command, event } from '@ccms/api'
|
||||
import { inject, provideSingleton, Container, ContainerInstance } from '@ccms/container'
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import { plugin, server } from '@ccms/api'
|
||||
import { provideSingleton, Container, ContainerInstance, Autowired } from '@ccms/container'
|
||||
|
||||
import './config'
|
||||
import { interfaces } from './interfaces'
|
||||
import { getConfigLoader } from './config'
|
||||
import { getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils'
|
||||
import { PluginTaskManager } from './task'
|
||||
import { PluginEventManager } from './event'
|
||||
import { PluginCommandManager } from './command'
|
||||
import { PluginConfigManager } from './config'
|
||||
|
||||
const Thread = Java.type('java.lang.Thread')
|
||||
|
||||
@provideSingleton(plugin.PluginManager)
|
||||
export class PluginManagerImpl implements plugin.PluginManager {
|
||||
@inject(ContainerInstance)
|
||||
@Autowired(ContainerInstance)
|
||||
private container: Container
|
||||
@inject(plugin.PluginInstance)
|
||||
@Autowired(plugin.PluginInstance)
|
||||
private pluginInstance: any
|
||||
@inject(plugin.PluginFolder)
|
||||
private pluginFolder: string
|
||||
@inject(server.ServerType)
|
||||
@Autowired(server.ServerType)
|
||||
private serverType: string
|
||||
@inject(command.Command)
|
||||
private CommandManager: command.Command
|
||||
@inject(event.Event)
|
||||
private EventManager: event.Event
|
||||
|
||||
@Autowired()
|
||||
private serverChecker: server.ServerChecker
|
||||
@Autowired()
|
||||
private nativePluginManager: server.NativePluginManager
|
||||
|
||||
@Autowired()
|
||||
private taskManager: PluginTaskManager
|
||||
@Autowired()
|
||||
private eventManager: PluginEventManager
|
||||
@Autowired()
|
||||
private configManager: PluginConfigManager
|
||||
@Autowired()
|
||||
private commandManager: PluginCommandManager
|
||||
|
||||
private initialized: boolean = false
|
||||
|
||||
@@ -38,13 +48,19 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
|
||||
this.instanceMap = new Map()
|
||||
this.metadataMap = new Map()
|
||||
|
||||
// ignore unused
|
||||
this.taskManager
|
||||
this.eventManager
|
||||
this.configManager
|
||||
this.commandManager
|
||||
}
|
||||
|
||||
initialize() {
|
||||
if (this.pluginInstance === undefined) { throw new Error("Can't found Plugin Instance!") }
|
||||
if (this.initialized !== true) {
|
||||
console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader })
|
||||
console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType })
|
||||
console.i18n('ms.plugin.event.map', { count: this.eventManager.mapEventName(), type: this.serverType })
|
||||
let pluginScanner = this.container.getAll<plugin.PluginScanner>(plugin.PluginScanner)
|
||||
pluginScanner.forEach((scanner) => {
|
||||
console.debug(`loading plugin sacnner ${scanner.type}...`)
|
||||
@@ -56,6 +72,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
this.loaderMap.set(loader.type, loader)
|
||||
})
|
||||
this.initialized = true
|
||||
process.emit('plugin.initialize')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,53 +81,81 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
this.initialize()
|
||||
for (const [, scanner] of this.sacnnerMap) {
|
||||
try {
|
||||
scanner.scan(folder).forEach(file => {
|
||||
this.loadPlugin(file, scanner)
|
||||
scanner.scan(folder).forEach(loadMetadata => {
|
||||
try {
|
||||
this.loadPlugin(scanner.load(loadMetadata))
|
||||
} catch (error) {
|
||||
console.error(`plugin scanner ${scanner.type} load ${loadMetadata.file} occurred error ${error}`)
|
||||
console.ex(error)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`plugin scanner ${scanner.type} occurred error ${error}`)
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
process.emit('plugin.scan', folder)
|
||||
}
|
||||
|
||||
build(): void {
|
||||
this.buildPlugins()
|
||||
process.emit('plugin.build')
|
||||
}
|
||||
|
||||
private logStage(plugin: plugin.Plugin, stage: string) {
|
||||
console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author })
|
||||
}
|
||||
|
||||
private runPluginStage(plugin: plugin.Plugin, stage: string, ext: Function) {
|
||||
private runPluginStage(plugin: plugin.Plugin, stage: string) {
|
||||
if (!plugin) { throw new Error(`can't run runPluginStage ${stage} because plugin is ${plugin}`) }
|
||||
try {
|
||||
this.logStage(plugin, i18n.translate(`ms.plugin.manager.stage.${stage}`))
|
||||
ext()
|
||||
process.emit(`plugin.before.${stage}`, plugin)
|
||||
this.runCatch(plugin, stage)
|
||||
this.runCatch(plugin, `${this.serverType}${stage}`)
|
||||
plugin.description.loader[stage](plugin)
|
||||
plugin.description.loadMetadata.loader[stage]?.(plugin)
|
||||
process.emit(`plugin.after.${stage}`, plugin)
|
||||
} catch (ex) {
|
||||
console.i18n("ms.plugin.manager.stage.exec.error", { plugin: plugin.description.name, executor: stage, error: ex })
|
||||
if (global.debug) { console.ex(ex) }
|
||||
}
|
||||
}
|
||||
|
||||
private loadPlugin(file: string, scanner: plugin.PluginScanner) {
|
||||
private loadPlugin(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
if (!loadMetadata) { throw new Error('loadMetadata can\'t be undefiend when loadPlugin!') }
|
||||
if (loadMetadata.loaded) { throw new Error(`Plugin file ${loadMetadata.file} is already loaded by ${loadMetadata.loader?.type}!`) }
|
||||
try {
|
||||
let requireInstance = scanner.load(file)
|
||||
for (const [, loader] of this.loaderMap) {
|
||||
let metadata = loader.require(file, requireInstance)
|
||||
if (metadata && metadata.source && metadata.name) {
|
||||
metadata.loader = loader
|
||||
this.metadataMap.set(metadata.name, metadata)
|
||||
return metadata
|
||||
}
|
||||
if (this.loaderRequirePlugin(loadMetadata, loader)?.loaded) return loadMetadata.metadata
|
||||
}
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.initialize.error", { name: file, ex: error })
|
||||
console.i18n("ms.plugin.manager.initialize.error", { name: loadMetadata.file, ex: error })
|
||||
console.ex(error)
|
||||
}
|
||||
console.console(`§efile §b${file} §ccan't load metadata. §eskip load!`)
|
||||
console.console(`§6scanner: §b${loadMetadata.scanner.type} §ccan\'t load §6file §b${loadMetadata.file}. §eskip!`)
|
||||
}
|
||||
|
||||
private loaderRequirePlugin(loadMetadata: plugin.PluginLoadMetadata, loader: plugin.PluginLoader) {
|
||||
try {
|
||||
if (loader.require(loadMetadata).loaded) {
|
||||
loadMetadata.loader = loader
|
||||
let metadata = loadMetadata.metadata
|
||||
if (this.metadataMap.has(metadata.name) && this.instanceMap.has(metadata.name)) {
|
||||
let oldMetadata = this.metadataMap.get(metadata.name)
|
||||
throw new Error(`Plugin ${oldMetadata.name} is already load from ${oldMetadata.source}...`)
|
||||
}
|
||||
this.metadataMap.set(metadata.name, metadata)
|
||||
metadata.loadMetadata = loadMetadata
|
||||
}
|
||||
return loadMetadata
|
||||
} catch (error) {
|
||||
if (global.debug) {
|
||||
console.console(`§6Loader §b${loader.type} §6load §a${loadMetadata.file} §cerror. §4Err: §c${error}`)
|
||||
console.ex(error)
|
||||
} else {
|
||||
console.warn(`Loader ${loader.type} load ${loadMetadata.file} error. Err: ${error}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,46 +163,35 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
* @param file java.io.File
|
||||
*/
|
||||
loadFromFile(file: string, scanner = this.sacnnerMap.get('file')): plugin.Plugin {
|
||||
if (!file) { throw new Error('plugin file can\'t be null!') }
|
||||
if (!scanner) { throw new Error('plugin scanner can\'t be null!') }
|
||||
let metadata = this.loadPlugin(file, scanner)
|
||||
let plugin = metadata.loader.build(metadata)
|
||||
if (!file) { throw new Error('plugin file can\'t be undefiend!') }
|
||||
if (!scanner) { throw new Error('plugin scanner can\'t be undefiend!') }
|
||||
let metadata = this.loadPlugin(scanner.load(scanner.read(file)))
|
||||
let plugin = this.buildPlugin(metadata)
|
||||
this.load(plugin)
|
||||
this.enable(plugin)
|
||||
return plugin
|
||||
}
|
||||
|
||||
load(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => {
|
||||
this.runPluginStage(plugin, 'load', () => {
|
||||
this.loadConfig(plugin)
|
||||
})
|
||||
})
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => this.runPluginStage(plugin, 'load'))
|
||||
}
|
||||
|
||||
enable(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => {
|
||||
this.runPluginStage(plugin, 'enable', () => {
|
||||
this.registryCommand(plugin)
|
||||
this.registryListener(plugin)
|
||||
})
|
||||
})
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => this.runPluginStage(plugin, 'enable'))
|
||||
}
|
||||
|
||||
disable(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => {
|
||||
this.runPluginStage(plugin, 'disable', () => {
|
||||
this.saveConfig(plugin)
|
||||
this.unregistryCommand(plugin)
|
||||
this.unregistryListener(plugin)
|
||||
})
|
||||
this.runPluginStage(plugin, 'disable')
|
||||
this.metadataMap.delete(plugin.description.name)
|
||||
this.instanceMap.delete(plugin.description.name)
|
||||
})
|
||||
}
|
||||
|
||||
reload(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((pl: plugin.Plugin) => {
|
||||
this.disable(pl)
|
||||
this.loadFromFile(pl.description.source, pl.description.scanner)
|
||||
this.loadFromFile(pl.description.loadMetadata.file, pl.description.loadMetadata.scanner)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -179,107 +213,44 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
|
||||
private checkAndGet(name: string | plugin.Plugin | undefined | any): Map<string, plugin.Plugin> | plugin.Plugin[] {
|
||||
if (name === undefined) throw new Error(`checkAndGet Plugin can't be undefiend!`)
|
||||
if (name == this.instanceMap) { return this.instanceMap }
|
||||
if (typeof name == 'string' && this.instanceMap.has(name)) { return [this.instanceMap.get(name)] }
|
||||
if (name instanceof interfaces.Plugin) { return [name as plugin.Plugin] }
|
||||
if (name.description || name.description.name) { return [name as plugin.Plugin] }
|
||||
if (name.description?.name) { return [name as plugin.Plugin] }
|
||||
throw new Error(`Plugin ${JSON.stringify(name)} not exist!`)
|
||||
}
|
||||
|
||||
private allowProcess(servers: string[]) {
|
||||
// Not set servers -> allow
|
||||
if (!servers || !servers.length) return true
|
||||
// include !type -> deny
|
||||
let denyServers = servers.filter(svr => svr.startsWith("!"))
|
||||
if (denyServers.length !== 0) {
|
||||
return !denyServers.includes(`!${this.serverType}`)
|
||||
} else {
|
||||
// only include -> allow
|
||||
return servers.includes(this.serverType)
|
||||
}
|
||||
}
|
||||
|
||||
private buildPlugins() {
|
||||
for (const [, metadata] of this.metadataMap) {
|
||||
let pluginInstance: plugin.Plugin
|
||||
if (!this.loaderMap.has(metadata.type)) {
|
||||
console.error(`§4无法加载插件 §c${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`)
|
||||
continue
|
||||
}
|
||||
(pluginInstance = this.loaderMap.get(metadata.type).build(metadata)) && this.instanceMap.set(metadata.name, pluginInstance)
|
||||
}
|
||||
}
|
||||
|
||||
private loadConfig(plugin: plugin.Plugin) {
|
||||
let configs = getPluginConfigMetadata(plugin)
|
||||
for (let [_, config] of configs) {
|
||||
this.metadataMap.forEach((metadata) => {
|
||||
try {
|
||||
let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format)
|
||||
let configFactory = getConfigLoader(config.format)
|
||||
if (!fs.exists(configFile)) {
|
||||
base.save(configFile, configFactory.dump(plugin[config.variable]))
|
||||
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: config.name, format: config.format })
|
||||
} else {
|
||||
plugin[config.variable] = configFactory.load(base.read(configFile))
|
||||
plugin[config.variable].save = () => {
|
||||
let result = configFactory.dump(plugin[config.variable])
|
||||
base.save(configFile, result)
|
||||
console.debug(`[${plugin.description.name}] Save Config ${config.variable} to file ${configFile} result ${result}`)
|
||||
}
|
||||
console.debug(`[${plugin.description.name}] Load Config ${config.variable} from file ${configFile} result ${JSON.stringify(plugin[config.variable])}`)
|
||||
}
|
||||
this.buildPlugin(metadata)
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.config.load.error", { plugin: plugin.description.name, name: config.name, format: config.format, error })
|
||||
console.console(`§4无法加载插件 §b${metadata.name} §4构建插件失败!`)
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private saveConfig(plugin: plugin.Plugin) {
|
||||
let configs = getPluginConfigMetadata(plugin)
|
||||
for (let [_, config] of configs) {
|
||||
try {
|
||||
let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format)
|
||||
let configFactory = getConfigLoader(config.format)
|
||||
if (!config.readonly) { base.save(configFile, configFactory.dump(plugin[config.variable])) }
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.config.save.error", { plugin: plugin.description.name, name: config.name, format: config.format, error })
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private registryCommand(pluginInstance: plugin.Plugin) {
|
||||
let cmds = getPluginCommandMetadata(pluginInstance)
|
||||
let tabs = getPluginTabCompleterMetadata(pluginInstance)
|
||||
for (const [_, cmd] of cmds) {
|
||||
let tab = tabs.get(cmd.name)
|
||||
if (!this.allowProcess(cmd.servers)) { continue }
|
||||
this.CommandManager.on(pluginInstance, cmd.name, {
|
||||
cmd: pluginInstance[cmd.executor].bind(pluginInstance),
|
||||
tab: tab ? pluginInstance[tab.executor].bind(pluginInstance) : undefined
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private registryListener(pluginInstance: plugin.Plugin) {
|
||||
let events = getPluginListenerMetadata(pluginInstance)
|
||||
for (const event of events) {
|
||||
// ignore space listener
|
||||
if (!this.allowProcess(event.servers)) { continue }
|
||||
// here must bind this to pluginInstance
|
||||
this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance))
|
||||
}
|
||||
}
|
||||
|
||||
private unregistryCommand(pluginInstance: plugin.Plugin) {
|
||||
let cmds = getPluginCommandMetadata(pluginInstance)
|
||||
cmds.forEach(cmd => {
|
||||
this.CommandManager.off(pluginInstance, cmd.name)
|
||||
})
|
||||
}
|
||||
|
||||
private unregistryListener(pluginInstance: plugin.Plugin) {
|
||||
this.EventManager.disable(pluginInstance)
|
||||
private checkDepends(depends: string | string[]) {
|
||||
if (!depends) return true
|
||||
for (const depend of depends) { if (!this.metadataMap.has(depend)) return false }
|
||||
return true
|
||||
}
|
||||
private checkNativeDepends(depends: string | string[]) {
|
||||
if (!depends) return true
|
||||
for (const depend of depends) { if (!this.nativePluginManager.has(depend)) return false }
|
||||
return true
|
||||
}
|
||||
private buildPlugin(metadata: plugin.PluginMetadata) {
|
||||
if (this.instanceMap.has(metadata.name)) { throw new Error(`Plugin ${metadata.name} is already load from ${metadata.source}...`) }
|
||||
if (!this.loaderMap.has(metadata.type)) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`) }
|
||||
if (!this.serverChecker.check(metadata.servers)) { throw new Error(`§6插件 §b${metadata.name} §c服务器类型不兼容(${metadata.servers.join(',')}) §6忽略加载...`) }
|
||||
if (!this.checkDepends(metadata.depends)) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查依赖 §3${metadata.depends.join(',')} §4是否安装完整!`) }
|
||||
if (!this.checkNativeDepends(metadata.nativeDepends)) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查插件依赖 §3${metadata.nativeDepends.join(',')} §4是否安装完整!`) }
|
||||
let pluginInstance = this.loaderMap.get(metadata.type).build(metadata)
|
||||
if (!pluginInstance) { throw new Error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`) }
|
||||
this.instanceMap.set(metadata.name, pluginInstance)
|
||||
return pluginInstance
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,26 @@ import { plugin } from "@ccms/api"
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import { provideSingletonNamed } from "@ccms/container"
|
||||
|
||||
@provideSingletonNamed(plugin.PluginScanner, 'file')
|
||||
export class JSFileScanner implements plugin.PluginScanner {
|
||||
type: string = 'file'
|
||||
const SCANNER_TYPE_NAME = 'file'
|
||||
|
||||
scan(target: any): string[] {
|
||||
return this.scanFolder(fs.concat(root, target))
|
||||
@provideSingletonNamed(plugin.PluginScanner, SCANNER_TYPE_NAME)
|
||||
export class JSFileScanner implements plugin.PluginScanner {
|
||||
type: string = SCANNER_TYPE_NAME
|
||||
|
||||
scan(target: any): plugin.PluginLoadMetadata[] {
|
||||
return this.scanFolder(fs.concat(root, target)).map((file) => this.read(file))
|
||||
}
|
||||
|
||||
load(file: string) {
|
||||
if (typeof file === "string") { return }
|
||||
this.updatePlugin(file)
|
||||
read(file: any): plugin.PluginLoadMetadata {
|
||||
return { file, type: this.type, scanner: this, loaded: false }
|
||||
}
|
||||
|
||||
load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
|
||||
if (metadata.type !== this.type) { return }
|
||||
this.updatePlugin(metadata.file)
|
||||
//@ts-ignore
|
||||
return require(file.toString(), { cache: false })
|
||||
metadata.instance = require(metadata.file.toString(), { cache: false })
|
||||
return metadata
|
||||
}
|
||||
|
||||
private scanFolder(folder: any): string[] {
|
||||
@@ -39,7 +46,7 @@ export class JSFileScanner implements plugin.PluginScanner {
|
||||
}
|
||||
|
||||
private updatePlugin(file: any) {
|
||||
var update = fs.file(fs.file(file.parentFile, 'update'), file.name)
|
||||
var update = fs.file(fs.file(fs.file(file).parentFile, 'update'), file.name)
|
||||
if (update.exists()) {
|
||||
console.i18n("ms.plugin.manager.build.update", { name: file.name })
|
||||
fs.move(update, file, true)
|
||||
|
||||
16
packages/plugin/src/task.ts
Normal file
16
packages/plugin/src/task.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { plugin, task } from '@ccms/api'
|
||||
import { provideSingleton, Autowired } from '@ccms/container'
|
||||
|
||||
@provideSingleton(PluginTaskManager)
|
||||
export class PluginTaskManager {
|
||||
@Autowired()
|
||||
private taskManager: task.TaskManager
|
||||
|
||||
constructor() {
|
||||
process.on('plugin.after.disable', this.cancelTask.bind(this))
|
||||
}
|
||||
|
||||
private cancelTask(pluginInstance: plugin.Plugin) {
|
||||
this.taskManager.disable(pluginInstance)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ function getPlugin(name: string) {
|
||||
|
||||
function getPluginSources() {
|
||||
let pluginSources: Map<string, plugin.PluginMetadata> = Reflect.getMetadata(
|
||||
METADATA_KEY.souece,
|
||||
METADATA_KEY.source,
|
||||
Reflect
|
||||
) || pluginSourceCache
|
||||
return pluginSources
|
||||
@@ -37,10 +37,10 @@ function getPluginMetadata(target: any) {
|
||||
}
|
||||
|
||||
function getPluginCommandMetadata(target: any) {
|
||||
let commandMetadata: Map<string, interfaces.CommandMetadata> = Reflect.getMetadata(
|
||||
let commandMetadata: interfaces.CommandMetadata[] = Reflect.getMetadata(
|
||||
METADATA_KEY.cmd,
|
||||
target.constructor
|
||||
) || new Map<string, interfaces.CommandMetadata>()
|
||||
) || []
|
||||
return commandMetadata
|
||||
}
|
||||
|
||||
|
||||
67
packages/plugins/docs/MiaoBlockly.md
Normal file
67
packages/plugins/docs/MiaoBlockly.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# MiaoBlockly
|
||||
|
||||
## 前言
|
||||
|
||||
- 嗯 我又来了
|
||||
- 之前承诺的 MiaoScript 腐竹可以用的版本
|
||||
- 第一个 Demo 版本 MiaoBlockly 写完了
|
||||
|
||||
### 什么是 MiaoBlockly
|
||||
|
||||
#### ~~对于开发者~~(开发一般不会用这玩意儿)
|
||||
|
||||
#### 对于腐竹
|
||||
- 可以在网页上开发一些简单的插件
|
||||
- 拖动方块就能实现了
|
||||
|
||||
### 图片展示
|
||||
|
||||

|
||||
|
||||
### MiaoBlockly 的兼容性
|
||||
- 跨端兼容 Sponge Bukkit BungeeCord
|
||||
- Bukkit 支持 1.12.2 +(由于1.12.2以下版本Netty版本过老需要适配 后续兼容到1.7.10)
|
||||
- Sponge
|
||||
- CatServer 1.12.2+
|
||||
- BungeeCord
|
||||
|
||||
### 安装教程
|
||||
- 安装 `MiaoConsole` [站内地址](https://www.mcbbs.net/thread-1129227-1-1.html) 没错 就是装 MiaoConsole
|
||||
- 因为 `MiaoBlockly` 底层还是运行的 js 只是用 `Blockly` 生成了代码而已
|
||||
- 需要 `MiaoConsole` 是因为需要和服务器通信
|
||||
|
||||
### 使用教程
|
||||
- 查看启动日志 获取临时 `Token`
|
||||
- 或者 执行 `mconsole token` 获得临时Token
|
||||
- 打开 `http://ms.yumc.pw/blockly.html`
|
||||
- 在地址栏输入服务器地址 需要带上端口
|
||||
- 在 Token 栏输入获取到的 Token
|
||||
- 点击链接即可
|
||||
|
||||
### 编辑器使用
|
||||
- 默认界面有一个插件主体
|
||||
- 拖动积木 右侧会自动生成代码
|
||||
- 在代码界面 按下 `Ctrl + S` 代码会自动传送到服务器 并且重载插件
|
||||
- 更多功能还在开发
|
||||
|
||||
### RoadMap
|
||||
- 开发聊天相关功能
|
||||
- 完善事件相关功能
|
||||
- 开发命令相关功能
|
||||
- 开发跨服相关功能
|
||||
- 统一各个端之间的事件(工作量较大 可能会咕咕咕)
|
||||
- 脚本商城 方便开发和腐竹之前的 PY 交易
|
||||
- 脚本打包成 Jar 方便部署
|
||||
|
||||
### Miao系列插件
|
||||
|
||||
- [[经济]MiaoReward —— 喵式奖励 让玩家看广告为服务器提供收入吧[1.7.10+全版本]](https://www.mcbbs.net/thread-1121423-1-1.html)
|
||||
- [[编程]MiaoBlockly —— 喵式积木 用简单的积木来写插件吧[1.12.2+全版本]](https://www.mcbbs.net/thread-1129411-1-1.html)
|
||||
- [[编程]MiaoConsole —— 喵式终端 通过MC端口直接控制服务器 调试插件[1.12.2+全版本]](https://www.mcbbs.net/thread-1129227-1-1.html)
|
||||
- [[管理]MiaoBind —— 喵式绑定 兼容SoulBound的绑定插件 支持自定义关键词[1.7+全版本]](https://www.mcbbs.net/thread-922072-1-1.html)
|
||||
- [[信息]MiaoBoard —— 喵式记分板 自定义动态记分板[1.7+全版本]](https://www.mcbbs.net/thread-631482-1-1.html)
|
||||
- [[聊天]MiaoChat —— 喵式聊天 多功能自定义聊天格式 新增支持跨服[1.7.10+全版本]](https://www.mcbbs.net/thread-631240-1-1.html)
|
||||
- [[菜单]MiaoMenu —— 喵式菜单 强大的自定义菜单 支持多种自定义操作[1.7+全版本]](https://www.mcbbs.net/thread-860047-1-1.html)
|
||||
- [[管理]YUM —— 全能的服务器插件管理工具 全自动安装插件 更新插件[1.7.2+全版本]](https://www.mcbbs.net/thread-701333-1-1.html)
|
||||
|
||||
本插件所用所有代码均为原创,不存在借用/抄袭等行为
|
||||
@@ -1,12 +1,24 @@
|
||||
# MiaoConsole
|
||||
|
||||
## 前言
|
||||
|
||||
> 各位开发 在调试Minecraft插件的时候 是不是经常面临下列情况
|
||||
- 编译等半天 重启等半天 测试找不到问题
|
||||
- 后台 IDE SCP/FTP 来回切换
|
||||
|
||||
### 什么是MiaoConsole
|
||||
- MiaoConsole 是 基于 MiaoScript 开发的 在线调试工具
|
||||
- 通过复用 Minecraft的Netty 通道 实现服务器端口 在线调试(并不是Java的调试哦)
|
||||
> 各位腐竹 在开服的时候 是不是经常遇到下列情况
|
||||
- 修改一个配置文件 经常格式错误
|
||||
- 各种报错看不懂 插件依赖找不到
|
||||
|
||||
### 什么是 MiaoConsole
|
||||
|
||||
#### 对于开发者
|
||||
- MiaoConsole 是 基于 `MiaoScript` 开发的 在线调试工具
|
||||
- 通过复用 Minecraft 的 Netty 通道 实现服务器端口 `MiaoScript` 在线调试
|
||||
|
||||
#### 对于腐竹
|
||||
- 可以在网页上实时看到服务器日志
|
||||
- 共享 Token 给插件开发者 让其帮助调试服务器
|
||||
|
||||
### 工具动图
|
||||
|
||||
@@ -16,8 +28,8 @@
|
||||
### MiaoConsole 有哪些功能
|
||||
- 无需编译 上传等步骤 实时在线debug
|
||||
- 实时转发服务端日志 无需来回切换
|
||||
- 基于Nashorn的动态脚本语言
|
||||
- 基于TypeScript的自动补全逻辑(以实现BungeeCord Sponge Bukkit的API全自动补全)
|
||||
- 基于 Nashorn 的动态脚本语言
|
||||
- 基于 TypeScript 的自动补全逻辑(以实现BungeeCord Sponge Bukkit的API全自动补全)
|
||||
|
||||
### MiaoConsole 的兼容性
|
||||
- 跨端兼容 Sponge Bukkit BungeeCord
|
||||
@@ -27,25 +39,27 @@
|
||||
- BungeeCord ~~终端转发暂未兼容~~ 已经能兼容了
|
||||
|
||||
### 安装教程
|
||||
- 安装MiaoScript [下载地址](http://w.yumc.pw/free.html#MiaoScript-download)
|
||||
- 首次启动 等待加载基础环境类库
|
||||
- 执行 `mpm i MiaoConsole`
|
||||
- 安装 `MiaoScript` [站内地址](https://www.mcbbs.net/thread-774401-1-1.html)
|
||||
- 如果你已经安装了 Yum 可以通过 Yum 快速安装 `/yum install MiaoScript`
|
||||
- 等待 MiaoScript 引擎启动完成(首次启动可能需要10-20秒 依赖于您的网络环境)
|
||||
- 执行 `mspm i MiaoConsole`
|
||||
|
||||
### 使用教程
|
||||
- 执行 `mconsole token` 获得临时Token
|
||||
- 查看启动日志 获取临时 `Token`
|
||||
- 或者 执行 `mconsole token` 获得临时Token
|
||||
- 打开 `http://ms.yumc.pw/console.html`
|
||||
- 在地址栏输入服务器地址 需要带上端口
|
||||
- 在Token栏输入获取到的Token
|
||||
- 在 Token 栏输入获取到的 Token
|
||||
- 点击链接即可
|
||||
|
||||
### 终端使用
|
||||
- 支持实时传输服务器日志到终端
|
||||
- 直接在终端输入命令 会转发到服务器
|
||||
- 目前暂不支持命令自动补全
|
||||
- 支持命令自动补全
|
||||
- 支持命令历史
|
||||
|
||||
### 代码编辑器使用
|
||||
- 支持 Java 的 lang包和util包自动补全
|
||||
- 支持 Java 的类自动补全
|
||||
- 支持服务端所有代码补全
|
||||
- 注意 暂时不支持自动导入包 所以需要手动输入全类名
|
||||
- 快捷键说明(选中代码后 只会执行选中部分)
|
||||
@@ -53,8 +67,27 @@
|
||||
- Ctrl + E 执行命令
|
||||
- Ctrl + Q 切换代码页
|
||||
|
||||
## 插件源码
|
||||
|
||||
- [MiaoScript包管理中心](https://git.yumc.pw/circlecloud/ms/src/branch/master/packages/plugins/src/MiaoConsole.ts)
|
||||
|
||||
### RoadMap
|
||||
- 支持命令自动补全
|
||||
- 支持常用的开发脚本
|
||||
- 支持快捷操作服务器数据
|
||||
- 支持 Java 类的自动补全
|
||||
- 支持命令自动补全(已完成)
|
||||
- 支持常用的开发脚本(开发中)
|
||||
- 支持快捷操作服务器数据(开发中)
|
||||
- 支持在线编辑服务器文件(开发了一半)
|
||||
- 配置文件格式效验(开发中)
|
||||
|
||||
### Miao系列插件
|
||||
|
||||
- [[经济]MiaoReward —— 喵式奖励 让玩家看广告为服务器提供收入吧[1.7.10+全版本]](https://www.mcbbs.net/thread-1121423-1-1.html)
|
||||
- [[编程]MiaoBlockly —— 喵式积木 用简单的积木来写插件吧[1.12.2+全版本]](https://www.mcbbs.net/thread-1129411-1-1.html)
|
||||
- [[编程]MiaoConsole —— 喵式终端 通过MC端口直接控制服务器 调试插件[1.12.2+全版本]](https://www.mcbbs.net/thread-1129227-1-1.html)
|
||||
- [[管理]MiaoBind —— 喵式绑定 兼容SoulBound的绑定插件 支持自定义关键词[1.7+全版本]](https://www.mcbbs.net/thread-922072-1-1.html)
|
||||
- [[信息]MiaoBoard —— 喵式记分板 自定义动态记分板[1.7+全版本]](https://www.mcbbs.net/thread-631482-1-1.html)
|
||||
- [[聊天]MiaoChat —— 喵式聊天 多功能自定义聊天格式 新增支持跨服[1.7.10+全版本]](https://www.mcbbs.net/thread-631240-1-1.html)
|
||||
- [[菜单]MiaoMenu —— 喵式菜单 强大的自定义菜单 支持多种自定义操作[1.7+全版本]](https://www.mcbbs.net/thread-860047-1-1.html)
|
||||
- [[管理]YUM —— 全能的服务器插件管理工具 全自动安装插件 更新插件[1.7.2+全版本]](https://www.mcbbs.net/thread-701333-1-1.html)
|
||||
|
||||
本插件所用所有代码均为原创,不存在借用/抄袭等行为
|
||||
|
||||
24
packages/plugins/docs/MiaoPlugin.md
Normal file
24
packages/plugins/docs/MiaoPlugin.md
Normal file
@@ -0,0 +1,24 @@
|
||||
### Miao系列插件
|
||||
|
||||
- [[经济]MiaoReward —— 喵式奖励 让玩家看广告为服务器提供收入吧[1.7.10+全版本]](https://www.mcbbs.net/thread-1121423-1-1.html)
|
||||
- [[编程]MiaoBlockly —— 喵式积木 用简单的积木来写插件吧[1.12.2+全版本]](https://www.mcbbs.net/thread-1129411-1-1.html)
|
||||
- [[编程]MiaoConsole —— 喵式终端 通过MC端口直接控制服务器 调试插件[1.12.2+全版本]](https://www.mcbbs.net/thread-1129227-1-1.html)
|
||||
- [[管理]MiaoBind —— 喵式绑定 兼容SoulBound的绑定插件 支持自定义关键词[1.7+全版本]](https://www.mcbbs.net/thread-922072-1-1.html)
|
||||
- [[信息]MiaoBoard —— 喵式记分板 自定义动态记分板[1.7+全版本]](https://www.mcbbs.net/thread-631482-1-1.html)
|
||||
- [[聊天]MiaoChat —— 喵式聊天 多功能自定义聊天格式 新增支持跨服[1.7.10+全版本]](https://www.mcbbs.net/thread-631240-1-1.html)
|
||||
- [[菜单]MiaoMenu —— 喵式菜单 强大的自定义菜单 支持多种自定义操作[1.7+全版本]](https://www.mcbbs.net/thread-860047-1-1.html)
|
||||
- [[管理]YUM —— 全能的服务器插件管理工具 全自动安装插件 更新插件[1.7.2+全版本]](https://www.mcbbs.net/thread-701333-1-1.html)
|
||||
|
||||
[size=20px][b]Miao系列插件[/b][/size]
|
||||
|
||||
[size=16px][list]
|
||||
[*][url=https://www.mcbbs.net/thread-1121423-1-1.html][经济]MiaoReward —— 喵式奖励 让玩家看广告为服务器提供收入吧[1.7.10+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-1129411-1-1.html][编程]MiaoBlockly —— 喵式积木 用简单的积木来写插件吧[1.12.2+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-1129227-1-1.html][编程]MiaoConsole —— 喵式终端 通过MC端口直接控制服务器 调试插件[1.12.2+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-922072-1-1.html][管理]MiaoBind —— 喵式绑定 兼容SoulBound的绑定插件 支持自定义关键词[1.7+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-631482-1-1.html][信息]MiaoBoard —— 喵式记分板 自定义动态记分板[1.7+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-631240-1-1.html][聊天]MiaoChat —— 喵式聊天 多功能自定义聊天格式 新增支持跨服[1.7.10+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-860047-1-1.html][菜单]MiaoMenu —— 喵式菜单 强大的自定义菜单 支持多种自定义操作[1.7+全版本][/url]
|
||||
[*][url=https://www.mcbbs.net/thread-701333-1-1.html][管理]YUM —— 全能的服务器插件管理工具 全自动安装插件 更新插件[1.7.2+全版本][/url][/list][/size]
|
||||
|
||||
[size=16px]本插件所用所有代码均为原创,不存在借用/抄袭等行为[/size]
|
||||
151
packages/plugins/docs/MiaoReward.md
Normal file
151
packages/plugins/docs/MiaoReward.md
Normal file
@@ -0,0 +1,151 @@
|
||||
# MiaoReward
|
||||
|
||||
## 插件简介
|
||||
- 还在为服务器收入不足而倒闭烦恼嘛
|
||||
- 还在为肝帝不氪金而烦恼嘛
|
||||
- 快来接入 喵式奖励
|
||||
- 通过QQ小程序广告 增加服务器收入
|
||||
|
||||
### 先来一张近期的服务器提现订单
|
||||
|
||||

|
||||
|
||||
## 插件展示
|
||||
|
||||
> 多图预警 折叠了
|
||||
|
||||
`[spoiler]`
|
||||
|
||||
- 插件帮助
|
||||
- 扫码绑定
|
||||
- 查看个人信息
|
||||
- 兑换排行
|
||||
- 玩家兑换公告
|
||||
- 配置兑换比例
|
||||
- 服务器信息查询
|
||||
- 玩家请求授权
|
||||
- 玩家扫码
|
||||
- 玩家扫码确认
|
||||
- 玩家扫码兑换完成
|
||||
- 腐竹扫码确认
|
||||
- 腐竹扫码完成
|
||||
|
||||
`[/spoiler]`
|
||||
|
||||
## 插件命令
|
||||
```
|
||||
>mrd
|
||||
[17:56:54 INFO]: [MS][MRD] ====== [广告系统] 帮助菜单 ======
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd bind 绑定圈云盒子
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd query 查询当前账户
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd draw <兑换数量> 兑换点券
|
||||
[17:56:54 INFO]: [MS][MRD] 由于您是管理员 以为您展示额外命令
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd bind server 绑定服务器
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd ratio <兑换比例> 设置喵币/点券兑换比例
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd statistic 近期收入统计
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd rank <boardcast>(是否公告) 今日兑换排行
|
||||
[17:56:54 INFO]: [MS][MRD] /mrd server 当前服务器信息
|
||||
[17:56:54 INFO]: [MS][MRD] 兑换比例设置说明: 默认比例为 0.001 => 1000喵币 兑换 1点券
|
||||
[17:56:54 INFO]: [MS][MRD] 注意 设置比例后 玩家兑换点券数量不能少于 1点券
|
||||
[17:56:54 INFO]: [MS][MRD] 比如 设置了0.001 那就是 玩家至少 1000喵币 才能兑换!
|
||||
```
|
||||
|
||||
## 接下来就是赚钱的操作
|
||||
|
||||
## 插件安装/配置
|
||||
|
||||
### 基础准备工作
|
||||
- QQ搜索小程序 `圈云盒子` 并登录
|
||||
- 申请内测服务器账号
|
||||
- 个人中心 => 配置面板 => 圈云盒子客服群
|
||||
- 打开群详情 加群
|
||||
|
||||
### 申请账号
|
||||
- 打开 [圈云盒子管理后台](https://reward.yumc.pw)
|
||||
- 扫码登录 并且加入客服群
|
||||
- 创建一个服务器 等待管理员审核通过
|
||||
|
||||
`[hide]`
|
||||
|
||||
### 服务器准备工作
|
||||
- 安装 `MiaoScript` [站内地址](https://www.mcbbs.net/thread-774401-1-1.html)
|
||||
- 如果你已经安装了 Yum 可以通过 Yum 快速安装 `/yum install MiaoScript`
|
||||
- 等待 MiaoScript 引擎启动完成(首次启动可能需要10-20秒 依赖于您的网络环境)
|
||||
- 执行 `/mspm install MiaoReward` 安装 MiaoReward 脚本插件
|
||||
- 完成安装
|
||||
|
||||
### 绑定服务器
|
||||
- 执行 `/mrd bind server`
|
||||
- 使用QQ扫码 => 选择需要绑定的服务器 => 完成绑定
|
||||
- 配置服务器兑换比例
|
||||
- 执行 `/mrd ratio <兑换比例>` (最低为 1:0.0001)
|
||||
- 执行兑换比例确认指令
|
||||
- 执行 `/mrd server` 查看服务器信息
|
||||
- 如果群组服需要绑定多台服务器(前提是每个服务器都有玩家在)
|
||||
- 执行 `/gmspm install MiaoReward` 批量安装
|
||||
- 可以直接复制绑定之后的 `MiaoScript/plugins/MiaoReward/config.yml`
|
||||
- 黏贴配置文件到所有需要绑定的服务器上
|
||||
- 然后 `/gmspm reload MiaoReward` 批量重载插件
|
||||
|
||||
### QQ群配置
|
||||
- 打开小程序
|
||||
- 登录
|
||||
- 个人中心 => 设置面板 => 添加到群应用 => 选择你的QQ群
|
||||
- 配置群快捷栏 个人中心 => 设置面板 => 查看设置群快捷栏
|
||||
- 配置结果
|
||||
|
||||
### 玩家绑定账号
|
||||
- 执行 `/mrd bind`
|
||||
- 使用QQ扫码
|
||||
- 完成绑定
|
||||
- 执行 `/mrd query` 查询信息
|
||||
|
||||
## 使用说明
|
||||
- 玩家可以通过下列方式获取喵币
|
||||
- 签到 5-10 喵币
|
||||
- 视频广告 5-15 喵币
|
||||
- 盒子广告 5-15 喵币
|
||||
- 获得的喵币可以在服务器兑换成对应的奖励
|
||||
- 腐竹可以在平台将喵币兑换成RMB(兑换比例请加群联系客服详谈)
|
||||
- 登录小程序后 个人中心 => 配置面板 => 圈云盒子客服群
|
||||
|
||||
## PAPI兼容
|
||||
- 目前已经兼容 PAPI 变量
|
||||
- `%mrd_balance%` 玩家喵币
|
||||
- `%mrd_sign%` 玩家签到(*/1)
|
||||
- `%mrd_video%` 玩家视频观看次数(*/300)
|
||||
- `%mrd_box%` 玩家盒子观看次数(*/300)
|
||||
- 如果玩家变量没有及时更新 执行 `/mrd query` 或 `重新登录游戏` 即可
|
||||
|
||||
## 配置文件
|
||||
|
||||
```yml
|
||||
# 服务器ID
|
||||
serverId: 16
|
||||
# 服务器Token
|
||||
serverToken: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
||||
# 兑换指令
|
||||
drawCommand: 'p give %player_name% %amount%'
|
||||
# 冷却时间
|
||||
drawCooldown: 300
|
||||
# 提示前缀
|
||||
prefix: '§6[§b广告系统§6]§r'
|
||||
```
|
||||
|
||||
## 插件源码
|
||||
|
||||
- [MiaoScript包管理中心](https://git.yumc.pw/circlecloud/ms/src/branch/master/packages/plugins/src/MiaoReward.ts)
|
||||
|
||||
`[/hide]`
|
||||
|
||||
## 更新日志
|
||||
- 2020-10-12 完成提现功能
|
||||
- 2020-10-06 使用发包的方式展示地图
|
||||
|
||||
## Roadmap
|
||||
- 绑定服务器(已完成)
|
||||
- 绑定玩家(已完成)
|
||||
- 兑换喵币(已完成)
|
||||
- [自助开通](https://reward.yumc.pw)服务器(已完成)
|
||||
- [自助兑换](https://reward.yumc.pw)RMB(已完成)
|
||||
- [自助提现](https://reward.yumc.pw)(已完成)
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "@ccms/plugins",
|
||||
"version": "0.7.0",
|
||||
"version": "0.11.0",
|
||||
"description": "MiaoScript plugins package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -20,13 +20,19 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@javatypes/bukkit-api": "^0.0.3",
|
||||
"@javatypes/bungee-api": "^0.0.3",
|
||||
"@javatypes/sponge-api": "^0.0.3",
|
||||
"@javatypes/spring-data-redis": "^0.0.3",
|
||||
"@javatypes/spring-web": "^0.0.3",
|
||||
"@javatypes/tomcat": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0",
|
||||
"@ccms/plugin": "^0.7.0"
|
||||
"@ccms/api": "^0.11.0",
|
||||
"@ccms/container": "^0.11.0",
|
||||
"@ccms/plugin": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/// <reference types="@ccms/types/dist/typings/bukkit" />
|
||||
/// <reference types="@ccms/types/dist/typings/sponge" />
|
||||
/// <reference types="@ccms/types/dist/typings/bungee" />
|
||||
/// <reference types="@ccms/types/dist/typings/nukkit" />
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/spigot-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { server } from '@ccms/api';
|
||||
import { inject } from '@ccms/container';
|
||||
|
||||
78
packages/plugins/src/McbbsPlugin.ts
Normal file
78
packages/plugins/src/McbbsPlugin.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { plugin, server } from '@ccms/api'
|
||||
import { Autowired } from '@ccms/container'
|
||||
import { Cmd, interfaces, JSPlugin, Tab } from '@ccms/plugin'
|
||||
import { Server as SocketIOServer, Socket as SocketIOSocket, Namespace } from '@ccms/websocket'
|
||||
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
|
||||
const FileFilter = Java.type('java.io.FileFilter')
|
||||
const ByteArrayInputStream = java.io.ByteArrayInputStream
|
||||
const ByteArrayOutputStream = java.io.ByteArrayOutputStream
|
||||
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
|
||||
const GZIPInputStream = Java.type('java.util.zip.GZIPInputStream')
|
||||
const ByteArray = Java.type("byte[]")
|
||||
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
export class McbbsPlugin extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private Server: server.Server
|
||||
@Autowired()
|
||||
private pluginManager: plugin.PluginManager
|
||||
|
||||
private namespace: any
|
||||
|
||||
load() {
|
||||
}
|
||||
|
||||
enable() {
|
||||
let consolePlugin: any = this.pluginManager.getPlugin('MiaoConsole')
|
||||
if (consolePlugin.socketIOServer) {
|
||||
this.startWebSocketServer(consolePlugin.socketIOServer)
|
||||
} else {
|
||||
process.on('websocket.create', (server: SocketIOServer) => {
|
||||
this.startWebSocketServer(server)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private startWebSocketServer(server: SocketIOServer) {
|
||||
this.namespace = server.of('/McbbsPlugin')
|
||||
this.namespace.on('connect', (client: SocketIOSocket) => {
|
||||
this.initWebSocketClient(client)
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.namespace.close()
|
||||
}
|
||||
|
||||
private initWebSocketClient(client: SocketIOSocket) {
|
||||
client.on('ungzip', (content) => {
|
||||
|
||||
})
|
||||
|
||||
client.on('error', (error) => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
||||
this.logger.error(error)
|
||||
})
|
||||
client.on('disconnect', () => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
@Cmd()
|
||||
msme(sender: any, command: string, args: string[]) {
|
||||
this.logger.log(sender, command, args)
|
||||
sender.sendMessage(JSON.stringify({ command, ...args }))
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmsme(_sender: any, _command: string, _args: string[]) {
|
||||
return ['world']
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
/// <reference types="@ccms/types/dist/typings/bukkit" />
|
||||
/// <reference types="@ccms/types/dist/typings/sponge" />
|
||||
/// <reference types="@ccms/types/dist/typings/bungee" />
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/spigot-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { server, plugin as pluginApi, channel, constants } from '@ccms/api'
|
||||
import { inject, optional } from '@ccms/container';
|
||||
import { server, channel, constants, chat } from '@ccms/api'
|
||||
import { optional, Autowired } from '@ccms/container'
|
||||
import { plugin, interfaces, cmd, listener, tab, config, enable } from '@ccms/plugin'
|
||||
import Tellraw from '@ccms/common/dist/tellraw'
|
||||
|
||||
const ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream");
|
||||
const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream");
|
||||
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
|
||||
const GZIPInputStream = Java.type("java.util.zip.GZIPInputStream");
|
||||
const GZIPOutputStream = Java.type("java.util.zip.GZIPOutputStream");
|
||||
const ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream")
|
||||
const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream")
|
||||
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
|
||||
const GZIPInputStream = Java.type("java.util.zip.GZIPInputStream")
|
||||
const GZIPOutputStream = Java.type("java.util.zip.GZIPOutputStream")
|
||||
const BiConsumer = Java.type('java.util.function.BiConsumer')
|
||||
const ByteArray = Java.type("byte[]")
|
||||
|
||||
@@ -22,42 +22,45 @@ class MiaoMessage {
|
||||
private static MAX_MESSAGE_LENGTH = 32000;
|
||||
|
||||
private static copy(input, output) {
|
||||
let buffer = new ByteArray(1024);
|
||||
let n: number;
|
||||
let buffer = new ByteArray(1024)
|
||||
let n: number
|
||||
while ((n = input.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, n);
|
||||
output.write(buffer, 0, n)
|
||||
}
|
||||
input.close();
|
||||
output.close();
|
||||
input.close()
|
||||
output.close()
|
||||
}
|
||||
public static encode(input: any): any {
|
||||
return new MiaoMessage(input).encode();
|
||||
return new MiaoMessage(input).encode()
|
||||
}
|
||||
public static decode(input: any): MiaoMessage {
|
||||
let baos = new ByteArrayOutputStream();
|
||||
MiaoMessage.copy(new GZIPInputStream(new ByteArrayInputStream(input)), baos);
|
||||
return new MiaoMessage(baos.toString(StandardCharsets.UTF_8.name()));
|
||||
let baos = new ByteArrayOutputStream()
|
||||
MiaoMessage.copy(new GZIPInputStream(new ByteArrayInputStream(input)), baos)
|
||||
return new MiaoMessage(baos.toString(StandardCharsets.UTF_8.name()))
|
||||
}
|
||||
|
||||
// private String json;
|
||||
constructor(public json: any) { }
|
||||
|
||||
public encode(): any {
|
||||
let baos = new ByteArrayOutputStream();
|
||||
MiaoMessage.copy(new ByteArrayInputStream(this.json.getBytes(StandardCharsets.UTF_8)), new GZIPOutputStream(baos));
|
||||
if (baos.size() > MiaoMessage.MAX_MESSAGE_LENGTH) { return null; }
|
||||
return baos.toByteArray();
|
||||
let baos = new ByteArrayOutputStream()
|
||||
MiaoMessage.copy(new ByteArrayInputStream(this.json.getBytes(StandardCharsets.UTF_8)), new GZIPOutputStream(baos))
|
||||
if (baos.size() > MiaoMessage.MAX_MESSAGE_LENGTH) { return null }
|
||||
return baos.toByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
@plugin({ name: 'MiaoChat', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
export class MiaoChat extends interfaces.Plugin {
|
||||
@inject(server.Server)
|
||||
@Autowired()
|
||||
private Server: server.Server
|
||||
@inject(channel.Channel)
|
||||
@optional() private Channel: channel.Channel
|
||||
@optional()
|
||||
@Autowired()
|
||||
private chat: chat.Chat
|
||||
@Autowired()
|
||||
private Channel: channel.Channel
|
||||
|
||||
private channelOff: { off: () => void };
|
||||
private channelOff: { off: () => void }
|
||||
|
||||
@config()
|
||||
private config = {
|
||||
@@ -129,34 +132,34 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
private chatFormats: any[];
|
||||
private styleFormats: any;
|
||||
private chatFormats: any[]
|
||||
private styleFormats: any
|
||||
// 用于匹配 '[xx]' 聊天格式
|
||||
private FORMAT_PATTERN = /[\[]([^\[\]]+)[\]]/ig;
|
||||
|
||||
private PlaceholderAPI: { setPlaceholders: (player: any, str: string) => string };
|
||||
private PlaceholderAPI: { setPlaceholders: (player: any, str: string) => string }
|
||||
|
||||
load() {
|
||||
this.chatFormats = Object.values(this.config.ChatFormats);
|
||||
this.chatFormats.sort(this.compare('index'));
|
||||
this.initFormat(this.chatFormats);
|
||||
this.styleFormats = this.config.StyleFormats;
|
||||
this.chatFormats = Object.values(this.config.ChatFormats)
|
||||
this.chatFormats.sort(this.compare('index'))
|
||||
this.initFormat(this.chatFormats)
|
||||
this.styleFormats = this.config.StyleFormats
|
||||
}
|
||||
|
||||
private compare(prop: string) {
|
||||
return function (obj1: { [x: string]: any; }, obj2: { [x: string]: any; }) {
|
||||
var val1 = obj1[prop];
|
||||
var val2 = obj2[prop];
|
||||
return function (obj1: { [x: string]: any }, obj2: { [x: string]: any }) {
|
||||
var val1 = obj1[prop]
|
||||
var val2 = obj2[prop]
|
||||
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
|
||||
val1 = Number(val1);
|
||||
val2 = Number(val2);
|
||||
val1 = Number(val1)
|
||||
val2 = Number(val2)
|
||||
}
|
||||
if (val1 < val2) {
|
||||
return -1;
|
||||
return -1
|
||||
} else if (val1 > val2) {
|
||||
return 1;
|
||||
return 1
|
||||
} else {
|
||||
return 0;
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,7 +167,7 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
enable() {
|
||||
this.PlaceholderAPI = {
|
||||
setPlaceholders: (player: any, string: string) => {
|
||||
return string;
|
||||
return string
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,10 +177,14 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
bukkitenable() {
|
||||
if (!this.chat) {
|
||||
this.logger.console('§4消息管理器注入失败 请检查当前服务器是否兼容...')
|
||||
this.AsyncPlayerChatEvent['off']()
|
||||
}
|
||||
// 尝试加载 Bukkit 的 PlaceholderAPI
|
||||
try {
|
||||
//@ts-ignore
|
||||
this.PlaceholderAPI = base.getClass("me.clip.placeholderapi.PlaceholderAPI").static;
|
||||
this.PlaceholderAPI = base.getClass("me.clip.placeholderapi.PlaceholderAPI").static
|
||||
this.logger.log('[PAPI] Found Bukkit PlaceholderAPI Hooking...')
|
||||
} catch (ex) {
|
||||
this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex)
|
||||
@@ -185,16 +192,20 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
spongeenable() {
|
||||
if (!this.chat) {
|
||||
this.logger.console('§4消息管理器注入失败 请检查当前服务器是否兼容...')
|
||||
this.MessageChannelEvent$Chat['off']()
|
||||
}
|
||||
// 尝试加载 Sponge 的 PlaceholderAPI
|
||||
try {
|
||||
var spongePapi = this.Server.getService('me.rojo8399.placeholderapi.PlaceholderService');
|
||||
var s = org.spongepowered.api.text.serializer.TextSerializers.formattingCode('§');
|
||||
var spongePapi = this.Server.getService('me.rojo8399.placeholderapi.PlaceholderService')
|
||||
var s = org.spongepowered.api.text.serializer.TextSerializers.formattingCode('§')
|
||||
if (spongePapi) {
|
||||
this.PlaceholderAPI = {
|
||||
setPlaceholders: (player: any, string: string) => {
|
||||
return s.serialize(spongePapi.replacePlaceholders(string, player, player));
|
||||
return s.serialize(spongePapi.replacePlaceholders(string, player, player))
|
||||
}
|
||||
};
|
||||
}
|
||||
this.logger.log('[PAPI] Found Sponge PlaceholderAPI Hooking...')
|
||||
}
|
||||
} catch (ex) {
|
||||
@@ -213,7 +224,7 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data, event: net.md_5.bungee.api.event.PluginMessageEvent) => {
|
||||
let bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
||||
if (event.getTag() == MiaoMessage.CHANNEL) {
|
||||
let origin = event.getSender().getAddress();
|
||||
let origin = event.getSender().getAddress()
|
||||
bungee.getServers().forEach(new BiConsumer({
|
||||
accept: (s, server) => {
|
||||
if (server.getAddress() != origin && server.getPlayers().size() > 0) {
|
||||
@@ -225,15 +236,15 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
})
|
||||
}
|
||||
|
||||
@cmd({ servers: ["bungee"] })
|
||||
@cmd({ servers: [constants.ServerType.Bungee] })
|
||||
mct(sender: any, command: string, args: string[]) {
|
||||
this.logger.log(sender, command, args);
|
||||
this.logger.log(sender, command, args)
|
||||
sender.sendMessage(JSON.stringify({ command, ...args }))
|
||||
}
|
||||
|
||||
@cmd({ servers: ["!bungee"] })
|
||||
@cmd({ servers: [`!${constants.ServerType.Bungee}`] })
|
||||
mchat(sender: any, command: string, args: string[]) {
|
||||
this.logger.log(sender, command, args);
|
||||
this.logger.log(sender, command, args)
|
||||
sender.sendMessage(JSON.stringify({ command, ...args }))
|
||||
}
|
||||
|
||||
@@ -241,104 +252,99 @@ export class MiaoChat extends interfaces.Plugin {
|
||||
tabmchat(_sender: any, _command: string, _args: string[]) {
|
||||
}
|
||||
|
||||
@listener({ servers: ['bukkit'] })
|
||||
@listener({ servers: [constants.ServerType.Bukkit] })
|
||||
AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) {
|
||||
this.sendChat(event.getPlayer(), event.getMessage(), function () {
|
||||
event.setCancelled(true);
|
||||
});
|
||||
this.sendChat(event.getPlayer(), event.getMessage(), () => event.setCancelled(true))
|
||||
}
|
||||
|
||||
@listener({ servers: ['sponge'] })
|
||||
@listener({ servers: [constants.ServerType.Sponge] })
|
||||
MessageChannelEvent$Chat(event: org.spongepowered.api.event.message.MessageChannelEvent.Chat) {
|
||||
//@ts-ignore
|
||||
var player = event.getCause().first(org.spongepowered.api.entity.living.player.Player.class).orElse(null);
|
||||
var player = event.getCause().first(org.spongepowered.api.entity.living.player.Player.class).orElse(null)
|
||||
if (player == null) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
var plain = event.getRawMessage().toPlain();
|
||||
var plain = event.getRawMessage().toPlain()
|
||||
if (plain.startsWith(Tellraw.duplicateChar)) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
this.sendChat(player, plain, function () {
|
||||
event.setMessageCancelled(true)
|
||||
});
|
||||
this.sendChat(player, plain, () => event.setMessageCancelled(true))
|
||||
}
|
||||
|
||||
initFormat(chatFormats: any[]) {
|
||||
chatFormats.forEach(chatFormat => {
|
||||
var chat_format_str = chatFormat.format;
|
||||
var temp = [];
|
||||
var r: any[];
|
||||
var chat_format_str = chatFormat.format
|
||||
var temp = []
|
||||
var r: any[]
|
||||
while (r = this.FORMAT_PATTERN.exec(chat_format_str)) {
|
||||
temp.push(r[1]);
|
||||
temp.push(r[1])
|
||||
}
|
||||
var format_list = [];
|
||||
var format_list = []
|
||||
temp.forEach(t => {
|
||||
var arr = chat_format_str.split('[' + t + ']', 2);
|
||||
var arr = chat_format_str.split('[' + t + ']', 2)
|
||||
if (arr[0]) {
|
||||
format_list.push(arr[0]);
|
||||
format_list.push(arr[0])
|
||||
}
|
||||
format_list.push(t);
|
||||
chat_format_str = arr[1];
|
||||
});
|
||||
format_list.push(t)
|
||||
chat_format_str = arr[1]
|
||||
})
|
||||
if (chat_format_str) {
|
||||
format_list.push(chat_format_str);
|
||||
format_list.push(chat_format_str)
|
||||
}
|
||||
chatFormat.format_list = format_list;
|
||||
chatFormat.format_list = format_list
|
||||
})
|
||||
}
|
||||
|
||||
sendChat(player: any, plain: string, callback: { (): void; }) {
|
||||
var chat_format = this.getChatFormat(player);
|
||||
sendChat(player: any, plain: string, callback: { (): void }) {
|
||||
var chat_format = this.getChatFormat(player)
|
||||
if (!chat_format) {
|
||||
console.debug('未获得用户', player.getName(), '的 ChatRule 跳过执行...');
|
||||
return;
|
||||
console.debug('未获得用户', player.getName(), '的 ChatRule 跳过执行...')
|
||||
return
|
||||
}
|
||||
callback();
|
||||
var tr = Tellraw.create();
|
||||
callback()
|
||||
var tr = Tellraw.create()
|
||||
chat_format.format_list.forEach((format) => {
|
||||
var style = this.styleFormats[format];
|
||||
var style = this.styleFormats[format]
|
||||
if (style) {
|
||||
tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')));
|
||||
tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')))
|
||||
if (style.hover) {
|
||||
tr.tip(this.replace(player, style.hover.join('\n')));
|
||||
tr.tip(this.replace(player, style.hover.join('\n')))
|
||||
}
|
||||
if (style.click && style.click.type && style.click.command) {
|
||||
var command = this.replace(player, style.click.command);
|
||||
var command = this.replace(player, style.click.command)
|
||||
switch (style.click.type.toUpperCase()) {
|
||||
case "COMMAND":
|
||||
tr.command(command);
|
||||
break;
|
||||
tr.command(command)
|
||||
break
|
||||
case "OPENURL":
|
||||
tr.link(command);
|
||||
break;
|
||||
tr.link(command)
|
||||
break
|
||||
case "SUGGEST":
|
||||
tr.suggest(command);
|
||||
break;
|
||||
tr.suggest(command)
|
||||
break
|
||||
default:
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tr.then(this.replace(player, format));
|
||||
tr.then(this.replace(player, format))
|
||||
}
|
||||
});
|
||||
})
|
||||
let json = tr.then(this.replace(player, plain)).json()
|
||||
this.sendChatAll(json)
|
||||
this.Channel?.send(player, MiaoMessage.CHANNEL, MiaoMessage.encode(json))
|
||||
}
|
||||
|
||||
sendChatAll(json: string) {
|
||||
this.Server.getOnlinePlayers().forEach(player => this.Server.sendJson(player, json))
|
||||
this.Server.getOnlinePlayers().forEach(player => this.chat.sendJson(player, json))
|
||||
}
|
||||
|
||||
getChatFormat(player: any) {
|
||||
for (var i in this.chatFormats) {
|
||||
var format = this.chatFormats[i];
|
||||
for (let format of this.chatFormats) {
|
||||
if (player.hasPermission(format.permission)) {
|
||||
return format;
|
||||
return format
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
replace(player: any, target: string) {
|
||||
|
||||
105
packages/plugins/src/MiaoCoin.ts
Normal file
105
packages/plugins/src/MiaoCoin.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { constants, task, server, channel } from "@ccms/api"
|
||||
import { JSPlugin, interfaces, Cmd, Tab, Listener, Config } from "@ccms/plugin"
|
||||
|
||||
import { QRCode, QRErrorCorrectLevel } from '@ccms/common/dist/qrcode'
|
||||
|
||||
import { inject, JSClass, optional } from '@ccms/container'
|
||||
import http from '@ccms/common/dist/http'
|
||||
import chat from '@ccms/bukkit/dist/enhance/chat'
|
||||
|
||||
const MapView = Java.type('org.bukkit.map.MapView')
|
||||
const Bukkit = Java.type('org.bukkit.Bukkit')
|
||||
const MapRenderer = Java.type('org.bukkit.map.MapRenderer')
|
||||
const ItemStack = Java.type('org.bukkit.inventory.ItemStack')
|
||||
const Material = Java.type('org.bukkit.Material')
|
||||
|
||||
const BufferedImage = Java.type('java.awt.image.BufferedImage')
|
||||
const Graphics2D = Java.type('java.awt.Graphics2D')
|
||||
const Color = Java.type('java.awt.Color')
|
||||
const Image = Java.type('java.awt.Image')
|
||||
const Font = Java.type('java.awt.Font')
|
||||
const RenderingHints = Java.type('java.awt.RenderingHints')
|
||||
|
||||
class QRCodeRender {
|
||||
private _proxy
|
||||
constructor(image: any) {
|
||||
const ProxyAdapter = Java.extend(MapRenderer, {
|
||||
render: (_mapView, mapCanvas) => {
|
||||
mapCanvas.drawImage(0, 0, image)
|
||||
}
|
||||
})
|
||||
this._proxy = new ProxyAdapter()
|
||||
}
|
||||
getHandler() {
|
||||
return this._proxy
|
||||
}
|
||||
}
|
||||
|
||||
interface PlaceholderAPI {
|
||||
registerPlaceholderHook: (key: string, onPlaceholderRequest: (player, s) => string) => void
|
||||
setPlaceholders: (player: any, str: string) => string
|
||||
}
|
||||
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename })
|
||||
export class MiaoCoin extends interfaces.Plugin {
|
||||
@inject(server.Server)
|
||||
private server: server.Server
|
||||
@inject(task.TaskManager)
|
||||
private taskManager: task.TaskManager
|
||||
|
||||
@JSClass('me.clip.placeholderapi.PlaceholderAPI')
|
||||
private PlaceholderAPI: PlaceholderAPI
|
||||
@JSClass('me.clip.placeholderapi.PlaceholderHook')
|
||||
private PlaceholderHook: any
|
||||
|
||||
@Config()
|
||||
private config = {
|
||||
MySQL: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
enable() {
|
||||
if (!this.PlaceholderAPI) {
|
||||
console.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced!")
|
||||
} else {
|
||||
this.PlaceholderAPI.registerPlaceholderHook("mcn", new this.PlaceholderHook({
|
||||
onPlaceholderRequest: (player: any, s: string) => {
|
||||
switch (s.toLowerCase()) {
|
||||
case "has":
|
||||
return ''
|
||||
case "add":
|
||||
return ''
|
||||
case "remove":
|
||||
return ''
|
||||
default:
|
||||
}
|
||||
return `未知的参数: ${s}`
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
disable() {
|
||||
// {"app":"com.tencent.autoreply","desc":"","view":"autoreply","ver":"0.0.0.1","prompt":"[动画表情]","meta":{"metadata":{"title":"点击蓝色字体有惊喜","buttons":[{"slot":1,"action_data":"我是傻逼","name":"点我","action":"notify"}],"type":"guest","token":"LAcV49xqyE57S17B8ZT6FU7odBveNMYJzux288tBD3c="}},"config":{"forward":1,"showSender":1}}
|
||||
}
|
||||
|
||||
@Cmd()
|
||||
mrd(sender: any, command: string, args: string[]) {
|
||||
let cmd = args[0] || 'help'
|
||||
let cmdKey = 'cmd' + cmd
|
||||
if (!this[cmdKey]) {
|
||||
console.sender(sender, '§4未知的子命令: §c' + cmd)
|
||||
console.sender(sender, `§6请执行 §b/${command} §ahelp §6查看帮助!`)
|
||||
return
|
||||
}
|
||||
args.shift()
|
||||
this.taskManager.create(() => this[cmdKey](sender, ...args)).async().submit()
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmrd(sender: any, _command: any, args: string | any[]) {
|
||||
if (args.length === 1) return ['help', 'bind', 'show', 'query', 'draw', "ratio"]
|
||||
if (args.length === 2 && args[0] === "bind" && sender.isOp()) return ['server']
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@ccms/types/dist/typings/bukkit" />
|
||||
/// <reference types="@ccms/types/dist/typings/sponge" />
|
||||
/// <reference types="@ccms/types/dist/typings/bungee" />
|
||||
|
||||
import { plugin as pluginApi, server, task, constants } from '@ccms/api'
|
||||
import { plugin, interfaces, cmd, tab, enable, config, disable } from '@ccms/plugin'
|
||||
import { inject, ContainerInstance, Container } from '@ccms/container'
|
||||
import { plugin as pluginApi, server, task, constants, command } from '@ccms/api'
|
||||
import { plugin, interfaces, cmd, tab, enable, config, disable, PluginConfig } from '@ccms/plugin'
|
||||
import { ContainerInstance, Container, Autowired } from '@ccms/container'
|
||||
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket'
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
@@ -22,20 +19,22 @@ let help = [
|
||||
'§6/mconsole §areload §6- §3重载插件',
|
||||
]
|
||||
|
||||
@plugin({ name: 'MiaoConsole', prefix: 'Console', version: '1.0.0', author: 'MiaoWoo', servers: ['!nukkit'], source: __filename })
|
||||
@plugin({ prefix: 'Console', version: '1.0.0', author: 'MiaoWoo', servers: ['!nukkit'], source: __filename })
|
||||
export class MiaoConsole extends interfaces.Plugin {
|
||||
@inject(ContainerInstance)
|
||||
@Autowired(ContainerInstance)
|
||||
private container: Container
|
||||
@inject(server.ServerType)
|
||||
@Autowired(server.ServerType)
|
||||
private serverType: string
|
||||
@inject(server.Server)
|
||||
private server: server.Server
|
||||
@inject(task.TaskManager)
|
||||
private task: task.TaskManager
|
||||
@inject(pluginApi.PluginManager)
|
||||
private pluginManager: pluginApi.PluginManager
|
||||
@inject(pluginApi.PluginFolder)
|
||||
@Autowired(pluginApi.PluginFolder)
|
||||
private pluginFolder: string
|
||||
@Autowired()
|
||||
private server: server.Server
|
||||
@Autowired()
|
||||
private command: command.Command
|
||||
@Autowired()
|
||||
private task: task.TaskManager
|
||||
@Autowired()
|
||||
private pluginManager: pluginApi.PluginManager
|
||||
|
||||
private token: string
|
||||
private instance: any
|
||||
@@ -47,7 +46,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
private logCache: string[] = []
|
||||
|
||||
@config()
|
||||
private secret = { token: undefined }
|
||||
private secret: PluginConfig = { token: undefined }
|
||||
|
||||
load() {
|
||||
if (this.secret.token) {
|
||||
@@ -91,6 +90,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
cmdtoken(sender: any, sub: string, token: string) {
|
||||
if (sub == "set") {
|
||||
this.secret.token = this.token = token
|
||||
this.secret.save()
|
||||
this.logger.sender(sender, '§a已保存§6服务器登录Token:§3', this.token, '§4请勿分享给其他人 防止服务器被攻击!')
|
||||
return
|
||||
}
|
||||
@@ -186,8 +186,8 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
@disable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
||||
removeLog4jForward() {
|
||||
try {
|
||||
this.appender.stop()
|
||||
this.rootLogger.removeAppender(this.appender)
|
||||
this.appender?.stop()
|
||||
this.rootLogger?.removeAppender(this.appender)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
@@ -196,7 +196,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
@disable({ servers: [constants.ServerType.Bungee] })
|
||||
removeLoggerForward() {
|
||||
try {
|
||||
this.rootLogger.removeHandler(this.handler)
|
||||
this.rootLogger?.removeHandler(this.handler)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
@@ -205,7 +205,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
@disable({ servers: [constants.ServerType.Spring] })
|
||||
removeLogbackForward() {
|
||||
try {
|
||||
this.rootLogger.detachAppender(this.LOGBACK_APPENDER_NAME)
|
||||
this.rootLogger?.detachAppender(this.LOGBACK_APPENDER_NAME)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
@@ -217,6 +217,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
root: fs.concat(root, 'wwwroot')
|
||||
})
|
||||
this.container.bind(io.Instance).toConstantValue(this.socketIOServer)
|
||||
process.emit('websocket.create', this.socketIOServer)
|
||||
}
|
||||
|
||||
startSocketIOServer() {
|
||||
@@ -225,20 +226,19 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
namespace.on('connect', (client: SocketIOSocket) => {
|
||||
if (!this.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
||||
client.emit('unauthorized')
|
||||
client.disconnect(true)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
if (this.token != client.handshake.query.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
||||
client.emit('unauthorized')
|
||||
client.disconnect(true)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
this.initWebSocketClient(client)
|
||||
this.logCache.forEach(msg => client.emit('log', msg))
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
||||
})
|
||||
process.emit('websocket.start', this.socketIOServer)
|
||||
}
|
||||
|
||||
private initWebSocketClient(client: SocketIOSocket) {
|
||||
@@ -251,7 +251,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
client.emit('log', `§6命令: §b${cmd} §a执行成功!`)
|
||||
})
|
||||
client.on('tabComplate', (input, index, callback) => {
|
||||
callback && callback(this.server.tabComplete(this.server.getConsoleSender(), input, index))
|
||||
callback?.(this.command.tabComplete(this.server.getConsoleSender(), input, index) || [])
|
||||
})
|
||||
client.on('exec', (code) => {
|
||||
try {
|
||||
@@ -281,7 +281,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
if (!dir.isDirectory()) {
|
||||
return fn(undefined, `${file} 不是一个目录!`)
|
||||
}
|
||||
fn(fs.list(dir))
|
||||
fn(fs.list(dir).map(f => f.name))
|
||||
})
|
||||
client.on('error', (error) => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
||||
|
||||
175
packages/plugins/src/MiaoExplorer.ts
Normal file
175
packages/plugins/src/MiaoExplorer.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { plugin, server } from '@ccms/api'
|
||||
import { Autowired } from '@ccms/container'
|
||||
import { Cmd, interfaces, JSPlugin, Tab } from '@ccms/plugin'
|
||||
import { Server as SocketIOServer, Socket as SocketIOSocket, Namespace } from '@ccms/websocket'
|
||||
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
|
||||
const FileFilter = Java.type('java.io.FileFilter')
|
||||
const ByteArrayInputStream = java.io.ByteArrayInputStream
|
||||
const ByteArrayOutputStream = java.io.ByteArrayOutputStream
|
||||
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
|
||||
const GZIPInputStream = Java.type('java.util.zip.GZIPInputStream')
|
||||
const ByteArray = Java.type("byte[]")
|
||||
|
||||
@JSPlugin({ prefix: 'Explorer', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
export class MiaoExplorer extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private Server: server.Server
|
||||
@Autowired()
|
||||
private pluginManager: plugin.PluginManager
|
||||
|
||||
private token: string
|
||||
private namespace: any
|
||||
private chunkCacheMap: Map<string, Array<string>>
|
||||
|
||||
load() {
|
||||
this.chunkCacheMap = new Map()
|
||||
}
|
||||
|
||||
enable() {
|
||||
let consolePlugin: any = this.pluginManager.getPlugin('MiaoConsole')
|
||||
if (consolePlugin.socketIOServer) {
|
||||
this.startWebSocketServer(consolePlugin.socketIOServer)
|
||||
} else {
|
||||
process.on('websocket.create', (server: SocketIOServer) => {
|
||||
this.startWebSocketServer(server)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private startWebSocketServer(server: SocketIOServer) {
|
||||
let consolePlugin: any = this.pluginManager.getPlugin('MiaoConsole')
|
||||
this.token = consolePlugin.token
|
||||
this.namespace = server.of('/MiaoExplorer')
|
||||
this.namespace.on('connect', (client: SocketIOSocket) => {
|
||||
if (!this.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
if (this.token != client.handshake.query.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
this.initWebSocketClient(client)
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.namespace.removeAllListeners('connect')
|
||||
this.namespace.close()
|
||||
}
|
||||
|
||||
private readDir(dir) {
|
||||
let children = Java.from(dir.listFiles(new FileFilter({
|
||||
accept: file => file.getName().endsWith('.yml') || file.isDirectory()
|
||||
}))).sort().map(file => {
|
||||
if (file.isDirectory()) {
|
||||
let children = this.readDir(file)
|
||||
if (!children.length) { return null }
|
||||
return {
|
||||
label: file.getName(),
|
||||
value: fs.path(file),
|
||||
children,
|
||||
disabled: true
|
||||
}
|
||||
}
|
||||
return {
|
||||
label: file.getName(),
|
||||
value: fs.path(file),
|
||||
creatable: true,
|
||||
editable: true,
|
||||
removable: true,
|
||||
deleteApi: "delete"
|
||||
}
|
||||
})
|
||||
return children.filter(s => s)
|
||||
}
|
||||
|
||||
private initWebSocketClient(client: SocketIOSocket) {
|
||||
client.on('ls', (fn) => {
|
||||
var pluginDirs = Java.from(fs.file('./plugins').listFiles(new FileFilter({
|
||||
accept: dirs => dirs.isDirectory()
|
||||
})))
|
||||
let result = {
|
||||
options: pluginDirs.sort().map(f => {
|
||||
let node: any = {
|
||||
label: f.getName(),
|
||||
value: fs.path(f)
|
||||
}
|
||||
let children = this.readDir(f)
|
||||
if (!children.length) {
|
||||
return null
|
||||
}
|
||||
node.children = children
|
||||
node.disabled = true
|
||||
return node
|
||||
})
|
||||
}
|
||||
fn && fn(result.options.filter(s => s))
|
||||
})
|
||||
client.on('read', (file, callback) => {
|
||||
callback?.(base.read(file))
|
||||
})
|
||||
client.on('chunk', (file, index, content, callback) => {
|
||||
if (!this.chunkCacheMap.has(file)) {
|
||||
this.chunkCacheMap.set(file, [])
|
||||
}
|
||||
this.chunkCacheMap.get(file)[index] = content
|
||||
callback?.(true)
|
||||
})
|
||||
client.on('ungzip', (content) => {
|
||||
try {
|
||||
console.log(content)
|
||||
let baos = new ByteArrayOutputStream()
|
||||
let byte = java.util.Base64.getDecoder().decode(content)
|
||||
let gzip = new GZIPInputStream(new ByteArrayInputStream(byte))
|
||||
let buffer = new ByteArray(1024)
|
||||
let n: number
|
||||
while ((n = gzip.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, n)
|
||||
}
|
||||
gzip.close()
|
||||
baos.close()
|
||||
content = baos.toString(StandardCharsets.UTF_8.name())
|
||||
this.logger.log(content)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
})
|
||||
client.on('save', (file, callback) => {
|
||||
if (!this.chunkCacheMap.has(file)) {
|
||||
return callback?.(false)
|
||||
}
|
||||
let content = this.chunkCacheMap.get(file).join('')
|
||||
this.chunkCacheMap.delete(file)
|
||||
base.save(file, content)
|
||||
callback?.(true)
|
||||
})
|
||||
client.on('error', (error) => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
||||
this.logger.error(error)
|
||||
})
|
||||
client.on('disconnect', () => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
@Cmd()
|
||||
msme(sender: any, command: string, args: string[]) {
|
||||
this.logger.log(sender, command, args)
|
||||
sender.sendMessage(JSON.stringify({ command, ...args }))
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmsme(_sender: any, _command: string, _args: string[]) {
|
||||
return ['world']
|
||||
}
|
||||
}
|
||||
87
packages/plugins/src/MiaoGUI.ts
Normal file
87
packages/plugins/src/MiaoGUI.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { constants, channel, server } from "@ccms/api"
|
||||
import { JSPlugin, interfaces, Cmd, Tab } from "@ccms/plugin"
|
||||
import { inject, optional } from '@ccms/container'
|
||||
|
||||
const StandardCharsets = Java.type('java.nio.charset.StandardCharsets')
|
||||
const JavaString = Java.type('java.lang.String')
|
||||
|
||||
@JSPlugin({ author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename })
|
||||
export class MiaoGUI extends interfaces.Plugin {
|
||||
@inject(server.Server)
|
||||
private server: server.Server
|
||||
@inject(channel.Channel)
|
||||
@optional()
|
||||
private channel: channel.Channel
|
||||
|
||||
private CHANNEL_NAME = `${MiaoGUI.name}:NORMAL`.toLowerCase();
|
||||
private channelOff: { off: () => void }
|
||||
|
||||
enable() {
|
||||
this.channelOff = this.channel?.listen(this, this.CHANNEL_NAME, (data) => {
|
||||
this.logger.info('MiaoGUI', new JavaString(data))
|
||||
})
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.channelOff?.off()
|
||||
}
|
||||
|
||||
@Cmd()
|
||||
mgui(sender: any, command: string, args: string[]) {
|
||||
let cmd = args[0] || 'help'
|
||||
let cmdKey = 'cmd' + cmd
|
||||
if (!this[cmdKey]) {
|
||||
console.sender(sender, '§4未知的子命令: §c' + cmd)
|
||||
console.sender(sender, `§6请执行 §b/${command} §ahelp §6查看帮助!`)
|
||||
return
|
||||
}
|
||||
args.shift()
|
||||
this[cmdKey](sender, ...args)
|
||||
}
|
||||
|
||||
cmdhhelp(sender: any) {
|
||||
this.logger.sender(sender, '')
|
||||
}
|
||||
|
||||
cmdrun(sender: any, ...args: string[]) {
|
||||
this.send(sender, "run", { cmd: args.join(' ') })
|
||||
}
|
||||
|
||||
cmdconfig(sender: org.bukkit.entity.Player) {
|
||||
this.send(sender, "config")
|
||||
}
|
||||
|
||||
cmdopen(sender: any, name: string) {
|
||||
this.send(sender, "open", { name })
|
||||
}
|
||||
|
||||
cmdopenfor(sender: any, target: string, name: string) {
|
||||
let player = this.server.getPlayer(target)
|
||||
if (!player) { return this.logger.sender(sender, `§c玩家 ${target} 不在线或不存在!`) }
|
||||
this.send(player, "open", { name })
|
||||
}
|
||||
|
||||
cmdadd(sender: any, type: string, ...param: string[]) {
|
||||
if (!type) return this.logger.sender(sender, '§c组件类型不得为空!')
|
||||
this.send(sender, "add", { type, param })
|
||||
}
|
||||
|
||||
cmdclear(sender: org.bukkit.entity.Player) {
|
||||
this.send(sender, "clear")
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmgui(args: string[]) {
|
||||
if (args.length == 1) return ['open', 'openfor', 'config', 'add', 'clear']
|
||||
if (args.length == 2) {
|
||||
switch (args[0]) {
|
||||
case "add":
|
||||
return ['text', 'image', 'broadcast']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private send(target: any, type: string, data: any = {}) {
|
||||
this.channel?.send(target, this.CHANNEL_NAME, new JavaString(JSON.stringify({ type, data })).getBytes(StandardCharsets.UTF_8))
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/// <reference types="@ccms/types" />
|
||||
/// <reference types="@javatypes/jdk" />
|
||||
|
||||
import { task, server } from "@ccms/api";
|
||||
import { inject } from "@ccms/container";
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user