Skip to content

Commit

Permalink
Try fixing Binja API for deprecation/removals
Browse files Browse the repository at this point in the history
Fixup imports

Fixup imports

Fixup imports

Fixup imports

Fixup imports

Fix address get variable name
  • Loading branch information
Rowan Hart committed Nov 22, 2024
1 parent 88ceac7 commit 5c0ba18
Showing 1 changed file with 44 additions and 77 deletions.
121 changes: 44 additions & 77 deletions plugins/lighthouse/util/disassembler/binja_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import binaryninja
from binaryninja import PythonScriptingInstance, binaryview
from binaryninja.plugin import BackgroundTaskThread
from binaryninjaui import Sidebar, SidebarWidget, SidebarWidgetType, SidebarWidgetLocation, UIActionHandler, UIContext
from PySide6 import QtCore
from PySide6.QtCore import Qt, QRectF
from PySide6.QtGui import QImage, QPixmap, QPainter, QFont, QColor

logger = logging.getLogger("Lighthouse.API.Binja")

Expand Down Expand Up @@ -160,29 +164,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 +212,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()
vf = ac.getCurrentViewFrame()
if not vf:
return 0
ac = vf.actionContext()
if not ac:
actx = vf.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 +280,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 @@ -363,61 +360,31 @@ def name_changed(self, address, name):
# UI
#------------------------------------------------------------------------------

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)

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 LighthouseWidget(SidebarWidget):
def __init__(self, name, frame, data):
SidebarWidget.__init__(self, name)
self.actionHandler = UIActionHandler()
self.actionHandler.setupActionHandler(self)

@visible.setter
def visible(self, is_visible):
self._visible_for_view[self._active_view] = is_visible

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]
class LighthouseWidgetType(SidebarWidgetType):
def __init__(self):
icon = QImage(56, 56, QImage.Format_RGB32)
icon.fill(0)

def notifyVisibilityChanged(self, is_visible):
self.visible = 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 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 5c0ba18

Please sign in to comment.