GP-1743 - Added a method to GhiraScript to allow script writers to

disable reusing previously chosen values in the various 'ask' methods.
This commit is contained in:
dragonmacher 2022-02-11 18:47:10 -05:00
parent 7a5f0b4e16
commit ea52da673e
2 changed files with 90 additions and 43 deletions

View file

@ -140,6 +140,7 @@ public abstract class GhidraScript extends FlatProgramAPI {
// Stores any parameters in a .properties file sharing the same base name as this GhidraScript // Stores any parameters in a .properties file sharing the same base name as this GhidraScript
protected GhidraScriptProperties propertiesFileParams; protected GhidraScriptProperties propertiesFileParams;
protected List<ResourceFile> potentialPropertiesFileLocs = new ArrayList<>(); protected List<ResourceFile> potentialPropertiesFileLocs = new ArrayList<>();
private boolean reusePreviousChoices = true;
private CodeUnitFormat cuFormat; private CodeUnitFormat cuFormat;
// Stores any script-specific arguments // Stores any script-specific arguments
@ -193,6 +194,25 @@ public abstract class GhidraScript extends FlatProgramAPI {
loadVariablesFromState(); loadVariablesFromState();
} }
/**
* Sets whether the user's previously selected values should be used when showing the various
* {@code ask} methods. This is true by default, meaning that previous choices will be shown
* instead of any provided default value.
* @param reuse true to reuse values; false to not reuse previous values
*/
public void setReusePreviousChoices(boolean reuse) {
this.reusePreviousChoices = reuse;
}
/**
* Returns whether scripts will reuse previously selected values when showing the various
* {@code ask} methods.
* @return true to reuse values; false to not reuse previous values
*/
public boolean getReusePreviousChoices() {
return reusePreviousChoices;
}
/** /**
* Execute/run script and {@link #doCleanup} afterwards. * Execute/run script and {@link #doCleanup} afterwards.
* *
@ -1954,7 +1974,7 @@ public abstract class GhidraScript extends FlatProgramAPI {
Map<Class<?>, Object> map = getScriptMap(key1, key2); Map<Class<?>, Object> map = getScriptMap(key1, key2);
T mappedValue = null; T mappedValue = null;
if (clazz != null) { if (clazz != null && reusePreviousChoices) {
mappedValue = (T) map.get(clazz); mappedValue = (T) map.get(clazz);
} }

View file

@ -622,13 +622,40 @@ public class GhidraScriptAskMethodsTest extends AbstractGhidraHeadedIntegrationT
createScript(); createScript();
final String defaultValue = "a default value"; String defaultValue = "a default value";
String myString = ask_TextInput(() -> { String myString = ask_TextInput(() -> {
return script.askString("Default Test", "Enter a string here:", defaultValue); return script.askString("Default Test", "Enter a string here:", defaultValue);
}); });
assertEquals(defaultValue, myString); assertEquals(defaultValue, myString);
} }
@Test
public void testAskStringDefaultValue_DoNotReusePreviousValues() throws Exception {
createScript();
String defaultValue = "a default value";
String myString = ask_TextInput(() -> {
return script.askString("Default Test", "Enter a string here:", defaultValue);
});
assertEquals(defaultValue, myString);
script.setReusePreviousChoices(false);
String secondDefaultValue = "a new default value";
String secondString = ask_TextInput(() -> {
return script.askString("Default Test", "Enter a string here:", secondDefaultValue);
});
assertEquals(secondDefaultValue, secondString);
script.setReusePreviousChoices(true);
String thirdDefaultValue = "a third default value";
String thirdString = ask_TextInput(() -> {
return script.askString("Default Test", "Enter a string here:", thirdDefaultValue);
});
assertEquals(secondString, thirdString);
}
@Test @Test
public void testAskChoice() throws Exception { public void testAskChoice() throws Exception {
createScript(); createScript();