1
0
mirror of synced 2024-12-03 19:57:20 +01:00
ImHex/plugins/libimhex/source/api/content_registry.cpp
WerWolv 3e6865ffa9
sys/build: Properly support per-system metadata file paths (#181)
* sys: Move away from metadata paths next to executable in the application

Build system doesn't properly install / pack stuff yet

* build: Updated README to contain better install instructions

* sys: Search for imhex resource files in ~/Application Support

* sys: MAX_PATH -> PATH_MAX

* sys: Seach for imhex resource files in Application Support using NSFileManager (#180)

* sys: Allow for multiple file search paths

Also use install prefix instead of just /usr on Linux

* build: Fixed IMHEX_INSTALL_PREFIX macro definition

* build: Fix duplicate switch entry on Linux

* docs: Updated readme to properly reflect new paths and dependencies

* sys: Install files in their proper paths on linux (#183)

* Install files in their proper paths on linux

* Only create user directories

* Follow the XDG specification on linux

XDG specification specifies how to find config and data directories on
linux systems. Specifically, it says this:

- Data should be written to $XDG_DATA_HOME
- Config should be written to $XDG_CONFIG_HOME
- Data should be read from $XDG_DATA_HOME:$XDG_DATA_DIRS
- Config should be read from $XDG_CONFIG_HOME:$XDG_CONFIG_DIRS

The default values are this:

- XDG_DATA_HOME: $HOME/.local/share
- XDG_CONFIG_HOME: $HOME/.config
- XDG_DATA_DIRS: /usr/share:/usr/local/share
- XDG_CONFIG_DIRS: /etc/xdg

Platforms with non-standard filesystems (like NixOS) will correctly set
up those environment variables, allowing softwares to work unmodified.

In order to make integration as simple as possible, we use a simple
header-only dependency called XDGPP which does all the hard work for us
to find the default directories.

* Look for plugins in all Plugin Paths

If the plugin folder was missing from one of the PluginPaths, we would
immediately stop loading plugins. We now keep looking even if one of the
path is missing.

Co-authored-by: Nichole Mattera <me@nicholemattera.com>
Co-authored-by: Robin Lambertz <unfiltered@roblab.la>
2021-03-01 08:56:49 +01:00

255 lines
9.9 KiB
C++

#include <hex/api/content_registry.hpp>
#include <hex/helpers/shared_data.hpp>
#include <filesystem>
#include <fstream>
namespace hex {
/* Settings */
void ContentRegistry::Settings::load() {
for (const auto &dir : hex::getPath(ImHexPath::Config)) {
std::ifstream settingsFile(dir + "/settings.json");
if (settingsFile.good()) {
settingsFile >> getSettingsData();
break;
}
}
}
void ContentRegistry::Settings::store() {
for (const auto &dir : hex::getPath(ImHexPath::Config)) {
std::ofstream settingsFile(dir + "/settings.json", std::ios::trunc);
if (settingsFile.good()) {
settingsFile << getSettingsData();
break;
}
}
}
void ContentRegistry::Settings::add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 defaultValue, const std::function<bool(std::string_view, nlohmann::json&)> &callback) {
ContentRegistry::Settings::getEntries()[unlocalizedCategory.data()].emplace_back(Entry{ unlocalizedName.data(), callback });
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
json[unlocalizedCategory.data()][unlocalizedName.data()] = defaultValue;
}
void ContentRegistry::Settings::add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view defaultValue, const std::function<bool(std::string_view, nlohmann::json&)> &callback) {
ContentRegistry::Settings::getEntries()[unlocalizedCategory.data()].emplace_back(Entry{ unlocalizedName.data(), callback });
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
json[unlocalizedCategory.data()][unlocalizedName.data()] = defaultValue;
}
void ContentRegistry::Settings::write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 value) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
json[unlocalizedCategory.data()][unlocalizedName.data()] = value;
}
void ContentRegistry::Settings::write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view value) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
json[unlocalizedCategory.data()][unlocalizedName.data()] = value;
}
void ContentRegistry::Settings::write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& value) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
json[unlocalizedCategory.data()][unlocalizedName.data()] = value;
}
s64 ContentRegistry::Settings::read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 defaultValue) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
return defaultValue;
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
return defaultValue;
return json[unlocalizedCategory.data()][unlocalizedName.data()].get<s64>();
}
std::string ContentRegistry::Settings::read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view defaultValue) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
return defaultValue.data();
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
return defaultValue.data();
return json[unlocalizedCategory.data()][unlocalizedName.data()].get<std::string>();
}
std::vector<std::string> ContentRegistry::Settings::read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& defaultValue) {
auto &json = getSettingsData();
if (!json.contains(unlocalizedCategory.data()))
return defaultValue;
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
return defaultValue;
return json[unlocalizedCategory.data()][unlocalizedName.data()].get<std::vector<std::string>>();
}
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
return SharedData::settingsEntries;
}
std::optional<nlohmann::json> ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) {
auto &settings = getSettingsData();
if (!settings.contains(unlocalizedCategory)) return { };
if (!settings[unlocalizedCategory.data()].contains(unlocalizedName)) return { };
return settings[unlocalizedCategory.data()][unlocalizedName.data()];
}
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
return SharedData::settingsJson;
}
/* Events */
auto ContentRegistry::Events::get(std::string_view name) {
auto &customEvents = SharedData::customEvents;
auto &lastId = SharedData::customEventsLastId;
if (!customEvents.contains(name.data())) {
customEvents[name.data()] = static_cast<hex::Events>(lastId);
lastId++;
}
return customEvents[name.data()];
}
/* Command Palette Commands */
void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view unlocalizedDescription, const std::function<std::string(std::string)> &displayCallback, const std::function<void(std::string)> &executeCallback) {
getEntries().push_back(ContentRegistry::CommandPaletteCommands::Entry{ type, command.data(), unlocalizedDescription.data(), displayCallback, executeCallback });
}
std::vector<ContentRegistry::CommandPaletteCommands::Entry>& ContentRegistry::CommandPaletteCommands::getEntries() {
return SharedData::commandPaletteCommands;
}
/* Pattern Language Functions */
void ContentRegistry::PatternLanguageFunctions::add(std::string_view name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>)> &func) {
getEntries()[name.data()] = Function{ parameterCount, func };
}
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& ContentRegistry::PatternLanguageFunctions::getEntries() {
return SharedData::patternLanguageFunctions;
}
/* Views */
void ContentRegistry::Views::add(std::unique_ptr<View> &&view) {
getEntries().emplace_back(std::move(view));
}
std::vector<std::unique_ptr<View>>& ContentRegistry::Views::getEntries() {
return SharedData::views;
}
/* Tools */
void ContentRegistry::Tools:: add(std::string_view unlocalizedName, const std::function<void()> &function) {
getEntries().emplace_back(Entry{ unlocalizedName.data(), function });
}
std::vector<ContentRegistry::Tools::Entry>& ContentRegistry::Tools::getEntries() {
return SharedData::toolsEntries;
}
/* Data Inspector */
void ContentRegistry::DataInspector::add(std::string_view unlocalizedName, size_t requiredSize, ContentRegistry::DataInspector::GeneratorFunction function) {
getEntries().push_back({ unlocalizedName.data(), requiredSize, std::move(function) });
}
std::vector<ContentRegistry::DataInspector::Entry>& ContentRegistry::DataInspector::getEntries() {
return SharedData::dataInspectorEntries;
}
/* Data Processor Nodes */
void ContentRegistry::DataProcessorNode::add(const Entry &entry) {
getEntries().push_back(entry);
}
void ContentRegistry::DataProcessorNode::addSeparator() {
getEntries().push_back({ "", "", []{ return nullptr; } });
}
std::vector<ContentRegistry::DataProcessorNode::Entry>& ContentRegistry::DataProcessorNode::getEntries() {
return SharedData::dataProcessorNodes;
}
/* Languages */
void ContentRegistry::Language::registerLanguage(std::string_view name, std::string_view languageCode) {
getLanguages().insert({ languageCode.data(), name.data() });
}
void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) {
getLanguageDefinitions()[languageCode.data()].push_back(definition);
EventManager::post(hex::Events::SettingsChanged, {});
}
std::map<std::string, std::string>& ContentRegistry::Language::getLanguages() {
return SharedData::languageNames;
}
std::map<std::string, std::vector<LanguageDefinition>>& ContentRegistry::Language::getLanguageDefinitions() {
return SharedData::languageDefinitions;
}
void ContentRegistry::Interface::addWelcomeScreenEntry(const ContentRegistry::Interface::DrawCallback &function) {
getWelcomeScreenEntries().push_back(function);
}
void ContentRegistry::Interface::addFooterItem(const ContentRegistry::Interface::DrawCallback &function){
getFooterItems().push_back(function);
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getWelcomeScreenEntries() {
return SharedData::welcomeScreenEntries;
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getFooterItems() {
return SharedData::footerItems;
}
}