Skip to content

Commit

Permalink
fix(gui): allow to select file on mapping export
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jun 20, 2022
1 parent 8486891 commit 18070eb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
Binary file modified jadx-gui/libs/mapping-io-0.4.0-SNAPSHOT.jar
Binary file not shown.
14 changes: 13 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/settings/JadxProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public JadxProject(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}

public @Nullable Path getWorkingDir() {
if (projectPath != null) {
return projectPath.toAbsolutePath().getParent();
}
List<Path> files = data.getFiles();
if (!files.isEmpty()) {
Path path = files.get(0);
return path.toAbsolutePath().getParent();
}
return null;
}

@Nullable
public Path getProjectPath() {
return projectPath;
Expand Down Expand Up @@ -166,7 +178,7 @@ private Path buildCacheDir() {
Path path = files.get(0);
return path.resolveSibling(path.getFileName() + ".cache");
}
throw new JadxRuntimeException("Can't get working dir");
throw new JadxRuntimeException("Failed to build cache dir");
}

public boolean isEnableLiveReload() {
Expand Down
35 changes: 23 additions & 12 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
Expand Down Expand Up @@ -85,7 +86,6 @@
import jadx.api.ResourceFile;
import jadx.api.plugins.utils.CommonFileUtils;
import jadx.core.Jadx;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.ListUtils;
import jadx.core.utils.StringUtils;
import jadx.core.utils.files.FileUtils;
Expand Down Expand Up @@ -362,17 +362,28 @@ private void saveProjectAs() {
}

private void exportMappings(MappingFormat mappingFormat) {
RootNode rootNode = wrapper.getDecompiler().getRoot();

Thread exportThread = new Thread(() -> {
new MappingExporter(rootNode).exportMappings(
Paths.get(project.getProjectPath().getParent().toString(),
"mappings" + (mappingFormat.hasSingleFile() ? "." + mappingFormat.fileExt : "")),
project.getCodeData(), mappingFormat);
});

backgroundExecutor.execute(NLS.str("progress.export_mappings"), exportThread);
update();
FileDialog fileDialog = new FileDialog(this, FileDialog.OpenMode.CUSTOM_SAVE);
fileDialog.setTitle(NLS.str("file.export_mappings_as"));
Path workingDir = project.getWorkingDir();
Path baseDir = workingDir != null ? workingDir : settings.getLastSaveFilePath();
if (mappingFormat.hasSingleFile()) {
fileDialog.setSelectedFile(baseDir.resolve("mappings." + mappingFormat.fileExt));
fileDialog.setFileExtList(Collections.singletonList(mappingFormat.fileExt));
fileDialog.setSelectionMode(JFileChooser.FILES_ONLY);
} else {
fileDialog.setCurrentDir(baseDir);
fileDialog.setSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
List<Path> paths = fileDialog.show();
if (paths.size() != 1) {
return;
}
Path savePath = paths.get(0);
LOG.info("Export mappings to: {}", savePath.toAbsolutePath());
backgroundExecutor.execute(NLS.str("progress.export_mappings"),
() -> new MappingExporter(wrapper.getDecompiler().getRoot())
.exportMappings(savePath, project.getCodeData(), mappingFormat),
s -> update());
}

void open(List<Path> paths) {
Expand Down
41 changes: 35 additions & 6 deletions jadx-gui/src/main/java/jadx/gui/ui/dialog/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
public class FileDialog {

public enum OpenMode {
OPEN, ADD, SAVE_PROJECT, EXPORT
OPEN,
ADD,
SAVE_PROJECT,
EXPORT,
CUSTOM_SAVE,
CUSTOM_OPEN
}

private final MainWindow mainWindow;
Expand All @@ -44,6 +49,26 @@ public FileDialog(MainWindow mainWindow, OpenMode mode) {
initForMode(mode);
}

public void setTitle(String title) {
this.title = title;
}

public void setFileExtList(List<String> fileExtList) {
this.fileExtList = fileExtList;
}

public void setSelectionMode(int selectionMode) {
this.selectionMode = selectionMode;
}

public void setSelectedFile(Path path) {
this.selectedFile = path;
}

public void setCurrentDir(Path currentDir) {
this.currentDir = currentDir;
}

public List<Path> show() {
FileChooser fileChooser = buildFileChooser();
int ret = isOpen ? fileChooser.showOpenDialog(mainWindow) : fileChooser.showSaveDialog(mainWindow);
Expand All @@ -66,10 +91,6 @@ public Path getCurrentDir() {
return currentDir;
}

public void setSelectedFile(Path path) {
this.selectedFile = path;
}

private void initForMode(OpenMode mode) {
switch (mode) {
case OPEN:
Expand Down Expand Up @@ -101,6 +122,14 @@ private void initForMode(OpenMode mode) {
currentDir = mainWindow.getSettings().getLastSaveFilePath();
isOpen = false;
break;

case CUSTOM_SAVE:
isOpen = false;
break;

case CUSTOM_OPEN:
isOpen = true;
break;
}
}

Expand All @@ -110,7 +139,7 @@ private FileChooser buildFileChooser() {
fileChooser.setFileSelectionMode(selectionMode);
fileChooser.setMultiSelectionEnabled(isOpen);
fileChooser.setAcceptAllFileFilterUsed(true);
if (!fileExtList.isEmpty()) {
if (Utils.notEmpty(fileExtList)) {
String description = NLS.str("file_dialog.supported_files") + ": (" + Utils.listToString(fileExtList) + ')';
fileChooser.setFileFilter(new FileNameExtensionFilter(description, fileExtList.toArray(new String[0])));
}
Expand Down

4 comments on commit 18070eb

@NebelNidas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly did you change in the mapping-io JAR? Just minification or something else?

@skylot
Copy link
Owner Author

@skylot skylot commented on 18070eb Jul 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NebelNidas
That jar contains embedded libraries (asm for example), this can cause weird issues if classes of different version loaded and mixed at the same time. So please do not pack other libs into this jar.

@NebelNidas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, noted! 👍

@NebelNidas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something must've gone wrong when you stripped out the embedded libraries, when importing mappings via my new PR it always crashes. Building a new copy (without shadowJar) from my mapping-io fork works, so I'll include that one

Please sign in to comment.