feat: Add constant data input to buffer node (#1981)
Added a hex data input field to the buffer node in the data processor. ![image](https://github.com/user-attachments/assets/a98e1174-7d35-4130-b9f7-9ef5d0703bc7)
This commit is contained in:
parent
7032473ecf
commit
813276c907
@ -1,3 +1,4 @@
|
|||||||
|
#include <algorithm>
|
||||||
#include <hex/api/content_registry.hpp>
|
#include <hex/api/content_registry.hpp>
|
||||||
#include <hex/api/localization_manager.hpp>
|
#include <hex/api/localization_manager.hpp>
|
||||||
#include <hex/helpers/utils.hpp>
|
#include <hex/helpers/utils.hpp>
|
||||||
@ -33,9 +34,39 @@ namespace hex::plugin::builtin {
|
|||||||
ImGui::PushItemWidth(100_scaled);
|
ImGui::PushItemWidth(100_scaled);
|
||||||
ImGui::InputScalar("hex.builtin.nodes.constants.buffer.size"_lang, ImGuiDataType_U32, &m_size, &StepSize, &FastStepSize);
|
ImGui::InputScalar("hex.builtin.nodes.constants.buffer.size"_lang, ImGuiDataType_U32, &m_size, &StepSize, &FastStepSize);
|
||||||
ImGui::PopItemWidth();
|
ImGui::PopItemWidth();
|
||||||
|
|
||||||
|
ImGui::InputTextMultiline("##buffer", m_constantString, ImVec2(150_scaled, 0), ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CharsHexadecimal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adapted from PatternLanguageBot
|
||||||
|
std::vector<u8> parseByteString(const std::string &string) {
|
||||||
|
if (string.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto byteString = std::string(string);
|
||||||
|
std::erase(byteString, ' ');
|
||||||
|
std::erase(byteString, '\n');
|
||||||
|
|
||||||
|
if ((byteString.length() % 2) != 0)
|
||||||
|
throwNodeError("Invalid byte string length");
|
||||||
|
|
||||||
|
std::vector<u8> result;
|
||||||
|
for (u32 i = 0; i < byteString.length(); i += 2) {
|
||||||
|
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
|
||||||
|
throwNodeError("Invalid byte string format");
|
||||||
|
|
||||||
|
result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void process() override {
|
void process() override {
|
||||||
|
m_buffer = parseByteString(m_constantString);
|
||||||
|
|
||||||
|
m_size = std::max<std::size_t>(m_buffer.size(), m_size);
|
||||||
|
|
||||||
|
// fill buffer with zeros if required
|
||||||
if (m_buffer.size() != m_size)
|
if (m_buffer.size() != m_size)
|
||||||
m_buffer.resize(m_size, 0x00);
|
m_buffer.resize(m_size, 0x00);
|
||||||
|
|
||||||
@ -46,16 +77,19 @@ namespace hex::plugin::builtin {
|
|||||||
j = nlohmann::json::object();
|
j = nlohmann::json::object();
|
||||||
|
|
||||||
j["size"] = m_size;
|
j["size"] = m_size;
|
||||||
|
j["constantString"] = m_constantString;
|
||||||
j["data"] = m_buffer;
|
j["data"] = m_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(const nlohmann::json &j) override {
|
void load(const nlohmann::json &j) override {
|
||||||
m_size = j.at("size");
|
m_size = j.at("size");
|
||||||
|
m_constantString = j.at("constantString").get<std::string>();
|
||||||
m_buffer = j.at("data").get<std::vector<u8>>();
|
m_buffer = j.at("data").get<std::vector<u8>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
u32 m_size = 1;
|
u32 m_size = 1;
|
||||||
|
std::string m_constantString;
|
||||||
std::vector<u8> m_buffer;
|
std::vector<u8> m_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user