diff options
| author | Glenn Smith <glenn@vector35.com> | 2023-05-23 20:11:07 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2023-06-08 17:18:21 -0400 |
| commit | 0882343191078862361937493035030128230878 (patch) | |
| tree | f5ef5168066c53b85fd9574225df7411817bb577 | |
| parent | 30784c736f7ed7cc796573d81021bd3ef10fded3 (diff) | |
Undo entry states and reverting
| -rw-r--r-- | binaryninjaapi.h | 33 | ||||
| -rw-r--r-- | binaryninjacore.h | 7 | ||||
| -rw-r--r-- | binaryview.cpp | 14 | ||||
| -rw-r--r-- | filemetadata.cpp | 17 | ||||
| -rw-r--r-- | python/filemetadata.py | 57 | ||||
| -rw-r--r-- | rust/src/filemetadata.rs | 15 |
6 files changed, 110 insertions, 33 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 3034ac52..d81c6320 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2149,12 +2149,22 @@ namespace BinaryNinja { const std::function<bool(size_t progress, size_t total)>& progressCallback); /*! Start recording actions taken so they can be undone at some point + + \return Id of UndoEntry created, for passing to either CommitUndoActions or RevertUndoActions */ - void BeginUndoActions(); + [[nodiscard]] std::string BeginUndoActions(); + + /*! Commit the actions taken since a call to BeginUndoActions. + + \param id Id of UndoEntry created by BeginUndoActions + */ + void CommitUndoActions(const std::string& id); + + /*! Revert the actions taken since a call to BeginUndoActions. - /*! Commit the actions taken since the last commit to the undo database. + \param id Id of UndoEntry created by BeginUndoActions */ - void CommitUndoActions(); + void RevertUndoActions(const std::string& id); /*! \return Whether it is possible to perform an Undo */ @@ -3549,13 +3559,22 @@ namespace BinaryNinja { Ref<SaveSettings> settings = new SaveSettings()); /*! Start recording actions taken so they can be undone at some point + + \return Id of UndoEntry created, for passing to either CommitUndoActions or RevertUndoActions */ - void BeginUndoActions(); - void AddUndoAction(UndoAction* action); + [[nodiscard]] std::string BeginUndoActions(); + + /*! Commit the actions taken since a call to BeginUndoActions. + + \param id Id of UndoEntry created by BeginUndoActions + */ + void CommitUndoActions(const std::string& id); + + /*! Revert the actions taken since a call to BeginUndoActions. - /*! Commit the actions taken since the last commit to the undo database. + \param id Id of UndoEntry created by BeginUndoActions */ - void CommitUndoActions(); + void RevertUndoActions(const std::string& id); /*! \return Whether it is possible to perform an Undo diff --git a/binaryninjacore.h b/binaryninjacore.h index 7ac582d6..815894f0 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -43,7 +43,7 @@ // will require rebuilding. The minimum version is increased when there are // incompatible changes that break binary compatibility, such as changes to // existing types or functions. -#define BN_MINIMUM_CORE_ABI_VERSION 34 +#define BN_MINIMUM_CORE_ABI_VERSION 35 #ifdef __GNUC__ #ifdef BINARYNINJACORE_LIBRARY @@ -3265,8 +3265,9 @@ extern "C" BINARYNINJACOREAPI char* BNGetFilename(BNFileMetadata* file); BINARYNINJACOREAPI void BNSetFilename(BNFileMetadata* file, const char* name); - BINARYNINJACOREAPI void BNBeginUndoActions(BNFileMetadata* file); - BINARYNINJACOREAPI void BNCommitUndoActions(BNFileMetadata* file); + BINARYNINJACOREAPI char* BNBeginUndoActions(BNFileMetadata* file); + BINARYNINJACOREAPI void BNCommitUndoActions(BNFileMetadata* file, const char* id); + BINARYNINJACOREAPI void BNRevertUndoActions(BNFileMetadata* file, const char* id); BINARYNINJACOREAPI bool BNCanUndo(BNFileMetadata* file); diff --git a/binaryview.cpp b/binaryview.cpp index be4c4ca7..b5e80a68 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1357,15 +1357,21 @@ bool BinaryView::SaveAutoSnapshot( } -void BinaryView::BeginUndoActions() +string BinaryView::BeginUndoActions() { - m_file->BeginUndoActions(); + return m_file->BeginUndoActions(); } -void BinaryView::CommitUndoActions() +void BinaryView::CommitUndoActions(const string& id) { - m_file->CommitUndoActions(); + m_file->CommitUndoActions(id); +} + + +void BinaryView::RevertUndoActions(const string& id) +{ + m_file->RevertUndoActions(id); } diff --git a/filemetadata.cpp b/filemetadata.cpp index 0a12eb00..4f75129e 100644 --- a/filemetadata.cpp +++ b/filemetadata.cpp @@ -270,15 +270,24 @@ bool FileMetadata::CreateSnapshotedView(BinaryView* data, const std::string& vie } -void FileMetadata::BeginUndoActions() +std::string FileMetadata::BeginUndoActions() { - BNBeginUndoActions(m_object); + char* id = BNBeginUndoActions(m_object); + std::string result = id; + BNFreeString(id); + return result; +} + + +void FileMetadata::CommitUndoActions(const std::string& id) +{ + BNCommitUndoActions(m_object, id.c_str()); } -void FileMetadata::CommitUndoActions() +void FileMetadata::RevertUndoActions(const std::string& id) { - BNCommitUndoActions(m_object); + BNRevertUndoActions(m_object, id.c_str()); } diff --git a/python/filemetadata.py b/python/filemetadata.py index 95cd8f7b..bdb46a00 100644 --- a/python/filemetadata.py +++ b/python/filemetadata.py @@ -140,6 +140,7 @@ class FileMetadata: self._nav: Optional[NavigationHandler] = None assert _handle is not None self.handle = _handle + self._previous_undos = [] def __repr__(self): return f"<FileMetadata: {self.filename}>" @@ -303,19 +304,20 @@ class FileMetadata: """ core.BNCloseFile(self.handle) - def begin_undo_actions(self) -> None: + def begin_undo_actions(self) -> str: """ - ``begin_undo_actions`` start recording actions taken so they can be undone at some point. + ``begin_undo_actions`` starts recording actions taken so they can be undone at some point. - :rtype: None + :return: Id of undo state, for passing to :py:func:`commit_undo_actions`` or :py:func:`revert_undo_actions`. + :rtype: str :Example: >>> bv.get_disassembly(0x100012f1) 'xor eax, eax' - >>> bv.begin_undo_actions() + >>> undo = bv.begin_undo_actions() >>> bv.convert_to_nop(0x100012f1) True - >>> bv.commit_undo_actions() + >>> bv.commit_undo_actions(undo) >>> bv.get_disassembly(0x100012f1) 'nop' >>> bv.undo() @@ -323,21 +325,26 @@ class FileMetadata: 'xor eax, eax' >>> """ - core.BNBeginUndoActions(self.handle) + id = core.BNBeginUndoActions(self.handle) + self._previous_undos.append(id) + return id - def commit_undo_actions(self) -> None: + def commit_undo_actions(self, id: Optional[str] = None) -> None: """ - ``commit_undo_actions`` commit the actions taken since the last commit to the undo database. + ``commit_undo_actions`` commits the actions taken since a call to :py:func:`begin_undo_actions` + Pass as `id` the value returned by :py:func:`begin_undo_actions`. Empty values of + `id` will commit all changes since the last call to :py:func:`begin_undo_actions`. + :param Optional[str] id: id of undo state, from :py:func:`begin_undo_actions` :rtype: None :Example: >>> bv.get_disassembly(0x100012f1) 'xor eax, eax' - >>> bv.begin_undo_actions() + >>> undo = bv.begin_undo_actions() >>> bv.convert_to_nop(0x100012f1) True - >>> bv.commit_undo_actions() + >>> bv.commit_undo_actions(undo) >>> bv.get_disassembly(0x100012f1) 'nop' >>> bv.undo() @@ -345,7 +352,35 @@ class FileMetadata: 'xor eax, eax' >>> """ - core.BNCommitUndoActions(self.handle) + + if id is None: + id = "" + core.BNCommitUndoActions(self.handle, id) + + def revert_undo_actions(self, id: Optional[str] = None) -> None: + """ + ``revert_undo_actions`` reverts the actions taken since a call to :py:func:`begin_undo_actions` + Pass as `id` the value returned by :py:func:`begin_undo_actions`. Empty values of + `id` will revert all changes since the last call to :py:func:`begin_undo_actions`. + + :param Optional[str] id: id of undo state, from :py:func:`begin_undo_actions` + :rtype: None + :Example: + + >>> bv.get_disassembly(0x100012f1) + 'xor eax, eax' + >>> undo = bv.begin_undo_actions() + >>> bv.convert_to_nop(0x100012f1) + True + >>> bv.revert_undo_actions(undo) + >>> bv.get_disassembly(0x100012f1) + 'xor eax, eax' + >>> + """ + + if id is None: + id = "" + core.BNRevertUndoActions(self.handle, id) def undo(self) -> None: """ diff --git a/rust/src/filemetadata.rs b/rust/src/filemetadata.rs index e3449b6e..b0e1c73f 100644 --- a/rust/src/filemetadata.rs +++ b/rust/src/filemetadata.rs @@ -38,6 +38,7 @@ use binaryninjacore_sys::{ BNOpenExistingDatabase, BNOpenProject, BNRedo, + BNRevertUndoActions, BNSaveAutoSnapshot, BNSetFilename, BNUndo, @@ -124,15 +125,21 @@ impl FileMetadata { unsafe { BNIsBackedByDatabase(self.handle, view_type.as_ref().as_ptr() as *const _) } } - pub fn begin_undo_actions(&self) { + pub fn begin_undo_actions(&self) -> BnString { + unsafe { BnString::from_raw(BNBeginUndoActions(self.handle)) } + } + + pub fn commit_undo_actions<S: BnStrCompatible>(&self, id: S) { + let id = id.into_bytes_with_nul(); unsafe { - BNBeginUndoActions(self.handle); + BNCommitUndoActions(self.handle, id.as_ref().as_ptr() as *const _); } } - pub fn commit_undo_actions(&self) { + pub fn revert_undo_actions<S: BnStrCompatible>(&self, id: S) { + let id = id.into_bytes_with_nul(); unsafe { - BNCommitUndoActions(self.handle); + BNRevertUndoActions(self.handle, id.as_ref().as_ptr() as *const _); } } |
