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

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;
});
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 */
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) {
this->m_textEditor.SetText(code);
this->m_hasUnevaluatedChanges = true;
});
EventManager::subscribe<EventSettingsChanged>(this, [this] {

View File

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