Files
UnrealEngineUWP/Engine/Plugins/ScriptPlugin/Source/ScriptEditorPlugin/Private/ScriptFactory.cpp
Andrew Rodham 93e71b9e6a Unified asset import data across all asset types.
The majority of asset reimport machinery is now held within UAssetImportData and FAssetImportInfo. This change allows us to remove the CachedAssetsBySourceFileName map from FAssetRegistry that was previously added to support auto reimport for 4.7.

The functionality of CachedAssetsBySourceFileName has been wrapped up in FAssetSourceFilenameCache, which sits on top of the asset registry in the editor.

This unification allows us to also consistently store the file timestamps and MD5 hashes of imported files. This subsequently allows auto-reimports to be ignored where a source content file has not actually changed (its MD5 is the same).

This addresses UETOOL-365 - Rework AssetRegistry changes to support auto-reimport

[CL 2567286 by Andrew Rodham in Main branch]
2015-05-27 16:16:21 -04:00

173 lines
5.1 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "ScriptEditorPluginPrivatePCH.h"
#include "ScriptBlueprintGeneratedClass.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;
TAutoPtr<FScriptContextBase> ScriptContext(FScriptContextBase::CreateContext(Buffer, nullptr, nullptr));
if (ScriptContext.IsValid())
{
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;
}
if (UFactory::StaticImportObject(ScriptClass->GetClass(), ScriptClass->GetOuter(), *ScriptClass->GetName(), RF_Public | RF_Standalone, *ResolvedSourceFilePath, NULL, this))
{
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
{
UE_LOG(LogScriptEditorPlugin, Warning, TEXT("-- import failed"));
}
return EReimportResult::Succeeded;
}
int32 UReimportScriptFactory::GetPriority() const
{
return ImportPriority;
}