sync
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import cc.maxmc.msm.child.misc.SubServer
|
||||
import kotlin.io.path.Path
|
||||
|
||||
suspend fun main() {
|
||||
val server = SubServer(10086, "test", 25571, Path("/Users/tony_all/Servers/MSM/"))
|
||||
server.initServer()
|
||||
server.startServer()
|
||||
Thread.sleep(10000)
|
||||
server.cleanup()
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cc.maxmc.msm.child.misc
|
||||
|
||||
import cc.maxmc.msm.child.MultiServerMan
|
||||
import cc.maxmc.msm.child.utils.ScriptRunner
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.copyToRecursively
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.deleteRecursively
|
||||
|
||||
@OptIn(kotlin.io.path.ExperimentalPathApi::class)
|
||||
class SubServer(
|
||||
val id: Int,
|
||||
val type: String,
|
||||
val port: Int,
|
||||
baseFolder: Path = MultiServerMan.instance.dataFolder.toPath()
|
||||
) {
|
||||
private val serverFolder = baseFolder.resolve("cache").resolve(id.toString()).also { it.createDirectories() }
|
||||
private val patternFolder = baseFolder.resolve("pattern").resolve(type).also { it.createDirectories() }
|
||||
private val runner = ScriptRunner(serverFolder.resolve("start.sh"))
|
||||
|
||||
suspend fun initServer() = withContext(Dispatchers.IO) {
|
||||
patternFolder.copyToRecursively(serverFolder, followLinks = false, overwrite = true)
|
||||
}
|
||||
|
||||
fun startServer() {
|
||||
runner.launch(port.toString())
|
||||
runner.onOutput("For help, type \"help\" or \"?\"") {
|
||||
println("Started.")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun cleanup() {
|
||||
runner.exec("stop")
|
||||
println("Exec stop")
|
||||
runner.exit()
|
||||
serverFolder.deleteRecursively()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package cc.maxmc.msm.child.utils
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.nio.file.Path
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import kotlin.concurrent.thread
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.io.path.name
|
||||
|
||||
class ScriptRunner(private val scriptPath: Path) {
|
||||
private lateinit var process: Process
|
||||
lateinit var thread: Thread
|
||||
private val listeners = CopyOnWriteArrayList<Pair<String, () -> Unit>>()
|
||||
private val writer by lazy { process.outputStream.bufferedWriter() }
|
||||
private val reader by lazy { process.inputStream.bufferedReader() }
|
||||
|
||||
fun launch(vararg args: String) {
|
||||
val builder = ProcessBuilder().directory(scriptPath.parent.toFile())
|
||||
if (scriptPath.extension == "sh") {
|
||||
builder.command("bash", scriptPath.name, *args)
|
||||
} else {
|
||||
builder.command("powershell.exe", scriptPath.name, *args)
|
||||
}
|
||||
process = builder.start()
|
||||
thread = thread {
|
||||
while (true) {
|
||||
if (listeners.isEmpty()) {
|
||||
continue
|
||||
}
|
||||
var line = ""
|
||||
while (reader.readLine()?.also { line = it } != null) {
|
||||
println(line)
|
||||
listeners.forEach {
|
||||
if (line.contains(it.first)) it.second()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun exec(command: String) {
|
||||
writer.appendLine(command)
|
||||
writer.flush()
|
||||
}
|
||||
|
||||
fun onOutput(string: String, func: () -> Unit) {
|
||||
listeners.add(string to func)
|
||||
}
|
||||
|
||||
suspend fun exit(): Int = withContext(Dispatchers.IO) {
|
||||
process.waitFor()
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,4 @@ parent:
|
||||
ports:
|
||||
- 30000
|
||||
- 30001..30019
|
||||
-
|
||||
- 30443
|
||||
|
||||
Reference in New Issue
Block a user