Skip to content

Commit

Permalink
override game location and data dir by local properties
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 21, 2023
1 parent 2441cb4 commit 60a6dee
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions TestProjectKt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ runMindustry {
addClient("from url") {
url("https://github.com/Anuken/Mindustry/releases/download/v146/Mindustry.jar")
useModpack("from url") {
url("https://github.com/liplum/MultiCrafterLib/releases/download/v1.8/MultiCrafterLib-1.8.jar")
url("https://github.com/liplum/MultiCrafterLib/releases/download/v1.8/MultiCrafterLib-1.8.jar")
}
}
addClient("debugging") {
Expand All @@ -127,7 +127,7 @@ runMindustry {
official(version = latest)
}
addClient(name = "From local.properties") {
// localProperties("mgpp.local-client")

}
addServer {
modpack = null
Expand Down
5 changes: 4 additions & 1 deletion main/src/LocalProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ object LocalProperties {
return properties
}

val Project.local: PropertiesSpec
val Project.localProp: PropertiesSpec
get() = PropertiesSpec(localProperties)

/**
* The cache should be clear when plugin starts being applied
*/
fun clearCache(project: Project? = null) {
_properties = null
project?.logger?.info("local.properties cache was cleared.")
Expand Down
1 change: 1 addition & 0 deletions main/src/plugin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.tasks.TaskProvider

class MindustryPlugin : Plugin<Project> {
override fun apply(target: Project) = target.func {
LocalProperties.clearCache(project)
GroovyBridge.attach(target)
// Register this for dynamically configure tasks without class reference in groovy.
if (plugins.hasPlugin<JavaPlugin>()) {
Expand Down
26 changes: 21 additions & 5 deletions main/src/plugin/Run.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import io.github.liplum.dsl.*
import io.github.liplum.dsl.afterEvaluateThis
import io.github.liplum.dsl.getOrCreate
import io.github.liplum.dsl.register
import io.github.liplum.mindustry.LocalProperties.localProp
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.JavaPlugin

/**
Expand Down Expand Up @@ -47,13 +47,21 @@ class MindustryRunPlugin : Plugin<Project> {
for (client in x.clients) {
val resolveClient = proj.tasks.register<ResolveGame>("resolveClient${client.name}") {
group = R.taskGroup.mindustryStuff
location.set(client.location)
val gameLocalProp = proj.localProp[client.locationLocalPropKey]
location.set(
if (gameLocalProp != null) LocalGameLoc(gameLocalProp)
else client.location
)
}
proj.tasks.register<RunClient>("runClient${client.name}") {
group = R.taskGroup.mindustry
dependsOn(resolveClient)
startupArgs.addAll(client.startupArgs)
dataDir.set(client.dataDir)
val dataDirLocalProp = proj.localProp[client.dataDirLocalPropKey]
dataDir.set(
if (dataDirLocalProp != null) LocalDataDirLoc(dataDirLocalProp)
else client.dataDir
)
mindustryFile.set(proj.provider {
resolveClient.get().outputs.files.singleFile
})
Expand Down Expand Up @@ -82,13 +90,21 @@ class MindustryRunPlugin : Plugin<Project> {
for (server in x.servers) {
val resolveServer = proj.tasks.register<ResolveGame>("resolveServer${server.name}") {
group = R.taskGroup.mindustryStuff
location.set(server.location)
val gameLocalProp = project.localProp[server.locationLocalPropKey]
location.set(
if (gameLocalProp != null) LocalGameLoc(gameLocalProp)
else server.location
)
}
proj.tasks.register<RunServer>("runServer${server.name}") {
group = R.taskGroup.mindustry
dependsOn(resolveServer)
startupArgs.addAll(server.startupArgs)
dataDir.set(server.dataDir)
val dataDirLocalProp = proj.localProp[server.dataDirLocalPropKey]
dataDir.set(
if (dataDirLocalProp != null) LocalDataDirLoc(dataDirLocalProp)
else server.dataDir
)
mindustryFile.set(proj.provider {
resolveServer.get().outputs.files.singleFile
})
Expand Down
13 changes: 11 additions & 2 deletions main/src/run/RunMindustry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.liplum.mindustry

import io.github.liplum.dsl.prop
import io.github.liplum.mindustry.LocalProperties.localProp
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
Expand Down Expand Up @@ -87,7 +88,11 @@ open class RunMindustryExtension(
name = newName.ifBlank { "__DEFAULT__" },
)
AddClientSpec(proj, client).config()
if (client.location == null) {
val locationLocalPropKey = client.locationLocalPropKey
val gameFromLocalProp = proj.localProp[locationLocalPropKey]
if (gameFromLocalProp != null) {
proj.logger.warn("Client<${client.name}> game location will be overridden by local properties at $locationLocalPropKey.")
} else if (client.location == null) {
proj.logger.warn("Client<${client.name}> location not specified")
}
clients.add(client)
Expand Down Expand Up @@ -168,7 +173,11 @@ open class RunMindustryExtension(
name = newName.ifBlank { "__DEFAULT__" },
)
AddServerSpec(proj, server).config()
if (server.location == null) {
val locationLocalPropKey = server.locationLocalPropKey
val gameFromLocalProp = proj.localProp[locationLocalPropKey]
if (gameFromLocalProp != null) {
proj.logger.warn("Server<${server.name}> game location will be overridden by local properties at $locationLocalPropKey.")
} else if (server.location == null) {
proj.logger.warn("Server<${server.name}> location not specified")
}
servers.add(server)
Expand Down
2 changes: 2 additions & 0 deletions main/src/run/model/DataDirLoc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ sealed interface IDataDirLoc : Serializable {
data class LocalDataDirLoc(
val dir: File
) : IDataDirLoc {
constructor(path: String) : this(File(path))

override fun resolveDir(task: Task, type: GameSideType): File = dir
}

Expand Down
18 changes: 16 additions & 2 deletions main/src/run/model/GameSide.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import org.gradle.api.Project
import java.io.File
import java.net.URL

enum class GameSideType {
Client, Server
enum class GameSideType(
val gradleName: String
) {
Client("client"),
Server("server")
}

open class GameSide(
Expand All @@ -27,6 +30,17 @@ open class GameSide(
var dataDir: IDataDirLoc? = null
var location: IGameLoc? = null
var modpack: String? = null
val gradleName get() = normalizeName4Gradle(name)
fun localPropKey(key: String): String {
val gradleName = gradleName
return if (gradleName.isBlank())
"mgpp.${type.gradleName}.${key}"
else
"mgpp.${type.gradleName}.${gradleName}.${key}"
}

val locationLocalPropKey get() = localPropKey("game")
val dataDirLocalPropKey get() = localPropKey("dataDir")
}

abstract class AddGameSideSpec<T : GameSide> {
Expand Down
2 changes: 2 additions & 0 deletions main/src/run/model/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ data class LatestMindustryBELoc(
data class LocalGameLoc(
val file: File,
) : IGameLoc {
constructor(path: String) : this(File(path))

override val fileName4Local: String = file.name

/**
Expand Down

0 comments on commit 60a6dee

Please sign in to comment.