From 346bca399070c0c47baebe5bd78de841a5fc33df Mon Sep 17 00:00:00 2001 From: asie Date: Thu, 24 Jan 2019 14:05:40 +0100 Subject: [PATCH] use tiny-mappings-parser library --- build.gradle | 7 +- .../tinyremapper/MappingProviderUtils.java | 54 +++++++ .../net/fabricmc/tinyremapper/TinyUtils.java | 149 ++---------------- 3 files changed, 77 insertions(+), 133 deletions(-) create mode 100644 src/main/java/net/fabricmc/tinyremapper/MappingProviderUtils.java diff --git a/build.gradle b/build.gradle index 5f8db0cd..2014dae1 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { } sourceCompatibility = 1.8 -version = '0.1.0' +version = '0.1.1' def ENV = System.getenv() if (ENV.BUILD_NUMBER) { @@ -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' diff --git a/src/main/java/net/fabricmc/tinyremapper/MappingProviderUtils.java b/src/main/java/net/fabricmc/tinyremapper/MappingProviderUtils.java new file mode 100644 index 00000000..5789096b --- /dev/null +++ b/src/main/java/net/fabricmc/tinyremapper/MappingProviderUtils.java @@ -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 . + */ + +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))); + } + }; + } +} diff --git a/src/main/java/net/fabricmc/tinyremapper/TinyUtils.java b/src/main/java/net/fabricmc/tinyremapper/TinyUtils.java index c1855120..809d0dbd 100644 --- a/src/main/java/net/fabricmc/tinyremapper/TinyUtils.java +++ b/src/main/java/net/fabricmc/tinyremapper/TinyUtils.java @@ -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 classMap; - - public SimpleClassMapper(Map 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 classMap, Map fieldMap, Map 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 classMappingConsumer, - BiConsumer fieldMappingConsumer, - BiConsumer methodMappingConsumer) - throws IOException { - String[] header = reader.readLine().split("\t"); - if (header.length <= 1 - || !header[0].equals("v1")) { - throw new IOException("Invalid mapping version!"); - } - - List 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 obfFrom = new HashMap<>(); - Map obfTo = new HashMap<>(); - List 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); } } }