-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathTextAssetFactory.cpp
More file actions
112 lines (83 loc) · 3.12 KB
/
TextAssetFactory.cpp
File metadata and controls
112 lines (83 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "TextAssetFactory.h"
#include "Containers/UnrealString.h"
#include "TextAsset.h"
#include "Misc/FileHelper.h"
/* UTextAssetFactory structors
*****************************************************************************/
UTextAssetFactory::UTextAssetFactory( const FObjectInitializer& ObjectInitializer )
: Super(ObjectInitializer)
{
Formats.Add(FString(TEXT("txt;")) + NSLOCTEXT("UTextAssetFactory", "FormatTxt", "Text File").ToString());
SupportedClass = UTextAsset::StaticClass();
bCreateNew = false;
bEditorImport = true;
}
/* UFactory overrides
*****************************************************************************/
/* This is the old API (only for demonstration purposes)
UObject* UTextAssetFactory::FactoryCreateBinary(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, const TCHAR* Type, const uint8*& Buffer, const uint8* BufferEnd, FFeedbackContext* Warn)
{
UTextAsset* TextAsset = nullptr;
FString TextString;
if (FFileHelper::LoadFileToString(TextString, *CurrentFilename))
{
TextAsset = NewObject<UTextAsset>(InParent, Class, Name, Flags);
TextAsset->Text = FText::FromString(TextString);
}
return TextAsset;
}*/
UObject* UTextAssetFactory::FactoryCreateFile(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, const FString& Filename, const TCHAR* Parms, FFeedbackContext* Warn, bool& bOutOperationCanceled)
{
UTextAsset* TextAsset = nullptr;
FString TextString;
if (FFileHelper::LoadFileToString(TextString, *Filename))
{
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);
pTextAsset->Modify();
return EReimportResult::Succeeded;
}
return EReimportResult::Failed;
}