Skip to content

Commit

Permalink
Support for reading/writing single Enigma files (#19)
Browse files Browse the repository at this point in the history
* Add support for reading single Enigma files

* Add support for writing single Enigma files

* Create a subpackage for each mapping format

* Add JetBrains annotations; move versions to gradle.properties

* Properly configure .editorconfig

* Consistently apply `FILE` and `DIR` naming for mapping formats

* Make Jetbrains Annotations `compileOnly`
  • Loading branch information
NebelNidas authored Jun 15, 2023
1 parent 74d481a commit 6a06f2a
Show file tree
Hide file tree
Showing 22 changed files with 603 additions and 440 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[*.{gradle,java}]
indent_style = tab
ij_continuation_indent_size = 8
ij_java_imports_layout = $*,|,java.**,|,javax.**,|,*,|,net.fabricmc.**
ij_java_class_count_to_use_import_on_demand = 999

[*.{yml,yaml}]
indent_style = space
indent_size = 2
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ group "net.fabricmc"
archivesBaseName = "mapping-io"

def ENV = System.getenv()
version = "0.4.2"

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
Expand All @@ -26,7 +25,7 @@ tasks.withType(JavaCompile).configureEach {

checkstyle {
configFile = project.file("checkstyle.xml")
toolVersion = "8.31"
toolVersion = project.checkstyle_tool_version
}

repositories {
Expand All @@ -38,8 +37,9 @@ repositories {
}

dependencies {
implementation "org.ow2.asm:asm:9.5"
implementation "net.fabricmc:tiny-remapper:0.8.0"
implementation "org.ow2.asm:asm:${project.asm_version}"
compileOnly "org.jetbrains:annotations:${project.jetbrains_annotations_version}"
implementation "net.fabricmc:tiny-remapper:${tiny_remapper_version}"
}

spotless {
Expand Down
11 changes: 11 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Gradle Properties
org.gradle.jvmargs = -Xmx2G

# Project properties
version = 0.4.2

# Dependencies
asm_version = 9.5
tiny_remapper_version = 0.8.0
jetbrains_annotations_version = 24.0.1
checkstyle_tool_version = 8.31
72 changes: 39 additions & 33 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
import java.util.Arrays;
import java.util.List;

import net.fabricmc.mappingio.format.EnigmaReader;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.ProGuardReader;
import net.fabricmc.mappingio.format.SrgReader;
import net.fabricmc.mappingio.format.Tiny1Reader;
import net.fabricmc.mappingio.format.Tiny2Reader;
import net.fabricmc.mappingio.format.TsrgReader;
import net.fabricmc.mappingio.format.enigma.EnigmaDirReader;
import net.fabricmc.mappingio.format.enigma.EnigmaFileReader;
import net.fabricmc.mappingio.format.proguard.ProGuardFileReader;
import net.fabricmc.mappingio.format.srg.SrgFileReader;
import net.fabricmc.mappingio.format.tiny.Tiny1FileReader;
import net.fabricmc.mappingio.format.tiny.Tiny2FileReader;
import net.fabricmc.mappingio.format.tsrg.TsrgFileReader;

public final class MappingReader {
public static MappingFormat detectFormat(Path file) throws IOException {
if (Files.isDirectory(file)) {
return MappingFormat.ENIGMA;
return MappingFormat.ENIGMA_DIR;
} else {
try (Reader reader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) {
return detectFormat(reader);
Expand All @@ -59,24 +60,26 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {

switch (String.valueOf(buffer, 0, 3)) {
case "v1\t":
return MappingFormat.TINY;
return MappingFormat.TINY_FILE;
case "tin":
return MappingFormat.TINY_2;
return MappingFormat.TINY_2_FILE;
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
return MappingFormat.TSRG2;
return MappingFormat.TSRG_2_FILE;
case "CLA":
return MappingFormat.ENIGMA_FILE;
case "PK:":
case "CL:":
case "MD:":
case "FD:":
return MappingFormat.SRG;
return MappingFormat.SRG_FILE;
}

String headerStr = String.valueOf(buffer, 0, pos);

if (headerStr.contains(" -> ")) {
return MappingFormat.PROGUARD;
return MappingFormat.PROGUARD_FILE;
} else if (headerStr.contains("\n\t")) {
return MappingFormat.TSRG;
return MappingFormat.TSRG_FILE;
}

return null; // unknown format or corrupted
Expand Down Expand Up @@ -118,12 +121,12 @@ public static List<String> getNamespaces(Reader reader, MappingFormat format) th
checkReaderCompatible(format);

switch (format) {
case TINY:
return Tiny1Reader.getNamespaces(reader);
case TINY_2:
return Tiny2Reader.getNamespaces(reader);
case TSRG2:
return TsrgReader.getNamespaces(reader);
case TINY_FILE:
return Tiny1FileReader.getNamespaces(reader);
case TINY_2_FILE:
return Tiny2FileReader.getNamespaces(reader);
case TSRG_2_FILE:
return TsrgFileReader.getNamespaces(reader);
default:
throw new IllegalStateException();
}
Expand All @@ -148,10 +151,10 @@ public static void read(Path file, MappingFormat format, MappingVisitor visitor)
}
} else {
switch (format) {
case ENIGMA:
EnigmaReader.read(file, visitor);
case ENIGMA_DIR:
EnigmaDirReader.read(file, visitor);
break;
case MCP:
case MCP_DIR:
throw new UnsupportedOperationException(); // TODO: implement
default:
throw new IllegalStateException();
Expand All @@ -175,21 +178,24 @@ public static void read(Reader reader, MappingFormat format, MappingVisitor visi
checkReaderCompatible(format);

switch (format) {
case TINY:
Tiny1Reader.read(reader, visitor);
case TINY_FILE:
Tiny1FileReader.read(reader, visitor);
break;
case TINY_2:
Tiny2Reader.read(reader, visitor);
case TINY_2_FILE:
Tiny2FileReader.read(reader, visitor);
break;
case SRG:
SrgReader.read(reader, visitor);
case ENIGMA_FILE:
EnigmaFileReader.read(reader, visitor);
break;
case TSRG:
case TSRG2:
TsrgReader.read(reader, visitor);
case SRG_FILE:
SrgFileReader.read(reader, visitor);
break;
case PROGUARD:
ProGuardReader.read(reader, visitor);
case TSRG_FILE:
case TSRG_2_FILE:
TsrgFileReader.read(reader, visitor);
break;
case PROGUARD_FILE:
ProGuardFileReader.read(reader, visitor);
break;
default:
throw new IllegalStateException();
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/net/fabricmc/mappingio/MappingWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
import java.nio.file.Files;
import java.nio.file.Path;

import net.fabricmc.mappingio.format.EnigmaWriter;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.ProGuardWriter;
import net.fabricmc.mappingio.format.Tiny1Writer;
import net.fabricmc.mappingio.format.Tiny2Writer;
import net.fabricmc.mappingio.format.enigma.EnigmaDirWriter;
import net.fabricmc.mappingio.format.enigma.EnigmaFileWriter;
import net.fabricmc.mappingio.format.proguard.ProGuardFileWriter;
import net.fabricmc.mappingio.format.tiny.Tiny1FileWriter;
import net.fabricmc.mappingio.format.tiny.Tiny2FileWriter;

public interface MappingWriter extends Closeable, MappingVisitor {
static MappingWriter create(Path file, MappingFormat format) throws IOException {
if (format.hasSingleFile()) {
return create(Files.newBufferedWriter(file), format);
} else {
switch (format) {
case ENIGMA: return new EnigmaWriter(file, true);
case ENIGMA_DIR: return new EnigmaDirWriter(file, true);
default: throw new UnsupportedOperationException("format "+format+" is not implemented");
}
}
Expand All @@ -44,9 +45,10 @@ static MappingWriter create(Writer writer, MappingFormat format) throws IOExcept
if (!format.hasSingleFile()) throw new IllegalArgumentException("format "+format+" is not applicable to a single writer");

switch (format) {
case TINY: return new Tiny1Writer(writer);
case TINY_2: return new Tiny2Writer(writer, false);
case PROGUARD: return new ProGuardWriter(writer);
case TINY_FILE: return new Tiny1FileWriter(writer);
case TINY_2_FILE: return new Tiny2FileWriter(writer, false);
case ENIGMA_FILE: return new EnigmaFileWriter(writer);
case PROGUARD_FILE: return new ProGuardFileWriter(writer);
default: throw new UnsupportedOperationException("format "+format+" is not implemented");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
import java.io.Reader;
import java.util.Arrays;

final class ColumnFileReader implements Closeable {
ColumnFileReader(Reader reader, char columnSeparator) {
import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.mappingio.format.tiny.Tiny2Util;

@ApiStatus.Internal
public final class ColumnFileReader implements Closeable {
public ColumnFileReader(Reader reader, char columnSeparator) {
this.reader = reader;
this.columnSeparator = columnSeparator;
}
Expand Down
Loading

0 comments on commit 6a06f2a

Please sign in to comment.