feat: add spring support

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-05-26 15:53:41 +08:00
parent 58478116c6
commit 72bd6b4c9c
15 changed files with 333 additions and 40 deletions

1
packages/spring/.npmignore Symbolic link
View File

@ -0,0 +1 @@
../../.npmignore

View File

@ -0,0 +1,31 @@
{
"name": "@ccms/spring",
"version": "0.6.7",
"description": "MiaoScript spring 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"
},
"devDependencies": {
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"typescript": "^3.9.2"
},
"dependencies": {
"@ccms/api": "^0.6.7",
"@ccms/common": "^0.6.7",
"@ccms/container": "^0.6.7"
}
}

View File

@ -0,0 +1,23 @@
import '@ccms/nashorn'
import { command, plugin } from '@ccms/api'
import { inject, provideSingleton, postConstruct } from '@ccms/container'
@provideSingleton(command.Command)
export class SpringCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
protected create(plugin: any, command: string) {
console.console('§4Spring暂不支持create命令!')
}
protected remove(plugin: any, command: string) {
console.console('§4Spring暂不支持remove命令!')
}
protected onCommand(plugin: any, command: any, executor: Function) {
console.console('§4Spring暂不支持onCommand!')
}
protected onTabComplete(plugin: any, command: any, tabCompleter: Function) {
console.console('§4Spring暂不支持onTabComplete!')
}
}

View File

@ -0,0 +1,41 @@
import { MiaoScriptConsole } from '@ccms/api'
var colorMap = []
colorMap['0'] = '38;5;0'
colorMap['1'] = '38;5;4'
colorMap['2'] = '38;5;2'
colorMap['3'] = '38;5;6'
colorMap['4'] = '38;5;1'
colorMap['5'] = '38;5;5'
colorMap['6'] = '38;5;3'
colorMap['7'] = '38;5;7'
colorMap['8'] = '38;5;8'
colorMap['9'] = '38;5;12'
colorMap['a'] = '38;5;10'
colorMap['b'] = '38;5;14'
colorMap['c'] = '38;5;9'
colorMap['d'] = '38;5;13'
colorMap['e'] = '38;5;11'
colorMap['f'] = '38;5;15'
colorMap['r'] = '0'
colorMap['l'] = '1'
colorMap['n'] = '4'
var regexMap = []
for (const c in colorMap) {
regexMap[colorMap[c]] = new RegExp(`§${c}`, "g")
}
function mcColor2ANSI(str: string) {
for (const regex in regexMap) {
str = str.replace(regexMap[regex], `\u001b[${regex}m`)
}
return str;
}
export class SpringConsole extends MiaoScriptConsole {
sender(sender: any, ...args: any[]) {
this.console(args.join(' '))
}
console(...args: string[]): void {
this.logger.info(mcColor2ANSI(args.join(' ') + '§r'))
}
}

View File

@ -0,0 +1,24 @@
import { event, plugin } from '@ccms/api'
import { inject, provideSingleton } from '@ccms/container'
@provideSingleton(event.Event)
export class SpringEvent extends event.Event {
@inject(plugin.PluginInstance)
private pluginInstance: any
constructor() {
super('');
}
mapEventName() {
return 0;
}
isValidEvent(clazz: any): boolean {
throw new Error("Method not implemented.");
}
register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean) {
throw new Error("Method not implemented.");
}
unregister(event: any, listener: any): void {
throw new Error("Method not implemented.");
}
}

View File

@ -0,0 +1,14 @@
import { server, plugin } from '@ccms/api'
import { Container } from '@ccms/container'
import { SpringConsole } from './console';
import './event';
import './server';
import './command';
import './task';
const BeanKit = Java.type('com.sixi.micro.common.kits.BeanKit')
export default function SpringImpl(container: Container) {
container.bind(server.Console).toConstantValue(SpringConsole)
}

View File

@ -0,0 +1,50 @@
import { server } from '@ccms/api'
import { provideSingleton } from '@ccms/container';
import { NativePluginManager } from '@ccms/api/dist/interfaces/server/native_plugin';
@provideSingleton(server.Server)
export class SpringServer implements server.Server {
constructor() {
}
getVersion(): string {
return "SpringFramework"
}
getPlayer(name: string) {
throw new Error("Method not implemented.");
}
getOnlinePlayers(): any[] {
throw new Error("Method not implemented.");
}
getConsoleSender() {
return undefined;
}
getService(service: string) {
throw new Error("Method not implemented.");
}
dispatchCommand(sender: any, command: string): boolean {
console.console('§4Spring暂不支持dispatchCommand!')
return false;
}
dispatchConsoleCommand(command: string): boolean {
console.console('§4Spring暂不支持dispatchConsoleCommand!')
return false;
}
getPluginsFolder(): string {
throw new Error("Method not implemented.");
}
getNativePluginManager(): NativePluginManager {
throw new Error("Method not implemented.");
}
getNettyPipeline() {
return base.getInstance().getAutowireCapableBeanFactory()
}
getRootLogger() {
return global.logger
}
sendJson(sender: any, json: string | object): void {
throw new Error("Method not implemented.");
}
tabComplete(sender: any, input: string, index?: number) {
throw new Error("Method not implemented.");
}
}

View File

@ -0,0 +1,88 @@
import { task, plugin } from '@ccms/api'
import { inject, provideSingleton } from '@ccms/container'
const AtomicBoolean = Java.type("java.util.concurrent.atomic.AtomicBoolean")
const Thread = Java.type('java.lang.Thread')
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
const ThreadPoolTaskExecutor = Java.type('org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor')
let executor: any
let tasks: { [key: number]: task.Cancelable } = {}
let taskId = 0
@provideSingleton(task.TaskManager)
export class SpringTaskManager implements task.TaskManager {
@inject(plugin.PluginInstance)
private pluginInstance: any
constructor() {
executor = new ThreadPoolTaskExecutor()
executor.setCorePoolSize(10)
executor.setMaxPoolSize(100)
executor.setQueueCapacity(500)
executor.setKeepAliveSeconds(60)
executor.setThreadNamePrefix("@ccms/spring-")
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy())
executor.initialize()
}
create(func: Function): task.Task {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !') }
return new SpringTask(this.pluginInstance, func)
}
callSyncMethod(func: Function): any {
return func()
}
disable() {
Object.values(tasks).forEach((task) => task.cancel())
executor.shutdown();
}
}
export class SpringTask extends task.Task {
public id = taskId++
private running = new AtomicBoolean(true)
run() {
if (this.laterTime > 0) {
try {
Thread.sleep(this.laterTime)
} catch (ex) {
Thread.currentThread().interrupt()
}
}
while (this.running.get()) {
try {
this.func()
} catch (t) {
console.error("Task exec error:", t)
console.ex(t)
}
// If we have a interval of 0 or less, only run once
if (this.interval <= 0) { break }
try {
Thread.sleep(this.interval)
} catch (ex) {
Thread.currentThread().interrupt()
}
}
this.cancel()
}
cancel(): any {
var wasRunning = this.running.getAndSet(false)
if (wasRunning) {
tasks[this.id] = undefined
}
}
submit(): task.Cancelable {
tasks[this.id] = this
executor.execute(this.run.bind(this))
return {
cancel: () => {
return this.cancel()
}
}
}
}

View File

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "src",
"outDir": "dist"
}
}