diff --git a/Source/TextAsset/Private/TextAsset.cpp b/Source/TextAsset/Private/TextAsset.cpp new file mode 100644 index 0000000..27eccd3 --- /dev/null +++ b/Source/TextAsset/Private/TextAsset.cpp @@ -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(this, TEXT("AssetImportData")); + } +#endif + Super::PostInitProperties(); +} diff --git a/Source/TextAsset/Public/TextAsset.h b/Source/TextAsset/Public/TextAsset.h index 0629c47..8bcf385 100644 --- a/Source/TextAsset/Public/TextAsset.h +++ b/Source/TextAsset/Public/TextAsset.h @@ -4,6 +4,7 @@ #include "Internationalization/Text.h" #include "UObject/Object.h" +#include "EditorFramework/AssetImportData.h" #include "UObject/ObjectMacros.h" #include "TextAsset.generated.h" @@ -21,7 +22,25 @@ class TEXTASSET_API UTextAsset public: + + virtual void PostInitProperties() override; + +#if WITH_EDITORONLY_DATA + + virtual void GetAssetRegistryTags(TArray& 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; }; diff --git a/Source/TextAsset/TextAsset.Build.cs b/Source/TextAsset/TextAsset.Build.cs index b88c8c9..a7cd802 100644 --- a/Source/TextAsset/TextAsset.Build.cs +++ b/Source/TextAsset/TextAsset.Build.cs @@ -12,6 +12,7 @@ public TextAsset(ReadOnlyTargetRules Target) : base(Target) new string[] { "Core", "CoreUObject", + "Engine" }); PrivateIncludePaths.AddRange( diff --git a/Source/TextAssetEditor/Private/AssetTools/TextAssetActions.cpp b/Source/TextAssetEditor/Private/AssetTools/TextAssetActions.cpp index 3fbc43b..aec78c9 100644 --- a/Source/TextAssetEditor/Private/AssetTools/TextAssetActions.cpp +++ b/Source/TextAssetEditor/Private/AssetTools/TextAssetActions.cpp @@ -2,6 +2,8 @@ #include "TextAssetActions.h" + +#include "EditorReimportHandler.h" #include "Framework/MultiBox/MultiBoxBuilder.h" #include "TextAsset.h" #include "Styling/SlateStyle.h" @@ -35,6 +37,23 @@ void FTextAssetActions::GetActions(const TArray& InObjects, FMenuBuild auto TextAssets = GetTypedWeakObjectPtrs(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)."), diff --git a/Source/TextAssetEditor/Private/Factories/TextAssetFactory.cpp b/Source/TextAssetEditor/Private/Factories/TextAssetFactory.cpp index fcec339..c503825 100644 --- a/Source/TextAssetEditor/Private/Factories/TextAssetFactory.cpp +++ b/Source/TextAssetEditor/Private/Factories/TextAssetFactory.cpp @@ -48,9 +48,64 @@ UObject* UTextAssetFactory::FactoryCreateFile(UClass* InClass, UObject* InParent { TextAsset = NewObject(InParent, InClass, InName, Flags); TextAsset->Text = FText::FromString(TextString); + TextAsset->AssetImportData->AddFileName(Filename, 0); } bOutOperationCanceled = false; return TextAsset; } + + + +bool UTextAssetFactory::CanReimport(UObject * Obj, TArray& OutFilenames) +{ + UTextAsset* pTextAsset = Cast(Obj); + + if (pTextAsset) + { + pTextAsset->AssetImportData->ExtractFilenames(OutFilenames); + return true; + } + + return false; +} + +void UTextAssetFactory::SetReimportPaths(UObject * Obj, const TArray& NewReimportPaths) +{ + UTextAsset* pTextAsset = Cast(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(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; +} + diff --git a/Source/TextAssetEditor/Private/Factories/TextAssetFactory.h b/Source/TextAssetEditor/Private/Factories/TextAssetFactory.h index 5f8f51f..ece861e 100644 --- a/Source/TextAssetEditor/Private/Factories/TextAssetFactory.h +++ b/Source/TextAssetEditor/Private/Factories/TextAssetFactory.h @@ -2,6 +2,7 @@ #pragma once +#include "EditorReimportHandler.h" #include "Factories/Factory.h" #include "UObject/ObjectMacros.h" @@ -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& OutFilenames) override; + virtual void SetReimportPaths(UObject* Obj, const TArray& NewReimportPaths) override; + virtual EReimportResult::Type Reimport(UObject* Obj) override; };