From fcae86692edc4dbb3764174a341cc622099f4969 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Fri, 1 Nov 2019 00:52:04 +0100 Subject: Add methods for parsing legacy mappings from string instead of file --- .../replaymod/gradle/remap/legacy/LegacyMapping.kt | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/main/kotlin/com/replaymod/gradle/remap') diff --git a/src/main/kotlin/com/replaymod/gradle/remap/legacy/LegacyMapping.kt b/src/main/kotlin/com/replaymod/gradle/remap/legacy/LegacyMapping.kt index 389b1c9..5530794 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/legacy/LegacyMapping.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/legacy/LegacyMapping.kt @@ -1,8 +1,8 @@ package com.replaymod.gradle.remap.legacy import org.cadixdev.lorenz.MappingSet +import java.io.* -import java.io.IOException import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Path @@ -18,17 +18,29 @@ class LegacyMapping(var oldName: String, var newName: String) { return LegacyMappingsReader(readMappings(mappingFile, invert)).read() } + @Throws(IOException::class) + fun readMappingSet(fileName: String, reader: Reader, invert: Boolean): MappingSet { + return LegacyMappingsReader(readMappings(fileName, reader, invert)).read() + } + @Throws(IOException::class) fun readMappings(mappingFile: Path, invert: Boolean): Map { + Files.newBufferedReader(mappingFile, StandardCharsets.UTF_8).use { + return readMappings(mappingFile.toString(), it, invert) + } + } + + @Throws(IOException::class) + fun readMappings(fileName: String, reader: Reader, invert: Boolean): Map { val mappings = HashMap() val revMappings = HashMap() var lineNumber = 0 - for (line in Files.readAllLines(mappingFile, StandardCharsets.UTF_8)) { + for (line in BufferedReader(reader).lineSequence()) { lineNumber++ if (line.trim { it <= ' ' }.startsWith("#") || line.trim { it <= ' ' }.isEmpty()) continue val parts = line.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - require(!(parts.size < 2 || line.contains(";"))) { "Failed to parse line $lineNumber in $mappingFile." } + require(!(parts.size < 2 || line.contains(";"))) { "Failed to parse line $lineNumber in $fileName." } var mapping: LegacyMapping? = mappings[parts[0]] if (mapping == null) { @@ -73,7 +85,7 @@ class LegacyMapping(var oldName: String, var newName: String) { revMapping.fields[fromName] = toName } } else { - throw IllegalArgumentException("Failed to parse line $lineNumber in $mappingFile.") + throw IllegalArgumentException("Failed to parse line $lineNumber in $fileName.") } } if (invert) { -- cgit