2021-01-30 22:39:06 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex/data_processor/attribute.hpp>
|
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
namespace hex::dp {
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
public:
|
2021-02-13 15:15:32 +01:00
|
|
|
Node(std::string_view unlocalizedName, std::vector<Attribute> attributes) : m_id(SharedData::dataProcessorNodeIdCounter++), m_unlocalizedName(unlocalizedName), m_attributes(std::move(attributes)) {
|
2021-01-30 22:39:06 +01:00
|
|
|
for (auto &attr : this->m_attributes)
|
|
|
|
attr.setParentNode(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Node() = default;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 getID() const { return this->m_id; }
|
2021-02-13 15:15:32 +01:00
|
|
|
[[nodiscard]] std::string_view getUnlocalizedName() const { return this->m_unlocalizedName; }
|
2021-01-30 22:39:06 +01:00
|
|
|
[[nodiscard]] std::vector<Attribute>& getAttributes() { return this->m_attributes; }
|
|
|
|
|
2021-01-31 16:11:25 +01:00
|
|
|
void setCurrentOverlay(prv::Overlay *overlay) {
|
|
|
|
this->m_overlay = overlay;
|
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
virtual void drawNode() { }
|
2021-01-31 16:11:25 +01:00
|
|
|
virtual void process() = 0;
|
2021-02-04 01:14:05 +01:00
|
|
|
|
|
|
|
using NodeError = std::pair<Node*, std::string>;
|
|
|
|
|
|
|
|
void resetOutputData() {
|
|
|
|
for (auto &attribute : this->m_attributes)
|
|
|
|
attribute.getOutputData().reset();
|
|
|
|
}
|
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
void resetProcessedInputs() {
|
|
|
|
this->m_processedInputs.clear();
|
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
private:
|
|
|
|
u32 m_id;
|
2021-02-13 15:15:32 +01:00
|
|
|
std::string m_unlocalizedName;
|
2021-01-30 22:39:06 +01:00
|
|
|
std::vector<Attribute> m_attributes;
|
2021-03-07 13:20:33 +01:00
|
|
|
std::set<u32> m_processedInputs;
|
2021-01-31 16:11:25 +01:00
|
|
|
prv::Overlay *m_overlay = nullptr;
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2021-01-31 16:11:25 +01:00
|
|
|
Attribute* getConnectedInputAttribute(u32 index) {
|
|
|
|
if (index >= this->getAttributes().size())
|
|
|
|
throw std::runtime_error("Attribute index out of bounds!");
|
|
|
|
|
|
|
|
auto &connectedAttribute = this->getAttributes()[index].getConnectedAttributes();
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
if (connectedAttribute.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return connectedAttribute.begin()->second;
|
|
|
|
}
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
void markInputProcessed(u32 index) {
|
|
|
|
const auto &[iter, inserted] = this->m_processedInputs.insert(index);
|
|
|
|
if (!inserted)
|
|
|
|
throwNodeError("Recursion detected!");
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:11:25 +01:00
|
|
|
protected:
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
[[noreturn]] void throwNodeError(std::string_view message) {
|
|
|
|
throw NodeError(this, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> getBufferOnInput(u32 index) {
|
2021-01-31 16:11:25 +01:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (attribute == nullptr)
|
2021-03-03 19:58:22 +01:00
|
|
|
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
2021-02-04 01:14:05 +01:00
|
|
|
|
|
|
|
if (attribute->getType() != Attribute::Type::Buffer)
|
|
|
|
throwNodeError("Tried to read buffer from non-buffer attribute");
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
markInputProcessed(index);
|
2021-01-31 16:11:25 +01:00
|
|
|
attribute->getParentNode()->process();
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &outputData = attribute->getOutputData();
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (!outputData.has_value())
|
|
|
|
throw std::runtime_error("No data available at connected attribute");
|
|
|
|
|
|
|
|
return outputData.value();
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
u64 getIntegerOnInput(u32 index) {
|
2021-01-31 16:11:25 +01:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (attribute == nullptr)
|
2021-03-03 19:58:22 +01:00
|
|
|
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
2021-02-04 01:14:05 +01:00
|
|
|
|
|
|
|
if (attribute->getType() != Attribute::Type::Integer)
|
|
|
|
throwNodeError("Tried to read integer from non-integer attribute");
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
markInputProcessed(index);
|
2021-01-31 16:11:25 +01:00
|
|
|
attribute->getParentNode()->process();
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &outputData = attribute->getOutputData();
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (!outputData.has_value())
|
|
|
|
throw std::runtime_error("No data available at connected attribute");
|
|
|
|
|
|
|
|
if (outputData->size() < sizeof(u64))
|
|
|
|
throw std::runtime_error("Not enough data provided for integer");
|
|
|
|
|
|
|
|
return *reinterpret_cast<u64*>(outputData->data());
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
float getFloatOnInput(u32 index) {
|
2021-01-31 16:11:25 +01:00
|
|
|
auto attribute = this->getConnectedInputAttribute(index);
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (attribute == nullptr)
|
2021-03-03 19:58:22 +01:00
|
|
|
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
2021-02-04 01:14:05 +01:00
|
|
|
|
|
|
|
if (attribute->getType() != Attribute::Type::Float)
|
|
|
|
throwNodeError("Tried to read float from non-float attribute");
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-03-07 13:20:33 +01:00
|
|
|
markInputProcessed(index);
|
2021-01-31 16:11:25 +01:00
|
|
|
attribute->getParentNode()->process();
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &outputData = attribute->getOutputData();
|
2021-01-31 16:11:25 +01:00
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
if (!outputData.has_value())
|
|
|
|
throw std::runtime_error("No data available at connected attribute");
|
|
|
|
|
|
|
|
if (outputData->size() < sizeof(float))
|
|
|
|
throw std::runtime_error("Not enough data provided for float");
|
|
|
|
|
|
|
|
return *reinterpret_cast<float*>(outputData->data());
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setBufferOnOutput(u32 index, std::vector<u8> data) {
|
|
|
|
if (index >= this->getAttributes().size())
|
|
|
|
throw std::runtime_error("Attribute index out of bounds!");
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &attribute = this->getAttributes()[index];
|
2021-01-31 16:11:25 +01:00
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
|
|
|
throw std::runtime_error("Tried to set output data of an input attribute!");
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
attribute.getOutputData() = data;
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setIntegerOnOutput(u32 index, u64 integer) {
|
|
|
|
if (index >= this->getAttributes().size())
|
|
|
|
throw std::runtime_error("Attribute index out of bounds!");
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &attribute = this->getAttributes()[index];
|
2021-01-31 16:11:25 +01:00
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
|
|
|
throw std::runtime_error("Tried to set output data of an input attribute!");
|
|
|
|
|
|
|
|
std::vector<u8> buffer(sizeof(u64), 0);
|
|
|
|
std::memcpy(buffer.data(), &integer, sizeof(u64));
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
attribute.getOutputData() = buffer;
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFloatOnOutput(u32 index, float floatingPoint) {
|
|
|
|
if (index >= this->getAttributes().size())
|
|
|
|
throw std::runtime_error("Attribute index out of bounds!");
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
auto &attribute = this->getAttributes()[index];
|
2021-01-31 16:11:25 +01:00
|
|
|
|
|
|
|
if (attribute.getIOType() != Attribute::IOType::Out)
|
|
|
|
throw std::runtime_error("Tried to set output data of an input attribute!");
|
|
|
|
|
|
|
|
std::vector<u8> buffer(sizeof(float), 0);
|
|
|
|
std::memcpy(buffer.data(), &floatingPoint, sizeof(float));
|
|
|
|
|
2021-01-31 18:59:47 +01:00
|
|
|
attribute.getOutputData() = buffer;
|
2021-01-31 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setOverlayData(u64 address, const std::vector<u8> &data) {
|
|
|
|
if (this->m_overlay == nullptr)
|
|
|
|
throw std::runtime_error("Tried setting overlay data on a node that's not the end of a chain!");
|
|
|
|
|
|
|
|
this->m_overlay->setAddress(address);
|
|
|
|
this->m_overlay->getData() = data;
|
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|