1
0
mirror of synced 2024-11-24 15:50:16 +01:00

feat: Added basic network interface support

This commit is contained in:
WerWolv 2023-05-15 11:30:24 +02:00
parent e685d65be8
commit c006062540
12 changed files with 194 additions and 2 deletions

@ -1 +1 @@
Subproject commit 7b441291cd8f13c5d15e104d46b82a3fc493adcf Subproject commit 6b02f52077093975cae49d7fa337b1e490b51841

View File

@ -12,6 +12,7 @@
#include <span> #include <span>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -813,6 +814,35 @@ namespace hex {
} }
} }
namespace BackgroundServices {
namespace impl {
using Callback = std::function<void()>;
struct Service {
std::string name;
std::jthread thread;
};
std::vector<Service> &getServices();
void stopServices();
}
void registerService(const std::string &unlocalizedName, const impl::Callback &callback);
}
namespace CommunicationInterface {
namespace impl {
using NetworkCallback = std::function<nlohmann::json(const nlohmann::json &)>;
std::map<std::string, NetworkCallback> &getNetworkEndpoints();
}
void registerNetworkEndpoint(const std::string &endpoint, const impl::NetworkCallback &callback);
}
} }
} }

View File

@ -7,6 +7,7 @@
#include <hex/data_processor/node.hpp> #include <hex/data_processor/node.hpp>
#include <filesystem> #include <filesystem>
#include <thread>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -787,4 +788,62 @@ namespace hex {
} }
namespace ContentRegistry::BackgroundServices {
namespace impl {
std::vector<Service> &getServices() {
static std::vector<Service> services;
return services;
}
void stopServices() {
for (auto &service : getServices()) {
service.thread.request_stop();
}
for (auto &service : getServices()) {
service.thread.join();
}
}
}
void registerService(const std::string &unlocalizedName, const impl::Callback &callback) {
log::debug("Registered new background service: {}", unlocalizedName);
impl::getServices().push_back(impl::Service {
unlocalizedName,
std::jthread([callback](const std::stop_token &stopToken){
while (!stopToken.stop_requested()) {
callback();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
})
});
}
}
namespace ContentRegistry::CommunicationInterface {
namespace impl {
std::map<std::string, NetworkCallback> &getNetworkEndpoints() {
static std::map<std::string, NetworkCallback> endpoints;
return endpoints;
}
}
void registerNetworkEndpoint(const std::string &endpoint, const impl::NetworkCallback &callback) {
log::debug("Registered new network endpoint: {}", endpoint);
impl::getNetworkEndpoints().insert({ endpoint, callback });
}
}
} }

View File

