From 27c8e19c14a98d0142b64720cde757aa54c82d88 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 26 Jul 2023 13:50:51 +0200 Subject: [PATCH] build: Remove all static variables from headers to hopefully fix plugins --- lib/libimhex/include/hex/api/event.hpp | 26 +++++++------- lib/libimhex/include/hex/api/keybinding.hpp | 3 -- .../include/hex/api/layout_manager.hpp | 4 --- lib/libimhex/include/hex/api/localization.hpp | 4 --- .../include/hex/api/plugin_manager.hpp | 8 +---- .../include/hex/api/project_file_manager.hpp | 15 ++------ lib/libimhex/include/hex/api/task.hpp | 11 ------ .../include/hex/api/theme_manager.hpp | 11 ++---- .../include/hex/data_processor/attribute.hpp | 7 +--- .../include/hex/data_processor/link.hpp | 7 +--- .../include/hex/data_processor/node.hpp | 7 +--- .../include/hex/helpers/http_requests.hpp | 17 +++------ .../include/hex/providers/provider.hpp | 3 -- lib/libimhex/include/hex/ui/popup.hpp | 9 ++--- lib/libimhex/include/hex/ui/view.hpp | 11 +++--- lib/libimhex/source/api/event.cpp | 14 ++++++-- lib/libimhex/source/api/keybinding.cpp | 19 ++++++---- lib/libimhex/source/api/layout_manager.cpp | 11 ++++-- lib/libimhex/source/api/localization.cpp | 31 +++++++++------- lib/libimhex/source/api/plugin_manager.cpp | 24 ++++++++----- .../source/api/project_file_manager.cpp | 35 +++++++++++++------ lib/libimhex/source/api/task.cpp | 27 ++++++++------ lib/libimhex/source/api/theme_manager.cpp | 34 ++++++++++++------ .../source/data_processor/attribute.cpp | 14 ++++++-- lib/libimhex/source/data_processor/link.cpp | 11 ++++-- lib/libimhex/source/data_processor/node.cpp | 13 +++++-- lib/libimhex/source/helpers/http_requests.cpp | 33 +++++++++-------- lib/libimhex/source/providers/provider.cpp | 7 +++- lib/libimhex/source/ui/popup.cpp | 8 ++++- lib/libimhex/source/ui/view.cpp | 14 ++++++-- main/source/init/tasks.cpp | 23 ------------ 31 files changed, 236 insertions(+), 225 deletions(-) diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index 0ce8d6d5d..decee0cc6 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -82,7 +82,8 @@ namespace hex { */ template static EventList::iterator subscribe(typename E::Callback function) { - return s_events.insert(s_events.end(), std::make_pair(E::Id, std::make_unique(function))); + auto &events = getEvents(); + return events.insert(events.end(), std::make_pair(E::Id, std::make_unique(function))); } /** @@ -93,7 +94,7 @@ namespace hex { */ template static void subscribe(void *token, typename E::Callback function) { - s_tokenStore.insert(std::make_pair(token, subscribe(function))); + getTokenStore().insert(std::make_pair(token, subscribe(function))); } /** @@ -101,7 +102,7 @@ namespace hex { * @param token Token returned by subscribe */ static void unsubscribe(const EventList::iterator &token) noexcept { - s_events.erase(token); + getEvents().erase(token); } /** @@ -111,13 +112,14 @@ namespace hex { */ template static void unsubscribe(void *token) noexcept { - auto iter = std::find_if(s_tokenStore.begin(), s_tokenStore.end(), [&](auto &item) { + auto &tokenStore = getTokenStore(); + auto iter = std::find_if(tokenStore.begin(), tokenStore.end(), [&](auto &item) { return item.first == token && item.second->first == E::Id; }); - if (iter != s_tokenStore.end()) { - s_events.remove(*iter->second); - s_tokenStore.erase(iter); + if (iter != tokenStore.end()) { + getEvents().remove(*iter->second); + tokenStore.erase(iter); } } @@ -129,7 +131,7 @@ namespace hex { */ template static void post(auto &&...args) noexcept { - for (const auto &[id, event] : s_events) { + for (const auto &[id, event] : getEvents()) { if (id == E::Id) { (*static_cast(event.get()))(std::forward(args)...); } @@ -145,13 +147,13 @@ namespace hex { * @brief Unsubscribe all subscribers from all events */ static void clear() noexcept { - s_events.clear(); - s_tokenStore.clear(); + getEvents().clear(); + getTokenStore().clear(); } private: - static std::map s_tokenStore; - static EventList s_events; + static std::map& getTokenStore(); + static EventList& getEvents(); }; /* Default Events */ diff --git a/lib/libimhex/include/hex/api/keybinding.hpp b/lib/libimhex/include/hex/api/keybinding.hpp index 7445c2656..1fc10d31d 100644 --- a/lib/libimhex/include/hex/api/keybinding.hpp +++ b/lib/libimhex/include/hex/api/keybinding.hpp @@ -413,9 +413,6 @@ namespace hex { * @brief Clear all shortcuts */ static void clearShortcuts(); - - private: - static std::map> s_globalShortcuts; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/api/layout_manager.hpp b/lib/libimhex/include/hex/api/layout_manager.hpp index 7a7825638..3f5a07228 100644 --- a/lib/libimhex/include/hex/api/layout_manager.hpp +++ b/lib/libimhex/include/hex/api/layout_manager.hpp @@ -30,10 +30,6 @@ namespace hex { private: LayoutManager() = default; - - static std::optional s_layoutPathToLoad; - static std::optional s_layoutStringToLoad; - static std::vector s_layouts; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/api/localization.hpp b/lib/libimhex/include/hex/api/localization.hpp index 036a8770f..b103ea286 100644 --- a/lib/libimhex/include/hex/api/localization.hpp +++ b/lib/libimhex/include/hex/api/localization.hpp @@ -43,10 +43,6 @@ namespace hex { private: std::string m_unlocalizedString; - - static std::string s_fallbackLanguage; - static std::string s_selectedLanguage; - static std::map s_currStrings; }; std::string operator+(const std::string &&left, const LangEntry &&right); diff --git a/lib/libimhex/include/hex/api/plugin_manager.hpp b/lib/libimhex/include/hex/api/plugin_manager.hpp index 9fceacb8a..e15f1adf5 100644 --- a/lib/libimhex/include/hex/api/plugin_manager.hpp +++ b/lib/libimhex/include/hex/api/plugin_manager.hpp @@ -90,13 +90,7 @@ namespace hex { static void unload(); static void reload(); - static const auto &getPlugins() { - return PluginManager::s_plugins; - } - - private: - static std::fs::path s_pluginFolder; - static std::vector s_plugins; + static const std::vector &getPlugins(); }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/api/project_file_manager.hpp b/lib/libimhex/include/hex/api/project_file_manager.hpp index f34edf35a..1e3015e79 100644 --- a/lib/libimhex/include/hex/api/project_file_manager.hpp +++ b/lib/libimhex/include/hex/api/project_file_manager.hpp @@ -116,27 +116,16 @@ namespace hex { * @brief Get the list of registered handlers * @return List of registered handlers */ - static std::vector& getHandlers() { - return s_handlers; - } + static std::vector& getHandlers(); /** * @brief Get the list of registered per-provider handlers * @return List of registered per-provider handlers */ - static std::vector& getProviderHandlers() { - return s_providerHandlers; - } + static std::vector& getProviderHandlers(); private: ProjectFile() = default; - - static std::function s_loadProjectFunction; - static std::function, bool)> s_storeProjectFunction; - - static std::fs::path s_currProjectPath; - static std::vector s_handlers; - static std::vector s_providerHandlers; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/api/task.hpp b/lib/libimhex/include/hex/api/task.hpp index 1d31d3015..eb8d6c42d 100644 --- a/lib/libimhex/include/hex/api/task.hpp +++ b/lib/libimhex/include/hex/api/task.hpp @@ -167,17 +167,6 @@ namespace hex { static void runDeferredCalls(); private: - static std::mutex s_deferredCallsMutex, s_tasksFinishedMutex; - - static std::list> s_tasks; - static std::list> s_taskQueue; - static std::list> s_deferredCalls; - static std::list> s_tasksFinishedCallbacks; - - static std::mutex s_queueMutex; - static std::condition_variable s_jobCondVar; - static std::vector s_workers; - static void runner(const std::stop_token &stopToken); }; diff --git a/lib/libimhex/include/hex/api/theme_manager.hpp b/lib/libimhex/include/hex/api/theme_manager.hpp index e0c93f8bc..64413ed24 100644 --- a/lib/libimhex/include/hex/api/theme_manager.hpp +++ b/lib/libimhex/include/hex/api/theme_manager.hpp @@ -78,18 +78,11 @@ namespace hex { StyleMap styleMap; }; - static std::map& getThemeHandlers() { return s_themeHandlers; } - static std::map& getStyleHandlers() { return s_styleHandlers; } + static std::map& getThemeHandlers(); + static std::map& getStyleHandlers(); private: ThemeManager() = default; - - - static std::map s_themes; - static std::map s_themeHandlers; - static std::map s_styleHandlers; - static std::string s_imageTheme; - static std::string s_currTheme; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/attribute.hpp b/lib/libimhex/include/hex/data_processor/attribute.hpp index f0848858d..cac7e20a5 100644 --- a/lib/libimhex/include/hex/data_processor/attribute.hpp +++ b/lib/libimhex/include/hex/data_processor/attribute.hpp @@ -51,10 +51,7 @@ namespace hex::dp { [[nodiscard]] std::vector& getDefaultData() { return this->m_defaultData; } - static void setIdCounter(int id) { - if (id > Attribute::s_idCounter) - Attribute::s_idCounter = id; - } + static void setIdCounter(int id); private: int m_id; @@ -69,8 +66,6 @@ namespace hex::dp { friend class Node; void setParentNode(Node *node) { this->m_parentNode = node; } - - static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/link.hpp b/lib/libimhex/include/hex/data_processor/link.hpp index 3495a6d1f..dc9aff26c 100644 --- a/lib/libimhex/include/hex/data_processor/link.hpp +++ b/lib/libimhex/include/hex/data_processor/link.hpp @@ -14,16 +14,11 @@ namespace hex::dp { [[nodiscard]] int getFromId() const { return this->m_from; } [[nodiscard]] int getToId() const { return this->m_to; } - static void setIdCounter(int id) { - if (id > Link::s_idCounter) - Link::s_idCounter = id; - } + static void setIdCounter(int id); private: int m_id; int m_from, m_to; - - static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/node.hpp b/lib/libimhex/include/hex/data_processor/node.hpp index 747b4dfaf..d3023f613 100644 --- a/lib/libimhex/include/hex/data_processor/node.hpp +++ b/lib/libimhex/include/hex/data_processor/node.hpp @@ -69,10 +69,7 @@ namespace hex::dp { return this->m_position; } - static void setIdCounter(int id) { - if (id > Node::s_idCounter) - Node::s_idCounter = id; - } + static void setIdCounter(int id); const std::vector& getBufferOnInput(u32 index); const i128& getIntegerOnInput(u32 index); @@ -90,8 +87,6 @@ namespace hex::dp { prv::Overlay *m_overlay = nullptr; ImVec2 m_position; - static int s_idCounter; - Attribute& getAttribute(u32 index) { if (index >= this->getAttributes().size()) throw std::runtime_error("Attribute index out of bounds!"); diff --git a/lib/libimhex/include/hex/helpers/http_requests.hpp b/lib/libimhex/include/hex/helpers/http_requests.hpp index 36e0c9514..721aa076e 100644 --- a/lib/libimhex/include/hex/helpers/http_requests.hpp +++ b/lib/libimhex/include/hex/helpers/http_requests.hpp @@ -93,13 +93,7 @@ namespace hex { return *this; } - static void setCACert(std::string data) { - HttpRequest::s_caCertData = std::move(data); - } - - static void setProxy(std::string proxy) { - HttpRequest::s_proxyUrl = std::move(proxy); - } + static void setProxy(std::string proxy); void setMethod(std::string method) { this->m_method = std::move(method); @@ -286,9 +280,7 @@ namespace hex { char *url = nullptr; curl_easy_getinfo(this->m_curl, CURLINFO_EFFECTIVE_URL, &url); log::error("Http request '{0} {1}' failed with error {2}: '{3}'", this->m_method, url, u32(result), curl_easy_strerror(result)); - if (!HttpRequest::s_proxyUrl.empty()){ - log::info("A custom proxy '{0}' is in use. Is it working correctly?", HttpRequest::s_proxyUrl); - } + checkProxyErrors(); return { }; } @@ -300,11 +292,13 @@ namespace hex { return Result(statusCode, { data.begin(), data.end() }); } - [[maybe_unused]] static CURLcode sslCtxFunction(CURL *ctx, void *sslctx, void *userData); static size_t writeToVector(void *contents, size_t size, size_t nmemb, void *userdata); static size_t writeToFile(void *contents, size_t size, size_t nmemb, void *userdata); static int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow); + private: + static void checkProxyErrors(); + private: CURL *m_curl; @@ -320,7 +314,6 @@ namespace hex { std::atomic m_canceled = false; [[maybe_unused]] std::unique_ptr m_caCert; - static std::string s_caCertData, s_proxyUrl; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index fbc462a1b..70eb3449f 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -206,9 +206,6 @@ namespace hex::prv { std::string m_errorMessage; size_t m_pageSize = MaxPageSize; - - private: - static u32 s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/ui/popup.hpp b/lib/libimhex/include/hex/ui/popup.hpp index 0fed2f1a7..679303f36 100644 --- a/lib/libimhex/include/hex/ui/popup.hpp +++ b/lib/libimhex/include/hex/ui/popup.hpp @@ -34,9 +34,7 @@ namespace hex { return { 0, 0 }; } - [[nodiscard]] static std::vector> &getOpenPopups() { - return s_openPopups; - } + [[nodiscard]] static std::vector> &getOpenPopups(); [[nodiscard]] const std::string &getUnlocalizedName() const { return this->m_unlocalizedName; @@ -58,9 +56,6 @@ namespace hex { return this->m_close; } - protected: - static std::vector> s_openPopups; - private: std::string m_unlocalizedName; @@ -84,7 +79,7 @@ namespace hex { auto popup = std::make_unique(std::forward(args)...); - s_openPopups.emplace_back(std::move(popup)); + getOpenPopups().emplace_back(std::move(popup)); } }; diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index 434762f03..9d22e04f3 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -51,20 +51,17 @@ namespace hex { return LangEntry(unlocalizedName) + "###" + unlocalizedName; } - static ImFontAtlas *getFontAtlas() { return View::s_fontAtlas; } - static void setFontAtlas(ImFontAtlas *atlas) { View::s_fontAtlas = atlas; } + static ImFontAtlas *getFontAtlas(); + static void setFontAtlas(ImFontAtlas *atlas); - static ImFontConfig getFontConfig() { return View::s_fontConfig; } - static void setFontConfig(ImFontConfig config) { View::s_fontConfig = config; } + static ImFontConfig getFontConfig(); + static void setFontConfig(ImFontConfig config); private: std::string m_unlocalizedViewName; bool m_windowOpen = false; std::map> m_shortcuts; - static ImFontAtlas *s_fontAtlas; - static ImFontConfig s_fontConfig; - friend class ShortcutManager; }; diff --git a/lib/libimhex/source/api/event.cpp b/lib/libimhex/source/api/event.cpp index 2526dee7f..7ef24bab3 100644 --- a/lib/libimhex/source/api/event.cpp +++ b/lib/libimhex/source/api/event.cpp @@ -2,7 +2,17 @@ namespace hex { - EventManager::EventList EventManager::s_events; - std::map EventManager::s_tokenStore; + std::map& EventManager::getTokenStore() { + static std::map tokenStore; + + return tokenStore; + } + + EventManager::EventList& EventManager::getEvents() { + static EventManager::EventList events; + + return events; + } + } \ No newline at end of file diff --git a/lib/libimhex/source/api/keybinding.cpp b/lib/libimhex/source/api/keybinding.cpp index 7cc8c0f7e..a51333fa2 100644 --- a/lib/libimhex/source/api/keybinding.cpp +++ b/lib/libimhex/source/api/keybinding.cpp @@ -5,10 +5,15 @@ namespace hex { - std::map> ShortcutManager::s_globalShortcuts; + namespace { + + std::map> s_globalShortcuts; + + } + void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::function &callback) { - ShortcutManager::s_globalShortcuts.insert({ shortcut, callback }); + s_globalShortcuts.insert({ shortcut, callback }); } void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::function &callback) { @@ -48,16 +53,16 @@ namespace hex { void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) { Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode); - if (ShortcutManager::s_globalShortcuts.contains(pressedShortcut + AllowWhileTyping)) { - ShortcutManager::s_globalShortcuts[pressedShortcut + AllowWhileTyping](); - } else if (ShortcutManager::s_globalShortcuts.contains(pressedShortcut)) { + if (s_globalShortcuts.contains(pressedShortcut + AllowWhileTyping)) { + s_globalShortcuts[pressedShortcut + AllowWhileTyping](); + } else if (s_globalShortcuts.contains(pressedShortcut)) { if (!ImGui::GetIO().WantTextInput) - ShortcutManager::s_globalShortcuts[pressedShortcut](); + s_globalShortcuts[pressedShortcut](); } } void ShortcutManager::clearShortcuts() { - ShortcutManager::s_globalShortcuts.clear(); + s_globalShortcuts.clear(); } } \ No newline at end of file diff --git a/lib/libimhex/source/api/layout_manager.cpp b/lib/libimhex/source/api/layout_manager.cpp index 410a357b9..0dacdcfb7 100644 --- a/lib/libimhex/source/api/layout_manager.cpp +++ b/lib/libimhex/source/api/layout_manager.cpp @@ -9,9 +9,14 @@ namespace hex { - std::optional LayoutManager::s_layoutPathToLoad; - std::optional LayoutManager::s_layoutStringToLoad; - std::vector LayoutManager::s_layouts; + namespace { + + std::optional s_layoutPathToLoad; + std::optional s_layoutStringToLoad; + std::vector s_layouts; + + } + void LayoutManager::load(const std::fs::path &path) { s_layoutPathToLoad = path; diff --git a/lib/libimhex/source/api/localization.cpp b/lib/libimhex/source/api/localization.cpp index bd161e4ac..f851f8965 100644 --- a/lib/libimhex/source/api/localization.cpp +++ b/lib/libimhex/source/api/localization.cpp @@ -4,9 +4,14 @@ namespace hex { - std::string LangEntry::s_fallbackLanguage; - std::string LangEntry::s_selectedLanguage; - std::map LangEntry::s_currStrings; + namespace { + + std::string s_fallbackLanguage; + std::string s_selectedLanguage; + std::map s_currStrings; + + } + LanguageDefinition::LanguageDefinition(std::map &&entries) { for (const auto &[key, value] : entries) { @@ -67,7 +72,7 @@ namespace hex { } const std::string &LangEntry::get() const { - auto &lang = LangEntry::s_currStrings; + auto &lang = s_currStrings; if (lang.contains(this->m_unlocalizedString)) return lang[this->m_unlocalizedString]; else @@ -75,7 +80,7 @@ namespace hex { } void LangEntry::loadLanguage(const std::string &language) { - LangEntry::s_currStrings.clear(); + s_currStrings.clear(); auto &definitions = ContentRegistry::Language::impl::getLanguageDefinitions(); @@ -83,15 +88,15 @@ namespace hex { return; for (auto &definition : definitions[language]) - LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); + s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); const auto fallbackLanguage = LangEntry::getFallbackLanguage(); if (language != fallbackLanguage) { for (auto &definition : definitions[fallbackLanguage]) - LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); + s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); } - LangEntry::s_selectedLanguage = language; + s_selectedLanguage = language; } const std::map &LangEntry::getSupportedLanguages() { @@ -99,20 +104,20 @@ namespace hex { } void LangEntry::setFallbackLanguage(const std::string &language) { - LangEntry::s_fallbackLanguage = language; + s_fallbackLanguage = language; } const std::string &LangEntry::getFallbackLanguage() { - return LangEntry::s_fallbackLanguage; + return s_fallbackLanguage; } void LangEntry::resetLanguageStrings() { - LangEntry::s_currStrings.clear(); - LangEntry::s_selectedLanguage.clear(); + s_currStrings.clear(); + s_selectedLanguage.clear(); } const std::string &LangEntry::getSelectedLanguage() { - return LangEntry::s_selectedLanguage; + return s_selectedLanguage; } } \ No newline at end of file diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index 5d917b0c1..e5be30a0a 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -170,34 +170,42 @@ namespace hex { } - std::fs::path PluginManager::s_pluginFolder; - std::vector PluginManager::s_plugins; + namespace { + + std::fs::path s_pluginFolder; + std::vector s_plugins; + + } bool PluginManager::load(const std::fs::path &pluginFolder) { if (!wolv::io::fs::exists(pluginFolder)) return false; - PluginManager::s_pluginFolder = pluginFolder; + s_pluginFolder = pluginFolder; for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) { if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") - PluginManager::s_plugins.emplace_back(pluginPath.path()); + s_plugins.emplace_back(pluginPath.path()); } - if (PluginManager::s_plugins.empty()) + if (s_plugins.empty()) return false; return true; } void PluginManager::unload() { - PluginManager::s_plugins.clear(); - PluginManager::s_pluginFolder.clear(); + s_plugins.clear(); + s_pluginFolder.clear(); } void PluginManager::reload() { PluginManager::unload(); - PluginManager::load(PluginManager::s_pluginFolder); + PluginManager::load(s_pluginFolder); + } + + const std::vector &PluginManager::getPlugins() { + return s_plugins; } } diff --git a/lib/libimhex/source/api/project_file_manager.cpp b/lib/libimhex/source/api/project_file_manager.cpp index d186a2070..21336ac28 100644 --- a/lib/libimhex/source/api/project_file_manager.cpp +++ b/lib/libimhex/source/api/project_file_manager.cpp @@ -11,20 +11,25 @@ namespace hex { - std::vector ProjectFile::s_handlers; - std::vector ProjectFile::s_providerHandlers; + namespace { - std::fs::path ProjectFile::s_currProjectPath; + std::vector s_handlers; + std::vector s_providerHandlers; + + std::fs::path s_currProjectPath; + + std::function s_loadProjectFunction; + std::function, bool)> s_storeProjectFunction; + + } - std::function ProjectFile::s_loadProjectFunction; - std::function, bool)> ProjectFile::s_storeProjectFunction; void ProjectFile::setProjectFunctions( const std::function &loadFun, const std::function, bool)> &storeFun ) { - ProjectFile::s_loadProjectFunction = loadFun; - ProjectFile::s_storeProjectFunction = storeFun; + s_loadProjectFunction = loadFun; + s_storeProjectFunction = storeFun; } bool ProjectFile::load(const std::fs::path &filePath) { @@ -36,19 +41,27 @@ namespace hex { } bool ProjectFile::hasPath() { - return !ProjectFile::s_currProjectPath.empty(); + return !s_currProjectPath.empty(); } void ProjectFile::clearPath() { - ProjectFile::s_currProjectPath.clear(); + s_currProjectPath.clear(); } std::fs::path ProjectFile::getPath() { - return ProjectFile::s_currProjectPath; + return s_currProjectPath; } void ProjectFile::setPath(const std::fs::path &path) { - ProjectFile::s_currProjectPath = path; + s_currProjectPath = path; + } + + std::vector &ProjectFile::getHandlers() { + return s_handlers; + } + + std::vector &ProjectFile::getProviderHandlers() { + return s_providerHandlers; } } \ No newline at end of file diff --git a/lib/libimhex/source/api/task.cpp b/lib/libimhex/source/api/task.cpp index 691675832..d970b992f 100644 --- a/lib/libimhex/source/api/task.cpp +++ b/lib/libimhex/source/api/task.cpp @@ -14,15 +14,20 @@ namespace hex { - std::mutex TaskManager::s_deferredCallsMutex, TaskManager::s_tasksFinishedMutex; + namespace { - std::list> TaskManager::s_tasks, TaskManager::s_taskQueue; - std::list> TaskManager::s_deferredCalls; - std::list> TaskManager::s_tasksFinishedCallbacks; + std::mutex s_deferredCallsMutex, s_tasksFinishedMutex; + + std::list> s_tasks, s_taskQueue; + std::list> s_deferredCalls; + std::list> s_tasksFinishedCallbacks; + + std::mutex s_queueMutex; + std::condition_variable s_jobCondVar; + std::vector s_workers; + + } - std::mutex TaskManager::s_queueMutex; - std::condition_variable TaskManager::s_jobCondVar; - std::vector TaskManager::s_workers; static void setThreadName(const std::string &name) { #if defined(OS_WINDOWS) @@ -210,19 +215,19 @@ namespace hex { log::debug("Initializing task manager thread pool with {} workers.", threadCount); for (u32 i = 0; i < threadCount; i++) - TaskManager::s_workers.emplace_back(TaskManager::runner); + s_workers.emplace_back(TaskManager::runner); } void TaskManager::exit() { - for (auto &task : TaskManager::s_tasks) + for (auto &task : s_tasks) task->interrupt(); - for (auto &thread : TaskManager::s_workers) + for (auto &thread : s_workers) thread.request_stop(); s_jobCondVar.notify_all(); - TaskManager::s_workers.clear(); + s_workers.clear(); } void TaskManager::runner(const std::stop_token &stopToken) { diff --git a/lib/libimhex/source/api/theme_manager.cpp b/lib/libimhex/source/api/theme_manager.cpp index c72e598d1..2679d2b22 100644 --- a/lib/libimhex/source/api/theme_manager.cpp +++ b/lib/libimhex/source/api/theme_manager.cpp @@ -9,11 +9,16 @@ namespace hex { - std::map ThemeManager::s_themes; - std::map ThemeManager::s_themeHandlers; - std::map ThemeManager::s_styleHandlers; - std::string ThemeManager::s_imageTheme; - std::string ThemeManager::s_currTheme; + namespace { + + std::map s_themes; + std::map s_themeHandlers; + std::map s_styleHandlers; + std::string s_imageTheme; + std::string s_currTheme; + + } + void ThemeManager::addThemeHandler(const std::string &name, const ColorMap &colorMap, const std::function &getFunction, const std::function &setFunction) { s_themeHandlers[name] = { colorMap, getFunction, setFunction }; @@ -201,11 +206,20 @@ namespace hex { } void ThemeManager::reset() { - ThemeManager::s_themes.clear(); - ThemeManager::s_styleHandlers.clear(); - ThemeManager::s_themeHandlers.clear(); - ThemeManager::s_imageTheme.clear(); - ThemeManager::s_currTheme.clear(); + s_themes.clear(); + s_styleHandlers.clear(); + s_themeHandlers.clear(); + s_imageTheme.clear(); + s_currTheme.clear(); + } + + + std::map &ThemeManager::getThemeHandlers() { + return s_themeHandlers; + } + + std::map &ThemeManager::getStyleHandlers() { + return s_styleHandlers; } } \ No newline at end of file diff --git a/lib/libimhex/source/data_processor/attribute.cpp b/lib/libimhex/source/data_processor/attribute.cpp index 6e6ee3756..1caa21451 100644 --- a/lib/libimhex/source/data_processor/attribute.cpp +++ b/lib/libimhex/source/data_processor/attribute.cpp @@ -3,9 +3,14 @@ namespace hex::dp { - int Attribute::s_idCounter = 1; + namespace { - Attribute::Attribute(IOType ioType, Type type, std::string unlocalizedName) : m_id(Attribute::s_idCounter++), m_ioType(ioType), m_type(type), m_unlocalizedName(std::move(unlocalizedName)) { + int s_idCounter = 1; + + } + + + Attribute::Attribute(IOType ioType, Type type, std::string unlocalizedName) : m_id(s_idCounter++), m_ioType(ioType), m_type(type), m_unlocalizedName(std::move(unlocalizedName)) { } Attribute::~Attribute() { @@ -13,4 +18,9 @@ namespace hex::dp { attr->removeConnectedAttribute(linkId); } + void Attribute::setIdCounter(int id) { + if (id > s_idCounter) + s_idCounter = id; + } + } \ No newline at end of file diff --git a/lib/libimhex/source/data_processor/link.cpp b/lib/libimhex/source/data_processor/link.cpp index 8884e74ff..76024b942 100644 --- a/lib/libimhex/source/data_processor/link.cpp +++ b/lib/libimhex/source/data_processor/link.cpp @@ -3,9 +3,16 @@ namespace hex::dp { - int Link::s_idCounter = 1; + namespace { - Link::Link(int from, int to) : m_id(Link::s_idCounter++), m_from(from), m_to(to) { } + int s_idCounter = 1; + } + Link::Link(int from, int to) : m_id(s_idCounter++), m_from(from), m_to(to) { } + + void Link::setIdCounter(int id) { + if (id > s_idCounter) + s_idCounter = id; + } } \ No newline at end of file diff --git a/lib/libimhex/source/data_processor/node.cpp b/lib/libimhex/source/data_processor/node.cpp index 681ec26ac..db0052e30 100644 --- a/lib/libimhex/source/data_processor/node.cpp +++ b/lib/libimhex/source/data_processor/node.cpp @@ -7,9 +7,13 @@ namespace hex::dp { - int Node::s_idCounter = 1; + namespace { - Node::Node(std::string unlocalizedTitle, std::vector attributes) : m_id(Node::s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) { + int s_idCounter = 1; + + } + + Node::Node(std::string unlocalizedTitle, std::vector attributes) : m_id(s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) { for (auto &attr : this->m_attributes) attr.setParentNode(this); } @@ -136,4 +140,9 @@ namespace hex::dp { this->m_overlay->getData() = data; } + void Node::setIdCounter(int id) { + if (id > s_idCounter) + s_idCounter = id; + } + } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/http_requests.cpp b/lib/libimhex/source/helpers/http_requests.cpp index 892144f90..88e0f1572 100644 --- a/lib/libimhex/source/helpers/http_requests.cpp +++ b/lib/libimhex/source/helpers/http_requests.cpp @@ -2,8 +2,12 @@ namespace hex { - std::string HttpRequest::s_caCertData; - std::string HttpRequest::s_proxyUrl; + namespace { + + std::string s_proxyUrl; + + } + HttpRequest::HttpRequest(std::string method, std::string url) : m_method(std::move(method)), m_url(std::move(url)) { AT_FIRST_TIME { @@ -38,21 +42,6 @@ namespace hex { curl_easy_setopt(this->m_curl, CURLOPT_PROXY, s_proxyUrl.c_str()); } - CURLcode HttpRequest::sslCtxFunction(CURL *ctx, void *sslctx, void *userData) { - hex::unused(ctx, userData); - - auto *cfg = static_cast(sslctx); - - auto crt = static_cast(userData); - mbedtls_x509_crt_init(crt); - - mbedtls_x509_crt_parse(crt, reinterpret_cast(HttpRequest::s_caCertData.data()), HttpRequest::s_caCertData.size()); - - mbedtls_ssl_conf_ca_chain(cfg, crt, nullptr); - - return CURLE_OK; - } - size_t HttpRequest::writeToVector(void *contents, size_t size, size_t nmemb, void *userdata) { auto &response = *reinterpret_cast*>(userdata); auto startSize = response.size(); @@ -84,4 +73,14 @@ namespace hex { return request.m_canceled ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK; } + void HttpRequest::setProxy(std::string proxy) { + s_proxyUrl = std::move(proxy); + } + + void HttpRequest::checkProxyErrors() { + if (!s_proxyUrl.empty()){ + log::info("A custom proxy '{0}' is in use. Is it working correctly?", s_proxyUrl); + } + } + } \ No newline at end of file diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 57e302f93..44907f41f 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -13,7 +13,12 @@ namespace hex::prv { - u32 Provider::s_idCounter = 0; + namespace { + + u32 s_idCounter = 0; + + } + Provider::Provider() : m_id(s_idCounter++) { this->m_patches.emplace_back(); diff --git a/lib/libimhex/source/ui/popup.cpp b/lib/libimhex/source/ui/popup.cpp index 688de942f..6d70c7669 100644 --- a/lib/libimhex/source/ui/popup.cpp +++ b/lib/libimhex/source/ui/popup.cpp @@ -2,6 +2,12 @@ namespace hex::impl { - std::vector> PopupBase::s_openPopups; + + [[nodiscard]] std::vector> &PopupBase::getOpenPopups() { + static std::vector> openPopups; + + return openPopups; + } + } \ No newline at end of file diff --git a/lib/libimhex/source/ui/view.cpp b/lib/libimhex/source/ui/view.cpp index a17d1a570..95e0b17e7 100644 --- a/lib/libimhex/source/ui/view.cpp +++ b/lib/libimhex/source/ui/view.cpp @@ -8,8 +8,12 @@ namespace hex { - ImFontAtlas *View::s_fontAtlas; - ImFontConfig View::s_fontConfig; + namespace { + + ImFontAtlas *s_fontAtlas; + ImFontConfig s_fontConfig; + + } View::View(std::string unlocalizedName) : m_unlocalizedViewName(std::move(unlocalizedName)) { } @@ -62,4 +66,10 @@ namespace hex { rightButtonFn(); } + ImFontAtlas *View::getFontAtlas() { return s_fontAtlas; } + void View::setFontAtlas(ImFontAtlas *atlas) { s_fontAtlas = atlas; } + + ImFontConfig View::getFontConfig() { return s_fontConfig; } + void View::setFontConfig(ImFontConfig config) { s_fontConfig = config; } + } \ No newline at end of file diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index f9133861b..8f67142e2 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -108,29 +108,6 @@ namespace hex::init { bool setupEnvironment() { hex::log::debug("Using romfs: '{}'", romfs::name()); - // Load the SSL certificate - constexpr static auto CaCertFileName = "cacert.pem"; - - // Look for a custom certificate in the config folder - std::fs::path caCertPath; - for (const auto &folder : fs::getDefaultPaths(fs::ImHexPath::Config)) { - for (const auto &file : std::fs::directory_iterator(folder)) { - if (file.path().filename() == CaCertFileName) { - caCertPath = file.path(); - break; - } - } - } - - // If a custom certificate was found, use it, otherwise use the one from the romfs - std::string caCertData; - if (!caCertPath.empty()) - caCertData = wolv::io::File(caCertPath, wolv::io::File::Mode::Read).readString(); - else - caCertData = std::string(romfs::get(CaCertFileName).string()); - - HttpRequest::setCACert(caCertData); - return true; }