mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-08 17:20:11 +08:00
Custom Command Prefix & Fix jvm option classpath
invalid. (#66)
* Custom Command Prefix & Fix jvm option `classpath` invalid. * Modify the loading order of Console ClassLoader; Extended Libraries
This commit is contained in:
parent
0d66a9959a
commit
68690a62b1
@ -139,5 +139,5 @@ internal suspend fun ByteReadChannel.saveToContent(filepath: String) {
|
|||||||
|
|
||||||
|
|
||||||
internal fun getContent(filepath: String):File{
|
internal fun getContent(filepath: String):File{
|
||||||
return File(contentPath.absolutePath + "/" + filepath)
|
return File(contentPath, filepath)
|
||||||
}
|
}
|
||||||
|
@ -15,18 +15,25 @@ import java.awt.TextArea
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
import java.net.URLClassLoader
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.jar.JarFile
|
||||||
import javax.swing.JFrame
|
import javax.swing.JFrame
|
||||||
import javax.swing.JPanel
|
import javax.swing.JPanel
|
||||||
|
|
||||||
|
|
||||||
val contentPath by lazy {
|
val contentPath by lazy {
|
||||||
File(System.getProperty("user.dir") + "/content/").also {
|
File(System.getProperty("user.dir"), "content").also {
|
||||||
if (!it.exists()) {
|
if (!it.exists()) {
|
||||||
it.mkdirs()
|
it.mkdirs()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val extendedLibraries by lazy {
|
||||||
|
val file =
|
||||||
|
System.getProperty("mirai.libraries")?.let { File(it) } ?: File(System.getProperty("user.dir"), "libraries")
|
||||||
|
file.also { if (!it.exists()) it.mkdirs() }
|
||||||
|
}
|
||||||
|
|
||||||
object WrapperMain {
|
object WrapperMain {
|
||||||
internal var uiBarOutput = StringBuilder()
|
internal var uiBarOutput = StringBuilder()
|
||||||
private val uilog = StringBuilder()
|
private val uilog = StringBuilder()
|
||||||
@ -56,6 +63,7 @@ object WrapperMain {
|
|||||||
uiLog("正在进行版本检查\n")
|
uiLog("正在进行版本检查\n")
|
||||||
val dic = System.getProperty("user.dir")
|
val dic = System.getProperty("user.dir")
|
||||||
uiLog("工作目录: ${dic}\n")
|
uiLog("工作目录: ${dic}\n")
|
||||||
|
uiLog("扩展库目录: ${extendedLibraries}\n")
|
||||||
uiLog("若无法启动, 请尝试清除工作目录下/content/文件夹\n")
|
uiLog("若无法启动, 请尝试清除工作目录下/content/文件夹\n")
|
||||||
var uiOpen = true
|
var uiOpen = true
|
||||||
GlobalScope.launch {
|
GlobalScope.launch {
|
||||||
@ -94,6 +102,7 @@ object WrapperMain {
|
|||||||
|
|
||||||
private fun preStartInNonNative() {
|
private fun preStartInNonNative() {
|
||||||
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir"))
|
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir"))
|
||||||
|
println("All additional libraries are located at $extendedLibraries")
|
||||||
var type = WrapperProperties.determineConsoleType(WrapperProperties.content)
|
var type = WrapperProperties.determineConsoleType(WrapperProperties.content)
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
println("Starting Mirai Console $type, reset by clear /content/")
|
println("Starting Mirai Console $type, reset by clear /content/")
|
||||||
@ -138,25 +147,19 @@ object WrapperMain {
|
|||||||
val loader = MiraiClassLoader(
|
val loader = MiraiClassLoader(
|
||||||
CoreUpdater.getProtocolLib()!!,
|
CoreUpdater.getProtocolLib()!!,
|
||||||
ConsoleUpdater.getFile()!!,
|
ConsoleUpdater.getFile()!!,
|
||||||
null
|
WrapperMain::class.java.classLoader
|
||||||
)
|
)
|
||||||
|
|
||||||
loader.loadClass("net.mamoe.mirai.BotFactoryJvm")
|
loader.loadClass("net.mamoe.mirai.BotFactoryJvm")
|
||||||
|
loader.loadClass(
|
||||||
|
when (type) {
|
||||||
|
CONSOLE_PURE -> "net.mamoe.mirai.console.pure.MiraiConsolePureLoader"
|
||||||
|
CONSOLE_GRAPHICAL -> "net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader"
|
||||||
|
else -> return
|
||||||
|
}
|
||||||
|
).getMethod("load", String::class.java, String::class.java)
|
||||||
|
.invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion())
|
||||||
|
|
||||||
when (type) {
|
|
||||||
CONSOLE_PURE -> {
|
|
||||||
loader.loadClass(
|
|
||||||
"net.mamoe.mirai.console.pure.MiraiConsolePureLoader"
|
|
||||||
).getMethod("load", String::class.java, String::class.java)
|
|
||||||
.invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion())
|
|
||||||
}
|
|
||||||
CONSOLE_GRAPHICAL -> {
|
|
||||||
loader.loadClass(
|
|
||||||
"net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader"
|
|
||||||
).getMethod("load", String::class.java, String::class.java)
|
|
||||||
.invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,13 +172,38 @@ private class MiraiClassLoader(
|
|||||||
arrayOf(
|
arrayOf(
|
||||||
protocol.toURI().toURL(),
|
protocol.toURI().toURL(),
|
||||||
console.toURI().toURL()
|
console.toURI().toURL()
|
||||||
), parent
|
), null
|
||||||
)
|
) {
|
||||||
|
init {
|
||||||
|
extendedLibraries.listFiles { file ->
|
||||||
|
file.isFile && file.extension == "jar"
|
||||||
|
}?.forEach {
|
||||||
|
kotlin.runCatching {
|
||||||
|
/*
|
||||||
|
Confirm that the current jar is valid
|
||||||
|
确认当前jar是否有效
|
||||||
|
*/
|
||||||
|
JarFile(it).close()
|
||||||
|
addURL(it.toURI().toURL())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val parent0: ClassLoader? = parent
|
||||||
|
override fun findClass(name: String?): Class<*> {
|
||||||
|
return try {
|
||||||
|
super.findClass(name)
|
||||||
|
} catch (exception: ClassNotFoundException) {
|
||||||
|
if (parent0 == null) throw exception
|
||||||
|
parent0.loadClass(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private object WrapperProperties {
|
private object WrapperProperties {
|
||||||
val contentFile by lazy {
|
val contentFile by lazy {
|
||||||
File(contentPath.absolutePath + "/.wrapper.txt").also {
|
File(contentPath, ".wrapper.txt").also {
|
||||||
if (!it.exists()) it.createNewFile()
|
if (!it.exists()) it.createNewFile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.mamoe.mirai.console.command;
|
package net.mamoe.mirai.console.command;
|
||||||
|
|
||||||
import jdk.jfr.Description;
|
// import jdk.jfr.Description;
|
||||||
|
|
||||||
public class JCommandManager {
|
public class JCommandManager {
|
||||||
|
|
||||||
|
@ -149,11 +149,11 @@ object CommandManager : Job by {
|
|||||||
|
|
||||||
private suspend fun processCommandImpl(sender: CommandSender, fullCommand: String): Boolean {
|
private suspend fun processCommandImpl(sender: CommandSender, fullCommand: String): Boolean {
|
||||||
val blocks = fullCommand.split(" ")
|
val blocks = fullCommand.split(" ")
|
||||||
val commandHead = blocks[0].replace("/", "")
|
val commandHead = blocks[0] //.replace("/", "")
|
||||||
val args = blocks.drop(1)
|
val args = blocks.drop(1)
|
||||||
return registeredCommand[commandHead]?.run {
|
return registeredCommand[commandHead]?.run {
|
||||||
try {
|
try {
|
||||||
return onCommand(sender, blocks.drop(1)).also {
|
return onCommand(sender, ArrayList(args)).also {
|
||||||
if (it) {
|
if (it) {
|
||||||
PluginManager.onCommand(this, sender, args)
|
PluginManager.onCommand(this, sender, args)
|
||||||
} else {
|
} else {
|
||||||
|
@ -33,6 +33,7 @@ import java.util.*
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
object DefaultCommands {
|
object DefaultCommands {
|
||||||
|
private val commandPrefix = "mirai.command.prefix".property() ?: "/"
|
||||||
private suspend fun CommandSender.login(account: Long, password: String) {
|
private suspend fun CommandSender.login(account: Long, password: String) {
|
||||||
MiraiConsole.logger("[Bot Login]", 0, "login...")
|
MiraiConsole.logger("[Bot Login]", 0, "login...")
|
||||||
try {
|
try {
|
||||||
@ -59,7 +60,7 @@ object DefaultCommands {
|
|||||||
}
|
}
|
||||||
bot.login()
|
bot.login()
|
||||||
bot.subscribeMessages {
|
bot.subscribeMessages {
|
||||||
startsWith("/") { message ->
|
startsWith(commandPrefix) { message ->
|
||||||
if (bot.checkManager(this.sender.id)) {
|
if (bot.checkManager(this.sender.id)) {
|
||||||
val sender = if (this is GroupMessage) {
|
val sender = if (this is GroupMessage) {
|
||||||
GroupContactCommandSender(this.sender, this.subject)
|
GroupContactCommandSender(this.sender, this.subject)
|
||||||
|
@ -43,7 +43,7 @@ class MiraiConsoleUIPure : MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
thread {
|
thread(name = "Mirai Console Input Thread") {
|
||||||
while (true) {
|
while (true) {
|
||||||
val input = readLine() ?: return@thread
|
val input = readLine() ?: return@thread
|
||||||
if (requesting) {
|
if (requesting) {
|
||||||
|
Loading…
Reference in New Issue
Block a user