Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for quilt.mod.json5 #150

Draft
wants to merge 1 commit into
base: dev/1.3
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ dependencies {
// Forge mods.toml parsing
implementation ('com.electronwill.night-config:toml:3.6.6')

// quilt.mod.json5 parsing
implementation ('blue.endless:jankson:1.2.2')

// Testing
testImplementation(gradleTestKit())
testImplementation('org.spockframework:spock-core:2.3-groovy-3.0') {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/dev/architectury/loom/metadata/ModMetadataFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import blue.endless.jankson.Jankson;
import blue.endless.jankson.JsonElement;
import blue.endless.jankson.JsonGrammar;
import blue.endless.jankson.api.SyntaxError;
import com.google.common.collect.ImmutableMap;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.SourceSet;
import org.jetbrains.annotations.Nullable;

import net.fabricmc.loom.util.ExceptionUtil;
import net.fabricmc.loom.util.ZipUtils;
import net.fabricmc.loom.util.gradle.SourceSetHelper;

Expand All @@ -24,6 +30,7 @@ public final class ModMetadataFiles {
private static final Logger LOGGER = Logging.getLogger(ModMetadataFiles.class);
private static final Map<String, Function<byte[], ModMetadataFile>> SINGLE_FILE_METADATA_TYPES = ImmutableMap.<String, Function<byte[], ModMetadataFile>>builder()
.put(QuiltModJson.FILE_NAME, QuiltModJson::of)
.put(QuiltModJson.JSON5_FILE_NAME, convertJson5ToJson(QuiltModJson::of))
.put(ArchitecturyCommonJson.FILE_NAME, ArchitecturyCommonJson::of)
.put(ModsToml.FILE_PATH, onError(ModsToml::of, "Could not load mods.toml", () -> new ErroringModMetadataFile("mods.toml")))
.build();
Expand All @@ -39,6 +46,20 @@ private static <A, B> Function<A, B> onError(Function<A, B> fn, String message,
};
}

private static <R> Function<byte[], R> convertJson5ToJson(Function<byte[], R> next) {
return bytes -> {
var text = new String(bytes, StandardCharsets.UTF_8);
Jankson jankson = Jankson.builder().build();
String json;
try {
json = jankson.fromJson(text, JsonElement.class).toJson(JsonGrammar.STRICT);
} catch (SyntaxError e) {
throw ExceptionUtil.createDescriptiveWrapper(RuntimeException::new, "Could not read JSON5 file", e);
}
return next.apply(json.getBytes(StandardCharsets.UTF_8));
};
}

/**
* Reads the mod metadata file from a jar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

public final class QuiltModJson implements JsonBackedModMetadataFile, SingleIdModMetadataFile {
public static final String FILE_NAME = "quilt.mod.json";
// See RFC 0083
public static final String JSON5_FILE_NAME = "quilt.mod.json5";
private static final Logger LOGGER = LoggerFactory.getLogger(QuiltModJson.class);
private static final String ACCESS_WIDENER_KEY = "access_widener";
private static final String MIXIN_KEY = "mixin";
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/net/fabricmc/loom/task/RemapJarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -42,8 +43,13 @@

import javax.inject.Inject;

import blue.endless.jankson.Jankson;
import blue.endless.jankson.JsonElement;
import blue.endless.jankson.JsonGrammar;
import blue.endless.jankson.api.SyntaxError;
import com.google.gson.JsonObject;
import dev.architectury.loom.extensions.ModBuildExtensions;
import dev.architectury.loom.metadata.QuiltModJson;
import dev.architectury.tinyremapper.OutputConsumerPath;
import dev.architectury.tinyremapper.TinyRemapper;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -79,6 +85,7 @@
import net.fabricmc.loom.task.service.TinyRemapperService;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.ExceptionUtil;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.Pair;
import net.fabricmc.loom.util.SidedClassVisitor;
Expand Down Expand Up @@ -325,6 +332,10 @@ public void execute() {
addNestedJars();
ModBuildExtensions.convertAwToAt(getParameters().getAtAccessWideners(), outputFile, getParameters().getMappingBuildServiceUuid());

if (getParameters().getPlatform().get() == ModPlatform.QUILT) {
convertQmj5();
}

if (getParameters().getPlatform().get() != ModPlatform.FORGE) {
modifyJarManifest();
}
Expand Down Expand Up @@ -451,6 +462,31 @@ private void addRefmaps() throws IOException {
})));
}
}

private void convertQmj5() throws IOException {
byte[] bytes = ZipUtils.unpackNullable(outputFile, QuiltModJson.JSON5_FILE_NAME);
if (bytes == null) return;

if (ZipUtils.contains(outputFile, QuiltModJson.FILE_NAME)) {
throw new IllegalStateException("Output file contains both quilt.mod.json and quilt.mod.json5");
}

Jankson jankson = Jankson.builder().build();
JsonElement json;

try {
json = jankson.fromJson(new String(bytes, StandardCharsets.UTF_8), JsonElement.class);
} catch (SyntaxError e) {
throw ExceptionUtil.createDescriptiveWrapper(RuntimeException::new, "Could not read quilt.mod.json5", e);
}

String qmj = json.toJson(JsonGrammar.STRICT);

try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(outputFile, false)) {
Files.delete(fs.getPath(QuiltModJson.JSON5_FILE_NAME));
Files.writeString(fs.getPath(QuiltModJson.FILE_NAME), qmj, StandardCharsets.UTF_8);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void run() throws IOException {
if (getExtension().isQuilt()) {
launchConfig
.argument("client", "--version")
.argument("client", "Architectury Loom");
.argument("client", "Architectury Loom")
.property("loader.enable_quilt_mod_json5_in_dev_env", "true");
}

if (getExtension().isForge()) {
Expand Down