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

use tiny-mappings-parser library #3

Open
wants to merge 1 commit into
base: master
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
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
}

sourceCompatibility = 1.8
version = '0.1.0'
version = '0.1.1'

def ENV = System.getenv()
if (ENV.BUILD_NUMBER) {
Expand All @@ -24,9 +24,14 @@ archivesBaseName = 'tiny-remapper'

repositories {
mavenCentral()
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
}

dependencies {
compile 'net.fabricmc:tiny-mappings-parser:0.1.0.6'
compile 'org.ow2.asm:asm:7.0'
compile 'org.ow2.asm:asm-commons:7.0'
compile 'org.ow2.asm:asm-tree:7.0'
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/net/fabricmc/tinyremapper/MappingProviderUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2016, 2018 Player, asie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.fabricmc.tinyremapper;

import net.fabricmc.mappings.*;

public final class MappingProviderUtils {
private MappingProviderUtils() {

}

private static String entryToValueString(EntryTriple triple) {
return triple.getOwner() + "/" + triple.getName();
}

private static String fieldToString(EntryTriple triple) {
return triple.getOwner() + "/" + triple.getName() + ";;" + triple.getDesc();
}

private static String methodToString(EntryTriple triple) {
return triple.getOwner() + "/" + triple.getName() + triple.getDesc();
}

public static IMappingProvider create(Mappings mappings, String from, String to) {
return (classMap, fieldMap, methodMap) -> {
for (ClassEntry entry : mappings.getClassEntries()) {
classMap.put(entry.get(from), entry.get(to));
}

for (FieldEntry entry : mappings.getFieldEntries()) {
fieldMap.put(fieldToString(entry.get(from)), entryToValueString(entry.get(to)));
}

for (MethodEntry entry : mappings.getMethodEntries()) {
methodMap.put(methodToString(entry.get(from)), entryToValueString(entry.get(to)));
}
};
}
}
149 changes: 17 additions & 132 deletions src/main/java/net/fabricmc/tinyremapper/TinyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,158 +25,43 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;

import net.fabricmc.mappings.*;
import org.objectweb.asm.commons.Remapper;

public final class TinyUtils {
public static class Mapping {
public final String owner, name, desc;

public Mapping(String owner, String name, String desc) {
this.owner = owner;
this.name = name;
this.desc = desc;
}

@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Mapping)) {
return false;
} else {
Mapping otherM = (Mapping) other;
return owner.equals(otherM.owner) && name.equals(otherM.name) && desc.equals(otherM.desc);
}
}

@Override
public int hashCode() {
return Objects.hash(owner, name, desc);
}
}

