Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
James Seibel
2022-10-05 22:39:11 -05:00
7 changed files with 189 additions and 181 deletions
+1 -5
View File
@@ -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'
@@ -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<HashMap<String, Boolean>> testHashMap = new ConfigEntry.Builder<HashMap<String, Boolean>>()
// .set(new HashMap<String, Boolean>())
// .build();
public static class Graphics
{
@@ -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
}
}
@@ -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<Class, ConverterBase> convertObjects = new HashMap<Class, ConverterBase>() {{
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<String, Object> mapObject = (Map<String, Object>) 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<String, Object> convertFromString(String s) {
Map<String, Object> 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();
}
}
}
@@ -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<String> releaseNames = new ArrayList<>(); // This list contains the release ID's
public static List<String> 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<String> getMcVersionsInRelease(String version) {
List<String> 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;
// }
}
@@ -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<String> releaseID = new ArrayList<>(); // This list contains the release ID's
public static List<String> 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<String>) 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();
}
}
@@ -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