2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/helpers/shared_data.hpp>
|
2021-01-11 20:31:40 +01:00
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
/* Settings */
|
|
|
|
|
|
|
|
void ContentRegistry::Settings::load() {
|
2021-01-12 23:28:41 +01:00
|
|
|
std::ifstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json");
|
2021-01-11 20:31:40 +01:00
|
|
|
|
|
|
|
if (settingsFile.good())
|
|
|
|
settingsFile >> getSettingsData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContentRegistry::Settings::store() {
|
2021-01-12 23:28:41 +01:00
|
|
|
std::ofstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc);
|
2021-01-11 20:31:40 +01:00
|
|
|
settingsFile << getSettingsData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, s64 defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
|
|
|
|
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
auto &json = getSettingsData();
|
2021-01-11 20:31:40 +01:00
|
|
|
|
|
|
|
if (!json.contains(category.data()))
|
|
|
|
json[category.data()] = nlohmann::json::object();
|
|
|
|
if (!json[category.data()].contains(name.data()))
|
|
|
|
json[category.data()][name.data()] = defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
|
|
|
|
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
getSettingsData()[category.data()] = nlohmann::json::object();
|
|
|
|
getSettingsData()[category.data()][name.data()] = defaultValue;
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::settingsEntries;
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::settingsJson;
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:11:03 +01:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
auto ContentRegistry::Events::get(std::string_view name) {
|
2021-01-12 23:28:41 +01:00
|
|
|
auto &customEvents = SharedData::customEvents;
|
|
|
|
auto &lastId = SharedData::customEventsLastId;
|
2021-01-11 21:11:03 +01:00
|
|
|
|
|
|
|
if (!customEvents.contains(name.data())) {
|
|
|
|
customEvents[name.data()] = static_cast<hex::Events>(lastId);
|
|
|
|
lastId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return customEvents[name.data()];
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:02:55 +01:00
|
|
|
|
|
|
|
/* Command Palette Commands */
|
|
|
|
|
|
|
|
void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view description, const std::function<std::string(std::string)> &callback) {
|
2021-01-12 16:50:15 +01:00
|
|
|
getEntries().push_back(ContentRegistry::CommandPaletteCommands::Entry{ type, command.data(), description.data(), callback });
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
std::vector<ContentRegistry::CommandPaletteCommands::Entry>& ContentRegistry::CommandPaletteCommands::getEntries() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::commandPaletteCommands;
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:54:12 +01:00
|
|
|
|
|
|
|
/* Pattern Language Functions */
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
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) {
|
2021-01-12 16:50:15 +01:00
|
|
|
getEntries()[name.data()] = Function{ parameterCount, func };
|
2021-01-11 23:54:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& ContentRegistry::PatternLanguageFunctions::getEntries() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::patternLanguageFunctions;
|
2021-01-11 23:54:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
|
|
|
|
/* Views */
|
|
|
|
|
|
|
|
View* ContentRegistry::Views::add(View *view) {
|
2021-01-30 22:39:06 +01:00
|
|
|
return getEntries().emplace_back(view);
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<View*>& ContentRegistry::Views::getEntries() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::views;
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Tools */
|
|
|
|
|
2021-01-13 13:18:03 +01:00
|
|
|
void ContentRegistry::Tools::add(std::string_view name, const std::function<void()> &function) {
|
|
|
|
getEntries().emplace_back(Entry{ name.data(), function });
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:03 +01:00
|
|
|
std::vector<ContentRegistry::Tools::Entry>& ContentRegistry::Tools::getEntries() {
|
2021-01-13 01:24:27 +01:00
|
|
|
return SharedData::toolsEntries;
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 01:24:27 +01:00
|
|
|
|
|
|
|
/* Data Inspector */
|
|
|
|
|
|
|
|
void ContentRegistry::DataInspector::add(std::string_view name, size_t requiredSize, ContentRegistry::DataInspector::GeneratorFunction function) {
|
2021-01-30 22:39:06 +01:00
|
|
|
getEntries().push_back({ name.data(), requiredSize, std::move(function) });
|
2021-01-13 01:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ContentRegistry::DataInspector::Entry>& ContentRegistry::DataInspector::getEntries() {
|
|
|
|
return SharedData::dataInspectorEntries;
|
|
|
|
}
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|