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

Access widener loom support #182

Merged
merged 20 commits into from
Apr 6, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.gradle.api.Project;

import net.fabricmc.loom.util.accesswidener.AccessWidenerJarProcessor;
import net.fabricmc.loom.LoomGradleExtension;

public class JarProcessorManager {
Expand All @@ -49,6 +50,10 @@ public JarProcessorManager(Project project) {
private List<JarProcessor> setupProcessors() {
List<JarProcessor> jarProcessors = new ArrayList<>();

if (extension.accessWidener != null) {
jarProcessors.add(new AccessWidenerJarProcessor());
}

jarProcessors.forEach(jarProcessor -> jarProcessor.setup(project));
return Collections.unmodifiableList(jarProcessors);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/fabricmc/loom/task/RemapJarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.fabricmc.loom.util.MixinRefmapHelper;
import net.fabricmc.loom.util.NestedJars;
import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
import net.fabricmc.loom.util.accesswidener.AccessWidenerJarProcessor;
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyUtils;
Expand Down Expand Up @@ -128,6 +129,10 @@ public void doTask() throws Throwable {
}
}

if (extension.accessWidener != null) {
extension.getJarProcessorManager().getByType(AccessWidenerJarProcessor.class).remapAccessWidener(output);
}

/*try {
if (modJar.exists()) {
Files.move(modJar, modJarUnmappedCopy);
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/net/fabricmc/loom/util/ModProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@

package net.fabricmc.loom.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -47,12 +50,15 @@
import org.gradle.api.artifacts.ResolvedArtifact;
import org.zeroturnaround.zip.ZipUtil;
import org.zeroturnaround.zip.commons.FileUtils;
import org.zeroturnaround.zip.transform.ByteArrayZipEntryTransformer;
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.providers.MinecraftMappedProvider;
import net.fabricmc.loom.util.accesswidener.AccessWidener;
import net.fabricmc.loom.util.accesswidener.AccessWidenerRemapper;
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;

Expand All @@ -71,6 +77,8 @@ public static void processMod(File input, File output, Project project, Configur
handleNestedJars(input, project, config, artifact);
}

remapaccessWidener(input, project);

//Always strip the nested jars
stripNestedJars(output);
}
Expand Down Expand Up @@ -140,6 +148,57 @@ protected String transform(ZipEntry zipEntry, String input) throws IOException {
}))});
}

private static void remapaccessWidener(File input, Project project) throws IOException {
JarFile jarFile = new JarFile(input);
JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json");

if (modJsonEntry == null) {
return;
}

String accessWidenerPath;

try (InputStream inputStream = jarFile.getInputStream(modJsonEntry)) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream), JsonObject.class);

if (!json.has("accessWidener")) {
return;
}

accessWidenerPath = json.get("accessWidener").getAsString();
}

if (accessWidenerPath == null) {
return;
}

ZipUtil.transformEntry(input, accessWidenerPath, new ByteArrayZipEntryTransformer() {
@Override
protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
return remapaccessWidener(input, project);
}
});
}

private static byte[] remapaccessWidener(byte[] input, Project project) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(input)))) {
AccessWidener accessWidener = new AccessWidener();
accessWidener.read(bufferedReader);

AccessWidenerRemapper accessWidenerRemapper = new AccessWidenerRemapper(accessWidener, project.getExtensions().getByType(LoomGradleExtension.class).getMappingsProvider().getMappings(), "named");
AccessWidener remapped = accessWidenerRemapper.remap();

StringWriter writer = new StringWriter();
remapped.write(writer);
byte[] bytes = writer.toString().getBytes();
writer.close();

return bytes;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void remapJar(File input, File output, Project project, ResolvedArtifact artifact) throws IOException {
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
String fromM = "intermediary";
Expand Down
Loading