mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-06-02 18:49:31 +02:00
port main to KT
added lazy initialization to objects in main (should cause significant performance improvement)
This commit is contained in:
parent
b2d8a7489a
commit
4f1a187c7d
|
@ -1,63 +0,0 @@
|
|||
package ley.anvil.modpacktools;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import ley.anvil.modpacktools.command.CommandLoader;
|
||||
import ley.anvil.modpacktools.command.CommandReturn;
|
||||
import ley.anvil.modpacktools.util.Config;
|
||||
import ley.anvil.modpacktools.util.ModpackJsonHandler;
|
||||
import okhttp3.Dispatcher;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
public static final Config CONFIG = new Config("modpacktoolsconfig.toml");
|
||||
public static final CommandLoader LOADER = new CommandLoader("ley.anvil.modpacktools.commands");
|
||||
public static ModpackJsonHandler MPJH;
|
||||
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private static final long httpTimeout = ((Long)CONFIG.getConfig().getPath("Downloads/httpTimeout")).intValue();
|
||||
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
|
||||
.callTimeout(httpTimeout, TimeUnit.MICROSECONDS)
|
||||
.connectTimeout(httpTimeout, TimeUnit.MICROSECONDS)
|
||||
.readTimeout(httpTimeout, TimeUnit.MICROSECONDS)
|
||||
.writeTimeout(httpTimeout, TimeUnit.MICROSECONDS)
|
||||
.dispatcher(new Dispatcher(Executors.newFixedThreadPool(((Long)CONFIG.getConfig().getPath("Downloads/maxThreads")).intValue())))
|
||||
.build();
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(CONFIG.configExists())
|
||||
MPJH = new ModpackJsonHandler(new File((String)CONFIG.getConfig().getPath("Locations", "modpackjsonFile")));
|
||||
|
||||
if(args.length <= 0) {
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
CommandReturn ret = LOADER.runCommand(args[0], args);
|
||||
if(ret.hasRet())
|
||||
System.out.println(ret.getRet());
|
||||
}catch(NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
printHelp();
|
||||
}
|
||||
|
||||
HTTP_CLIENT.dispatcher().executorService().shutdown();
|
||||
HTTP_CLIENT.connectionPool().evictAll();
|
||||
}
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.println("Commands:");
|
||||
LOADER.getCommands()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.sorted(Map.Entry.comparingByKey())
|
||||
.forEach(e -> System.out.println(e.getKey() + ": " + e.getValue().getHelpMessage()));
|
||||
}
|
||||
}
|
|
@ -37,7 +37,7 @@ public class ModInfo {
|
|||
public static ArrayList<ModInfo> getModInfo() {
|
||||
try {
|
||||
System.out.println("Getting Info From Curse API");
|
||||
File manifestFile = new File(Main.CONFIG.getJarLocation(), Main.CONFIG.getConfig()
|
||||
File manifestFile = new File(Main.getCONFIG().getJarLocation(), Main.getCONFIG().getConfig()
|
||||
.getPath(
|
||||
"Locations",
|
||||
"manifestFile"
|
||||
|
|
|
@ -24,7 +24,7 @@ public class AddMod implements ICommand {
|
|||
public CommandReturn execute(String[] args) {
|
||||
//Check if the command has the correct number of args
|
||||
if(args.length >= 2) {
|
||||
AddonscriptJSON json = Main.MPJH.getJson();
|
||||
AddonscriptJSON json = Main.getMPJH().getJson();
|
||||
AddonscriptJSON.Version version = null;
|
||||
if(json != null && json.versions != null) {
|
||||
if(json.versions.size() == 1) {
|
||||
|
@ -100,7 +100,7 @@ public class AddMod implements ICommand {
|
|||
//Overwrite Old Manifest File
|
||||
FileWriter manifestWriter = null;
|
||||
try {
|
||||
manifestWriter = new FileWriter(Main.MPJH.getModpackJsonFile(), false);
|
||||
manifestWriter = new FileWriter(Main.getMPJH().getModpackJsonFile(), false);
|
||||
System.out.println("Printing Manifest");
|
||||
json.write(manifestWriter);
|
||||
manifestWriter.close();
|
||||
|
|
58
src/main/kotlin/ley/anvil/modpacktools/Main.kt
Normal file
58
src/main/kotlin/ley/anvil/modpacktools/Main.kt
Normal file
|
@ -0,0 +1,58 @@
|
|||
@file:JvmName("Main")
|
||||
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 okhttp3.Dispatcher
|
||||
import okhttp3.OkHttpClient
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit.MICROSECONDS
|
||||
|
||||
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/modpackjsonFile")!!))}
|
||||
val GSON by lazy {GsonBuilder().setPrettyPrinting().create()}
|
||||
private val httpClient0 = lazy {
|
||||
val timeout = CONFIG.config.getPath<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())))
|
||||
.build()
|
||||
}
|
||||
val HTTP_CLIENT by httpClient0
|
||||
|
||||
|
||||
fun main(args: Array<out String>) {
|
||||
if(args.isEmpty()) {
|
||||
printHelp()
|
||||
} else {
|
||||
|
||||
try {
|
||||
val ret = LOADER.runCommand(args[0], args)
|
||||
if(ret.hasRet())
|
||||
println(ret.ret)
|
||||
} catch(e: NoSuchElementException) {
|
||||
println(e.message)
|
||||
printHelp()
|
||||
}
|
||||
}
|
||||
if(httpClient0.isInitialized()) {
|
||||
HTTP_CLIENT.dispatcher.executorService.shutdown()
|
||||
HTTP_CLIENT.connectionPool.evictAll()
|
||||
}
|
||||
}
|
||||
|
||||
fun printHelp() {
|
||||
println("Commands:")
|
||||
LOADER.commands.entries.stream()
|
||||
.sorted(Comparator.comparing {e: MutableMap.MutableEntry<String, ICommand> -> e.key})
|
||||
.forEach {println("${it.key}: ${it.value.helpMessage}")}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package ley.anvil.modpacktools.command
|
||||
|
||||
import ley.anvil.modpacktools.Main.CONFIG
|
||||
import ley.anvil.modpacktools.Main.MPJH
|
||||
import ley.anvil.modpacktools.CONFIG
|
||||
import ley.anvil.modpacktools.MPJH
|
||||
import ley.anvil.modpacktools.command.CommandReturn.Companion.fail
|
||||
import org.reflections.Reflections
|
||||
import org.reflections.scanners.SubTypesScanner
|
||||
|
@ -38,7 +38,7 @@ class CommandLoader(private val pkg: String) {
|
|||
//Only annotated classes
|
||||
.filter {it.isAnnotationPresent(LoadCommand::class.java)}
|
||||
//can be object
|
||||
.map {it.kotlin.objectInstance?: it}
|
||||
.map {it.kotlin.objectInstance ?: it}
|
||||
.forEach {if(it is ICommand) addCommand(it) else addClass(it as Class<out ICommand>)}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package ley.anvil.modpacktools.commands
|
|||
|
||||
import j2html.TagCreator.*
|
||||
import ley.anvil.addonscript.wrapper.MetaData
|
||||
import ley.anvil.modpacktools.Main
|
||||
import ley.anvil.modpacktools.MPJH
|
||||
import ley.anvil.modpacktools.command.CommandReturn
|
||||
import ley.anvil.modpacktools.command.CommandReturn.Companion.fail
|
||||
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
|
||||
|
@ -122,7 +122,7 @@ object CreateModlist : ICommand {
|
|||
|
||||
private fun getMods(): List<MetaData> {
|
||||
println("Getting mods... this may take a while (TODO)")
|
||||
val asJson = Main.MPJH.asWrapper
|
||||
val asJson = MPJH.asWrapper
|
||||
val mods = mutableListOf<MetaData>()
|
||||
val toGet = mutableListOf<String>()
|
||||
|
||||
|
|
|
@ -4,16 +4,9 @@ import org.apache.commons.io.FileUtils
|
|||
import java.io.File
|
||||
|
||||
class Config(val configName: String) {
|
||||
var jarLocation: File? = null
|
||||
private set
|
||||
var configLocation: File? = null
|
||||
private set
|
||||
var config: CustomToml? = null
|
||||
private set
|
||||
|
||||
init {
|
||||
val jarLocation by lazy {
|
||||
//Get the Location of the jarfile
|
||||
jarLocation = File(
|
||||
var file = File(
|
||||
this::class.java
|
||||
.protectionDomain
|
||||
.codeSource
|
||||
|
@ -21,22 +14,22 @@ class Config(val configName: String) {
|
|||
.toURI()
|
||||
)
|
||||
//Ensure That JAR_LOCATION is the jarfiles directory and not the file itself
|
||||
if(jarLocation!!.isFile)
|
||||
jarLocation = jarLocation!!.parentFile
|
||||
|
||||
configLocation = File(jarLocation, configName)
|
||||
config = readConfig()
|
||||
if(file.isFile)
|
||||
file = file.parentFile
|
||||
file
|
||||
}
|
||||
val configLocation by lazy {File(jarLocation, configName)}
|
||||
val config by lazy {readConfig()}
|
||||
|
||||
/**
|
||||
* reads the config it it exists and the default config otherwise
|
||||
*
|
||||
* @return the Toml object of the config file
|
||||
*/
|
||||
private fun readConfig(): CustomToml? {
|
||||
private fun readConfig(): CustomToml {
|
||||
return if(configExists()) {
|
||||
//parse file to toml
|
||||
CustomToml().read(configLocation) as CustomToml?
|
||||
CustomToml().read(configLocation) as CustomToml
|
||||
//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
|
||||
}
|
||||
|
@ -46,7 +39,7 @@ class Config(val configName: String) {
|
|||
*
|
||||
* @return true if the config file exists
|
||||
*/
|
||||
fun configExists(): Boolean = configLocation?.exists() ?: false
|
||||
fun configExists(): Boolean = configLocation.exists()
|
||||
|
||||
/**
|
||||
* Copies the Config file from the resources into the tool's folder
|
||||
|
@ -56,6 +49,5 @@ class Config(val configName: String) {
|
|||
val conf = ClassLoader.getSystemResourceAsStream(configName)
|
||||
FileUtils.copyInputStreamToFile(conf, configLocation)
|
||||
conf!!.close()
|
||||
config = readConfig()
|
||||
}
|
||||
}
|
|
@ -15,12 +15,12 @@ class CustomToml : Toml() {
|
|||
}
|
||||
}
|
||||
|
||||
fun <T> getPath(path: String): T = getPath(*path.split('/', '.', '\\').toTypedArray())
|
||||
fun <T> getPath(path: String): T? = getPath(*path.split('/', '.', '\\').toTypedArray())
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> getPath(vararg path: String): T {
|
||||
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
|
||||
return toml.get(path[path.size - 1]) as? T
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue