2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/data_processor/node.hpp>
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
namespace hex::dp {
|
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
namespace {
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
int s_idCounter = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::Node(std::string unlocalizedTitle, std::vector<Attribute> attributes) : m_id(s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) {
|
2021-08-29 14:18:45 +02:00
|
|
|
for (auto &attr : this->m_attributes)
|
|
|
|
attr.setParentNode(this);
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
const std::vector<u8>& Node::getBufferOnInput(u32 index) {
|
2021-08-29 14:18:45 +02:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
|
|
|
if (attribute == nullptr)
|
2022-01-15 14:14:53 +01:00
|
|
|
throwNodeError(hex::format("Nothing connected to input '{0}'", LangEntry(this->m_attributes[index].getUnlocalizedName())));
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
if (attribute->getType() != Attribute::Type::Buffer)
|
|
|
|
throwNodeError("Tried to read buffer from non-buffer attribute");
|
|
|
|
|
|
|
|
markInputProcessed(index);
|
|
|
|
attribute->getParentNode()->process();
|
|
|
|
|
|
|
|
auto &outputData = attribute->getOutputData();
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
if (outputData.empty())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("No data available at connected attribute");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
return outputData;
|
2021-08-29 14:18:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
const i128& Node::getIntegerOnInput(u32 index) {
|
2021-08-29 14:18:45 +02:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
auto &outputData = [&] -> std::vector<u8>& {
|
|
|
|
if (attribute != nullptr) {
|
|
|
|
if (attribute->getType() != Attribute::Type::Integer)
|
|
|
|
throwNodeError("Tried to read integer from non-integer attribute");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
markInputProcessed(index);
|
|
|
|
attribute->getParentNode()->process();
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
return attribute->getOutputData();
|
|
|
|
} else {
|
|
|
|
return this->getAttribute(index).getOutputData();
|
|
|
|
}
|
|
|
|
}();
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
if (outputData.empty())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("No data available at connected attribute");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
if (outputData.size() < sizeof(i128))
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Not enough data provided for integer");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
return *reinterpret_cast<i128 *>(outputData.data());
|
2021-08-29 14:18:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
const long double& Node::getFloatOnInput(u32 index) {
|
2021-08-29 14:18:45 +02:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
auto &outputData = [&] -> std::vector<u8>& {
|
|
|
|
if (attribute != nullptr) {
|
|
|
|
if (attribute->getType() != Attribute::Type::Float)
|
|
|
|
throwNodeError("Tried to read integer from non-float attribute");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
markInputProcessed(index);
|
|
|
|
attribute->getParentNode()->process();
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
return attribute->getOutputData();
|
|
|
|
} else {
|
|
|
|
return this->getAttribute(index).getOutputData();
|
|
|
|
}
|
|
|
|
}();
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
if (outputData.empty())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("No data available at connected attribute");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
if (outputData.size() < sizeof(long double))
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Not enough data provided for float");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-02-12 17:33:53 +01:00
|
|
|
return *reinterpret_cast<long double *>(outputData.data());
|
2021-08-29 14:18:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
void Node::setBufferOnOutput(u32 index, std::span<const u8> data) {
|
2021-08-29 14:18:45 +02:00
|
|
|
if (index >= this->getAttributes().size())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Attribute index out of bounds!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
auto &attribute = this->getAttributes()[index];
|
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Tried to set output data of an input attribute!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-09-04 19:59:09 +02:00
|
|
|
if (attribute.getType() != Attribute::Type::Buffer)
|
|
|
|
throwNodeError("Tried to set buffer on non-buffer attribute!");
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
attribute.getOutputData() = { data.begin(), data.end() };
|
2021-08-29 14:18:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 08:54:08 +02:00
|
|
|
void Node::setIntegerOnOutput(u32 index, i128 integer) {
|
2021-08-29 14:18:45 +02:00
|
|
|
if (index >= this->getAttributes().size())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Attribute index out of bounds!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
auto &attribute = this->getAttributes()[index];
|
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Tried to set output data of an input attribute!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-09-04 19:59:09 +02:00
|
|
|
if (attribute.getType() != Attribute::Type::Integer)
|
|
|
|
throwNodeError("Tried to set integer on non-integer attribute!");
|
|
|
|
|
2022-10-26 08:54:08 +02:00
|
|
|
std::vector<u8> buffer(sizeof(integer), 0);
|
|
|
|
std::memcpy(buffer.data(), &integer, sizeof(integer));
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
attribute.getOutputData() = buffer;
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:54:08 +02:00
|
|
|
void Node::setFloatOnOutput(u32 index, long double floatingPoint) {
|
2021-08-29 14:18:45 +02:00
|
|
|
if (index >= this->getAttributes().size())
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Attribute index out of bounds!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
auto &attribute = this->getAttributes()[index];
|
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Tried to set output data of an input attribute!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
2023-09-04 19:59:09 +02:00
|
|
|
if (attribute.getType() != Attribute::Type::Float)
|
|
|
|
throwNodeError("Tried to set float on non-float attribute!");
|
|
|
|
|
2022-10-26 08:54:08 +02:00
|
|
|
std::vector<u8> buffer(sizeof(floatingPoint), 0);
|
|
|
|
std::memcpy(buffer.data(), &floatingPoint, sizeof(floatingPoint));
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
attribute.getOutputData() = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::setOverlayData(u64 address, const std::vector<u8> &data) {
|
|
|
|
if (this->m_overlay == nullptr)
|
2022-10-22 23:17:18 +02:00
|
|
|
throwNodeError("Tried setting overlay data on a node that's not the end of a chain!");
|
2021-08-29 14:18:45 +02:00
|
|
|
|
|
|
|
this->m_overlay->setAddress(address);
|
|
|
|
this->m_overlay->getData() = data;
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
void Node::setIdCounter(int id) {
|
|
|
|
if (id > s_idCounter)
|
|
|
|
s_idCounter = id;
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
}
|