summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java
diff options
context:
space:
mode:
authorGravatar NebelNidas2022-10-06 20:03:14 +0200
committerGravatar NebelNidas2023-10-09 11:54:30 +0200
commit6c7738ca744e55e8e2ea03518a5b93000fe9a864 (patch)
tree6a7a3b15f5c56de1992cf7851655f88e6f45c93a /enigma-swing/src/main/java
parentAdd initial Mapping-IO export support (diff)
downloadenigma-6c7738ca744e55e8e2ea03518a5b93000fe9a864.tar.gz
enigma-6c7738ca744e55e8e2ea03518a5b93000fe9a864.tar.xz
enigma-6c7738ca744e55e8e2ea03518a5b93000fe9a864.zip
Add Mapping-IO import support; small cleanup
Diffstat (limited to 'enigma-swing/src/main/java')
-rw-r--r--enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java61
-rw-r--r--enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java119
2 files changed, 87 insertions, 93 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java
index e7e75669..73472ca9 100644
--- a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java
+++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java
@@ -29,6 +29,7 @@ import javax.swing.JOptionPane;
29import javax.swing.SwingUtilities; 29import javax.swing.SwingUtilities;
30 30
31import com.google.common.collect.Lists; 31import com.google.common.collect.Lists;
32import net.fabricmc.mappingio.MappingReader;
32import net.fabricmc.mappingio.MappingWriter; 33import net.fabricmc.mappingio.MappingWriter;
33import net.fabricmc.mappingio.tree.MemoryMappingTree; 34import net.fabricmc.mappingio.tree.MemoryMappingTree;
34 35
@@ -102,6 +103,7 @@ public class GuiController implements ClientPacketHandler {
102 103
103 private Path loadedMappingPath; 104 private Path loadedMappingPath;
104 private MappingFormat loadedMappingFormat; 105 private MappingFormat loadedMappingFormat;
106 public boolean useMappingIo;
105 107
106 private ClassHandleProvider chp; 108 private ClassHandleProvider chp;
107 109
@@ -152,8 +154,16 @@ public class GuiController implements ClientPacketHandler {
152 return ProgressDialog.runOffThread(gui.getFrame(), progress -> { 154 return ProgressDialog.runOffThread(gui.getFrame(), progress -> {
153 try { 155 try {
154 MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters(); 156 MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
157 EntryTree<EntryMapping> mappings;
158
159 if (useMappingIo) {
160 MemoryMappingTree mappingTree = new MemoryMappingTree();
161 MappingReader.read(path, format.getMappingIoCounterpart(), mappingTree);
162 mappings = MappingIoConverter.fromMappingIo(mappingTree);
163 } else {
164 mappings = format.read(path, progress, saveParameters);
165 }
155 166
156 EntryTree<EntryMapping> mappings = format.read(path, progress, saveParameters);
157 project.setMappings(mappings); 167 project.setMappings(mappings);
158 168
159 loadedMappingFormat = format; 169 loadedMappingFormat = format;
@@ -208,7 +218,12 @@ public class GuiController implements ClientPacketHandler {
208 loadedMappingFormat = format; 218 loadedMappingFormat = format;
209 loadedMappingPath = path; 219 loadedMappingPath = path;
210 220
211 if (saveAll) { 221 if (useMappingIo) {
222 MemoryMappingTree mappingTree = MappingIoConverter.toMappingIo(mapper.getObfToDeobf());
223 MappingWriter writer = MappingWriter.create(path, format.getMappingIoCounterpart());
224 mappingTree.accept(writer);
225 writer.close();
226 } else if (saveAll) {
212 format.write(mapper.getObfToDeobf(), path, progress, saveParameters); 227 format.write(mapper.getObfToDeobf(), path, progress, saveParameters);
213 } else { 228 } else {
214 format.write(mapper.getObfToDeobf(), delta, path, progress, saveParameters); 229 format.write(mapper.getObfToDeobf(), delta, path, progress, saveParameters);
@@ -216,48 +231,6 @@ public class GuiController implements ClientPacketHandler {
216 }); 231 });
217 } 232 }
218 233
219 public CompletableFuture<Void> saveMappings(Path path, net.fabricmc.mappingio.format.MappingFormat format) {
220 if (project == null) {
221 return CompletableFuture.completedFuture(null);
222 }
223
224 return ProgressDialog.runOffThread(this.gui.getFrame(), progress -> {
225 EntryRemapper mapper = project.getMapper();
226 MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
227
228 MappingDelta<EntryMapping> delta = mapper.takeMappingDelta();
229 boolean saveAll = !path.equals(loadedMappingPath);
230
231 switch (format) {
232 case ENIGMA:
233 loadedMappingFormat = MappingFormat.ENIGMA_DIRECTORY;
234 loadedMappingPath = path;
235 break;
236 case PROGUARD:
237 loadedMappingFormat = MappingFormat.PROGUARD;
238 loadedMappingPath = path;
239 break;
240 case SRG:
241 loadedMappingFormat = MappingFormat.SRG_FILE;
242 loadedMappingPath = path;
243 break;
244 case TINY:
245 loadedMappingFormat = MappingFormat.TINY_FILE;
246 loadedMappingPath = path;
247 break;
248 case TINY_2:
249 loadedMappingFormat = MappingFormat.TINY_V2;
250 loadedMappingPath = path;
251 break;
252 }
253
254 MemoryMappingTree mappingTree = MappingIoConverter.toMappingIo(mapper.getObfToDeobf());
255 MappingWriter writer = MappingWriter.create(path, format);
256 mappingTree.accept(writer);
257 writer.close();
258 });
259 }
260
261 public void closeMappings() { 234 public void closeMappings() {
262 if (project == null) { 235 if (project == null) {
263 return; 236 return;
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
index 1d4c1ca1..3692eb3b 100644
--- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
+++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
@@ -46,7 +46,7 @@ public class MenuBar {
46 private final JMenu fileMenu = new JMenu(); 46 private final JMenu fileMenu = new JMenu();
47 private final JMenuItem jarOpenItem = new JMenuItem(); 47 private final JMenuItem jarOpenItem = new JMenuItem();
48 private final JMenuItem jarCloseItem = new JMenuItem(); 48 private final JMenuItem jarCloseItem = new JMenuItem();
49 private final JMenu openMenu = new JMenu(); 49 private final JMenu openMappingsMenu = new JMenu();
50 private final JMenuItem saveMappingsItem = new JMenuItem(); 50 private final JMenuItem saveMappingsItem = new JMenuItem();
51 private final JMenu saveMappingsAsMenu = new JMenu(); 51 private final JMenu saveMappingsAsMenu = new JMenu();
52 private final JMenuItem closeMappingsItem = new JMenuItem(); 52 private final JMenuItem closeMappingsItem = new JMenuItem();
@@ -89,7 +89,7 @@ public class MenuBar {
89 89
90 this.retranslateUi(); 90 this.retranslateUi();
91 91
92 prepareOpenMenu(this.openMenu, gui); 92 prepareOpenMappingsMenu(this.openMappingsMenu, gui);
93 prepareSaveMappingsAsMenu(this.saveMappingsAsMenu, this.saveMappingsItem, gui); 93 prepareSaveMappingsAsMenu(this.saveMappingsAsMenu, this.saveMappingsItem, gui);
94 prepareDecompilerMenu(this.decompilerMenu, gui); 94 prepareDecompilerMenu(this.decompilerMenu, gui);
95 prepareThemesMenu(this.themesMenu, gui); 95 prepareThemesMenu(this.themesMenu, gui);
@@ -99,7 +99,7 @@ public class MenuBar {
99 this.fileMenu.add(this.jarOpenItem); 99 this.fileMenu.add(this.jarOpenItem);
100 this.fileMenu.add(this.jarCloseItem); 100 this.fileMenu.add(this.jarCloseItem);
101 this.fileMenu.addSeparator(); 101 this.fileMenu.addSeparator();
102 this.fileMenu.add(this.openMenu); 102 this.fileMenu.add(this.openMappingsMenu);
103 this.fileMenu.add(this.saveMappingsItem); 103 this.fileMenu.add(this.saveMappingsItem);
104 this.fileMenu.add(this.saveMappingsAsMenu); 104 this.fileMenu.add(this.saveMappingsAsMenu);
105 this.fileMenu.add(this.closeMappingsItem); 105 this.fileMenu.add(this.closeMappingsItem);
@@ -175,7 +175,7 @@ public class MenuBar {
175 this.startServerItem.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop")); 175 this.startServerItem.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop"));
176 176
177 this.jarCloseItem.setEnabled(jarOpen); 177 this.jarCloseItem.setEnabled(jarOpen);
178 this.openMenu.setEnabled(jarOpen); 178 this.openMappingsMenu.setEnabled(jarOpen);
179 this.saveMappingsItem.setEnabled(jarOpen && this.gui.enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED); 179 this.saveMappingsItem.setEnabled(jarOpen && this.gui.enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED);
180 this.saveMappingsAsMenu.setEnabled(jarOpen); 180 this.saveMappingsAsMenu.setEnabled(jarOpen);
181 this.closeMappingsItem.setEnabled(jarOpen); 181 this.closeMappingsItem.setEnabled(jarOpen);
@@ -190,7 +190,7 @@ public class MenuBar {
190 this.fileMenu.setText(I18n.translate("menu.file")); 190 this.fileMenu.setText(I18n.translate("menu.file"));
191 this.jarOpenItem.setText(I18n.translate("menu.file.jar.open")); 191 this.jarOpenItem.setText(I18n.translate("menu.file.jar.open"));
192 this.jarCloseItem.setText(I18n.translate("menu.file.jar.close")); 192 this.jarCloseItem.setText(I18n.translate("menu.file.jar.close"));
193 this.openMenu.setText(I18n.translate("menu.file.mappings.open")); 193 this.openMappingsMenu.setText(I18n.translate("menu.file.mappings.open"));
194 this.saveMappingsItem.setText(I18n.translate("menu.file.mappings.save")); 194 this.saveMappingsItem.setText(I18n.translate("menu.file.mappings.save"));
195 this.saveMappingsAsMenu.setText(I18n.translate("menu.file.mappings.save_as")); 195 this.saveMappingsAsMenu.setText(I18n.translate("menu.file.mappings.save_as"));
196 this.closeMappingsItem.setText(I18n.translate("menu.file.mappings.close")); 196 this.closeMappingsItem.setText(I18n.translate("menu.file.mappings.close"));
@@ -403,67 +403,88 @@ public class MenuBar {
403 GuiUtil.openUrl("https://github.com/FabricMC/Enigma"); 403 GuiUtil.openUrl("https://github.com/FabricMC/Enigma");
404 } 404 }
405 405
406 private static void prepareOpenMenu(JMenu openMenu, Gui gui) { 406 private static void prepareOpenMappingsMenu(JMenu openMappingsMenu, Gui gui) {
407 List<MappingFormat> readableMappingIoFormats = Arrays.asList(
408 MappingFormat.ENIGMA_DIRECTORY,
409 MappingFormat.TINY_FILE,
410 MappingFormat.TINY_V2,
411 MappingFormat.SRG_FILE,
412 MappingFormat.TSRG_FILE,
413 MappingFormat.TSRG_2_FILE,
414 MappingFormat.PROGUARD);
415
416 // Enigma's own readers
407 for (MappingFormat format : MappingFormat.values()) { 417 for (MappingFormat format : MappingFormat.values()) {
408 if (format.getReader() != null) { 418 if (format.getReader() != null) {
409 JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); 419 addOpenMappingsMenuEntry(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT)),
410 item.addActionListener(event -> { 420 format, false, openMappingsMenu, gui);
411 gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir()));
412
413 if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
414 File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile();
415 gui.getController().openMappings(format, selectedFile.toPath());
416 UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
417 }
418 });
419 openMenu.add(item);
420 } 421 }
421 } 422 }
423
424 openMappingsMenu.addSeparator();
425
426 // Mapping-IO readers
427 for (MappingFormat format : readableMappingIoFormats) {
428 addOpenMappingsMenuEntry(I18n.translate(format.getMappingIoCounterpart().name + " (via Mapping-IO, experimental)"),
429 format, true, openMappingsMenu, gui);
430 }
431 }
432
433 private static void addOpenMappingsMenuEntry(String text, MappingFormat format, boolean mappingIo, JMenu openMappingsMenu, Gui gui) {
434 JMenuItem item = new JMenuItem(text);
435 item.addActionListener(event -> {
436 gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir()));
437
438 if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
439 File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile();
440 gui.getController().useMappingIo = mappingIo;
441 gui.getController().openMappings(format, selectedFile.toPath());
442 UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
443 }
444 });
445 openMappingsMenu.add(item);
422 } 446 }
423 447
424 private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) { 448 private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) {
449 List<MappingFormat> writableMappingIoFormats = Arrays.asList(
450 MappingFormat.ENIGMA_DIRECTORY,
451 MappingFormat.TINY_V2);
452
453 // Enigma's own writers
425 for (MappingFormat format : MappingFormat.values()) { 454 for (MappingFormat format : MappingFormat.values()) {
426 if (format.getWriter() != null) { 455 if (format.getWriter() != null) {
427 JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); 456 addSaveMappingsAsMenuEntry(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT)),
428 item.addActionListener(event -> { 457 format, false, saveMappingsAsMenu, saveMappingsItem, gui);
429 // TODO: Use a specific file chooser for it
430 if (gui.enigmaMappingsFileChooser.getCurrentDirectory() == null) {
431 gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir()));
432 }
433
434 if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
435 gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format);
436 saveMappingsItem.setEnabled(true);
437 UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
438 }
439 });
440 saveMappingsAsMenu.add(item);
441 } 458 }
442 } 459 }
443 460
444 saveMappingsAsMenu.addSeparator(); 461 saveMappingsAsMenu.addSeparator();
445 462
446 List<net.fabricmc.mappingio.format.MappingFormat> writableMappingIoFormats = Arrays.asList( 463 // Mapping-IO writers
447 net.fabricmc.mappingio.format.MappingFormat.ENIGMA, 464 for (MappingFormat format : writableMappingIoFormats) {
448 net.fabricmc.mappingio.format.MappingFormat.TINY_2); 465 addSaveMappingsAsMenuEntry(format.getMappingIoCounterpart().name + " (via Mapping-IO, experimental)",
449 for (net.fabricmc.mappingio.format.MappingFormat format : writableMappingIoFormats) { 466 format, true, saveMappingsAsMenu, saveMappingsItem, gui);
450 JMenuItem item = new JMenuItem(format.name + " (via mapping-io, experimental)");
451 item.addActionListener(event -> {
452 // TODO: Use a specific file chooser for it
453 if (gui.enigmaMappingsFileChooser.getCurrentDirectory() == null) {
454 gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir()));
455 }
456
457 if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
458 gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format);
459 saveMappingsItem.setEnabled(true);
460 UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
461 }
462 });
463 saveMappingsAsMenu.add(item);
464 } 467 }
465 } 468 }
466 469
470 private static void addSaveMappingsAsMenuEntry(String text, MappingFormat format, boolean mappingIo, JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) {
471 JMenuItem item = new JMenuItem(text);
472 item.addActionListener(event -> {
473 // TODO: Use a specific file chooser for it
474 if (gui.enigmaMappingsFileChooser.getCurrentDirectory() == null) {
475 gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir()));
476 }
477
478 if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
479 gui.getController().useMappingIo = mappingIo;
480 gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format);
481 saveMappingsItem.setEnabled(true);
482 UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString());
483 }
484 });
485 saveMappingsAsMenu.add(item);
486 }
487
467 private static void prepareDecompilerMenu(JMenu decompilerMenu, Gui gui) { 488 private static void prepareDecompilerMenu(JMenu decompilerMenu, Gui gui) {
468 ButtonGroup decompilerGroup = new ButtonGroup(); 489 ButtonGroup decompilerGroup = new ButtonGroup();
469 490