Skip to content

Commit

Permalink
let snippets run without a bv context
Browse files Browse the repository at this point in the history
  • Loading branch information
psifertex committed Nov 6, 2020
1 parent b019eaa commit 5f0bec5
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ def actionFromSnippet(snippetName, snippetDescription):
else:
return "Snippets\\" + snippetDescription


def executeSnippet(code, context):
def setupGlobals(context):
snippetGlobals = {}
if context.binaryView == None:
dock = DockHandler.getActiveDockHandler()
if not dock:
log_error("Snippet triggered with no context and no dock handler. This should not happen. Please report reproduction steps if possible.")
return
return snippetGlobals
viewFrame = dock.getViewFrame()
if not viewFrame:
log_error("Snippet triggered with no context and no view frame. Snippets require at least one open binary.")
return
return snippetGlobals
viewInterface = viewFrame.getCurrentViewInterface()
context.binaryView = viewInterface.getData()
snippetGlobals['current_view'] = context.binaryView
Expand Down Expand Up @@ -109,12 +106,17 @@ def executeSnippet(code, context):
else:
snippetGlobals['current_selection'] = None
snippetGlobals['uicontext'] = context
return snippetGlobals


def executeSnippet(code, context):
snippetGlobals = setupGlobals(context)

exec("from binaryninja import *", snippetGlobals)
exec(code, snippetGlobals)
if snippetGlobals['here'] != context.address:
if hasattr(snippetGlobals, "here") and snippetGlobals['here'] != context.address:
context.binaryView.file.navigate(context.binaryView.file.view, snippetGlobals['here'])
if snippetGlobals['current_address'] != context.address:
if hasattr(snippetGlobals, "current_address") and snippetGlobals['current_address'] != context.address:
context.binaryView.file.navigate(context.binaryView.file.view, snippetGlobals['current_address'])


Expand Down

0 comments on commit 5f0bec5

Please sign in to comment.