Files
UnrealEngineUWP/Engine/Source/Editor/NiagaraEditor/Private/NiagaraScriptSource.cpp
Ben Marsh 4ba423868f Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3209340 on 2016/11/23 by Ben.Marsh

	Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.

	Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.

	  * Every header now includes everything it needs to compile.
	        * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
	        * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
	  * Every .cpp file includes its matching .h file first.
	        * This helps validate that each header is including everything it needs to compile.
	  * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
	        * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
	        * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
	  * No engine code explicitly includes a precompiled header any more.
	        * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
	        * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.

	Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.

[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00

242 lines
6.6 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "NiagaraScriptSource.h"
#include "Modules/ModuleManager.h"
#include "NiagaraCommon.h"
#include "NiagaraEditorModule.h"
#include "NiagaraScript.h"
#include "NiagaraComponent.h"
#include "UObject/UObjectHash.h"
#include "UObject/UObjectIterator.h"
#include "ComponentReregisterContext.h"
#include "NiagaraGraph.h"
#include "NiagaraConstants.h"
#include "NiagaraEffect.h"
#include "NiagaraNodeOutput.h"
#include "NiagaraNodeInput.h"
#include "NiagaraNodeWriteDataSet.h"
#include "NiagaraNodeReadDataSet.h"
#include "EdGraphSchema_Niagara.h"
//////////////////////////////////////////////////////////////////////////
// NiagaraGraph
UNiagaraGraph::UNiagaraGraph(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
Schema = UEdGraphSchema_Niagara::StaticClass();
}
void UNiagaraGraph::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
NotifyGraphChanged();
}
class UNiagaraScriptSource* UNiagaraGraph::GetSource() const
{
return CastChecked<UNiagaraScriptSource>(GetOuter());
}
UNiagaraNodeOutput* UNiagaraGraph::FindOutputNode() const
{
for (UEdGraphNode* Node : Nodes)
{
if (UNiagaraNodeOutput* OutNode = Cast<UNiagaraNodeOutput>(Node))
{
return OutNode;
}
}
check(0);
return NULL;
}
void UNiagaraGraph::FindInputNodes(TArray<class UNiagaraNodeInput*>& OutInputNodes) const
{
for (UEdGraphNode* Node : Nodes)
{
if (UNiagaraNodeInput* InNode = Cast<UNiagaraNodeInput>(Node))
{
OutInputNodes.Add(InNode);
}
}
}
void UNiagaraGraph::FindReadDataSetNodes(TArray<class UNiagaraNodeReadDataSet*>& OutReadNodes) const
{
for (UEdGraphNode* Node : Nodes)
{
if (UNiagaraNodeReadDataSet* InNode = Cast<UNiagaraNodeReadDataSet>(Node))
{
OutReadNodes.Add(InNode);
}
}
}
void UNiagaraGraph::FindWriteDataSetNodes(TArray<class UNiagaraNodeWriteDataSet*>& OutWriteNodes) const
{
for (UEdGraphNode* Node : Nodes)
{
if (UNiagaraNodeWriteDataSet* InNode = Cast<UNiagaraNodeWriteDataSet>(Node))
{
OutWriteNodes.Add(InNode);
}
}
}
int32 UNiagaraGraph::GetAttributeIndex(const FNiagaraVariableInfo& Attr)const
{
const UNiagaraNodeOutput* OutNode = FindOutputNode();
check(OutNode);
for (int32 i = 0; i < OutNode->Outputs.Num(); ++i)
{
if (OutNode->Outputs[i] == Attr)
{
return i;
}
}
return INDEX_NONE;
}
void UNiagaraGraph::GetAttributes(TArray< FNiagaraVariableInfo >& OutAttributes)const
{
const UNiagaraNodeOutput* OutNode = FindOutputNode();
check(OutNode);
for (const FNiagaraVariableInfo& Attr : OutNode->Outputs)
{
check(!OutAttributes.Find(Attr));
OutAttributes.Add(Attr);
}
}
//////////////////////////////////////////////////////////////////////////
// UNiagraScriptSource
UNiagaraScriptSource::UNiagaraScriptSource(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void UNiagaraScriptSource::PostLoad()
{
Super::PostLoad();
#if WITH_EDITOR
UNiagaraScript* ScriptOwner = Cast<UNiagaraScript>(GetOuter());
if (ScriptOwner && ScriptOwner->ByteCode.Num() == 0)
{
ScriptOwner->ConditionalPostLoad();
//FNiagaraEditorModule& NiagaraEditorModule = FModuleManager::Get().LoadModuleChecked<FNiagaraEditorModule>(TEXT("NiagaraEditor"));
//NiagaraEditorModule.CompileScript(ScriptOwner);
}
Compile();
#endif
}
struct FNiagaraComponentReregisterContext : FComponentReregisterContext
{
FNiagaraEmitterScriptProperties* ScriptProps;
UNiagaraEmitterProperties* EmitterProps;
FNiagaraComponentReregisterContext(UNiagaraComponent* Comp, FNiagaraEmitterScriptProperties* InScriptProps, UNiagaraEmitterProperties* InEmitterProps)
: FComponentReregisterContext(Comp)
, ScriptProps(InScriptProps)
, EmitterProps(InEmitterProps)
{
}
~FNiagaraComponentReregisterContext()
{
ScriptProps->Init(EmitterProps);
}
};
class FNiagaraScriptCompileContext
{
public:
/** Initialization constructor. */
FNiagaraScriptCompileContext(UNiagaraScript* Script)
{
// wait until resources are released
FlushRenderingCommands();
// Reregister all components usimg Script.
for (TObjectIterator<UNiagaraComponent> ComponentIt; ComponentIt; ++ComponentIt)
{
UNiagaraComponent* Comp = *ComponentIt;
TSharedPtr<FNiagaraEffectInstance> Inst = Comp->GetEffectInstance();
if (Inst.IsValid())
{
TArray<TSharedPtr<FNiagaraSimulation>>& Emitters = Inst->GetEmitters();
for (TSharedPtr<FNiagaraSimulation> Sim : Emitters)
{
if (Sim.IsValid())
{
if (UNiagaraEmitterProperties* Props = Sim->GetProperties().Get())
{
if (Props->UpdateScriptProps.Script == Script)
{
new(ComponentContexts)FNiagaraComponentReregisterContext(Comp, &Props->UpdateScriptProps, Props);
}
else if (Props->SpawnScriptProps.Script == Script)
{
new(ComponentContexts)FNiagaraComponentReregisterContext(Comp, &Props->SpawnScriptProps, Props);
}
}
}
}
}
}
}
private:
/** The recreate contexts for the individual components. */
TIndirectArray<FNiagaraComponentReregisterContext> ComponentContexts;
};
void UNiagaraScriptSource::Compile()
{
UNiagaraScript* ScriptOwner = Cast<UNiagaraScript>(GetOuter());
FNiagaraScriptCompileContext ScriptCompileContext(ScriptOwner);
FNiagaraEditorModule& NiagaraEditorModule = FModuleManager::Get().LoadModuleChecked<FNiagaraEditorModule>(TEXT("NiagaraEditor"));
NiagaraEditorModule.CompileScript(ScriptOwner);
FNiagaraConstants& ExternalConsts = ScriptOwner->ConstantData.GetExternalConstants();
//Build the constant list.
//This is mainly just jumping through some hoops for the custom UI. Should be removed and have the UI just read directly from the constants stored in the UScript.
const UEdGraphSchema_Niagara* Schema = CastChecked<UEdGraphSchema_Niagara>(NodeGraph->GetSchema());
ExposedVectorConstants.Empty();
for (int32 ConstIdx = 0; ConstIdx < ExternalConsts.GetNumVectorConstants(); ConstIdx++)
{
FNiagaraVariableInfo Info;
FVector4 Value;
ExternalConsts.GetVectorConstant(ConstIdx, Value, Info);
if (Schema->IsSystemConstant(Info))
{
continue;//System constants are "external" but should not be exposed to the editor.
}
EditorExposedVectorConstant *Const = new EditorExposedVectorConstant();
Const->ConstName = Info.Name;
Const->Value = Value;
ExposedVectorConstants.Add(MakeShareable(Const));
}
}
void UNiagaraScriptSource::GetEmitterAttributes(TArray<FName>& VectorInputs, TArray<FName>& MatrixInputs)
{
for (uint32 i=0; i < NiagaraConstants::NumBuiltinConstants; i++)
{
VectorInputs.Add(NiagaraConstants::GConstantNames[i]);
}
MatrixInputs.Empty();
MatrixInputs.Add(FName(TEXT("Emitter Transform")));
}