Skip to content

Commit

Permalink
Revert back to J8
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Mar 26, 2024
1 parent 84ad678 commit 691918e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/minecraftforge/fart/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

import joptsimple.OptionException;
import joptsimple.OptionParser;
Expand Down Expand Up @@ -167,11 +168,11 @@ private static String[] expandArgs(String[] args) throws IOException {
if (x + 1 == args.length)
throw new IllegalArgumentException("No value specified for '--cfg'");

try (var lines = Files.lines(Paths.get(args[++x]))) {
try (Stream<String> lines = Files.lines(Paths.get(args[++x]))) {
lines.forEach(ret::add);
}
} else if (args[x].startsWith("--cfg=")) {
try (var lines = Files.lines(Paths.get(args[x].substring(6)))) {
try (Stream<String> lines = Files.lines(Paths.get(args[x].substring(6)))) {
lines.forEach(ret::add);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static class ClassInfo implements IClassInfo {
this.name = nameToBytecode(node);
this.access = new Access(node.getModifiers());
this.superName = nameToBytecode(node.getSuperclass());
this.interfaces = Arrays.stream(node.getInterfaces()).map(ClassInfo::nameToBytecode).toList();
this.interfaces = Arrays.stream(node.getInterfaces()).map(ClassInfo::nameToBytecode).collect(Collectors.toList());

Map<String, MethodInfo> mtds = Stream.concat(
Arrays.stream(node.getConstructors()).map(MethodInfo::new),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Handle;
Expand Down Expand Up @@ -71,8 +72,8 @@ public Optional<String> mapJavadocMember(final String owner, final String name,
}

private Optional<MClass.MMethod> findMethod(final String owner, final String name, final int paramCount) {
return getClass(owner).stream()
.flatMap(c -> c.getMethods().stream().flatMap(Optional::stream))
return stream(getClass(owner))
.flatMap(c -> c.getMethods().stream().flatMap(EnhancedRemapper::stream))
.filter(m -> m.getName().equals(name) && Type.getMethodType(m.getDescriptor()).getArgumentTypes().length == paramCount)
.findFirst();
}
Expand Down Expand Up @@ -141,11 +142,15 @@ private IMappingFile getMap() {
private Optional<MClass> computeClass(String cls) {
Optional<? extends IClassInfo> icls = this.getClassProvider().getClass(cls);
IMappingFile.IClass mcls = this.map.getClass(cls);
if (icls.isEmpty() && mcls == null)
if (!icls.isPresent() && mcls == null)
return Optional.empty();
return Optional.of(new MClass(icls.orElse(null), mcls));
}

private static <T> Stream<T> stream(Optional<T> optional) {
return optional.isPresent() ? Stream.of(optional.get()) : Stream.empty();
}

private class MClass {
private final IClassInfo icls;
private final IMappingFile.IClass mcls;
Expand Down Expand Up @@ -183,12 +188,12 @@ private class MClass {

for (MClass parentCls : parents) {
for (Optional<MField> fldOpt : parentCls.getFields()) {
if (fldOpt.isEmpty())
if (!fldOpt.isPresent())
continue;

MField fld = fldOpt.get();
Optional<MField> existing = this.fields.get(fld.getKey());
if (existing == null || existing.isEmpty()) {
if (existing == null || !existing.isPresent()) {
/* There are some weird cases where a field will be referenced as if it were owned by the current class,
* but it needs a field from the parent. So lets follow the linking spec and pull
* down fields from parents.
Expand All @@ -208,7 +213,7 @@ private class MClass {
}

for (Optional<MMethod> mtdOpt : parentCls.getMethods()) {
if (mtdOpt.isEmpty())
if (!mtdOpt.isPresent())
continue;

MMethod mtd = mtdOpt.get();
Expand Down Expand Up @@ -241,7 +246,7 @@ private class MClass {


Optional<MMethod> existingOpt = this.methods.get(mtd.getKey());
if (existingOpt == null || existingOpt.isEmpty()) {
if (existingOpt == null || !existingOpt.isPresent()) {
/* If there is none existing, then we pull in what we have found from the parents.
* This intentionally uses the same object as the parents so that if we have weird edge
* cases, we can migrate the mapping transitively.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private String[] mapParams(String containedClass, String[] params) {
}

private String replaceLinks(String containedClass, Matcher matcher, Function<Matcher, String> prefix) {
StringBuilder sb = new StringBuilder();
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
final String matchedOwner = matcher.group("owner");
final String owner = references.getInternalName((matchedOwner == null || matchedOwner.isEmpty()) ? containedClass : matchedOwner);
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/net/minecraftforge/fart/internal/RenamerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package net.minecraftforge.fart.internal;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -103,7 +105,7 @@ public void run(File input, File output) {
if (e.isDirectory())
continue;
String name = e.getName();
byte[] data = in.getInputStream(e).readAllBytes();
byte[] data = readAllBytes(in.getInputStream(e), e.getSize());

if (name.endsWith(".class"))
oldEntries.add(ClassEntry.create(name, e.getTime(), data));
Expand Down Expand Up @@ -214,6 +216,19 @@ else if (name.equals("javadoctor.json"))
}
}

private byte[] readAllBytes(InputStream in, long size) throws IOException {
// This program w ill crash if size exceeds MAX_INT anyway since arrays are limited to 32-bit indices
ByteArrayOutputStream tmp = new ByteArrayOutputStream(size >= 0 ? (int) size : 0);

byte[] buffer = new byte[8192];
int read;
while ((read = in.read(buffer)) != -1) {
tmp.write(buffer, 0, read);
}

return tmp.toByteArray();
}

// Tho Directory entries are not strictly necessary, we add them because some bad implementations of Zip extractors
// attempt to extract files without making sure the parents exist.
private void addDirectory(ZipOutputStream zos, Set<String> seen, String path) throws IOException {
Expand Down

0 comments on commit 691918e

Please sign in to comment.