Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
FileHelper.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/ArrayView.h"
7#include "Containers/ContainersFwd.h"
8#include "Containers/StringFwd.h"
9#include "Containers/StringView.h"
10#include "Containers/UnrealString.h"
11#include "CoreTypes.h"
12#include "HAL/FileManager.h"
13#include "HAL/PlatformCrt.h"
14#include "Math/Color.h"
15#include "Math/MathFwd.h"
16#include "Misc/EnumClassFlags.h"
17#include "Templates/UnrealTemplate.h"
18
19class FArchive;
20class FText;
21class IPlatformFile;
22template <typename FuncType> class TFunctionRef;
23
24/*-----------------------------------------------------------------------------
25 FFileHelper
26-----------------------------------------------------------------------------*/
28{
29 enum class EHashOptions
30 {
31 None =0,
32 /** Enable the async task for verifying the hash for the file being loaded */
33 EnableVerify =1<<0,
34 /** A missing hash entry should trigger an error */
36 };
37
39 {
45 };
46
47 enum class EColorChannel
48 {
49 R,
50 G,
51 B,
52 A,
53 All
54 };
55
56 /**
57 * Load a text file to an FString.
58 * Supports all combination of ANSI/Unicode files and platforms.
59 */
60 static void BufferToString( FString& Result, const uint8* Buffer, int32 Size );
61
62 /**
63 * Load a binary file to a dynamic array with two uninitialized bytes at end as padding.
64 *
65 * @param Result Receives the contents of the file
66 * @param Filename The file to read
67 * @param Flags Flags to pass to IFileManager::CreateFileReader
68 */
69 static bool LoadFileToArray( TArray<uint8>& Result, const TCHAR* Filename, uint32 Flags = 0 );
70
71 /**
72 * Load a binary file to a dynamic array with two uninitialized bytes at end as padding.
73 *
74 * @param Result Receives the contents of the file
75 * @param Filename The file to read
76 * @param Flags Flags to pass to IFileManager::CreateFileReader
77 */
78 static bool LoadFileToArray( TArray64<uint8>& Result, const TCHAR* Filename, uint32 Flags = 0 );
79
80 /**
81 * Load a text file to an FString. Supports all combination of ANSI/Unicode files and platforms.
82 *
83 * @param Result String representation of the loaded file
84 * @param Archive Name of the archive to load from
85 * @param VerifyFlags Flags controlling the hash verification behavior ( see EHashOptions )
86 */
87 static bool LoadFileToString(FString& Result, FArchive& Reader, EHashOptions VerifyFlags = EHashOptions::None);
88
89 /**
90 * Load a text file to an FString. Supports all combination of ANSI/Unicode files and platforms.
91 *
92 * @param Result String representation of the loaded file
93 * @param Filename Name of the file to load
94 * @param VerifyFlags Flags controlling the hash verification behavior ( see EHashOptions )
95 */
96 static bool LoadFileToString( FString& Result, const TCHAR* Filename, EHashOptions VerifyFlags = EHashOptions::None, uint32 ReadFlags = 0 );
97
98 /**
99 * Load a text file to an FString. Supports all combination of ANSI/Unicode files and platforms.
100 *
101 * @param Result String representation of the loaded file
102 * @param PlatformFile PlatformFile interface to use
103 * @param Filename Name of the file to load
104 * @param VerifyFlags Flags controlling the hash verification behavior ( see EHashOptions )
105 */
106 static bool LoadFileToString(FString& Result, IPlatformFile* PlatformFile, const TCHAR* Filename, EHashOptions VerifyFlags = EHashOptions::None, uint32 ReadFlags = 0);
107
108 /**
109 * Load a text file to an array of strings. Supports all combination of ANSI/Unicode files and platforms.
110 *
111 * @param Result String representation of the loaded file
112 * @param Filename Name of the file to load
113 */
114 static bool LoadFileToStringArray( TArray<FString>& Result, const TCHAR* Filename );
115
116 UE_DEPRECATED(4.26, "LoadFileToStringArray no longer supports VerifyFlags. You can use UE::String::ParseLines to split up a string loaded with LoadFileToString")
117 static bool LoadFileToStringArray(TArray<FString>& Result, const TCHAR* Filename, EHashOptions VerifyFlags);
118
119 /**
120 * Load a text file to an array of strings, filtered by a user-defined predicate. Supports all combination of ANSI/Unicode files and platforms.
121 *
122 * @param Result String representation of the loaded file
123 * @param Filename Name of the file to load
124 * @param Predicate Condition for whether or not to add the line to the array
125 */
126 static bool LoadFileToStringArrayWithPredicate(TArray<FString>& Result, const TCHAR* Filename, TFunctionRef<bool(const FString&)> Predicate);
127
128 UE_DEPRECATED(4.26, "LoadFileToStringArrayWithPredicate no longer supports VerifyFlags. You can use UE::String::ParseLines to split up a string loaded with LoadFileToString")
129 static bool LoadFileToStringArrayWithPredicate(TArray<FString>& Result, const TCHAR* Filename, TFunctionRef<bool(const FString&)> Predicate, EHashOptions VerifyFlags);
130
131 /**
132 * Load a text file and invoke a visitor for each line. Supports all combination of ANSI/Unicode files and platforms.
133 *
134 * @param Filename Name of the file to load
135 * @param Visitor Visitor to invoke for each non-empty line in the file
136 */
137 static bool LoadFileToStringWithLineVisitor(const TCHAR* Filename, TFunctionRef<void(FStringView Line)> Visitor);
138
139 /**
140 * Save a binary array to a file.
141 */
142 static bool SaveArrayToFile(TArrayView<const uint8> Array, const TCHAR* Filename, IFileManager* FileManager=&IFileManager::Get(), uint32 WriteFlags = 0);
143
144 /**
145 * Save a binary array to a file.
146 */
147 static bool SaveArrayToFile( const TArray64<uint8>& Array, const TCHAR* Filename, IFileManager* FileManager = &IFileManager::Get(), uint32 WriteFlags = 0 );
148
149 /**
150 * Write the FString to a file.
151 * Supports all combination of ANSI/Unicode files and platforms.
152 */
153 static bool SaveStringToFile( FStringView String, const TCHAR* Filename, EEncodingOptions EncodingOptions = EEncodingOptions::AutoDetect, IFileManager* FileManager = &IFileManager::Get(), uint32 WriteFlags = 0 );
154
155 /**
156 * Write the FString to a file.
157 * Supports all combination of ANSI/Unicode files and platforms.
158 */
159 static bool SaveStringArrayToFile( const TArray<FString>& Lines, const TCHAR* Filename, EEncodingOptions EncodingOptions = EEncodingOptions::AutoDetect, IFileManager* FileManager = &IFileManager::Get(), uint32 WriteFlags = 0 );
160
161 /**
162 * Saves a 24/32Bit BMP file to disk for debug image dump purposes
163 *
164 * for general image saving (to BMP or any other format); use FImageUtils::SaveImage instead
165 * CreateBitmap is mainly for debug dump images
166 *
167 * note this also calls SendDataToPCViaUnrealConsole
168 * and uses GenerateNextBitmapFilename if Pattern does not have ".bmp" on it
169 *
170 * @param Pattern filename with path, must not be 0, if with "bmp" extension (e.g. "out.bmp") the filename stays like this, if without (e.g. "out") automatic index numbers are addended (e.g. "out00002.bmp")
171 * @param DataWidth - Width of the bitmap supplied in Data >0
172 * @param DataHeight - Height of the bitmap supplied in Data >0
173 * @param Data must not be 0
174 * @param SubRectangle optional, specifies a sub-rectangle of the source image to save out. If NULL, the whole bitmap is saved
175 * @param FileManager must not be 0
176 * @param OutFilename optional, if specified filename will be output
177 * @param bInWriteAlpha optional, specifies whether to write out the alpha channel. Will force BMP V4 format.
178 * @param ColorChannel optional, specifies a specific channel to write out (will be written out to all channels gray scale).
179 *
180 * @return true if success
181 */
182 static bool CreateBitmap( const TCHAR* Pattern, int32 DataWidth, int32 DataHeight, const struct FColor* Data, FIntRect* SubRectangle = NULL, IFileManager* FileManager = &IFileManager::Get(), FString* OutFilename = NULL, bool bInWriteAlpha = false, EColorChannel ColorChannel = EColorChannel::All);
183
184 /**
185 * Generates the next unique bitmap filename with a specified extension
186 *
187 * @param Pattern Filename with path, but without extension.
188 * @param Extension File extension to be appended
189 * @param OutFilename Reference to an FString where the newly generated filename will be placed
190 * @param FileManager Reference to a IFileManager (or the global instance by default)
191 *
192 * @return true if success
193 */
194 static bool GenerateNextBitmapFilename(const FString& Pattern, const FString& Extension, FString& OutFilename, IFileManager* FileManager = &IFileManager::Get());
195
196 /**
197 * Generates the next unique bitmap filename with a specified extension
198 *
199 * @param Pattern Filename with path, but without extension.
200 * @param Extension File extension to be appended
201 * @param OutFilename Reference to an FString where the newly generated filename will be placed
202 *
203 * @return true if success
204 */
205 static void GenerateDateTimeBasedBitmapFilename(const FString& Pattern, const FString& Extension, FString& OutFilename);
206
207 /**
208 * Load the given ANSI text file to an array of strings - one FString per line of the file.
209 * Intended for use in simple text parsing actions
210 *
211 * @param InFilename The text file to read, full path
212 * @param InFileManager The filemanager to use - NULL will use &IFileManager::Get()
213 * @param OutStrings The array of FStrings to fill in
214 *
215 * @return bool true if successful, false if not
216 */
217 static bool LoadANSITextFileToStrings(const TCHAR* InFilename, IFileManager* InFileManager, TArray<FString>& OutStrings);
218
219 /**
220 * Checks to see if a filename is valid for saving.
221 * A filename must be under FPlatformMisc::GetMaxPathLength() to be saved
222 *
223 * @param Filename Filename, with or without path information, to check.
224 * @param OutError If an error occurs, this is the reason why
225 */
226 static bool IsFilenameValidForSaving(const FString& Filename, FText& OutError);
227
228 enum class UE_DEPRECATED(5.0, "EChannelMask has been deprecated in favor of EColorChannel") EChannelMask
229 {
230 R = STRUCT_OFFSET(FColor, R),
231 G = STRUCT_OFFSET(FColor, G),
232 B = STRUCT_OFFSET(FColor, B),
233 A = STRUCT_OFFSET(FColor, A),
234 All = R | G | B | A
235 };
236
238 UE_DEPRECATED(5.0, "EChannelMask has been deprecated in favor of EColorChannel, please use the other CreateBitmap() method.")
239 static bool CreateBitmap(const TCHAR* Pattern, int32 DataWidth, int32 DataHeight, const struct FColor* Data, FIntRect* SubRectangle, IFileManager* FileManager, FString* OutFilename, bool bInWriteAlpha, EChannelMask ChannelMask);
241};
242
#define UE_DEPRECATED(Version, Message)
#define ENUM_CLASS_FLAGS(Enum)
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
#define STRUCT_OFFSET(struc, member)
Definition Text.h:357
static IFileManager & Get()
static bool LoadFileToStringWithLineVisitor(const TCHAR *Filename, TFunctionRef< void(FStringView Line)> Visitor)
static bool GenerateNextBitmapFilename(const FString &Pattern, const FString &Extension, FString &OutFilename, IFileManager *FileManager=&IFileManager::Get())
static bool LoadANSITextFileToStrings(const TCHAR *InFilename, IFileManager *InFileManager, TArray< FString > &OutStrings)
static bool SaveStringArrayToFile(const TArray< FString > &Lines, const TCHAR *Filename, EEncodingOptions EncodingOptions=EEncodingOptions::AutoDetect, IFileManager *FileManager=&IFileManager::Get(), uint32 WriteFlags=0)
static bool LoadFileToArray(TArray< uint8 > &Result, const TCHAR *Filename, uint32 Flags=0)
static bool LoadFileToStringArray(TArray< FString > &Result, const TCHAR *Filename)
static bool LoadFileToString(FString &Result, FArchive &Reader, EHashOptions VerifyFlags=EHashOptions::None)
static bool LoadFileToStringArray(TArray< FString > &Result, const TCHAR *Filename, EHashOptions VerifyFlags)
static bool LoadFileToStringArrayWithPredicate(TArray< FString > &Result, const TCHAR *Filename, TFunctionRef< bool(const FString &)> Predicate, EHashOptions VerifyFlags)
static bool CreateBitmap(const TCHAR *Pattern, int32 DataWidth, int32 DataHeight, const struct FColor *Data, FIntRect *SubRectangle=NULL, IFileManager *FileManager=&IFileManager::Get(), FString *OutFilename=NULL, bool bInWriteAlpha=false, EColorChannel ColorChannel=EColorChannel::All)
static void GenerateDateTimeBasedBitmapFilename(const FString &Pattern, const FString &Extension, FString &OutFilename)
static void BufferToString(FString &Result, const uint8 *Buffer, int32 Size)
static bool SaveArrayToFile(TArrayView< const uint8 > Array, const TCHAR *Filename, IFileManager *FileManager=&IFileManager::Get(), uint32 WriteFlags=0)
static bool LoadFileToString(FString &Result, const TCHAR *Filename, EHashOptions VerifyFlags=EHashOptions::None, uint32 ReadFlags=0)
static bool SaveStringToFile(FStringView String, const TCHAR *Filename, EEncodingOptions EncodingOptions=EEncodingOptions::AutoDetect, IFileManager *FileManager=&IFileManager::Get(), uint32 WriteFlags=0)
static bool SaveArrayToFile(const TArray64< uint8 > &Array, const TCHAR *Filename, IFileManager *FileManager=&IFileManager::Get(), uint32 WriteFlags=0)
static bool IsFilenameValidForSaving(const FString &Filename, FText &OutError)
static bool LoadFileToStringArrayWithPredicate(TArray< FString > &Result, const TCHAR *Filename, TFunctionRef< bool(const FString &)> Predicate)
static PRAGMA_DISABLE_DEPRECATION_WARNINGS bool CreateBitmap(const TCHAR *Pattern, int32 DataWidth, int32 DataHeight, const struct FColor *Data, FIntRect *SubRectangle, IFileManager *FileManager, FString *OutFilename, bool bInWriteAlpha, EChannelMask ChannelMask)
static bool LoadFileToString(FString &Result, IPlatformFile *PlatformFile, const TCHAR *Filename, EHashOptions VerifyFlags=EHashOptions::None, uint32 ReadFlags=0)