Skip to content

Commit 4c419bb

Browse files
committed
Refactor RepositoryManager; No longer ref counted object (one global plugin manager going forward); Move user plugin/repo plugin logic to core under single streamlined function
1 parent 1956f69 commit 4c419bb

File tree

8 files changed

+29
-117
lines changed

8 files changed

+29
-117
lines changed

binaryninjaapi.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ bool BinaryNinja::InitPlugins(bool allowUserPlugins)
4949
}
5050

5151

52-
void BinaryNinja::InitCorePlugins()
53-
{
54-
BNInitCorePlugins();
55-
}
56-
57-
58-
void BinaryNinja::InitUserPlugins()
59-
{
60-
BNInitUserPlugins();
61-
}
62-
63-
64-
void BinaryNinja::InitRepoPlugins()
65-
{
66-
BNInitRepoPlugins();
67-
}
68-
69-
7052
string BinaryNinja::GetBundledPluginDirectory()
7153
{
7254
char* path = BNGetBundledPluginDirectory();

binaryninjaapi.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18211,17 +18211,13 @@ namespace BinaryNinja {
1821118211
/*!
1821218212
\ingroup pluginmanager
1821318213
*/
18214-
class RepositoryManager :
18215-
public CoreRefCountObject<BNRepositoryManager, BNNewRepositoryManagerReference, BNFreeRepositoryManager>
18214+
class RepositoryManager
1821618215
{
1821718216
public:
18218-
RepositoryManager(const std::string& enabledPluginsPath);
18219-
RepositoryManager(BNRepositoryManager* repoManager);
18220-
RepositoryManager();
18221-
bool CheckForUpdates();
18222-
std::vector<Ref<Repository>> GetRepositories();
18223-
Ref<Repository> GetRepositoryByPath(const std::string& repoName);
18224-
bool AddRepository(const std::string& url, // URL to raw plugins.json file
18217+
static bool CheckForUpdates();
18218+
static std::vector<Ref<Repository>> GetRepositories();
18219+
static Ref<Repository> GetRepositoryByPath(const std::string& repoName);
18220+
static bool AddRepository(const std::string& url, // URL to raw plugins.json file
1822518221
const std::string& repoPath); // Relative path within the repositories directory
1822618222
Ref<Repository> GetDefaultRepository();
1822718223
};

binaryninjacore.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ extern "C"
255255
typedef struct BNBackgroundTask BNBackgroundTask;
256256
typedef struct BNRepository BNRepository;
257257
typedef struct BNRepoPlugin BNRepoPlugin;
258-
typedef struct BNRepositoryManager BNRepositoryManager;
259258
typedef struct BNComponent BNComponent;
260259
typedef struct BNSettings BNSettings;
261260
typedef struct BNMetadata BNMetadata;
@@ -3839,11 +3838,8 @@ extern "C"
38393838

38403839
// Plugin initialization
38413840
BINARYNINJACOREAPI bool BNInitPlugins(bool allowUserPlugins);
3842-
BINARYNINJACOREAPI bool BNInitCorePlugins(void); // Deprecated, use BNInitPlugins
38433841
BINARYNINJACOREAPI void BNDisablePlugins(void);
38443842
BINARYNINJACOREAPI bool BNIsPluginsEnabled(void);
3845-
BINARYNINJACOREAPI void BNInitUserPlugins(void); // Deprecated, use BNInitPlugins
3846-
BINARYNINJACOREAPI void BNInitRepoPlugins(void);
38473843

38483844
BINARYNINJACOREAPI char* BNGetInstallDirectory(void);
38493845
BINARYNINJACOREAPI char* BNGetBundledPluginDirectory(void);
@@ -7551,18 +7547,13 @@ extern "C"
75517547
BINARYNINJACOREAPI BNRepoPlugin* BNRepositoryGetPluginByPath(BNRepository* r, const char* pluginPath);
75527548
BINARYNINJACOREAPI const char* BNRepositoryGetPluginsPath(BNRepository* r);
75537549

7554-
BINARYNINJACOREAPI BNRepositoryManager* BNCreateRepositoryManager(const char* enabledPluginsPath);
7555-
BINARYNINJACOREAPI BNRepositoryManager* BNNewRepositoryManagerReference(BNRepositoryManager* r);
7556-
BINARYNINJACOREAPI void BNFreeRepositoryManager(BNRepositoryManager* r);
7557-
BINARYNINJACOREAPI bool BNRepositoryManagerCheckForUpdates(BNRepositoryManager* r);
7558-
BINARYNINJACOREAPI BNRepository** BNRepositoryManagerGetRepositories(BNRepositoryManager* r, size_t* count);
7550+
BINARYNINJACOREAPI bool BNRepositoryManagerCheckForUpdates();
7551+
BINARYNINJACOREAPI BNRepository** BNRepositoryManagerGetRepositories(size_t* count);
75597552
BINARYNINJACOREAPI void BNFreeRepositoryManagerRepositoriesList(BNRepository** r);
7560-
BINARYNINJACOREAPI bool BNRepositoryManagerAddRepository(
7561-
BNRepositoryManager* r, const char* url, const char* repoPath);
7562-
BINARYNINJACOREAPI BNRepository* BNRepositoryGetRepositoryByPath(BNRepositoryManager* r, const char* repoPath);
7563-
BINARYNINJACOREAPI BNRepositoryManager* BNGetRepositoryManager(void);
7553+
BINARYNINJACOREAPI bool BNRepositoryManagerAddRepository(const char* url, const char* repoPath);
7554+
BINARYNINJACOREAPI BNRepository* BNRepositoryGetRepositoryByPath(const char* repoPath);
75647555

7565-
BINARYNINJACOREAPI BNRepository* BNRepositoryManagerGetDefaultRepository(BNRepositoryManager* r);
7556+
BINARYNINJACOREAPI BNRepository* BNRepositoryManagerGetDefaultRepository();
75667557

75677558
// Components
75687559

pluginmanager.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -310,31 +310,16 @@ string Repository::GetFullPath() const
310310
RETURN_STRING(BNRepositoryGetPluginsPath(m_object));
311311
}
312312

313-
RepositoryManager::RepositoryManager(const string& enabledPluginsPath)
314-
{
315-
m_object = BNCreateRepositoryManager(enabledPluginsPath.c_str());
316-
}
317-
318-
RepositoryManager::RepositoryManager(BNRepositoryManager* mgr)
319-
{
320-
m_object = mgr;
321-
}
322-
323-
RepositoryManager::RepositoryManager()
324-
{
325-
m_object = BNGetRepositoryManager();
326-
}
327-
328313
bool RepositoryManager::CheckForUpdates()
329314
{
330-
return BNRepositoryManagerCheckForUpdates(m_object);
315+
return BNRepositoryManagerCheckForUpdates();
331316
}
332317

333318
vector<Ref<Repository>> RepositoryManager::GetRepositories()
334319
{
335320
vector<Ref<Repository>> repos;
336321
size_t count = 0;
337-
BNRepository** reposPtr = BNRepositoryManagerGetRepositories(m_object, &count);
322+
BNRepository** reposPtr = BNRepositoryManagerGetRepositories(&count);
338323
for (size_t i = 0; i < count; i++)
339324
repos.push_back(new Repository(BNNewRepositoryReference(reposPtr[i])));
340325
BNFreeRepositoryManagerRepositoriesList(reposPtr);
@@ -344,15 +329,15 @@ vector<Ref<Repository>> RepositoryManager::GetRepositories()
344329
bool RepositoryManager::AddRepository(const std::string& url,
345330
const std::string& repoPath) // Relative path within the repositories directory
346331
{
347-
return BNRepositoryManagerAddRepository(m_object, url.c_str(), repoPath.c_str());
332+
return BNRepositoryManagerAddRepository(url.c_str(), repoPath.c_str());
348333
}
349334

350335
Ref<Repository> RepositoryManager::GetRepositoryByPath(const std::string& repoPath)
351336
{
352-
return new Repository(BNRepositoryGetRepositoryByPath(m_object, repoPath.c_str()));
337+
return new Repository(BNRepositoryGetRepositoryByPath(repoPath.c_str()));
353338
}
354339

355340
Ref<Repository> RepositoryManager::GetDefaultRepository()
356341
{
357-
return new Repository(BNRepositoryManagerGetDefaultRepository(m_object));
342+
return new Repository(BNRepositoryManagerGetDefaultRepository());
358343
}

python/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ def _init_plugins():
248248
if _enable_default_log and is_headless_init_once and min_level in LogLevel.__members__ and not core_ui_enabled(
249249
) and sys.stderr.isatty():
250250
log_to_stderr(LogLevel[min_level])
251-
core.BNInitRepoPlugins()
252251
if core.BNIsLicenseValidated():
253252
_plugin_init = True
254253
else:

python/pluginmanager.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ class RepositoryManager:
337337
"""
338338
def __init__(self):
339339
binaryninja._init_plugins()
340-
self.handle = core.BNGetRepositoryManager()
341340

342341
def __getitem__(self, repo_path: str) -> Repository:
343342
for repo in self.repositories:
@@ -347,14 +346,14 @@ def __getitem__(self, repo_path: str) -> Repository:
347346

348347
def check_for_updates(self) -> bool:
349348
"""Check for updates for all managed Repository objects"""
350-
return core.BNRepositoryManagerCheckForUpdates(self.handle)
349+
return core.BNRepositoryManagerCheckForUpdates()
351350

352351
@property
353352
def repositories(self) -> List[Repository]:
354353
"""List of Repository objects being managed"""
355354
result = []
356355
count = ctypes.c_ulonglong(0)
357-
repos = core.BNRepositoryManagerGetRepositories(self.handle, count)
356+
repos = core.BNRepositoryManagerGetRepositories(count)
358357
assert repos is not None, "core.BNRepositoryManagerGetRepositories returned None"
359358
try:
360359
for i in range(count.value):
@@ -376,7 +375,7 @@ def plugins(self) -> Dict[str, List[RepoPlugin]]:
376375
@property
377376
def default_repository(self) -> Repository:
378377
"""Gets the default Repository"""
379-
repo_handle = core.BNRepositoryManagerGetDefaultRepository(self.handle)
378+
repo_handle = core.BNRepositoryManagerGetDefaultRepository()
380379
assert repo_handle is not None, "core.BNRepositoryManagerGetDefaultRepository returned None"
381380
repo_handle_ref = core.BNNewRepositoryReference(repo_handle)
382381
assert repo_handle_ref is not None, "core.BNNewRepositoryReference returned None"
@@ -406,4 +405,4 @@ def add_repository(self, url: Optional[str] = None, repopath: Optional[str] = No
406405
if not isinstance(url, str) or not isinstance(repopath, str):
407406
raise ValueError("Expected url or repopath to be of type str.")
408407

409-
return core.BNRepositoryManagerAddRepository(self.handle, url, repopath)
408+
return core.BNRepositoryManagerAddRepository(url, repopath)

rust/src/headless.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::enterprise::EnterpriseCheckoutStatus;
2626
use crate::main_thread::{MainThreadAction, MainThreadHandler};
2727
use crate::progress::ProgressCallback;
2828
use crate::rc::Ref;
29-
use binaryninjacore_sys::{BNInitPlugins, BNInitRepoPlugins};
29+
use binaryninjacore_sys::{BNInitPlugins};
3030
use std::sync::mpsc::Sender;
3131
use std::sync::Mutex;
3232
use std::thread::JoinHandle;
@@ -221,10 +221,6 @@ pub fn init_with_opts(options: InitializationOptions) -> Result<(), Initializati
221221

222222
unsafe {
223223
BNInitPlugins(options.user_plugins);
224-
if options.repo_plugins {
225-
// We are allowed to initialize repo plugins, so do it!
226-
BNInitRepoPlugins();
227-
}
228224
}
229225

230226
if !is_license_validated() {

rust/src/repository/manager.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use crate::rc::{Array, Ref, RefCountable};
1+
use crate::rc::{Array, Ref};
22
use crate::repository::Repository;
33
use crate::string::IntoCStr;
4-
use binaryninjacore_sys::{
5-
BNCreateRepositoryManager, BNFreeRepositoryManager, BNGetRepositoryManager,
6-
BNNewRepositoryManagerReference, BNRepositoryGetRepositoryByPath, BNRepositoryManager,
4+
use binaryninjacore_sys::{BNRepositoryGetRepositoryByPath,
75
BNRepositoryManagerAddRepository, BNRepositoryManagerCheckForUpdates,
86
BNRepositoryManagerGetDefaultRepository, BNRepositoryManagerGetRepositories,
97
};
@@ -13,38 +11,24 @@ use std::ptr::NonNull;
1311

1412
/// Keeps track of all the repositories and keeps the `enabled_plugins.json`
1513
/// file coherent with the plugins that are installed/uninstalled enabled/disabled
16-
#[repr(transparent)]
1714
pub struct RepositoryManager {
18-
handle: NonNull<BNRepositoryManager>,
1915
}
2016

2117
impl RepositoryManager {
22-
#[allow(clippy::should_implement_trait)]
23-
pub fn default() -> Ref<Self> {
24-
let result = unsafe { BNGetRepositoryManager() };
25-
unsafe { Self::ref_from_raw(NonNull::new(result).unwrap()) }
26-
}
27-
28-
pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNRepositoryManager>) -> Ref<Self> {
29-
Ref::new(Self { handle })
30-
}
31-
32-
pub fn new(plugins_path: &str) -> Ref<Self> {
33-
let plugins_path = plugins_path.to_cstr();
34-
let result = unsafe { BNCreateRepositoryManager(plugins_path.as_ptr()) };
35-
unsafe { Self::ref_from_raw(NonNull::new(result).unwrap()) }
18+
pub fn new() -> Self {
19+
Self {}
3620
}
3721

3822
/// Check for updates for all managed [`Repository`] objects
3923
pub fn check_for_updates(&self) -> bool {
40-
unsafe { BNRepositoryManagerCheckForUpdates(self.handle.as_ptr()) }
24+
unsafe { BNRepositoryManagerCheckForUpdates() }
4125
}
4226

4327
/// List of [`Repository`] objects being managed
4428
pub fn repositories(&self) -> Array<Repository> {
4529
let mut count = 0;
4630
let result =
47-
unsafe { BNRepositoryManagerGetRepositories(self.handle.as_ptr(), &mut count) };
31+
unsafe { BNRepositoryManagerGetRepositories(&mut count) };
4832
assert!(!result.is_null());
4933
unsafe { Array::new(result, count, ()) }
5034
}
@@ -64,20 +48,20 @@ impl RepositoryManager {
6448
let url = url.to_cstr();
6549
let repo_path = repository_path.to_cstr();
6650
unsafe {
67-
BNRepositoryManagerAddRepository(self.handle.as_ptr(), url.as_ptr(), repo_path.as_ptr())
51+
BNRepositoryManagerAddRepository(url.as_ptr(), repo_path.as_ptr())
6852
}
6953
}
7054

7155
pub fn repository_by_path(&self, path: &Path) -> Option<Repository> {
7256
let path = path.to_cstr();
7357
let result =
74-
unsafe { BNRepositoryGetRepositoryByPath(self.handle.as_ptr(), path.as_ptr()) };
58+
unsafe { BNRepositoryGetRepositoryByPath(path.as_ptr()) };
7559
NonNull::new(result).map(|raw| unsafe { Repository::from_raw(raw) })
7660
}
7761

7862
/// Gets the default [`Repository`]
7963
pub fn default_repository(&self) -> Ref<Repository> {
80-
let result = unsafe { BNRepositoryManagerGetDefaultRepository(self.handle.as_ptr()) };
64+
let result = unsafe { BNRepositoryManagerGetDefaultRepository() };
8165
assert!(!result.is_null());
8266
unsafe { Repository::ref_from_raw(NonNull::new(result).unwrap()) }
8367
}
@@ -90,23 +74,3 @@ impl Debug for RepositoryManager {
9074
.finish()
9175
}
9276
}
93-
94-
impl ToOwned for RepositoryManager {
95-
type Owned = Ref<Self>;
96-
97-
fn to_owned(&self) -> Self::Owned {
98-
unsafe { RefCountable::inc_ref(self) }
99-
}
100-
}
101-
102-
unsafe impl RefCountable for RepositoryManager {
103-
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
104-
Self::ref_from_raw(
105-
NonNull::new(BNNewRepositoryManagerReference(handle.handle.as_ptr())).unwrap(),
106-
)
107-
}
108-
109-
unsafe fn dec_ref(handle: &Self) {
110-
BNFreeRepositoryManager(handle.handle.as_ptr())
111-
}
112-
}

0 commit comments

Comments
 (0)