@ -325,6 +325,11 @@ namespace hex::init {
ContentRegistry::FileHandler::impl::getEntries().clear(); ContentRegistry::FileHandler::impl::getEntries().clear();
ContentRegistry::Hashes::impl::getHashes().clear(); ContentRegistry::Hashes::impl::getHashes().clear();
ContentRegistry::BackgroundServices::impl::stopServices();
ContentRegistry::BackgroundServices::impl::getServices().clear();
ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints().clear();
LayoutManager::reset(); LayoutManager::reset();
ThemeManager::reset(); ThemeManager::reset();

View File

@ -164,6 +164,8 @@ namespace hex {
} }
this->m_windowTitle = title; this->m_windowTitle = title;
if (this->m_window != nullptr)
glfwSetWindowTitle(this->m_window, title.c_str()); glfwSetWindowTitle(this->m_window, title.c_str());
}); });
@ -1012,6 +1014,8 @@ namespace hex {
void Window::exitGLFW() { void Window::exitGLFW() {
glfwDestroyWindow(this->m_window); glfwDestroyWindow(this->m_window);
glfwTerminate(); glfwTerminate();
this->m_window = nullptr;
} }
void Window::exitImGui() { void Window::exitImGui() {

View File

@ -6,7 +6,9 @@ project(builtin)
add_library(${PROJECT_NAME} SHARED add_library(${PROJECT_NAME} SHARED
source/plugin_builtin.cpp source/plugin_builtin.cpp
source/content/background_services.cpp
source/content/command_palette_commands.cpp source/content/command_palette_commands.cpp
source/content/communication_interface.cpp
source/content/data_inspector.cpp source/content/data_inspector.cpp
source/content/pl_builtin_functions.cpp source/content/pl_builtin_functions.cpp
source/content/pl_pragmas.cpp source/content/pl_pragmas.cpp

View File

@ -421,6 +421,7 @@
"hex.builtin.setting.general.auto_load_patterns": "Auto-load supported pattern", "hex.builtin.setting.general.auto_load_patterns": "Auto-load supported pattern",
"hex.builtin.setting.general.check_for_updates": "Check for updates on startup", "hex.builtin.setting.general.check_for_updates": "Check for updates on startup",
"hex.builtin.setting.general.enable_unicode": "Load all unicode characters", "hex.builtin.setting.general.enable_unicode": "Load all unicode characters",
"hex.builtin.setting.general.network_interface": "Enable network interface",
"hex.builtin.setting.general.save_recent_providers": "Save recently used providers", "hex.builtin.setting.general.save_recent_providers": "Save recently used providers",
"hex.builtin.setting.general.show_tips": "Show tips on startup", "hex.builtin.setting.general.show_tips": "Show tips on startup",
"hex.builtin.setting.general.sync_pattern_source": "Sync pattern source code between providers", "hex.builtin.setting.general.sync_pattern_source": "Sync pattern source code between providers",

View File

@ -0,0 +1,58 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/localization.hpp>
#include <hex/api/event.hpp>
#include <wolv/net/socket_server.hpp>
#include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp>
namespace hex::plugin::builtin {
static bool networkInterfaceServiceEnabled = false;
namespace {
void handleNetworkInterfaceService() {
if (!networkInterfaceServiceEnabled) {
std::this_thread::yield();
return;
}
static wolv::net::SocketServer networkInterfaceServer(51337);
networkInterfaceServer.accept([](auto, const std::vector<u8> &data) -> std::vector<u8> {
nlohmann::json result;
try {
auto json = nlohmann::json::parse(data.begin(), data.end());
const auto &endpoints = ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints();
if (auto callback = endpoints.find(json["endpoint"].get<std::string>()); callback != endpoints.end()) {
auto responseData = callback->second(json["data"]);
result["status"] = "success";
result["data"] = responseData;
}
} catch (const std::exception &e) {
log::error("Network interface service error: {}", e.what());
result["status"] = "error";
result["data"]["error"] = e.what();
}
auto resultString = result.dump();
return { resultString.begin(), resultString.end() };
});
}
}
void registerBackgroundServices() {
EventManager::subscribe<EventSettingsChanged>([]{
networkInterfaceServiceEnabled = bool(ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", 0));
});
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.network_interface"_lang, handleNetworkInterfaceService);
}
}

View File

@ -0,0 +1,17 @@
#include <hex/api/content_registry.hpp>
#include <nlohmann/json.hpp>
namespace hex::plugin::builtin {
void registerNetworkEndpoints() {
ContentRegistry::CommunicationInterface::registerNetworkEndpoint("pattern_editor/set_code", [](const nlohmann::json &data) -> nlohmann::json {
auto code = data["code"].get<std::string>();
EventManager::post<RequestSetPatternLanguageCode>(code);
return { };
});
}
}

View File

@ -113,6 +113,17 @@ namespace hex::plugin::builtin {
return false; return false;
}); });
ContentRegistry::Settings::add("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", 0, [](auto name, nlohmann::json &setting) {
static bool enabled = static_cast<int>(setting);
if (ImGui::Checkbox(name.data(), &enabled)) {
setting = static_cast<int>(enabled);
return true;
}
return false;
});
/* Interface */ /* Interface */
ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", "Dark", [](auto name, nlohmann::json &setting) { ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", "Dark", [](auto name, nlohmann::json &setting) {

View File

@ -724,6 +724,7 @@ namespace hex::plugin::builtin {
EventManager::subscribe<RequestSetPatternLanguageCode>(this, [this](const std::string &code) { EventManager::subscribe<RequestSetPatternLanguageCode>(this, [this](const std::string &code) {
this->m_textEditor.SetText(code); this->m_textEditor.SetText(code);
this->m_hasUnevaluatedChanges = true;
}); });
EventManager::subscribe<EventSettingsChanged>(this, [this] { EventManager::subscribe<EventSettingsChanged>(this, [this] {

View File

@ -28,6 +28,8 @@ namespace hex::plugin::builtin {
void registerThemeHandlers(); void registerThemeHandlers();
void registerStyleHandlers(); void registerStyleHandlers();
void registerThemes(); void registerThemes();
void registerBackgroundServices();
void registerNetworkEndpoints();
void addFooterItems(); void addFooterItems();
void addToolbarItems(); void addToolbarItems();
@ -64,6 +66,8 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
registerThemeHandlers(); registerThemeHandlers();
registerStyleHandlers(); registerStyleHandlers();
registerThemes(); registerThemes();
registerBackgroundServices();
registerNetworkEndpoints();
addFooterItems(); addFooterItems();
addToolbarItems(); addToolbarItems();