Class GhidraScriptUtil


  • public class GhidraScriptUtil
    extends java.lang.Object
    A utility class for managing script directories and ScriptInfo objects.
    • Field Detail

      • USER_SCRIPTS_DIR

        public static java.lang.String USER_SCRIPTS_DIR
        User's home scripts directory
      • USER_SCRIPTS_BIN_DIR

        public static java.lang.String USER_SCRIPTS_BIN_DIR
        The default compile output directory
    • Constructor Detail

      • GhidraScriptUtil

        public GhidraScriptUtil()
    • Method Detail

      • refreshRequested

        public static void refreshRequested()
        Stores the time of the refresh request so that clients may later ask when the last refresh took place.
      • getLastRefreshRequestTimestamp

        public static long getLastRefreshRequestTimestamp()
      • getDefaultScriptDirectories

        public static java.util.List<Path> getDefaultScriptDirectories()
        Returns a list of the default script directories.
        Returns:
        a list of the default script directories
      • isSystemScriptPath

        public static boolean isSystemScriptPath​(ResourceFile file)
        Determine if the specified file is contained within the Ghidra installation.
        Parameters:
        file - script file or directory
        Returns:
        true if file contained within Ghidra installation area
      • isSystemFile

        public static boolean isSystemFile​(ResourceFile file)
        Determine if the specified file is contained within the Ghidra installation.
        Parameters:
        file - - file or directory to check
        Returns:
        true if file is contained within Ghidra application root.
      • getScriptSourceDirectories

        public static java.util.List<ResourceFile> getScriptSourceDirectories()
        Returns a list of the current script directories.
        Returns:
        a list of the current script directories
      • setScriptDirectories

        public static void setScriptDirectories​(java.util.List<Path> newPaths)
        Sets the script directories to the new paths.
        Parameters:
        newPaths - the new script directories
      • getScriptPath

        public static Path getScriptPath​(ResourceFile directory)
        Returns the PATH for the specified directory.
        Parameters:
        directory - the directory
        Returns:
        the path for the specified directory
      • getScriptCompileOutputDirectory

        public static ResourceFile getScriptCompileOutputDirectory​(ResourceFile scriptFile)
        Returns the output directory to which the given script file's generated .class file should be written
        Parameters:
        scriptFile - the script file
        Returns:
        the directory
      • clean

        public static void clean()
        Deletes all script class files.
      • getBaseName

        public static java.lang.String getBaseName​(ResourceFile script)
        Returns the base name give a script file. For example, given "C:\Temp\SomeClass.java", it will return "SomeClass".
        Parameters:
        script - the script
        Returns:
        the base name
      • contains

        public static boolean contains​(ResourceFile scriptFile)
        Returns true if a ScriptInfo object exists for the specified script file.
        Parameters:
        scriptFile - the script file
        Returns:
        true if a ScriptInfo object exists
      • unloadScript

        public static void unloadScript​(ResourceFile scriptFile)
        Removes the ScriptInfo object for the specified file
        Parameters:
        scriptFile - the script file
      • getScriptInfoIterator

        public static java.util.Iterator<ScriptInfo> getScriptInfoIterator()
        Returns an iterator over all script info objects.
        Returns:
        an iterator over all script info objects
      • getScriptInfo

        public static ScriptInfo getScriptInfo​(ResourceFile scriptFile)
        Returns the script info object for the specified script file
        Parameters:
        scriptFile - the script file
        Returns:
        the script info object for the specified script file
      • getProviders

        public static java.util.List<GhidraScriptProvider> getProviders()
        Returns a list of all Ghidra script providers
        Returns:
        a list of all Ghidra script providers
      • getProvider

        public static GhidraScriptProvider getProvider​(ResourceFile scriptFile)
        Returns the corresponding Ghidra script providers for the specified script file.
        Parameters:
        scriptFile - the script file
        Returns:
        the Ghidra script provider
      • hasScriptProvider

        public static boolean hasScriptProvider​(ResourceFile scriptFile)
        Returns true if a provider exists that can process the specified file.
        Parameters:
        scriptFile - the script file
        Returns:
        true if a provider exists that can process the specified file
      • createNewScript

        public static ResourceFile createNewScript​(GhidraScriptProvider provider,
                                                   ResourceFile parentDirectory,
                                                   java.util.List<Path> scriptDirectories)
                                            throws java.io.IOException
        Creates a new script with a unique name using the specified provider in the specified directory.
        Parameters:
        provider - the Ghidra script provider
        parentDirectory - the directory where the new script will be created.
        scriptDirectories - The list of directories containing scripts (used to find a unique name).
        Returns:
        the newly created script file
        Throws:
        java.io.IOException - if an i/o error occurs
      • findScriptByName

        public static ScriptInfo findScriptByName​(java.lang.String name)
        Uses the given name to find a matching script. This method only works because of the limitation that all script names in Ghidra must be unique. If the given name has multiple script matches, then a warning will be logged.
        Parameters:
        name - The name for which to find a script
        Returns:
        The ScriptInfo that has the given name
      • getAllScripts

        public static java.util.List<ResourceFile> getAllScripts()
      • alreadyExists

        public static boolean alreadyExists​(java.lang.String scriptName)
        Looks through all of the current ScriptInfos to see if one already exists with the given name.
        Parameters:
        scriptName - The name to check
        Returns:
        true if the name is not taken by an existing ScriptInfo.
      • getExistingScriptInfo

        public static ScriptInfo getExistingScriptInfo​(java.lang.String scriptName)
        Returns the existing script info for the given name. The script environment limits scripts such that names are unique. If this method returns a non-null value, then the name given name is taken.
        Parameters:
        scriptName - the name of the script for which to get a ScriptInfo
        Returns:
        a ScriptInfo matching the given name; null if no script by that name is known to the script manager
      • runScript

        public static boolean runScript​(GhidraState scriptState,
                                        GhidraScript script,
                                        java.io.PrintWriter writer,
                                        java.lang.Object originator,
                                        TaskMonitor monitor)
        Runs the specified script with the specified state
        Parameters:
        scriptState - state representing environment variables that the script is able to access
        script - Script to be run
        writer - the writer to which warning and error messages will be written
        originator - the client class requesting the script run; used for logging
        monitor - the task monitor
        Returns:
        whether the script successfully completed running
      • refreshDuplicates

        public static void refreshDuplicates()
        Updates every known script's duplicate value.