From 0c5e72ab6a8d211b6fdc3a3ffdc377bfc521c0c6 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 26 Oct 2022 08:54:08 +0200 Subject: [PATCH] nodes: Improve precision of values passed between nodes --- .../include/hex/data_processor/node.hpp | 8 ++--- lib/libimhex/source/data_processor/node.cpp | 22 ++++++------ .../source/content/data_processor_nodes.cpp | 34 +++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/libimhex/include/hex/data_processor/node.hpp b/lib/libimhex/include/hex/data_processor/node.hpp index 9563e713f..71ff6a1b2 100644 --- a/lib/libimhex/include/hex/data_processor/node.hpp +++ b/lib/libimhex/include/hex/data_processor/node.hpp @@ -104,12 +104,12 @@ namespace hex::dp { } std::vector getBufferOnInput(u32 index); - i64 getIntegerOnInput(u32 index); - float getFloatOnInput(u32 index); + i128 getIntegerOnInput(u32 index); + long double getFloatOnInput(u32 index); void setBufferOnOutput(u32 index, const std::vector &data); - void setIntegerOnOutput(u32 index, i64 integer); - void setFloatOnOutput(u32 index, float floatingPoint); + void setIntegerOnOutput(u32 index, i128 integer); + void setFloatOnOutput(u32 index, long double floatingPoint); void setOverlayData(u64 address, const std::vector &data); }; diff --git a/lib/libimhex/source/data_processor/node.cpp b/lib/libimhex/source/data_processor/node.cpp index 5d9af5b3b..b116238fc 100644 --- a/lib/libimhex/source/data_processor/node.cpp +++ b/lib/libimhex/source/data_processor/node.cpp @@ -34,7 +34,7 @@ namespace hex::dp { return outputData.value(); } - i64 Node::getIntegerOnInput(u32 index) { + i128 Node::getIntegerOnInput(u32 index) { auto attribute = this->getConnectedInputAttribute(index); if (attribute == nullptr) @@ -57,7 +57,7 @@ namespace hex::dp { return *reinterpret_cast(outputData->data()); } - float Node::getFloatOnInput(u32 index) { + long double Node::getFloatOnInput(u32 index) { auto attribute = this->getConnectedInputAttribute(index); if (attribute == nullptr) @@ -74,11 +74,11 @@ namespace hex::dp { if (!outputData.has_value()) throwNodeError("No data available at connected attribute"); - if (outputData->size() < sizeof(float)) + if (outputData->size() < sizeof(long double)) throwNodeError("Not enough data provided for float"); - float result = 0; - std::memcpy(&result, outputData->data(), sizeof(float)); + long double result = 0; + std::memcpy(&result, outputData->data(), sizeof(long double)); return result; } @@ -94,7 +94,7 @@ namespace hex::dp { attribute.getOutputData() = data; } - void Node::setIntegerOnOutput(u32 index, i64 integer) { + void Node::setIntegerOnOutput(u32 index, i128 integer) { if (index >= this->getAttributes().size()) throwNodeError("Attribute index out of bounds!"); @@ -103,13 +103,13 @@ namespace hex::dp { if (attribute.getIOType() != Attribute::IOType::Out) throwNodeError("Tried to set output data of an input attribute!"); - std::vector buffer(sizeof(u64), 0); - std::memcpy(buffer.data(), &integer, sizeof(u64)); + std::vector buffer(sizeof(integer), 0); + std::memcpy(buffer.data(), &integer, sizeof(integer)); attribute.getOutputData() = buffer; } - void Node::setFloatOnOutput(u32 index, float floatingPoint) { + void Node::setFloatOnOutput(u32 index, long double floatingPoint) { if (index >= this->getAttributes().size()) throwNodeError("Attribute index out of bounds!"); @@ -118,8 +118,8 @@ namespace hex::dp { if (attribute.getIOType() != Attribute::IOType::Out) throwNodeError("Tried to set output data of an input attribute!"); - std::vector buffer(sizeof(float), 0); - std::memcpy(buffer.data(), &floatingPoint, sizeof(float)); + std::vector buffer(sizeof(floatingPoint), 0); + std::memcpy(buffer.data(), &floatingPoint, sizeof(floatingPoint)); attribute.getOutputData() = buffer; } diff --git a/plugins/builtin/source/content/data_processor_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes.cpp index 29694f571..11269b172 100644 --- a/plugins/builtin/source/content/data_processor_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes.cpp @@ -37,7 +37,7 @@ namespace hex::plugin::builtin { void drawNode() override { constexpr static int StepSize = 1, FastStepSize = 10; - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::InputScalar("hex.builtin.nodes.constants.buffer.size"_lang, ImGuiDataType_U32, &this->m_size, &StepSize, &FastStepSize); ImGui::PopItemWidth(); } @@ -73,13 +73,13 @@ namespace hex::plugin::builtin { } void drawNode() override { - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::InputTextIcon("##string", ICON_VS_SYMBOL_KEY, this->m_value); ImGui::PopItemWidth(); } void process() override { - this->setBufferOnOutput(0, { this->m_value.begin(), this->m_value.end() }); + this->setBufferOnOutput(0, hex::decodeByteString(this->m_value)); } void store(nlohmann::json &j) override { @@ -101,7 +101,7 @@ namespace hex::plugin::builtin { NodeInteger() : Node("hex.builtin.nodes.constants.int.header", { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Integer, "") }) { } void drawNode() override { - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::InputHexadecimal("##integer_value", &this->m_value); ImGui::PopItemWidth(); } @@ -129,7 +129,7 @@ namespace hex::plugin::builtin { NodeFloat() : Node("hex.builtin.nodes.constants.float.header", { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Float, "") }) { } void drawNode() override { - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::InputScalar("##floatValue", ImGuiDataType_Float, &this->m_value, nullptr, nullptr, "%f", ImGuiInputTextFlags_CharsDecimal); ImGui::PopItemWidth(); } @@ -161,16 +161,16 @@ namespace hex::plugin::builtin { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Integer, "hex.builtin.nodes.constants.rgba8.output.a") }) { } void drawNode() override { - ImGui::PushItemWidth(200); + ImGui::PushItemWidth(200_scaled); ImGui::ColorPicker4("##colorPicker", &this->m_color.Value.x, ImGuiColorEditFlags_AlphaBar); ImGui::PopItemWidth(); } void process() override { - this->setBufferOnOutput(0, hex::toBytes(this->m_color.Value.x * 0xFF)); - this->setBufferOnOutput(1, hex::toBytes(this->m_color.Value.y * 0xFF)); - this->setBufferOnOutput(2, hex::toBytes(this->m_color.Value.z * 0xFF)); - this->setBufferOnOutput(3, hex::toBytes(this->m_color.Value.w * 0xFF)); + this->setBufferOnOutput(0, hex::toBytes(this->m_color.Value.x * 0xFF)); + this->setBufferOnOutput(1, hex::toBytes(this->m_color.Value.y * 0xFF)); + this->setBufferOnOutput(2, hex::toBytes(this->m_color.Value.z * 0xFF)); + this->setBufferOnOutput(3, hex::toBytes(this->m_color.Value.w * 0xFF)); } void store(nlohmann::json &j) override { @@ -224,7 +224,7 @@ namespace hex::plugin::builtin { NodeDisplayInteger() : Node("hex.builtin.nodes.display.int.header", { dp::Attribute(dp::Attribute::IOType::In, dp::Attribute::Type::Integer, "hex.builtin.nodes.common.input") }) { } void drawNode() override { - ImGui::PushItemWidth(150); + ImGui::PushItemWidth(150_scaled); if (this->m_value.has_value()) ImGui::TextFormatted("0x{0:X}", this->m_value.value()); else @@ -248,7 +248,7 @@ namespace hex::plugin::builtin { NodeDisplayFloat() : Node("hex.builtin.nodes.display.float.header", { dp::Attribute(dp::Attribute::IOType::In, dp::Attribute::Type::Float, "hex.builtin.nodes.common.input") }) { } void drawNode() override { - ImGui::PushItemWidth(150); + ImGui::PushItemWidth(150_scaled); if (this->m_value.has_value()) ImGui::TextFormatted("{0}", this->m_value.value()); else @@ -559,14 +559,14 @@ namespace hex::plugin::builtin { auto from = this->getIntegerOnInput(1); auto to = this->getIntegerOnInput(2); - if (from < 0 || static_cast(from) >= input.size()) + if (from < 0 || static_cast(from) >= input.size()) throwNodeError("'from' input out of range"); - if (to < 0 || static_cast(from) >= input.size()) + if (to < 0 || static_cast(to) >= input.size()) throwNodeError("'to' input out of range"); if (to <= from) throwNodeError("'to' input needs to be greater than 'from' input"); - this->setBufferOnOutput(3, std::vector(input.begin() + from, input.begin() + to)); + this->setBufferOnOutput(3, std::vector(input.begin() + u64(from), input.begin() + u64(to))); } }; @@ -706,7 +706,7 @@ namespace hex::plugin::builtin { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Buffer, "hex.builtin.nodes.common.output") }) { } void drawNode() override { - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::Combo("hex.builtin.nodes.crypto.aes.mode"_lang, &this->m_mode, "ECB\0CBC\0CFB128\0CTR\0GCM\0CCM\0OFB\0"); ImGui::Combo("hex.builtin.nodes.crypto.aes.key_length"_lang, &this->m_keyLength, "128 Bits\000192 Bits\000256 Bits\000"); ImGui::PopItemWidth(); @@ -1089,7 +1089,7 @@ namespace hex::plugin::builtin { NodePatternLanguageOutVariable() : Node("hex.builtin.nodes.pattern_language.out_var.header", { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Buffer, "hex.builtin.nodes.common.output") }) { } void drawNode() override { - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(100_scaled); ImGui::InputText("##name", this->m_name); ImGui::PopItemWidth(); }