Skip to content

Commit 5f0bec5

Browse files
committed
let snippets run without a bv context
1 parent b019eaa commit 5f0bec5

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,15 @@ def actionFromSnippet(snippetName, snippetDescription):
6262
else:
6363
return "Snippets\\" + snippetDescription
6464

65-
66-
def executeSnippet(code, context):
65+
def setupGlobals(context):
6766
snippetGlobals = {}
6867
if context.binaryView == None:
6968
dock = DockHandler.getActiveDockHandler()
7069
if not dock:
71-
log_error("Snippet triggered with no context and no dock handler. This should not happen. Please report reproduction steps if possible.")
72-
return
70+
return snippetGlobals
7371
viewFrame = dock.getViewFrame()
7472
if not viewFrame:
75-
log_error("Snippet triggered with no context and no view frame. Snippets require at least one open binary.")
76-
return
73+
return snippetGlobals
7774
viewInterface = viewFrame.getCurrentViewInterface()
7875
context.binaryView = viewInterface.getData()
7976
snippetGlobals['current_view'] = context.binaryView
@@ -109,12 +106,17 @@ def executeSnippet(code, context):
109106
else:
110107
snippetGlobals['current_selection'] = None
111108
snippetGlobals['uicontext'] = context
109+
return snippetGlobals
110+
111+
112+
def executeSnippet(code, context):
113+
snippetGlobals = setupGlobals(context)
112114

113115
exec("from binaryninja import *", snippetGlobals)
114116
exec(code, snippetGlobals)
115-
if snippetGlobals['here'] != context.address:
117+
if hasattr(snippetGlobals, "here") and snippetGlobals['here'] != context.address:
116118
context.binaryView.file.navigate(context.binaryView.file.view, snippetGlobals['here'])
117-
if snippetGlobals['current_address'] != context.address:
119+
if hasattr(snippetGlobals, "current_address") and snippetGlobals['current_address'] != context.address:
118120
context.binaryView.file.navigate(context.binaryView.file.view, snippetGlobals['current_address'])
119121

120122

0 commit comments

Comments
 (0)