4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-06-11 06:59:28 +02:00

add and use MissingConfigValueException

minor cleanups
This commit is contained in:
LordMZTE 2020-07-29 23:43:34 +02:00
parent 312cb39459
commit 789cfa53b7
7 changed files with 103 additions and 55 deletions

View file

@ -5,8 +5,9 @@ package ley.anvil.modpacktools
import com.google.gson.GsonBuilder
import ley.anvil.modpacktools.command.CommandLoader
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.util.Config
import ley.anvil.modpacktools.util.ModpackJsonHandler
import ley.anvil.modpacktools.util.config.Config
import ley.anvil.modpacktools.util.config.MissingConfigValueException
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.io.File
@ -17,18 +18,18 @@ import java.util.concurrent.TimeUnit.MICROSECONDS
//Lazy initialization will prevent objects from being initilized if not needed
val CONFIG by lazy {Config("modpacktoolsconfig.toml")}
val LOADER by lazy {CommandLoader("ley.anvil.modpacktools.commands")}
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.getPath<String>("Locations/src")!!, "modpack.json"))}
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.pathOrException<String>("Locations/src"), "modpack.json"))}
val GSON by lazy {GsonBuilder().setPrettyPrinting().create()}
//for checking if the client has been initialized when closing it
private val httpClient0 = lazy {
val timeout = CONFIG.config.getPath<Long>("Downloads/httpTimeout")!!
val timeout = CONFIG.config.pathOrException<Long>("Downloads/httpTimeout")
OkHttpClient.Builder()
.callTimeout(timeout, MICROSECONDS)
.connectTimeout(timeout, MICROSECONDS)
.readTimeout(timeout, MICROSECONDS)
.writeTimeout(timeout, MICROSECONDS)
.dispatcher(Dispatcher(Executors.newFixedThreadPool(CONFIG.config.getPath<Long>("Downloads/maxThreads")!!.toInt())))
.dispatcher(Dispatcher(Executors.newFixedThreadPool(CONFIG.config.pathOrException<Long>("Downloads/maxThreads").toInt())))
.build()
}
val HTTP_CLIENT by httpClient0
@ -67,6 +68,11 @@ fun runCommand(args: Array<out String>) {
println("Config is needed for this command yet it is not present. Run 'init' to generate")
} catch(e: CommandLoader.ModpackJsonMissingException) {
println("Modpackjson is needed for this command yet it is not present.")
} catch(e: MissingConfigValueException) {
println("The Config value ${e.missingValue} was expected but was not found")
e.message?.let {println(it)}
println('\n')
e.printStackTrace()
}
}
}

View file

