[Editor] Create Application Runner which allows to run game from the UI

This commit is contained in:
2021-02-26 17:59:32 +01:00
parent 6971780f05
commit 1c8afd14e6
7 changed files with 112 additions and 2 deletions

View File

@@ -98,7 +98,6 @@ class DefaultBuildPipelineService : BuildPipelineService {
}
companion object {
private const val OUTPUT_JAR_NAME = "game.jar"
private const val TAG = "Build"
}
}

View File

@@ -3,12 +3,14 @@ package com.bartlomiejpluta.base.editor.main.view
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
import com.bartlomiejpluta.base.editor.main.controller.MainController
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import com.bartlomiejpluta.base.editor.process.runner.app.ApplicationRunner
import tornadofx.*
class MainMenuView : View() {
private val mainController: MainController by di()
private val projectContext: ProjectContext by di()
private val buildPipelineService: BuildPipelineService by di()
private val applicationRunner: ApplicationRunner by di()
override val root = menubar {
menu("File") {
@@ -18,7 +20,6 @@ class MainMenuView : View() {
}
}
item("Open project...") {
action {
mainController.openProject()
@@ -61,6 +62,13 @@ class MainMenuView : View() {
buildPipelineService.build()
}
}
item("Run") {
enableWhen(applicationRunner.isRunningProperty.not())
action {
applicationRunner.run()
}
}
}
}
}

View File

@@ -0,0 +1,8 @@
package com.bartlomiejpluta.base.editor.process.runner.app
import javafx.beans.binding.BooleanExpression
interface ApplicationRunner {
val isRunningProperty: BooleanExpression
fun run()
}

View File

@@ -0,0 +1,63 @@
package com.bartlomiejpluta.base.editor.process.runner.app
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
import com.bartlomiejpluta.base.editor.process.runner.jar.JarRunner
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import com.bartlomiejpluta.base.editor.project.model.Project
import javafx.event.EventHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tornadofx.getValue
import tornadofx.runAsync
import tornadofx.setValue
import tornadofx.toProperty
@Component
class DefaultApplicationRunner : ApplicationRunner {
@Autowired
private lateinit var projectContext: ProjectContext
@Autowired
private lateinit var pipelineService: BuildPipelineService
@Autowired
private lateinit var jarRunner: JarRunner
override val isRunningProperty = false.toProperty()
private var isRunning by isRunningProperty
override fun run() {
projectContext.project?.let { project ->
if (isRunning) {
return@let
}
isRunning = true
pipelineService.build().onSucceeded = EventHandler { runApplication(project) }
}
}
private fun runApplication(project: Project) {
val builder = jarRunner.run(project.buildOutputJarFile)
val process = builder.start()
runAsync {
process.inputStream.bufferedReader().forEachLine {
println("OUTPUT -> $it")
}
}
runAsync {
process.errorStream.bufferedReader().forEachLine {
println("ERROR -> $it")
}
}
process.onExit().thenApply {
println("EXIT WITH ${it.exitValue()}")
isRunning = false
}
}
}

View File

@@ -0,0 +1,19 @@
package com.bartlomiejpluta.base.editor.process.runner.jar
import org.springframework.stereotype.Component
import java.io.File
@Component
class DefaultJarRunner : JarRunner {
private val javaHome = System.getProperty("java.home")
private val classpath = System.getProperty("java.class.path")
private val javaBin = "$javaHome${FS}bin${FS}java" // == $JAVA_HOME/bin/java
override fun run(jarFile: File, jvmArgs: Array<String>, args: Array<String>) = listOf(
javaBin, *jvmArgs, "-cp", classpath, "-jar", jarFile.absolutePath, *args
).let { ProcessBuilder(it) }
companion object {
private val FS = File.separator
}
}

View File

@@ -0,0 +1,7 @@
package com.bartlomiejpluta.base.editor.process.runner.jar
import java.io.File
interface JarRunner {
fun run(jarFile: File, jvmArgs: Array<String> = emptyArray(), args: Array<String> = emptyArray()): ProcessBuilder
}

View File

@@ -59,6 +59,10 @@ class Project {
var buildOutDirectory by buildOutDirectoryProperty
private set
val buildOutputJarFileProperty =
createObjectBinding({ File(buildOutDirectory, PROJECT_OUTPUT_JAR_FILE) }, buildOutDirectoryProperty)
val buildOutputJarFile by buildOutputJarFileProperty
init {
sourceDirectoryProperty.addListener { _, _, dir ->
dir?.let {
@@ -83,6 +87,8 @@ class Project {
companion object {
const val PROJECT_FILE = "project.bep"
const val PROJECT_OUTPUT_JAR_FILE = "game.jar"
const val MAPS_DIR = "maps"
const val TILESETS_DIR = "tilesets"
const val IMAGES_DIR = "images"