Skip to content

Commit

Permalink
fix signed jar handling
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolMineman committed Mar 19, 2022
1 parent b923976 commit b0fcfb8
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 76 deletions.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>brachyura</artifactId>
<version>0.72</version>
<version>0.73</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.coolcrabs.brachyura.compiler.java;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -55,7 +54,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept

@Override
public InputStream openInputStream() {
return new ByteArrayInputStream(bytes.buf(), 0, bytes.size());
return bytes.toIs();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.coolcrabs.brachyura.fabric;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
Expand All @@ -16,7 +15,6 @@
import java.security.MessageDigest;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -56,8 +54,10 @@
import io.github.coolcrabs.brachyura.mappings.Namespaces;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.Jsr2JetbrainsMappingProvider;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.MappingTreeMappingProvider;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.MetaInfFixer;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.RemapperProcessor;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.TinyRemapperHelper;
import io.github.coolcrabs.brachyura.mappings.tinyremapper.TrWrapper;
import io.github.coolcrabs.brachyura.maven.Maven;
import io.github.coolcrabs.brachyura.maven.MavenId;
import io.github.coolcrabs.brachyura.minecraft.Minecraft;
Expand Down Expand Up @@ -371,14 +371,16 @@ public JavaJarDependency build() {
}
}
ProcessingSponge trout = new ProcessingSponge();
new ProcessorChain(
new RemapperProcessor(TinyRemapper.newRemapper().withMappings(new MappingTreeMappingProvider(compmappings, Namespaces.NAMED, Namespaces.INTERMEDIARY)), getCompileDependencies())
).apply(trout, compilationOutput);
try (AtomicZipProcessingSink out = new AtomicZipProcessingSink(getBuildJarPath())) {
resourcesProcessingChain().apply(out, new DirectoryProcessingSource(getResourcesDir()));
trout.getInputs(out);
out.commit();
}
try (TrWrapper trw = new TrWrapper(TinyRemapper.newRemapper().withMappings(new MappingTreeMappingProvider(compmappings, Namespaces.NAMED, Namespaces.INTERMEDIARY)))) {
new ProcessorChain(
new RemapperProcessor(trw, getCompileDependencies())
).apply(trout, compilationOutput);
try (AtomicZipProcessingSink out = new AtomicZipProcessingSink(getBuildJarPath())) {
resourcesProcessingChain().apply(out, new DirectoryProcessingSource(getResourcesDir()));
trout.getInputs(out);
out.commit();
}
}
return new JavaJarDependency(getBuildJarPath(), null, getId());
} catch (Exception e) {
throw Util.sneak(e);
Expand Down Expand Up @@ -683,7 +685,7 @@ class RemapInfo {
List<RemapInfo> remapinfo = new ArrayList<>(unmapped.size());
List<ModDependency> remapped = new ArrayList<>(unmapped.size());
MessageDigest dephasher = MessageDigestUtil.messageDigest(MessageDigestUtil.SHA256);
dephasher.update((byte) 7); // Bump this if the logic changes
dephasher.update((byte) 8); // Bump this if the logic changes
for (ModDependency dep : unmapped) {
hashDep(dephasher, dep);
}
Expand Down Expand Up @@ -737,15 +739,18 @@ class RemapInfo {
c.put(s, ri.source.jarDependency.mavenId);
}
Logger.info("Remapping {} mods", b.size());
new ProcessorChain(
new RemapperProcessor(tr, cp),
JijRemover.INSTANCE,
new AccessWidenerRemapper(mappings.get(), mappings.get().getNamespaceId(Namespaces.NAMED)),
new FmjGenerator(c)
).apply(
(in, id) -> b.get(id.source).sink(in, id),
b.keySet()
);
try (TrWrapper trw = new TrWrapper(tr)) {
new ProcessorChain(
new RemapperProcessor(trw, cp),
new MetaInfFixer(trw),
JijRemover.INSTANCE,
new AccessWidenerRemapper(mappings.get(), mappings.get().getNamespaceId(Namespaces.NAMED)),
new FmjGenerator(c)
).apply(
(in, id) -> b.get(id.source).sink(in, id),
b.keySet()
);
}
}
FindReplaceSourceRemapper sourceRemapper = new FindReplaceSourceRemapper(mappings.get(), mappings.get().getNamespaceId(Namespaces.INTERMEDIARY), mappings.get().getNamespaceId(Namespaces.NAMED));
for (ModDependency u : unmapped) {
Expand Down Expand Up @@ -899,9 +904,10 @@ public void remapJar(MappingTree mappings, @Nullable Consumer<AccessWidenerVisit
}
try (
ZipProcessingSource source = new ZipProcessingSource(inputJar);
ZipProcessingSink sink = new ZipProcessingSink(outputJar)
ZipProcessingSink sink = new ZipProcessingSink(outputJar);
TrWrapper trw = new TrWrapper(remapperBuilder);
) {
new ProcessorChain(new RemapperProcessor(remapperBuilder, classpath)).apply(sink, source);
new ProcessorChain(new RemapperProcessor(trw, classpath)).apply(sink, source);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.github.coolcrabs.brachyura.mappings.tinyremapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Iterator;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import io.github.coolcrabs.brachyura.processing.ProcessingEntry;
import io.github.coolcrabs.brachyura.processing.ProcessingSink;
import io.github.coolcrabs.brachyura.processing.Processor;
import io.github.coolcrabs.brachyura.util.ByteArrayOutputStreamEx;
import net.fabricmc.tinyremapper.TinyRemapper;

// https://github.com/FabricMC/tiny-remapper/blob/master/src/main/java/net/fabricmc/tinyremapper/MetaInfFixer.java
// Rewritten since tr's is heavily nio tied atm
public class MetaInfFixer implements Processor {
final TinyRemapper remapper;

public MetaInfFixer(TrWrapper remapper) {
this.remapper = remapper.tr;
}

@Override
public void process(Collection<ProcessingEntry> inputs, ProcessingSink sink) throws IOException {
for (ProcessingEntry e : inputs) {
if (e.id.path.startsWith("META-INF/")) {
int si = e.id.path.lastIndexOf('/');
String fn = si == -1 ? e.id.path : e.id.path.substring(si + 1);
if (e.id.path.equals("META-INF/MANIFEST.MF")) {
Manifest m;
try (InputStream i = e.in.get()) {
m = new Manifest(i);
}
fixManifest(m, remapper);
ByteArrayOutputStreamEx ex = new ByteArrayOutputStreamEx();
m.write(ex);
sink.sink(ex::toIs, e.id);
} else if (e.id.path.startsWith("META-INF/services/")) {
ByteArrayOutputStreamEx ex = new ByteArrayOutputStreamEx();
try (
BufferedReader r = new BufferedReader(new InputStreamReader(e.in.get()));
Writer w = new OutputStreamWriter(ex);
) {
fixServiceDecl(r, w, remapper);
}
sink.sink(ex::toIs, e.id);
} else if (fn.endsWith(".SF") || fn.endsWith(".DSA") || fn.endsWith(".RSA") || fn.startsWith("SIG-")) {
// Strip (noop)
} else {
sink.sink(e.in, e.id);
}
} else {
sink.sink(e.in, e.id);
}
}
}

private static String mapFullyQualifiedClassName(String name, TinyRemapper tr) {
return tr.getEnvironment().getRemapper().map(name.replace('.', '/')).replace('/', '.');
}

private static void fixManifest(Manifest manifest, TinyRemapper remapper) {
Attributes mainAttrs = manifest.getMainAttributes();
if (remapper != null) {
String val = mainAttrs.getValue(Attributes.Name.MAIN_CLASS);
if (val != null)
mainAttrs.put(Attributes.Name.MAIN_CLASS, mapFullyQualifiedClassName(val, remapper));
val = mainAttrs.getValue("Launcher-Agent-Class");
if (val != null)
mainAttrs.putValue("Launcher-Agent-Class", mapFullyQualifiedClassName(val, remapper));
}
mainAttrs.remove(Attributes.Name.SIGNATURE_VERSION);
for (Iterator<Attributes> it = manifest.getEntries().values().iterator(); it.hasNext();) {
Attributes attrs = it.next();
for (Iterator<Object> it2 = attrs.keySet().iterator(); it2.hasNext();) {
Attributes.Name attrName = (Attributes.Name) it2.next();
String name = attrName.toString();
if (name.endsWith("-Digest") || name.contains("-Digest-") || name.equals("Magic")) {
it2.remove();
}
}
if (attrs.isEmpty()) it.remove();
}
}

private static void fixServiceDecl(BufferedReader reader, Writer writer, TinyRemapper remapper) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
int end = line.indexOf('#');
if (end < 0) end = line.length();
// trim start+end to skip ' ' and '\t'
int start = 0;
char c;
while (start < end && ((c = line.charAt(start)) == ' ' || c == '\t')) {
start++;
}
while (end > start && ((c = line.charAt(end - 1)) == ' ' || c == '\t')) {
end--;
}
if (start == end) {
writer.write(line);
} else {
writer.write(line, 0, start);
writer.write(mapFullyQualifiedClassName(line.substring(start, end), remapper));
writer.write(line, end, line.length() - end);
}
writer.write('\n');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,36 @@
import io.github.coolcrabs.brachyura.util.StreamUtil;
import net.fabricmc.tinyremapper.InputTag;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyRemapper.Builder;

//TODO update when tr finally doesn't require paths for sources
public class RemapperProcessor implements Processor {
TinyRemapper.Builder builder;
List<Path> classpath;
final TinyRemapper remapper;
final List<Path> classpath;

public RemapperProcessor(Builder builder, List<Path> classpath) {
this.builder = builder;
public RemapperProcessor(TrWrapper tr, List<Path> classpath) {
this.remapper = tr.tr;
this.classpath = classpath;
}

@Override
public void process(Collection<ProcessingEntry> inputs, ProcessingSink sink) throws IOException {
BruhFileSystemProvider bruh = new BruhFileSystemProvider();
TinyRemapper remapper = builder.build();
try {
for (Path j : classpath) {
TinyRemapperHelper.readJar(remapper, j, JarType.CLASSPATH);
for (Path j : classpath) {
TinyRemapperHelper.readJar(remapper, j, JarType.CLASSPATH);
}
HashMap<ProcessingSource, InputTag> tags = new HashMap<>();
for (ProcessingEntry e : inputs) {
tags.computeIfAbsent(e.id.source, k -> remapper.createInputTag());
}
for (ProcessingEntry e : inputs) {
if (e.id.path.endsWith(".class")) {
remapper.readInputs(tags.get(e.id.source), bruh.child.createPath(e));
} else {
sink.sink(e.in, e.id);
}
HashMap<ProcessingSource, InputTag> tags = new HashMap<>();
for (ProcessingEntry e : inputs) {
tags.computeIfAbsent(e.id.source, k -> remapper.createInputTag());
}
for (ProcessingEntry e : inputs) {
if (e.id.path.endsWith(".class")) {
remapper.readInputs(tags.get(e.id.source), bruh.child.createPath(e));
} else {
sink.sink(e.in, e.id);
}
}
for (Map.Entry<ProcessingSource, InputTag> entry : tags.entrySet()) {
remapper.apply((path, bytes) -> sink.sink(() -> new ByteArrayInputStream(bytes), new ProcessingId(path + ".class", entry.getKey())), entry.getValue());
}
} finally {
remapper.finish(); // Epic java 6 style https://github.com/FabricMC/tiny-remapper/pull/71
}
for (Map.Entry<ProcessingSource, InputTag> entry : tags.entrySet()) {
remapper.apply((path, bytes) -> sink.sink(() -> new ByteArrayInputStream(bytes), new ProcessingId(path + ".class", entry.getKey())), entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,4 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
tr.readInputs(tag, inputs.toArray(new Path[inputs.size()]));
}
}

public static void copyNonClassfilesFromFileSystem(FileSystem input, FileSystem output) throws IOException {
copyNonClassfilesFromDir(input.getPath("/"), output);
}

public static void copyNonClassfilesFromDir(Path dir, FileSystem output) throws IOException {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.toString().endsWith(".class")) {
Path target = output.getPath("/").resolve(dir.relativize(file).toString());
Files.createDirectories(target.getParent());
Files.copy(file, target);
}
return FileVisitResult.CONTINUE;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.coolcrabs.brachyura.mappings.tinyremapper;

import net.fabricmc.tinyremapper.TinyRemapper;

public class TrWrapper implements AutoCloseable {
public final TinyRemapper tr;

public TrWrapper(TinyRemapper.Builder b) {
tr = b.build();
}

@Override
public void close() {
tr.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static Supplier<InputStream> stubPom(MavenId id) {
w.newline();
w.writeEndElement();
w.newline();
return () -> new ByteArrayInputStream(o.buf(), 0, o.size());
return o::toIs;
}
} catch (Exception e) {
throw Util.sneak(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.coolcrabs.brachyura.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
* Allows to avoid copying
Expand All @@ -9,4 +11,8 @@ public class ByteArrayOutputStreamEx extends ByteArrayOutputStream {
public byte[] buf() {
return this.buf;
}

public InputStream toIs() {
return new ByteArrayInputStream(buf, 0, size());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.coolcrabs.brachyura.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
Expand All @@ -22,6 +21,6 @@ public static InputStream toIs(JsonElement e, Gson g) {
// Shouldn't be possible
throw Util.sneak(e0);
}
return new ByteArrayInputStream(os.buf(), 0, os.size());
return os.toIs();
}
}
Loading

0 comments on commit b0fcfb8

Please sign in to comment.