4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-11-17 23:41:55 +01:00

added ModpackJsonHandler

added getPath method to config
This commit is contained in:
LordMZTE 2020-06-18 22:18:59 +02:00
parent e3e5d8a7c3
commit 5179c3faee
7 changed files with 75 additions and 13 deletions

View file

@ -3,15 +3,21 @@ package ley.anvil.modpacktools;
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 java.io.File;
import java.util.Map;
import java.util.NoSuchElementException;
public class Main {
public static final Config CONFIG = new Config();
public static final CommandLoader LOADER = new CommandLoader("ley.anvil.modpacktools.commands");
public static ModpackJsonHandler MPJH;
public static void main(String[] args) {
if(CONFIG.configExists())
MPJH = new ModpackJsonHandler(new File((CONFIG.CONFIG.getPath(String.class, "Locations", "modpackjsonFile"))));
if(args.length <= 0) {
printHelp();
return;

View file

@ -35,8 +35,10 @@ public class ModInfo {
try {
System.out.println("Getting Info From Curse API");
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG
.getTable("Locations")
.getString("manifestFile"));
.getPath(String.class,
"Locations",
"manifestFile"
));
//Read manifest
JsonObject manifest = Util.readJsonFile(manifestFile);
JsonArray files = manifest.getAsJsonArray("files");

View file

@ -26,8 +26,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) {
//TODO Theres no way this command will work in this state of course
AddonscriptJSON json = null;
AddonscriptJSON json = Main.MPJH.getJson();
AddonscriptJSON.Version version = null;
if(json != null && json.versions != null) {
if(json.versions.size() == 1) {
@ -61,9 +60,7 @@ public class AddMod implements ICommand {
if(matcher.find()) {
fileID = Integer.parseInt(matcher.group(0));
}
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG
.getTable("Locations")
.getString("manifestFile"));
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.getPath(String.class, "Locations", "manifestFile"));
System.out.println("Reading Addonscript");
//Get Mods in manifest file
//Check if Mod already exsits

View file

@ -1,7 +1,5 @@
package ley.anvil.modpacktools.util;
import com.moandjiezana.toml.Toml;
import java.io.*;
import java.net.URISyntaxException;
@ -9,7 +7,7 @@ public class Config {
public final File JAR_LOCATION;
public final File CONFIG_LOCATION;
private final String CONFIG_NAME = "modpacktoolsconfig.toml";
public Toml CONFIG;
public CustomToml CONFIG;
public Config() {
//Get the Location of the jarfile
@ -39,10 +37,10 @@ public class Config {
*
* @return the Toml object of the config file
*/
private Toml readConfig() {
private CustomToml readConfig() {
if(configExists()) {
//parse file to toml
return new Toml().read(CONFIG_LOCATION);
return (CustomToml)new CustomToml().read(CONFIG_LOCATION);
}
return null;
}

View file

@ -0,0 +1,34 @@
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 {
@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) {
System.out.println("get fail");
e.printStackTrace();
}
return null;
}
}

View file

@ -0,0 +1,24 @@
package ley.anvil.modpacktools.util;
import ley.anvil.addonscript.v1.AddonscriptJSON;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ModpackJsonHandler {
File modpackJsonFile;
public ModpackJsonHandler(File modpackJsonFile) {
this.modpackJsonFile = modpackJsonFile;
}
public AddonscriptJSON getJson() {
try(FileReader reader = new FileReader(modpackJsonFile)) {
return AddonscriptJSON.read(reader, AddonscriptJSON.class);
}catch(IOException e) {
e.printStackTrace();
}
return null;
}
}

View file

@ -1,6 +1,7 @@
[Locations]
#The location of the twitch manifest file (will be replaced with addonscript in later versions)
manifestFile = "src/twitch/manifest.json"
addonscript = "src/modpack.json"
#The location of the modpackjson file
modpackjsonFile = "src/modpack.json"
[Local]
selectedVersion = -1