Use NIO instead of ZipUtil (#513)
* Use nio for zip utils * Make tests work * Please work * Fix some issues with tests * Fix more issues with tests * NIOZipUtils -> ZipUtils * Resolve Juuxel's reviews * Use our own FS utils * Improve error handling, add loom Pair * Add Unit tests + fixes Co-authored-by: modmuss50 <modmuss50@gmail.com>dev/0.11
parent
b7142ae468
commit
5c190cc3ef
|
@ -62,7 +62,6 @@ dependencies {
|
||||||
|
|
||||||
// libraries
|
// libraries
|
||||||
implementation ('commons-io:commons-io:2.8.0')
|
implementation ('commons-io:commons-io:2.8.0')
|
||||||
implementation ('org.zeroturnaround:zt-zip:1.14')
|
|
||||||
implementation ('com.google.code.gson:gson:2.8.8')
|
implementation ('com.google.code.gson:gson:2.8.8')
|
||||||
implementation ('com.fasterxml.jackson.core:jackson-databind:2.12.5')
|
implementation ('com.fasterxml.jackson.core:jackson-databind:2.12.5')
|
||||||
implementation ('com.google.guava:guava:30.1.1-jre')
|
implementation ('com.google.guava:guava:30.1.1-jre')
|
||||||
|
|
|
@ -27,6 +27,7 @@ package net.fabricmc.loom.build;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -42,13 +43,12 @@ import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
import net.fabricmc.loom.extension.MixinExtension;
|
import net.fabricmc.loom.extension.MixinExtension;
|
||||||
|
import net.fabricmc.loom.util.Pair;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public final class MixinRefmapHelper {
|
public final class MixinRefmapHelper {
|
||||||
private MixinRefmapHelper() { }
|
private MixinRefmapHelper() { }
|
||||||
|
@ -76,18 +76,17 @@ public final class MixinRefmapHelper {
|
||||||
|
|
||||||
String refmapName = container.refmapNameProvider().get();
|
String refmapName = container.refmapNameProvider().get();
|
||||||
|
|
||||||
return ZipUtil.transformEntries(output, mixinConfigs.map(f -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") {
|
try {
|
||||||
@Override
|
return ZipUtils.transformJson(JsonObject.class, outputPath, mixinConfigs.map(f -> new Pair<>(f, json -> {
|
||||||
protected String transform(ZipEntry zipEntry, String input) {
|
|
||||||
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
|
||||||
|
|
||||||
if (!json.has("refmap")) {
|
if (!json.has("refmap")) {
|
||||||
json.addProperty("refmap", refmapName);
|
json.addProperty("refmap", refmapName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoomGradlePlugin.GSON.toJson(json);
|
return json;
|
||||||
|
}))) > 0;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to transform mixin configs in jar", e);
|
||||||
}
|
}
|
||||||
})).toArray(ZipEntryTransformerEntry[]::new));
|
|
||||||
}).reduce(false, Boolean::logicalOr);
|
}).reduce(false, Boolean::logicalOr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
project.getLogger().error(e.getMessage());
|
project.getLogger().error(e.getMessage());
|
||||||
|
|
|
@ -25,22 +25,22 @@
|
||||||
package net.fabricmc.loom.build.nesting;
|
package net.fabricmc.loom.build.nesting;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import org.gradle.api.UncheckedIOException;
|
||||||
import org.gradle.api.logging.Logger;
|
import org.gradle.api.logging.Logger;
|
||||||
import org.zeroturnaround.zip.FileSource;
|
|
||||||
import org.zeroturnaround.zip.ZipEntrySource;
|
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
|
||||||
import net.fabricmc.loom.util.ModUtils;
|
import net.fabricmc.loom.util.ModUtils;
|
||||||
|
import net.fabricmc.loom.util.Pair;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public class JarNester {
|
public class JarNester {
|
||||||
public static void nestJars(Collection<File> jars, File modJar, Logger logger) {
|
public static void nestJars(Collection<File> jars, File modJar, Logger logger) {
|
||||||
|
@ -51,12 +51,16 @@ public class JarNester {
|
||||||
|
|
||||||
Preconditions.checkArgument(ModUtils.isMod(modJar), "Cannot nest jars into none mod jar " + modJar.getName());
|
Preconditions.checkArgument(ModUtils.isMod(modJar), "Cannot nest jars into none mod jar " + modJar.getName());
|
||||||
|
|
||||||
ZipUtil.addEntries(modJar, jars.stream().map(file -> new FileSource("META-INF/jars/" + file.getName(), file)).toArray(ZipEntrySource[]::new));
|
try {
|
||||||
|
ZipUtils.add(modJar.toPath(), jars.stream().map(file -> {
|
||||||
|
try {
|
||||||
|
return new Pair<>("META-INF/jars/" + file.getName(), Files.readAllBytes(file.toPath()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException(e);
|
||||||
|
}
|
||||||
|
}).collect(Collectors.toList()));
|
||||||
|
|
||||||
boolean didNest = ZipUtil.transformEntries(modJar, single(new ZipEntryTransformerEntry("fabric.mod.json", new StringZipEntryTransformer() {
|
int count = ZipUtils.transformJson(JsonObject.class, modJar.toPath(), Stream.of(new Pair<>("fabric.mod.json", json -> {
|
||||||
@Override
|
|
||||||
protected String transform(ZipEntry zipEntry, String input) {
|
|
||||||
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
|
||||||
JsonArray nestedJars = json.getAsJsonArray("jars");
|
JsonArray nestedJars = json.getAsJsonArray("jars");
|
||||||
|
|
||||||
if (nestedJars == null || !json.has("jars")) {
|
if (nestedJars == null || !json.has("jars")) {
|
||||||
|
@ -83,13 +87,12 @@ public class JarNester {
|
||||||
|
|
||||||
json.add("jars", nestedJars);
|
json.add("jars", nestedJars);
|
||||||
|
|
||||||
return LoomGradlePlugin.GSON.toJson(json);
|
return json;
|
||||||
}
|
|
||||||
})));
|
})));
|
||||||
Preconditions.checkArgument(didNest, "Failed to nest jars into " + modJar.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ZipEntryTransformerEntry[] single(ZipEntryTransformerEntry element) {
|
Preconditions.checkState(count > 0, "Failed to transform fabric.mod.json");
|
||||||
return new ZipEntryTransformerEntry[]{element};
|
} catch (IOException e) {
|
||||||
|
throw new java.io.UncheckedIOException("Failed to nest jars into " + modJar.getName(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ package net.fabricmc.loom.build.nesting;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -47,12 +48,12 @@ import org.gradle.api.artifacts.ResolvedArtifact;
|
||||||
import org.gradle.api.artifacts.ResolvedConfiguration;
|
import org.gradle.api.artifacts.ResolvedConfiguration;
|
||||||
import org.gradle.api.artifacts.ResolvedDependency;
|
import org.gradle.api.artifacts.ResolvedDependency;
|
||||||
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
|
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
import net.fabricmc.loom.task.RemapJarTask;
|
import net.fabricmc.loom.task.RemapJarTask;
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public final class NestedDependencyProvider implements NestedJarProvider {
|
public final class NestedDependencyProvider implements NestedJarProvider {
|
||||||
final Project project;
|
final Project project;
|
||||||
|
@ -157,7 +158,7 @@ public final class NestedDependencyProvider implements NestedJarProvider {
|
||||||
File file = metaFile.file;
|
File file = metaFile.file;
|
||||||
|
|
||||||
//A lib that doesnt have a mod.json, we turn it into a fake mod
|
//A lib that doesnt have a mod.json, we turn it into a fake mod
|
||||||
if (!ZipUtil.containsEntry(file, "fabric.mod.json")) {
|
if (!ZipUtils.contains(file.toPath(), "fabric.mod.json")) {
|
||||||
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||||
File tempDir = new File(extension.getFiles().getUserCache(), "temp/modprocessing");
|
File tempDir = new File(extension.getFiles().getUserCache(), "temp/modprocessing");
|
||||||
|
|
||||||
|
@ -177,7 +178,12 @@ public final class NestedDependencyProvider implements NestedJarProvider {
|
||||||
throw new RuntimeException("Failed to copy file", e);
|
throw new RuntimeException("Failed to copy file", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipUtil.addEntry(tempFile, "fabric.mod.json", generateModForDependency(metaFile).getBytes());
|
try {
|
||||||
|
ZipUtils.add(tempFile.toPath(), "fabric.mod.json", generateModForDependency(metaFile).getBytes());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to add dummy mod while including %s".formatted(file), e);
|
||||||
|
}
|
||||||
|
|
||||||
fileList.add(tempFile);
|
fileList.add(tempFile);
|
||||||
} else {
|
} else {
|
||||||
// Default copy the jar right in
|
// Default copy the jar right in
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
package net.fabricmc.loom.configuration;
|
package net.fabricmc.loom.configuration;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -45,12 +47,12 @@ import org.gradle.api.artifacts.Configuration;
|
||||||
import org.gradle.api.artifacts.Dependency;
|
import org.gradle.api.artifacts.Dependency;
|
||||||
import org.gradle.api.artifacts.ResolvedDependency;
|
import org.gradle.api.artifacts.ResolvedDependency;
|
||||||
import org.gradle.api.artifacts.SelfResolvingDependency;
|
import org.gradle.api.artifacts.SelfResolvingDependency;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
import net.fabricmc.loom.configuration.providers.MinecraftProvider;
|
import net.fabricmc.loom.configuration.providers.MinecraftProvider;
|
||||||
import net.fabricmc.loom.extension.LoomFiles;
|
import net.fabricmc.loom.extension.LoomFiles;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public abstract class DependencyProvider {
|
public abstract class DependencyProvider {
|
||||||
private LoomDependencyManager dependencyManager;
|
private LoomDependencyManager dependencyManager;
|
||||||
|
@ -239,10 +241,12 @@ public abstract class DependencyProvider {
|
||||||
} else {
|
} else {
|
||||||
group = "net.fabricmc.synthetic";
|
group = "net.fabricmc.synthetic";
|
||||||
File root = classifierToFile.get(""); //We've built the classifierToFile map, now to try find a name and version for our dependency
|
File root = classifierToFile.get(""); //We've built the classifierToFile map, now to try find a name and version for our dependency
|
||||||
|
byte[] modJson;
|
||||||
|
|
||||||
if ("jar".equals(FilenameUtils.getExtension(root.getName())) && ZipUtil.containsEntry(root, "fabric.mod.json")) {
|
try {
|
||||||
|
if ("jar".equals(FilenameUtils.getExtension(root.getName())) && (modJson = ZipUtils.unpackNullable(root.toPath(), "fabric.mod.json")) != null) {
|
||||||
//It's a Fabric mod, see how much we can extract out
|
//It's a Fabric mod, see how much we can extract out
|
||||||
JsonObject json = new Gson().fromJson(new String(ZipUtil.unpackEntry(root, "fabric.mod.json"), StandardCharsets.UTF_8), JsonObject.class);
|
JsonObject json = new Gson().fromJson(new String(modJson, StandardCharsets.UTF_8), JsonObject.class);
|
||||||
|
|
||||||
if (json == null || !json.has("id") || !json.has("version")) {
|
if (json == null || !json.has("id") || !json.has("version")) {
|
||||||
throw new IllegalArgumentException("Invalid Fabric mod jar: " + root + " (malformed json: " + json + ')');
|
throw new IllegalArgumentException("Invalid Fabric mod jar: " + root + " (malformed json: " + json + ')');
|
||||||
|
@ -260,6 +264,9 @@ public abstract class DependencyProvider {
|
||||||
name = FilenameUtils.removeExtension(root.getName());
|
name = FilenameUtils.removeExtension(root.getName());
|
||||||
version = "1.0";
|
version = "1.0";
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to read input file: " + root, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,12 +24,15 @@
|
||||||
|
|
||||||
package net.fabricmc.loom.configuration.accesswidener;
|
package net.fabricmc.loom.configuration.accesswidener;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public record AccessWidenerFile(
|
public record AccessWidenerFile(
|
||||||
String name,
|
String name,
|
||||||
|
@ -40,7 +43,13 @@ public record AccessWidenerFile(
|
||||||
* Reads the access-widener contained in a mod jar, or returns null if there is none.
|
* Reads the access-widener contained in a mod jar, or returns null if there is none.
|
||||||
*/
|
*/
|
||||||
public static AccessWidenerFile fromModJar(Path modJarPath) {
|
public static AccessWidenerFile fromModJar(Path modJarPath) {
|
||||||
byte[] modJsonBytes = ZipUtil.unpackEntry(modJarPath.toFile(), "fabric.mod.json");
|
byte[] modJsonBytes;
|
||||||
|
|
||||||
|
try {
|
||||||
|
modJsonBytes = ZipUtils.unpackNullable(modJarPath, "fabric.mod.json");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to read access-widener file from: " + modJarPath.toAbsolutePath(), e);
|
||||||
|
}
|
||||||
|
|
||||||
if (modJsonBytes == null) {
|
if (modJsonBytes == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -55,7 +64,13 @@ public record AccessWidenerFile(
|
||||||
String awPath = jsonObject.get("accessWidener").getAsString();
|
String awPath = jsonObject.get("accessWidener").getAsString();
|
||||||
String modId = jsonObject.get("id").getAsString();
|
String modId = jsonObject.get("id").getAsString();
|
||||||
|
|
||||||
byte[] content = ZipUtil.unpackEntry(modJarPath.toFile(), awPath);
|
byte[] content;
|
||||||
|
|
||||||
|
try {
|
||||||
|
content = ZipUtils.unpack(modJarPath, awPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Could not find access widener file (%s) defined in the fabric.mod.json file of %s".formatted(awPath, modJarPath.toAbsolutePath()), e);
|
||||||
|
}
|
||||||
|
|
||||||
return new AccessWidenerFile(
|
return new AccessWidenerFile(
|
||||||
awPath,
|
awPath,
|
||||||
|
|
|
@ -26,6 +26,7 @@ package net.fabricmc.loom.configuration.accesswidener;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -34,7 +35,6 @@ import java.util.Arrays;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.objectweb.asm.commons.Remapper;
|
import org.objectweb.asm.commons.Remapper;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.accesswidener.AccessWidener;
|
import net.fabricmc.accesswidener.AccessWidener;
|
||||||
import net.fabricmc.accesswidener.AccessWidenerReader;
|
import net.fabricmc.accesswidener.AccessWidenerReader;
|
||||||
|
@ -43,6 +43,7 @@ import net.fabricmc.accesswidener.AccessWidenerWriter;
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||||
import net.fabricmc.loom.configuration.processors.JarProcessor;
|
import net.fabricmc.loom.configuration.processors.JarProcessor;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public class AccessWidenerJarProcessor implements JarProcessor {
|
public class AccessWidenerJarProcessor implements JarProcessor {
|
||||||
// Filename used to store hash of input access widener in processed jar file
|
// Filename used to store hash of input access widener in processed jar file
|
||||||
|
@ -87,7 +88,12 @@ public class AccessWidenerJarProcessor implements JarProcessor {
|
||||||
public void process(File file) {
|
public void process(File file) {
|
||||||
AccessWidenerTransformer applier = new AccessWidenerTransformer(project.getLogger(), accessWidener);
|
AccessWidenerTransformer applier = new AccessWidenerTransformer(project.getLogger(), accessWidener);
|
||||||
applier.apply(file);
|
applier.apply(file);
|
||||||
ZipUtil.addEntry(file, HASH_FILENAME, inputHash);
|
|
||||||
|
try {
|
||||||
|
ZipUtils.add(file.toPath(), HASH_FILENAME, inputHash);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to write aw jar hash", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,7 +117,13 @@ public class AccessWidenerJarProcessor implements JarProcessor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isInvalid(File file) {
|
public boolean isInvalid(File file) {
|
||||||
byte[] hash = ZipUtil.unpackEntry(file, HASH_FILENAME);
|
byte[] hash;
|
||||||
|
|
||||||
|
try {
|
||||||
|
hash = ZipUtils.unpackNullable(file.toPath(), HASH_FILENAME);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (hash == null) {
|
if (hash == null) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -25,21 +25,22 @@
|
||||||
package net.fabricmc.loom.configuration.accesswidener;
|
package net.fabricmc.loom.configuration.accesswidener;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.gradle.api.logging.Logger;
|
import org.gradle.api.logging.Logger;
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
import org.objectweb.asm.ClassVisitor;
|
import org.objectweb.asm.ClassVisitor;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.ByteArrayZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
import net.fabricmc.accesswidener.AccessWidener;
|
import net.fabricmc.accesswidener.AccessWidener;
|
||||||
import net.fabricmc.accesswidener.AccessWidenerClassVisitor;
|
import net.fabricmc.accesswidener.AccessWidenerClassVisitor;
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
|
import net.fabricmc.loom.util.Pair;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
final class AccessWidenerTransformer {
|
final class AccessWidenerTransformer {
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
@ -55,19 +56,22 @@ final class AccessWidenerTransformer {
|
||||||
*/
|
*/
|
||||||
void apply(File jarFile) {
|
void apply(File jarFile) {
|
||||||
logger.lifecycle("Processing file: " + jarFile.getName());
|
logger.lifecycle("Processing file: " + jarFile.getName());
|
||||||
ZipUtil.transformEntries(jarFile, getTransformers(accessWidener.getTargets()));
|
|
||||||
|
try {
|
||||||
|
ZipUtils.transform(jarFile.toPath(), getTransformers(accessWidener.getTargets()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to apply access wideners to %s".formatted(jarFile), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ZipEntryTransformerEntry[] getTransformers(Set<String> classes) {
|
private List<Pair<String, ZipUtils.UnsafeUnaryOperator<byte[]>>> getTransformers(Set<String> classes) {
|
||||||
return classes.stream()
|
return classes.stream()
|
||||||
.map(string -> new ZipEntryTransformerEntry(string.replaceAll("\\.", "/") + ".class", getTransformer(string)))
|
.map(string -> new Pair<>(string.replaceAll("\\.", "/") + ".class", getTransformer(string)))
|
||||||
.toArray(ZipEntryTransformerEntry[]::new);
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private ZipEntryTransformer getTransformer(String className) {
|
private ZipUtils.UnsafeUnaryOperator<byte[]> getTransformer(String className) {
|
||||||
return new ByteArrayZipEntryTransformer() {
|
return input -> {
|
||||||
@Override
|
|
||||||
protected byte[] transform(ZipEntry zipEntry, byte[] input) {
|
|
||||||
ClassReader reader = new ClassReader(input);
|
ClassReader reader = new ClassReader(input);
|
||||||
ClassWriter writer = new ClassWriter(0);
|
ClassWriter writer = new ClassWriter(0);
|
||||||
ClassVisitor classVisitor = AccessWidenerClassVisitor.createClassVisitor(Constants.ASM_VERSION, writer, accessWidener);
|
ClassVisitor classVisitor = AccessWidenerClassVisitor.createClassVisitor(Constants.ASM_VERSION, writer, accessWidener);
|
||||||
|
@ -76,7 +80,6 @@ final class AccessWidenerTransformer {
|
||||||
|
|
||||||
reader.accept(classVisitor, 0);
|
reader.accept(classVisitor, 0);
|
||||||
return writer.toByteArray();
|
return writer.toByteArray();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ package net.fabricmc.loom.configuration.mods;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -41,9 +42,6 @@ import java.util.zip.ZipEntry;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.objectweb.asm.commons.Remapper;
|
import org.objectweb.asm.commons.Remapper;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
import net.fabricmc.accesswidener.AccessWidenerReader;
|
import net.fabricmc.accesswidener.AccessWidenerReader;
|
||||||
import net.fabricmc.accesswidener.AccessWidenerRemapper;
|
import net.fabricmc.accesswidener.AccessWidenerRemapper;
|
||||||
|
@ -57,6 +55,7 @@ import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
|
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
import net.fabricmc.loom.util.TinyRemapperHelper;
|
import net.fabricmc.loom.util.TinyRemapperHelper;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
import net.fabricmc.tinyremapper.InputTag;
|
import net.fabricmc.tinyremapper.InputTag;
|
||||||
import net.fabricmc.tinyremapper.NonClassCopyMode;
|
import net.fabricmc.tinyremapper.NonClassCopyMode;
|
||||||
import net.fabricmc.tinyremapper.OutputConsumerPath;
|
import net.fabricmc.tinyremapper.OutputConsumerPath;
|
||||||
|
@ -95,14 +94,14 @@ public class ModProcessor {
|
||||||
|
|
||||||
private static void stripNestedJars(File file) {
|
private static void stripNestedJars(File file) {
|
||||||
// Strip out all contained jar info as we dont want loader to try and load the jars contained in dev.
|
// Strip out all contained jar info as we dont want loader to try and load the jars contained in dev.
|
||||||
ZipUtil.transformEntries(file, new ZipEntryTransformerEntry[]{(new ZipEntryTransformerEntry("fabric.mod.json", new StringZipEntryTransformer() {
|
try {
|
||||||
@Override
|
ZipUtils.transformJson(JsonObject.class, file.toPath(), Map.of("fabric.mod.json", json -> {
|
||||||
protected String transform(ZipEntry zipEntry, String input) {
|
|
||||||
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
|
||||||
json.remove("jars");
|
json.remove("jars");
|
||||||
return LoomGradlePlugin.GSON.toJson(json);
|
return json;
|
||||||
|
}));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to strip nested jars from %s".formatted(file), e);
|
||||||
}
|
}
|
||||||
}))});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -180,7 +179,7 @@ public class ModProcessor {
|
||||||
String accessWidener = info.getAccessWidener();
|
String accessWidener = info.getAccessWidener();
|
||||||
|
|
||||||
if (accessWidener != null) {
|
if (accessWidener != null) {
|
||||||
accessWidenerMap.put(info, remapAccessWidener(ZipUtil.unpackEntry(info.inputFile, accessWidener), remapper.getRemapper()));
|
accessWidenerMap.put(info, remapAccessWidener(ZipUtils.unpack(info.inputFile.toPath(), accessWidener), remapper.getRemapper()));
|
||||||
}
|
}
|
||||||
|
|
||||||
remapper.apply(outputConsumer, tagMap.get(info));
|
remapper.apply(outputConsumer, tagMap.get(info));
|
||||||
|
@ -199,7 +198,7 @@ public class ModProcessor {
|
||||||
byte[] accessWidener = accessWidenerMap.get(info);
|
byte[] accessWidener = accessWidenerMap.get(info);
|
||||||
|
|
||||||
if (accessWidener != null) {
|
if (accessWidener != null) {
|
||||||
ZipUtil.replaceEntry(info.getRemappedOutput(), info.getAccessWidener(), accessWidener);
|
ZipUtils.replace(info.getRemappedOutput().toPath(), info.getAccessWidener(), accessWidener);
|
||||||
}
|
}
|
||||||
|
|
||||||
info.finaliseRemapping();
|
info.finaliseRemapping();
|
||||||
|
|
|
@ -24,24 +24,24 @@
|
||||||
|
|
||||||
package net.fabricmc.loom.configuration.processors;
|
package net.fabricmc.loom.configuration.processors;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.jar.Attributes;
|
import java.util.jar.Attributes;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import com.google.common.io.CharSource;
|
import com.google.common.io.CharSource;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.StreamZipEntryTransformer;
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
public class JarProcessorManager {
|
public class JarProcessorManager {
|
||||||
private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
|
private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
|
||||||
|
@ -107,19 +107,18 @@ public class JarProcessorManager {
|
||||||
jarProcessor.process(file);
|
jarProcessor.process(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean manifestTransformed = ZipUtil.transformEntries(file, new ZipEntryTransformerEntry[] {
|
try {
|
||||||
new ZipEntryTransformerEntry(MANIFEST_PATH, new StreamZipEntryTransformer() {
|
int count = ZipUtils.transform(file.toPath(), Map.of(MANIFEST_PATH, bytes -> {
|
||||||
@Override
|
Manifest manifest = new Manifest(new ByteArrayInputStream(bytes));
|
||||||
protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
|
|
||||||
Manifest manifest = new Manifest(in);
|
|
||||||
manifest.getMainAttributes().putValue(JAR_PROCESSOR_HASH_ATTRIBUTE, getJarProcessorHash());
|
manifest.getMainAttributes().putValue(JAR_PROCESSOR_HASH_ATTRIBUTE, getJarProcessorHash());
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
manifest.write(out);
|
manifest.write(out);
|
||||||
}
|
return out.toByteArray();
|
||||||
})
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
if (!manifestTransformed) {
|
Preconditions.checkState(count > 0, "Did not add data to jar manifest in " + file);
|
||||||
throw new RuntimeException("Could not add data to jar manifest in " + file);
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Could not add data to jar manifest in " + file, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,14 +43,12 @@ import org.gradle.api.artifacts.FileCollectionDependency;
|
||||||
import org.gradle.api.artifacts.SelfResolvingDependency;
|
import org.gradle.api.artifacts.SelfResolvingDependency;
|
||||||
import org.gradle.api.file.FileCollection;
|
import org.gradle.api.file.FileCollection;
|
||||||
import org.gradle.api.tasks.TaskDependency;
|
import org.gradle.api.tasks.TaskDependency;
|
||||||
import org.zeroturnaround.zip.ByteSource;
|
|
||||||
import org.zeroturnaround.zip.ZipEntrySource;
|
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingContext;
|
import net.fabricmc.loom.api.mappings.layered.MappingContext;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingLayer;
|
import net.fabricmc.loom.api.mappings.layered.MappingLayer;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
import net.fabricmc.mappingio.adapter.MappingDstNsReorder;
|
import net.fabricmc.mappingio.adapter.MappingDstNsReorder;
|
||||||
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
|
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
|
||||||
import net.fabricmc.mappingio.format.Tiny2Writer;
|
import net.fabricmc.mappingio.format.Tiny2Writer;
|
||||||
|
@ -104,9 +102,8 @@ public class LayeredMappingsDependency implements SelfResolvingDependency, FileC
|
||||||
MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(nsReorder, MappingsNamespace.INTERMEDIARY.toString(), true);
|
MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(nsReorder, MappingsNamespace.INTERMEDIARY.toString(), true);
|
||||||
mappings.accept(nsSwitch);
|
mappings.accept(nsSwitch);
|
||||||
|
|
||||||
ZipUtil.pack(new ZipEntrySource[] {
|
Files.deleteIfExists(mappingsFile);
|
||||||
new ByteSource("mappings/mappings.tiny", writer.toString().getBytes(StandardCharsets.UTF_8))
|
ZipUtils.add(mappingsFile, "mappings/mappings.tiny", writer.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
}, mappingsFile.toFile());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,10 +116,7 @@ public class LayeredMappingsDependency implements SelfResolvingDependency, FileC
|
||||||
|
|
||||||
byte[] data = LoomGradlePlugin.OBJECT_MAPPER.writeValueAsString(signatureFixes).getBytes(StandardCharsets.UTF_8);
|
byte[] data = LoomGradlePlugin.OBJECT_MAPPER.writeValueAsString(signatureFixes).getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
ZipUtil.addEntry(
|
ZipUtils.add(mappingsFile, "extras/record_signatures.json", data);
|
||||||
mappingsFile.toFile(),
|
|
||||||
new ByteSource("extras/record_signatures.json", data)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -37,8 +37,8 @@ import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -48,9 +48,6 @@ import com.google.gson.JsonObject;
|
||||||
import org.apache.tools.ant.util.StringUtils;
|
import org.apache.tools.ant.util.StringUtils;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.zeroturnaround.zip.FileSource;
|
|
||||||
import org.zeroturnaround.zip.ZipEntrySource;
|
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
|
@ -65,6 +62,7 @@ import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvid
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
import net.fabricmc.loom.util.DeletingFileVisitor;
|
import net.fabricmc.loom.util.DeletingFileVisitor;
|
||||||
import net.fabricmc.loom.util.DownloadUtil;
|
import net.fabricmc.loom.util.DownloadUtil;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
import net.fabricmc.mappingio.MappingReader;
|
import net.fabricmc.mappingio.MappingReader;
|
||||||
import net.fabricmc.mappingio.adapter.MappingNsCompleter;
|
import net.fabricmc.mappingio.adapter.MappingNsCompleter;
|
||||||
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
|
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
|
||||||
|
@ -131,7 +129,8 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
|
||||||
mappingTree = readMappings();
|
mappingTree = readMappings();
|
||||||
|
|
||||||
if (Files.notExists(tinyMappingsJar) || isRefreshDeps()) {
|
if (Files.notExists(tinyMappingsJar) || isRefreshDeps()) {
|
||||||
ZipUtil.pack(new ZipEntrySource[] {new FileSource("mappings/mappings.tiny", tinyMappings.toFile())}, tinyMappingsJar.toFile());
|
Files.deleteIfExists(tinyMappingsJar);
|
||||||
|
ZipUtils.add(tinyMappingsJar, "mappings/mappings.tiny", Files.readAllBytes(tinyMappings));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasUnpickDefinitions()) {
|
if (hasUnpickDefinitions()) {
|
||||||
|
|
|
@ -35,12 +35,12 @@ import java.util.stream.Collectors;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.gradle.api.GradleException;
|
import org.gradle.api.GradleException;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.LoomGradlePlugin;
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
import net.fabricmc.loom.util.HashedDownloadUtil;
|
import net.fabricmc.loom.util.HashedDownloadUtil;
|
||||||
import net.fabricmc.loom.util.MirrorUtil;
|
import net.fabricmc.loom.util.MirrorUtil;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
|
|
||||||
public class MinecraftNativesProvider {
|
public class MinecraftNativesProvider {
|
||||||
private final Project project;
|
private final Project project;
|
||||||
|
@ -101,7 +101,7 @@ public class MinecraftNativesProvider {
|
||||||
throw new GradleException("Native jar not found at " + libJarFile.getAbsolutePath());
|
throw new GradleException("Native jar not found at " + libJarFile.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipUtil.unpack(libJarFile, nativesDir);
|
ZipUtils.unpackAll(libJarFile.toPath(), nativesDir.toPath());
|
||||||
|
|
||||||
// Store a file containing the hash of the extracted natives, used on subsequent runs to skip extracting all the natives if they haven't changed
|
// Store a file containing the hash of the extracted natives, used on subsequent runs to skip extracting all the natives if they haven't changed
|
||||||
File libSha1File = new File(nativesDir, libJarFile.getName() + ".sha1");
|
File libSha1File = new File(nativesDir, libJarFile.getName() + ".sha1");
|
||||||
|
|
|
@ -62,6 +62,7 @@ import net.fabricmc.loom.configuration.accesswidener.TransitiveAccessWidenerMapp
|
||||||
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||||
import net.fabricmc.loom.decompilers.LineNumberRemapper;
|
import net.fabricmc.loom.decompilers.LineNumberRemapper;
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
|
import net.fabricmc.loom.util.FileSystemUtil;
|
||||||
import net.fabricmc.loom.util.IOStringConsumer;
|
import net.fabricmc.loom.util.IOStringConsumer;
|
||||||
import net.fabricmc.loom.util.OperatingSystem;
|
import net.fabricmc.loom.util.OperatingSystem;
|
||||||
import net.fabricmc.loom.util.gradle.ThreadedProgressLoggerConsumer;
|
import net.fabricmc.loom.util.gradle.ThreadedProgressLoggerConsumer;
|
||||||
|
@ -69,7 +70,6 @@ import net.fabricmc.loom.util.gradle.ThreadedSimpleProgressLogger;
|
||||||
import net.fabricmc.loom.util.gradle.WorkerDaemonClientsManagerHelper;
|
import net.fabricmc.loom.util.gradle.WorkerDaemonClientsManagerHelper;
|
||||||
import net.fabricmc.loom.util.ipc.IPCClient;
|
import net.fabricmc.loom.util.ipc.IPCClient;
|
||||||
import net.fabricmc.loom.util.ipc.IPCServer;
|
import net.fabricmc.loom.util.ipc.IPCServer;
|
||||||
import net.fabricmc.stitch.util.StitchUtil;
|
|
||||||
|
|
||||||
public abstract class GenerateSourcesTask extends AbstractLoomTask {
|
public abstract class GenerateSourcesTask extends AbstractLoomTask {
|
||||||
public final LoomDecompiler decompiler;
|
public final LoomDecompiler decompiler;
|
||||||
|
@ -265,8 +265,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
|
||||||
LineNumberRemapper remapper = new LineNumberRemapper();
|
LineNumberRemapper remapper = new LineNumberRemapper();
|
||||||
remapper.readMappings(linemap.toFile());
|
remapper.readMappings(linemap.toFile());
|
||||||
|
|
||||||
try (StitchUtil.FileSystemDelegate inFs = StitchUtil.getJarFileSystem(oldCompiledJar.toFile(), true);
|
try (FileSystemUtil.Delegate inFs = FileSystemUtil.getJarFileSystem(oldCompiledJar.toFile(), true);
|
||||||
StitchUtil.FileSystemDelegate outFs = StitchUtil.getJarFileSystem(linemappedJarDestination.toFile(), true)) {
|
FileSystemUtil.Delegate outFs = FileSystemUtil.getJarFileSystem(linemappedJarDestination.toFile(), true)) {
|
||||||
remapper.process(logger, inFs.get().getPath("/"), outFs.get().getPath("/"));
|
remapper.process(logger, inFs.get().getPath("/"), outFs.get().getPath("/"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,20 +24,21 @@
|
||||||
|
|
||||||
package net.fabricmc.loom.task;
|
package net.fabricmc.loom.task;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.UncheckedIOException;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import org.gradle.api.Action;
|
import org.gradle.api.Action;
|
||||||
|
@ -52,9 +53,6 @@ import org.gradle.api.tasks.InputFile;
|
||||||
import org.gradle.api.tasks.TaskAction;
|
import org.gradle.api.tasks.TaskAction;
|
||||||
import org.gradle.jvm.tasks.Jar;
|
import org.gradle.jvm.tasks.Jar;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
import org.zeroturnaround.zip.transform.StreamZipEntryTransformer;
|
|
||||||
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||||
|
@ -72,6 +70,7 @@ import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||||
import net.fabricmc.loom.util.Constants;
|
import net.fabricmc.loom.util.Constants;
|
||||||
import net.fabricmc.loom.util.TinyRemapperHelper;
|
import net.fabricmc.loom.util.TinyRemapperHelper;
|
||||||
import net.fabricmc.loom.util.ZipReprocessorUtil;
|
import net.fabricmc.loom.util.ZipReprocessorUtil;
|
||||||
|
import net.fabricmc.loom.util.ZipUtils;
|
||||||
import net.fabricmc.stitch.util.Pair;
|
import net.fabricmc.stitch.util.Pair;
|
||||||
import net.fabricmc.tinyremapper.TinyRemapper;
|
import net.fabricmc.tinyremapper.TinyRemapper;
|
||||||
import net.fabricmc.tinyremapper.TinyUtils;
|
import net.fabricmc.tinyremapper.TinyUtils;
|
||||||
|
@ -190,26 +189,31 @@ public class RemapJarTask extends Jar {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accessWidener != null) {
|
if (accessWidener != null) {
|
||||||
boolean replaced = ZipUtil.replaceEntry(data.output.toFile(), accessWidener.getLeft(), accessWidener.getRight());
|
try {
|
||||||
Preconditions.checkArgument(replaced, "Failed to remap access widener");
|
ZipUtils.replace(data.output, accessWidener.getLeft(), accessWidener.getRight());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to replace access widener in output jar", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add data to the manifest
|
// Add data to the manifest
|
||||||
boolean transformed = ZipUtil.transformEntries(data.output.toFile(), new ZipEntryTransformerEntry[]{
|
try {
|
||||||
new ZipEntryTransformerEntry(MANIFEST_PATH, new StreamZipEntryTransformer() {
|
int count = ZipUtils.transform(data.output, Map.of(MANIFEST_PATH, bytes -> {
|
||||||
@Override
|
var manifest = new Manifest(new ByteArrayInputStream(bytes));
|
||||||
protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
|
|
||||||
var manifest = new Manifest(in);
|
|
||||||
var manifestConfiguration = new JarManifestConfiguration(project);
|
var manifestConfiguration = new JarManifestConfiguration(project);
|
||||||
|
|
||||||
manifestConfiguration.configure(manifest);
|
manifestConfiguration.configure(manifest);
|
||||||
manifest.getMainAttributes().putValue("Fabric-Mapping-Namespace", toM);
|
manifest.getMainAttributes().putValue("Fabric-Mapping-Namespace", toM);
|
||||||
|
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
manifest.write(out);
|
manifest.write(out);
|
||||||
|
return out.toByteArray();
|
||||||
|
}));
|
||||||
|
|
||||||
|
Preconditions.checkState(count > 0, "Did not transform any jar manifest");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to transform jar manifest", e);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
});
|
|
||||||
Preconditions.checkArgument(transformed, "Failed to transform jar manifest");
|
|
||||||
|
|
||||||
if (isReproducibleFileOrder() || !isPreserveFileTimestamps()) {
|
if (isReproducibleFileOrder() || !isPreserveFileTimestamps()) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*
|
||||||
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016-2017 FabricMC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.fabricmc.loom.util;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.file.FileSystem;
|
||||||
|
import java.nio.file.FileSystemAlreadyExistsException;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public final class FileSystemUtil {
|
||||||
|
public record Delegate(FileSystem fs, boolean owner) implements AutoCloseable, Supplier<FileSystem> {
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (owner) {
|
||||||
|
fs.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileSystem get() {
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileSystemUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Map<String, String> jfsArgsCreate = Map.of("create", "true");
|
||||||
|
private static final Map<String, String> jfsArgsEmpty = Collections.emptyMap();
|
||||||
|
|
||||||
|
public static Delegate getJarFileSystem(File file, boolean create) throws IOException {
|
||||||
|
return getJarFileSystem(file.toURI(), create);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Delegate getJarFileSystem(Path path, boolean create) throws IOException {
|
||||||
|
return getJarFileSystem(path.toUri(), create);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Delegate getJarFileSystem(URI uri, boolean create) throws IOException {
|
||||||
|
URI jarUri;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jarUri = new URI("jar:" + uri.getScheme(), uri.getHost(), uri.getPath(), uri.getFragment());
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new Delegate(FileSystems.newFileSystem(jarUri, create ? jfsArgsCreate : jfsArgsEmpty), true);
|
||||||
|
} catch (FileSystemAlreadyExistsException e) {
|
||||||
|
return new Delegate(FileSystems.getFileSystem(jarUri), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 FabricMC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.fabricmc.loom.util;
|
||||||
|
|
||||||
|
public record Pair<L, R>(L left, R right) {
|
||||||
|
}
|
|
@ -37,7 +37,6 @@ import org.cadixdev.lorenz.MappingSet;
|
||||||
import org.cadixdev.mercury.Mercury;
|
import org.cadixdev.mercury.Mercury;
|
||||||
import org.cadixdev.mercury.remapper.MercuryRemapper;
|
import org.cadixdev.mercury.remapper.MercuryRemapper;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
|
||||||
|
|
||||||
import net.fabricmc.loom.LoomGradleExtension;
|
import net.fabricmc.loom.LoomGradleExtension;
|
||||||
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||||
|
@ -46,7 +45,6 @@ import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||||
import net.fabricmc.loom.util.gradle.ProgressLoggerHelper;
|
import net.fabricmc.loom.util.gradle.ProgressLoggerHelper;
|
||||||
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
||||||
import net.fabricmc.mappingio.tree.MemoryMappingTree;
|
import net.fabricmc.mappingio.tree.MemoryMappingTree;
|
||||||
import net.fabricmc.stitch.util.StitchUtil;
|
|
||||||
|
|
||||||
public class SourceRemapper {
|
public class SourceRemapper {
|
||||||
private final Project project;
|
private final Project project;
|
||||||
|
@ -126,7 +124,7 @@ public class SourceRemapper {
|
||||||
// create tmp directory
|
// create tmp directory
|
||||||
isSrcTmp = true;
|
isSrcTmp = true;
|
||||||
srcPath = Files.createTempDirectory("fabric-loom-src");
|
srcPath = Files.createTempDirectory("fabric-loom-src");
|
||||||
ZipUtil.unpack(source, srcPath.toFile());
|
ZipUtils.unpackAll(source.toPath(), srcPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!destination.isDirectory() && destination.exists()) {
|
if (!destination.isDirectory() && destination.exists()) {
|
||||||
|
@ -135,7 +133,7 @@ public class SourceRemapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StitchUtil.FileSystemDelegate dstFs = destination.isDirectory() ? null : StitchUtil.getJarFileSystem(destination, true);
|
FileSystemUtil.Delegate dstFs = destination.isDirectory() ? null : FileSystemUtil.getJarFileSystem(destination, true);
|
||||||
Path dstPath = dstFs != null ? dstFs.get().getPath("/") : destination.toPath();
|
Path dstPath = dstFs != null ? dstFs.get().getPath("/") : destination.toPath();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,230 @@
|
||||||
|
/*
|
||||||
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 FabricMC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.fabricmc.loom.util;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.NoSuchFileException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import net.fabricmc.loom.LoomGradlePlugin;
|
||||||
|
|
||||||
|
public class ZipUtils {
|
||||||
|
public static boolean contains(Path zip, String path) {
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, false)) {
|
||||||
|
Path fsPath = fs.get().getPath(path);
|
||||||
|
|
||||||
|
return Files.exists(fsPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException("Failed to check file from zip", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unpackAll(Path zip, Path output) throws IOException {
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, false);
|
||||||
|
Stream<Path> walk = Files.walk(fs.get().getPath("/"))) {
|
||||||
|
Iterator<Path> iterator = walk.iterator();
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Path fsPath = iterator.next();
|
||||||
|
if (!Files.isRegularFile(fsPath)) continue;
|
||||||
|
Path dstPath = output.resolve(fs.get().getPath("/").relativize(fsPath).toString());
|
||||||
|
Path dstPathParent = dstPath.getParent();
|
||||||
|
if (dstPathParent != null) Files.createDirectories(dstPathParent);
|
||||||
|
Files.copy(fsPath, dstPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte @Nullable [] unpackNullable(Path zip, String path) throws IOException {
|
||||||
|
try {
|
||||||
|
return unpack(zip, path);
|
||||||
|
} catch (NoSuchFileException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] unpack(Path zip, String path) throws IOException {
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, false)) {
|
||||||
|
Path fsPath = fs.get().getPath(path);
|
||||||
|
|
||||||
|
if (Files.exists(fsPath)) {
|
||||||
|
return Files.readAllBytes(fsPath);
|
||||||
|
} else {
|
||||||
|
throw new NoSuchFileException(fsPath.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void pack(Path from, Path zip) throws IOException {
|
||||||
|
Files.deleteIfExists(zip);
|
||||||
|
|
||||||
|
if (!Files.isDirectory(from)) throw new IllegalArgumentException(from + " is not a directory!");
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, true);
|
||||||
|
Stream<Path> walk = Files.walk(from)) {
|
||||||
|
Iterator<Path> iterator = walk.iterator();
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Path fromPath = iterator.next();
|
||||||
|
if (!Files.isRegularFile(fromPath)) continue;
|
||||||
|
Path fsPath = fs.get().getPath(from.relativize(fromPath).toString());
|
||||||
|
Path fsPathParent = fsPath.getParent();
|
||||||
|
if (fsPathParent != null) Files.createDirectories(fsPathParent);
|
||||||
|
Files.copy(fromPath, fsPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count == 0) {
|
||||||
|
throw new IOException("Noting packed into %s from %s".formatted(zip, from));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void add(Path zip, String path, byte[] bytes) throws IOException {
|
||||||
|
add(zip, Collections.singleton(new Pair<>(path, bytes)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void add(Path zip, Iterable<Pair<String, byte[]>> files) throws IOException {
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, true)) {
|
||||||
|
for (Pair<String, byte[]> pair : files) {
|
||||||
|
Path fsPath = fs.get().getPath(pair.left());
|
||||||
|
Path fsPathParent = fsPath.getParent();
|
||||||
|
if (fsPathParent != null) Files.createDirectories(fsPathParent);
|
||||||
|
Files.write(fsPath, pair.right(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replace(Path zip, String path, byte[] bytes) throws IOException {
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, true)) {
|
||||||
|
Path fsPath = fs.get().getPath(path);
|
||||||
|
|
||||||
|
if (Files.exists(fsPath)) {
|
||||||
|
Files.write(fsPath, bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
|
} else {
|
||||||
|
throw new NoSuchFileException(fsPath.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transformString(Path zip, Collection<Pair<String, UnsafeUnaryOperator<String>>> transforms) throws IOException {
|
||||||
|
return transformString(zip, transforms.stream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transformString(Path zip, Stream<Pair<String, UnsafeUnaryOperator<String>>> transforms) throws IOException {
|
||||||
|
return transformString(zip, collectTransformersStream(transforms));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transformString(Path zip, Map<String, UnsafeUnaryOperator<String>> transforms) throws IOException {
|
||||||
|
return transformMapped(zip, transforms, bytes -> new String(bytes, StandardCharsets.UTF_8), s -> s.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> int transformJson(Class<T> typeOfT, Path zip, Collection<Pair<String, UnsafeUnaryOperator<T>>> transforms) throws IOException {
|
||||||
|
return transformJson(typeOfT, zip, transforms.stream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> int transformJson(Class<T> typeOfT, Path zip, Stream<Pair<String, UnsafeUnaryOperator<T>>> transforms) throws IOException {
|
||||||
|
return transformJson(typeOfT, zip, collectTransformersStream(transforms));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> int transformJson(Class<T> typeOfT, Path zip, Map<String, UnsafeUnaryOperator<T>> transforms) throws IOException {
|
||||||
|
return transformMapped(zip, transforms, bytes -> LoomGradlePlugin.GSON.fromJson(new InputStreamReader(new ByteArrayInputStream(bytes)), typeOfT),
|
||||||
|
s -> LoomGradlePlugin.GSON.toJson(s, typeOfT).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transform(Path zip, Collection<Pair<String, UnsafeUnaryOperator<byte[]>>> transforms) throws IOException {
|
||||||
|
return transform(zip, transforms.stream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transform(Path zip, Stream<Pair<String, UnsafeUnaryOperator<byte[]>>> transforms) throws IOException {
|
||||||
|
return transform(zip, collectTransformersStream(transforms));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> int transformMapped(Path zip, Map<String, UnsafeUnaryOperator<T>> transforms, Function<byte[], T> deserializer, Function<T, byte[]> serializer) throws IOException {
|
||||||
|
Map<String, UnsafeUnaryOperator<byte[]>> newTransforms = new HashMap<>();
|
||||||
|
|
||||||
|
for (Map.Entry<String, UnsafeUnaryOperator<T>> entry : transforms.entrySet()) {
|
||||||
|
if (entry.getValue() != null) {
|
||||||
|
newTransforms.put(entry.getKey(), bytes -> {
|
||||||
|
return serializer.apply(entry.getValue().apply(deserializer.apply(bytes)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return transform(zip, newTransforms);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int transform(Path zip, Map<String, UnsafeUnaryOperator<byte[]>> transforms) throws IOException {
|
||||||
|
int replacedCount = 0;
|
||||||
|
|
||||||
|
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(zip, false)) {
|
||||||
|
for (Map.Entry<String, UnsafeUnaryOperator<byte[]>> entry : transforms.entrySet()) {
|
||||||
|
Path fsPath = fs.get().getPath(entry.getKey());
|
||||||
|
|
||||||
|
if (Files.exists(fsPath) && entry.getValue() != null) {
|
||||||
|
Files.write(fsPath, entry.getValue().apply(Files.readAllBytes(fsPath)), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
|
replacedCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return replacedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface UnsafeUnaryOperator<T> {
|
||||||
|
T apply(T arg) throws IOException;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Map<String, UnsafeUnaryOperator<T>> collectTransformersStream(Stream<Pair<String, UnsafeUnaryOperator<T>>> transforms) {
|
||||||
|
Map<String, UnsafeUnaryOperator<T>> map = new HashMap<>();
|
||||||
|
Iterator<Pair<String, UnsafeUnaryOperator<T>>> iterator = transforms.iterator();
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Pair<String, UnsafeUnaryOperator<T>> next = iterator.next();
|
||||||
|
map.put(next.left(), next.right());
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,7 +25,7 @@
|
||||||
package net.fabricmc.loom.test.integration
|
package net.fabricmc.loom.test.integration
|
||||||
|
|
||||||
import net.fabricmc.loom.test.util.GradleProjectTestTrait
|
import net.fabricmc.loom.test.util.GradleProjectTestTrait
|
||||||
import org.zeroturnaround.zip.ZipUtil
|
import net.fabricmc.loom.util.ZipUtils
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
import spock.lang.Unroll
|
import spock.lang.Unroll
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ class AccessWidenerTest extends Specification implements GradleProjectTestTrait
|
||||||
def "transitive accesswidener (gradle #version)"() {
|
def "transitive accesswidener (gradle #version)"() {
|
||||||
setup:
|
setup:
|
||||||
def gradle = gradleProject(project: "transitiveAccesswidener", version: version)
|
def gradle = gradleProject(project: "transitiveAccesswidener", version: version)
|
||||||
ZipUtil.pack(new File(gradle.projectDir, "dummyDependency"), new File(gradle.projectDir, "dummy.jar"))
|
ZipUtils.pack(new File(gradle.projectDir, "dummyDependency").toPath(), new File(gradle.projectDir, "dummy.jar").toPath())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def result = gradle.run(task: "build")
|
def result = gradle.run(task: "build")
|
||||||
|
|
|
@ -25,8 +25,7 @@
|
||||||
package net.fabricmc.loom.test.integration
|
package net.fabricmc.loom.test.integration
|
||||||
|
|
||||||
import net.fabricmc.loom.test.util.GradleProjectTestTrait
|
import net.fabricmc.loom.test.util.GradleProjectTestTrait
|
||||||
|
import net.fabricmc.loom.util.ZipUtils
|
||||||
import org.zeroturnaround.zip.ZipUtil
|
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
|
@ -66,6 +65,6 @@ class UnpickTest extends Specification implements GradleProjectTestTrait {
|
||||||
|
|
||||||
private static String getClassSource(GradleProject gradle, String classname, String mappings = MAPPINGS) {
|
private static String getClassSource(GradleProject gradle, String classname, String mappings = MAPPINGS) {
|
||||||
File sourcesJar = gradle.getGeneratedSources(mappings)
|
File sourcesJar = gradle.getGeneratedSources(mappings)
|
||||||
return new String(ZipUtil.unpackEntry(sourcesJar, classname), StandardCharsets.UTF_8)
|
return new String(ZipUtils.unpack(sourcesJar.toPath(), classname), StandardCharsets.UTF_8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 FabricMC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.fabricmc.loom.test.unit
|
||||||
|
|
||||||
|
import net.fabricmc.loom.util.Pair
|
||||||
|
import net.fabricmc.loom.util.ZipUtils
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
import java.nio.file.Files
|
||||||
|
|
||||||
|
class ZipUtilsTest extends Specification {
|
||||||
|
def "pack"() {
|
||||||
|
given:
|
||||||
|
def dir = File.createTempDir()
|
||||||
|
def zip = File.createTempFile("loom-zip-test", ".zip").toPath()
|
||||||
|
new File(dir, "test.txt").text = "This is a test of packing"
|
||||||
|
|
||||||
|
when:
|
||||||
|
ZipUtils.pack(dir.toPath(), zip)
|
||||||
|
|
||||||
|
then:
|
||||||
|
Files.exists(zip)
|
||||||
|
ZipUtils.contains(zip, "test.txt")
|
||||||
|
!ZipUtils.contains(zip, "nope.txt")
|
||||||
|
new String( ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This is a test of packing"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "transform string"() {
|
||||||
|
given:
|
||||||
|
def dir = File.createTempDir()
|
||||||
|
def zip = File.createTempFile("loom-zip-test", ".zip").toPath()
|
||||||
|
new File(dir, "test.txt").text = "This is a test of transforming"
|
||||||
|
|
||||||
|
when:
|
||||||
|
ZipUtils.pack(dir.toPath(), zip)
|
||||||
|
def transformed = ZipUtils.transformString(zip, [
|
||||||
|
new Pair<String, ZipUtils.UnsafeUnaryOperator<String>>("test.txt", new ZipUtils.UnsafeUnaryOperator<String>() {
|
||||||
|
@Override
|
||||||
|
String apply(String arg) throws IOException {
|
||||||
|
return arg.toUpperCase()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
])
|
||||||
|
|
||||||
|
then:
|
||||||
|
transformed == 1
|
||||||
|
ZipUtils.contains(zip, "test.txt")
|
||||||
|
new String( ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "THIS IS A TEST OF TRANSFORMING"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "replace string"() {
|
||||||
|
given:
|
||||||
|
def dir = File.createTempDir()
|
||||||
|
def zip = File.createTempFile("loom-zip-test", ".zip").toPath()
|
||||||
|
new File(dir, "test.txt").text = "This has not been replaced"
|
||||||
|
|
||||||
|
when:
|
||||||
|
ZipUtils.pack(dir.toPath(), zip)
|
||||||
|
ZipUtils.replace(zip, "test.txt", "This has been replaced".bytes)
|
||||||
|
|
||||||
|
then:
|
||||||
|
ZipUtils.contains(zip, "test.txt")
|
||||||
|
new String(ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This has been replaced"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "add file"() {
|
||||||
|
given:
|
||||||
|
def dir = File.createTempDir()
|
||||||
|
def zip = File.createTempFile("loom-zip-test", ".zip").toPath()
|
||||||
|
new File(dir, "test.txt").text = "This is original"
|
||||||
|
|
||||||
|
when:
|
||||||
|
ZipUtils.pack(dir.toPath(), zip)
|
||||||
|
ZipUtils.add(zip, "test2.txt", "This has been added".bytes)
|
||||||
|
|
||||||
|
then:
|
||||||
|
ZipUtils.contains(zip, "test.txt")
|
||||||
|
ZipUtils.contains(zip, "test2.txt")
|
||||||
|
new String(ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This is original"
|
||||||
|
new String(ZipUtils.unpack(zip, "test2.txt"), StandardCharsets.UTF_8) == "This has been added"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "unpack all"() {
|
||||||
|
given:
|
||||||
|
def input = File.createTempDir()
|
||||||
|
def output = File.createTempDir()
|
||||||
|
|
||||||
|
def zip = File.createTempFile("loom-zip-test", ".zip").toPath()
|
||||||
|
new File(input, "test.txt").text = "This is a test of unpacking all"
|
||||||
|
|
||||||
|
def outputFile = new File(output, "test.txt")
|
||||||
|
|
||||||
|
when:
|
||||||
|
ZipUtils.pack(input.toPath(), zip)
|
||||||
|
ZipUtils.unpackAll(zip, output.toPath())
|
||||||
|
|
||||||
|
then:
|
||||||
|
outputFile.exists()
|
||||||
|
outputFile.text == "This is a test of unpacking all"
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,9 +26,9 @@ package net.fabricmc.loom.test.util
|
||||||
|
|
||||||
import groovy.transform.Immutable
|
import groovy.transform.Immutable
|
||||||
import net.fabricmc.loom.test.LoomTestConstants
|
import net.fabricmc.loom.test.LoomTestConstants
|
||||||
|
import net.fabricmc.loom.util.ZipUtils
|
||||||
import org.gradle.testkit.runner.BuildResult
|
import org.gradle.testkit.runner.BuildResult
|
||||||
import org.gradle.testkit.runner.GradleRunner
|
import org.gradle.testkit.runner.GradleRunner
|
||||||
import org.zeroturnaround.zip.ZipUtil
|
|
||||||
import spock.lang.Shared
|
import spock.lang.Shared
|
||||||
|
|
||||||
trait GradleProjectTestTrait {
|
trait GradleProjectTestTrait {
|
||||||
|
@ -192,7 +192,7 @@ trait GradleProjectTestTrait {
|
||||||
|
|
||||||
String getOutputZipEntry(String filename, String entryName) {
|
String getOutputZipEntry(String filename, String entryName) {
|
||||||
def file = getOutputFile(filename)
|
def file = getOutputFile(filename)
|
||||||
def bytes = ZipUtil.unpackEntry(file, entryName)
|
def bytes = ZipUtils.unpackNullable(file.toPath(), entryName)
|
||||||
|
|
||||||
if (bytes == null) {
|
if (bytes == null) {
|
||||||
throw new FileNotFoundException("Could not find ${entryName} in ${entryName}")
|
throw new FileNotFoundException("Could not find ${entryName} in ${entryName}")
|
||||||
|
@ -203,7 +203,7 @@ trait GradleProjectTestTrait {
|
||||||
|
|
||||||
boolean hasOutputZipEntry(String filename, String entryName) {
|
boolean hasOutputZipEntry(String filename, String entryName) {
|
||||||
def file = getOutputFile(filename)
|
def file = getOutputFile(filename)
|
||||||
return ZipUtil.unpackEntry(file, entryName) != null
|
return ZipUtils.unpackNullable(file.toPath(), entryName) != null
|
||||||
}
|
}
|
||||||
|
|
||||||
File getGeneratedSources(String mappings) {
|
File getGeneratedSources(String mappings) {
|
||||||
|
|
Loading…
Reference in New Issue