Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3193 - Tool - Fixed bug that caused exported tool to exclude plugin config settings

dragonmacher16a6b4eb5212
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/Tool/Ghidra_Tool_Administration.htm b/Ghidra/Features/Base/src/main/help/help/topics/Tool/Ghidra_Tool_Administration.htm
index f58b8b696e..8031bc4cb5 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/Tool/Ghidra_Tool_Administration.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/Tool/Ghidra_Tool_Administration.htm
@@ -266,6 +266,18 @@
</OL>
</BLOCKQUOTE>
+
+ <H4><A name="Export_Default_Tool"></A>Export Default Tool</H4>
+
+ <BLOCKQUOTE>
+ <P><A HREF="#Export_Tool">Exports</A> the chosen tool <b>without including user configuration information</b>.
+ This allows the user to export a tool without including personal settings. This
+ may be helpful if the user is going to share the exported tool with others.
+ </P>
+ </BLOCKQUOTE>
+
+
+
<H3><A name="Run_Tool"></A>Run Tool</H3>
<BLOCKQUOTE>
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/TestEnv.java b/Ghidra/Features/Base/src/main/java/ghidra/test/TestEnv.java
index 512e7229a7..e03dc96785 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/test/TestEnv.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/TestEnv.java
@@ -540,7 +540,7 @@ public class TestEnv {
return AbstractGenericTest.runSwing(() -> {
ToolTemplate toolTemplate =
- ToolUtils.readToolTemplate("defaultTools/" + toolName + ".tool");
+ ToolUtils.readToolTemplate("defaultTools/" + toolName + ToolUtils.TOOL_EXTENSION);
if (toolTemplate == null) {
Msg.debug(TestEnv.class, "Unable to find tool: " + toolName);
return null;
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/project/tool/ToolSaving3Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/project/tool/ToolSaving3Test.java
index ff5eab7820..89efc101a6 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/project/tool/ToolSaving3Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/project/tool/ToolSaving3Test.java
@@ -15,11 +15,21 @@
*/
package ghidra.framework.project.tool;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
import org.junit.Test;
+import docking.action.DockingActionIf;
+import docking.widgets.OptionDialog;
+import docking.widgets.dialogs.InputWithChoicesDialog;
+import docking.widgets.filechooser.GhidraFileChooser;
+import ghidra.framework.ToolUtils;
import ghidra.framework.plugintool.PluginTool;
+import utilities.util.FileUtilities;
public class ToolSaving3Test extends AbstractToolSavingTest {
@@ -66,4 +76,77 @@ public class ToolSaving3Test extends AbstractToolSavingTest {
assertEquals("Changed tool was not saved", !isSet, getBooleanFooOptions(newTool));
}
+ @Test
+ public void testExportDefaultTool() throws Exception {
+
+ //
+ // Verify a 'default' tool does not contain 'config state' when written
+ //
+
+ PluginTool tool1 = launchTool(DEFAULT_TEST_TOOL_NAME);
+ DockingActionIf exportAction = getAction(tool1, "Export Default Tool");
+ performAction(exportAction, false);
+
+ InputWithChoicesDialog chooseToolDialog =
+ waitForDialogComponent(InputWithChoicesDialog.class);
+ pressButtonByText(chooseToolDialog, "OK");
+
+ GhidraFileChooser chooser = waitForDialogComponent(GhidraFileChooser.class);
+ File exportedFile = createTempFile("ExportedDefaultTool", ToolUtils.TOOL_EXTENSION);
+ chooser.setSelectedFile(exportedFile);
+ waitForUpdateOnChooser(chooser);
+ pressButtonByText(chooser, "Export");
+
+ OptionDialog overwriteDialog = waitForDialogComponent(OptionDialog.class);
+ pressButtonByText(overwriteDialog, "Overwrite");
+ waitForCondition(() -> exportedFile.length() > 0);
+
+ assertExportedFileDoesNotContainsLine(exportedFile, "PLUGIN_STATE");
+ }
+
+ @Test
+ public void testExportedToolContainsConfigSettings() throws Exception {
+
+ //
+ // Regression test: ensure that an exported tool contains config settings
+ //
+
+ PluginTool tool1 = launchTool(DEFAULT_TEST_TOOL_NAME);
+ DockingActionIf exportAction = getAction(tool1, "Export Tool");
+ performAction(exportAction, false);
+
+ GhidraFileChooser chooser = waitForDialogComponent(GhidraFileChooser.class);
+ File exportedFile = createTempFile("ExportedTool", ToolUtils.TOOL_EXTENSION);
+ chooser.setSelectedFile(exportedFile);
+ waitForUpdateOnChooser(chooser);
+ pressButtonByText(chooser, "Export");
+
+ OptionDialog overwriteDialog = waitForDialogComponent(OptionDialog.class);
+ pressButtonByText(overwriteDialog, "Overwrite");
+ waitForCondition(() -> exportedFile.length() > 0);
+
+ assertExportedFileContainsLine(exportedFile, "PLUGIN_STATE");
+ }
+
+ private void assertExportedFileContainsLine(File file, String line) throws IOException {
+
+ List<String> exportedLines = FileUtilities.getLines(file);
+ for (String fileLine : exportedLines) {
+ if (fileLine.contains(line)) {
+ return;
+ }
+ }
+
+ fail("File text does not have a line containing '" + line + "'");
+ }
+
+ private void assertExportedFileDoesNotContainsLine(File file, String line) throws Exception {
+ List<String> exportedLines = FileUtilities.getLines(file);
+ for (String fileLine : exportedLines) {
+ if (fileLine.contains(line)) {
+ fail("File text should not have a line containing '" + line + "'");
+ }
+ }
+ }
+
}
diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/gui/plugin/VTSubToolManager.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/gui/plugin/VTSubToolManager.java
index a22fc6d4a7..7db615c5a5 100644
--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/gui/plugin/VTSubToolManager.java
+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/gui/plugin/VTSubToolManager.java
@@ -123,7 +123,7 @@ public class VTSubToolManager implements VTControllerListener, OptionsChangeList
}
private void resetTool(String toolName) {
- String toolFileName = toolName + ".tool";
+ String toolFileName = toolName + ToolUtils.TOOL_EXTENSION;
File toolFile = new File(ToolUtils.getApplicationToolDirPath(), toolFileName);
if (toolFile.exists()) {
toolFile.delete();
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/ToolUtils.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/ToolUtils.java
index 76d0108b60..73872ea8fc 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/ToolUtils.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/ToolUtils.java
@@ -36,6 +36,7 @@ import resources.ResourceManager;
public class ToolUtils {
+ public static final String TOOL_EXTENSION = ".tool";
private static final Logger LOGGER = LogManager.getLogger(ToolUtils.class);
private static final File USER_TOOLS_DIR = new File(getApplicationToolDirPath());
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/FrontEndPlugin.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/FrontEndPlugin.java
index c0ff975aac..15a8addafa 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/FrontEndPlugin.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/FrontEndPlugin.java
@@ -18,16 +18,14 @@ package ghidra.framework.main;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseEvent;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
-import org.jdom.Document;
-import org.jdom.output.XMLOutputter;
-
import docking.*;
import docking.action.DockingAction;
import docking.action.MenuData;
@@ -52,8 +50,8 @@ import ghidra.framework.plugintool.util.PluginStatus;
import ghidra.framework.preferences.Preferences;
import ghidra.framework.remote.User;
import ghidra.util.*;
-import ghidra.util.filechooser.*;
-import ghidra.util.xml.GenericXMLOutputter;
+import ghidra.util.filechooser.GhidraFileChooserModel;
+import ghidra.util.filechooser.GhidraFileFilter;
import resources.ResourceManager;
/**
@@ -103,8 +101,6 @@ public class FrontEndPlugin extends Plugin
private LogPanel statusPanel;
private String projectName;
- private GhidraFileChooser exportFileChooser;
-
private FileActionManager fileActionManager;
private ProjectActionManager projectActionManager;
private ToolActionManager toolActionManager;
@@ -302,39 +298,10 @@ public class FrontEndPlugin extends Plugin
return (FrontEndTool) tool;
}
- /**
- * Get the component for this plugin.
- *
- * NOTE: made plugin for JUnits...
- */
public JComponent getComponent() {
return mainGuiPanel;
}
- /**
- * Get the descriptive name for this plugin.
- */
- public static String getDescriptiveName() {
- return "Front End";
- }
-
- /**
- * Get the plugin's description.
- */
- public static String getDescription() {
- return "Front End Components for Ghidra";
- }
-
- /**
- * Get the category where this plugin belongs.
- */
- public static String getCategory() {
- return "FrontEnd";
- }
-
- /* (non Javadoc)
- * @see ghidra.framework.client.RemoteAdapterListener#connectionStateChanged(java.lang.Object)
- */
@Override
public void connectionStateChanged(final Object adapter) {
if (activeProject != null) {
@@ -506,8 +473,11 @@ public class FrontEndPlugin extends Plugin
* general project utility that brings up a file chooser for
* the user to specify a directory and filename that are used
* for the Project location and name
- * @param create The mode for the fileChooser to be in: create or open existing
- * @param approveButtonText The label for the "Open" button on the file chooser
+ *
+ * @param fileChooser the chooser used to pick the project
+ * @param mode read-only or not
+ * @param preferenceName the preference property name used to save the last opened project
+ * @return the project locator for the opened project
*/
ProjectLocator chooseProject(GhidraFileChooser fileChooser, String mode,
String preferenceName) {
@@ -632,24 +602,10 @@ public class FrontEndPlugin extends Plugin
return actionContext;
}
- /**
- * designates whether the user interface is showing or hidden
- * @return true if showing; false if not showing
- */
- final boolean isGuiShowing() {
- return mainGuiPanel.isShowing();
- }
-
- /**
- * Get the tool button transferable.
- */
ToolButtonTransferable getToolButtonTransferable() {
return toolButtonTransferable;
}
- /**
- * Set the tool button transferable.
- */
void setToolButtonTransferable(ToolButtonTransferable t) {
if (t == null && toolButtonTransferable != null) {
toolButtonTransferable.clearTransferData();
@@ -685,89 +641,19 @@ public class FrontEndPlugin extends Plugin
* @param msgSource source of status message for successful export
*/
void exportToolConfig(ToolTemplate template, String msgSource) {
- try {
- GhidraFileChooser fileChooser = getFileChooser(template);
- File selectedFile = fileChooser.getSelectedFile(true);
- if (selectedFile == null) { // user cancelled
- return;
- }
+ ToolServices services = activeProject.getToolServices();
- Preferences.setProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY,
- selectedFile.getParent());
- if (selectedFile.exists()) {
- int result = OptionDialog.showOptionDialog(tool.getToolFrame(), "Overwrite?",
- "File exists. Do you want to overwrite?", "Yes", "No",
- OptionDialog.QUESTION_MESSAGE);
-
- // the user chose not to overwrite the file, ask them to choose again
- // if the user hit yes, then it will be handled below
- if (result == OptionDialog.OPTION_TWO) {
- exportToolConfig(template, msgSource);
- return;
- }
+ try {
+ File savedFile = services.exportTool(template);
+ if (savedFile != null) {
+ Msg.info(this, msgSource + ": Successfully exported " + template.getName() +
+ " to " + savedFile.getAbsolutePath());
}
-
- exportToolTemplate(selectedFile, template, msgSource);
- }
- catch (IOException e) {
- Msg.showError(this, tool.getToolFrame(), "Error Exporting Tool",
- "Error exporting tool: " + e.getMessage(), e);
}
catch (Exception e) {
- Msg.showError(this, null, "Error", "Error exporting tool", e);
- }
- }
-
- private GhidraFileChooser getFileChooser(ToolTemplate template) {
- if (exportFileChooser == null) {
- exportFileChooser = new GhidraFileChooser(tool.getToolFrame());
- exportFileChooser.setFileFilter(
- new ExtensionFileFilter(ToolConstants.TOOL_HELP_TOPIC, "Tools"));
- exportFileChooser.setApproveButtonText("Export");
-
- // always prefer the last export directory...
- String exportDir = Preferences.getProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY);
- if (exportDir != null) {
- exportFileChooser.setCurrentDirectory(new File(exportDir));
- }
- else {
- // ...then try the import directory
- String importDir = Preferences.getProperty(Preferences.LAST_TOOL_IMPORT_DIRECTORY);
- if (importDir != null) {
- exportFileChooser.setCurrentDirectory(new File(importDir));
- }
- }
+ Msg.showError(this, null, "Error", "Error exporting tool tool", e);
}
-
- exportFileChooser.setTitle("Export " + template.getName());
- return exportFileChooser;
- }
-
- /**
- * Export the tool.
- */
- private void exportToolTemplate(File location, ToolTemplate template, String msgSource)
- throws FileNotFoundException, IOException {
-
- String filename = location.getName();
- if (filename.endsWith(".tool")) {
- filename = filename.substring(0, filename.length() - 5);
- }
-
- FileOutputStream f =
- new FileOutputStream(location.getParent() + File.separator + filename + ".tool");
- BufferedOutputStream bf = new BufferedOutputStream(f);
-
- Document doc = new Document(template.saveToXml());
-
- XMLOutputter xmlout = new GenericXMLOutputter();
- xmlout.output(doc, bf);
-
- bf.close();
-
- Msg.info(this, msgSource + ": Successfully exported " + template.getName() + " to " +
- location.getAbsolutePath());
}
private void updateConnectionPanel(Project project) {
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/ToolActionManager.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/ToolActionManager.java
index f4839571ca..90d9398a5c 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/ToolActionManager.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/ToolActionManager.java
@@ -48,7 +48,7 @@ class ToolActionManager implements ToolChestChangeListener {
private static final String MENU_ITEM_RUN_TOOL = "&Run Tool"; // Group: BTools
private static final String MENU_ITEM_DELETE_TOOL = "Delete Tool"; // Group: CTools
private static final String MENU_ITEM_IMPORT_TOOL = "&Import Tool..."; // Group: DTools
- private static final String MENU_ITEM_IMPORT_DEFTOOLS = "Import &Default Tools...";
+ private static final String MENU_ITEM_IMPORT_DEFAULT_TOOLS = "Import &Default Tools...";
private static final String MENU_ITEM_EXPORT_TOOL = "&Export Tool";
private static final String MENU_ITEM_CONNECT_TOOLS = "Connect &Tools..."; // Group: ETools
private static final String MENU_ITEM_SET_DEFAULT_TOOL = "&Set As Default"; // Group: FTools
@@ -80,9 +80,6 @@ class ToolActionManager implements ToolChestChangeListener {
createActions();
}
- /**
- * Enable actions according to enabled param.
- */
void enableActions(boolean enabled) {
createToolAction.setEnabled(enabled);
enableConnectTools();
@@ -95,9 +92,6 @@ class ToolActionManager implements ToolChestChangeListener {
setToolAssociationsAction.setEnabled(enabled);
}
- /**
- * Update the tool connection dialog.
- */
void updateConnectionDialog() {
if (toolConnectionDialog != null) {
toolConnectionDialog.updateDisplay();
@@ -105,9 +99,6 @@ class ToolActionManager implements ToolChestChangeListener {
enableConnectTools();
}
- /**
- * Set the active project; update tool menus.
- */
void setActiveProject(Project activeProject) {
if (toolConnectionDialog != null) {
if (activeProject != null) {
@@ -133,7 +124,8 @@ class ToolActionManager implements ToolChestChangeListener {
createToolAction.setMenuBarData(new MenuData(
new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_CREATE_TOOL }, null, "ATools"));
createToolAction.setEnabled(false);
- createToolAction.setHelpLocation(new HelpLocation("Tool", "Create_Tool"));
+ createToolAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Create_Tool"));
tool.addAction(createToolAction);
@@ -145,7 +137,8 @@ class ToolActionManager implements ToolChestChangeListener {
};
importAction.setMenuBarData(new MenuData(
new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_IMPORT_TOOL }, null, "DTools"));
- importAction.setHelpLocation(new HelpLocation("Tool", "Import Tool"));
+ importAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Import Tool"));
importAction.setEnabled(false);
tool.addAction(importAction);
@@ -156,9 +149,11 @@ class ToolActionManager implements ToolChestChangeListener {
addDefaultTools();
}
};
- importDefaultToolsAction.setMenuBarData(new MenuData(
- new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_IMPORT_DEFTOOLS }, null, "DTools"));
- importDefaultToolsAction.setHelpLocation(new HelpLocation("Tool", "Import Ghidra Tools"));
+ importDefaultToolsAction.setMenuBarData(
+ new MenuData(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_IMPORT_DEFAULT_TOOLS },
+ null, "DTools"));
+ importDefaultToolsAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Import Ghidra Tools"));
importDefaultToolsAction.setEnabled(false);
tool.addAction(importDefaultToolsAction);
@@ -185,7 +180,7 @@ class ToolActionManager implements ToolChestChangeListener {
tool.addAction(setToolAssociationsAction);
setToolAssociationsAction.setHelpLocation(
- new HelpLocation("Tool", "Set Tool Associations"));
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Set Tool Associations"));
tool.setMenuGroup(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_RUN_TOOL }, "BTools");
tool.setMenuGroup(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_DELETE_TOOL },
@@ -222,12 +217,6 @@ class ToolActionManager implements ToolChestChangeListener {
}
}
- /**
- * Add default tool to the project tool chest.
- *
- * @param filename tool template filename
- * @param toolName tool name
- */
private void addDefaultTool(String filename) {
try {
InputStream is = ResourceManager.getResourceAsStream(filename);
@@ -238,12 +227,6 @@ class ToolActionManager implements ToolChestChangeListener {
}
}
- /**
- * Enable/disable actions in the given map.
- *
- * @param map
- * @param enabled
- */
private void enableActions(Map<String, DockingAction> map, boolean enabled) {
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
@@ -253,11 +236,6 @@ class ToolActionManager implements ToolChestChangeListener {
}
}
- /**
- * Rebuild the tool menus.
- *
- * @param activeProject
- */
private void populateToolMenus(Project activeProject) {
removeActions(runToolActionMap);
removeActions(delToolActionMap);
@@ -280,9 +258,6 @@ class ToolActionManager implements ToolChestChangeListener {
}
}
- /**
- * Remove actions in the given map.
- */
private void removeActions(Map<String, DockingAction> map) {
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
@@ -402,7 +377,7 @@ class ToolActionManager implements ToolChestChangeListener {
};
action.setMenuBarData(new MenuData(
new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_RUN_TOOL }, null, "BTools"));
- action.setHelpLocation(new HelpLocation("Tool", "Run Tool"));
+ action.setHelpLocation(new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Run Tool"));
action.setEnabled(false);
tool.addAction(action);
@@ -416,7 +391,7 @@ class ToolActionManager implements ToolChestChangeListener {
};
action.setMenuBarData(new MenuData(
new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_DELETE_TOOL }, null, "CTools"));
- action.setHelpLocation(new HelpLocation("Tool", "Delete Tool"));
+ action.setHelpLocation(new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Delete Tool"));
action.setEnabled(false);
tool.addAction(action);
@@ -430,7 +405,7 @@ class ToolActionManager implements ToolChestChangeListener {
};
action.setMenuBarData(new MenuData(
new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_EXPORT_TOOL }, null, "DTools"));
- action.setHelpLocation(new HelpLocation("Tool", "Export Tool"));
+ action.setHelpLocation(new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Export Tool"));
action.setEnabled(false);
exportToolActionMap.put(action.getName(), action);
@@ -519,7 +494,7 @@ class ToolActionManager implements ToolChestChangeListener {
runAction.setMenuBarData(
new MenuData(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_RUN_TOOL, toolName },
null, "BTools"));
- runAction.setHelpLocation(new HelpLocation("Tool", "Run_Tool"));
+ runAction.setHelpLocation(new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Run_Tool"));
runToolActionMap.put(toolName, runAction);
tool.addAction(runAction);
@@ -539,12 +514,13 @@ class ToolActionManager implements ToolChestChangeListener {
deleteAction.setMenuBarData(
new MenuData(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_DELETE_TOOL, toolName },
null, "CTools"));
- deleteAction.setHelpLocation(new HelpLocation("Tool", "Delete_Tool"));
+ deleteAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Delete_Tool"));
delToolActionMap.put(toolName, deleteAction);
tool.addAction(deleteAction);
- ToolAction exportAction = new ToolAction(toolName, "Export_Tool") {
+ ToolAction exportToolAction = new ToolAction(toolName, "Export_Tool") {
@Override
public void actionPerformed(ActionContext context) {
String name = getName();
@@ -552,14 +528,15 @@ class ToolActionManager implements ToolChestChangeListener {
plugin.exportToolConfig(toolChest.getToolTemplate(name), "Tool Menu");
}
};
- exportAction.setEnabled(true);
- exportAction.setMenuBarData(
+ exportToolAction.setEnabled(true);
+ exportToolAction.setMenuBarData(
new MenuData(new String[] { ToolConstants.MENU_TOOLS, MENU_ITEM_EXPORT_TOOL, toolName },
null, "DTools"));
- exportAction.setHelpLocation(new HelpLocation("Tool", "Export_Tool"));
+ exportToolAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Export_Tool"));
- exportToolActionMap.put(toolName, exportAction);
- tool.addAction(exportAction);
+ exportToolActionMap.put(toolName, exportToolAction);
+ tool.addAction(exportToolAction);
}
/////////////////////////////////////////////////////////////////////
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/model/ToolServices.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/model/ToolServices.java
index 64182ee132..76f82be5a8 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/model/ToolServices.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/model/ToolServices.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +15,11 @@
*/
package ghidra.framework.model;
-import ghidra.framework.plugintool.PluginEvent;
-
import java.io.*;
import java.util.Set;
+import ghidra.framework.plugintool.PluginEvent;
+
/**
* Services that the Tool uses.
*/
@@ -47,13 +46,12 @@ public interface ToolServices {
/**
* Save the tool to the given location on the local file system.
*
- * @param location file to export tool description into
- * @param tool tool to export
- *
+ * @param tool the tool template to write
+ * @return the file to which the tool was saved
* @throws FileNotFoundException thrown if the file's directory doesn't exist.
* @throws IOException thrown if there is an error writing the file.
*/
- public void exportTool(File location, Tool tool) throws FileNotFoundException, IOException;
+ public File exportTool(ToolTemplate tool) throws FileNotFoundException, IOException;
/**
* Get the tool chest for the project.
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/PluginTool.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/PluginTool.java
index 91d89fdaa9..94b136af0d 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/PluginTool.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/PluginTool.java
@@ -15,8 +15,7 @@
*/
package ghidra.framework.plugintool;
-import static ghidra.framework.model.ToolTemplate.TOOL_INSTANCE_NAME_XML_NAME;
-import static ghidra.framework.model.ToolTemplate.TOOL_NAME_XML_NAME;
+import static ghidra.framework.model.ToolTemplate.*;
import java.awt.*;
import java.awt.event.KeyEvent;
@@ -102,7 +101,6 @@ public abstract class PluginTool extends AbstractDockingTool implements Tool, Se
protected ToolIconURL iconURL = new ToolIconURL("view_detailed.png");
- private DockingAction exportToolAction;
private ToolServices toolServices;
private boolean isConfigurable = true;
@@ -974,21 +972,35 @@ public abstract class PluginTool extends AbstractDockingTool implements Tool, Se
}
protected void addExportToolAction() {
- exportToolAction = new DockingAction("Export Tool", ToolConstants.TOOL_OWNER) {
- @Override
- public void actionPerformed(ActionContext context) {
- dialogMgr.exportTool();
- }
- };
+ DockingAction exportToolAction =
+ new DockingAction("Export Tool", ToolConstants.TOOL_OWNER) {
+ @Override
+ public void actionPerformed(ActionContext context) {
+ dialogMgr.exportTool();
+ }
+ };
MenuData menuData =
new MenuData(new String[] { ToolConstants.MENU_FILE, "Export Tool..." }, null, "Tool");
menuData.setMenuSubGroup("3Tool");
exportToolAction.setMenuBarData(menuData);
- exportToolAction.setEnabled(true);
exportToolAction.setHelpLocation(
- new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Export Tool"));
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Export_Tool"));
addAction(exportToolAction);
+
+ DockingAction exportDefautToolAction =
+ new DockingAction("Export Default Tool", ToolConstants.TOOL_OWNER) {
+ @Override
+ public void actionPerformed(ActionContext e) {
+ dialogMgr.exportDefaultTool();
+ }
+ };
+ exportDefautToolAction.setMenuBarData(new MenuData(
+ new String[] { ToolConstants.MENU_TOOLS, "Export Default Tool..." }, null, "Tool"));
+ exportDefautToolAction.setHelpLocation(
+ new HelpLocation(ToolConstants.TOOL_HELP_TOPIC, "Export_Default_Tool"));
+
+ addAction(exportDefautToolAction);
}
protected void addHelpActions() {
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/ToolServicesAdapter.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/ToolServicesAdapter.java
index bad897d188..144a4b8005 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/ToolServicesAdapter.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/ToolServicesAdapter.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +15,11 @@
*/
package ghidra.framework.plugintool;
-import ghidra.framework.model.*;
-
import java.io.*;
import java.util.Set;
+import ghidra.framework.model.*;
+
public class ToolServicesAdapter implements ToolServices {
@Override
@@ -43,8 +42,8 @@ public class ToolServicesAdapter implements ToolServices {
}
@Override
- public void exportTool(File location, Tool tool) throws FileNotFoundException, IOException {
- // override
+ public File exportTool(ToolTemplate tool) throws FileNotFoundException, IOException {
+ return null;
}
@Override
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/mgr/DialogManager.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/mgr/DialogManager.java
index 6d0964eefa..8ef0f3d20a 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/mgr/DialogManager.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/mgr/DialogManager.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,18 +15,17 @@
*/
package ghidra.framework.plugintool.mgr;
-import ghidra.framework.plugintool.PluginTool;
-import ghidra.framework.plugintool.dialog.SaveToolConfigDialog;
-import ghidra.framework.preferences.Preferences;
-import ghidra.util.Msg;
-import ghidra.util.filechooser.GhidraFileChooserModel;
-import ghidra.util.filechooser.GhidraFileFilter;
-
import java.io.File;
-import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
import docking.widgets.OptionDialog;
import docking.widgets.filechooser.GhidraFileChooser;
+import ghidra.framework.model.*;
+import ghidra.framework.plugintool.PluginTool;
+import ghidra.framework.plugintool.dialog.SaveToolConfigDialog;
+import ghidra.util.Msg;
/**
* Helper class to manage actions for saving and exporting the tool.
@@ -57,81 +55,55 @@ public class DialogManager {
}
/**
- * Write this tool to a filename; the user is prompted for
- * a filename.
+ * Write our tool to a filename; the user is prompted for a filename
*/
public void exportTool() {
- GhidraFileChooser fileChooser = getFileChooser();
-
- File exportFile = null;
- while (exportFile == null) {
- exportFile = fileChooser.getSelectedFile(); // show the chooser
- if (exportFile == null) {
- return; // user cancelled
- }
-
- Preferences.setProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY, exportFile.getParent());
- if (!exportFile.getName().endsWith(".tool")) {
- exportFile = new File(exportFile.getAbsolutePath() + ".tool");
- }
- if (exportFile.exists()) {
- int result =
- OptionDialog.showOptionDialog(tool.getToolFrame(), "Overwrite?",
- "Overwrite existing file, " + exportFile.getName() + "?", "Overwrite",
- OptionDialog.QUESTION_MESSAGE);
- if (result != OptionDialog.OPTION_ONE) {
- exportFile = null; // user chose not to overwrite
- }
- }
- }
+ ToolTemplate template = tool.getToolTemplate(true);
+ exportTool(template);
+ }
+ private void exportTool(ToolTemplate template) {
+ ToolServices services = tool.getProject().getToolServices();
try {
- tool.getProject().getToolServices().exportTool(exportFile, tool);
- Msg.info(this, "Successfully exported " + tool.getName() + " to " +
- exportFile.getAbsolutePath());
- }
- catch (IOException e) {
- Msg.showError(this, tool.getToolFrame(), "Error Exporting Tool",
- "Error exporting tool: " + e.getMessage(), e);
+ File savedFile = services.exportTool(template);
+ if (savedFile != null) {
+ Msg.info(this, "Successfully exported " + tool.getName() + " to " +
+ savedFile.getAbsolutePath());
+ }
}
catch (Exception e) {
Msg.showError(this, null, "Error", "Error exporting tool tool", e);
}
-
}
- private GhidraFileChooser getFileChooser() {
- if (ghidraFileChooser != null) {
- return ghidraFileChooser; // lazy inited
+ /**
+ * Exports a version of our tool without any config settings. This is useful for making a
+ * new 'default' tool to be shared with others, which will not contain any user settings.
+ */
+ public void exportDefaultTool() {
+
+ Project project = tool.getProject();
+ ToolChest toolChest = project.getLocalToolChest();
+ ToolTemplate[] templates = toolChest.getToolTemplates();
+ List<String> namesList =
+ Arrays.stream(templates).map(t -> t.getName()).collect(Collectors.toList());
+ if (namesList.isEmpty()) {
+ Msg.showInfo(this, null, "No Tools", "There are no tools to export");
+ return;
}
- GhidraFileChooser newFileChooser = new GhidraFileChooser(tool.getToolFrame());
- newFileChooser.setFileFilter(new GhidraFileFilter() {
- public boolean accept(File file, GhidraFileChooserModel model) {
- if (file == null) {
- return false;
- }
-
- if (file.isDirectory()) {
- return true;
- }
-
- return file.getAbsolutePath().toLowerCase().endsWith("tool");
- }
-
- public String getDescription() {
- return "Tools";
- }
- });
-
- String exportDir = Preferences.getProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY);
- if (exportDir != null) {
- newFileChooser.setCurrentDirectory(new File(exportDir));
+ String[] names = namesList.toArray(new String[namesList.size()]);
+ String choice = OptionDialog.showInputChoiceDialog(null, "Choose Tool", "Choose Tool",
+ names, null, OptionDialog.QUESTION_MESSAGE);
+ if (choice == null) {
+ return;
}
- newFileChooser.setTitle("Export Tool");
- newFileChooser.setApproveButtonText("Export");
+ ToolTemplate template = toolChest.getToolTemplate(choice);
+ Tool templateTool = template.createTool(project);
+ ToolTemplate defaultTemplate = templateTool.getToolTemplate(false);
- return newFileChooser;
+ exportTool(defaultTemplate);
}
+
}
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/project/tool/ToolServicesImpl.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/project/tool/ToolServicesImpl.java
index 2656eda05e..6a0dd00348 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/project/tool/ToolServicesImpl.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/project/tool/ToolServicesImpl.java
@@ -21,6 +21,8 @@ import java.util.*;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;
+import docking.widgets.OptionDialog;
+import docking.widgets.filechooser.GhidraFileChooser;
import ghidra.framework.ToolUtils;
import ghidra.framework.data.ContentHandler;
import ghidra.framework.data.DomainObjectAdapter;
@@ -30,6 +32,8 @@ import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.preferences.Preferences;
import ghidra.util.Msg;
import ghidra.util.classfinder.ClassSearcher;
+import ghidra.util.filechooser.GhidraFileChooserModel;
+import ghidra.util.filechooser.GhidraFileFilter;
import ghidra.util.xml.GenericXMLOutputter;
/**
@@ -61,30 +65,87 @@ class ToolServicesImpl implements ToolServices {
}
@Override
- public void exportTool(File location, Tool tool) throws FileNotFoundException, IOException {
+ public File exportTool(ToolTemplate tool) throws FileNotFoundException, IOException {
+
+ File location = chooseToolFile(tool);
+ if (location == null) {
+ return location; // user cancelled
+ }
String filename = location.getName();
- if (filename.endsWith(".tool")) {
- filename = filename.substring(0, filename.length() - 5);
+ if (!filename.endsWith(ToolUtils.TOOL_EXTENSION)) {
+ filename = filename + ToolUtils.TOOL_EXTENSION;
}
- FileOutputStream f =
- new FileOutputStream(location.getParent() + File.separator + filename + ".tool");
- BufferedOutputStream bf = new BufferedOutputStream(f);
+ try (FileOutputStream f =
+ new FileOutputStream(location.getParent() + File.separator + filename)) {
+ BufferedOutputStream bf = new BufferedOutputStream(f);
+ Document doc = new Document(tool.saveToXml());
+ XMLOutputter xmlout = new GenericXMLOutputter();
+ xmlout.output(doc, bf);
+ }
- ToolTemplate template = tool.getToolTemplate(false);
+ return location;
+ }
- Document doc = new Document(template.saveToXml());
+ private File chooseToolFile(ToolTemplate tool) {
+ GhidraFileChooser fileChooser = getFileChooser();
- XMLOutputter xmlout = new GenericXMLOutputter();
- try {
- xmlout.output(doc, bf);
+ File exportFile = null;
+ while (exportFile == null) {
+ exportFile = fileChooser.getSelectedFile(); // show the chooser
+ if (exportFile == null) {
+ return null; // user cancelled
+ }
+
+ Preferences.setProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY, exportFile.getParent());
+ if (!exportFile.getName().endsWith(ToolUtils.TOOL_EXTENSION)) {
+ exportFile = new File(exportFile.getAbsolutePath() + ToolUtils.TOOL_EXTENSION);
+ }
+ if (exportFile.exists()) {
+ int result = OptionDialog.showOptionDialog(null, "Overwrite?",
+ "Overwrite existing file, " + exportFile.getName() + "?", "Overwrite",
+ OptionDialog.QUESTION_MESSAGE);
+ if (result != OptionDialog.OPTION_ONE) {
+ exportFile = null; // user chose not to overwrite
+ }
+ }
}
- catch (IOException ioe) {
- Msg.error(this, "Unexpected exception exporting tool", ioe);
+
+ return exportFile;
+ }
+
+ private GhidraFileChooser getFileChooser() {
+ GhidraFileChooser newFileChooser = new GhidraFileChooser(null);
+ newFileChooser.setFileFilter(new GhidraFileFilter() {
+ @Override
+ public boolean accept(File file, GhidraFileChooserModel model) {
+ if (file == null) {
+ return false;
+ }
+
+ if (file.isDirectory()) {
+ return true;
+ }
+
+ return file.getAbsolutePath().toLowerCase().endsWith("tool");
+ }
+
+ @Override
+ public String getDescription() {
+ return "Tools";
+ }
+ });
+
+ String exportDir = Preferences.getProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY);
+ if (exportDir != null) {
+ newFileChooser.setCurrentDirectory(new File(exportDir));
}
- f.close();
+ newFileChooser.setTitle("Export Tool");
+ newFileChooser.setApproveButtonText("Export");
+
+ return newFileChooser;
}
@Override
diff --git a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/framework/main/ToolActionManagerTest.java b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/framework/main/ToolActionManagerTest.java
index 252a62741d..cae30ebb12 100644
--- a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/framework/main/ToolActionManagerTest.java
+++ b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/framework/main/ToolActionManagerTest.java
@@ -39,6 +39,7 @@ import docking.widgets.tree.GTreeRootNode;
import docking.wizard.WizardManager;
import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;
import ghidra.framework.GenericRunInfo;
+import ghidra.framework.ToolUtils;
import ghidra.framework.data.ContentHandler;
import ghidra.framework.main.datatree.DataTree;
import ghidra.framework.model.*;
@@ -172,8 +173,8 @@ public class ToolActionManagerTest extends AbstractGhidraHeadedIntegrationTest {
int count = tc.getToolCount();
String toolNamePrefix = "TestCodeBrowser";
- final File cbFile =
- ResourceManager.getResourceFile("defaultTools/" + toolNamePrefix + ".tool");
+ final File cbFile = ResourceManager.getResourceFile(
+ "defaultTools/" + toolNamePrefix + ToolUtils.TOOL_EXTENSION);
assertNotNull(cbFile);
DockingActionIf importAction = getAction("Import Tool");