4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-06-11 06:59:28 +02:00
modpacktools/src/main/kotlin/ley/anvil/modpacktools/util/config/Config.kt
LordMZTE a5d5562d93 improve docs
remove mergeTo function due to stdlib containing alternative
getModMetas now takes predicate
2020-08-15 20:49:29 +02:00

40 lines
1.2 KiB
Kotlin

package ley.anvil.modpacktools.util.config
import org.apache.commons.io.FileUtils
import java.io.File
class Config(val configName: String) {
val configLocation = File(configName)
val config = readConfig()
/**
* reads the [configLocation] file if it exists and the default config otherwise
*
* @return the Toml object of the config file
*/
private fun readConfig(): ConfigToml {
return if(exists) {
//parse file to toml
ConfigToml().read(configLocation) as ConfigToml
//reads config from resources if no config file exists as a default value. commands that require the config still won't run without it
} else ConfigToml().read(ClassLoader.getSystemResourceAsStream(configName)) as ConfigToml
}
/**
* Checks if the [configLocation] file exists
*
* @return true if the config file exists
*/
val exists: Boolean get() = configLocation.exists()
/**
* Copies the Config file from the resources into the project's folder
*/
fun copyConfig() {
//copy from resources
val conf = ClassLoader.getSystemResourceAsStream(configName)
FileUtils.copyInputStreamToFile(conf, configLocation)
conf!!.close()
}
}