mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 13:37:42 -05:00
Hopefully fixed the whole plugin mess I started
This commit is contained in:
@@ -10,14 +10,14 @@ namespace hex {
|
||||
/* Settings */
|
||||
|
||||
void ContentRegistry::Settings::load() {
|
||||
std::ifstream settingsFile(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path() / "settings.json");
|
||||
std::ifstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json");
|
||||
|
||||
if (settingsFile.good())
|
||||
settingsFile >> getSettingsData();
|
||||
}
|
||||
|
||||
void ContentRegistry::Settings::store() {
|
||||
std::ofstream settingsFile(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc);
|
||||
std::ofstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc);
|
||||
settingsFile << getSettingsData();
|
||||
}
|
||||
|
||||
@@ -40,19 +40,19 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
|
||||
return *SharedData::get().settingsEntries;
|
||||
return SharedData::settingsEntries;
|
||||
}
|
||||
|
||||
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
|
||||
return *SharedData::get().settingsJson;
|
||||
return SharedData::settingsJson;
|
||||
}
|
||||
|
||||
|
||||
/* Events */
|
||||
|
||||
auto ContentRegistry::Events::get(std::string_view name) {
|
||||
auto &customEvents = *SharedData::get().customEvents;
|
||||
auto &lastId = *SharedData::get().customEventsLastId;
|
||||
auto &customEvents = SharedData::customEvents;
|
||||
auto &lastId = SharedData::customEventsLastId;
|
||||
|
||||
if (!customEvents.contains(name.data())) {
|
||||
customEvents[name.data()] = static_cast<hex::Events>(lastId);
|
||||
@@ -70,7 +70,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::vector<ContentRegistry::CommandPaletteCommands::Entry>& ContentRegistry::CommandPaletteCommands::getEntries() {
|
||||
return *SharedData::get().commandPaletteCommands;
|
||||
return SharedData::commandPaletteCommands;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& ContentRegistry::PatternLanguageFunctions::getEntries() {
|
||||
return *SharedData::get().patternLanguageFunctions;
|
||||
return SharedData::patternLanguageFunctions;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::vector<View*>& ContentRegistry::Views::getEntries() {
|
||||
return *SharedData::get().views;
|
||||
return SharedData::views;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::vector<std::function<void()>>& ContentRegistry::Tools::getEntries() {
|
||||
return *SharedData::get().tools;
|
||||
return SharedData::tools;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,21 +5,21 @@
|
||||
namespace hex {
|
||||
|
||||
void EventManager::post(Events eventType, const void *userData) {
|
||||
for (auto &handler : *SharedData::get().eventHandlers)
|
||||
for (auto &handler : SharedData::eventHandlers)
|
||||
if (eventType == handler.eventType)
|
||||
handler.callback(userData);
|
||||
}
|
||||
|
||||
void EventManager::subscribe(Events eventType, void *owner, std::function<void(const void*)> callback) {
|
||||
for (auto &handler : *SharedData::get().eventHandlers)
|
||||
for (auto &handler : SharedData::eventHandlers)
|
||||
if (eventType == handler.eventType && owner == handler.owner)
|
||||
return;
|
||||
|
||||
SharedData::get().eventHandlers->push_back(EventHandler { owner, eventType, callback });
|
||||
SharedData::eventHandlers.push_back(EventHandler { owner, eventType, callback });
|
||||
}
|
||||
|
||||
void EventManager::unsubscribe(Events eventType, void *sender) {
|
||||
std::erase_if(*SharedData::get().eventHandlers, [&eventType, &sender](EventHandler handler) {
|
||||
std::erase_if(SharedData::eventHandlers, [&eventType, &sender](EventHandler handler) {
|
||||
return eventType == handler.eventType && sender == handler.owner;
|
||||
});
|
||||
}
|
||||
|
||||
24
plugins/libimhex/source/helpers/shared_data.cpp
Normal file
24
plugins/libimhex/source/helpers/shared_data.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <helpers/shared_data.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
std::vector<EventHandler> SharedData::eventHandlers;
|
||||
std::vector<std::function<void()>> SharedData::deferredCalls;
|
||||
prv::Provider *SharedData::currentProvider;
|
||||
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> SharedData::settingsEntries;
|
||||
nlohmann::json SharedData::settingsJson;
|
||||
std::map<std::string, Events> SharedData::customEvents;
|
||||
u32 SharedData::customEventsLastId;
|
||||
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;
|
||||
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> SharedData::patternLanguageFunctions;
|
||||
std::vector<View*> SharedData::views;
|
||||
std::vector<std::function<void()>> SharedData::tools;
|
||||
|
||||
int SharedData::mainArgc;
|
||||
char **SharedData::mainArgv;
|
||||
|
||||
ImVec2 SharedData::windowPos;
|
||||
ImVec2 SharedData::windowSize;
|
||||
|
||||
std::map<std::string, std::any> SharedData::sharedVariables;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace hex {
|
||||
bool View::handleShortcut(int key, int mods) { return false; }
|
||||
|
||||
std::vector<std::function<void()>>& View::getDeferedCalls() {
|
||||
return *SharedData::get().deferredCalls;
|
||||
return SharedData::deferredCalls;
|
||||
}
|
||||
|
||||
void View::postEvent(Events eventType, const void *userData) {
|
||||
@@ -76,7 +76,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
void View::doLater(std::function<void()> &&function) {
|
||||
SharedData::get().deferredCalls->push_back(function);
|
||||
SharedData::deferredCalls.push_back(function);
|
||||
}
|
||||
|
||||
void View::confirmButtons(const char *textLeft, const char *textRight, std::function<void()> leftButtonFn, std::function<void()> rightButtonFn) {
|
||||
|
||||
Reference in New Issue
Block a user