From ced25abbcc9fe1136c10299e36417c35b96fe068 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Thu, 25 Apr 2024 22:27:32 -0500 Subject: [PATCH] Fix processing to add empty list --- .../multisource/jarinjar/JarInJar.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/main/java/dev/lukebemish/multisource/jarinjar/JarInJar.java b/src/main/java/dev/lukebemish/multisource/jarinjar/JarInJar.java index cfbc284..0b0f028 100644 --- a/src/main/java/dev/lukebemish/multisource/jarinjar/JarInJar.java +++ b/src/main/java/dev/lukebemish/multisource/jarinjar/JarInJar.java @@ -70,36 +70,38 @@ private void addRemainingEntries(Task task) { List includedJars = getJarJarArtifacts().getResolvedArtifacts().get(); Map entries = Maps.newHashMap(); if (getMakeFabricJsons().get()) { - try (ZipFile original = new ZipFile(getArchiveFile().get().getAsFile())) { - var fmj = original.getEntry("fabric.mod.json"); - if (fmj != null) { - try (var is = original.getInputStream(fmj); - var reader = new InputStreamReader(is)) { - var json = GSON.fromJson(reader, JsonObject.class); - JsonArray jars; - if (json.has("jars")) { - jars = json.getAsJsonArray("jars"); - } else { - jars = new JsonArray(); - } - for (var jar : includedJars) { - JsonObject nestedJarEntry = new JsonObject(); - nestedJarEntry.addProperty("file", "META-INF/jars/"+jar.getFile().getName()); - jars.add(nestedJarEntry); + includedJars.forEach(jar -> entries.put("META-INF/jars/"+jar.getFile().getName(), new ByteProvider.FileProvider(addFabricJsonIfMissing(jar)))); + if (!includedJars.isEmpty()) { + try (ZipFile original = new ZipFile(getArchiveFile().get().getAsFile())) { + var fmj = original.getEntry("fabric.mod.json"); + if (fmj != null) { + try (var is = original.getInputStream(fmj); + var reader = new InputStreamReader(is)) { + var json = GSON.fromJson(reader, JsonObject.class); + JsonArray jars; + if (json.has("jars")) { + jars = json.getAsJsonArray("jars"); + } else { + jars = new JsonArray(); + } + for (var jar : includedJars) { + JsonObject nestedJarEntry = new JsonObject(); + nestedJarEntry.addProperty("file", "META-INF/jars/" + jar.getFile().getName()); + jars.add(nestedJarEntry); + } + json.remove("jars"); + json.add("jars", jars); + byte[] bytes = GSON.toJson(json).getBytes(StandardCharsets.UTF_8); + entries.put("fabric.mod.json", new ByteProvider.DirectProvider(bytes)); + } catch (Exception ignored) { + // Could not parse FMJ? + getLogger().warn("Could not parse fabric.mod.json from the original jar, ignoring."); } - json.remove("jars"); - json.add("jars", jars); - byte[] bytes = GSON.toJson(json).getBytes(StandardCharsets.UTF_8); - entries.put("fabric.mod.json", new ByteProvider.DirectProvider(bytes)); - } catch (Exception ignored) { - // Could not parse FMJ? - getLogger().warn("Could not parse fabric.mod.json from the original jar, ignoring."); } + } catch (IOException e) { + throw new UncheckedIOException(e); } - } catch (IOException e) { - throw new UncheckedIOException(e); } - includedJars.forEach(jar -> entries.put("META-INF/jars/"+jar.getFile().getName(), new ByteProvider.FileProvider(addFabricJsonIfMissing(jar)))); } else { includedJars.forEach(jar -> entries.put("META-INF/jars/"+jar.getFile().getName(), new ByteProvider.FileProvider(jar.getFile()))); }