Skip to content

Commit

Permalink
Add support for quilt.mod.json5
Browse files Browse the repository at this point in the history
  • Loading branch information
EnnuiL committed Jul 3, 2023
1 parent 2967882 commit 2a6327a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public static void findCandidatesStatic(ModAdder out) {
try {
Enumeration<URL> fabricMods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources("fabric.mod.json");
Enumeration<URL> quiltMods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources("quilt.mod.json");
Enumeration<URL> quiltQmj5Mods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources("quilt.mod.json5");
while (quiltQmj5Mods.hasMoreElements()) {
URL url = quiltQmj5Mods.nextElement();

try {
Path path = LoaderUtil.normalizeExistingPath(UrlUtil.getCodeSource(url, "quilt.mod.json5"));
List<Path> paths = pathGroups.get(path);

if (paths == null) {
out.addMod(Collections.singletonList(path));
} else {
out.addMod(paths);
}
} catch (UrlConversionException e) {
Log.debug(LogCategory.DISCOVERY, "Error determining location for quilt.mod.json5 from %s", url, e);
}
}
while (quiltMods.hasMoreElements()) {
URL url = quiltMods.nextElement();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public static InternalModMetadata read(InputStream json) throws IOException, Par
public static InternalModMetadata read(InputStream json, Path path, QuiltPluginManager manager, PluginGuiTreeNode warningNode) throws IOException, ParseException {
JsonLoaderValue value;

try (JsonReader reader = JsonReader.json(new InputStreamReader(json, StandardCharsets.UTF_8))) {
try (JsonReader reader = JsonReader.json5(new InputStreamReader(json, StandardCharsets.UTF_8))) {
// Only read as a JSON5 reader if it is a JSON5 file
if (!path.toString().endsWith(".json5")) {
reader.setStrictJson();
}

// Root must be an object
if (reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new ParseException(reader, "A quilt.mod.json must have an object at the root");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,44 @@ private ModLoadOption[] scan0(Path root, QuiltLoaderIcon fileIcon, ModLocation l
PluginGuiTreeNode guiNode) throws IOException {

Path qmj = root.resolve("quilt.mod.json");
if (!FasterFiles.isRegularFile(qmj)) {
Path qmj5 = root.resolve("quilt.mod.json5");
Path usedQmj = qmj;

if (FasterFiles.exists(qmj5)) {
boolean enableQmj5 = QuiltLoader.isDevelopmentEnvironment() || Boolean.getBoolean(SystemProperties.ENABLE_QUILT_MOD_JSON5);
if (!QuiltLoader.isDevelopmentEnvironment() && !enableQmj5) {
QuiltLoaderText title = QuiltLoaderText.translate("gui.text.qmj5_on_production.title");
QuiltDisplayedError error = context().reportError(title);
String describedPath = context().manager().describePath(usedQmj);
error.appendReportText(
"An attempt to read a 'quilt.mod.json5' file was detected: " + describedPath,
"'quilt.mod.json5' files can't be used outside of a development environment without converting to a 'quilt.mod.json' file!"
);
error.appendDescription(
QuiltLoaderText.translate("gui.text.qmj5_on_production.desc.0"),
QuiltLoaderText.translate("gui.text.qmj5_on_production.desc.1"),
QuiltLoaderText.translate("gui.text.qmj5_on_production.desc.2", describedPath)
);
context().manager().getRealContainingFile(root).ifPresent(real ->
error.addFileViewButton(real)
.icon(QuiltLoaderGui.iconJarFile().withDecoration(QuiltLoaderGui.iconQuilt()))
);

guiNode.addChild(QuiltLoaderText.translate("gui.text.qmj5_on_production"));
return null;
}

if (enableQmj5) {
usedQmj = qmj5;
}
}

if (!FasterFiles.isRegularFile(usedQmj)) {
return null;
}

try {
InternalModMetadata meta = ModMetadataReader.read(qmj, context().manager(), guiNode);
InternalModMetadata meta = ModMetadataReader.read(usedQmj, context().manager(), guiNode);

Path from = root;
if (isZip) {
Expand Down Expand Up @@ -282,8 +314,8 @@ private ModLoadOption[] scan0(Path root, QuiltLoaderIcon fileIcon, ModLocation l
"gui.text.invalid_metadata.title", "quilt.mod.json", parse.getMessage()
);
QuiltDisplayedError error = context().reportError(title);
String describedPath = context().manager().describePath(qmj);
error.appendReportText("Invalid 'quilt.mod.json' metadata file:" + describedPath);
String describedPath = context().manager().describePath(usedQmj);
error.appendReportText("Invalid 'quilt.mod.json' metadata file: " + describedPath);
error.appendDescription(QuiltLoaderText.translate("gui.text.invalid_metadata.desc.0", describedPath));
error.appendThrowable(parse);
context().manager().getRealContainingFile(root).ifPresent(real ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public final class SystemProperties {
public static final String DISABLE_PRELOAD_TRANSFORM_CACHE = "loader.transform_cache.disable_preload";
public static final String LOG_CACHE_KEY_CHANGES = "loader.transform_cache.log_changed_keys";
public static final String DISABLE_BEACON = "loader.disable_beacon";
public static final String ENABLE_QUILT_MOD_JSON5 = "loader.debug.enable_quilt_mod_json5";

private SystemProperties() {
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/lang/quilt_loader.properties
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ gui.error.ioexception.desc.0=%s
gui.text.invalid_metadata=Invalid Metadata: %s
gui.text.invalid_metadata.title=Unable to read %s: %s
gui.text.invalid_metadata.desc.0=From %s
gui.text.qmj5_on_production=Unconverted 'quilt.mod.json5' found
gui.text.qmj5_on_production.title=Attempted to read an unconverted 'quilt.mod.json5' file
gui.text.qmj5_on_production.desc.0=An attempt to read a 'quilt.mod.json5' file was detected!
gui.text.qmj5_on_production.desc.1='quilt.mod.json5' files can't be used outside a development environment without converting to a 'quilt.mod.json' file!
gui.text.qmj5_on_production.desc.2=From %s
error.unhandled_solver=Complex solver error; see the crash report for more information
error.unhandled_solver.desc=Please try updating quilt-loader to see if an update can describe this.\nOr report this to the quilt-loader project so this can be fixed.
error.unhandled_solver.desc.rule_n= \n \nRule %s (%s):
Expand Down

0 comments on commit 2a6327a

Please sign in to comment.