This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d5223b
commit de05d10
Showing
6 changed files
with
167 additions
and
39 deletions.
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
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
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
67 changes: 67 additions & 0 deletions
67
src/main/java/com/pocolifo/jarremapper/extensions/ExtensionUtility.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,67 @@ | ||
package com.pocolifo.jarremapper.extensions; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.*; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
import com.pocolifo.jarremapper.Utility; | ||
|
||
public class ExtensionUtility { | ||
private static Map<String, String> getEnvironment() { | ||
Map<String, String> env = new HashMap<>(); | ||
env.put("create", "true"); | ||
return env; | ||
} | ||
|
||
private static FileSystem getFileSystem(File targetFile) throws IOException { | ||
URI uri = URI.create("jar:" + targetFile.toURI()); | ||
return FileSystems.newFileSystem(uri, getEnvironment()); | ||
} | ||
|
||
public static void copyResources(File inputFile, File outputFile) throws IOException { | ||
try (FileSystem fileSystem = getFileSystem(outputFile)) { | ||
try (ZipInputStream stream = new ZipInputStream(new FileInputStream(inputFile))) { | ||
for (ZipEntry entry; (entry = stream.getNextEntry()) != null;) { | ||
if (entry.getName().endsWith(".class")) continue; | ||
|
||
Path path = fileSystem.getPath(entry.getName()); | ||
|
||
if (path.getParent() != null) { | ||
Files.createDirectories(path.getParent()); | ||
} | ||
|
||
Files.write(path, Utility.readInputStream(stream)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void recursivelyRemovePath(Path path) throws IOException { | ||
if (!Files.isDirectory(path)) return; | ||
|
||
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException { | ||
return Files.deleteIfExists(path) ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException { | ||
return Files.deleteIfExists(path) ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE; | ||
} | ||
}); | ||
} | ||
|
||
public static void removeMetaInf(File file) throws IOException { | ||
try (FileSystem fileSystem = getFileSystem(file)) { | ||
recursivelyRemovePath(fileSystem.getPath("META-INF")); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/pocolifo/jarremapper/extensions/SpecialSourceEngine.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,63 @@ | ||
package com.pocolifo.jarremapper.extensions; | ||
|
||
import java.io.IOException; | ||
|
||
import com.pocolifo.jarremapper.engine.AbstractRemappingEngine; | ||
import com.pocolifo.jarremapper.mapping.ClassMapping; | ||
import com.pocolifo.jarremapper.mapping.FieldMapping; | ||
import com.pocolifo.jarremapper.mapping.MethodMapping; | ||
import net.md_5.specialsource.Jar; | ||
import net.md_5.specialsource.JarMapping; | ||
import net.md_5.specialsource.JarRemapper; | ||
import net.md_5.specialsource.provider.JarProvider; | ||
import net.md_5.specialsource.provider.JointProvider; | ||
|
||
public class SpecialSourceEngine extends AbstractRemappingEngine { | ||
private boolean excludeMetaInf; | ||
|
||
public SpecialSourceEngine excludeMetaInf() { | ||
this.excludeMetaInf = !this.excludeMetaInf; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void remap() throws IOException { | ||
JarMapping jarMapping = this.convertJarMapping(); | ||
Jar jar = Jar.init(this.inputFile); | ||
|
||
JointProvider inheritanceProviders = new JointProvider(); | ||
inheritanceProviders.add(new JarProvider(jar)); | ||
jarMapping.setFallbackInheritanceProvider(inheritanceProviders); | ||
|
||
JarRemapper remapper = new JarRemapper(jarMapping); | ||
remapper.remapJar(jar, this.outputFile); | ||
|
||
ExtensionUtility.copyResources(this.inputFile, this.outputFile); | ||
if (this.excludeMetaInf) ExtensionUtility.removeMetaInf(this.outputFile); | ||
} | ||
|
||
public JarMapping convertJarMapping() { | ||
JarMapping jarMapping = new JarMapping(); | ||
|
||
for (ClassMapping classMapping : this.mapping.classMappings) { | ||
jarMapping.classes.put(classMapping.fromClassName, classMapping.toClassName); | ||
|
||
for (FieldMapping fieldMapping : classMapping.fieldMappings) { | ||
jarMapping.fields.put(String.format("%s/%s", classMapping.fromClassName, | ||
fieldMapping.fromFieldName), fieldMapping.toFieldName); | ||
} | ||
|
||
for (MethodMapping methodMapping : classMapping.methodMappings) { | ||
jarMapping.methods.put( | ||
String.format("%s/%s %s", | ||
classMapping.fromClassName, | ||
methodMapping.fromMethodName, | ||
methodMapping.fromMethodDescriptor), | ||
methodMapping.toMethodName); | ||
} | ||
|
||
} | ||
|
||
return jarMapping; | ||
} | ||
} |
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