diff --git a/src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt b/src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt index 02d40664..dc691d58 100644 --- a/src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt +++ b/src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt @@ -2,9 +2,9 @@ package teksturepako.pakku.api.data -import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import teksturepako.pakku.api.overrides.filterOverrides +import teksturepako.pakku.api.projects.Project import teksturepako.pakku.api.projects.ProjectSide import teksturepako.pakku.api.projects.ProjectType import teksturepako.pakku.api.projects.UpdateStrategy @@ -34,16 +34,19 @@ data class ConfigFile( private val overrides: MutableList = mutableListOf(), /** A mutable list of server overrides packed up with the modpack. */ - @SerialName("server_overrides") private val serverOverrides: MutableList = mutableListOf(), + private val serverOverrides: MutableList = mutableListOf(), /** A mutable list of client overrides packed up with the modpack. */ - @SerialName("client_overrides") private val clientOverrides: MutableList = mutableListOf(), + private val clientOverrides: MutableList = mutableListOf(), /** A map of project types to their respective paths. */ val paths: MutableMap = mutableMapOf(), /** A mutable map of _project slugs, names, IDs or filenames_ to _project configs_. */ - val projects: MutableMap = mutableMapOf() + val projects: MutableMap = mutableMapOf(), + + /** A mutable map of project aliases to their respective project slugs. */ + val projectAliases: MutableMap = mutableMapOf() ) { // -- PACK -- @@ -110,8 +113,8 @@ data class ConfigFile( data class ProjectConfig( var type: ProjectType? = null, var side: ProjectSide? = null, - @SerialName("update_strategy") var updateStrategy: UpdateStrategy? = null, - @SerialName("redistributable") var redistributable: Boolean? = null, + var updateStrategy: UpdateStrategy? = null, + var redistributable: Boolean? = null, var subpath: String? = null ) @@ -136,5 +139,5 @@ data class ConfigFile( fun readToResultFrom(path: String): Result = decodeToResult(path) } - suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = json) + suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = jsonSnakeCase) } \ No newline at end of file diff --git a/src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt b/src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt index 7d933d45..37cb50c8 100644 --- a/src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt +++ b/src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt @@ -2,6 +2,7 @@ package teksturepako.pakku.api.data import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy @OptIn(ExperimentalSerializationApi::class) val json = Json { @@ -18,3 +19,8 @@ val jsonEncodeDefaults = Json { encodeDefaults = true } +@OptIn(ExperimentalSerializationApi::class) +val jsonSnakeCase = Json(json) { + namingStrategy = JsonNamingStrategy.SnakeCase +} + diff --git a/src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt b/src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt index 082ec999..4dc74aa2 100644 --- a/src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt +++ b/src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt @@ -205,8 +205,9 @@ data class LockFile( return removed } - fun getProject(input: String): Project? = this.projects - .find { project -> input in project || project.files.any { input in it.fileName } } + fun getProject(input: String): Project? = (ConfigFile.readOrNull()?.projectAliases?.get(input) ?: input).let { transformedInput -> + this.projects.find { project -> transformedInput in project || project.files.any { transformedInput in it.fileName } } + } fun getProject(project: Project): Project? = this.projects.find { it isAlmostTheSameAs project } diff --git a/src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt b/src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt index dc53b9ad..c64ad8a0 100644 --- a/src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt +++ b/src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.StringFormat import kotlinx.serialization.serializer import teksturepako.pakku.api.data.PakkuException import teksturepako.pakku.api.data.json +import teksturepako.pakku.api.data.jsonSnakeCase import kotlin.io.path.Path import kotlin.io.path.readBytes import kotlin.io.path.readText @@ -19,13 +20,13 @@ fun readPathBytesOrNull(path: String): ByteArray? } inline fun decodeOrNew( - value: T, path: String, format: StringFormat = json + value: T, path: String, format: StringFormat = jsonSnakeCase ): T = readPathTextOrNull(path)?.let { runCatching { format.decodeFromString(format.serializersModule.serializer(), it) }.getOrElse { value } } ?: value inline fun decodeToResult( - path: String, format: StringFormat = json + path: String, format: StringFormat = jsonSnakeCase ): Result = readPathTextOrNull(path)?.let { runCatching { Result.success(format.decodeFromString(format.serializersModule.serializer(), it)) }.getOrElse { exception -> Result.failure(PakkuException("Error occurred while reading '$path': ${exception.message}")) diff --git a/src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt b/src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt index e9ab660b..0c9a0803 100644 --- a/src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt +++ b/src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt @@ -6,13 +6,14 @@ import kotlinx.serialization.StringFormat import kotlinx.serialization.encodeToString import teksturepako.pakku.api.actions.ActionError import teksturepako.pakku.api.data.json +import teksturepako.pakku.api.data.jsonSnakeCase import kotlin.io.path.* suspend inline fun writeToFile( value: T, path: String, overrideText: Boolean = false, - format: StringFormat = json + format: StringFormat = jsonSnakeCase ): ActionError? { val file = Path(path)