Files
UnrealEngineUWP/Engine/Plugins/ScriptPlugin/Source/ScriptEditorPlugin/Private/ScriptFactory.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

192 lines
5.6 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "ScriptFactory.h"
#include "GameFramework/Actor.h"
#include "Modules/ModuleManager.h"
#include "ClassViewerModule.h"
#include "Kismet2/SClassPickerDialog.h"
#include "Kismet2/KismetEditorUtilities.h"
#include "ScriptEditorPluginLog.h"
#include "Editor.h"
#include "UnrealEdGlobals.h"
#include "EditorFramework/AssetImportData.h"
#include "ReimportScriptFactory.h"
#include "HAL/FileManager.h"
#include "ScriptBlueprintGeneratedClass.h"
#include "UniquePtr.h"
UScriptFactory::UScriptFactory(const FObjectInitializer& ObjectInitializer)
: Super( ObjectInitializer )
{
SupportedClass = UScriptBlueprint::StaticClass();
ParentClass = AActor::StaticClass();
FScriptContextBase::GetSupportedScriptFileFormats(Formats);
bCreateNew = false;
bEditorImport = true;
bText = true;
bEditAfterNew = true;
}
bool UScriptFactory::ConfigureProperties()
{
// Null the parent class so we can check for selection later
ParentClass = NULL;
// Load the class viewer module to display a class picker
FClassViewerModule& ClassViewerModule = FModuleManager::LoadModuleChecked<FClassViewerModule>("ClassViewer");
// Fill in options
FClassViewerInitializationOptions Options;
Options.Mode = EClassViewerMode::ClassPicker;
Options.DisplayMode = EClassViewerDisplayMode::TreeView;
Options.bShowObjectRootClass = true;
Options.bIsBlueprintBaseOnly = true;
Options.bShowUnloadedBlueprints = true;
const FText TitleText = NSLOCTEXT("EditorFactories", "CreateScriptOptions", "Pick Parent Class");
UClass* ChosenClass = NULL;
const bool bPressedOk = SClassPickerDialog::PickClass(TitleText, Options, ChosenClass, UScriptBlueprintGeneratedClass::StaticClass());
if (bPressedOk)
{
ParentClass = ChosenClass;
}
return bPressedOk;
}
bool UScriptFactory::DoesSupportClass(UClass* Class)
{
return Class == UScriptBlueprint::StaticClass();
}
UObject* UScriptFactory::FactoryCreateText(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, UObject* Context, const TCHAR* Type, const TCHAR*& Buffer, const TCHAR* BufferEnd, FFeedbackContext* Warn)
{
GEditor->SelectNone(true, true, false);
UScriptBlueprint* NewBlueprint = nullptr;
TUniquePtr<FScriptContextBase> ScriptContext(FScriptContextBase::CreateContext(Buffer, nullptr, nullptr));
if (ScriptContext)
{
NewBlueprint = Cast<UScriptBlueprint>( FindObject<UBlueprint>( InParent, *InName.ToString() ) );
if( NewBlueprint != nullptr )
{
NewBlueprint->Modify();
}
else
{
NewBlueprint = CastChecked<UScriptBlueprint>(FKismetEditorUtilities::CreateBlueprint(ParentClass, InParent, InName, BPTYPE_Normal, UScriptBlueprint::StaticClass(), UScriptBlueprintGeneratedClass::StaticClass(), "UScriptFactory"));
}
NewBlueprint->SourceCode = Buffer;
NewBlueprint->AssetImportData->Update(CurrentFilename);
// Need to make sure we compile with the new source code
FKismetEditorUtilities::CompileBlueprint(NewBlueprint);
FEditorDelegates::OnAssetPostImport.Broadcast(this, NewBlueprint);
}
else
{
UE_LOG(LogScriptEditorPlugin, Warning, TEXT("Failed to import %s: could not compile script."), *CurrentFilename);
}
return NewBlueprint;
}
/** UReimportScriptFactory */
UReimportScriptFactory::UReimportScriptFactory(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
bool UReimportScriptFactory::ConfigureProperties()
{
return UFactory::ConfigureProperties();
}
bool UReimportScriptFactory::CanReimport(UObject* Obj, TArray<FString>& OutFilenames)
{
UScriptBlueprint* ScriptClass = Cast<UScriptBlueprint>(Obj);
if (ScriptClass)
{
ScriptClass->AssetImportData->ExtractFilenames(OutFilenames);
return true;
}
return false;
}
void UReimportScriptFactory::SetReimportPaths(UObject* Obj, const TArray<FString>& NewReimportPaths)
{
UScriptBlueprint* ScriptClass = Cast<UScriptBlueprint>(Obj);
if (ScriptClass && ensure(NewReimportPaths.Num() == 1))
{
ScriptClass->AssetImportData->UpdateFilenameOnly(NewReimportPaths[0]);
}
}
/**
* Reimports specified texture from its source material, if the meta-data exists
*/
EReimportResult::Type UReimportScriptFactory::Reimport(UObject* Obj)
{
UScriptBlueprint* ScriptClass = Cast<UScriptBlueprint>(Obj);
if (!ScriptClass)
{
return EReimportResult::Failed;
}
TGuardValue<UScriptBlueprint*> OriginalScriptGuardValue(OriginalScript, ScriptClass);
const FString ResolvedSourceFilePath = ScriptClass->AssetImportData->GetFirstFilename();
if (!ResolvedSourceFilePath.Len())
{
return EReimportResult::Failed;
}
UE_LOG(LogScriptEditorPlugin, Log, TEXT("Performing atomic reimport of [%s]"), *ResolvedSourceFilePath);
// Ensure that the file provided by the path exists
if (IFileManager::Get().FileSize(*ResolvedSourceFilePath) == INDEX_NONE)
{
UE_LOG(LogScriptEditorPlugin, Warning, TEXT("Cannot reimport: source file cannot be found."));
return EReimportResult::Failed;
}
bool OutCanceled = false;
if (ImportObject(ScriptClass->GetClass(), ScriptClass->GetOuter(), *ScriptClass->GetName(), RF_Public | RF_Standalone, ResolvedSourceFilePath, nullptr, OutCanceled) != nullptr)
{
UE_LOG(LogScriptEditorPlugin, Log, TEXT("Imported successfully"));
// Try to find the outer package so we can dirty it up
if (ScriptClass->GetOuter())
{
ScriptClass->GetOuter()->MarkPackageDirty();
}
else
{
ScriptClass->MarkPackageDirty();
}
}
else if(OutCanceled)
{
UE_LOG(LogScriptEditorPlugin, Warning, TEXT("-- import canceled"));
}
else
{
UE_LOG(LogScriptEditorPlugin, Warning, TEXT("-- import failed"));
}
return EReimportResult::Succeeded;
}
int32 UReimportScriptFactory::GetPriority() const
{
return ImportPriority;
}