From 80cb1f68c83112c4d99893ad2f23bf22abdc4a6d Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Sun, 14 Oct 2018 22:47:22 -0400 Subject: Initial Enhanced Settings System. --- settings.cpp | 227 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 128 insertions(+), 99 deletions(-) (limited to 'settings.cpp') diff --git a/settings.cpp b/settings.cpp index ca1e42a3..62425f5b 100644 --- a/settings.cpp +++ b/settings.cpp @@ -1,178 +1,207 @@ #include "binaryninjaapi.h" +#include "json/json.h" #include using namespace BinaryNinja; using namespace std; -bool Setting::GetBool(const std::string& pluginName, const std::string& name, bool defaultValue) +bool Settings::RegisterGroup(const string& group, const string& title) { - return BNSettingGetBool(pluginName.c_str(), name.c_str(), defaultValue); + return BNSettingsRegisterGroup(m_registry.c_str(), group.c_str(), title.c_str()); } -int64_t Setting::GetInteger(const std::string& pluginName, const std::string& name, int64_t defaultValue) + +bool Settings::RegisterSetting(const string& id, const string& properties) { - return BNSettingGetInteger(pluginName.c_str(), name.c_str(), defaultValue); + return BNSettingsRegisterSetting(m_registry.c_str(), id.c_str(), properties.c_str()); } -std::string Setting::GetString(const std::string& pluginName, const std::string& name, const std::string& defaultValue) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property) { - char* str = BNSettingGetString(pluginName.c_str(), name.c_str(), defaultValue.c_str()); - string result(str); - BNFreeString(str); - return result; + return BNSettingsUpdateProperty(m_registry.c_str(), id.c_str(), property.c_str()); } -double Setting::GetDouble(const std::string& pluginName, const std::string& name, double defaultValue) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property, bool value) { - return BNSettingGetDouble(pluginName.c_str(), name.c_str(), defaultValue); + return BNSettingsUpdateBoolProperty(m_registry.c_str(), id.c_str(), property.c_str(), value); } -std::vector Setting::GetIntegerList(const std::string& pluginName, - const std::string& name, - const std::vector& defaultValue) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property, double value) { - int64_t* buffer = new int64_t[defaultValue.size()]; - memcpy(&buffer[0], &defaultValue[0], sizeof(int64_t) * defaultValue.size()); - size_t size = defaultValue.size(); - int64_t* outBuffer = BNSettingGetIntegerList(pluginName.c_str(), name.c_str(), buffer, &size); - delete[] buffer; + return BNSettingsUpdateDoubleProperty(m_registry.c_str(), id.c_str(), property.c_str(), value); +} + - vector out(outBuffer, outBuffer + size); - BNFreeSettingIntegerList(outBuffer); - return out; +bool Settings::UpdateProperty(const std::string& id, const std::string& property, int value) +{ + return BNSettingsUpdateInt64Property(m_registry.c_str(), id.c_str(), property.c_str(), value); } -std::vector Setting::GetStringList(const std::string& pluginName, - const std::string& name, - const std::vector& defaultValue) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property, int64_t value) { - char** buffer = new char*[defaultValue.size()]; - for (size_t i = 0; i < defaultValue.size(); i++) - buffer[i] = BNAllocString(defaultValue[i].c_str()); - size_t size = defaultValue.size(); - char** outBuffer = (char**)BNSettingGetStringList(pluginName.c_str(), name.c_str(), (const char**)buffer, &size); + return BNSettingsUpdateInt64Property(m_registry.c_str(), id.c_str(), property.c_str(), value); +} - vector result; - result.reserve(size); - for (size_t i = 0; i < size; i++) - result.emplace_back(outBuffer[i]); - for (size_t i = 0; i < defaultValue.size(); i++) - BNFreeString(buffer[i]); - delete[] buffer; - BNFreeStringList(outBuffer, size); - return result; +bool Settings::UpdateProperty(const std::string& id, const std::string& property, uint64_t value) +{ + return BNSettingsUpdateUInt64Property(m_registry.c_str(), id.c_str(), property.c_str(), value); } -bool Setting::IsPresent(const std::string& pluginName, const std::string& name) +bool Settings::UpdateProperty(const std::string& id, const std::string& property, const char* value) { - return BNSettingIsPresent(pluginName.c_str(), name.c_str()); + return BNSettingsUpdateStringProperty(m_registry.c_str(), id.c_str(), property.c_str(), value); } -bool Setting::IsBool(const std::string& pluginName, const std::string& name) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property, const std::string& value) { - return BNSettingIsBool(pluginName.c_str(), name.c_str()); + return BNSettingsUpdateStringProperty(m_registry.c_str(), id.c_str(), property.c_str(), value.c_str()); } -bool Setting::IsInteger(const std::string& pluginName, const std::string& name) + +bool Settings::UpdateProperty(const std::string& id, const std::string& property, const std::vector& value) { - return BNSettingIsInteger(pluginName.c_str(), name.c_str()); + char** buffer = new char*[value.size()]; + if (!buffer) + return false; + + for (size_t i = 0; i < value.size(); i++) + buffer[i] = BNAllocString(value[i].c_str()); + + bool result = BNSettingsUpdateStringListProperty(m_registry.c_str(), id.c_str(), property.c_str(), (const char**)buffer, value.size()); + BNFreeStringList(buffer, value.size()); + return result; } -bool Setting::IsString(const std::string& pluginName, const std::string& name) + +string Settings::GetSchema() { - return BNSettingIsString(pluginName.c_str(), name.c_str()); + char* schemaStr = BNSettingsGetSchema(m_registry.c_str()); + string schema(schemaStr); + BNFreeString(schemaStr); + return schema; } -bool Setting::IsIntegerList(const std::string& pluginName, const std::string& name) + +bool Settings::Reset(const string& id, Ref view, BNSettingsScope scope) { - return BNSettingIsIntegerList(pluginName.c_str(), name.c_str()); + return BNSettingsReset(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::IsStringList(const std::string& pluginName, const std::string& name) + +bool Settings::ResetAll(Ref view, BNSettingsScope scope) { - return BNSettingIsStringList(pluginName.c_str(), name.c_str()); + return BNSettingsResetAll(m_registry.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::IsDouble(const std::string& pluginName, const std::string& name) + +template<> bool Settings::Get(const string& id, Ref view, BNSettingsScope* scope) { - return BNSettingIsDouble(pluginName.c_str(), name.c_str()); + return BNSettingsGetBool(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - bool value, - bool autoFlush) + +template<> double Settings::Get(const string& id, Ref view, BNSettingsScope* scope) { - return BNSettingSetBool(settingGroup.c_str(), name.c_str(), value, autoFlush); + return BNSettingsGetDouble(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - int64_t value, - bool autoFlush) + +template<> int64_t Settings::Get(const string& id, Ref view, BNSettingsScope* scope) { - return BNSettingSetInteger(settingGroup.c_str(), name.c_str(), value, autoFlush); + return BNSettingsGetInt64(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - const std::string& value, - bool autoFlush) + +template<> uint64_t Settings::Get(const string& id, Ref view, BNSettingsScope* scope) { - return BNSettingSetString(settingGroup.c_str(), name.c_str(), value.c_str(), autoFlush); + return BNSettingsGetUInt64(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - const std::vector& value, - bool autoFlush) + +template<> string Settings::Get(const string& id, Ref view, BNSettingsScope* scope) { - return BNSettingSetIntegerList(settingGroup.c_str(), name.c_str(), &value[0], value.size(), autoFlush); + char* tmpStr = BNSettingsGetString(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope); + string result(tmpStr); + BNFreeString(tmpStr); + return result; } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - const std::vector& value, - bool autoFlush) + +template<> vector Settings::Get>(const string& id, Ref view, BNSettingsScope* scope) { - char** buffer = new char*[value.size()]; - if (!buffer) - return false; - for (size_t i = 0; i < value.size(); i++) - buffer[i] = BNAllocString(value[i].c_str()); + size_t size = 0; + char** outBuffer = (char**)BNSettingsGetStringList(m_registry.c_str(), id.c_str(), view ? view->GetObject() : nullptr, scope, &size); - bool result = BNSettingSetStringList(settingGroup.c_str(), - name.c_str(), - (const char**)buffer, - value.size(), - autoFlush); + vector result; + result.reserve(size); + for (size_t i = 0; i < size; i++) + result.emplace_back(outBuffer[i]); - BNFreeStringList(buffer, value.size()); + BNFreeStringList(outBuffer, size); return result; } -bool Setting::Set(const std::string& settingGroup, - const std::string& name, - double value, - bool autoFlush) + +bool Settings::Set(const string& id, bool value, Ref view, BNSettingsScope scope) +{ + return BNSettingsSetBool(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); +} + + +bool Settings::Set(const string& id, double value, Ref view, BNSettingsScope scope) { - return BNSettingSetDouble(settingGroup.c_str(), name.c_str(), value, autoFlush); + return BNSettingsSetDouble(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); } -bool Setting::RemoveSettingGroup(const std::string& settingGroup, bool autoFlush) + +bool Settings::Set(const string& id, int value, Ref view, BNSettingsScope scope) { - return BNSettingRemoveSettingGroup(settingGroup.c_str(), autoFlush); + return BNSettingsSetInt64(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); } -bool Setting::RemoveSetting(const std::string& settingGroup, const std::string& setting, bool autoFlush) + +bool Settings::Set(const string& id, int64_t value, Ref view, BNSettingsScope scope) { - return BNSettingRemoveSetting(settingGroup.c_str(), setting.c_str(), autoFlush); + return BNSettingsSetInt64(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); } -bool Setting::FlushSettings() + +bool Settings::Set(const string& id, uint64_t value, Ref view, BNSettingsScope scope) { - return BNSettingFlushSettings(); + return BNSettingsSetUInt64(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); +} + + +bool Settings::Set(const string& id, const char* value, Ref view, BNSettingsScope scope) +{ + return BNSettingsSetString(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value); +} + + +bool Settings::Set(const string& id, const string& value, Ref view, BNSettingsScope scope) +{ + return BNSettingsSetString(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), value.c_str()); +} + + +bool Settings::Set(const string& id, const vector& value, Ref view, BNSettingsScope scope) +{ + char** buffer = new char*[value.size()]; + if (!buffer) + return false; + + for (size_t i = 0; i < value.size(); i++) + buffer[i] = BNAllocString(value[i].c_str()); + + bool result = BNSettingsSetStringList(m_registry.c_str(), view ? view->GetObject() : nullptr, scope, id.c_str(), (const char**)buffer, value.size()); + BNFreeStringList(buffer, value.size()); + return result; } -- cgit v1.3.1