Skip to content

Commit

Permalink
mods folder lock
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 26, 2023
1 parent d27ce0b commit 8e716b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
6 changes: 4 additions & 2 deletions main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "1.9.21"
groovy
`java-gradle-plugin`
id("maven-publish")
Expand Down Expand Up @@ -65,11 +66,12 @@ val arcVersion: String by project
val jarIncluding = configurations.create("including")
dependencies {
jarIncluding("com.github.anuken.arc:arc-core:$arcVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
compileOnly("com.github.anuken.arc:arc-core:$arcVersion")
implementation("org.hjson:hjson:3.0.0")
implementation("org.hjson:hjson:3.0.1")
implementation("com.google.code.gson:gson:2.9.0")
testCompileOnly("com.github.anuken.arc:arc-core:$arcVersion")
testImplementation("org.hjson:hjson:3.0.0")
testImplementation("org.hjson:hjson:3.0.1")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.8.22")
Expand Down
2 changes: 1 addition & 1 deletion main/src/task/ResolveModpack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ open class ResolveModpack : DefaultTask() {
}
}

data class ModLock(
private data class ModLock(
val path: String,
)
48 changes: 46 additions & 2 deletions main/src/task/RunMindustryAbstract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package io.github.liplum.mindustry
import io.github.liplum.dsl.*
import io.github.liplum.dsl.fileProp
import io.github.liplum.dsl.stringsProp
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.gradle.api.tasks.*
import java.io.File

private
const val lockFile = ".lock.json"

abstract class RunMindustryAbstract : JavaExec() {
val gameFile = project.fileProp()
@InputFile get
Expand All @@ -20,12 +26,15 @@ abstract class RunMindustryAbstract : JavaExec() {
mainClass.set("-jar")
}

fun loadMods(modsDir: File) {
protected fun loadMods(modsDir: File) {
modsDir.mkdirs()
val lastLock = readLock(modsDir)
// parse lock
if (modsDir.isDirectory) {
// TODO: Record the mod signature, and don't always delete all.
modsDir.deleteRecursively()
}

for (modFile in mods) {
if (modFile.isFile) {
createSymbolicLinkOrCopy(link = modsDir.resolve(modFile.name), target = modFile)
Expand All @@ -34,4 +43,39 @@ abstract class RunMindustryAbstract : JavaExec() {
}
}
}
}

/**
* @return (existing, missing)
*/
private fun restoreLastLock(modsDir: File): Pair<List<File>, List<File>> {
val lastLock = readLock(modsDir)
val files = modsDir.listFiles() ?: arrayOf()
val restored = files.groupBy { lastLock.mods[it.name] != null }
return Pair(restored[true]!!, restored[false]!!)
}

private fun readLock(modsDir: File): Mods4LoadLock {
val lockFile = modsDir.resolve(lockFile)
return runCatching {
val text = lockFile.readText(charset = Charsets.UTF_8)
return Json.decodeFromString(text)
}.getOrNull() ?: Mods4LoadLock()
}

private fun writeLock(modsDir: File, lock: Mods4LoadLock) {
val lockFile = modsDir.resolve(lockFile)
val json = Json.encodeToString(lock)
lockFile.writeText(json, charset = Charsets.UTF_8)
}
}

@Serializable
private data class Mod4LoadLock(
val fileName: String,
)

@Serializable
private data class Mods4LoadLock(
/** [Mod4LoadLock.fileName] to [Mod4LoadLock]*/
val mods: Map<String, Mod4LoadLock> = mapOf(),
)

0 comments on commit 8e716b0

Please sign in to comment.