diff --git a/api/build.gradle b/api/build.gradle index 75e5e7063..8ebac8aa8 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -17,13 +17,9 @@ shadowJar { relocate 'org.tukaani', 'distanthorizons.libraries.tukaani' relocate 'org.apache.commons.compress', 'distanthorizons.libraries.apache.commons.compress' - // Toml & Json for config + // NightConfig (includes Toml & Json) relocate 'com.electronwill.nightconfig', 'distanthorizons.libraries.electronwill.nightconfig' - // FIXME: This is a massive library that is located in lots of different spots - relocate 'com.googlecode.json-simple', 'distanthorizons.libraries.googlecode.json-simple' - relocate 'org.json.simple', 'distanthorizons.libraries.json.simple' - // Theming relocate 'com.formdev.flatlaf', 'distanthorizons.libraries.formdev.flatlaf' diff --git a/core/src/main/java/com/seibel/lod/core/config/Config.java b/core/src/main/java/com/seibel/lod/core/config/Config.java index ecfdcf69e..7c38b4fc7 100644 --- a/core/src/main/java/com/seibel/lod/core/config/Config.java +++ b/core/src/main/java/com/seibel/lod/core/config/Config.java @@ -24,6 +24,9 @@ import com.seibel.lod.api.enums.config.*; import com.seibel.lod.api.enums.rendering.*; import com.seibel.lod.core.config.types.*; +import java.util.HashMap; +import java.util.Map; + /** * This handles any configuration the user has access to. @@ -70,6 +73,10 @@ public class Config .comment("Show the lod button in the options screen next to fov") .build(); +// public static ConfigEntry> testHashMap = new ConfigEntry.Builder>() +// .set(new HashMap()) +// .build(); + public static class Graphics { diff --git a/core/src/main/java/com/seibel/lod/core/config/ConfigBase.java b/core/src/main/java/com/seibel/lod/core/config/ConfigBase.java index e4664e676..fad61fb15 100644 --- a/core/src/main/java/com/seibel/lod/core/config/ConfigBase.java +++ b/core/src/main/java/com/seibel/lod/core/config/ConfigBase.java @@ -9,6 +9,7 @@ import org.apache.logging.log4j.Logger; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -53,6 +54,7 @@ public class ConfigBase // acceptableInputs.add(Float.class); acceptableInputs.add(String.class); acceptableInputs.add(Map.class); // TODO[CONFIG]: This is handled separately to check the first input is String and the second input is valid + acceptableInputs.add(HashMap.class); } /** Disables the minimum and maximum of a variable */ @@ -90,9 +92,9 @@ public class ConfigBase entry.configBase = this; if (ConfigEntry.class.isAssignableFrom(field.getType())) { // If item is type ConfigEntry - if (!isAcceptableType(((ConfigEntry) entry).get().getClass())) { + if (!isAcceptableType(((ConfigEntry) entry).getType())) { LOGGER.error("Invalid variable type at [" + (category.isEmpty() ? "" : category + ".") + field.getName() + "]."); - LOGGER.error("Type [" + ((ConfigEntry) entry).get().getClass() + "] is not one of these types [" + acceptableInputs.toString() + "]"); + LOGGER.error("Type [" + ((ConfigEntry) entry).getType() + "] is not one of these types [" + acceptableInputs.toString() + "]"); entries.remove(entries.size() -1); // Delete the entry if it is invalid so the game can still run } } diff --git a/core/src/main/java/com/seibel/lod/core/config/file/ConfigTypeConverters.java b/core/src/main/java/com/seibel/lod/core/config/file/ConfigTypeConverters.java index 4d2a4a1fe..60aa26d1a 100644 --- a/core/src/main/java/com/seibel/lod/core/config/file/ConfigTypeConverters.java +++ b/core/src/main/java/com/seibel/lod/core/config/file/ConfigTypeConverters.java @@ -1,8 +1,8 @@ package com.seibel.lod.core.config.file; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; +import com.electronwill.nightconfig.core.Config; +import com.electronwill.nightconfig.core.io.ParsingMode; +import com.electronwill.nightconfig.json.JsonFormat; import java.util.HashMap; import java.util.Map; @@ -17,6 +17,7 @@ public class ConfigTypeConverters { // Once you've made a converter add it to here where the first value is the type you want to convert and the 2nd value is the converter public static final Map convertObjects = new HashMap() {{ put(Map.class, new MapConverter()); + put(HashMap.class, new MapConverter()); }}; public static String convertToString(Class clazz, Object value) { @@ -46,35 +47,34 @@ public class ConfigTypeConverters { } + + + + @SuppressWarnings("unchecked") public static class MapConverter extends ConverterBase { @Override public String convertToString(Object item) { Map mapObject = (Map) item; - JSONObject jsonObject = new JSONObject(); + Config jsonObject = Config.inMemory(); for (int i = 0; i < mapObject.size(); i++) { - jsonObject.put(mapObject.keySet().toArray()[i], mapObject.get(mapObject.keySet().toArray()[i])); + jsonObject.add(mapObject.keySet().toArray()[i].toString(), mapObject.get(mapObject.keySet().toArray()[i])); } - return jsonObject.toJSONString(); + return JsonFormat.fancyInstance().createWriter().writeToString(jsonObject); } @Override public Map convertFromString(String s) { Map map = new HashMap<>(); - JSONObject jsonObject = null; + Config jsonObject = Config.inMemory(); try { - jsonObject = (JSONObject) new JSONParser().parse(s); - } catch (ParseException p) { - p.printStackTrace(); - } + JsonFormat.fancyInstance().createParser().parse(s, jsonObject, ParsingMode.REPLACE); + } catch (Exception e) { e.printStackTrace(); } - for (int i = 0; i < jsonObject.keySet().toArray().length; i++) { - map.put((String) jsonObject.keySet().toArray()[i], jsonObject.get(jsonObject.keySet().toArray()[i])); - } - return map; + return jsonObject.valueMap(); } } } diff --git a/core/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java b/core/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java index ca3d30552..d521df754 100644 --- a/core/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java +++ b/core/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java @@ -1,9 +1,5 @@ package com.seibel.lod.core.jar.installer; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - import java.net.URL; import java.util.ArrayList; import java.util.Collections; @@ -22,7 +18,7 @@ import java.util.stream.Collectors; public class GitlabGetter { public static final String GitLabApi = "https://gitlab.com/api/v4/projects/"; public static final String projectID = "18204078"; - public static JSONArray projectRelease = new JSONArray(); +// public static JSONArray projectRelease = new JSONArray(); public static List releaseNames = new ArrayList<>(); // This list contains the release ID's public static List readableReleaseNames = new ArrayList<>(); // This list contains the readable names of the ID's @@ -31,155 +27,155 @@ public class GitlabGetter { public static void init() { - try { - // TODO: Modify the projectRelease to fix 1.6.0a's versions rather than fixing it everytime we want to use projectReleases - projectRelease = (JSONArray) new JSONParser().parse(WebDownloader.downloadAsString(new URL(GitLabApi+projectID+"/releases"))); - - for (int i = 0; i < projectRelease.size(); i++) { - JSONObject currentRelease = (JSONObject) projectRelease.get(i); - if (!currentRelease.get("tag_name").toString().contains("-1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering - releaseNames.add(currentRelease.get("tag_name").toString()); - if (currentRelease.get("tag_name").toString().startsWith("1.16.4") || currentRelease.get("tag_name").toString().startsWith("1.16.5")) { - // We want to do this to remove the mc version from the start of the name in 1.5.4 and prior - readableReleaseNames.add(currentRelease.get("name").toString().replace("1.16.4 ","").replace("1.16.5 ","")); - } else { - readableReleaseNames.add(currentRelease.get("name").toString()); - } - } else if (!releaseNames.contains("1.6.0a")) { - releaseNames.add("1.6.0a"); - readableReleaseNames.add("Alpha 1.6.0"); - } - } - - // Some tests for getting the release versions -// System.out.println(getRelease("1.6.3a", "1.18.2")); -// System.out.println(getRelease("1.16.4-a1.2", null)); // The oldest downloadable version is 1.2 as versions before that didn't include downloads - - // Set the mcVersionReleases - JSONArray minecraftReleases = (JSONArray) ((JSONObject) new JSONParser().parse(WebDownloader.downloadAsString(new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json")))).get("versions"); - for (int i = 0; i < minecraftReleases.size(); i++) { - JSONObject jsonObject = (JSONObject) minecraftReleases.get(i); - if (jsonObject.get("type").toString().equals("release")) - mcVersionReleases.add(jsonObject.get("id").toString()); - } - - // Some tests to get minecraft versions available in that version of the mod -// System.out.println(getMcVersionsInRelease("1.6.5a")); -// System.out.println(getMcVersionsInRelease("1.16.4-a1.2")); - } catch (Exception e) { e.printStackTrace(); } +// try { +// // TODO: Modify the projectRelease to fix 1.6.0a's versions rather than fixing it everytime we want to use projectReleases +// projectRelease = (JSONArray) new JSONParser().parse(WebDownloader.downloadAsString(new URL(GitLabApi+projectID+"/releases"))); +// +// for (int i = 0; i < projectRelease.size(); i++) { +// JSONObject currentRelease = (JSONObject) projectRelease.get(i); +// if (!currentRelease.get("tag_name").toString().contains("-1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering +// releaseNames.add(currentRelease.get("tag_name").toString()); +// if (currentRelease.get("tag_name").toString().startsWith("1.16.4") || currentRelease.get("tag_name").toString().startsWith("1.16.5")) { +// // We want to do this to remove the mc version from the start of the name in 1.5.4 and prior +// readableReleaseNames.add(currentRelease.get("name").toString().replace("1.16.4 ","").replace("1.16.5 ","")); +// } else { +// readableReleaseNames.add(currentRelease.get("name").toString()); +// } +// } else if (!releaseNames.contains("1.6.0a")) { +// releaseNames.add("1.6.0a"); +// readableReleaseNames.add("Alpha 1.6.0"); +// } +// } +// +// // Some tests for getting the release versions +//// System.out.println(getRelease("1.6.3a", "1.18.2")); +//// System.out.println(getRelease("1.16.4-a1.2", null)); // The oldest downloadable version is 1.2 as versions before that didn't include downloads +// +// // Set the mcVersionReleases +// JSONArray minecraftReleases = (JSONArray) ((JSONObject) new JSONParser().parse(WebDownloader.downloadAsString(new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json")))).get("versions"); +// for (int i = 0; i < minecraftReleases.size(); i++) { +// JSONObject jsonObject = (JSONObject) minecraftReleases.get(i); +// if (jsonObject.get("type").toString().equals("release")) +// mcVersionReleases.add(jsonObject.get("id").toString()); +// } +// +// // Some tests to get minecraft versions available in that version of the mod +//// System.out.println(getMcVersionsInRelease("1.6.5a")); +//// System.out.println(getMcVersionsInRelease("1.16.4-a1.2")); +// } catch (Exception e) { e.printStackTrace(); } } /** Gets the compatible minecraft versions a release of the mod works with */ public static List getMcVersionsInRelease(String version) { List versions = new ArrayList<>(); - - JSONArray releaseArray = getScuffedReleaseArray(version); - - - for (int i = 0; i < releaseArray.size(); i++) { - String name = ((JSONObject) releaseArray.get(i)).get("name").toString(); - for (String mcVersion : mcVersionReleases) { - if (name.contains(mcVersion)) { - versions.add(mcVersion); - break; - } - } - } - - // Sort it so the newest versions of minecraft are at the top - Collections.sort(versions); - Collections.reverse(versions); +// +// JSONArray releaseArray = getScuffedReleaseArray(version); +// +// +// for (int i = 0; i < releaseArray.size(); i++) { +// String name = ((JSONObject) releaseArray.get(i)).get("name").toString(); +// for (String mcVersion : mcVersionReleases) { +// if (name.contains(mcVersion)) { +// versions.add(mcVersion); +// break; +// } +// } +// } +// +// // Sort it so the newest versions of minecraft are at the top +// Collections.sort(versions); +// Collections.reverse(versions); return versions; } /** Gets the url to the download of a release of the mod */ public static URL getRelease(String version, String mcVersion) { - JSONArray releaseArray = getScuffedReleaseArray(version); - - if (mcVersion != null) { - for (int i = 0; i < releaseArray.size(); i++) { - if (((JSONObject) releaseArray.get(i)).get("name").toString().contains(mcVersion)) { // With the way our GitLab releases is set up, the only way to check the mc version is to check if it is in the name - try { - return new URL(((JSONObject) releaseArray.get(i)).get("direct_asset_url").toString()); - } catch (Exception e) { e.printStackTrace(); } - } - } - } else { - // If version is null it gets the first version available - try { - return new URL(((JSONObject) releaseArray.get(0)).get("direct_asset_url").toString()); - } catch (Exception e) { e.printStackTrace(); } - } +// JSONArray releaseArray = getScuffedReleaseArray(version); +// +// if (mcVersion != null) { +// for (int i = 0; i < releaseArray.size(); i++) { +// if (((JSONObject) releaseArray.get(i)).get("name").toString().contains(mcVersion)) { // With the way our GitLab releases is set up, the only way to check the mc version is to check if it is in the name +// try { +// return new URL(((JSONObject) releaseArray.get(i)).get("direct_asset_url").toString()); +// } catch (Exception e) { e.printStackTrace(); } +// } +// } +// } else { +// // If version is null it gets the first version available +// try { +// return new URL(((JSONObject) releaseArray.get(0)).get("direct_asset_url").toString()); +// } catch (Exception e) { e.printStackTrace(); } +// } return null; } /** Gets the update log of a release */ public static String getVersionDescription(String version) { - try { - if (!version.equals("1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering - // Do this hack to remove all the mcVer-1.6.0a items from the releaseNames - int newVer = releaseNames.indexOf(version); - if (releaseNames.indexOf(version) > releaseNames.size()-14) - newVer += 2; - - return ((JSONObject) projectRelease.get(newVer)).get("description").toString(); - } else { - for (int i = 0; i < projectRelease.size(); i++) { - JSONObject currentRelease = ((JSONObject) new JSONParser().parse(projectRelease.get(i).toString())); - if (currentRelease.get("tag_name").toString().contains("-1.6.0a")) - return currentRelease.get("description").toString(); - } - } - } catch (Exception e) { - e.printStackTrace(); - } +// try { +// if (!version.equals("1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering +// // Do this hack to remove all the mcVer-1.6.0a items from the releaseNames +// int newVer = releaseNames.indexOf(version); +// if (releaseNames.indexOf(version) > releaseNames.size()-14) +// newVer += 2; +// +// return ((JSONObject) projectRelease.get(newVer)).get("description").toString(); +// } else { +// for (int i = 0; i < projectRelease.size(); i++) { +// JSONObject currentRelease = ((JSONObject) new JSONParser().parse(projectRelease.get(i).toString())); +// if (currentRelease.get("tag_name").toString().contains("-1.6.0a")) +// return currentRelease.get("description").toString(); +// } +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } return null; } - public static JSONArray getScuffedReleaseArray(String version) { - // Get the asset links of the releases - JSONArray releaseArray = new JSONArray(); - - if (!version.equals("1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering - try { - // Do this hack to remove all the mcVer-1.6.0a items from the releaseNames - int newVer = releaseNames.indexOf(version); - if (releaseNames.indexOf(version) > releaseNames.size()-14) - newVer += 2; - - releaseArray = ( - ((JSONArray) - ((JSONObject) - ((JSONObject) - projectRelease.get(newVer) - ).get("assets") - ).get("links"))); - } catch (Exception e) { - System.out.println("ERROR: Release [" + version + "] is not a valid release. Printing stacktrace..."); - e.printStackTrace(); - return null; - } - } else { - try { - for (int i = 0; i < projectRelease.size(); i++) { - JSONObject currentRelease = ((JSONObject) new JSONParser().parse(projectRelease.get(i).toString())); - if (currentRelease.get("tag_name").toString().contains("-1.6.0a")) { - releaseArray.add( - ((JSONArray) - ((JSONObject) - currentRelease.get("assets") - ).get("links") - ).get(0) - ); - } - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - return releaseArray; - } +// public static JSONArray getScuffedReleaseArray(String version) { +// // Get the asset links of the releases +// JSONArray releaseArray = new JSONArray(); +// +// if (!version.equals("1.6.0a")) { // We have to do this cus 1.6.0a stuffed up some ordering +// try { +// // Do this hack to remove all the mcVer-1.6.0a items from the releaseNames +// int newVer = releaseNames.indexOf(version); +// if (releaseNames.indexOf(version) > releaseNames.size()-14) +// newVer += 2; +// +// releaseArray = ( +// ((JSONArray) +// ((JSONObject) +// ((JSONObject) +// projectRelease.get(newVer) +// ).get("assets") +// ).get("links"))); +// } catch (Exception e) { +// System.out.println("ERROR: Release [" + version + "] is not a valid release. Printing stacktrace..."); +// e.printStackTrace(); +// return null; +// } +// } else { +// try { +// for (int i = 0; i < projectRelease.size(); i++) { +// JSONObject currentRelease = ((JSONObject) new JSONParser().parse(projectRelease.get(i).toString())); +// if (currentRelease.get("tag_name").toString().contains("-1.6.0a")) { +// releaseArray.add( +// ((JSONArray) +// ((JSONObject) +// currentRelease.get("assets") +// ).get("links") +// ).get(0) +// ); +// } +// } +// } catch (Exception e) { +// e.printStackTrace(); +// return null; +// } +// } +// +// return releaseArray; +// } } diff --git a/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java b/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java index 575c25b3f..1d0c4f91c 100644 --- a/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java +++ b/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java @@ -1,8 +1,8 @@ package com.seibel.lod.core.jar.installer; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; +import com.electronwill.nightconfig.core.Config; +import com.electronwill.nightconfig.core.io.ParsingMode; +import com.electronwill.nightconfig.json.JsonFormat; import java.net.URL; import java.util.*; @@ -12,10 +12,11 @@ import java.util.*; * * @author coolGi */ +// TODO: Fix stuff in here (check how to do stuff with `[{jsonStuff},{jsonStuff}]` which needs to be remade with nightconfig's json public class ModrinthGetter { public static final String ModrinthAPI = "https://api.modrinth.com/v2/project/"; public static final String projectID = "distanthorizons"; - public static JSONArray projectRelease = new JSONArray(); + public static Config projectRelease = Config.inMemory(); public static List releaseID = new ArrayList<>(); // This list contains the release ID's public static List mcVersions = new ArrayList<>(); // List of available Minecraft versions in the mod @@ -35,18 +36,18 @@ public class ModrinthGetter { public static boolean init() { try { - projectRelease = (JSONArray) new JSONParser().parse(WebDownloader.downloadAsString(new URL(ModrinthAPI+projectID+"/version"))); + JsonFormat.fancyInstance().createParser().parse(WebDownloader.downloadAsString(new URL(ModrinthAPI+projectID+"/version")), projectRelease, ParsingMode.REPLACE); for (int i = 0; i < projectRelease.size(); i++) { - JSONObject currentRelease = (JSONObject) projectRelease.get(i); + Config currentRelease = (Config) projectRelease.get(String.valueOf(i)); String workingID = currentRelease.get("id").toString(); releaseID.add(workingID); releaseNames.put(workingID, currentRelease.get("name").toString().replaceAll(" - 1\\..*", "")); changeLogs.put(workingID, currentRelease.get("changelog").toString()); try { - downloadUrl.put(workingID, new URL(((JSONObject) ((JSONArray) currentRelease.get("files")).get(0)).get("url").toString())); + downloadUrl.put(workingID, new URL(currentRelease.get("files.0.url").toString())); } catch (Exception e) { e.printStackTrace(); } // Get all the mc versions this mod is available for @@ -76,14 +77,15 @@ public class ModrinthGetter { return downloadUrl.get(((List) mcVerToReleaseID.get(mcVer)).get(0)); } public static String getLatestShaForVersion(String mcVer) { - return ((JSONObject) - ((JSONObject) - ((JSONArray) - ((JSONObject) projectRelease.get(mcVersions.indexOf(mcVer))) - .get("files")) - .get(0)) - .get("hashes")) - .get("sha1") - .toString(); + return projectRelease.get(mcVersions.indexOf(mcVer) + ".files.0.hashes.sha1").toString(); +// return ((JSONObject) +// ((JSONObject) +// ((JSONArray) +// ((JSONObject) projectRelease.get(mcVersions.indexOf(mcVer))) +// .get("files")) +// .get(0)) +// .get("hashes")) +// .get("sha1") +// .toString(); } } diff --git a/core/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java b/core/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java index 753ec3f21..e71b939aa 100644 --- a/core/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java +++ b/core/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java @@ -1,21 +1,26 @@ package com.seibel.lod.core.jar.wrapperInterfaces.config; +import com.electronwill.nightconfig.core.Config; +import com.electronwill.nightconfig.core.io.ParsingMode; +import com.electronwill.nightconfig.json.JsonFormat; import com.seibel.lod.core.jar.JarUtils; import com.seibel.lod.core.wrapperInterfaces.config.IConfigWrapper; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; import java.util.Locale; public class ConfigWrapper implements IConfigWrapper { public static final ConfigWrapper INSTANCE = new ConfigWrapper(); - private static JSONObject jsonObject = new JSONObject(); + private static Config jsonObject = Config.inMemory(); public static void init() { try { - jsonObject = (JSONObject) new JSONParser().parse(JarUtils.convertInputStreamToString(JarUtils.accessFile("assets/lod/lang/"+ Locale.getDefault().toString().toLowerCase()+".json"))); - } catch (ParseException e) { e.printStackTrace(); } +// System.out.println(JarUtils.convertInputStreamToString(JarUtils.accessFile("assets/lod/lang/"+ Locale.getDefault().toString().toLowerCase()+".json")).replaceAll(":\\n.+?(?=\")",":")); + // FIXME: Is there something in the config that the parser cant read? + JsonFormat.fancyInstance().createParser().parse( + JarUtils.convertInputStreamToString(JarUtils.accessFile("assets/lod/lang/"+ Locale.getDefault().toString().toLowerCase()+".json")), + jsonObject, ParsingMode.REPLACE + ); + } catch (Exception e) { e.printStackTrace(); } } @Override