diff --git a/include/helpers/project_file_handler.hpp b/include/helpers/project_file_handler.hpp index 4f0f0717f..58f581d58 100644 --- a/include/helpers/project_file_handler.hpp +++ b/include/helpers/project_file_handler.hpp @@ -33,6 +33,9 @@ namespace hex { [[nodiscard]] static const std::list& getBookmarks() { return ProjectFile::s_bookmarks; } static void setBookmarks(const std::list &bookmarks) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_bookmarks = bookmarks; } + [[nodiscard]] static const std::string_view getDataProcessorContent() { return ProjectFile::s_dataProcessorContent; } + static void setDataProcessorContent(std::string_view json) { ProjectFile::s_dataProcessorContent = json; } + private: static inline std::string s_currProjectFilePath; static inline bool s_hasUnsavedChanged = false; @@ -41,6 +44,7 @@ namespace hex { static inline std::string s_pattern; static inline Patches s_patches; static inline std::list s_bookmarks; + static inline std::string s_dataProcessorContent; }; } \ No newline at end of file diff --git a/plugins/builtin/source/content/data_processor_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes.cpp index cc12b3ddd..77ac4591b 100644 --- a/plugins/builtin/source/content/data_processor_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes.cpp @@ -38,6 +38,20 @@ namespace hex::plugin::builtin { this->setBufferOnOutput(0, this->m_buffer); } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["size"] = this->m_size; + output["data"] = this->m_buffer; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_size = j["size"]; + this->m_buffer = j["data"].get>(); + } + private: u32 m_size = 1; std::vector m_buffer; @@ -66,6 +80,18 @@ namespace hex::plugin::builtin { this->setBufferOnOutput(0, output); } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["data"] = this->m_value; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_value = j["data"]; + } + private: std::string m_value; }; @@ -89,6 +115,18 @@ namespace hex::plugin::builtin { this->setBufferOnOutput(0, data); } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["data"] = this->m_value; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_value = j["data"]; + } + private: u64 m_value = 0; }; @@ -113,6 +151,18 @@ namespace hex::plugin::builtin { this->setBufferOnOutput(0, data); } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["data"] = this->m_value; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_value = j["data"]; + } + private: float m_value = 0; }; @@ -139,6 +189,22 @@ namespace hex::plugin::builtin { } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["data"] = nlohmann::json::object(); + output["data"]["r"] = this->m_color.Value.x; + output["data"]["g"] = this->m_color.Value.y; + output["data"]["b"] = this->m_color.Value.z; + output["data"]["a"] = this->m_color.Value.w; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_color = ImVec4(j["data"]["r"], j["data"]["g"], j["data"]["b"], j["data"]["a"]); + } + private: ImColor m_color; }; @@ -157,6 +223,18 @@ namespace hex::plugin::builtin { } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["comment"] = this->m_comment; + + return output; + } + + void load(nlohmann::json &j) override { + this->m_comment = j["comment"]; + } + private: std::string m_comment; }; @@ -660,6 +738,22 @@ namespace hex::plugin::builtin { this->setBufferOnOutput(4, output); } + nlohmann::json store() override { + auto output = nlohmann::json::object(); + + output["data"] = nlohmann::json::object(); + output["data"]["mode"] = this->m_mode; + output["data"]["key_length"] = this->m_keyLength; + + + return output; + } + + void load(nlohmann::json &j) override { + this->m_mode = j["data"]["mode"]; + this->m_keyLength = j["data"]["key_length"]; + } + private: int m_mode = 0; int m_keyLength = 0; diff --git a/plugins/libimhex/include/hex/data_processor/node.hpp b/plugins/libimhex/include/hex/data_processor/node.hpp index a3bf70b28..4ec7f9c2b 100644 --- a/plugins/libimhex/include/hex/data_processor/node.hpp +++ b/plugins/libimhex/include/hex/data_processor/node.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace hex::dp { class Node { @@ -32,6 +34,9 @@ namespace hex::dp { virtual void drawNode() { } virtual void process() = 0; + virtual nlohmann::json store() { return nullptr; } + virtual void load(nlohmann::json &j) { } + using NodeError = std::pair; void resetOutputData() { diff --git a/source/helpers/project_file_handler.cpp b/source/helpers/project_file_handler.cpp index c8ae6aeee..bbaf7333c 100644 --- a/source/helpers/project_file_handler.cpp +++ b/source/helpers/project_file_handler.cpp @@ -36,9 +36,10 @@ namespace hex { std::ifstream projectFile(filePath.data()); projectFile >> projectFileData; - ProjectFile::s_filePath = projectFileData["filePath"]; - ProjectFile::s_pattern = projectFileData["pattern"]; - ProjectFile::s_patches = projectFileData["patches"].get(); + ProjectFile::s_filePath = projectFileData["filePath"]; + ProjectFile::s_pattern = projectFileData["pattern"]; + ProjectFile::s_patches = projectFileData["patches"].get(); + ProjectFile::s_dataProcessorContent = projectFileData["dataProcessor"]; for (auto &element : projectFileData["bookmarks"].items()) { ProjectFile::s_bookmarks.push_back(element.value().get()); @@ -64,9 +65,10 @@ namespace hex { filePath = ProjectFile::s_currProjectFilePath; try { - projectFileData["filePath"] = ProjectFile::s_filePath; - projectFileData["pattern"] = ProjectFile::s_pattern; - projectFileData["patches"] = ProjectFile::s_patches; + projectFileData["filePath"] = ProjectFile::s_filePath; + projectFileData["pattern"] = ProjectFile::s_pattern; + projectFileData["patches"] = ProjectFile::s_patches; + projectFileData["dataProcessor"] = ProjectFile::s_dataProcessorContent; for (auto &bookmark : ProjectFile::s_bookmarks) { projectFileData["bookmarks"].push_back(bookmark); diff --git a/source/views/view_data_processor.cpp b/source/views/view_data_processor.cpp index f7ac43daa..4d1aa98cf 100644 --- a/source/views/view_data_processor.cpp +++ b/source/views/view_data_processor.cpp @@ -1,6 +1,7 @@ #include "views/view_data_processor.hpp" #include +#include #include #include @@ -39,6 +40,14 @@ namespace hex { } }); + EventManager::subscribe(this, [this] { + ProjectFile::setDataProcessorContent(this->saveNodes()); + }); + + EventManager::subscribe(this, [this] { + this->loadNodes(ProjectFile::getDataProcessorContent()); + }); + EventManager::subscribe(this, [this](const std::string &path){ for (auto &node : this->m_nodes) { node->setCurrentOverlay(nullptr); @@ -53,6 +62,8 @@ namespace hex { EventManager::unsubscribe(this); EventManager::unsubscribe(this); + EventManager::unsubscribe(this); + EventManager::unsubscribe(this); imnodes::PopAttributeFlag(); imnodes::PopAttributeFlag(); @@ -383,6 +394,8 @@ namespace hex { currNodeOutput["attrs"] = json::array(); currNodeOutput["id"] = id; + currNodeOutput["data"] = node->store(); + u32 attrIndex = 0; for (auto &attr : node->getAttributes()) { currNodeOutput["attrs"][attrIndex] = attr.getID(); @@ -408,7 +421,9 @@ namespace hex { json input = json::parse(data); - u32 maxId = 0; + u32 maxNodeId = 0; + u32 maxAttrId = 0; + u32 maxLinkId = 0; for (auto &node : this->m_nodes) delete node; @@ -428,10 +443,10 @@ namespace hex { if (newNode == nullptr) continue; - u32 id = node["id"]; - maxId = std::max(id, maxId); + u32 nodeId = node["id"]; + maxNodeId = std::max(nodeId, maxNodeId); - newNode->setID(id); + newNode->setID(nodeId); bool hasOutput = false; bool hasInput = false; @@ -443,21 +458,30 @@ namespace hex { if (attr.getIOType() == dp::Attribute::IOType::In) hasInput = true; - attr.setID(node["attrs"][attrIndex]); + u32 attrId = node["attrs"][attrIndex]; + maxAttrId = std::max(attrId, maxAttrId); + + attr.setID(attrId); attrIndex++; } + if (!node["data"].is_null()) + newNode->load(node["data"]); + if (hasInput && !hasOutput) this->m_endNodes.push_back(newNode); this->m_nodes.push_back(newNode); - imnodes::SetNodeGridSpacePos(id, ImVec2(node["pos"]["x"], node["pos"]["y"])); + imnodes::SetNodeGridSpacePos(nodeId, ImVec2(node["pos"]["x"], node["pos"]["y"])); } for (auto &link : input["links"]) { dp::Link newLink(link["from"], link["to"]); - newLink.setID(link["id"]); + u32 linkId = link["id"]; + maxLinkId = std::max(linkId, maxLinkId); + + newLink.setID(linkId); this->m_links.push_back(newLink); dp::Attribute *fromAttr, *toAttr; @@ -485,6 +509,10 @@ namespace hex { fromAttr->addConnectedAttribute(newLink.getID(), toAttr); toAttr->addConnectedAttribute(newLink.getID(), fromAttr); } + + SharedData::dataProcessorNodeIdCounter = maxNodeId + 1; + SharedData::dataProcessorAttrIdCounter = maxAttrId + 1; + SharedData::dataProcessorLinkIdCounter = maxLinkId + 1; } } \ No newline at end of file