1
0
mirror of synced 2024-11-25 00:00:27 +01:00

nodes: Allow data processor content to be stored in project files

This commit is contained in:
WerWolv 2021-05-18 18:06:47 +02:00
parent ee2b412a10
commit 0d11f4460f
5 changed files with 146 additions and 13 deletions

View File

@ -33,6 +33,9 @@ namespace hex {
[[nodiscard]] static const std::list<ImHexApi::Bookmarks::Entry>& getBookmarks() { return ProjectFile::s_bookmarks; }
static void setBookmarks(const std::list<ImHexApi::Bookmarks::Entry> &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<ImHexApi::Bookmarks::Entry> s_bookmarks;
static inline std::string s_dataProcessorContent;
};
}

View File

@ -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<std::vector<u8>>();
}
private:
u32 m_size = 1;
std::vector<u8> 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;

View File

@ -5,6 +5,8 @@
#include <set>
#include <vector>
#include <nlohmann/json.hpp>
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<Node*, std::string>;
void resetOutputData() {

View File

@ -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<Patches>();
ProjectFile::s_filePath = projectFileData["filePath"];
ProjectFile::s_pattern = projectFileData["pattern"];
ProjectFile::s_patches = projectFileData["patches"].get<Patches>();
ProjectFile::s_dataProcessorContent = projectFileData["dataProcessor"];
for (auto &element : projectFileData["bookmarks"].items()) {
ProjectFile::s_bookmarks.push_back(element.value().get<ImHexApi::Bookmarks::Entry>());
@ -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);

View File

@ -1,6 +1,7 @@
#include "views/view_data_processor.hpp"
#include <hex/providers/provider.hpp>
#include <helpers/project_file_handler.hpp>
#include <imnodes.h>
#include <nlohmann/json.hpp>
@ -39,6 +40,14 @@ namespace hex {
}
});
EventManager::subscribe<EventProjectFileStore>(this, [this] {
ProjectFile::setDataProcessorContent(this->saveNodes());
});
EventManager::subscribe<EventProjectFileLoad>(this, [this] {
this->loadNodes(ProjectFile::getDataProcessorContent());
});
EventManager::subscribe<EventFileLoaded>(this, [this](const std::string &path){
for (auto &node : this->m_nodes) {
node->setCurrentOverlay(nullptr);
@ -53,6 +62,8 @@ namespace hex {
EventManager::unsubscribe<EventSettingsChanged>(this);
EventManager::unsubscribe<EventFileLoaded>(this);
EventManager::unsubscribe<EventProjectFileStore>(this);
EventManager::unsubscribe<EventProjectFileLoad>(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;
}
}