Skip to content

Commit

Permalink
Use System Property for Mapping-IO
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Oct 9, 2023
1 parent 4681411 commit 75c6da4
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
Expand Down Expand Up @@ -38,15 +37,12 @@ public void run(String... args) throws Exception {
Enigma enigma = Enigma.create();

System.out.println("Reading JAR...");

EnigmaProject project = enigma.openJar(fileJarIn, new ClasspathClassProvider(), ProgressListener.none());

System.out.println("Reading mappings...");

MappingFormat format = chooseEnigmaFormat(fileMappings);
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();

EntryTree<EntryMapping> mappings = format.read(fileMappings, ProgressListener.none(), saveParameters);
EntryTree<EntryMapping> mappings = readMappings(fileMappings, ProgressListener.none(), saveParameters);
project.setMappings(mappings);

JarIndex idx = project.getJarIndex();
Expand Down
26 changes: 18 additions & 8 deletions enigma-cli/src/main/java/cuchaz/enigma/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package cuchaz.enigma.command;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.google.common.io.MoreFiles;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitableMappingTree;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
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.tree.EntryTree;

Expand Down Expand Up @@ -41,22 +47,26 @@ protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings) th
System.out.println("Reading mappings...");

MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
EntryTree<EntryMapping> mappings = chooseEnigmaFormat(fileMappings).read(fileMappings, progress, saveParameters);
EntryTree<EntryMapping> mappings = readMappings(fileMappings, progress, saveParameters);

project.setMappings(mappings);
}

return project;
}

