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

Add JarReader #16

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;

import net.fabricmc.mappingio.format.EnigmaReader;
import net.fabricmc.mappingio.format.JarReader;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.ProGuardReader;
import net.fabricmc.mappingio.format.SrgReader;
Expand Down Expand Up @@ -69,6 +70,9 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {
case "MD:":
case "FD:":
return MappingFormat.SRG;
case "PK\u0003": // zip/jar
case "MZ\ufffd": // windows exe
return MappingFormat.JAR;
}

String headerStr = String.valueOf(buffer, 0, pos);
Expand Down Expand Up @@ -141,8 +145,12 @@ public static void read(Path file, MappingFormat format, MappingVisitor visitor)
}

if (format.hasSingleFile()) {
try (Reader reader = Files.newBufferedReader(file)) {
read(reader, format, visitor);
if (format == MappingFormat.JAR) {
JarReader.read(file, visitor);
} else {
try (Reader reader = Files.newBufferedReader(file)) {
read(reader, format, visitor);
}
}
} else {
switch (format) {
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/net/fabricmc/mappingio/format/JarReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import net.fabricmc.mappingio.MappingVisitor;

public class JarReader {
public static void read(Path path, MappingVisitor mappingVisitor) throws IOException {
AnalyzingVisitor analyzingVisitor = new AnalyzingVisitor(mappingVisitor);

ZipFile zipFile = new ZipFile(path.toFile());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably use a jar file system instead,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it's really convenient to pass a ZipEntry to ClassReader, what's the benefit of a jar file system?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For it's more modern, and you can skip invalid directory with its children (like in a DirectoryStream) than having to step into it like when you are iterating with a zip file.

Enumeration<? extends ZipEntry> entries = zipFile.entries();

mappingVisitor.visitNamespaces("jar", new ArrayList<String>());

while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
String parentName = Paths.get("/", entryName).getParent().toString();

if (entryName.endsWith(".class") && !parentName.contains("-")) {
processClass(zipFile.getInputStream(entry), analyzingVisitor);
}
}
}

private static void processClass(InputStream inputStream, AnalyzingVisitor analyzingVisitor) throws IOException {
ClassReader reader = new ClassReader(inputStream);
reader.accept(analyzingVisitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
}

private static final class AnalyzingVisitor extends ClassVisitor {
AnalyzingVisitor(MappingVisitor mappingVisitor) {
super(Integer.getInteger("mappingIo.asmApiVersion", Opcodes.ASM9));

this.mappingVisitor = mappingVisitor;
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
try {
mappingVisitor.visitClass(name);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
try {
mappingVisitor.visitField(name, descriptor);
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
try {
mappingVisitor.visitMethod(name, descriptor);
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

private final MappingVisitor mappingVisitor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public enum MappingFormat {
SRG("SRG", "srg", false, false, false, false, false),
TSRG("TSRG", "tsrg", false, false, false, false, false),
TSRG2("TSRG2", "tsrg", true, false, false, true, false),
PROGUARD("ProGuard", "map", false, true, false, false, false);
PROGUARD("ProGuard", "map", false, true, false, false, false),
JAR("JAR", "jar", false, true, false, true, true);

MappingFormat(String name, String fileExt,
boolean hasNamespaces, boolean hasFieldDescriptors,
Expand Down