2020-06-02 09:50:47 +00:00
|
|
|
import * as reflect from '@ccms/common/dist/reflect'
|
2020-07-06 08:07:16 +00:00
|
|
|
import { injectable, inject } from '@ccms/container'
|
2020-06-02 09:50:47 +00:00
|
|
|
|
2020-03-01 11:37:34 +00:00
|
|
|
import { NativePluginManager } from './native_plugin'
|
2020-06-02 09:50:47 +00:00
|
|
|
import { constants } from '../../constants'
|
2020-03-01 11:37:34 +00:00
|
|
|
|
2019-09-07 04:23:15 +00:00
|
|
|
export namespace server {
|
2020-01-15 08:48:18 +00:00
|
|
|
/**
|
|
|
|
* Runtime ServerType
|
|
|
|
*/
|
2020-06-02 09:50:47 +00:00
|
|
|
export const ServerType = Symbol("ServerType")
|
2020-01-15 08:48:18 +00:00
|
|
|
/**
|
|
|
|
* Runtime Console
|
|
|
|
*/
|
2020-06-02 09:50:47 +00:00
|
|
|
export const Console = Symbol("Console")
|
2020-01-15 08:48:18 +00:00
|
|
|
/**
|
|
|
|
* MiaoScript Server
|
|
|
|
*/
|
2020-06-02 09:50:47 +00:00
|
|
|
export const Server = Symbol("Server")
|
2020-01-15 08:48:18 +00:00
|
|
|
/**
|
|
|
|
* Runtime Server Instance
|
|
|
|
*/
|
2020-06-02 09:50:47 +00:00
|
|
|
export const ServerInstance = Symbol("ServerInstance")
|
2020-01-15 08:48:18 +00:00
|
|
|
/**
|
|
|
|
* MiaoScript Server
|
|
|
|
*/
|
2019-11-04 12:18:30 +00:00
|
|
|
export interface Server {
|
2020-06-02 09:50:47 +00:00
|
|
|
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
|
2020-06-20 08:38:14 +00:00
|
|
|
tabComplete?(sender: string | any, input: string, index?: number): string[]
|
2020-06-02 09:50:47 +00:00
|
|
|
}
|
|
|
|
@injectable()
|
2020-07-06 08:07:16 +00:00
|
|
|
export class ServerChecker {
|
|
|
|
@inject(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@injectable()
|
2020-06-02 09:50:47 +00:00
|
|
|
export abstract class ReflectServer implements server.Server {
|
|
|
|
protected pipeline: any
|
|
|
|
protected rootLogger: any
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.reflect()
|
|
|
|
}
|
|
|
|
|
|
|
|
getVersion(): string {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getPlayer(name: string) {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getOnlinePlayers(): any[] {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getConsoleSender() {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getService(service: string) {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
dispatchCommand(sender: any, command: string): boolean {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
dispatchConsoleCommand(command: string): boolean {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getPluginsFolder(): string {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getNativePluginManager(): NativePluginManager {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getDedicatedServer() {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getNettyPipeline() {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
getRootLogger() {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
sendJson(sender: any, json: string | object): void {
|
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
2020-06-20 08:38:14 +00:00
|
|
|
tabComplete?(sender: any, input: string, index?: number): string[] {
|
2020-06-02 09:50:47 +00:00
|
|
|
throw new Error("Method not implemented.")
|
|
|
|
}
|
|
|
|
protected reflect() {
|
|
|
|
try {
|
|
|
|
let consoleServer = this.getDedicatedServer()
|
|
|
|
this.reflectPipeline(consoleServer)
|
|
|
|
this.reflectRootLogger(consoleServer)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error When Reflect MinecraftServer!', error)
|
|
|
|
console.ex(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected reflectPipeline(consoleServer: any) {
|
|
|
|
let connection: any
|
|
|
|
let promise: any
|
|
|
|
for (const method of constants.Reflect.Method.getServerConnection) {
|
|
|
|
try {
|
|
|
|
connection = reflect.on(consoleServer).call(method).get()
|
|
|
|
if (connection.class.name.indexOf('ServerConnection') !== -1
|
|
|
|
|| connection.class.name.indexOf('NetworkSystem') !== -1) { break }
|
|
|
|
connection = undefined
|
|
|
|
} catch (error) { }
|
|
|
|
}
|
|
|
|
if (!connection) { console.error("Can't found ServerConnection!"); return }
|
|
|
|
for (const field of constants.Reflect.Field.listeningChannels) {
|
|
|
|
try {
|
|
|
|
promise = reflect.on(connection).get(field).get().get(0)
|
|
|
|
if (promise.class.name.indexOf('Promise') !== -1) { break }
|
|
|
|
promise = undefined
|
|
|
|
} catch (error) { }
|
|
|
|
}
|
|
|
|
if (!promise) { console.error("Can't found listeningChannels!"); return }
|
|
|
|
this.pipeline = reflect.on(promise).get('channel').get().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 (this.rootLogger && this.rootLogger.class.name.indexOf('Logger') === -1) {
|
|
|
|
console.error('Error Logger Class: ' + this.rootLogger.class.name)
|
|
|
|
this.rootLogger = undefined
|
|
|
|
}
|
|
|
|
// get root logger
|
|
|
|
for (let index = 0; index < 5 && this.rootLogger.parent; index++) {
|
|
|
|
this.rootLogger = this.rootLogger.parent
|
|
|
|
}
|
|
|
|
if (!this.rootLogger) { console.error("Can't found rootLogger!") }
|
|
|
|
}
|
2019-11-04 12:18:30 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|