From 7b01aa607c745eabadcf0508e1d20d1b6fcf1e4b Mon Sep 17 00:00:00 2001 From: Konloch Date: Thu, 15 Jul 2021 01:26:18 -0700 Subject: [PATCH] Automatically Import Known Resources Still a work in progress. If you open a know file-type through the resource list in BCV it will now attempt to automatically import that resource instead of displaying the binary text --- .../gui/resourcelist/ResourceListPane.java | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java index 940b93b8..5e3bf31a 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java @@ -25,9 +25,14 @@ import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; + +import me.konloch.kontainer.io.DiskWriter; +import org.apache.commons.io.FilenameUtils; import org.objectweb.asm.tree.ClassNode; import the.bytecode.club.bytecodeviewer.BytecodeViewer; +import the.bytecode.club.bytecodeviewer.Constants; import the.bytecode.club.bytecodeviewer.resources.IconResources; +import the.bytecode.club.bytecodeviewer.resources.importing.Import; import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; import the.bytecode.club.bytecodeviewer.translation.Translation; import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJCheckBox; @@ -37,6 +42,9 @@ import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.util.FileDrop; import the.bytecode.club.bytecodeviewer.util.LazyNameUtil; +import static the.bytecode.club.bytecodeviewer.Constants.fs; +import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory; + /*************************************************************************** * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com * @@ -155,6 +163,8 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File { try { + + //TODO refresh while preserving the opened files from before the refresh treeRoot.removeAllChildren(); for (ResourceContainer container : BytecodeViewer.resourceContainers) { @@ -317,7 +327,43 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File } else if(container.resourceFiles.containsKey(name)) { - BytecodeViewer.viewer.workPane.addFileResource(container, name); + final String fn = name.toLowerCase(); + final String extension = fn.contains(":") ? null : FilenameUtils.getExtension(fn); + + Import imp = Import.extensionMap.get(extension); + if(imp == null) //show images, text files, or hex view + BytecodeViewer.viewer.workPane.addFileResource(container, name); + else //attempt to import known resources + { + int hash = (container.name + name).hashCode(); + + //TODO make a settings toggle to disable preservation of the original name + // it should also detect if the file name is not compatible with the current OS and enable automatically + File tempFile = new File(tempDirectory + fs + hash + fs + name + "." + extension); + if(!tempFile.exists()) + { + DiskWriter.replaceFileBytes(tempFile.getAbsolutePath(), container.resourceFiles.get(name), false); + + try + { + imp.getImporter().open(tempFile); + try { + updateTree(); + } catch (NullPointerException ignored) { } + } + catch (Exception e) + { + e.printStackTrace(); + + //failsafe + BytecodeViewer.viewer.workPane.addFileResource(container, name); + } + } + else + { + //alert the user + } + } } }