Skip to content

Commit

Permalink
Try fixing Binja API for deprecation/removals
Browse files Browse the repository at this point in the history
  • Loading branch information
Rowan Hart committed Nov 22, 2024
1 parent 88ceac7 commit a420dcb
Showing 1 changed file with 39 additions and 75 deletions.
114 changes: 39 additions & 75 deletions plugins/lighthouse/util/disassembler/binja_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,29 @@ def message(self, message):
#--------------------------------------------------------------------------

def register_dockable(self, dockable_name, create_widget_callback):
dock_handler = DockHandler.getActiveDockHandler()
dock_handler.addDockWidget(dockable_name, create_widget_callback, QtCore.Qt.RightDockWidgetArea, QtCore.Qt.Horizontal, False)
Sidebar.addSidebarWidgetType(LighthouseWidgetType())

def create_dockable_widget(self, parent, dockable_name):
return DockableWidget(parent, dockable_name)
# return DockableWidget(parent, dockable_name)
pass

def show_dockable(self, dockable_name):
dock_handler = DockHandler.getActiveDockHandler()
dock_handler.setVisible(dockable_name, True)
Sidebar.current().focus(LighthouseWidgetType())

def hide_dockable(self, dockable_name):
dock_handler = DockHandler.getActiveDockHandler()
dock_handler.setVisible(dockable_name, False)
# dock_handler = DockHandler.getActiveDockHandler()
# dock_handler.setVisible(dockable_name, False)
pass

#--------------------------------------------------------------------------
# XXX Binja Specfic Helpers
#--------------------------------------------------------------------------

def binja_get_bv_from_dock(self):
dh = DockHandler.getActiveDockHandler()
if not dh:
ac = UIContext.activeContext()
if not ac:
return None
vf = dh.getViewFrame()
vf = ac.getCurrentViewFrame()
if not vf:
return None
vi = vf.getCurrentViewInterface()
Expand All @@ -208,23 +208,16 @@ def busy(self):
#--------------------------------------------------------------------------

def get_current_address(self):

# TODO/V35: this doen't work because of the loss of context bug...
#ctx = UIContext.activeContext()
#ah = ctx.contentActionHandler()
#ac = ah.actionContext()
#return ac.address

dh = DockHandler.getActiveDockHandler()
if not dh:
ac = UIContext.activeContext()
if not ac:
return 0
vf = dh.getViewFrame()
if not vf:
v = ac.getCurrentView()
if not v:
return 0
ac = vf.actionContext()
if not ac:
actx = av.actionContext()
if not actx:
return 0
return ac.address
return actx.address

@BinjaCoreAPI.execute_read
def get_database_directory(self):
Expand Down Expand Up @@ -283,8 +276,8 @@ def navigate_to_function(self, function_address, address):
else:
return False

dh = DockHandler.getActiveDockHandler()
vf = dh.getViewFrame()
ac = UIContext.activeContext()
vf = ac.getCurrentViewFrame()
vi = vf.getCurrentViewInterface()

return vi.navigateToFunction(func, address)
Expand Down Expand Up @@ -364,60 +357,31 @@ def name_changed(self, address, name):
#------------------------------------------------------------------------------

if QT_AVAILABLE:

import binaryninjaui
from binaryninjaui import DockHandler, DockContextHandler, UIContext, UIActionHandler

class DockableWidget(QtWidgets.QWidget, DockContextHandler):
"""
A dockable Qt widget for Binary Ninja.
"""

def __init__(self, parent, name):
QtWidgets.QWidget.__init__(self, parent)
DockContextHandler.__init__(self, self, name)

class LighthouseWidget(SidebarWidget):
def __init__(self, name, frame, data):
SidebarWidget.__init__(self, name)
self.actionHandler = UIActionHandler()
self.actionHandler.setupActionHandler(self)

self._active_view = None
self._visible_for_view = collections.defaultdict(lambda: False)

@property
def visible(self):
return self._visible_for_view[self._active_view]
class LighthouseWidgetType(SidebarWidgetType):
def __init__(self):
icon = QImage(56, 56, QImage.Format_RGB32)
icon.fill(0)

@visible.setter
def visible(self, is_visible):
self._visible_for_view[self._active_view] = is_visible
p = QPainter()
p.begin(icon)
p.setFont(QFont("Open Sans", 56))
p.setPen(QColor(255, 255, 255, 255))
p.drawText(QRectF(0, 0, 56, 56), Qt.AlignCenter, "L")
p.end()

def shouldBeVisible(self, view_frame):
if not view_frame:
return False

if USING_PYSIDE6:
import shiboken6 as shiboken
else:
import shiboken2 as shiboken

vf_ptr = shiboken.getCppPointer(view_frame)[0]
return self._visible_for_view[vf_ptr]

def notifyVisibilityChanged(self, is_visible):
self.visible = is_visible

def notifyViewChanged(self, view_frame):
if not view_frame:
self._active_view = None
return
SidebarWidgetType.__init__(self, icon, "Lighthouse")

if USING_PYSIDE6:
import shiboken6 as shiboken
else:
import shiboken2 as shiboken
def createWidget(self, frame, data):
return Widget("Lighthouse", frame, data)

self._active_view = shiboken.getCppPointer(view_frame)[0]
def defaultLocation(self):
return SidebarWidgetLocation.RightContent

if self.visible:
dock_handler = DockHandler.getActiveDockHandler()
dock_handler.setVisible(self.m_name, True)
def contextSensitivity(self):
return SidebarContextSensitivity.SelfManagedSidebarContext

0 comments on commit a420dcb

Please sign in to comment.