A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
DlgHelper.cpp
Go to the documentation of this file.
1// Copyright Csaba Molnar, Daniel Butum. All Rights Reserved.
2#include "DlgHelper.h"
3
5#include "HAL/FileManager.h"
6#include "Engine/Blueprint.h"
7#include "Logging/DlgLogger.h"
8#include "DlgSystemSettings.h"
9#include "Engine/BlueprintGeneratedClass.h"
10#include "Misc/Paths.h"
11#include "UObject/UObjectIterator.h"
12#include "Framework/Docking/TabManager.h"
13
14bool FDlgHelper::DeleteFile(const FString& PathName, bool bVerbose)
15{
16 IFileManager& FileManager = IFileManager::Get();
17
18 // Text file does not exist, ignore
19 if (!FileManager.FileExists(*PathName))
20 {
21 if (bVerbose)
22 {
23 FDlgLogger::Get().Debugf(TEXT("File does not exist at path = `%s`. Can't delete."), *PathName);
24 }
25 return false;
26 }
27
28 // Delete the text file
29 if (!FileManager.Delete(*PathName))
30 {
31 if (bVerbose)
32 {
33 FDlgLogger::Get().Errorf(TEXT("Can't delete file at path = `%s`"), *PathName);
34 }
35 return false;
36 }
37
38 if (bVerbose)
39 {
40 FDlgLogger::Get().Infof(TEXT("Deleted file %s"), *PathName);
41 }
42 return true;
43}
44
45bool FDlgHelper::RenameFile(const FString& OldPathName, const FString& NewPathName, bool bOverWrite, bool bVerbose)
46{
47 IFileManager& FileManager = IFileManager::Get();
48
49 // File we want to rename does not exist anymore
50 if (!FileManager.FileExists(*OldPathName))
51 {
52 if (bVerbose)
53 {
54 FDlgLogger::Get().Debugf(TEXT("File before rename at path = `%s` does not exist. Can't Rename."), *OldPathName);
55 }
56 return false;
57 }
58
59 // File at destination already exists, conflict :/
60 if (!bOverWrite && FileManager.FileExists(*NewPathName))
61 {
62 if (bVerbose)
63 {
65 TEXT("File at destination (after rename) at path = `%s` already exists. Current text file at path = `%s` won't be moved/renamed."),
66 *NewPathName, *OldPathName
67 );
68 }
69 return false;
70 }
71
72 // Finally Move/Rename
73 if (!FileManager.Move(/*Dest=*/ *NewPathName, /*Src=*/ *OldPathName, /*bReplace=*/ bOverWrite))
74 {
75 if (bVerbose)
76 {
77 FDlgLogger::Get().Errorf(TEXT("Failure to move/rename file from `%s` to `%s`"), *OldPathName, *NewPathName);
78 }
79 return false;
80 }
81
82 if (bVerbose)
83 {
84 FDlgLogger::Get().Infof(TEXT("Text file moved/renamed from `%s` to `%s`"), *OldPathName, *NewPathName);
85 }
86 return true;
87}
88
89
90TSharedPtr<SDockTab> FDlgHelper::InvokeTab(TSharedPtr<FTabManager> TabManager, const FTabId& TabID)
91{
92 if (!TabManager.IsValid())
93 {
94 return nullptr;
95 }
96
97#if ENGINE_MINOR_VERSION >= 26
98 return TabManager->TryInvokeTab(TabID);
99#else
100 return TabManager->InvokeTab(TabID);
101#endif // ENGINE_MINOR_VERSION >= 26
102}
103
105{
106 Name.RemoveFromEnd(TEXT("_C"));
107
108 // Get rid of the extension from `filename.extension` from the end of the path
109 static constexpr bool bRemovePath = false;
110 Name = FPaths::GetBaseFilename(Name, bRemovePath);
111
112 return Name;
113}
114
115bool FDlgHelper::IsClassIgnored(const UClass* Class)
116{
117 if (!Class)
118 {
119 return true;
120 }
121
122 // Ignore generated types that cannot be spawned
123 const FString Name = Class->GetName();
124 return Name.StartsWith(TEXT("SKEL_")) || Name.StartsWith(TEXT("REINST_"));
125}
126
127bool FDlgHelper::IsABlueprintClass(const UClass* Class)
128{
129 return Cast<UBlueprintGeneratedClass>(Class) != nullptr;
130}
131
133{
134 return Cast<UBlueprint>(Object) != nullptr;
135}
136
137bool FDlgHelper::IsObjectAChildOf(const UObject* Object, const UClass* ParentClass)
138{
139 if (!Object || !ParentClass)
140 {
141 return false;
142 }
143
144 // Blueprint
145 if (const UBlueprint* Blueprint = Cast<UBlueprint>(Object))
146 {
147 if (const UClass* GeneratedClass = Cast<UClass>(Blueprint->GeneratedClass))
148 {
149 return GeneratedClass->IsChildOf(ParentClass);
150 }
151 }
152
153 // A class object, does this ever happen?
154 if (const UClass* ClassObject = Cast<UClass>(Object))
155 {
156 return ClassObject->IsChildOf(ParentClass);
157 }
158
159 // All other object types
160 return Object->GetClass()->IsChildOf(ParentClass);
161}
162
163bool FDlgHelper::IsObjectImplementingInterface(const UObject* Object, const UClass* InterfaceClass)
164{
165 if (!Object || !InterfaceClass)
166 {
167 return false;
168 }
169
170 // Blueprint
171 if (const UBlueprint* Blueprint = Cast<UBlueprint>(Object))
172 {
173 if (const UClass* GeneratedClass = Cast<UClass>(Blueprint->GeneratedClass))
174 {
175 return GeneratedClass->ImplementsInterface(InterfaceClass);
176 }
177 }
178
179 // A class object, does this ever happen?
180 if (const UClass* Class = Cast<UClass>(Object))
181 {
182 return Class->ImplementsInterface(InterfaceClass);
183 }
184
185 // All other object types
186 return Object->GetClass()->ImplementsInterface(InterfaceClass);
187}
188
189bool FDlgHelper::GetAllChildClassesOf(const UClass* ParentClass, TArray<UClass*>& OutNativeClasses, TArray<UClass*>& OutBlueprintClasses)
190{
191 // Iterate over UClass, this might be heavy on performance
192 for (TObjectIterator<UClass> It; It; ++It)
193 {
194 UClass* ChildClass = *It;
195 if (!ChildClass->IsChildOf(ParentClass))
196 {
197 continue;
198 }
199
200 // It is a child of the Parent Class
201 // make sure we don't include our parent class in the array
202 if (ChildClass == ParentClass)
203 {
204 continue;
205 }
206
207 if (IsABlueprintClass(ChildClass))
208 {
209 // Blueprint
210 OutBlueprintClasses.Add(ChildClass);
211 }
212 else
213 {
214 // Native
215 OutNativeClasses.Add(ChildClass);
216 }
217 }
218
219 return OutNativeClasses.Num() > 0 || OutBlueprintClasses.Num() > 0;
220}
221
222bool FDlgHelper::GetAllClassesImplementingInterface(const UClass* InterfaceClass, TArray<UClass*>& OutNativeClasses, TArray<UClass*>& OutBlueprintClasses)
223{
224 // Iterate over UClass, this might be heavy on performance
225 for (TObjectIterator<UClass> It; It; ++It)
226 {
227 UClass* Class = *It;
228 if (!Class->ImplementsInterface(InterfaceClass))
229 {
230 continue;
231 }
232 if (IsClassIgnored(Class))
233 {
234 continue;
235 }
236
237 if (IsABlueprintClass(Class))
238 {
239 // Blueprint
240 OutBlueprintClasses.Add(Class);
241 }
242 else
243 {
244 // Native
245 OutNativeClasses.Add(Class);
246 }
247 }
248
249 return OutNativeClasses.Num() > 0 || OutBlueprintClasses.Num() > 0;
250}
251
252TMap<FName, TArray<FDlgClassAndObject>> FDlgHelper::ConvertDialogueParticipantsClassesIntoMap(const TArray<UClass*>& Classes)
253{
254 TMap<FName, TArray<FDlgClassAndObject>> ObjectsMap;
255
256 for (UClass* Class : Classes)
257 {
258 // How did this even get here
259 if (!Class->ImplementsInterface(UDlgDialogueParticipant::StaticClass()))
260 {
261 continue;
262 }
263
264 // Should be the same for native and blueprint classes
265 UObject* Object = Class->GetDefaultObject();
266
267 // Something is wrong
268 if (!Object)
269 {
270 continue;
271 }
272
273 FDlgClassAndObject Struct;
274 Struct.Class = Class;
275 Struct.Object = Object;
276
277 const FName ParticipantName = IDlgDialogueParticipant::Execute_GetParticipantName(Object);
278 if (ObjectsMap.Contains(ParticipantName))
279 {
280 // Update
281 ObjectsMap[ParticipantName].Add(Struct);
282 }
283 else
284 {
285 // Create
286 TArray<FDlgClassAndObject> Array;
287 Array.Add(Struct);
288 ObjectsMap.Add(ParticipantName, Array);
289 }
290 }
291
292 return ObjectsMap;
293}
static bool IsClassIgnored(const UClass *Class)
static bool DeleteFile(const FString &PathName, bool bVerbose=true)
Definition DlgHelper.cpp:14
static bool IsObjectAChildOf(const UObject *Object, const UClass *ParentClass)
static bool IsABlueprintObject(const UObject *Object)
static bool IsObjectImplementingInterface(const UObject *Object, const UClass *InterfaceClass)
static bool GetAllChildClassesOf(const UClass *ParentClass, TArray< UClass * > &OutNativeClasses, TArray< UClass * > &OutBlueprintClasses)
static FString CleanObjectName(FString Name)
static bool GetAllClassesImplementingInterface(const UClass *InterfaceClass, TArray< UClass * > &OutNativeClasses, TArray< UClass * > &OutBlueprintClasses)
static bool IsABlueprintClass(const UClass *Class)
static bool RenameFile(const FString &OldPathName, const FString &NewPathName, bool bOverWrite=false, bool bVerbose=true)
Definition DlgHelper.cpp:45
static TSharedPtr< SDockTab > InvokeTab(TSharedPtr< FTabManager > TabManager, const FTabId &TabID)
Definition DlgHelper.cpp:90
static TMap< FName, TArray< FDlgClassAndObject > > ConvertDialogueParticipantsClassesIntoMap(const TArray< UClass * > &Classes)
static FDlgLogger & Get()
Definition DlgLogger.h:24
void Infof(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:311
void Errorf(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:305
void Debugf(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:314
UObject * Object
UPROPERTY()
Definition DlgHelper.h:37
UClass * Class
UPROPERTY()
Definition DlgHelper.h:32