diff options
| author | 2023-11-28 09:00:33 +0000 | |
|---|---|---|
| committer | 2023-11-28 09:00:33 +0000 | |
| commit | 7622b753d2a99bd8f2bbb4568fd84ca9458ffba5 (patch) | |
| tree | e3a5a6d2006787f2668e47bca34fb46beb40250c /enigma-swing/src/main/java/cuchaz | |
| parent | Update deps (#535) (diff) | |
| parent | Merge branch 'upstream' into mapping-io (diff) | |
| download | enigma-fork-7622b753d2a99bd8f2bbb4568fd84ca9458ffba5.tar.gz enigma-fork-7622b753d2a99bd8f2bbb4568fd84ca9458ffba5.tar.xz enigma-fork-7622b753d2a99bd8f2bbb4568fd84ca9458ffba5.zip | |
Merge pull request #463 from NebelNidas/mapping-io
Initial Mapping-IO support
Diffstat (limited to 'enigma-swing/src/main/java/cuchaz')
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java | 17 | ||||
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | 88 |
2 files changed, 71 insertions, 34 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 91037b0..c579901 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; | |||
| 29 | import javax.swing.SwingUtilities; | 29 | import javax.swing.SwingUtilities; |
| 30 | 30 | ||
| 31 | import com.google.common.collect.Lists; | 31 | import com.google.common.collect.Lists; |
| 32 | import org.jetbrains.annotations.ApiStatus; | ||
| 32 | 33 | ||
| 33 | import cuchaz.enigma.Enigma; | 34 | import cuchaz.enigma.Enigma; |
| 34 | import cuchaz.enigma.EnigmaProfile; | 35 | import cuchaz.enigma.EnigmaProfile; |
| @@ -139,6 +140,12 @@ public class GuiController implements ClientPacketHandler { | |||
| 139 | this.gui.onCloseJar(); | 140 | this.gui.onCloseJar(); |
| 140 | } | 141 | } |
| 141 | 142 | ||
| 143 | @ApiStatus.Internal | ||
| 144 | public CompletableFuture<Void> openMappings(MappingFormat format, Path path, boolean useMappingIo) { | ||
| 145 | System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false"); | ||
| 146 | return openMappings(format, path); | ||
| 147 | } | ||
| 148 | |||
| 142 | public CompletableFuture<Void> openMappings(MappingFormat format, Path path) { | 149 | public CompletableFuture<Void> openMappings(MappingFormat format, Path path) { |
| 143 | if (project == null) { | 150 | if (project == null) { |
| 144 | return CompletableFuture.completedFuture(null); | 151 | return CompletableFuture.completedFuture(null); |
| @@ -149,9 +156,7 @@ public class GuiController implements ClientPacketHandler { | |||
| 149 | return ProgressDialog.runOffThread(gui.getFrame(), progress -> { | 156 | return ProgressDialog.runOffThread(gui.getFrame(), progress -> { |
| 150 | try { | 157 | try { |
| 151 | MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters(); | 158 | MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters(); |
| 152 | 159 | project.setMappings(format.read(path, progress, saveParameters, project.getJarIndex())); | |
| 153 | EntryTree<EntryMapping> mappings = format.read(path, progress, saveParameters); | ||
| 154 | project.setMappings(mappings); | ||
| 155 | 160 | ||
| 156 | loadedMappingFormat = format; | 161 | loadedMappingFormat = format; |
| 157 | loadedMappingPath = path; | 162 | loadedMappingPath = path; |
| @@ -179,6 +184,12 @@ public class GuiController implements ClientPacketHandler { | |||
| 179 | return saveMappings(path, loadedMappingFormat); | 184 | return saveMappings(path, loadedMappingFormat); |
| 180 | } | 185 | } |
| 181 | 186 | ||
| 187 | @ApiStatus.Internal | ||
| 188 | public CompletableFuture<Void> saveMappings(Path path, MappingFormat format, boolean useMappingIo) { | ||
| 189 | System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false"); | ||
| 190 | return saveMappings(path, format); | ||
| 191 | } | ||
| 192 | |||
| 182 | /** | 193 | /** |
| 183 | * Saves the mappings, with a dialog popping up, showing the progress. | 194 | * Saves the mappings, with a dialog popping up, showing the progress. |
| 184 | * | 195 | * |
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 1cfad50..30e3586 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 | |||
| @@ -45,7 +45,7 @@ public class MenuBar { | |||
| 45 | private final JMenu fileMenu = new JMenu(); | 45 | private final JMenu fileMenu = new JMenu(); |
| 46 | private final JMenuItem jarOpenItem = new JMenuItem(); | 46 | private final JMenuItem jarOpenItem = new JMenuItem(); |
| 47 | private final JMenuItem jarCloseItem = new JMenuItem(); | 47 | private final JMenuItem jarCloseItem = new JMenuItem(); |
| 48 | private final JMenu openMenu = new JMenu(); | 48 | private final JMenu openMappingsMenu = new JMenu(); |
| 49 | private final JMenuItem saveMappingsItem = new JMenuItem(); | 49 | private final JMenuItem saveMappingsItem = new JMenuItem(); |
| 50 | private final JMenu saveMappingsAsMenu = new JMenu(); | 50 | private final JMenu saveMappingsAsMenu = new JMenu(); |
| 51 | private final JMenuItem closeMappingsItem = new JMenuItem(); | 51 | private final JMenuItem closeMappingsItem = new JMenuItem(); |
| @@ -88,7 +88,7 @@ public class MenuBar { | |||
| 88 | 88 | ||
| 89 | this.retranslateUi(); | 89 | this.retranslateUi(); |
| 90 | 90 | ||
| 91 | prepareOpenMenu(this.openMenu, gui); | 91 | prepareOpenMappingsMenu(this.openMappingsMenu, gui); |
| 92 | prepareSaveMappingsAsMenu(this.saveMappingsAsMenu, this.saveMappingsItem, gui); | 92 | prepareSaveMappingsAsMenu(this.saveMappingsAsMenu, this.saveMappingsItem, gui); |
| 93 | prepareDecompilerMenu(this.decompilerMenu, gui); | 93 | prepareDecompilerMenu(this.decompilerMenu, gui); |
| 94 | prepareThemesMenu(this.themesMenu, gui); | 94 | prepareThemesMenu(this.themesMenu, gui); |
| @@ -98,7 +98,7 @@ public class MenuBar { | |||
| 98 | this.fileMenu.add(this.jarOpenItem); | 98 | this.fileMenu.add(this.jarOpenItem); |
| 99 | this.fileMenu.add(this.jarCloseItem); | 99 | this.fileMenu.add(this.jarCloseItem); |
| 100 | this.fileMenu.addSeparator(); | 100 | this.fileMenu.addSeparator(); |
| 101 | this.fileMenu.add(this.openMenu); | 101 | this.fileMenu.add(this.openMappingsMenu); |
| 102 | this.fileMenu.add(this.saveMappingsItem); | 102 | this.fileMenu.add(this.saveMappingsItem); |
| 103 | this.fileMenu.add(this.saveMappingsAsMenu); | 103 | this.fileMenu.add(this.saveMappingsAsMenu); |
| 104 | this.fileMenu.add(this.closeMappingsItem); | 104 | this.fileMenu.add(this.closeMappingsItem); |
| @@ -174,7 +174,7 @@ public class MenuBar { | |||
| 174 | this.startServerItem.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop")); | 174 | this.startServerItem.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop")); |
| 175 | 175 | ||
| 176 | this.jarCloseItem.setEnabled(jarOpen); | 176 | this.jarCloseItem.setEnabled(jarOpen); |
| 177 | this.openMenu.setEnabled(jarOpen); | 177 | this.openMappingsMenu.setEnabled(jarOpen); |
| 178 | this.saveMappingsItem.setEnabled(jarOpen && this.gui.enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED); | 178 | this.saveMappingsItem.setEnabled(jarOpen && this.gui.enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED); |
| 179 | this.saveMappingsAsMenu.setEnabled(jarOpen); | 179 | this.saveMappingsAsMenu.setEnabled(jarOpen); |
| 180 | this.closeMappingsItem.setEnabled(jarOpen); | 180 | this.closeMappingsItem.setEnabled(jarOpen); |
| @@ -189,7 +189,7 @@ public class MenuBar { | |||
| 189 | this.fileMenu.setText(I18n.translate("menu.file")); | 189 | this.fileMenu.setText(I18n.translate("menu.file")); |
| 190 | this.jarOpenItem.setText(I18n.translate("menu.file.jar.open")); | 190 | this.jarOpenItem.setText(I18n.translate("menu.file.jar.open")); |
| 191 | this.jarCloseItem.setText(I18n.translate("menu.file.jar.close")); | 191 | this.jarCloseItem.setText(I18n.translate("menu.file.jar.close")); |
| 192 | this.openMenu.setText(I18n.translate("menu.file.mappings.open")); | 192 | this.openMappingsMenu.setText(I18n.translate("menu.file.mappings.open")); |
| 193 | this.saveMappingsItem.setText(I18n.translate("menu.file.mappings.save")); | 193 | this.saveMappingsItem.setText(I18n.translate("menu.file.mappings.save")); |
| 194 | this.saveMappingsAsMenu.setText(I18n.translate("menu.file.mappings.save_as")); | 194 | this.saveMappingsAsMenu.setText(I18n.translate("menu.file.mappings.save_as")); |
| 195 | this.closeMappingsItem.setText(I18n.translate("menu.file.mappings.close")); | 195 | this.closeMappingsItem.setText(I18n.translate("menu.file.mappings.close")); |
| @@ -402,45 +402,71 @@ public class MenuBar { | |||
| 402 | GuiUtil.openUrl("https://github.com/FabricMC/Enigma"); | 402 | GuiUtil.openUrl("https://github.com/FabricMC/Enigma"); |
| 403 | } | 403 | } |
| 404 | 404 | ||
| 405 | private static void prepareOpenMenu(JMenu openMenu, Gui gui) { | 405 | private static void prepareOpenMappingsMenu(JMenu openMappingsMenu, Gui gui) { |
| 406 | // Mapping-IO readers | ||
| 407 | MappingFormat.getReadableFormats().stream() | ||
| 408 | .filter(format -> format.getMappingIoCounterpart() != null) | ||
| 409 | .forEach(format -> addOpenMappingsMenuEntry(I18n.translate(format.getMappingIoCounterpart().name), | ||
| 410 | format, true, openMappingsMenu, gui)); | ||
| 411 | openMappingsMenu.addSeparator(); | ||
| 412 | |||
| 413 | // Enigma's own readers | ||
| 406 | for (MappingFormat format : MappingFormat.values()) { | 414 | for (MappingFormat format : MappingFormat.values()) { |
| 407 | if (format.getReader() != null) { | 415 | if (format.getReader() != null) { |
| 408 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | 416 | addOpenMappingsMenuEntry(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT)) + " (legacy)", |
| 409 | item.addActionListener(event -> { | 417 | format, false, openMappingsMenu, gui); |
| 410 | gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir())); | ||
| 411 | |||
| 412 | if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 413 | File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile(); | ||
| 414 | gui.getController().openMappings(format, selectedFile.toPath()); | ||
| 415 | UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString()); | ||
| 416 | } | ||
| 417 | }); | ||
| 418 | openMenu.add(item); | ||
| 419 | } | 418 | } |
| 420 | } | 419 | } |
| 421 | } | 420 | } |
| 422 | 421 | ||
| 422 | private static void addOpenMappingsMenuEntry(String text, MappingFormat format, boolean mappingIo, JMenu openMappingsMenu, Gui gui) { | ||
| 423 | JMenuItem item = new JMenuItem(text); | ||
| 424 | item.addActionListener(event -> { | ||
| 425 | gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir())); | ||
| 426 | |||
| 427 | if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 428 | File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile(); | ||
| 429 | gui.getController().openMappings(format, selectedFile.toPath(), mappingIo); | ||
| 430 | UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString()); | ||
| 431 | } | ||
| 432 | }); | ||
| 433 | openMappingsMenu.add(item); | ||
| 434 | } | ||
| 435 | |||
| 423 | private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) { | 436 | private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) { |
| 437 | // Mapping-IO writers | ||
| 438 | MappingFormat.getWritableFormats().stream() | ||
| 439 | .filter(format -> format.hasMappingIoWriter()) | ||
| 440 | .forEach(format -> addSaveMappingsAsMenuEntry(format.getMappingIoCounterpart().name, | ||
| 441 | format, true, saveMappingsAsMenu, saveMappingsItem, gui)); | ||
| 442 | saveMappingsAsMenu.addSeparator(); | ||
| 443 | |||
| 444 | // Enigma's own writers | ||
| 424 | for (MappingFormat format : MappingFormat.values()) { | 445 | for (MappingFormat format : MappingFormat.values()) { |
| 425 | if (format.getWriter() != null) { | 446 | if (format.getWriter() != null) { |
| 426 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | 447 | addSaveMappingsAsMenuEntry(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT)) + " (legacy)", |
| 427 | item.addActionListener(event -> { | 448 | format, false, saveMappingsAsMenu, saveMappingsItem, gui); |
| 428 | // TODO: Use a specific file chooser for it | ||
| 429 | if (gui.enigmaMappingsFileChooser.getCurrentDirectory() == null) { | ||
| 430 | gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir())); | ||
| 431 | } | ||
| 432 | |||
| 433 | if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 434 | gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format); | ||
| 435 | saveMappingsItem.setEnabled(true); | ||
| 436 | UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString()); | ||
| 437 | } | ||
| 438 | }); | ||
| 439 | saveMappingsAsMenu.add(item); | ||
| 440 | } | 449 | } |
| 441 | } | 450 | } |
| 442 | } | 451 | } |
| 443 | 452 | ||
| 453 | private static void addSaveMappingsAsMenuEntry(String text, MappingFormat format, boolean mappingIo, JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) { | ||
| 454 | JMenuItem item = new JMenuItem(text); | ||
| 455 | item.addActionListener(event -> { | ||
| 456 | // TODO: Use a specific file chooser for it | ||
| 457 | if (gui.enigmaMappingsFileChooser.getCurrentDirectory() == null) { | ||
| 458 | gui.enigmaMappingsFileChooser.setCurrentDirectory(new File(UiConfig.getLastSelectedDir())); | ||
| 459 | } | ||
| 460 | |||
| 461 | if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 462 | gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format, mappingIo); | ||
| 463 | saveMappingsItem.setEnabled(true); | ||
| 464 | UiConfig.setLastSelectedDir(gui.enigmaMappingsFileChooser.getCurrentDirectory().toString()); | ||
| 465 | } | ||
| 466 | }); | ||
| 467 | saveMappingsAsMenu.add(item); | ||
| 468 | } | ||
| 469 | |||
| 444 | private static void prepareDecompilerMenu(JMenu decompilerMenu, Gui gui) { | 470 | private static void prepareDecompilerMenu(JMenu decompilerMenu, Gui gui) { |
| 445 | ButtonGroup decompilerGroup = new ButtonGroup(); | 471 | ButtonGroup decompilerGroup = new ButtonGroup(); |
| 446 | 472 | ||