@ -23,8 +23,9 @@ import java.util.zip.ZipOutputStream
object BuildTwitch : ICommand {
override val name: String = "buildtwitch"
override val helpMessage: String = "builds a twitch export"
private val tmp: File by lazy {File(CONFIG.config.getPath<String>("Locations/tempDir"), "twitch")}
private val downloadDir by lazy {File(CONFIG.config.getPath<String>("Locations/tempDir"), "download")}
private val tempDir by lazy {File(CONFIG.config.pathOrException<String>("Locations/tempDir"))}
private val tmp: File by lazy {File(tempDir, "twitch")}
private val downloadDir by lazy {File(tempDir, "download")}
override fun execute(args: Array<out String>): CommandReturn {
val wr = MPJH.asWrapper!!
@ -32,7 +33,7 @@ object BuildTwitch : ICommand {
val archiveName = "${wr.json.id}-${wr.defaultVersion.versionName}-twitch"
val dir = File("./build")
val toDownload = mutableMapOf<URL, Pair<File, String>>()
val srcDir by lazy {File(CONFIG.config.getPath<String>("Locations/src")!!)}
val srcDir by lazy {File(CONFIG.config.pathOrException<String>("Locations/src"))}
dir.mkdirs()
tmp.mkdirs()
downloadDir.mkdirs()
@ -46,7 +47,7 @@ object BuildTwitch : ICommand {
//TODO download & install files
for(uf in ml.links) {
if(uf.key.isFile) {
if (!uf.key.isASDirSet)
if(!uf.key.isASDirSet)
uf.key.setASDir(srcDir)
val file = uf.key.getFile()
if(file.exists()) {

View file

@ -6,7 +6,6 @@ import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.command.LoadCommand
import ley.anvil.modpacktools.util.mergeTo
import java.io.File
import java.io.FileWriter
@ -20,24 +19,36 @@ object Init : ICommand {
override fun execute(args: Array<out String>): CommandReturn {
if(!CONFIG.exists)
CONFIG.copyConfig()
val srcDir = File(CONFIG.config.getPath<String>("Locations/src")!!)
val overrides = srcDir.mergeTo(File("overrides"))
val srcDir = File(CONFIG.config.pathOrException<String>("Locations/src"))
val overrides = File(srcDir, "overrides")
if(!overrides.exists())
overrides.mkdirs()
val asjson = srcDir.mergeTo(File("modpack.json"))
if (!asjson.exists()) {
val writer = FileWriter(asjson)
val asJson = File(srcDir, "modpack.json")
if (!asJson.exists()) {
//create new file
val writer = FileWriter(asJson)
val addsc = AddonscriptJSON.create()
//set type and add version
addsc.type = "modpack"
val ver = AddonscriptJSON.Version()
addsc.versions = mutableListOf(ver)
ver.versionid = -1
//create overrides
val file = AddonscriptJSON.File()
ver.files = mutableListOf(file)
file.id = "overrides"
file.link = "file://overrides"
file.installer = "internal.overrides"
file.options = mutableListOf("client", "server", "required", "included")
//write file
addsc.write(writer)
writer.close()
}

View file

@ -1,37 +0,0 @@
package ley.anvil.modpacktools.util
import com.moandjiezana.toml.Toml
class CustomToml : Toml() {
companion object {
@JvmStatic
fun Toml.get(key: String): Any? {
//Getting Around things being private for no reason 101 (dont look :P)
val getFunc by lazy {Toml::class.getFun("get")}
return getFunc?.call(this, key)
}
}
/**
* gets a path from a config.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config separated by /, . or \
*/
fun <T> getPath(path: String): T? = getPath(*path.split('/', '.', '\\').toTypedArray())
/**
* gets a path from a config.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config
*/
@Suppress("UNCHECKED_CAST")
fun <T> getPath(vararg path: String): T? {
var toml: Toml = this
path.slice(0..path.size - 2).forEach {toml = toml.getTable(it)}
return toml.get(path[path.size - 1]) as? T
}
}

View file

@ -1,4 +1,4 @@
package ley.anvil.modpacktools.util
package ley.anvil.modpacktools.util.config
import org.apache.commons.io.FileUtils
import java.io.File
@ -12,12 +12,12 @@ class Config(val configName: String) {
*
* @return the Toml object of the config file
*/
private fun readConfig(): CustomToml {
private fun readConfig(): ConfigToml {
return if(exists) {
//parse file to toml
CustomToml().read(configLocation) as CustomToml
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 CustomToml().read(ClassLoader.getSystemResourceAsStream(configName)) as CustomToml
} else ConfigToml().read(ClassLoader.getSystemResourceAsStream(configName)) as ConfigToml
}
/**

View file

@ -0,0 +1,58 @@
package ley.anvil.modpacktools.util.config
import com.moandjiezana.toml.Toml
import ley.anvil.modpacktools.util.getFun
class ConfigToml : Toml() {
companion object {
@JvmStatic
fun Toml.get(key: String): Any? {
//Getting Around things being private for no reason 101 (dont look :P)
val getFunc by lazy {Toml::class.getFun("get")}
return getFunc?.call(this, key)
}
}
/**
* gets a path from a config.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config separated by /, . or \
*/
fun <T> getPath(path: String): T? = getPath(*path.split('/', '.', '\\').toTypedArray())
/**
* gets a path from a config.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config
*/
@Suppress("UNCHECKED_CAST")
fun <T> getPath(vararg path: String): T? {
var toml: Toml = this
path.slice(0..path.size - 2).forEach {toml = toml.getTable(it)}
return toml.get(path[path.size - 1]) as? T
}
/**
* gets a path from a config and throws a MissingConfigValueException if not found.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config separated by /, . or \
* @param message an optional message to provide the exception
*/
fun <T> pathOrException(path: String, message: String? = null): T = getPath(path) ?: throw MissingConfigValueException(path, message)
/**
* gets a path from a config and throws a MissingConfigValueException if not found.
* when getting an Int do NOT supply int to T. instead supply Long and then convert to Int!
*
* @param T what to get from the config
* @param path the path to get from the config
* @param message an optional message to provide the exception
*/
fun <T> pathOrException(vararg path: String, message: String? = null): T = getPath(*path) ?: throw MissingConfigValueException(path.joinToString("/"), message)
}

View file

@ -0,0 +1,9 @@
package ley.anvil.modpacktools.util.config
/**
* this should be thrown if an expected config value is not found
*
* @param missingValue the config value that is missing
* @param message an optional message to be displayed
*/
data class MissingConfigValueException(val missingValue: String, override val message: String? = null) : IllegalStateException(message)