Skip to content

Commit

Permalink
read modid and version from fabric.mod.json
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolMineman committed Nov 3, 2021
1 parent e0bfc7e commit b307efa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>brachyura</artifactId>
<version>0.16</version>
<version>0.17</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,35 @@ public abstract class FabricProject extends BaseJavaProject {
public final Lazy<MappingTree> mappings = new Lazy<>(this::createMappings);
public abstract MappingTree createMappings();
public abstract FabricLoader getLoader();
public abstract String getModId();
public abstract String getVersion();

public final Lazy<List<ModDependency>> modDependencies = new Lazy<>(() -> {
ModDependencyCollector d = new ModDependencyCollector();
getModDependencies(d);
return d.dependencies;
});
public abstract void getModDependencies(ModDependencyCollector d);

public String getModId() {
return fmjParseThingy.get()[0];
}

public String getVersion() {
return fmjParseThingy.get()[1];
}

private Lazy<String[]> 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<ModDependency> dependencies = new ArrayList<>();

Expand Down Expand Up @@ -404,28 +424,6 @@ public void process(Collection<ProcessingEntry> inputs, ProcessingSink sink) thr
}
}

public class FmjMiscProcessor implements Processor {
@Override
public void process(Collection<ProcessingEntry> 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<Path> jij = new ArrayList<>();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions testmod/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -12,4 +12,4 @@
"mixins": [
"brachyuratestmod.mixins.json"
]
}
}

0 comments on commit b307efa

Please sign in to comment.