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
This commit is contained in:
Konloch 2021-07-15 01:26:18 -07:00
parent 4d0021f2ef
commit 7b01aa607c
1 changed files with 47 additions and 1 deletions

View File

@ -25,9 +25,14 @@ import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode; import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath; import javax.swing.tree.TreePath;
import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.io.FilenameUtils;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; 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.IconResources;
import the.bytecode.club.bytecodeviewer.resources.importing.Import;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.translation.Translation; import the.bytecode.club.bytecodeviewer.translation.Translation;
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJCheckBox; 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.FileDrop;
import the.bytecode.club.bytecodeviewer.util.LazyNameUtil; 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 * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com * * Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
@ -155,6 +163,8 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File
{ {
try try
{ {
//TODO refresh while preserving the opened files from before the refresh
treeRoot.removeAllChildren(); treeRoot.removeAllChildren();
for (ResourceContainer container : BytecodeViewer.resourceContainers) for (ResourceContainer container : BytecodeViewer.resourceContainers)
{ {
@ -317,7 +327,43 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File
} }
else if(container.resourceFiles.containsKey(name)) 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
}
}
} }
} }