diff --git a/src/main/java/cam72cam/universalmodcore/Config.java b/src/main/java/cam72cam/universalmodcore/Config.java index 98d6fbe..6aaec53 100644 --- a/src/main/java/cam72cam/universalmodcore/Config.java +++ b/src/main/java/cam72cam/universalmodcore/Config.java @@ -26,6 +26,7 @@ public static class Mod { public final String id; public final String version; public final List dependencies; + public final List libraries = new ArrayList<>(); public static class Dependency { public final String id; @@ -49,6 +50,9 @@ public Mod(JsonObject data) { this.dependencies = data.get("dependencies").getAsJsonObject().entrySet().stream() .map((e) -> new Dependency(e.getKey(), e.getValue().getAsJsonObject())) .collect(Collectors.toList()); + if (data.has("libraries")) { + data.get("libraries").getAsJsonArray().forEach(e -> libraries.add(new Library(e.getAsJsonObject()))); + } } } @@ -74,6 +78,49 @@ public UMC(JsonObject data) { } } + public static class Library { + public final boolean isDir; + public final String id; + public final String repo; + public final boolean hasRelocation; + public String relocateFrom; + public String relocateTo; + public final String type; + public final List onlyIn = new ArrayList<>(); + + public Library(JsonObject data) { + this.id = data.get("artifact").getAsString(); + + String repoType = data.get("repositoryType").getAsString(); + + switch (repoType) { + case "URL": this.isDir = false; break; + case "Dir": this.isDir = true; break; + default: throw new IllegalArgumentException("The repository Type needs to be either \"URL\" or \"Dir\""); + } + + + this.hasRelocation = data.has("relocate"); + + this.type = data.get("type").getAsString(); + + if (data.has("onlyIn")) { + data.get("onlyIn").getAsJsonArray().forEach(e -> onlyIn.add(e.getAsString())); + } + + if (hasRelocation) { + String[] path = data.get("relocate").getAsString().replaceAll(" ", "").split("\\|"); + if (path.length != 2) { + throw new IllegalArgumentException("Relocate needs needs two paths separated by a '|'"); + } + relocateFrom = path[0]; + relocateTo = path[1]; + } + + this.repo = data.get("repository").getAsString(); + } + } + public Config(JsonObject data, String mcVersion, Loader brand) throws GitAPIException, IOException { mod = new Mod(data.get("mod").getAsJsonObject()); integration = data.has("integration") ? new Integration(data.get("integration").getAsJsonObject()) : null; @@ -91,6 +138,42 @@ public Config(JsonObject data, String mcVersion, Loader brand) throws GitAPIExce vars.put("MINECRAFT", mcVersion); vars.put("LOADER", brand.toString()); + List repoLines = new ArrayList<>(); + List depLines = new ArrayList<>(); + List relLines = new ArrayList<>(); + List flatDirs = new ArrayList<>(); + + for (Library library : mod.libraries) { + if (!library.onlyIn.isEmpty() && !library.onlyIn.contains(minecraftLoader)) { + continue; + } + + if (library.isDir) { + flatDirs.add("'" + library.repo + "'"); + } else { + repoLines.add("\tmaven { url = \"" + library.repo + "\""); + } + + if (library.hasRelocation) { + relLines.add("\trelocate'" + library.relocateFrom + "', \"" + library.relocateTo + "\""); + } + + depLines.add("\t" + library.type + " '" + library.id + "'"); + } + + if (!flatDirs.isEmpty()) { + String flatDirBlock = + "\tflatDir {\n" + + "\t\tdirs " + String.join(", ", flatDirs) + "\n" + + "\t}"; + + repoLines.add(0, flatDirBlock); + } + + vars.put("LIB_REPOS", String.join("\n", repoLines)); + vars.put("MOD_DEPENDENCIES", String.join("\n", depLines)); + vars.put("RELOCATION", String.join("\n", relLines)); + String version = require("umc.version", umc.version); if (version.equals("latest")) { @@ -131,11 +214,11 @@ public Config(JsonObject data, String mcVersion, Loader brand) throws GitAPIExce if (!jar.exists()) { throw new RuntimeException(String.format("Unable to find UMC jar: %s", jar)); } - vars.put("UMC_REPO", String.format("repositories { flatDir { dirs '%s' } }", jar.getParent())); + vars.put("UMC_REPO", String.format("flatDir { dirs '%s' }", jar.getParent())); vars.put("UMC_DEPENDENCY", String.format("name: '%s'", jar.getName().replace(".jar", ""))); vars.put("UMC_FILE", jar.getPath()); } else { - vars.put("UMC_REPO", "repositories { maven { url = \"https://teamopenindustry.cc/maven\" }}"); + vars.put("UMC_REPO", "maven { url = \"https://teamopenindustry.cc/maven\" }"); vars.put("UMC_DEPENDENCY", String.format("'cam72cam.universalmodcore:UniversalModCore:%s-%s'", minecraftLoader, version)); vars.put("UMC_DOWNLOAD", String.format("https://teamopenindustry.cc/maven/cam72cam/universalmodcore/UniversalModCore/%s-%s/UniversalModCore-%s-%s.jar", minecraftLoader, version, minecraftLoader, version)); }