private static class SimpleClassMapper extends Remapper {
final Map<String, String> classMap;

public SimpleClassMapper(Map<String, String> map) {
this.classMap = map;
}

@Override
public String map(String typeName) {
return classMap.getOrDefault(typeName, typeName);
}
private interface InputStreamConsumer {
void apply(InputStream stream) throws IOException;
}

private TinyUtils() {

}

public static IMappingProvider createTinyMappingProvider(final Path mappings, String fromM, String toM) {
return (classMap, fieldMap, methodMap) -> {
try (BufferedReader reader = getMappingReader(mappings.toFile())) {
readInternal(reader, fromM, toM, classMap, fieldMap, methodMap);
} catch (IOException e) {
throw new RuntimeException(e);
}

System.out.printf("%s: %d classes, %d methods, %d fields%n", mappings.getFileName().toString(), classMap.size(), methodMap.size(), fieldMap.size());
};
}

private static BufferedReader getMappingReader(File file) throws IOException {
InputStream is = new FileInputStream(file);

if (file.getName().endsWith(".gz")) {
is = new GZIPInputStream(is);
}

return new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
}

public static IMappingProvider createTinyMappingProvider(final BufferedReader reader, String fromM, String toM) {
static IMappingProvider createTinyMappingProvider(final Path mappingsPath, String fromM, String toM) {
return (classMap, fieldMap, methodMap) -> {
try {
readInternal(reader, fromM, toM, classMap, fieldMap, methodMap);
withMapping(mappingsPath, (stream) -> {
Mappings mappings = MappingsProvider.readTinyMappings(stream);
MappingProviderUtils.create(mappings, fromM, toM).load(classMap, fieldMap, methodMap);
});
} catch (IOException e) {
throw new RuntimeException(e);
}

System.out.printf("%d classes, %d methods, %d fields%n", classMap.size(), methodMap.size(), fieldMap.size());
System.out.printf("%s: %d classes, %d methods, %d fields%n", mappingsPath.toFile().getName(), classMap.size(), methodMap.size(), fieldMap.size());
};
}

private static void readInternal(BufferedReader reader, String fromM, String toM, Map<String, String> classMap, Map<String, String> fieldMap, Map<String, String> methodMap) throws IOException {
TinyUtils.read(reader, fromM, toM, (classFrom, classTo) -> {
classMap.put(classFrom, classTo);
}, (fieldFrom, fieldTo) -> {
fieldMap.put(fieldFrom.owner + "/" + fieldFrom.name + ";;" + fieldFrom.desc, fieldTo.owner + "/" + fieldTo.name);
}, (methodFrom, methodTo) -> {
methodMap.put(methodFrom.owner + "/" + methodFrom.name + methodFrom.desc, methodTo.owner + "/" + methodTo.name);
});
}

public static void read(BufferedReader reader, String from, String to,
BiConsumer<String, String> classMappingConsumer,
BiConsumer<Mapping, Mapping> fieldMappingConsumer,
BiConsumer<Mapping, Mapping> methodMappingConsumer)
throws IOException {
String[] header = reader.readLine().split("\t");
if (header.length <= 1
|| !header[0].equals("v1")) {
throw new IOException("Invalid mapping version!");
}

List<String> headerList = Arrays.asList(header);
int fromIndex = headerList.indexOf(from) - 1;
int toIndex = headerList.indexOf(to) - 1;

if (fromIndex < 0) throw new IOException("Could not find mapping '" + from + "'!");
if (toIndex < 0) throw new IOException("Could not find mapping '" + to + "'!");

Map<String, String> obfFrom = new HashMap<>();
Map<String, String> obfTo = new HashMap<>();
List<String[]> linesStageTwo = new ArrayList<>();

String line;
while ((line = reader.readLine()) != null) {
String[] splitLine = line.split("\t");
if (splitLine.length >= 2) {
if ("CLASS".equals(splitLine[0])) {
classMappingConsumer.accept(splitLine[1 + fromIndex], splitLine[1 + toIndex]);
obfFrom.put(splitLine[1], splitLine[1 + fromIndex]);
obfTo.put(splitLine[1], splitLine[1 + toIndex]);
} else {
linesStageTwo.add(splitLine);
}
}
}

SimpleClassMapper descObfFrom = new SimpleClassMapper(obfFrom);
SimpleClassMapper descObfTo = new SimpleClassMapper(obfTo);

for (String[] splitLine : linesStageTwo) {
if ("FIELD".equals(splitLine[0])) {
String owner = obfFrom.getOrDefault(splitLine[1], splitLine[1]);
String desc = descObfFrom.mapDesc(splitLine[2]);
String tOwner = obfTo.getOrDefault(splitLine[1], splitLine[1]);
String tDesc = descObfTo.mapDesc(splitLine[2]);
fieldMappingConsumer.accept(
new Mapping(owner, splitLine[3 + fromIndex], desc),
new Mapping(tOwner, splitLine[3 + toIndex], tDesc)
);
} else if ("METHOD".equals(splitLine[0])) {
String owner = obfFrom.getOrDefault(splitLine[1], splitLine[1]);
String desc = descObfFrom.mapMethodDesc(splitLine[2]);
String tOwner = obfTo.getOrDefault(splitLine[1], splitLine[1]);
String tDesc = descObfTo.mapMethodDesc(splitLine[2]);
methodMappingConsumer.accept(
new Mapping(owner, splitLine[3 + fromIndex], desc),
new Mapping(tOwner, splitLine[3 + toIndex], tDesc)
);
}
private static void withMapping(Path path, InputStreamConsumer consumer) throws IOException {
File file = path.toFile();
try (
InputStream is = new FileInputStream(file);
InputStream targetIs = file.getName().endsWith(".gz") ? new GZIPInputStream(is) : is
) {
consumer.apply(targetIs);
}
}
}