-
Notifications
You must be signed in to change notification settings - Fork 20
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
jackassmc
wants to merge
8
commits into
FabricMC:dev
Choose a base branch
from
jackassmc:jar-reader
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add JarReader #16
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0c27a6
Add JarReader
jackassmc 9f8341a
Skip classes in META-INF and doc-files folders
jackassmc 4f0342b
Skip classes in folders with names containing "-"
jackassmc b2e7b0e
Skip jars in doc-files folders
jackassmc 6b52aed
Use ZipFile in JarReader
jackassmc c095c31
Read Windows executables with JarReader
jackassmc f6536fa
Allow dashes in filenames in JarReader
jackassmc 41a6484
Fix parent path
jackassmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/net/fabricmc/mappingio/format/JarReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.