feat: add readme and update ts config

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-21 15:06:17 +08:00
parent 2a60b36e23
commit e5ffa70ed9
4 changed files with 114 additions and 38 deletions

22
README.MD Normal file
View File

@ -0,0 +1,22 @@
# MiaoScript implement
## MiaoScript JS 实现
项目 由 TypeScript 进行编写 然后编译至 `es5` 用于兼容 Java8 的 `Nashorn`
### Project Path
```txt
└─packages
├─api 全平台兼容的接口
├─core 核心代码 用于引导加载
├─common 公共类库代码 例如 http reflect 模块
├─container IOC容器 用于注入具体实现
├─nashorn Nashorn 的类型定义
├─bukkit BukkitAPI内部实现
├─sponge SpongeAPI内部实现
├─plugin 插件管理器
└─plugins 这里当然是插件啦
├─bukkit 只兼容Bukkit的插件
└─sponge 只兼容Sponge的插件
```

View File

@ -18,7 +18,9 @@ class Reflect {
this.class = obj; this.class = obj;
} else { } else {
this.obj = obj; this.obj = obj;
this.class = obj.class; if (obj !== null && obj !== undefined && obj.class) {
this.class = obj.class;
}
} }
} }
@ -30,7 +32,7 @@ class Reflect {
return Java.from(declaredMethods(this.class)); return Java.from(declaredMethods(this.class));
} }
field(name) { field(name): Reflect {
try { try {
// Try getting a public field // Try getting a public field
var field = this.class.field(name); var field = this.class.field(name);
@ -41,36 +43,34 @@ class Reflect {
} }
}; };
fields(declared) { fields(declared = false) {
return Java.from(declared ? this.class.declaredFields : this.class.fields); return Java.from(declared ? this.class.declaredFields : this.class.fields);
} }
values(declared) { values(declared = false) {
var cache = {}; var cache = {};
var feds = declared ? this.class.declaredFields : this.class.fields; this.fields(declared).forEach(fed => cache[fed.name] = this.field(fed.name).get())
Java.from(feds).forEach(function(fed) {
cache[fed.name] = this.field(fed.name).get();
}.bind(this))
return cache; return cache;
} }
call(...args) { call(...args): Reflect {
var params = args.slice(1); var params = args.slice(1);
var method = declaredMethod(this.class, args[0], types(params)); var method = accessible(declaredMethod(this.class, args[0], types(params)));
return on(method.invoke(this.get(), params)); let result = method.invoke(this.get(), params);
return result && on(result);
}; };
get(...args) { get(...args): Reflect | any {
return args.length === 1 ? this.field(args[0]) : this.obj; return args.length === 1 ? this.field(args[0]) : this.obj;
}; };
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
set(name, value) { set(name, value): Reflect {
accessible(declaredField(this.class, name)).set(this.obj, value); accessible(declaredField(this.class, name)).set(this.obj, value);
return this; return this;
}; };
create(...args) { create(...args): Reflect {
return on(declaredConstructor(this.class, args).newInstance(args)); return on(declaredConstructor(this.class, args).newInstance(args));
}; };
} }
@ -112,16 +112,17 @@ function declaredConstructor(clazz, param) {
} }
function declaredField(clazz, name) { function declaredField(clazz, name) {
var clazzt = clazz;
var field = null; var field = null;
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
while (clazz !== JavaObject.class) { while (clazzt !== JavaObject.class) {
try { try {
field = clazz.getDeclaredField(name); field = clazzt.getDeclaredField(name);
if (field !== null) { if (field !== null) {
break; break;
} }
} catch (e) { } catch (e) {
clazz = clazz.getSuperclass(); clazzt = clazzt.getSuperclass();
} }
} }
if (field === null) { if (field === null) {
@ -136,7 +137,16 @@ function declaredMethod(clazz, name, clazzs) {
try { try {
methodCache[key] = clazz.getMethod(name, clazzs); methodCache[key] = clazz.getMethod(name, clazzs);
} catch (ex) { } catch (ex) {
methodCache[key] = clazz.getDeclaredMethod(name, clazzs); try {
methodCache[key] = clazz.getDeclaredMethod(name, clazzs);
} catch (ex) {
for (const m of Java.from(declaredMethods(clazz))) {
if (m.name == name) {
methodCache[key] = m;
break;
}
}
}
} }
} }
return methodCache[key]; return methodCache[key];
@ -179,7 +189,7 @@ function mapMethod(target, source, name) {
} }
function on(obj) { function on(obj) {
if (!obj || !obj.class) { throw new TypeError(`参数 ${obj} 不是一个Java对象!`) } // if (!obj || !obj.class) { throw new TypeError(`参数 ${obj} 不是一个Java对象!`) }
return new Reflect(obj); return new Reflect(obj);
} }

View File

@ -1,28 +1,71 @@
import '@ms/nashorn' import '@ms/nashorn'
import { plugin, server } from '@ms/api' import { plugin, server, task } from '@ms/api'
import { DefaultContainer as container } from '@ms/container' import { DefaultContainer as container, injectable, inject, postConstruct } from '@ms/container'
import { PluginManagerImpl } from '@ms/plugin' import { PluginManagerImpl } from '@ms/plugin'
try { @injectable()
Java.type("org.bukkit.Bukkit"); class MiaoScriptCore {
require('@ms/bukkit'); @inject(server.Console)
} catch (ex) { private Console: Console;
@inject(task.TaskManager)
private taskManager: task.TaskManager;
@inject(plugin.PluginManager)
private pluginManager: plugin.PluginManager;
enable() {
try {
this.loadServerConsole();
this.loadTaskFunction();
this.loadPlugins();
} catch (error) {
console.console(`§cMiaoScript start error please contact plugin author!`);
console.ex(error);
}
return () => this.disable();
}
loadServerConsole() {
//@ts-ignore
global.console = new this.Console();
}
loadTaskFunction() {
//@ts-ignore
global.setTimeout = (func: Function, tick: number) => this.taskManager.create(func).later(tick).run()
//@ts-ignore
global.setInterval = (func: Function, tick: number) => this.taskManager.create(func).timer(tick).run()
}
loadPlugins() {
this.pluginManager.scan('plugins');
this.pluginManager.build(container);
this.pluginManager.load();
this.pluginManager.enable();
}
disable() {
this.pluginManager.disable();
}
} }
try { function init() {
Java.type("org.spongepowered.api.Sponge"); try {
require('@ms/sponge'); Java.type("org.bukkit.Bukkit");
} catch (ex) { require('@ms/bukkit');
} catch (ex) {
}
try {
Java.type("org.spongepowered.api.Sponge");
require('@ms/sponge');
} catch (ex) {
}
container.bind(plugin.PluginManager).to(PluginManagerImpl).inSingletonScope();
container.bind(MiaoScriptCore).to(MiaoScriptCore).inSingletonScope();
} }
let Console = container.get(server.Console); init();
//@ts-ignore
global.console = new Console();
container.bind(plugin.PluginManager).to(PluginManagerImpl).inSingletonScope(); export default container.get<MiaoScriptCore>(MiaoScriptCore).enable();
let manager = container.get<plugin.PluginManager>(plugin.PluginManager);
manager.scan('plugins');
manager.load(container);
manager.enable();

View File

@ -7,6 +7,7 @@
"sourceMap": false, "sourceMap": false,
"declaration": true, "declaration": true,
"noImplicitAny": false, "noImplicitAny": false,
"downlevelIteration": true,
"allowUnreachableCode": true, "allowUnreachableCode": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,