2020-09-24 05:51:18 +00:00
|
|
|
import { plugin as pluginApi, task, server, channel, constants, proxy } from '@ccms/api'
|
2020-05-02 16:14:47 +00:00
|
|
|
import { Translate } from '@ccms/i18n'
|
2020-09-24 05:51:18 +00:00
|
|
|
import { DefaultContainer as container, JSClass, Autowired } from '@ccms/container'
|
2020-08-14 08:07:17 +00:00
|
|
|
import { interfaces, JSPlugin, Cmd, Tab, enable, Listener, disable } from '@ccms/plugin'
|
2019-09-22 15:31:52 +00:00
|
|
|
|
2020-05-02 16:14:47 +00:00
|
|
|
import * as fs from '@ccms/common/dist/fs'
|
2020-06-20 08:40:34 +00:00
|
|
|
import * as reflect from '@ccms/common/dist/reflect'
|
2020-05-02 16:14:47 +00:00
|
|
|
import http from '@ccms/common/dist/http'
|
2020-03-30 10:49:19 +00:00
|
|
|
|
2019-09-22 15:31:52 +00:00
|
|
|
let help = [
|
2020-05-07 09:13:48 +00:00
|
|
|
'§6========= §6[§aMiaoScriptPackageManager§6] 帮助 §aBy §bMiaoWoo §6=========',
|
2020-09-24 05:51:18 +00:00
|
|
|
'§6/mspm §ainstall §e<插件名称> §6- §3安装仓库插件',
|
|
|
|
'§6/mspm §aload §e<插件名称> §6- §3安装本地插件',
|
|
|
|
'§6/mspm §aunload §e<插件名称> §6- §3卸载已安装插件',
|
|
|
|
'§6/mspm §areload §e<插件名称> §6- §3重载已安装插件(无名称则重载自身)',
|
|
|
|
'§6/mspm §alist [i] §6- §3列出仓库插件[已安装的插件]',
|
|
|
|
'§6/mspm §aupdate §e[插件名称] §6- §3更新插件(无名称则更新源)',
|
|
|
|
'§6/mspm §aupgrade §e[插件名称] §6- §3升级插件/框架(§4无名称则升级框架§3)',
|
|
|
|
'§6/mspm §arun §e<JS代码> §6- §3运行JS代码',
|
|
|
|
'§6/mspm §adeploy §e<插件名称> §6- §3发布插件',
|
|
|
|
'§6/mspm §crestart §6- §4重启MiaoScript脚本引擎'
|
2020-06-20 08:40:34 +00:00
|
|
|
]
|
2019-09-22 15:31:52 +00:00
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
let langMap = {
|
2020-05-11 16:07:05 +00:00
|
|
|
'main.command.not.exists': '§4未知的子命令: §c{command}',
|
|
|
|
'main.command.help.tip': '§6请执行 §b/{command} §ahelp §6查看帮助!',
|
2020-09-17 09:44:48 +00:00
|
|
|
'main.command.no.permission': '§c你没有此命令的权限!',
|
2020-05-07 16:50:30 +00:00
|
|
|
'list.install.header': '§6当前 §bMiaoScript §6已安装下列插件:',
|
2020-09-24 05:51:18 +00:00
|
|
|
'list.install.body': `§6┌插件名称: §b{name}
|
|
|
|
§6├版本: §a{version}
|
|
|
|
§6├作者: §3{author}
|
|
|
|
§6└来源: §c{from}`,
|
2020-03-30 10:49:19 +00:00
|
|
|
'list.header': '§6当前 §bMiaoScriptPackageCenter §6中存在下列插件:',
|
2020-09-24 05:51:18 +00:00
|
|
|
'list.body': `§6┌插件名称: §b{name}
|
|
|
|
§6├版本: §a{version}
|
|
|
|
§6├作者: §3{author}
|
|
|
|
§6└更新时间: §9{updated_at}`,
|
2020-03-30 10:49:19 +00:00
|
|
|
'plugin.not.exists': '§6插件 §b{name} §c不存在!',
|
2020-09-22 10:43:02 +00:00
|
|
|
'plugin.unload.start': '§c开始卸载 §6插件 §b{name} §6版本 §3{version}!',
|
|
|
|
'plugin.unload.finish': '§6插件 §b{name} §6版本 §3{version} §a已卸载!',
|
|
|
|
'plugin.reload.start': '§a开始重载 §6插件 §b{name} §6版本 §3{version}!',
|
|
|
|
'plugin.reload.finish': '§6插件 §b{name} §6版本 §3{version} §a重载完成!',
|
2020-03-30 10:49:19 +00:00
|
|
|
'plugin.name.empty': '§c请输入插件名称!',
|
|
|
|
'cloud.update.finish': '§6成功从 §aMiaoScriptPackageCenter §6获取到 §a{length} §6个插件!',
|
2020-05-07 09:13:48 +00:00
|
|
|
'cloud.not.exists': '§6当前 §aMiaoScriptPackageCenter §c不存在 §a{name} §c插件!',
|
2020-08-14 08:07:17 +00:00
|
|
|
'cloud.update.exists': '§6插件 §b{name} §a发现新版本 §3{new_version} §6当前版本 §3{old_version}!',
|
2020-09-22 10:43:02 +00:00
|
|
|
'download.start': '§6开始下载插件: §b{name} §6版本 §3{version}',
|
2020-05-07 09:13:48 +00:00
|
|
|
'download.url': '§6插件下载地址: §b{url}',
|
2020-09-22 10:43:02 +00:00
|
|
|
'download.finish': '§6插件 §b{name} §6版本 §3{version} §a下载完毕 开始加载 ...',
|
2020-10-02 10:54:10 +00:00
|
|
|
'install.already': '§6插件 §b{name} §6版本 §3{version} §c已安装在服务器 §3更新请用 update 命令!',
|
2020-09-22 10:43:02 +00:00
|
|
|
'install.finish': '§6插件 §b{name} §6版本 §3{version} §a安装成功!',
|
|
|
|
'update.finish': '§6插件 §b{name} §6版本 §3{version} §a更新成功!',
|
|
|
|
'upgrade.confirm': '§6您正在尝试更新 §bMiaoScript §c核心 §6请执行 §b/mpm §aupgrade §cconfirm §6确认执行!',
|
|
|
|
'upgrade.failed': '§6尝试热更新 §bMiaoScript §c核心 §4失败! §6请重启服务器完成更新...',
|
2020-05-11 16:07:05 +00:00
|
|
|
'deploy.token.not.exists': '§4请先配置发布Token!',
|
2020-09-22 10:43:02 +00:00
|
|
|
'deploy.success': '§6插件 §b{name} §6版本 §3{version} §a发布成功! §6服务器返回: §a{msg}',
|
|
|
|
'deploy.fail': '§6插件 §b{name} §6版本 §3{version} §c发布失败! §6服务器返回: §c{msg}',
|
2020-09-24 10:36:53 +00:00
|
|
|
'prun.script': '§6指定插件:§b {name}',
|
2020-05-11 16:07:05 +00:00
|
|
|
'run.script': '§b运行脚本:§r {script}',
|
|
|
|
'run.result': '§a返回结果:§r {result}',
|
|
|
|
'run.noresult': '§4没有返回结果!',
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let fallbackMap = langMap
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
class FakeSender {
|
|
|
|
private _proxy
|
|
|
|
private name: string
|
|
|
|
private plugin: MiaoScriptPackageManager
|
|
|
|
constructor(name: string, plugin: MiaoScriptPackageManager, superclass: any) {
|
|
|
|
this.name = name
|
|
|
|
this.plugin = plugin
|
|
|
|
let FakeSenderAdapter = Java.extend(superclass, {
|
|
|
|
getName: () => this.name,
|
|
|
|
isOp: () => true,
|
2020-09-24 05:51:18 +00:00
|
|
|
hasPermission: () => true,
|
|
|
|
sendMessage: (message) => this.sendMessage(message)
|
2020-09-22 10:43:02 +00:00
|
|
|
})
|
|
|
|
this._proxy = new FakeSenderAdapter()
|
|
|
|
}
|
2020-09-24 05:51:18 +00:00
|
|
|
|
|
|
|
sendMessage(message) {
|
|
|
|
return this.plugin.sendBungeeCordMessage(this.name, `§6[§3BPM§6][§a${this.plugin.serverName}§6] ${message}`)
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
getHandler() {
|
|
|
|
return this._proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
class BukkitFakeSender extends FakeSender {
|
|
|
|
constructor(name: string, plugin: MiaoScriptPackageManager) {
|
|
|
|
super(name, plugin, Java.type('org.bukkit.command.CommandSender'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SpongeFakeSender extends FakeSender {
|
|
|
|
constructor(name: string, plugin: MiaoScriptPackageManager) {
|
|
|
|
super(name, plugin, Java.type('org.spongepowered.api.command.CommandSource'))
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
|
|
|
return super.sendMessage(message.content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
@JSPlugin({ prefix: 'PM', version: '1.3.0', author: 'MiaoWoo', source: __filename })
|
2020-01-16 09:27:24 +00:00
|
|
|
export class MiaoScriptPackageManager extends interfaces.Plugin {
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
2020-06-20 08:40:34 +00:00
|
|
|
private pluginManager: pluginApi.PluginManager
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
2020-06-20 08:40:34 +00:00
|
|
|
private taskManager: task.TaskManager
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired(server.ServerType)
|
2020-06-20 08:40:34 +00:00
|
|
|
private serverType: string
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
2020-01-16 09:27:24 +00:00
|
|
|
private server: server.Server
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired(pluginApi.PluginFolder)
|
2020-06-20 08:40:34 +00:00
|
|
|
private pluginFolder: string
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
|
|
|
private channel: channel.Channel
|
|
|
|
@Autowired()
|
|
|
|
private bungee: proxy.BungeeCord
|
2020-08-14 08:07:17 +00:00
|
|
|
|
|
|
|
@JSClass('com.google.common.io.ByteStreams')
|
|
|
|
private ByteStreams: any
|
2019-09-22 15:31:52 +00:00
|
|
|
|
2020-01-16 09:27:24 +00:00
|
|
|
private packageCache: any[] = [];
|
2020-03-30 10:49:19 +00:00
|
|
|
private packageNameCache: string[] = [];
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
private isBungeeCord = false
|
|
|
|
public serverName: string
|
2020-06-20 08:40:34 +00:00
|
|
|
private translate: Translate
|
2020-08-14 08:07:17 +00:00
|
|
|
private channelOff: { off: () => void }
|
2019-09-22 15:31:52 +00:00
|
|
|
|
|
|
|
load() {
|
2020-03-30 10:49:19 +00:00
|
|
|
this.translate = new Translate({
|
|
|
|
langMap,
|
|
|
|
fallbackMap
|
|
|
|
})
|
|
|
|
this.updateRepo(this.server.getConsoleSender())
|
2019-09-22 15:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:07:17 +00:00
|
|
|
@enable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
|
|
|
serverEnbale() {
|
|
|
|
this.channelOff = this.channel?.listen(this, 'BungeeCord', (data) => {
|
|
|
|
let input = this.ByteStreams.newDataInput(data)
|
|
|
|
let subChannel = input.readUTF()
|
|
|
|
switch (subChannel) {
|
|
|
|
case "GetServer":
|
2020-09-22 10:43:02 +00:00
|
|
|
this.isBungeeCord = true
|
2020-08-14 08:07:17 +00:00
|
|
|
this.serverName = input.readUTF()
|
|
|
|
break
|
|
|
|
case "MiaoScriptPackageManager":
|
|
|
|
this.readForward(input)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let players = this.server.getOnlinePlayers()
|
|
|
|
if (players.length) this.bungeeCordDetect(players[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
@disable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
|
|
|
serverDisable() {
|
|
|
|
this.channelOff?.off()
|
|
|
|
}
|
|
|
|
|
|
|
|
private bungeeCordDetect(player) {
|
|
|
|
if (player) {
|
2020-09-24 05:51:18 +00:00
|
|
|
this.bungee.for(player).getServer().send()
|
2020-08-14 08:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private bungeeCordForward(player, command) {
|
|
|
|
if (player) {
|
2020-09-24 05:51:18 +00:00
|
|
|
this.bungee.for(player).forward("ALL", "MiaoScriptPackageManager", command).send()
|
2020-08-14 08:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private readForward(input) {
|
|
|
|
let message = JSON.parse(input.readUTF())
|
2020-09-22 10:43:02 +00:00
|
|
|
let fakeSender = this.getProxySender(message.sender)
|
|
|
|
this.taskManager.create(() => this.main(fakeSender, message.command, message.args)).async().submit()
|
2020-10-02 10:54:10 +00:00
|
|
|
this.logger.sender(fakeSender, `§6命令 §b/mspm ${message.args?.join?.(' ')} §a执行成功!`)
|
2020-09-22 10:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getProxySender(name: string) {
|
|
|
|
switch (this.serverType) {
|
|
|
|
case constants.ServerType.Bukkit:
|
2020-09-24 05:51:18 +00:00
|
|
|
return new BukkitFakeSender(name, this).getHandler()
|
2020-09-22 10:43:02 +00:00
|
|
|
case constants.ServerType.Sponge:
|
2020-09-24 05:51:18 +00:00
|
|
|
return new SpongeFakeSender(name, this).getHandler()
|
2020-09-22 10:43:02 +00:00
|
|
|
default:
|
|
|
|
return this.server.getConsoleSender()
|
|
|
|
}
|
2020-08-14 08:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Listener({ servers: [constants.ServerType.Bukkit] })
|
|
|
|
PlayerJoinEvent(event: org.bukkit.event.player.PlayerJoinEvent) {
|
|
|
|
this.bungeeCordDetect(event.getPlayer())
|
|
|
|
if (event.getPlayer().isOp()) {
|
|
|
|
this.updateRepo(event.getPlayer())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Listener({ servers: [constants.ServerType.Sponge] })
|
|
|
|
ClientConnectionEvent$Join(event: org.spongepowered.api.event.network.ClientConnectionEvent.Join) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this.bungeeCordDetect(event.getTargetEntity())
|
2020-08-14 08:07:17 +00:00
|
|
|
if (event.getTargetEntity().hasPermission('ms.mpm.admin')) {
|
|
|
|
this.updateRepo(event.getTargetEntity())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
sendBungeeCordMessage(sender, message) {
|
2020-08-14 08:07:17 +00:00
|
|
|
let players = this.server.getOnlinePlayers()
|
|
|
|
if (players.length) {
|
2020-09-24 05:51:18 +00:00
|
|
|
this.bungee.for(players[0]).message(sender, message).send()
|
2020-08-14 08:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.channelOff?.off()
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
@Cmd({ servers: [`!${constants.ServerType.Bungee}`] })
|
2020-09-24 05:51:18 +00:00
|
|
|
gmspm(sender: any, command: string, args: string[]) {
|
|
|
|
if (!sender.hasPermission('mspm.admin')) { return this.i18n(sender, 'main.command.no.permission') }
|
|
|
|
if (!this.isBungeeCord) return this.logger.sender(sender, '§c当前服务器尚未检测到 BungeeCord 链接...')
|
2020-09-22 10:43:02 +00:00
|
|
|
this.taskManager.create(() => this.main(sender, command, args)).async().submit()
|
2020-08-14 08:07:17 +00:00
|
|
|
this.bungeeCordForward(sender, { sender: sender.getName(), command, args })
|
2020-10-02 10:54:10 +00:00
|
|
|
this.logger.sender(sender, `§6[§3BPM§6][§a${this.serverName}§6] §6命令 §b/mspm ${args.join?.(' ')} §a发布成功!`)
|
2020-08-14 08:07:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
@Cmd({ servers: [constants.ServerType.Bungee] })
|
2020-09-24 05:51:18 +00:00
|
|
|
bungeemspm(sender: any, command: string, args: string[]) {
|
|
|
|
if (!sender.hasPermission('mspm.admin')) { return this.i18n(sender, 'main.command.no.permission') }
|
2020-09-17 09:44:48 +00:00
|
|
|
this.taskManager.create(() => this.main(sender, command, args)).async().submit()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Cmd({ servers: [`!${constants.ServerType.Bungee}`] })
|
2020-09-24 05:51:18 +00:00
|
|
|
mspm(sender: any, command: string, args: string[]) {
|
|
|
|
if (!sender.hasPermission('mspm.admin')) { return this.i18n(sender, 'main.command.no.permission') }
|
2020-06-20 08:40:34 +00:00
|
|
|
this.taskManager.create(() => this.main(sender, command, args)).async().submit()
|
2019-09-22 15:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
i18n(sender: any, name: string, params?: any) {
|
2020-08-14 08:07:17 +00:00
|
|
|
this.logger.sender(sender, this.translate.translate(name, params).split('\n'))
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 15:31:52 +00:00
|
|
|
main(sender: any, command: string, args: string[]) {
|
2020-05-13 10:42:05 +00:00
|
|
|
let cmdKey = 'cmd' + (args[0] || 'help')
|
2020-05-11 09:22:45 +00:00
|
|
|
if (!this[cmdKey]) {
|
2020-05-11 16:07:05 +00:00
|
|
|
this.i18n(sender, 'main.command.not.exists', { command: args[0] })
|
|
|
|
this.i18n(sender, 'main.command.help.tip', { command })
|
2020-06-20 08:40:34 +00:00
|
|
|
return
|
2019-09-22 15:31:52 +00:00
|
|
|
}
|
2020-03-30 10:49:19 +00:00
|
|
|
args.shift()
|
2020-06-20 08:40:34 +00:00
|
|
|
this[cmdKey](sender, ...args)
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 16:07:05 +00:00
|
|
|
cmdhelp(sender: any) {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.logger.sender(sender, help)
|
2020-05-11 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
cmdinfo(sender: any) {
|
|
|
|
this.logger.sender(sender, [
|
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2020-10-02 10:54:10 +00:00
|
|
|
cmdsudo(sender: any, name: string, ...args: string[]) {
|
|
|
|
this.taskManager.create(() => {
|
|
|
|
let player = this.server.getPlayer(name)
|
|
|
|
if (!player) {
|
|
|
|
return this.logger.sender(sender, `§4玩家 ${player.getName()} 不在线或不存在!`)
|
|
|
|
}
|
|
|
|
let command = args.join(' ')
|
|
|
|
this.server.dispatchCommand(player, command)
|
|
|
|
this.logger.sender(sender, `§6玩家 §a${player.getName()} §6命令 §b${command} §a执行完成!`)
|
|
|
|
}).submit()
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:22:45 +00:00
|
|
|
cmdload(sender: any, name: string) {
|
2020-06-20 08:40:34 +00:00
|
|
|
let pluginFile = fs.concat(__dirname + '', name)
|
2020-05-11 09:22:45 +00:00
|
|
|
if (!fs.exists(pluginFile)) {
|
2020-05-11 16:07:05 +00:00
|
|
|
this.i18n(sender, 'plugin.not.exists', { name: `${name}(${pluginFile})` })
|
2020-06-20 08:40:34 +00:00
|
|
|
return
|
2020-05-11 09:22:45 +00:00
|
|
|
}
|
2020-06-20 08:40:34 +00:00
|
|
|
this.pluginManager.loadFromFile(fs.file(pluginFile))
|
2020-05-11 09:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 09:13:48 +00:00
|
|
|
cmdlist(sender: any, type: string = 'cloud') {
|
2020-05-13 10:42:05 +00:00
|
|
|
if (type == "i" || type == "install") {
|
2020-05-07 16:50:30 +00:00
|
|
|
this.i18n(sender, 'list.install.header')
|
2020-03-30 10:49:19 +00:00
|
|
|
this.pluginManager.getPlugins().forEach((plugin) => {
|
2020-07-25 07:24:02 +00:00
|
|
|
this.i18n(sender, 'list.install.body', { ...plugin.description, from: plugin.description.loadMetadata.type })
|
2020-03-30 10:49:19 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.i18n(sender, 'list.header')
|
|
|
|
for (var pkgName in this.packageCache) {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.i18n(sender, 'list.body', this.packageCache[pkgName])
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdinstall(sender: any, name: string) {
|
|
|
|
if (!name) { return this.i18n(sender, 'plugin.name.empty') }
|
2020-09-17 09:44:48 +00:00
|
|
|
if (this.pluginManager.getPlugins().has(name)) {
|
2020-10-02 10:54:10 +00:00
|
|
|
this.i18n(sender, 'install.already', { name, version: this.pluginManager.getPlugins().get(name).description.version })
|
2020-09-17 09:44:48 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
this.download(sender, name, false, () => {
|
|
|
|
let plugin = this.pluginManager.loadFromFile(fs.concat(root, this.pluginFolder, name + '.js'))
|
|
|
|
this.i18n(sender, 'install.finish', { name, version: plugin.description.version })
|
|
|
|
})
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdupdate(sender: any, name: string) {
|
|
|
|
if (name) {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.update(sender, name)
|
2020-03-30 10:49:19 +00:00
|
|
|
} else {
|
|
|
|
this.updateRepo(sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:13:18 +00:00
|
|
|
cmdupgrade(sender: any, name: string, confirm: boolean) {
|
|
|
|
if (name == "system") {
|
|
|
|
if (!confirm) { return this.i18n(sender, 'upgrade.confirm') }
|
|
|
|
let enginePath = fs.path(fs.file(root, 'node_modules'))
|
2020-05-07 16:50:30 +00:00
|
|
|
if (enginePath.startsWith(root)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
try {
|
|
|
|
base.delete(enginePath)
|
|
|
|
this.cmdrestart(sender)
|
|
|
|
} catch (ex) {
|
|
|
|
if (global.debug) {
|
|
|
|
console.ex(ex)
|
|
|
|
}
|
|
|
|
if (fs.exists(enginePath)) {
|
|
|
|
this.i18n(sender, 'upgrade.failed')
|
|
|
|
fs.create(fs.file(root, 'upgrade'))
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 16:50:30 +00:00
|
|
|
}
|
2020-07-06 08:13:18 +00:00
|
|
|
return
|
2020-05-07 16:50:30 +00:00
|
|
|
}
|
|
|
|
if (this.checkPlugin(sender, name)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.update(sender, name, () => this.reload(sender, name))
|
2020-05-07 16:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
cmdunload(sender: any, name: string) {
|
|
|
|
if (this.checkPlugin(sender, name)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
let plugin = this.pluginManager.getPlugins().get(name)
|
|
|
|
this.i18n(sender, 'plugin.unload.start', { name, version: plugin.description.version })
|
|
|
|
this.pluginManager.disable(plugin)
|
|
|
|
this.i18n(sender, 'plugin.unload.finish', { name, version: plugin.description.version })
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdreload(sender: any, name: string) {
|
2020-05-07 10:33:10 +00:00
|
|
|
name = name || this.description.name
|
2020-09-22 10:43:02 +00:00
|
|
|
this.reload(sender, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
private reload(sender: any, name: string) {
|
2020-03-30 10:49:19 +00:00
|
|
|
if (this.checkPlugin(sender, name)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
let plugin = this.pluginManager.getPlugins().get(name)
|
|
|
|
this.i18n(sender, 'plugin.reload.start', { name, version: plugin.description.version })
|
|
|
|
this.pluginManager.reload(plugin)
|
|
|
|
this.i18n(sender, 'plugin.reload.finish', { name, version: this.pluginManager.getPlugins().get(name).description.version })
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPlugin(sender: any, name: string) {
|
|
|
|
if (name && this.pluginManager.getPlugins().has(name)) { return true }
|
|
|
|
this.i18n(sender, 'plugin.not.exists', { name })
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:13:48 +00:00
|
|
|
checkCloudPlugin(sender: any, name: string) {
|
|
|
|
if (name && this.packageNameCache.includes(name)) { return true }
|
|
|
|
this.i18n(sender, 'cloud.not.exists', { name })
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
cmdrestart(sender: any) {
|
|
|
|
if (this.serverType === "sponge") {
|
|
|
|
setTimeout(() => this.server.dispatchConsoleCommand('sponge plugins reload'), 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.logger.sender(sender, '§6Reloading §3MiaoScript Engine...')
|
|
|
|
ScriptEngineContextHolder.disableEngine()
|
|
|
|
Packages.java.lang.System.gc()
|
|
|
|
ScriptEngineContextHolder.enableEngine()
|
|
|
|
this.logger.sender(sender, '§3MiaoScript Engine §6Reload §aSuccessful...')
|
2020-03-30 10:49:19 +00:00
|
|
|
} catch (ex) {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.logger.sender(sender, "§3MiaoScript Engine §6Reload §cError! ERR: " + ex)
|
|
|
|
this.logger.sender(sender, this.logger.stack(ex))
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:13:48 +00:00
|
|
|
cmdrun(sender: any, ...args: any[]) {
|
2020-03-30 10:49:19 +00:00
|
|
|
try {
|
2020-06-20 08:40:34 +00:00
|
|
|
let script = args.join(' ')
|
2020-05-11 16:07:05 +00:00
|
|
|
this.i18n(sender, 'run.script', { script })
|
2020-09-24 10:36:53 +00:00
|
|
|
let result = this.runCode(script, sender, this)
|
2020-05-13 10:42:05 +00:00
|
|
|
this.i18n(sender, 'run.result', { result: result == undefined ? this.translate.translate('run.noresult') : result + '' })
|
2020-03-30 10:49:19 +00:00
|
|
|
} catch (ex) {
|
2020-06-20 08:40:34 +00:00
|
|
|
this.logger.sender(sender, this.logger.stack(ex))
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:36:53 +00:00
|
|
|
cmdprun(sender: any, name: string, ...args: any[]) {
|
|
|
|
try {
|
|
|
|
if (!this.pluginManager.getPlugins().has(name)) { return this.logger.sender(sender, `§4插件 §c${name} §4不存在!`) }
|
|
|
|
let script = args.join(' ')
|
|
|
|
this.i18n(sender, 'prun.script', { name })
|
|
|
|
this.i18n(sender, 'run.script', { script })
|
|
|
|
let result = this.runCode(script, sender, this.pluginManager.getPlugins().get(name))
|
2020-10-02 10:54:10 +00:00
|
|
|
this.i18n(sender, 'run.result', { result: result == undefined ? this.translate.translate('run.noresult') : typeof result == "string" ? result : JSON.stringify(result) })
|
2020-09-24 10:36:53 +00:00
|
|
|
} catch (ex) {
|
|
|
|
this.logger.sender(sender, this.logger.stack(ex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private runCode(code: string, sender: any, _this: any) {
|
2020-06-20 08:40:34 +00:00
|
|
|
let paramNames = [
|
|
|
|
'sender',
|
|
|
|
'reflect',
|
|
|
|
'container',
|
|
|
|
'pluginManager'
|
|
|
|
]
|
|
|
|
let params = [
|
|
|
|
sender,
|
|
|
|
reflect,
|
|
|
|
container,
|
|
|
|
this.pluginManager
|
|
|
|
]
|
|
|
|
let tfunc = new Function(
|
|
|
|
...paramNames,
|
|
|
|
`var api = require('@ccms/api');
|
|
|
|
if (this.serverType == "spring") {
|
|
|
|
var dbm = container.get(api.database.DataBaseManager)
|
|
|
|
var db = dbm.getMainDatabase()
|
|
|
|
var df = base.getInstance().getAutowireCapableBeanFactory()
|
|
|
|
}
|
2020-10-02 10:54:10 +00:00
|
|
|
return eval(${JSON.stringify(code)});`)
|
|
|
|
return tfunc.apply(_this, params)
|
2020-06-20 08:40:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 09:13:48 +00:00
|
|
|
cmddeploy(sender: any, name: any) {
|
2020-05-11 16:07:05 +00:00
|
|
|
if (!process.env.AccessToken) { return this.i18n(sender, 'deploy.token.not.exists') }
|
2020-05-07 09:13:48 +00:00
|
|
|
this.taskManager.create(() => {
|
|
|
|
if (this.checkPlugin(sender, name)) {
|
2020-06-20 08:40:34 +00:00
|
|
|
let plugin: pluginApi.Plugin = this.pluginManager.getPlugins().get(name)
|
2020-05-11 16:07:05 +00:00
|
|
|
let result = http.post("http://ms.yumc.pw/api/plugin/deploy?access_token=" + process.env.AccessToken, {
|
2020-05-07 09:13:48 +00:00
|
|
|
name,
|
|
|
|
author: plugin.description.author,
|
|
|
|
version: plugin.description.version,
|
2020-07-06 08:13:18 +00:00
|
|
|
source: base.read((plugin.description.source || plugin.description.loadMetadata.file).toString())
|
2020-05-07 09:13:48 +00:00
|
|
|
})
|
2020-09-24 07:27:51 +00:00
|
|
|
this.i18n(sender, result.code == 200 ? 'deploy.success' : 'deploy.fail', { name, version: plugin.description.version, msg: result.msg })
|
2020-05-07 09:13:48 +00:00
|
|
|
}
|
|
|
|
}).async().submit()
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
private update(sender: any, name: string, callback?: () => void) {
|
2020-05-07 09:13:48 +00:00
|
|
|
if (this.checkCloudPlugin(sender, name)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.download(sender, name, true, () => {
|
|
|
|
this.i18n(sender, 'update.finish', { name, version: this.packageCache[name].version })
|
|
|
|
callback?.()
|
|
|
|
})
|
2019-09-22 15:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
@Tab({ alias: ['gmspm', 'bungeemspm'] })
|
2020-09-24 07:27:51 +00:00
|
|
|
tabmspm(_sender: any, _command: any, args: string | any[]) {
|
2020-10-02 10:54:10 +00:00
|
|
|
if (args.length === 1) { return ['list', 'install', 'update', 'upgrade', 'reload', 'restart', 'run', 'prun', 'sudo', 'help', 'create', 'deploy'] }
|
2019-09-22 15:31:52 +00:00
|
|
|
if (args.length > 1) {
|
|
|
|
switch (args[0]) {
|
2020-03-30 10:49:19 +00:00
|
|
|
case "list":
|
|
|
|
return ["install", "cloud"]
|
2019-09-22 15:31:52 +00:00
|
|
|
case "install":
|
2020-06-20 08:40:34 +00:00
|
|
|
return this.packageNameCache
|
2019-09-22 15:31:52 +00:00
|
|
|
case "upgrade":
|
2020-07-06 08:13:18 +00:00
|
|
|
if (args.length == 2) return ["system", ...this.pluginManager.getPlugins().keys()]
|
|
|
|
if (args.length == 3 && args[1] == "system") return ["confirm"]
|
|
|
|
return []
|
2020-09-24 10:36:53 +00:00
|
|
|
case "prun":
|
|
|
|
if (args.length == 2) return [...this.pluginManager.getPlugins().keys()]
|
|
|
|
return []
|
2020-10-02 10:54:10 +00:00
|
|
|
case "sudo":
|
|
|
|
if (args.length == 2) return Java.from(this.server.getOnlinePlayers()).map(p => p.getName())
|
|
|
|
return []
|
2020-07-06 08:13:18 +00:00
|
|
|
case "update":
|
2020-02-23 16:13:19 +00:00
|
|
|
case "load":
|
|
|
|
case "unload":
|
2019-09-22 15:31:52 +00:00
|
|
|
case "reload":
|
2020-05-07 10:33:10 +00:00
|
|
|
case "deploy":
|
2020-06-20 08:40:34 +00:00
|
|
|
return [...this.pluginManager.getPlugins().keys()]
|
2019-09-22 15:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 10:49:19 +00:00
|
|
|
|
|
|
|
updateRepo(sender: any) {
|
|
|
|
this.taskManager.create(() => {
|
2020-06-20 08:40:34 +00:00
|
|
|
let result = http.get('http://ms.yumc.pw/api/plugin/list')
|
|
|
|
for (const pl of result.data) { this.packageCache[pl.name] = pl }
|
|
|
|
this.packageNameCache = Object.keys(this.packageCache)
|
2020-03-30 10:49:19 +00:00
|
|
|
this.i18n(sender, 'cloud.update.finish', { length: this.packageNameCache.length })
|
2020-08-14 08:07:17 +00:00
|
|
|
this.pluginManager.getPlugins().forEach(p => {
|
|
|
|
let cloudPlugin = this.packageCache[p.description.name]
|
|
|
|
//§6插件名称: §b{name}\n§6版本: §a{version}\n§6作者: §3{author}\§6更新时间: §9{updated_at}
|
|
|
|
if (cloudPlugin && cloudPlugin.version != p.description.version) {
|
|
|
|
this.i18n(sender, 'cloud.update.exists', { name: p.description.name, new_version: cloudPlugin.version, old_version: p.description.version })
|
|
|
|
}
|
|
|
|
})
|
2020-06-20 08:40:34 +00:00
|
|
|
}).async().submit()
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
download(sender: any, name: string, update: boolean, callback?: () => void) {
|
2020-05-07 09:13:48 +00:00
|
|
|
this.taskManager.create(() => {
|
2020-09-22 10:43:02 +00:00
|
|
|
let pluginPkg = this.packageCache[name]
|
|
|
|
this.i18n(sender, 'download.start', { name, version: pluginPkg.version })
|
|
|
|
this.i18n(sender, 'download.url', { url: pluginPkg.url })
|
2020-05-13 10:42:05 +00:00
|
|
|
let pluginFile = update ? fs.concat(root, this.pluginFolder, 'update', name + '.js') : fs.concat(root, this.pluginFolder, name + '.js')
|
2020-09-22 10:43:02 +00:00
|
|
|
http.download(pluginPkg.url, pluginFile)
|
|
|
|
this.i18n(sender, 'download.finish', { name, version: pluginPkg.version })
|
|
|
|
callback?.()
|
2020-05-07 09:13:48 +00:00
|
|
|
}).async().submit()
|
2020-03-30 10:49:19 +00:00
|
|
|
}
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|