Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions src/main/java/cam72cam/universalmodcore/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class Mod {
public final String id;
public final String version;
public final List<Dependency> dependencies;
public final List<Library> libraries = new ArrayList<>();

public static class Dependency {
public final String id;
Expand All @@ -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())));
}
}
}

Expand All @@ -74,6 +78,41 @@ public UMC(JsonObject data) {
}
}

public static class Library {
public final boolean isPath;
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<String> onlyIn = new ArrayList<>();

public Library(JsonObject data) {
this.isPath = data.has("path");
this.id = isPath ? data.get("path").getAsString() : data.get("artifact").getAsString();

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 = isPath ? "" : 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;
Expand All @@ -91,6 +130,36 @@ public Config(JsonObject data, String mcVersion, Loader brand) throws GitAPIExce
vars.put("MINECRAFT", mcVersion);
vars.put("LOADER", brand.toString());

StringBuilder libRepos = new StringBuilder();
StringBuilder depends = new StringBuilder();
StringBuilder relocate = new StringBuilder();

if (!mod.libraries.isEmpty()) {
for (Library library : mod.libraries) {
if (!library.onlyIn.isEmpty() && !library.onlyIn.contains(minecraftLoader)) {
continue;
}

if (!library.repo.isEmpty()) {
libRepos.append(String.format("\tmaven { url = \"%s\" }", library.repo)).append("\n");
}

if (library.hasRelocation) {
relocate.append(String.format("\trelocate '%s', \"%s\"", library.relocateFrom, library.relocateTo)).append("\n");
}

if (library.isPath) {
depends.append(String.format("\t%s files('%s')", library.type, library.id)).append("\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might have to deal with flatDir above 1.17 as files won't work on there anymore

} else {
depends.append(String.format("\t%s '%s'", library.type, library.id)).append("\n");
}
}
}

vars.put("LIB_REPOS", stripNewline(libRepos));
vars.put("SHADOW", stripNewline(depends));
vars.put("RELOCATE", stripNewline(relocate));

String version = require("umc.version", umc.version);

if (version.equals("latest")) {
Expand Down Expand Up @@ -131,11 +200,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));
}
Expand Down Expand Up @@ -182,6 +251,14 @@ public Config(JsonObject data, String mcVersion, Loader brand) throws GitAPIExce
vars.put("FABRIC_SOMETHING_DEPENDENCIES", "TODO");
}

private String stripNewline(StringBuilder sb) {
int len = sb.length();
if (len > 0 && sb.charAt(len - 1) == '\n') {
sb.deleteCharAt(len - 1);
}
return sb.toString();
}


private String require(String name, String s) {
if (s == null || s.trim().isEmpty()) {
Expand Down