4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-06-10 22:49:26 +02:00

port CustomToml to kt

This commit is contained in:
LordMZTE 2020-07-26 22:39:53 +02:00
parent b427ff532b
commit b2d8a7489a
4 changed files with 30 additions and 41 deletions

View file

@ -21,18 +21,18 @@ public class Main {
public static ModpackJsonHandler MPJH;
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final long httpTimeout = CONFIG.getConfig().getPath(Long.class, "Downloads/httpTimeout");
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(CONFIG.getConfig().getPath(Long.class, "Downloads/maxThreads").intValue())))
.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((CONFIG.getConfig().getPath(String.class, "Locations", "modpackjsonFile"))));
MPJH = new ModpackJsonHandler(new File((String)CONFIG.getConfig().getPath("Locations", "modpackjsonFile")));
if(args.length <= 0) {
printHelp();

View file

@ -38,7 +38,7 @@ public class ModInfo {
try {
System.out.println("Getting Info From Curse API");
File manifestFile = new File(Main.CONFIG.getJarLocation(), Main.CONFIG.getConfig()
.getPath(String.class,
.getPath(
"Locations",
"manifestFile"
));

View file

@ -1,37 +0,0 @@
package ley.anvil.modpacktools.util;
import com.moandjiezana.toml.Toml;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
public class CustomToml extends Toml {
public <T> T getPath(Class<T> type, String path) {
return getPath(type, path.split("/"));
}
@SuppressWarnings("unchecked")
public <T> T getPath(Class<T> type, String... path) {
LinkedList<String> paths = new LinkedList<>(Arrays.asList(path));
String key = paths.get(paths.size() - 1);
paths.remove(paths.size() - 1);
Toml toml = this;
for(String str : paths)
toml = toml.getTable(str);
return (T)get(toml, key);
}
public Object get(Toml clazz, String key) {
try {
//Getting Around things being private for no reason 101
Method method = Toml.class.getDeclaredMethod("get", String.class);
method.setAccessible(true);
return method.invoke(clazz, key);
}catch(IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}

View file

@ -0,0 +1,26 @@
package ley.anvil.modpacktools.util
import com.moandjiezana.toml.Toml
import kotlin.reflect.full.functions
import kotlin.reflect.jvm.isAccessible
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 = this::class.functions.reduce {a, b -> if(b.name == "get") b else a}
getFunc.isAccessible = true
return getFunc.call(this, key)
}
}
fun <T> getPath(path: String): T = getPath(*path.split('/', '.', '\\').toTypedArray())
@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
}
}