From b307efa357b38a1ead52306bd441cef095cc8d7f Mon Sep 17 00:00:00 2001 From: CoolMineman <62723322+CoolMineman@users.noreply.github.com> Date: Tue, 2 Nov 2021 21:17:15 -0500 Subject: [PATCH] read modid and version from fabric.mod.json --- brachyura/pom.xml | 2 +- .../brachyura/fabric/FabricProject.java | 48 +++++++++---------- .../brachyura/fabric/FabricProjectTest.java | 10 ---- testmod/src/main/resources/fabric.mod.json | 4 +- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/brachyura/pom.xml b/brachyura/pom.xml index 48b59b08..8b51534e 100644 --- a/brachyura/pom.xml +++ b/brachyura/pom.xml @@ -5,7 +5,7 @@ io.github.coolcrabs brachyura - 0.16 + 0.17 1.8 diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/FabricProject.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/FabricProject.java index cd8a3eb3..0b71d33b 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/FabricProject.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/FabricProject.java @@ -100,8 +100,7 @@ public abstract class FabricProject extends BaseJavaProject { public final Lazy mappings = new Lazy<>(this::createMappings); public abstract MappingTree createMappings(); public abstract FabricLoader getLoader(); - public abstract String getModId(); - public abstract String getVersion(); + public final Lazy> modDependencies = new Lazy<>(() -> { ModDependencyCollector d = new ModDependencyCollector(); getModDependencies(d); @@ -109,6 +108,27 @@ public abstract class FabricProject extends BaseJavaProject { }); public abstract void getModDependencies(ModDependencyCollector d); + public String getModId() { + return fmjParseThingy.get()[0]; + } + + public String getVersion() { + return fmjParseThingy.get()[1]; + } + + private Lazy fmjParseThingy = new Lazy<>(() -> { + try { + Gson gson = new GsonBuilder().setPrettyPrinting().setLenient().create(); + JsonObject fabricModJson; + try (BufferedReader reader = PathUtil.newBufferedReader(getResourcesDir().resolve("fabric.mod.json"))) { + fabricModJson = gson.fromJson(reader, JsonObject.class); + } + return new String[] {fabricModJson.get("id").getAsString(), fabricModJson.get("version").getAsString()}; + } catch (Exception e) { + throw Util.sneak(e); + } + }); + public static class ModDependencyCollector { public final List dependencies = new ArrayList<>(); @@ -404,28 +424,6 @@ public void process(Collection inputs, ProcessingSink sink) thr } } - public class FmjMiscProcessor implements Processor { - @Override - public void process(Collection inputs, ProcessingSink sink) throws IOException { - for (ProcessingEntry e : inputs) { - if ("fabric.mod.json".equals(e.id.path)) { - Gson gson = new GsonBuilder().setPrettyPrinting().setLenient().create(); - JsonObject fabricModJson; - try (BufferedReader reader = new BufferedReader(new InputStreamReader(e.in.get(), StandardCharsets.UTF_8))) { - fabricModJson = gson.fromJson(reader, JsonObject.class); - } - fabricModJson.addProperty("version", getVersion()); - if (!getModId().equals(fabricModJson.get("id").getAsString())) { - throw new IllegalArgumentException("Modid in fabric.mod.json not the same as buildscript"); - } - sink.sink(() -> GsonUtil.toIs(fabricModJson, gson), e.id); - } else { - sink.sink(e.in, e.id); - } - } - } - } - @Override public boolean processResources(Path source, Path target) throws IOException { List jij = new ArrayList<>(); @@ -434,7 +432,7 @@ public boolean processResources(Path source, Path target) throws IOException { jij.add(modDependency.jarDependency.jar); } } - ProcessorChain c = new ProcessorChain(new FmjMiscProcessor(), FMJRefmapApplier.INSTANCE, new FmjJijApplier(jij)); + ProcessorChain c = new ProcessorChain(FMJRefmapApplier.INSTANCE, new FmjJijApplier(jij)); c.apply(new DirectoryProcessingSink(target), new DirectoryProcessingSource(source)); return true; } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java index 14afbbf9..54d0cf7f 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java @@ -15,16 +15,6 @@ class FabricProjectTest { FabricProject fabricProject = new FabricProject() { - @Override - public String getModId() { - return "brachyuratestmod"; - } - - @Override - public String getVersion() { - return "1.0.0"; - } - @Override public String getMcVersion() { return "21w39a"; diff --git a/testmod/src/main/resources/fabric.mod.json b/testmod/src/main/resources/fabric.mod.json index 057d2f7b..7dcf853e 100644 --- a/testmod/src/main/resources/fabric.mod.json +++ b/testmod/src/main/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "brachyuratestmod", - "version": "${version}", + "version": "1337.0.0", "name": "Test Mod", "description": "Test mod for brachyura build tool.", "entrypoints": { @@ -12,4 +12,4 @@ "mixins": [ "brachyuratestmod.mixins.json" ] -} \ No newline at end of file +}