Skip to content

Commit 48e8459

Browse files
committed
Add API to get all project metadata
1 parent 24eabc0 commit 48e8459

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

binaryninjaapi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,7 @@ namespace BinaryNinja {
36873687

36883688
Ref<Metadata> QueryMetadata(const std::string& key);
36893689
bool StoreMetadata(const std::string& key, Ref<Metadata> value);
3690+
Ref<Metadata> GetMetadata();
36903691
bool RemoveMetadata(const std::string& key);
36913692

36923693
Ref<ProjectFolder> CreateFolderFromPath(const std::string& path, Ref<ProjectFolder> parent, const std::string& description,
@@ -5774,7 +5775,7 @@ namespace BinaryNinja {
57745775
void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> sym, uint64_t reloc);
57755776

57765777
/*! OnAfterSnapshotDataApplied is called when loading a view from a database, after snapshot data has been applied to it.
5777-
5778+
57785779
\note This method **may** be overridden by custom BinaryViews.
57795780

57805781
\warning This method **must not** be called directly.

binaryninjacore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4308,6 +4308,7 @@ extern "C"
43084308

43094309
BINARYNINJACOREAPI BNMetadata* BNProjectQueryMetadata(BNProject* project, const char* key);
43104310
BINARYNINJACOREAPI bool BNProjectStoreMetadata(BNProject* project, const char* key, BNMetadata* value);
4311+
BINARYNINJACOREAPI BNMetadata* BNProjectGetMetadata(BNProject* project);
43114312
BINARYNINJACOREAPI bool BNProjectRemoveMetadata(BNProject* project, const char* key);
43124313

43134314
BINARYNINJACOREAPI BNProjectFile* BNProjectCreateFileFromPath(BNProject* project, const char* path, BNProjectFolder* folder, const char* name, const char* description, void* ctxt,

project.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ bool Project::StoreMetadata(const std::string& key, Ref<Metadata> value)
340340
}
341341

342342

343+
Ref<Metadata> Project::GetMetadata()
344+
{
345+
BNMetadata* value = BNProjectGetMetadata(m_object);
346+
if (value == nullptr)
347+
return nullptr;
348+
return new Metadata(value);
349+
}
350+
351+
343352
bool Project::RemoveMetadata(const std::string& key)
344353
{
345354
return BNProjectRemoveMetadata(m_object, key.c_str());

python/project.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from contextlib import contextmanager
2424
from os import PathLike
25-
from typing import Any, Callable, List, Optional, Union
25+
from typing import Any, Callable, Dict, List, Optional, Union
2626

2727
import binaryninja
2828
from . import _binaryninjacore as core
@@ -585,6 +585,19 @@ def store_metadata(self, key: str, value: MetadataValueType) -> bool:
585585
_val = Metadata(_val)
586586
return core.BNProjectStoreMetadata(self._handle, key, _val.handle)
587587

588+
@property
589+
def metadata(self) -> Dict[str, 'MetadataValueType']:
590+
"""
591+
Retrieves the metadata associated with the project.
592+
593+
:return: metadata associated with the Project
594+
"""
595+
md_handle = core.BNProjectGetMetadata(self._handle)
596+
assert md_handle is not None, "core.BNProjectGetMetadata returned None"
597+
value = Metadata(handle=md_handle).value
598+
assert isinstance(value, dict), "core.BNProjectGetMetadata did not return a dict"
599+
return value
600+
588601
def remove_metadata(self, key: str) -> bool:
589602
"""
590603
Removes the metadata associated with this key from the project

rust/src/project.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ impl Project {
132132
unsafe { BNProjectStoreMetadata(self.handle.as_ptr(), key_raw.as_ptr(), value.handle) }
133133
}
134134

135+
/// Retrieves the metadata associated with the project.
136+
pub fn metadata(&self) -> Ref<Metadata> {
137+
let md_handle = unsafe { BNProjectGetMetadata(self.handle.as_ptr()) };
138+
assert!(!md_handle.is_null());
139+
unsafe { Metadata::ref_from_raw(md_handle) }
140+
}
141+
135142
/// Removes the metadata associated with this `key` from the project
136143
pub fn remove_metadata(&self, key: &str) -> bool {
137144
let key_raw = key.to_cstr();

0 commit comments

Comments
 (0)