summaryrefslogtreecommitdiff
path: root/enigma-cli/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java
blob: 3a00aecbb2dc574568d3ac6ea4b2f07006c3f1ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cuchaz.enigma.command;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;

import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.tree.VisitableMappingTree;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingIoConverter;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.tiny.TinyMappingsWriter;
import cuchaz.enigma.translation.mapping.serde.tinyv2.TinyV2Writer;
import cuchaz.enigma.translation.mapping.tree.EntryTree;

public final class MappingCommandsUtil {
	private MappingCommandsUtil() {
	}

	public static EntryTree<EntryMapping> read(String type, Path path, MappingSaveParameters saveParameters) throws MappingParseException, IOException {
		if (type.equals("enigma")) {
			return (Files.isDirectory(path) ? MappingFormat.ENIGMA_DIRECTORY : MappingFormat.ENIGMA_ZIP).read(path, ProgressListener.none(), saveParameters, null);
		}

		if (type.equals("tiny")) {
			return MappingFormat.TINY_FILE.read(path, ProgressListener.none(), saveParameters, null);
		}

		MappingFormat format = null;

		try {
			format = MappingFormat.valueOf(type.toUpperCase());
		} catch (IllegalArgumentException ignored) {
			if (type.equals("tinyv2")) {
				format = MappingFormat.TINY_V2;
			}
		}

		if (format != null) {
			return format.read(path, ProgressListener.none(), saveParameters, null);
		}

		throw new IllegalArgumentException("no reader for " + type);
	}

	public static void write(EntryTree<EntryMapping> mappings, String type, Path path, MappingSaveParameters saveParameters) {
		if (type.equals("enigma")) {
			MappingFormat.ENIGMA_DIRECTORY.write(mappings, path, ProgressListener.none(), saveParameters);
			return;
		}

		if (type.startsWith("tinyv2:") || type.startsWith("tiny_v2:")) {
			String[] split = type.split(":");

			if (split.length != 3) {
				throw new IllegalArgumentException("specify column names as 'tinyv2:from_namespace:to_namespace'");
			}

			if (!System.getProperty("enigma.use_mappingio", "true").equals("true")) {
				new TinyV2Writer(split[1], split[2]).write(mappings, path, ProgressListener.none(), saveParameters);
				return;
			}
			
			try {
				VisitableMappingTree tree = MappingIoConverter.toMappingIo(mappings, ProgressListener.none(), split[1], split[2]);
				tree.accept(MappingWriter.create(path, net.fabricmc.mappingio.format.MappingFormat.TINY_2_FILE));
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}

			return;
		}

		if (type.startsWith("tiny:")) {
			String[] split = type.split(":");

			if (split.length != 3) {
				throw new IllegalArgumentException("specify column names as 'tiny:from_column:to_column'");
			}

			if (!System.getProperty("enigma.use_mappingio", "true").equals("true")) {
				new TinyMappingsWriter(split[1], split[2]).write(mappings, path, ProgressListener.none(), saveParameters);
				return;
			}

			try {
				VisitableMappingTree tree = MappingIoConverter.toMappingIo(mappings, ProgressListener.none(), split[1], split[2]);
				tree.accept(MappingWriter.create(path, net.fabricmc.mappingio.format.MappingFormat.TINY_FILE));
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}

			return;
		}

		MappingFormat format = null;

		try {
			format = MappingFormat.valueOf(type.toUpperCase());
		} catch (IllegalArgumentException ignored) {
			// ignored
		}

		if (format != null) {
			format.write(mappings, path, ProgressListener.none(), saveParameters);
			return;
		}

		throw new IllegalArgumentException("no writer for " + type);
	}
}