Skip to content

Commit

Permalink
re-import support (from both actions and toolkit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielmelody committed Nov 24, 2020
1 parent bb17414 commit 3164df9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 9 deletions.
14 changes: 14 additions & 0 deletions Source/TextAsset/Private/TextAsset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "TextAsset.h"
#include "EditorFramework/AssetImportData.h"
// #include "UObjectBaseUtility.h"

void UTextAsset::PostInitProperties()
{
#if WITH_EDITORONLY_DATA
if (!HasAnyFlags(RF_ClassDefaultObject))
{
AssetImportData = NewObject<UAssetImportData>(this, TEXT("AssetImportData"));
}
#endif
Super::PostInitProperties();
}
23 changes: 21 additions & 2 deletions Source/TextAsset/Public/TextAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Internationalization/Text.h"
#include "UObject/Object.h"
#include "EditorFramework/AssetImportData.h"
#include "UObject/ObjectMacros.h"

#include "TextAsset.generated.h"
Expand All @@ -21,7 +22,25 @@ class TEXTASSET_API UTextAsset

public:


virtual void PostInitProperties() override;

#if WITH_EDITORONLY_DATA

virtual void GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const override
{
if (AssetImportData)
{
OutTags.Add(FAssetRegistryTag(SourceFileTagName(), AssetImportData->GetSourceData().ToJson(), FAssetRegistryTag::TT_Hidden));
}

Super::GetAssetRegistryTags(OutTags);
}

UPROPERTY(VisibleAnywhere, Instanced, Category = ImportSettings)
UAssetImportData* AssetImportData;
#endif
/** Holds the stored text. */
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category="TextAsset")
FText Text;
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category="TextAsset")
FText Text;
};
1 change: 1 addition & 0 deletions Source/TextAsset/TextAsset.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public TextAsset(ReadOnlyTargetRules Target) : base(Target)
new string[] {
"Core",
"CoreUObject",
"Engine"
});

PrivateIncludePaths.AddRange(
Expand Down
19 changes: 19 additions & 0 deletions Source/TextAssetEditor/Private/AssetTools/TextAssetActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "TextAssetActions.h"


#include "EditorReimportHandler.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "TextAsset.h"
#include "Styling/SlateStyle.h"
Expand Down Expand Up @@ -35,6 +37,23 @@ void FTextAssetActions::GetActions(const TArray<UObject*>& InObjects, FMenuBuild

auto TextAssets = GetTypedWeakObjectPtrs<UTextAsset>(InObjects);

MenuBuilder.AddMenuEntry(
LOCTEXT("TextAsset_ReImportText", "Re-Import Text"),
LOCTEXT("TextAsset_ReImportTextToolTip", "Re Import text from disk."),
FSlateIcon(),
FUIAction(
FExecuteAction::CreateLambda([=]
{
for (auto &TextAsset : TextAssets) {
FReimportManager::Instance()->Reimport(TextAsset.Get(), true);
}
}),
FCanExecuteAction::CreateLambda([=]
{
return true;
}
)));

MenuBuilder.AddMenuEntry(
LOCTEXT("TextAsset_ReverseText", "Reverse Text"),
LOCTEXT("TextAsset_ReverseTextToolTip", "Reverse the text stored in the selected text asset(s)."),
Expand Down
55 changes: 55 additions & 0 deletions Source/TextAssetEditor/Private/Factories/TextAssetFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,64 @@ UObject* UTextAssetFactory::FactoryCreateFile(UClass* InClass, UObject* InParent
{
TextAsset = NewObject<UTextAsset>(InParent, InClass, InName, Flags);
TextAsset->Text = FText::FromString(TextString);
TextAsset->AssetImportData->AddFileName(Filename, 0);
}

bOutOperationCanceled = false;

return TextAsset;
}



bool UTextAssetFactory::CanReimport(UObject * Obj, TArray<FString>& OutFilenames)
{
UTextAsset* pTextAsset = Cast<UTextAsset>(Obj);

if (pTextAsset)
{
pTextAsset->AssetImportData->ExtractFilenames(OutFilenames);
return true;
}

return false;
}

void UTextAssetFactory::SetReimportPaths(UObject * Obj, const TArray<FString>& NewReimportPaths)
{
UTextAsset* pTextAsset = Cast<UTextAsset>(Obj);

//TextAsset can only own one pertaining file.
if (pTextAsset && ensure(NewReimportPaths.Num() == 1))
{
pTextAsset->AssetImportData->UpdateFilenameOnly(NewReimportPaths[0]);
}
}

EReimportResult::Type UTextAssetFactory::Reimport(UObject * Obj)
{
//Actually do reimport;
if (!Obj || !Obj->IsA(UTextAsset::StaticClass()))
{
return EReimportResult::Failed;
}

UTextAsset* pTextAsset = Cast<UTextAsset>(Obj);
const FString ResolvedSourceFilePath = pTextAsset->AssetImportData->GetFirstFilename();

if (!ResolvedSourceFilePath.Len())
{
return EReimportResult::Failed;
}

FString NewText;

if (FFileHelper::LoadFileToString(NewText, *ResolvedSourceFilePath))
{
pTextAsset->Text = FText::FromString(NewText);
return EReimportResult::Succeeded;
}

return EReimportResult::Failed;
}

17 changes: 10 additions & 7 deletions Source/TextAssetEditor/Private/Factories/TextAssetFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "EditorReimportHandler.h"
#include "Factories/Factory.h"
#include "UObject/ObjectMacros.h"

Expand All @@ -13,14 +14,16 @@
*/
UCLASS(hidecategories=Object)
class UTextAssetFactory
: public UFactory
{
GENERATED_UCLASS_BODY()
: public UFactory, public FReimportHandler {
GENERATED_UCLASS_BODY()

public:
public:

//~ UFactory Interface
//~ UFactory Interface

// virtual UObject* FactoryCreateBinary(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, const TCHAR* Type, const uint8*& Buffer, const uint8* BufferEnd, FFeedbackContext* Warn) override;
virtual UObject* FactoryCreateFile(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, const FString& Filename, const TCHAR* Parms, FFeedbackContext* Warn, bool& bOutOperationCanceled) override;
// virtual UObject* FactoryCreateBinary(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, const TCHAR* Type, const uint8*& Buffer, const uint8* BufferEnd, FFeedbackContext* Warn) override;
virtual UObject* FactoryCreateFile(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, const FString& Filename, const TCHAR* Parms, FFeedbackContext* Warn, bool& bOutOperationCanceled) override;
virtual bool CanReimport(UObject* Obj, TArray<FString>& OutFilenames) override;
virtual void SetReimportPaths(UObject* Obj, const TArray<FString>& NewReimportPaths) override;
virtual EReimportResult::Type Reimport(UObject* Obj) override;
};

0 comments on commit 3164df9

Please sign in to comment.