Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gui): add script rename feature #2374

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
Expand Down Expand Up @@ -267,6 +270,20 @@ public static String readFile(Path textFile) throws IOException {
return Files.readString(textFile);
}

public static boolean renameFile(Path sourcePath, Path targetPath) {
try {
Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (NoSuchFileException e) {
LOG.error("File to rename not found {}", sourcePath, e);
} catch (FileAlreadyExistsException e) {
LOG.error("File with that name already exists {}", targetPath, e);
} catch (IOException e) {
LOG.error("Error renaming file {}", e.getMessage(), e);
}
return false;
}

@NotNull
public static File prepareFile(File file) {
File saveFile = cutFileName(file);
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/java/jadx/gui/treemodel/JInputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public JPopupMenu onTreePopupMenu(MainWindow mainWindow) {
JPopupMenu menu = new JPopupMenu();
menu.add(new SimpleMenuItem(NLS.str("popup.add_files"), mainWindow::addFiles));
menu.add(new SimpleMenuItem(NLS.str("popup.remove"), () -> mainWindow.removeInput(filePath)));
menu.add(new SimpleMenuItem(NLS.str("popup.rename"), () -> mainWindow.renameInput(filePath)));
return menu;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public JPopupMenu onTreePopupMenu(MainWindow mainWindow) {
menu.add(new SimpleMenuItem(NLS.str("popup.add_scripts"), mainWindow::addFiles));
menu.add(new SimpleMenuItem(NLS.str("popup.new_script"), mainWindow::addNewScript));
menu.add(new SimpleMenuItem(NLS.str("popup.remove"), () -> mainWindow.removeInput(scriptPath)));
menu.add(new SimpleMenuItem(NLS.str("popup.rename"), () -> mainWindow.renameInput(scriptPath)));
return menu;
}

Expand Down
30 changes: 27 additions & 3 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,38 @@ public void addNewScript() {
}
List<Path> inputs = project.getFilePaths();
inputs.add(scriptFile);
project.setFilePaths(inputs);
project.save();
reopen();
refreshTree(inputs);
}

public void removeInput(Path file) {
List<Path> inputs = project.getFilePaths();
inputs.remove(file);
refreshTree(inputs);
}

public void renameInput(Path file) {
String newName = JOptionPane.showInputDialog(this, NLS.str("message.enter_new_name"), file.getFileName().toString());
if (newName == null || newName.trim().isEmpty()) {
return;
}
Path targetPath = file.resolveSibling(newName);

boolean success = FileUtils.renameFile(file, targetPath);
if (success) {
List<Path> inputs = project.getFilePaths();
inputs.remove(file);
inputs.add(targetPath);

refreshTree(inputs);
} else {
JOptionPane.showMessageDialog(this,
NLS.str("message.could_not_rename"),
NLS.str("message.errorTitle"),
JOptionPane.ERROR_MESSAGE);
}
}

private void refreshTree(List<Path> inputs) {
project.setFilePaths(inputs);
project.save();
reopen();
Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=Keine Klassen geladen, nichts zu dekompilieren!
message.saveIncomplete=<html>Speichern unvollständig.<br> %s<br> %d Klassen oder Ressourcen wurden nicht gespeichert!</html>
message.indexIncomplete=<html>Index einiger Klassen übersprungen.<br> %s<br> %d Klassen wurden nicht indiziert und werden nicht in den Suchergebnissen erscheinen!</html>
message.indexingClassesSkipped=<html>Jadx hat nur noch wenig Speicherplatz. Daher wurden %d Klassen nicht indiziert.<br>Wenn Sie möchten, dass alle Klassen indiziert werden, Jadx mit erhöhter maximaler Heap-Größe neustarten.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=JADX-Speicherauslastung: %.2f GB von %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=No classes loaded, nothing to decompile!
message.saveIncomplete=<html>Save incomplete.<br> %s<br> %d classes or resources were not saved!</html>
message.indexIncomplete=<html>Index of some classes skipped.<br> %s<br> %d classes were not indexed and will not appear in search results!</html>
message.indexingClassesSkipped=<html>Jadx is running low on memory. Therefore %d classes were not indexed.<br>If you want all classes to be indexed restart Jadx with increased maximum heap size.</html>
message.enter_new_name=Enter new name
message.could_not_rename=Can't rename the file

heapUsage.text=JADX memory usage: %.2f GB of %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ nav.forward=Adelante
#message.saveIncomplete=<html>Save incomplete.<br> %s<br> %d classes or resources were not saved!</html>
#message.indexIncomplete=<html>Index of some classes skipped.<br> %s<br> %d classes were not indexed and will not appear in search results!</html>
#message.indexingClassesSkipped=<html>Jadx is running low on memory. Therefore %d classes were not indexed.<br>If you want all classes to be indexed restart Jadx with increased maximum heap size.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

#heapUsage.text=JADX memory usage: %.2f GB of %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_id_ID.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=Tidak ada kelas yang dimuat, tidak ada yang dapat didekompila
message.saveIncomplete=<html>Simpan tidak lengkap.<br> %s<br> %d kelas atau sumber daya tidak disimpan!</html>
message.indexIncomplete=<html>Indeks beberapa kelas dilewati.<br> %s<br> %d kelas tidak diindeks dan tidak akan muncul dalam hasil pencarian!</html>
message.indexingClassesSkipped=<html>JADX kekurangan memori. Oleh karena itu %d kelas tidak diindeks.<br>Jika Anda ingin semua kelas diindeks, restart JADX dengan ukuran heap maksimum yang lebih besar.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=Penggunaan memori JADX: %.2f GB dari %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=로드된 클래스 없음, 디컴파일 대상 존재하지
message.saveIncomplete=<html>저장이 완료되지 않았습니다.<br> %s<br> %d개의 클래스 또는 리소스가 저장되지 않았습니다!</html>
message.indexIncomplete=<html>일부 클래스의 색인을 건너뛰었습니다.<br> %s<br> %d개의 클래스는 색인이 생성되지 않았으며 검색 결과에 나타나지 않습니다.</html>
message.indexingClassesSkipped=<html>Jadx의 메모리가 부족합니다. 따라서 %d 개의 클래스가 인덱싱되지 않았습니다. <br> 모든 클래스를 인덱싱하려면 최대 힙 크기를 늘린 상태로 Jadx를 다시 시작하십시오.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=JADX 메모리 사용량 : %.2f GB / %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=Nenhuma classe carregada, nada para ser descompilado!
message.saveIncomplete=<html>Operação não foi completa.<br> %s<br> %d classes ou recursos não foram salvos!</html>
message.indexIncomplete=<html>Indexação de algumas classes foram ignoradas.<br> %s<br> %d classes não foram indexadas não vão aparecer nos resultados de busca!</html>
message.indexingClassesSkipped=<html>Jadx está rodando com pouca memória. Por conta disso, %d classes não foram indexadas.<br>Se você deseja que todas classes sejam indexadas, reinicie com um limite de memória heap maior.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=Uso de memória do JADX: %.2f GB of %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=Классы не найдены, нечего декомпи
message.saveIncomplete=<html>Сохранение не завершено.<br> %s<br> %d классов или ресурсов не сохранено!</html>
message.indexIncomplete=<html>Индексирование некоторых классов пропущено.<br> %s<br> %d классов не индексировано, и не будет отображаться в результатах поиска!</html>
message.indexingClassesSkipped=<html>JaDX запущен с малым количеством ОЗУ. %d классов не индексировано.<br>Если вы хотите их индексировать, перезапустите JaDX с большим Heap Size.</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=JADX использует: %.2f ГБ из %.2f ГБ

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=无类被加载,没什么可反编译!
message.saveIncomplete=<html>保存未完成。<br> %s<br> %d 类或资源未被保存!</html>
message.indexIncomplete=<html>已跳过某些类索引。<br> %s<br> %d 类未被索引,不会出现在搜索结果中!</html>
message.indexingClassesSkipped=<html>Jadx 的内存不足。因此,%d 个类没有编入索引。<br>如果要将所有类编入索引,请增加的最大堆空间后重新启动 Jadx。</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=JADX 内存使用率:%.2f GB / %.2f GB

Expand Down
2 changes: 2 additions & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ message.no_classes=未載入任何類別,沒有東西可以反編譯!
message.saveIncomplete=<html>儲存未完成。<br> %s<br> %d 個類別或資源尚未儲存!</html>
message.indexIncomplete=<html>某些類別的索引被略過。<br> %s<br> %d 個類別未被索引,故不會出現在搜尋結果中。</html>
message.indexingClassesSkipped=<html>Jadx 的記憶體不足。故 %d 個類別未被索引。<br>如果您想要索引所有類別,請增加最大堆疊大小並重新啟動。</html>
#message.enter_new_name=Enter new name
#message.could_not_rename=Can't rename the file

heapUsage.text=JADX 記憶體使用率:%.2f GB / %.2f GB

Expand Down
Loading