Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
1.2.0: Special Source engine
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungerMax committed Jan 12, 2022
1 parent 3d5223b commit de05d10
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ build/

/*.jar

src/test/java/com/pocolifo/jarremapperextension/tests/*
src/test/java/com/pocolifo/jarremapper/extensions/tests/*
src/test/resources/*
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ Extra features for [JAR Remapper](https://github.com/pocolifo/jar-remapper)
## Features

#### More Remapping Engines
- Tiny Remapper by FabricMC
- [Tiny Remapper](https://github.com/FabricMC/tiny-remapper) by FabricMC
- [Special Source](https://github.com/md-5/SpecialSource) by MD-5


## Mapping Engine Support
| Remapping Engine | Classes | Fields | Methods | Parameters |
|---|---|---|---|---|
| Standard | ✔️ | ✔️ | ✔️ | ✔️ |
| Tiny Remapper | ✔️ | ✔️ | ✔️ |* |
| Special Source | ✔️ | ✔️ | ✔️ ||

*coming soon

## Coming Soon
- Parameter name remapping
- Parameter name remapping for Tiny Remapper

## Getting Started

Expand All @@ -33,6 +43,19 @@ JarRemapper.newRemap()
// ...whatever other options you use for JAR Remapper...
```

#### Special Source Remapping Engine

Append the `withRemappingEngine` option to JAR Remapper.

```java
JarRemapper.newRemap()
.withRemappingEngine(new SpecialSourceEngine()
// (optional)
// Excludes the META-INF directory from output JAR
.excludeMetaInf()
)
// ...whatever other options you use for JAR Remapper...
```

# Develop

Expand Down
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.pocolifo.jarremapper'
version '1.1.1'
version '1.2.0'

repositories {
mavenCentral()
Expand All @@ -22,6 +22,7 @@ repositories {
dependencies {
implementation 'com.pocolifo:jarremapper:2.0.0'
implementation 'net.fabricmc:tiny-remapper:0.7.0'
implementation 'net.md-5:SpecialSource:1.11.0'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
Expand All @@ -46,8 +47,13 @@ jarremapper {
'com.pocolifo.jarremapper.reader.tiny.Tiny2MappingReader/.read(\"official\", \"named\")/fabric+legacyfabric': ['mappings-merged.tiny']]

engines = [
// Tiny remapper
'com.pocolifo.jarremapper.extensions.TinyRemapperEngine/new TinyRemapperEngine().setOptions(net.fabricmc.tinyremapper.TinyRemapper.newRemapper().ignoreConflicts(true))',
'com.pocolifo.jarremapper.extensions.TinyRemapperEngine/new TinyRemapperEngine().setOptions(net.fabricmc.tinyremapper.TinyRemapper.newRemapper().ignoreConflicts(true)).excludeMetaInf()'
'com.pocolifo.jarremapper.extensions.TinyRemapperEngine/new TinyRemapperEngine().setOptions(net.fabricmc.tinyremapper.TinyRemapper.newRemapper().ignoreConflicts(true)).excludeMetaInf()',

// Special source
//'com.pocolifo.jarremapper.extensions.SpecialSourceEngine/new SpecialSourceEngine()',
'com.pocolifo.jarremapper.extensions.SpecialSourceEngine/new SpecialSourceEngine().excludeMetaInf()'
]
}

Expand Down
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"));
}
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package com.pocolifo.jarremapper.extensions;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.pocolifo.jarremapper.Utility;

import com.pocolifo.jarremapper.engine.AbstractRemappingEngine;
import com.pocolifo.jarremapper.mapping.ClassMapping;
import com.pocolifo.jarremapper.mapping.FieldMapping;
Expand Down Expand Up @@ -53,28 +42,8 @@ public void remap() throws IOException {
}

// copy resources
Map<String, String> env = new HashMap<>();
env.put("create", "true");

URI uri = URI.create("jar:" + this.outputFile.toURI());

try (FileSystem fileSystem = FileSystems.newFileSystem(uri, env)) {
try (ZipInputStream stream = new ZipInputStream(new FileInputStream(this.inputFile))) {
for (ZipEntry entry; (entry = stream.getNextEntry()) != null;) {
if (!entry.getName().endsWith(".class") && !entry.isDirectory()) {
if (this.excludeMetaInf && entry.getName().startsWith("META-INF")) continue;

Path path = fileSystem.getPath(entry.getName());

if (path.getParent() != null) {
Files.createDirectories(path.getParent());
}

Files.write(path, Utility.readInputStream(stream));
}
}
}
}
ExtensionUtility.copyResources(this.inputFile, this.outputFile);
if (this.excludeMetaInf) ExtensionUtility.removeMetaInf(this.outputFile);
}

public static IMappingProvider createProvider(JarMapping mapping) {
Expand Down

0 comments on commit de05d10

Please sign in to comment.