Files
FLESH/Source/FLESHEditor/Private/FLESHGraph/FLESHCompiler.cpp
2025-04-23 01:18:06 +08:00

151 lines
3.6 KiB
C++

#include "FLESHGraph/FLESHCompiler.h"
#include "FLESHGraph/FLESHGraph.h"
#include "FLESHGraph/FLESHGraphNode.h"
#include "Logging/LogMacros.h"
// Define log category
DEFINE_LOG_CATEGORY_STATIC(LogFLESHCompiler, Log, All);
UFLESHCompiler::UFLESHCompiler()
{
// Initialize default values
SourceGraph = nullptr;
bCompilationSuccessful = false;
ErrorMessage = TEXT("");
}
void UFLESHCompiler::Initialize(UFLESHGraph* InGraph)
{
// Set source graph
SourceGraph = InGraph;
// Reset compilation status
bCompilationSuccessful = false;
ErrorMessage = TEXT("");
CompiledNodeData.Empty();
ExecutionOrder.Empty();
UE_LOG(LogFLESHCompiler, Log, TEXT("FLESH Compiler initialized with graph: %s"),
SourceGraph ? *SourceGraph->GetName() : TEXT("Invalid"));
}
bool UFLESHCompiler::Compile()
{
// Check if source graph is valid
if (!SourceGraph)
{
ErrorMessage = TEXT("Invalid source graph");
UE_LOG(LogFLESHCompiler, Error, TEXT("FLESH Compiler error: %s"), *ErrorMessage);
return false;
}
// Reset compilation data
CompiledNodeData.Empty();
ExecutionOrder.Empty();
// Validate graph
if (!ValidateGraph())
{
UE_LOG(LogFLESHCompiler, Error, TEXT("FLESH Compiler error: %s"), *ErrorMessage);
return false;
}
// Process all nodes in the graph
TArray<UFLESHGraphNode*> AllNodes;
// TODO: Get all nodes from graph
// Process each node
for (int32 NodeIndex = 0; NodeIndex < AllNodes.Num(); NodeIndex++)
{
if (!ProcessNode(AllNodes[NodeIndex], NodeIndex))
{
UE_LOG(LogFLESHCompiler, Error, TEXT("FLESH Compiler error: %s"), *ErrorMessage);
return false;
}
}
// Sort nodes in execution order
SortNodes();
// Set compilation status
bCompilationSuccessful = true;
UE_LOG(LogFLESHCompiler, Log, TEXT("FLESH Graph compiled successfully. Nodes: %d, Execution order: %d"),
CompiledNodeData.Num(), ExecutionOrder.Num());
return true;
}
TArray<FFLESHNodeData> UFLESHCompiler::GetCompiledNodeData() const
{
return CompiledNodeData;
}
TArray<int32> UFLESHCompiler::GetExecutionOrder() const
{
return ExecutionOrder;
}
bool UFLESHCompiler::IsCompilationSuccessful() const
{
return bCompilationSuccessful;
}
FString UFLESHCompiler::GetErrorMessage() const
{
return ErrorMessage;
}
bool UFLESHCompiler::ProcessNode(UFLESHGraphNode* Node, int32 NodeIndex)
{
// Check if node is valid
if (!Node)
{
ErrorMessage = FString::Printf(TEXT("Invalid node at index %d"), NodeIndex);
return false;
}
// Create node data
FFLESHNodeData NodeData;
NodeData.NodeName = FName(*Node->GetNodeTitle());
// TODO: Set node type and parameters
// Add node data to compiled data
CompiledNodeData.Add(NodeData);
return true;
}
void UFLESHCompiler::SortNodes()
{
// TODO: Implement topological sort to determine execution order
// For now, just add all nodes in order
for (int32 i = 0; i < CompiledNodeData.Num(); i++)
{
ExecutionOrder.Add(i);
}
}
bool UFLESHCompiler::ValidateGraph()
{
// TODO: Implement graph validation
// Check if source graph is valid
if (!SourceGraph)
{
ErrorMessage = TEXT("Source graph is null");
return false;
}
// Check if graph has at least one node
TArray<UFLESHGraphNode*> AllNodes = SourceGraph->GetAllNodes();
if (AllNodes.Num() == 0)
{
ErrorMessage = TEXT("Graph has no nodes");
return false;
}
return true;
}