Init: Create & Init ms Project...
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
4
packages/api/.gitignore
vendored
Normal file
4
packages/api/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules
|
||||
/dist
|
||||
/package-lock.json
|
||||
/yarn.lock
|
22
packages/api/.npmignore
Normal file
22
packages/api/.npmignore
Normal file
@ -0,0 +1,22 @@
|
||||
src
|
||||
test
|
||||
typings
|
||||
bundled
|
||||
build
|
||||
coverage
|
||||
docs
|
||||
wiki
|
||||
gulpfile.js
|
||||
bower.json
|
||||
karma.conf.js
|
||||
tsconfig.json
|
||||
typings.json
|
||||
CONTRIBUTING.md
|
||||
ISSUE_TEMPLATE.md
|
||||
PULL_REQUEST_TEMPLATE.md
|
||||
tslint.json
|
||||
wallaby.js
|
||||
.travis.yml
|
||||
.gitignore
|
||||
.vscode
|
||||
type_definitions
|
33
packages/api/package.json
Normal file
33
packages/api/package.json
Normal file
@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "@ms/api",
|
||||
"version": "0.0.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
"minecraft",
|
||||
"bukkit",
|
||||
"sponge"
|
||||
],
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/circlecloud/ms.git",
|
||||
"license": "ISC",
|
||||
"main": "dist/index.js",
|
||||
"publishConfig": {
|
||||
"registry": "https://repo.yumc.pw/repository/npm/"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf dist",
|
||||
"watch": "npx tsc --watch",
|
||||
"build": "yarn clean && npx tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ms/common": "^0.0.0",
|
||||
"inversify": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.0",
|
||||
"typescript": "^3.6.2"
|
||||
}
|
||||
}
|
23
packages/api/src/command.ts
Normal file
23
packages/api/src/command.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { PluginInfo } from './typings/plugin';
|
||||
export namespace command {
|
||||
export abstract class Command {
|
||||
on(plugin: PluginInfo, name: string, exec: { cmd: Function, tab?: Function }) {
|
||||
var cmd = this.create(plugin, { name: name });
|
||||
console.debug(`插件 ${plugin.description.name} 创建命令 ${name}(${cmd})...`)
|
||||
if (exec.cmd && typeof exec.cmd === "function") {
|
||||
this.onCommand(plugin, cmd, exec.cmd)
|
||||
} else {
|
||||
throw Error("CommandExec Must be a function... Input: " + exec.cmd)
|
||||
}
|
||||
if (exec.tab && typeof exec.tab === "function") {
|
||||
this.onTabComplete(plugin, cmd, exec.tab)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create Server Command Object
|
||||
*/
|
||||
abstract create(plugin: object, opts: { name: string });
|
||||
abstract onCommand(plugin: object, command: any, opts: { name: string });
|
||||
abstract onTabComplete(plugin: object, command: any, opts: { name: string });
|
||||
}
|
||||
}
|
146
packages/api/src/console.ts
Normal file
146
packages/api/src/console.ts
Normal file
@ -0,0 +1,146 @@
|
||||
let Arrays = Java.type('java.util.Arrays');
|
||||
let Level = Java.type('java.util.logging.Level');
|
||||
let ignoreLogPrefix = ['java.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.'];
|
||||
|
||||
export class MiaoScriptConsole implements Console {
|
||||
Console: NodeJS.ConsoleConstructor;
|
||||
|
||||
private _name: string = '';
|
||||
private logger: any;
|
||||
|
||||
protected prefix: string = '§6[§bMiaoScript§6]§r ';
|
||||
|
||||
constructor() {
|
||||
this.logger = global.logger;
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
set name(name: string) {
|
||||
if (name) {
|
||||
this._name = `[${name}] `;
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
this.prefix = `§6[§cMS§6][§b${name}§6]§r `;
|
||||
}
|
||||
}
|
||||
log(...args): void {
|
||||
this.logger.info(this.name + args.join(' '));
|
||||
}
|
||||
info(...args) {
|
||||
this.logger.info(this.name + args.join(' '));
|
||||
};
|
||||
warn(...args) {
|
||||
this.logger.warning(this.name + args.join(' '));
|
||||
};
|
||||
error(...args) {
|
||||
this.logger.log(Level.SEVERE, this.name + args.join(' '));
|
||||
};
|
||||
debug(...args) {
|
||||
if (global.debug) {
|
||||
this.logger.info(this.name + '[DEBUG] ' + args.join(' '));
|
||||
}
|
||||
};
|
||||
sender(...args) {
|
||||
this.info(args)
|
||||
}
|
||||
console(...args) {
|
||||
this.info(args)
|
||||
}
|
||||
object(obj) {
|
||||
for (var i in obj) {
|
||||
this.logger(i, '=>', obj[i])
|
||||
}
|
||||
};
|
||||
ex(ex: Error) {
|
||||
this.stack(ex).forEach(line => this.console(line))
|
||||
};
|
||||
stack(ex: Error | any): string[] {
|
||||
var stack = ex.getStackTrace();
|
||||
var cache = ['§4' + ex];
|
||||
if (stack.class) {
|
||||
stack = Arrays.asList(stack)
|
||||
}
|
||||
stack.forEach(function(trace) {
|
||||
if (trace.className.startsWith('<')) {
|
||||
var fileName = trace.fileName
|
||||
fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName;
|
||||
cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${trace.lineNumber}`)
|
||||
} else {
|
||||
var className = trace.className;
|
||||
if (className.startsWith('jdk.nashorn.internal.scripts')) {
|
||||
className = className.substr(className.lastIndexOf('$') + 1)
|
||||
} else {
|
||||
for (var prefix in ignoreLogPrefix) {
|
||||
if (className.startsWith(ignoreLogPrefix[prefix])) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
cache.push(` §e->§c ${className}.${trace.methodName}(§4${trace.fileName}:${trace.lineNumber}§c)`);
|
||||
}
|
||||
});
|
||||
return cache;
|
||||
}
|
||||
assert(value: any, message?: string, ...optionalParams: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
clear(): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
count(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
countReset(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
dir(obj: any, options?: NodeJS.InspectOptions): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
dirxml(...data: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
group(...label: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
groupCollapsed(...label: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
groupEnd(): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
table(tabularData: any, properties?: string[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
time(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
timeEnd(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
timeLog(label?: string, ...data: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
trace(message?: any, ...optionalParams: any[]): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
markTimeline(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
profile(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
profileEnd(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
timeStamp(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
timeline(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
timelineEnd(label?: string): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
154
packages/api/src/event.ts
Normal file
154
packages/api/src/event.ts
Normal file
@ -0,0 +1,154 @@
|
||||
'use strict';
|
||||
/**
|
||||
* MiaoScript Event处理类
|
||||
*/
|
||||
import '@ms/core'
|
||||
import '@ms/nashorn'
|
||||
import { injectable } from 'inversify'
|
||||
|
||||
const Thread = Java.type("java.lang.Thread");
|
||||
|
||||
@injectable()
|
||||
abstract class EventService {
|
||||
private plugin;
|
||||
private mapEvent = [];
|
||||
private listenerMap = [];
|
||||
private baseEventDir = '';
|
||||
constructor(baseEventDir: string) {
|
||||
this.baseEventDir = baseEventDir;
|
||||
|
||||
}
|
||||
/**
|
||||
* 扫描包 org.bukkit.event 下的所有事件
|
||||
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||
*/
|
||||
mapEventName() {
|
||||
if (this.baseEventDir === "") {
|
||||
throw new Error("事件基础包名为空 无法进行事件映射!");
|
||||
}
|
||||
var count = 0;
|
||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(this.baseEventDir);
|
||||
while (dirs.hasMoreElements()) {
|
||||
var url = dirs.nextElement();
|
||||
var protocol = url.protocol;
|
||||
if (protocol === "jar") {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var jar = url.openConnection().jarFile;
|
||||
var entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
var entry = entries.nextElement();
|
||||
var name = entry.name;
|
||||
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
||||
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
||||
var i = name.replaceAll('/', '.');
|
||||
try {
|
||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||
if (this.isValidEvent(clz)) {
|
||||
var simpleName = this.class2Name(clz).toLowerCase();
|
||||
console.debug(`Mapping Event [${clz.canonicalName}] => ${simpleName}`);
|
||||
this.mapEvent[simpleName] = clz;
|
||||
count++;
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
class2Name(clazz) {
|
||||
return clazz.simpleName;
|
||||
};
|
||||
|
||||
name2Class(name, event) {
|
||||
var eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
this.mapEvent[event] = eventCls;
|
||||
} catch (ex) {
|
||||
console.console(`§6插件 §b${name} §6注册事件 §c${event} §6失败 §4事件未找到!`);
|
||||
console.ex(new Error(`插件 ${name} 注册事件 ${event} 失败 事件未找到!`));
|
||||
return;
|
||||
}
|
||||
}
|
||||
return eventCls;
|
||||
};
|
||||
|
||||
execute(name, exec, eventCls) {
|
||||
return (...args) => {
|
||||
try {
|
||||
var time = new Date().getTime()
|
||||
exec(args[args.length - 1]);
|
||||
var cost = new Date().getTime() - time;
|
||||
if (cost > 20) {
|
||||
console.console(`§c注意! §6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件 §c耗时 §4${cost}ms !`)
|
||||
}
|
||||
} catch (ex) {
|
||||
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
|
||||
console.ex(ex);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 添加事件监听
|
||||
* @param jsp
|
||||
* @param event
|
||||
* @param exec {function}
|
||||
* @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||
var name = jsp.description.name;
|
||||
var eventCls = this.name2Class(name, event);
|
||||
if (!eventCls) { return; }
|
||||
if (typeof priority === 'boolean') {
|
||||
ignoreCancel = priority;
|
||||
priority = 'NORMAL';
|
||||
}
|
||||
priority = priority || 'NORMAL';
|
||||
ignoreCancel = ignoreCancel || false;
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel);
|
||||
var listenerMap = this.listenerMap;
|
||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||
if (!listenerMap[name]) listenerMap[name] = [];
|
||||
var offExec = () => {
|
||||
this.unregister(eventCls, listener);
|
||||
console.debug(`插件 ${name} 注销事件 ${this.class2Name(eventCls)}`);
|
||||
};
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: offExec
|
||||
};
|
||||
listenerMap[name].push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug(`插件 ${name} 注册事件 ${this.class2Name(eventCls)} => ${exec.name || '匿名方法'}`);
|
||||
return off;
|
||||
}
|
||||
|
||||
abstract isValidEvent(clazz: any): boolean;
|
||||
abstract register(eventCls: any, exec: Function, priority, ignoreCancel);
|
||||
abstract unregister(event, listener);
|
||||
}
|
||||
|
||||
var EventHandler = Object.assign(new EventHandler(), require('event'));
|
||||
// 映射事件名称
|
||||
console.info(`bukkit 事件映射完毕 共计 ${EventHandler.mapEventName().toFixed(0)} 个事件!`);
|
||||
module.exports = {
|
||||
on: EventHandler.listen.bind(EventHandler),
|
||||
disable: function(jsp) {
|
||||
var eventCache = EventHandler.listenerMap[jsp.description.name];
|
||||
if (eventCache) {
|
||||
eventCache.forEach(t => t.off());
|
||||
delete EventHandler.listenerMap[jsp.description.name];
|
||||
}
|
||||
}
|
||||
};
|
5
packages/api/src/index.ts
Normal file
5
packages/api/src/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import './typings/global'
|
||||
|
||||
export * from './command'
|
||||
export * from './interfaces'
|
||||
export * from './console'
|
2
packages/api/src/interfaces/index.ts
Normal file
2
packages/api/src/interfaces/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './plugin'
|
||||
export * from './server'
|
25
packages/api/src/interfaces/plugin.ts
Normal file
25
packages/api/src/interfaces/plugin.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Container } from "inversify";
|
||||
|
||||
export namespace plugin {
|
||||
/**
|
||||
* MiaoScript Plugin
|
||||
*/
|
||||
export const Plugin = Symbol("Plugin");
|
||||
/**
|
||||
* Runtime Plugin Instance
|
||||
*/
|
||||
export const PluginInstance = Symbol("PluginInstance");
|
||||
/**
|
||||
* MiaoScript Plugin Manager
|
||||
*/
|
||||
export const PluginManager = Symbol("PluginManager");
|
||||
/**
|
||||
* MiaoScript Plugin Manager
|
||||
*/
|
||||
export interface PluginManager {
|
||||
scan(folder: string): void;
|
||||
load(container: Container): void;
|
||||
enable(): void;
|
||||
disable(): void;
|
||||
}
|
||||
}
|
4
packages/api/src/interfaces/server.ts
Normal file
4
packages/api/src/interfaces/server.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export namespace server {
|
||||
export const ServerType = Symbol("ServerType");
|
||||
export const Console = Symbol("Console");
|
||||
}
|
24
packages/api/src/typings/global.ts
Normal file
24
packages/api/src/typings/global.ts
Normal file
@ -0,0 +1,24 @@
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
interface Global {
|
||||
logger: any;
|
||||
debug: boolean;
|
||||
noop: Function;
|
||||
console: Console;
|
||||
}
|
||||
}
|
||||
var root: string;
|
||||
var base: Core;
|
||||
var ScriptEngineContextHolder: any;
|
||||
function engineLoad(str: string): any;
|
||||
interface Core {
|
||||
getClass(name: String);
|
||||
}
|
||||
interface Console {
|
||||
ex(err: Error): void;
|
||||
stack(err: Error): string[];
|
||||
sender(...args: any): void;
|
||||
console(...args: any): void;
|
||||
}
|
||||
}
|
||||
export { }
|
13
packages/api/src/typings/plugin.ts
Normal file
13
packages/api/src/typings/plugin.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export interface CommandInfo {
|
||||
aliases: string[];
|
||||
description: string;
|
||||
}
|
||||
export interface PluginInfo {
|
||||
description: PluginDescription;
|
||||
}
|
||||
export interface PluginDescription {
|
||||
name: string;
|
||||
version: string;
|
||||
author: string;
|
||||
commands: { [key: string]: CommandInfo };
|
||||
}
|
7
packages/api/tsconfig.json
Normal file
7
packages/api/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user