protected static MappingFormat chooseEnigmaFormat(Path path) {
if (Files.isDirectory(path)) {
return MappingFormat.ENIGMA_DIRECTORY;
} else if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) {
return MappingFormat.ENIGMA_ZIP;
} else {
return MappingFormat.ENIGMA_FILE;
protected static EntryTree<EntryMapping> readMappings(Path path, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException {
// Legacy
if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) {
return MappingFormat.ENIGMA_ZIP.read(path, progress, saveParameters);
}

net.fabricmc.mappingio.format.MappingFormat format = MappingReader.detectFormat(path);
if (format == null) throw new IllegalArgumentException("Unknown mapping format!");

VisitableMappingTree tree = new MemoryMappingTree();
MappingReader.read(path, format, tree);
return MappingIoConverter.fromMappingIo(tree, progress);
}

protected static File getWritableFile(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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.enigma.EnigmaMappingsReader;
import cuchaz.enigma.translation.mapping.serde.enigma.EnigmaMappingsWriter;
import cuchaz.enigma.translation.mapping.serde.tiny.TinyMappingsReader;
import cuchaz.enigma.translation.mapping.serde.tiny.TinyMappingsWriter;
import cuchaz.enigma.translation.mapping.serde.tinyv2.TinyV2Writer;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
Expand All @@ -22,11 +23,11 @@ private MappingCommandsUtil() {

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

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

MappingFormat format = null;
Expand All @@ -40,15 +41,15 @@ public static EntryTree<EntryMapping> read(String type, Path path, MappingSavePa
}

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

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

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

Expand All @@ -59,7 +60,13 @@ public static void write(EntryTree<EntryMapping> mappings, String type, Path pat
throw new IllegalArgumentException("specify column names as 'tinyv2:from_namespace:to_namespace'");
}

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

VisitableMappingTree tree = MappingIoConverter.toMappingIo(mappings, ProgressListener.none(), split[1], split[2]);
tree.accept(MappingWriter.create(path, net.fabricmc.mappingio.format.MappingFormat.TINY_2_FILE));
return;
}

Expand All @@ -70,7 +77,13 @@ public static void write(EntryTree<EntryMapping> mappings, String type, Path pat
throw new IllegalArgumentException("specify column names as 'tiny:from_column:to_column'");
}

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

VisitableMappingTree tree = MappingIoConverter.toMappingIo(mappings, ProgressListener.none(), split[1], split[2]);
tree.accept(MappingWriter.create(path, net.fabricmc.mappingio.format.MappingFormat.TINY_FILE));
return;
}

Expand All @@ -83,7 +96,7 @@ public static void write(EntryTree<EntryMapping> mappings, String type, Path pat
}

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

Expand Down
48 changes: 12 additions & 36 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
import javax.swing.SwingUtilities;

import com.google.common.collect.Lists;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitOrder;
import net.fabricmc.mappingio.tree.VisitableMappingTree;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProfile;
Expand Down Expand Up @@ -82,7 +77,6 @@
import cuchaz.enigma.translation.mapping.MappingDelta;
import cuchaz.enigma.translation.mapping.ResolutionStrategy;
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.tree.EntryTree;
Expand All @@ -105,7 +99,6 @@ public class GuiController implements ClientPacketHandler {

private Path loadedMappingPath;
private MappingFormat loadedMappingFormat;
public boolean useMappingIo = true;

private ClassHandleProvider chp;

Expand Down Expand Up @@ -146,6 +139,11 @@ public void closeJar() {
this.gui.onCloseJar();
}

public CompletableFuture<Void> openMappings(MappingFormat format, Path path, boolean useMappingIo) {
System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false");
return openMappings(format, path);
}

public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
if (project == null) {
return CompletableFuture.completedFuture(null);
Expand All @@ -156,26 +154,7 @@ public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
return ProgressDialog.runOffThread(gui.getFrame(), progress -> {
try {
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
EntryTree<EntryMapping> mappings;

if (useMappingIo) {
String loadingMessage;

if (format.getMappingIoCounterpart().hasSingleFile()) {
loadingMessage = I18n.translate("progress.mappings.loading_file");
} else {
loadingMessage = I18n.translate("progress.mappings.loading_directory");
}

progress.init(1, loadingMessage);
VisitableMappingTree mappingTree = new MemoryMappingTree();
MappingReader.read(path, format.getMappingIoCounterpart(), mappingTree);
mappings = MappingIoConverter.fromMappingIo(mappingTree, progress);
} else {
mappings = format.read(path, progress, saveParameters);
}

project.setMappings(mappings);
project.setMappings(format.read(path, progress, saveParameters));

loadedMappingFormat = format;
loadedMappingPath = path;
Expand Down Expand Up @@ -203,6 +182,11 @@ public CompletableFuture<Void> saveMappings(Path path) {
return saveMappings(path, loadedMappingFormat);
}

public CompletableFuture<Void> saveMappings(Path path, MappingFormat format, boolean useMappingIo) {
System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false");
return saveMappings(path, format);
}

/**
* Saves the mappings, with a dialog popping up, showing the progress.
*
Expand All @@ -229,15 +213,7 @@ public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) {
loadedMappingFormat = format;
loadedMappingPath = path;

if (useMappingIo) {
VisitableMappingTree mappingTree = MappingIoConverter.toMappingIo(mapper.getObfToDeobf(), progress);

progress.init(1, I18n.translate("progress.mappings.writing"));
MappingWriter writer = MappingWriter.create(path, format.getMappingIoCounterpart());
mappingTree.accept(writer, VisitOrder.createByName());
writer.close();
progress.step(1, I18n.translate("progress.done"));
} else if (saveAll) {
if (saveAll) {
format.write(mapper.getObfToDeobf(), path, progress, saveParameters);
} else {
format.write(mapper.getObfToDeobf(), delta, path, progress, saveParameters);
Expand Down
42 changes: 10 additions & 32 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -404,22 +403,11 @@ private void onGithubClicked() {
}

private static void prepareOpenMappingsMenu(JMenu openMappingsMenu, Gui gui) {
List<MappingFormat> readableMappingIoFormats = Arrays.asList(
MappingFormat.ENIGMA_DIRECTORY,
MappingFormat.ENIGMA_FILE,
MappingFormat.TINY_FILE,
MappingFormat.TINY_V2,
MappingFormat.SRG_FILE,
MappingFormat.TSRG_FILE,
MappingFormat.TSRG_2_FILE,
MappingFormat.PROGUARD);

// Mapping-IO readers
for (MappingFormat format : readableMappingIoFormats) {
addOpenMappingsMenuEntry(I18n.translate(format.getMappingIoCounterpart().name),
format, true, openMappingsMenu, gui);
}

MappingFormat.getReadableFormats().stream()
.filter(format -> format.getMappingIoCounterpart() != null)
.forEach(format -> addOpenMappingsMenuEntry(I18n.translate(format.getMappingIoCounterpart().name),
format, true, openMappingsMenu, gui));
openMappingsMenu.addSeparator();

// Enigma's own readers
Expand All @@ -438,28 +426,19 @@ private static void addOpenMappingsMenuEntry(String text, MappingFormat format,

if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile();
gui.getController().useMappingIo = mappingIo;
gui.getController().openMappings(format, selectedFile.toPath());
gui.getController().openMappings(format, selectedFile.toPath(), mappingIo);
UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
}
});
openMappingsMenu.add(item);
}

private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) {
List<MappingFormat> writableMappingIoFormats = Arrays.asList(
MappingFormat.ENIGMA_DIRECTORY,
MappingFormat.ENIGMA_FILE,
MappingFormat.TINY_FILE,
MappingFormat.TINY_V2,
MappingFormat.PROGUARD);

// Mapping-IO writers
for (MappingFormat format : writableMappingIoFormats) {
addSaveMappingsAsMenuEntry(format.getMappingIoCounterpart().name,
format, true, saveMappingsAsMenu, saveMappingsItem, gui);
}

MappingFormat.getWritableFormats().stream()
.filter(format -> format.hasMappingIoWriter())
.forEach(format -> addSaveMappingsAsMenuEntry(format.getMappingIoCounterpart().name,
format, true, saveMappingsAsMenu, saveMappingsItem, gui));
saveMappingsAsMenu.addSeparator();

// Enigma's own writers
Expand All @@ -480,8 +459,7 @@ private static void addSaveMappingsAsMenuEntry(String text, MappingFormat format
}

if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
gui.getController().useMappingIo = mappingIo;
gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format);
gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format, mappingIo);
saveMappingsItem.setEnabled(true);
UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
}
Expand Down
Loading

0 comments on commit 75c6da4

Please sign in to comment.