impr: Various web build improvements, API cleanup (#1541)

This commit is contained in:
Nik
2024-02-10 23:31:05 +01:00
committed by GitHub
parent 4b20e35fd2
commit bcbcb1f23c
43 changed files with 977 additions and 847 deletions

View File

@@ -7,41 +7,43 @@
namespace hex {
std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>> &AchievementManager::getAchievements() {
static AutoReset<std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>>> achievements;
return achievements;
static AutoReset<std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>>> s_achievements;
const std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>> &AchievementManager::getAchievements() {
return *s_achievements;
}
std::unordered_map<std::string, std::list<AchievementManager::AchievementNode>>& AchievementManager::getAchievementNodes(bool rebuild) {
static AutoReset<std::unordered_map<std::string, std::list<AchievementNode>>> nodeCategoryStorage;
static AutoReset<std::unordered_map<std::string, std::list<AchievementManager::AchievementNode>>> s_nodeCategoryStorage;
std::unordered_map<std::string, std::list<AchievementManager::AchievementNode>>& getAchievementNodesMutable(bool rebuild) {
if (!s_nodeCategoryStorage->empty() || !rebuild)
return s_nodeCategoryStorage;
if (!nodeCategoryStorage->empty() || !rebuild)
return nodeCategoryStorage;
nodeCategoryStorage->clear();
s_nodeCategoryStorage->clear();
// Add all achievements to the node storage
for (auto &[categoryName, achievements] : getAchievements()) {
auto &nodes = (*nodeCategoryStorage)[categoryName];
for (auto &[categoryName, achievements] : AchievementManager::getAchievements()) {
auto &nodes = (*s_nodeCategoryStorage)[categoryName];
for (auto &[achievementName, achievement] : achievements) {
nodes.emplace_back(achievement.get());
}
}
return nodeCategoryStorage;
return s_nodeCategoryStorage;
}
std::unordered_map<std::string, std::vector<AchievementManager::AchievementNode*>>& AchievementManager::getAchievementStartNodes(bool rebuild) {
static AutoReset<std::unordered_map<std::string, std::vector<AchievementNode*>>> startNodes;
const std::unordered_map<std::string, std::list<AchievementManager::AchievementNode>>& AchievementManager::getAchievementNodes(bool rebuild) {
return getAchievementNodesMutable(rebuild);
}
if (!startNodes->empty() || !rebuild)
return startNodes;
static AutoReset<std::unordered_map<std::string, std::vector<AchievementManager::AchievementNode*>>> s_startNodes;
const std::unordered_map<std::string, std::vector<AchievementManager::AchievementNode*>>& AchievementManager::getAchievementStartNodes(bool rebuild) {
auto &nodeCategoryStorage = getAchievementNodes();
if (!s_startNodes->empty() || !rebuild)
return s_startNodes;
startNodes->clear();
auto &nodeCategoryStorage = getAchievementNodesMutable(rebuild);
s_startNodes->clear();
// Add all parents and children to the nodes
for (auto &[categoryName, achievements] : nodeCategoryStorage) {
@@ -76,17 +78,17 @@ namespace hex {
for (auto &[categoryName, achievements] : nodeCategoryStorage) {
for (auto &achievementNode : achievements) {
if (!achievementNode.hasParents()) {
(*startNodes)[categoryName].emplace_back(&achievementNode);
(*s_startNodes)[categoryName].emplace_back(&achievementNode);
}
for (const auto &parent : achievementNode.parents) {
if (parent->achievement->getUnlocalizedCategory() != achievementNode.achievement->getUnlocalizedCategory())
(*startNodes)[categoryName].emplace_back(&achievementNode);
(*s_startNodes)[categoryName].emplace_back(&achievementNode);
}
}
}
return startNodes;
return s_startNodes;
}
void AchievementManager::unlockAchievement(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName) {
@@ -104,8 +106,11 @@ namespace hex {
return;
}
const auto &nodes = getAchievementNodes()[categoryName];
for (const auto &node : nodes) {
const auto &nodes = getAchievementNodes();
if (!nodes.contains(categoryName))
return;
for (const auto &node : nodes.at(categoryName)) {
auto &achievement = node.achievement;
if (achievement->getUnlocalizedCategory() != unlocalizedCategory) {
@@ -134,14 +139,8 @@ namespace hex {
}
}
void AchievementManager::clear() {
getAchievements().clear();
getAchievementStartNodes(false).clear();
getAchievementNodes(false).clear();
}
void AchievementManager::clearTemporary() {
auto &categories = getAchievements();
auto &categories = *s_achievements;
for (auto &[categoryName, achievements] : categories) {
std::erase_if(achievements, [](auto &data) {
auto &[achievementName, achievement] = data;
@@ -154,8 +153,8 @@ namespace hex {
return achievements.empty();
});
getAchievementStartNodes(false).clear();
getAchievementNodes(false).clear();
s_startNodes->clear();
s_nodeCategoryStorage->clear();
}
std::pair<u32, u32> AchievementManager::getProgress() {
@@ -175,10 +174,26 @@ namespace hex {
}
void AchievementManager::achievementAdded() {
getAchievementStartNodes(false).clear();
getAchievementNodes(false).clear();
s_startNodes->clear();
s_nodeCategoryStorage->clear();
}
Achievement &AchievementManager::addAchievementImpl(std::unique_ptr<Achievement> &&newAchievement) {
const auto &category = newAchievement->getUnlocalizedCategory();
const auto &name = newAchievement->getUnlocalizedName();
auto [categoryIter, categoryInserted] = s_achievements->insert({ category, std::unordered_map<std::string, std::unique_ptr<Achievement>>{} });
auto &[categoryKey, achievements] = *categoryIter;
auto [achievementIter, achievementInserted] = achievements.insert({ name, std::move(newAchievement) });
auto &[achievementKey, achievement] = *achievementIter;
achievementAdded();
return *achievement;
}
constexpr static auto AchievementsFile = "achievements.json";
void AchievementManager::loadProgress() {

View File

@@ -28,8 +28,13 @@ namespace hex {
namespace impl {
static AutoReset<nlohmann::json> s_settings;
const nlohmann::json& getSettingsData() {
return s_settings;
}
nlohmann::json& getSetting(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &defaultValue) {
auto &settings = getSettingsData();
auto &settings = *s_settings;
if (!settings.contains(unlocalizedCategory))
settings[unlocalizedCategory] = {};
@@ -40,12 +45,6 @@ namespace hex {
return settings[unlocalizedCategory][unlocalizedName];
}
nlohmann::json &getSettingsData() {
static AutoReset<nlohmann::json> settings;
return settings;
}
#if defined(OS_WEB)
void load() {
char *data = (char *) MAIN_THREAD_EM_ASM_INT({
@@ -56,12 +55,12 @@ namespace hex {
if (data == nullptr) {
store();
} else {
getSettingsData() = nlohmann::json::parse(data);
s_settings = nlohmann::json::parse(data);
}
}
void store() {
auto data = getSettingsData().dump();
auto data = s_settings->dump();
MAIN_THREAD_EM_ASM({
localStorage.setItem("config", UTF8ToString($0));
}, data.c_str());
@@ -72,14 +71,16 @@ namespace hex {
localStorage.removeItem("config");
});
}
#else
void load() {
bool loaded = false;
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Read);
if (file.isValid()) {
getSettingsData() = nlohmann::json::parse(file.readString());
s_settings = nlohmann::json::parse(file.readString());
loaded = true;
break;
}
@@ -139,14 +140,13 @@ namespace hex {
return foundEntry;
}
std::vector<Category> &getSettings() {
static AutoReset<std::vector<Category>> categories;
return categories;
static AutoReset<std::vector<Category>> s_categories;
const std::vector<Category>& getSettings() {
return *s_categories;
}
Widgets::Widget* add(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedSubCategory, const UnlocalizedString &unlocalizedName, std::unique_ptr<Widgets::Widget> &&widget) {
const auto category = insertOrGetEntry(getSettings(), unlocalizedCategory);
const auto category = insertOrGetEntry(*s_categories, unlocalizedCategory);
const auto subCategory = insertOrGetEntry(category->subCategories, unlocalizedSubCategory);
const auto entry = insertOrGetEntry(subCategory->entries, unlocalizedName);
@@ -163,7 +163,7 @@ namespace hex {
}
void setCategoryDescription(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedDescription) {
const auto category = insertOrGetEntry(impl::getSettings(), unlocalizedCategory);
const auto category = insertOrGetEntry(*impl::s_categories, unlocalizedCategory);
category->unlocalizedDescription = unlocalizedDescription;
}
@@ -386,32 +386,30 @@ namespace hex {
namespace ContentRegistry::CommandPaletteCommands {
namespace impl {
static AutoReset<std::vector<Entry>> s_entries;
const std::vector<Entry>& getEntries() {
return *s_entries;
}
static AutoReset<std::vector<Handler>> s_handlers;
const std::vector<Handler>& getHandlers() {
return *s_handlers;
}
}
void add(Type type, const std::string &command, const UnlocalizedString &unlocalizedDescription, const impl::DisplayCallback &displayCallback, const impl::ExecuteCallback &executeCallback) {
log::debug("Registered new command palette command: {}", command);
impl::getEntries().push_back(impl::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback });
impl::s_entries->push_back(impl::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback });
}
void addHandler(Type type, const std::string &command, const impl::QueryCallback &queryCallback, const impl::DisplayCallback &displayCallback) {
log::debug("Registered new command palette command handler: {}", command);
impl::getHandlers().push_back(impl::Handler { type, command, queryCallback, displayCallback });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> commands;
return commands;
}
std::vector<Handler> &getHandlers() {
static AutoReset<std::vector<Handler>> commands;
return commands;
}
impl::s_handlers->push_back(impl::Handler { type, command, queryCallback, displayCallback });
}
}
@@ -419,6 +417,30 @@ namespace hex {
namespace ContentRegistry::PatternLanguage {
namespace impl {
static AutoReset<std::map<std::string, Visualizer>> s_visualizers;
const std::map<std::string, Visualizer>& getVisualizers() {
return *s_visualizers;
}
static AutoReset<std::map<std::string, Visualizer>> s_inlineVisualizers;
const std::map<std::string, Visualizer>& getInlineVisualizers() {
return *s_inlineVisualizers;
}
static AutoReset<std::map<std::string, pl::api::PragmaHandler>> s_pragmas;
const std::map<std::string, pl::api::PragmaHandler>& getPragmas() {
return *s_pragmas;
}
static AutoReset<std::vector<FunctionDefinition>> s_functions;
const std::vector<FunctionDefinition>& getFunctions() {
return *s_functions;
}
}
static std::string getFunctionName(const pl::api::Namespace &ns, const std::string &name) {
std::string functionName;
for (auto &scope : ns)
@@ -476,13 +498,13 @@ namespace hex {
void addPragma(const std::string &name, const pl::api::PragmaHandler &handler) {
log::debug("Registered new pattern language pragma: {}", name);
impl::getPragmas()[name] = handler;
(*impl::s_pragmas)[name] = handler;
}
void addFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) {
log::debug("Registered new pattern language function: {}", getFunctionName(ns, name));
impl::getFunctions().push_back({
impl::s_functions->push_back({
ns, name,
parameterCount, func,
false
@@ -492,7 +514,7 @@ namespace hex {
void addDangerousFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) {
log::debug("Registered new dangerous pattern language function: {}", getFunctionName(ns, name));
impl::getFunctions().push_back({
impl::s_functions->push_back({
ns, name,
parameterCount, func,
true
@@ -502,44 +524,14 @@ namespace hex {
void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) {
log::debug("Registered new pattern visualizer function: {}", name);
impl::getVisualizers()[name] = impl::Visualizer { parameterCount, function };
(*impl::s_visualizers)[name] = impl::Visualizer { parameterCount, function };
}
void addInlineVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) {
log::debug("Registered new inline pattern visualizer function: {}", name);
impl::getInlineVisualizers()[name] = impl::Visualizer { parameterCount, function };
(*impl::s_inlineVisualizers)[name] = impl::Visualizer { parameterCount, function };
}
namespace impl {
std::map<std::string, Visualizer> &getVisualizers() {
static AutoReset<std::map<std::string, Visualizer>> visualizers;
return visualizers;
}
std::map<std::string, Visualizer> &getInlineVisualizers() {
static AutoReset<std::map<std::string, Visualizer>> visualizers;
return visualizers;
}
std::map<std::string, pl::api::PragmaHandler> &getPragmas() {
static AutoReset<std::map<std::string, pl::api::PragmaHandler>> pragmas;
return pragmas;
}
std::vector<FunctionDefinition> &getFunctions() {
static AutoReset<std::vector<FunctionDefinition>> functions;
return functions;
}
}
}
@@ -547,22 +539,21 @@ namespace hex {
namespace impl {
std::map<std::string, std::unique_ptr<View>> &getEntries() {
static AutoReset<std::map<std::string, std::unique_ptr<View>>> views;
return views;
static AutoReset<std::map<std::string, std::unique_ptr<View>>> s_views;
const std::map<std::string, std::unique_ptr<View>>& getEntries() {
return *s_views;
}
}
void impl::add(std::unique_ptr<View> &&view) {
void add(std::unique_ptr<View> &&view) {
log::debug("Registered new view: {}", view->getUnlocalizedName().get());
getEntries().insert({ view->getUnlocalizedName(), std::move(view) });
s_views->insert({ view->getUnlocalizedName(), std::move(view) });
}
}
View* getViewByName(const UnlocalizedString &unlocalizedName) {
auto &views = impl::getEntries();
auto &views = *impl::s_views;
if (views.contains(unlocalizedName))
return views[unlocalizedName].get();
@@ -574,77 +565,87 @@ namespace hex {
namespace ContentRegistry::Tools {
namespace impl {
static AutoReset<std::vector<Entry>> s_tools;
const std::vector<Entry>& getEntries() {
return *s_tools;
}
}
void add(const UnlocalizedString &unlocalizedName, const impl::Callback &function) {
log::debug("Registered new tool: {}", unlocalizedName.get());
impl::getEntries().emplace_back(impl::Entry { unlocalizedName, function, false });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> tools;
return tools;
}
impl::s_tools->emplace_back(impl::Entry { unlocalizedName, function });
}
}
namespace ContentRegistry::DataInspector {
namespace impl {
static AutoReset<std::vector<Entry>> s_entries;
const std::vector<Entry>& getEntries() {
return *s_entries;
}
}
void add(const UnlocalizedString &unlocalizedName, size_t requiredSize, impl::GeneratorFunction displayGeneratorFunction, std::optional<impl::EditingFunction> editingFunction) {
log::debug("Registered new data inspector format: {}", unlocalizedName.get());
impl::getEntries().push_back({ unlocalizedName, requiredSize, requiredSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
impl::s_entries->push_back({ unlocalizedName, requiredSize, requiredSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
}
void add(const UnlocalizedString &unlocalizedName, size_t requiredSize, size_t maxSize, impl::GeneratorFunction displayGeneratorFunction, std::optional<impl::EditingFunction> editingFunction) {
log::debug("Registered new data inspector format: {}", unlocalizedName.get());
impl::getEntries().push_back({ unlocalizedName, requiredSize, maxSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
impl::s_entries->push_back({ unlocalizedName, requiredSize, maxSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> entries;
return entries;
}
}
}
namespace ContentRegistry::DataProcessorNode {
void impl::add(const Entry &entry) {
log::debug("Registered new data processor node type: [{}]: {}", entry.unlocalizedCategory.get(), entry.unlocalizedName.get());
namespace impl {
static AutoReset<std::vector<Entry>> s_nodes;
const std::vector<Entry>& getEntries() {
return *s_nodes;
}
void add(const Entry &entry) {
log::debug("Registered new data processor node type: [{}]: {}", entry.unlocalizedCategory.get(), entry.unlocalizedName.get());
s_nodes->push_back(entry);
}
getEntries().push_back(entry);
}
void addSeparator() {
impl::getEntries().push_back({ "", "", [] { return nullptr; } });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> nodes;
return nodes;
}
impl::s_nodes->push_back({ "", "", [] { return nullptr; } });
}
}
namespace ContentRegistry::Language {
namespace impl {
static AutoReset<std::map<std::string, std::string>> s_languages;
const std::map<std::string, std::string>& getLanguages() {
return *s_languages;
}
static AutoReset<std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>>> s_definitions;
const std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>>& getLanguageDefinitions() {
return *s_definitions;
}
}
void addLocalization(const nlohmann::json &data) {
if (!data.is_object())
return;
@@ -671,7 +672,7 @@ namespace hex {
LocalizationManager::impl::setFallbackLanguage(code.get<std::string>());
}
impl::getLanguages().insert({ code.get<std::string>(), hex::format("{} ({})", language.get<std::string>(), country.get<std::string>()) });
impl::s_languages->insert({ code.get<std::string>(), hex::format("{} ({})", language.get<std::string>(), country.get<std::string>()) });
std::map<std::string, std::string> translationDefinitions;
for (auto &[key, value] : translations.items()) {
@@ -683,34 +684,60 @@ namespace hex {
translationDefinitions[key] = value.get<std::string>();
}
impl::getLanguageDefinitions()[code.get<std::string>()].emplace_back(std::move(translationDefinitions));
(*impl::s_definitions)[code.get<std::string>()].emplace_back(std::move(translationDefinitions));
}
namespace impl {
std::map<std::string, std::string> &getLanguages() {
static AutoReset<std::map<std::string, std::string>> languages;
return languages;
}
std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>> &getLanguageDefinitions() {
static AutoReset<std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>>> definitions;
return definitions;
}
}
}
namespace ContentRegistry::Interface {
namespace impl {
static AutoReset<std::multimap<u32, MainMenuItem>> s_mainMenuItems;
const std::multimap<u32, MainMenuItem>& getMainMenuItems() {
return *s_mainMenuItems;
}
static AutoReset<std::multimap<u32, MenuItem>> s_menuItems;
const std::multimap<u32, MenuItem>& getMenuItems() {
return *s_menuItems;
}
std::multimap<u32, MenuItem>& getMenuItemsMutable() {
return *s_menuItems;
}
static AutoReset<std::vector<DrawCallback>> s_welcomeScreenEntries;
const std::vector<DrawCallback>& getWelcomeScreenEntries() {
return *s_welcomeScreenEntries;
}
static AutoReset<std::vector<DrawCallback>> s_footerItems;
const std::vector<DrawCallback>& getFooterItems() {
return *s_footerItems;
}
static AutoReset<std::vector<DrawCallback>> s_toolbarItems;
const std::vector<DrawCallback>& getToolbarItems() {
return *s_toolbarItems;
}
static AutoReset<std::vector<SidebarItem>> s_sidebarItems;
const std::vector<SidebarItem>& getSidebarItems() {
return *s_sidebarItems;
}
static AutoReset<std::vector<TitleBarButton>> s_titlebarButtons;
const std::vector<TitleBarButton>& getTitlebarButtons() {
return *s_titlebarButtons;
}
}
void registerMainMenuItem(const UnlocalizedString &unlocalizedName, u32 priority) {
log::debug("Registered new main menu item: {}", unlocalizedName.get());
impl::getMainMenuItems().insert({ priority, { unlocalizedName } });
impl::s_mainMenuItems->insert({ priority, { unlocalizedName } });
}
void addMenuItem(const std::vector<UnlocalizedString> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, const impl::SelectedCallback &selectedCallback, View *view) {
@@ -732,7 +759,7 @@ namespace hex {
if (coloredIcon.color == 0x00)
coloredIcon.color = ImGuiCustomCol_ToolbarGray;
impl::getMenuItems().insert({
impl::s_menuItems->insert({
priority, impl::MenuItem { unlocalizedMainMenuNames, coloredIcon, std::make_unique<Shortcut>(shortcut), view, function, enabledCallback, selectedCallback, -1 }
});
@@ -752,28 +779,28 @@ namespace hex {
log::debug("Added new menu item sub menu to menu {} with priority {}", unlocalizedMainMenuNames[0].get(), priority);
unlocalizedMainMenuNames.emplace_back(impl::SubMenuValue);
impl::getMenuItems().insert({
impl::s_menuItems->insert({
priority, impl::MenuItem { unlocalizedMainMenuNames, icon, std::make_unique<Shortcut>(), nullptr, function, enabledCallback, []{ return false; }, -1 }
});
}
void addMenuItemSeparator(std::vector<UnlocalizedString> unlocalizedMainMenuNames, u32 priority) {
unlocalizedMainMenuNames.emplace_back(impl::SeparatorValue);
impl::getMenuItems().insert({
impl::s_menuItems->insert({
priority, impl::MenuItem { unlocalizedMainMenuNames, "", std::make_unique<Shortcut>(), nullptr, []{}, []{ return true; }, []{ return false; }, -1 }
});
}
void addWelcomeScreenEntry(const impl::DrawCallback &function) {
impl::getWelcomeScreenEntries().push_back(function);
impl::s_welcomeScreenEntries->push_back(function);
}
void addFooterItem(const impl::DrawCallback &function) {
impl::getFooterItems().push_back(function);
impl::s_footerItems->push_back(function);
}
void addToolbarItem(const impl::DrawCallback &function) {
impl::getToolbarItems().push_back(function);
impl::s_toolbarItems->push_back(function);
}
void addMenuItemToToolbar(const UnlocalizedString& unlocalizedName, ImGuiCustomCol color) {
@@ -781,7 +808,7 @@ namespace hex {
return a.second.toolbarIndex < b.second.toolbarIndex;
})->second.toolbarIndex;
for (auto &[priority, menuItem] : impl::getMenuItems()) {
for (auto &[priority, menuItem] : *impl::s_menuItems) {
if (menuItem.unlocalizedNames.back() == unlocalizedName) {
menuItem.toolbarIndex = maxIndex + 1;
menuItem.icon.color = color;
@@ -792,52 +819,11 @@ namespace hex {
void addSidebarItem(const std::string &icon, const impl::DrawCallback &function, const impl::EnabledCallback &enabledCallback) {
impl::getSidebarItems().push_back({ icon, function, enabledCallback });
impl::s_sidebarItems->push_back({ icon, function, enabledCallback });
}
void addTitleBarButton(const std::string &icon, const UnlocalizedString &unlocalizedTooltip, const impl::ClickCallback &function) {
impl::getTitleBarButtons().push_back({ icon, unlocalizedTooltip, function });
}
namespace impl {
std::multimap<u32, MainMenuItem> &getMainMenuItems() {
static AutoReset<std::multimap<u32, MainMenuItem>> items;
return items;
}
std::multimap<u32, MenuItem> &getMenuItems() {
static AutoReset<std::multimap<u32, MenuItem>> items;
return items;
}
std::vector<DrawCallback> &getWelcomeScreenEntries() {
static AutoReset<std::vector<DrawCallback>> entries;
return entries;
}
std::vector<DrawCallback> &getFooterItems() {
static AutoReset<std::vector<DrawCallback>> items;
return items;
}
std::vector<DrawCallback> &getToolbarItems() {
static AutoReset<std::vector<DrawCallback>> items;
return items;
}
std::vector<SidebarItem> &getSidebarItems() {
static AutoReset<std::vector<SidebarItem>> items;
return items;
}
std::vector<TitleBarButton> &getTitleBarButtons() {
static AutoReset<std::vector<TitleBarButton>> buttons;
return buttons;
}
impl::s_titlebarButtons->push_back({ icon, unlocalizedTooltip, function });
}
}
@@ -850,25 +836,24 @@ namespace hex {
(void)RequestCreateProvider::subscribe([expectedName = typeName, creationFunction](const std::string &name, bool skipLoadInterface, bool selectProvider, prv::Provider **provider) {
if (name != expectedName) return;
prv::Provider *newProvider = creationFunction();
auto newProvider = creationFunction();
ImHexApi::Provider::add(newProvider, skipLoadInterface, selectProvider);
if (provider != nullptr)
*provider = newProvider;
if (provider != nullptr) {
*provider = newProvider.get();
ImHexApi::Provider::add(std::move(newProvider), skipLoadInterface, selectProvider);
}
});
}
std::vector<std::string> &getEntries() {
static AutoReset<std::vector<std::string>> providerNames;
return providerNames;
static AutoReset<std::vector<std::string>> s_providerNames;
const std::vector<std::string>& getEntries() {
return *s_providerNames;
}
void addProviderName(const UnlocalizedString &unlocalizedName) {
log::debug("Registered new provider: {}", unlocalizedName.get());
getEntries().push_back(unlocalizedName);
s_providerNames->push_back(unlocalizedName);
}
}
@@ -878,41 +863,39 @@ namespace hex {
namespace ContentRegistry::DataFormatter {
namespace impl {
static AutoReset<std::vector<Entry>> s_entries;
const std::vector<Entry>& getEntries() {
return *s_entries;
}
}
void add(const UnlocalizedString &unlocalizedName, const impl::Callback &callback) {
log::debug("Registered new data formatter: {}", unlocalizedName.get());
impl::getEntries().push_back({ unlocalizedName, callback });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> entries;
return entries;
}
impl::s_entries->push_back({ unlocalizedName, callback });
}
}
namespace ContentRegistry::FileHandler {
namespace impl {
static AutoReset<std::vector<Entry>> s_entries;
const std::vector<Entry>& getEntries() {
return *s_entries;
}
}
void add(const std::vector<std::string> &extensions, const impl::Callback &callback) {
for (const auto &extension : extensions)
log::debug("Registered new data handler for extensions: {}", extension);
impl::getEntries().push_back({ extensions, callback });
}
namespace impl {
std::vector<Entry> &getEntries() {
static AutoReset<std::vector<Entry>> entries;
return entries;
}
impl::s_entries->push_back({ extensions, callback });
}
}
@@ -985,23 +968,21 @@ namespace hex {
namespace impl {
static AutoReset<std::vector<std::shared_ptr<DataVisualizer>>> s_visualizers;
const std::vector<std::shared_ptr<DataVisualizer>>& getVisualizers() {
return *s_visualizers;
}
static AutoReset<std::vector<std::shared_ptr<MiniMapVisualizer>>> s_miniMapVisualizers;
const std::vector<std::shared_ptr<MiniMapVisualizer>>& getMiniMapVisualizers() {
return *s_miniMapVisualizers;
}
void addDataVisualizer(std::shared_ptr<DataVisualizer> &&visualizer) {
getVisualizers().emplace_back(std::move(visualizer));
s_visualizers->emplace_back(std::move(visualizer));
}
std::vector<std::shared_ptr<DataVisualizer>> &getVisualizers() {
static AutoReset<std::vector<std::shared_ptr<DataVisualizer>>> visualizers;
return visualizers;
}
std::vector<std::shared_ptr<MiniMapVisualizer>> &getMiniMapVisualizers() {
static AutoReset<std::vector<std::shared_ptr<MiniMapVisualizer>>> visualizers;
return visualizers;
}
}
std::shared_ptr<DataVisualizer> getVisualizerByName(const UnlocalizedString &unlocalizedName) {
@@ -1014,7 +995,7 @@ namespace hex {
}
void addMiniMapVisualizer(UnlocalizedString unlocalizedName, MiniMapVisualizer::Callback callback) {
impl::getMiniMapVisualizers().emplace_back(std::make_shared<MiniMapVisualizer>(std::move(unlocalizedName), std::move(callback)));
impl::s_miniMapVisualizers->emplace_back(std::make_shared<MiniMapVisualizer>(std::move(unlocalizedName), std::move(callback)));
}
}
@@ -1023,14 +1004,13 @@ namespace hex {
namespace impl {
std::vector<std::unique_ptr<Algorithm>>& getAlgorithms() {
static AutoReset<std::vector<std::unique_ptr<Algorithm>>> algorithms;
return algorithms;
static AutoReset<std::vector<std::unique_ptr<Algorithm>>> s_algorithms;
const std::vector<std::unique_ptr<Algorithm>>& getAlgorithms() {
return *s_algorithms;
}
void addAlgorithm(std::unique_ptr<Algorithm> &&hash) {
getAlgorithms().emplace_back(std::move(hash));
s_algorithms->emplace_back(std::move(hash));
}
}
@@ -1041,14 +1021,13 @@ namespace hex {
namespace impl {
std::vector<std::unique_ptr<Hash>> &getHashes() {
static AutoReset<std::vector<std::unique_ptr<Hash>>> hashes;
return hashes;
static AutoReset<std::vector<std::unique_ptr<Hash>>> s_hashes;
const std::vector<std::unique_ptr<Hash>>& getHashes() {
return *s_hashes;
}
void add(std::unique_ptr<Hash> &&hash) {
getHashes().emplace_back(std::move(hash));
s_hashes->emplace_back(std::move(hash));
}
}
@@ -1073,11 +1052,11 @@ namespace hex {
Service& operator=(const Service&) = delete;
Service& operator=(Service &&) = default;
[[nodiscard]] const std::string &getName() const {
[[nodiscard]] const std::string& getName() const {
return m_name;
}
[[nodiscard]] const std::jthread &getThread() const {
[[nodiscard]] const std::jthread& getThread() const {
return m_thread;
}
@@ -1086,15 +1065,13 @@ namespace hex {
std::jthread m_thread;
};
std::vector<Service> &getServices() {
static AutoReset<std::vector<Service>> services;
return services;
static AutoReset<std::vector<Service>> s_services;
const std::vector<Service>& getServices() {
return *s_services;
}
void stopServices() {
auto &services = getServices();
services.clear();
s_services->clear();
}
}
@@ -1102,7 +1079,7 @@ namespace hex {
void registerService(const UnlocalizedString &unlocalizedName, const impl::Callback &callback) {
log::debug("Registered new background service: {}", unlocalizedName.get());
impl::getServices().emplace_back(
impl::s_services->emplace_back(
unlocalizedName,
std::jthread([callback = auto(callback)](const std::stop_token &stopToken){
while (!stopToken.stop_requested()) {
@@ -1119,10 +1096,9 @@ namespace hex {
namespace impl {
std::map<std::string, NetworkCallback> &getNetworkEndpoints() {
static AutoReset<std::map<std::string, NetworkCallback>> endpoints;
return endpoints;
static AutoReset<std::map<std::string, NetworkCallback>> s_endpoints;
const std::map<std::string, NetworkCallback>& getNetworkEndpoints() {
return *s_endpoints;
}
}
@@ -1130,7 +1106,7 @@ namespace hex {
void registerNetworkEndpoint(const std::string &endpoint, const impl::NetworkCallback &callback) {
log::debug("Registered new network endpoint: {}", endpoint);
impl::getNetworkEndpoints().insert({ endpoint, callback });
impl::s_endpoints->insert({ endpoint, callback });
}
}
@@ -1139,16 +1115,15 @@ namespace hex {
namespace impl {
std::map<std::string, Experiment> &getExperiments() {
static AutoReset<std::map<std::string, Experiment>> experiments;
return experiments;
static AutoReset<std::map<std::string, Experiment>> s_experiments;
const std::map<std::string, Experiment>& getExperiments() {
return *s_experiments;
}
}
void addExperiment(const std::string &experimentName, const UnlocalizedString &unlocalizedName, const UnlocalizedString &unlocalizedDescription) {
auto &experiments = impl::getExperiments();
auto &experiments = *impl::s_experiments;
if (experiments.contains(experimentName)) {
log::error("Experiment with name '{}' already exists!", experimentName);
@@ -1163,7 +1138,7 @@ namespace hex {
}
void enableExperiement(const std::string &experimentName, bool enabled) {
auto &experiments = impl::getExperiments();
auto &experiments = *impl::s_experiments;
if (!experiments.contains(experimentName)) {
log::error("Experiment with name '{}' does not exist!", experimentName);
@@ -1174,7 +1149,7 @@ namespace hex {
}
[[nodiscard]] bool isExperimentEnabled(const std::string &experimentName) {
auto &experiments = impl::getExperiments();
auto &experiments = *impl::s_experiments;
if (!experiments.contains(experimentName)) {
log::error("Experiment with name '{}' does not exist!", experimentName);
@@ -1190,16 +1165,15 @@ namespace hex {
namespace impl {
std::vector<ReportGenerator> &getGenerators() {
static AutoReset<std::vector<ReportGenerator>> generators;
return generators;
static AutoReset<std::vector<ReportGenerator>> s_generators;
const std::vector<ReportGenerator>& getGenerators() {
return *s_generators;
}
}
void addReportProvider(impl::Callback callback) {
impl::getGenerators().push_back(impl::ReportGenerator { std::move(callback ) });
impl::s_generators->push_back(impl::ReportGenerator { std::move(callback ) });
}
}

View File

@@ -37,34 +37,34 @@ namespace hex {
namespace impl {
std::map<u32, Highlighting> &getBackgroundHighlights() {
static AutoReset<std::map<u32, Highlighting>> backgroundHighlights;
return backgroundHighlights;
static AutoReset<std::map<u32, Highlighting>> s_backgroundHighlights;
const std::map<u32, Highlighting>& getBackgroundHighlights() {
return *s_backgroundHighlights;
}
std::map<u32, HighlightingFunction> &getBackgroundHighlightingFunctions() {
static AutoReset<std::map<u32, HighlightingFunction>> backgroundHighlightingFunctions;
return backgroundHighlightingFunctions;
static AutoReset<std::map<u32, HighlightingFunction>> s_backgroundHighlightingFunctions;
const std::map<u32, HighlightingFunction>& getBackgroundHighlightingFunctions() {
return *s_backgroundHighlightingFunctions;
}
std::map<u32, Highlighting> &getForegroundHighlights() {
static AutoReset<std::map<u32, Highlighting>> foregroundHighlights;
return foregroundHighlights;
static AutoReset<std::map<u32, Highlighting>> s_foregroundHighlights;
const std::map<u32, Highlighting>& getForegroundHighlights() {
return *s_foregroundHighlights;
}
std::map<u32, HighlightingFunction> &getForegroundHighlightingFunctions() {
static AutoReset<std::map<u32, HighlightingFunction>> foregroundHighlightingFunctions;
return foregroundHighlightingFunctions;
static AutoReset<std::map<u32, HighlightingFunction>> s_foregroundHighlightingFunctions;
const std::map<u32, HighlightingFunction>& getForegroundHighlightingFunctions() {
return *s_foregroundHighlightingFunctions;
}
std::map<u32, Tooltip> &getTooltips() {
static AutoReset<std::map<u32, Tooltip>> tooltips;
return tooltips;
static AutoReset<std::map<u32, Tooltip>> s_tooltips;
const std::map<u32, Tooltip>& getTooltips() {
return *s_tooltips;
}
std::map<u32, TooltipFunction> &getTooltipFunctions() {
static AutoReset<std::map<u32, TooltipFunction>> tooltipFunctions;
return tooltipFunctions;
static AutoReset<std::map<u32, TooltipFunction>> s_tooltipFunctions;
const std::map<u32, TooltipFunction>& getTooltipFunctions() {
return *s_tooltipFunctions;
}
static AutoReset<std::optional<ProviderRegion>> s_currentSelection;
@@ -79,8 +79,8 @@ namespace hex {
id++;
impl::getBackgroundHighlights().insert({
id, Highlighting {region, color}
impl::s_backgroundHighlights->insert({
id, Highlighting { region, color }
});
EventHighlightingChanged::post();
@@ -89,7 +89,7 @@ namespace hex {
}
void removeBackgroundHighlight(u32 id) {
impl::getBackgroundHighlights().erase(id);
impl::s_backgroundHighlights->erase(id);
EventHighlightingChanged::post();
}
@@ -99,7 +99,7 @@ namespace hex {
id++;
impl::getBackgroundHighlightingFunctions().insert({ id, function });
impl::s_backgroundHighlightingFunctions->insert({ id, function });
EventHighlightingChanged::post();
@@ -107,7 +107,7 @@ namespace hex {
}
void removeBackgroundHighlightingProvider(u32 id) {
impl::getBackgroundHighlightingFunctions().erase(id);
impl::s_backgroundHighlightingFunctions->erase(id);
EventHighlightingChanged::post();
}
@@ -117,8 +117,8 @@ namespace hex {
id++;
impl::getForegroundHighlights().insert({
id, Highlighting {region, color}
impl::s_foregroundHighlights->insert({
id, Highlighting { region, color }
});
EventHighlightingChanged::post();
@@ -127,7 +127,7 @@ namespace hex {
}
void removeForegroundHighlight(u32 id) {
impl::getForegroundHighlights().erase(id);
impl::s_foregroundHighlights->erase(id);
EventHighlightingChanged::post();
}
@@ -137,7 +137,7 @@ namespace hex {
id++;
impl::getForegroundHighlightingFunctions().insert({ id, function });
impl::s_foregroundHighlightingFunctions->insert({ id, function });
EventHighlightingChanged::post();
@@ -145,7 +145,7 @@ namespace hex {
}
void removeForegroundHighlightingProvider(u32 id) {
impl::getForegroundHighlightingFunctions().erase(id);
impl::s_foregroundHighlightingFunctions->erase(id);
EventHighlightingChanged::post();
}
@@ -153,25 +153,25 @@ namespace hex {
static u32 tooltipId = 0;
u32 addTooltip(Region region, std::string value, color_t color) {
tooltipId++;
impl::getTooltips().insert({ tooltipId, { region, std::move(value), color } });
impl::s_tooltips->insert({ tooltipId, { region, std::move(value), color } });
return tooltipId;
}
void removeTooltip(u32 id) {
impl::getTooltips().erase(id);
impl::s_tooltips->erase(id);
}
static u32 tooltipFunctionId;
u32 addTooltipProvider(TooltipFunction function) {
tooltipFunctionId++;
impl::getTooltipFunctions().insert({ tooltipFunctionId, std::move(function) });
impl::s_tooltipFunctions->insert({ tooltipFunctionId, std::move(function) });
return tooltipFunctionId;
}
void removeTooltipProvider(u32 id) {
impl::getTooltipFunctions().erase(id);
impl::s_tooltipFunctions->erase(id);
}
bool isSelectionValid() {
@@ -228,7 +228,7 @@ namespace hex {
namespace ImHexApi::Provider {
static i64 s_currentProvider = -1;
static AutoReset<std::vector<prv::Provider *>> s_providers;
static AutoReset<std::vector<std::unique_ptr<prv::Provider>>> s_providers;
namespace impl {
@@ -247,11 +247,16 @@ namespace hex {
if (!ImHexApi::Provider::isValid())
return nullptr;
return (*s_providers)[s_currentProvider];
return (*s_providers)[s_currentProvider].get();
}
const std::vector<prv::Provider *> &getProviders() {
return s_providers;
std::vector<prv::Provider*> getProviders() {
std::vector<prv::Provider*> result;
result.reserve(s_providers->size());
for (const auto &provider : *s_providers)
result.push_back(provider.get());
return result;
}
void setCurrentProvider(u32 index) {
@@ -288,15 +293,15 @@ namespace hex {
});
}
void add(prv::Provider *provider, bool skipLoadInterface, bool select) {
void add(std::unique_ptr<prv::Provider> &&provider, bool skipLoadInterface, bool select) {
if (TaskManager::getRunningTaskCount() > 0)
return;
if (skipLoadInterface)
provider->skipLoadInterface();
s_providers->push_back(provider);
EventProviderCreated::post(provider);
EventProviderCreated::post(provider.get());
s_providers->emplace_back(std::move(provider));
if (select || s_providers->size() == 1)
setCurrentProvider(s_providers->size() - 1);
@@ -318,7 +323,10 @@ namespace hex {
return;
}
const auto it = std::ranges::find(*s_providers, provider);
const auto it = std::ranges::find_if(*s_providers, [provider](const auto &p) {
return p.get() == provider;
});
if (it == s_providers->end())
return;
@@ -328,7 +336,7 @@ namespace hex {
setCurrentProvider(0);
if (s_providers->size() > 1)
EventProviderChanged::post(s_providers->at(0), s_providers->at(1));
EventProviderChanged::post(s_providers->at(0).get(), s_providers->at(1).get());
}
else if (std::distance(s_providers->begin(), it) == s_currentProvider) {
// If the current provider is being closed, select the one that's before it
@@ -337,7 +345,9 @@ namespace hex {
else {
// If any other provider is being closed, find the current provider in the list again and select it again
const auto currentProvider = get();
const auto currentIt = std::ranges::find(*s_providers, currentProvider);
const auto currentIt = std::ranges::find_if(*s_providers, [currentProvider](const auto &p) {
return p.get() == currentProvider;
});
if (currentIt != s_providers->end()) {
auto newIndex = std::distance(s_providers->begin(), currentIt);
@@ -353,21 +363,20 @@ namespace hex {
}
}
s_providers->erase(it);
if (s_currentProvider >= i64(s_providers->size()))
setCurrentProvider(0);
if (s_providers->empty())
EventProviderChanged::post(provider, nullptr);
provider->close();
EventProviderClosed::post(provider);
RequestUpdateWindowTitle::post();
TaskManager::runWhenTasksFinished([provider] {
TaskManager::runWhenTasksFinished([it, provider] {
EventProviderDeleted::post(provider);
std::erase(impl::s_closingProviders, provider);
delete provider;
s_providers->erase(it);
if (s_currentProvider >= i64(s_providers->size()))
setCurrentProvider(0);
if (s_providers->empty())
EventProviderChanged::post(provider, nullptr);
});
}
@@ -449,11 +458,12 @@ namespace hex {
s_portableVersion = enabled;
}
static AutoReset<std::map<std::string, std::string>> s_initArguments;
void addInitArgument(const std::string &key, const std::string &value) {
static std::mutex initArgumentsMutex;
std::scoped_lock lock(initArgumentsMutex);
getInitArguments()[key] = value;
(*s_initArguments)[key] = value;
}
static double s_lastFrameTime;
@@ -538,12 +548,18 @@ namespace hex {
return impl::s_initialWindowProperties;
}
std::map<std::string, std::string> &getInitArguments() {
static AutoReset<std::map<std::string, std::string>> initArgs;
return initArgs;
const std::map<std::string, std::string>& getInitArguments() {
return *impl::s_initArguments;
}
std::string getInitArgument(const std::string &key) {
if (impl::s_initArguments->contains(key))
return impl::s_initArguments->at(key);
else
return "";
}
static bool s_systemThemeDetection;
void enableSystemThemeDetection(bool enabled) {
@@ -557,13 +573,13 @@ namespace hex {
}
std::vector<std::fs::path> &getAdditionalFolderPaths() {
static AutoReset<std::vector<std::fs::path>> additionalFolderPaths;
return additionalFolderPaths;
static AutoReset<std::vector<std::fs::path>> s_additionalFolderPaths;
const std::vector<std::fs::path>& getAdditionalFolderPaths() {
return *s_additionalFolderPaths;
}
void setAdditionalFolderPaths(const std::vector<std::fs::path> &paths) {
getAdditionalFolderPaths() = paths;
s_additionalFolderPaths = paths;
}
@@ -742,14 +758,13 @@ namespace hex {
namespace impl {
std::map<std::string, MessagingHandler> &getHandlers() {
static AutoReset<std::map<std::string, MessagingHandler>> handlers;
return handlers;
static AutoReset<std::map<std::string, MessagingHandler>> s_handlers;
const std::map<std::string, MessagingHandler>& getHandlers() {
return *s_handlers;
}
void runHandler(const std::string &eventName, const std::vector<u8> &args) {
const auto& handlers = impl::getHandlers();
const auto& handlers = getHandlers();
const auto matchHandler = handlers.find(eventName);
if (matchHandler == handlers.end()) {
@@ -765,7 +780,7 @@ namespace hex {
void registerHandler(const std::string &eventName, const impl::MessagingHandler &handler) {
log::debug("Registered new forward event handler: {}", eventName);
impl::getHandlers().insert({ eventName, handler });
impl::s_handlers->insert({ eventName, handler });
}
}
@@ -774,10 +789,9 @@ namespace hex {
namespace impl {
std::vector<Font>& getFonts() {
static AutoReset<std::vector<Font>> fonts;
return fonts;
static AutoReset<std::vector<Font>> s_fonts;
const std::vector<Font>& getFonts() {
return *s_fonts;
}
static AutoReset<std::fs::path> s_customFontPath;
@@ -845,7 +859,7 @@ namespace hex {
return;
}
impl::getFonts().emplace_back(Font {
impl::s_fonts->emplace_back(Font {
wolv::util::toUTF8String(path.filename()),
fontFile.readVector(),
glyphRanges,
@@ -855,7 +869,7 @@ namespace hex {
}
void loadFont(const std::string &name, const std::span<const u8> &data, const std::vector<GlyphRange> &glyphRanges, Offset offset, u32 flags) {
impl::getFonts().emplace_back(Font {
impl::s_fonts->emplace_back(Font {
name,
{ data.begin(), data.end() },
glyphRanges,
@@ -864,7 +878,7 @@ namespace hex {
});
}
std::fs::path &getCustomFontPath() {
const std::fs::path& getCustomFontPath() {
return impl::s_customFontPath;
}

View File

@@ -17,6 +17,9 @@ namespace hex {
AutoReset<std::optional<std::string>> s_layoutStringToLoad;
AutoReset<std::vector<LayoutManager::Layout>> s_layouts;
AutoReset<std::vector<LayoutManager::LoadCallback>> s_loadCallbacks;
AutoReset<std::vector<LayoutManager::StoreCallback>> s_storeCallbacks;
bool s_layoutLocked = false;
}
@@ -130,4 +133,24 @@ namespace hex {
s_layoutLocked = locked;
}
void LayoutManager::registerLoadCallback(const LoadCallback &callback) {
s_loadCallbacks->push_back(callback);
}
void LayoutManager::registerStoreCallback(const StoreCallback &callback) {
s_storeCallbacks->push_back(callback);
}
void LayoutManager::onLoad(std::string_view line) {
for (const auto &callback : *s_loadCallbacks)
callback(line);
}
void LayoutManager::onStore(ImGuiTextBuffer *buffer) {
for (const auto &callback : *s_storeCallbacks)
callback(buffer);
}
}

View File

@@ -49,12 +49,12 @@ namespace hex {
s_currStrings->clear();
for (auto &definition : definitions[language])
for (const auto &definition : definitions.at(language))
s_currStrings->insert(definition.getEntries().begin(), definition.getEntries().end());
const auto& fallbackLanguage = getFallbackLanguage();
if (language != fallbackLanguage) {
for (auto &definition : definitions[fallbackLanguage])
if (language != fallbackLanguage && definitions.contains(fallbackLanguage)) {
for (const auto &definition : definitions.at(fallbackLanguage))
s_currStrings->insert(definition.getEntries().begin(), definition.getEntries().end());
}
@@ -70,7 +70,7 @@ namespace hex {
return "";
std::string localizedString;
for (const auto &definition : languageDefinitions[language]) {
for (const auto &definition : languageDefinitions.at(language)) {
if (definition.getEntries().contains(unlocalizedString)) {
localizedString = definition.getEntries().at(unlocalizedString);
break;

View File

@@ -228,9 +228,6 @@ namespace hex {
return m_addedManually;
}
void *Plugin::getPluginFunction(const std::string &symbol) const {
#if defined(OS_WINDOWS)
return reinterpret_cast<void *>(GetProcAddress(HMODULE(m_handle), symbol.c_str()));
@@ -239,8 +236,11 @@ namespace hex {
#endif
}
AutoReset<std::vector<std::fs::path>> PluginManager::s_pluginPaths, PluginManager::s_pluginLoadPaths;
void PluginManager::addLoadPath(const std::fs::path& path) {
getPluginLoadPaths().emplace_back(path);
s_pluginLoadPaths->emplace_back(path);
}
@@ -257,13 +257,13 @@ namespace hex {
if (!wolv::io::fs::exists(pluginFolder))
return false;
getPluginPaths().push_back(pluginFolder);
s_pluginPaths->push_back(pluginFolder);
// Load library plugins first
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexpluglib") {
if (!isPluginLoaded(pluginPath.path())) {
getPlugins().emplace_back(pluginPath.path());
getPluginsMutable().emplace_back(pluginPath.path());
}
}
}
@@ -272,12 +272,12 @@ namespace hex {
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") {
if (!isPluginLoaded(pluginPath.path())) {
getPlugins().emplace_back(pluginPath.path());
getPluginsMutable().emplace_back(pluginPath.path());
}
}
}
std::erase_if(getPlugins(), [](const Plugin &plugin) {
std::erase_if(getPluginsMutable(), [](const Plugin &plugin) {
return !plugin.isValid();
});
@@ -292,10 +292,10 @@ namespace hex {
}
void PluginManager::unload() {
getPluginPaths().clear();
s_pluginPaths->clear();
// Unload plugins in reverse order
auto &plugins = getPlugins();
auto &plugins = getPluginsMutable();
std::list<Plugin> savedPlugins;
while (!plugins.empty()) {
@@ -304,29 +304,29 @@ namespace hex {
plugins.pop_back();
}
getPlugins() = std::move(savedPlugins);
getPluginsMutable() = std::move(savedPlugins);
}
void PluginManager::addPlugin(const std::string &name, hex::PluginFunctions functions) {
getPlugins().emplace_back(name, functions);
getPluginsMutable().emplace_back(name, functions);
}
std::list<Plugin> &PluginManager::getPlugins() {
static std::list<Plugin> plugins;
const std::list<Plugin>& PluginManager::getPlugins() {
return getPluginsMutable();
}
std::list<Plugin>& PluginManager::getPluginsMutable() {
static std::list<Plugin> plugins;
return plugins;
}
std::vector<std::fs::path> &PluginManager::getPluginPaths() {
static AutoReset<std::vector<std::fs::path>> pluginPaths;
return pluginPaths;
const std::vector<std::fs::path>& PluginManager::getPluginPaths() {
return s_pluginPaths;
}
std::vector<std::fs::path> &PluginManager::getPluginLoadPaths() {
static AutoReset<std::vector<std::fs::path>> pluginPaths;
return pluginPaths;
const std::vector<std::fs::path>& PluginManager::getPluginLoadPaths() {
return s_pluginLoadPaths;
}
bool PluginManager::isPluginLoaded(const std::fs::path &path) {

View File

@@ -51,11 +51,19 @@ namespace hex {
s_currProjectPath = path;
}
std::vector<ProjectFile::Handler> &ProjectFile::getHandlers() {
void ProjectFile::registerHandler(const Handler &handler) {
s_handlers->push_back(handler);
}
void ProjectFile::registerPerProviderHandler(const ProviderHandler &handler) {
s_providerHandlers->push_back(handler);
}
const std::vector<ProjectFile::Handler>& ProjectFile::getHandlers() {
return s_handlers;
}
std::vector<ProjectFile::ProviderHandler> &ProjectFile::getProviderHandlers() {
const std::vector<ProjectFile::ProviderHandler>& ProjectFile::getProviderHandlers() {
return s_providerHandlers;
}

View File

@@ -335,7 +335,7 @@ namespace hex {
}
std::list<std::shared_ptr<Task>> &TaskManager::getRunningTasks() {
const std::list<std::shared_ptr<Task>>& TaskManager::getRunningTasks() {
return s_tasks;
}

View File

@@ -219,11 +219,11 @@ namespace hex {
}
std::map<std::string, ThemeManager::ThemeHandler> &ThemeManager::getThemeHandlers() {
const std::map<std::string, ThemeManager::ThemeHandler> &ThemeManager::getThemeHandlers() {
return s_themeHandlers;
}
std::map<std::string, ThemeManager::StyleHandler> &ThemeManager::getStyleHandlers() {
const std::map<std::string, ThemeManager::StyleHandler> &ThemeManager::getStyleHandlers() {
return s_styleHandlers;
}

View File

@@ -107,17 +107,17 @@ namespace hex::subcommands {
void registerSubCommand(const std::string &cmdName, const ForwardCommandHandler &handler) {
log::debug("Registered new forward command handler: {}", cmdName);
ImHexApi::Messaging::impl::getHandlers().insert({ hex::format("command/{}", cmdName), [handler](const std::vector<u8> &eventData){
std::string str(reinterpret_cast<const char *>(eventData.data()), eventData.size());
ImHexApi::Messaging::registerHandler(hex::format("command/{}", cmdName), [handler](const std::vector<u8> &eventData){
std::string string(reinterpret_cast<const char *>(eventData.data()), eventData.size());
std::vector<std::string> args;
for (const auto &arg_view : std::views::split(str, '\0')) {
for (const auto &arg_view : std::views::split(string, char(0x00))) {
std::string arg(arg_view.data(), arg_view.size());
args.push_back(arg);
}
handler(args);
}});
});
}
}