Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
FileManagerGeneric.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/ContainersFwd.h"
7#include "Containers/UnrealString.h"
8#include "CoreTypes.h"
9#include "GenericPlatform/GenericPlatformFile.h"
10#include "HAL/FileManager.h"
11#include "HAL/PlatformFileManager.h"
12#include "Misc/DateTime.h"
13#include "Serialization/Archive.h"
14#include "Templates/UniquePtr.h"
15
16#ifndef PLATFORM_FILE_READER_BUFFER_SIZE
17 #define PLATFORM_FILE_READER_BUFFER_SIZE 1024
18#endif
19
20#ifndef PLATFORM_FILE_WRITER_BUFFER_SIZE
21 #define PLATFORM_FILE_WRITER_BUFFER_SIZE 4096
22#endif
23
24#ifndef PLATFORM_DEBUG_FILE_WRITER_BUFFER_SIZE
25 #define PLATFORM_DEBUG_FILE_WRITER_BUFFER_SIZE 4096
26#endif
27
28/**
29 * Base class for file managers.
30 *
31 * This base class simplifies IFileManager implementations by providing
32 * simple, unoptimized implementations of functions whose implementations
33 * can be derived from other functions.
34 */
36 : public IFileManager
37{
38 // instead of caching the LowLevel, we call the singleton each time to never be incorrect
40 {
42 }
43
44public:
45
46 /**
47 * Default constructor.
48 */
50
51 /**
52 * Virtual destructor.
53 */
54 virtual ~FFileManagerGeneric( ) { }
55
56public:
57
58 // IFileManager interface
59
60 virtual void ProcessCommandLineOptions() override;
61
62 virtual void SetSandboxEnabled(bool bInEnabled) override
63 {
65 }
66
67 virtual bool IsSandboxEnabled() const override
68 {
70 }
71
72 FArchive* CreateFileReader( const TCHAR* Filename, uint32 ReadFlags=0 ) override
73 {
75 }
76
77 FArchive* CreateFileWriter( const TCHAR* Filename, uint32 WriteFlags=0 ) override
78 {
80 }
81
84 {
86 }
87#endif
88
89 bool Delete( const TCHAR* Filename, bool RequireExists=0, bool EvenReadOnly=0, bool Quiet=0 ) override;
90 bool IsReadOnly( const TCHAR* Filename ) override;
91 bool Move( const TCHAR* Dest, const TCHAR* Src, bool Replace=1, bool EvenIfReadOnly=0, bool Attributes=0, bool bDoNotRetryOrError=0 ) override;
92 bool FileExists( const TCHAR* Filename ) override;
93 bool DirectoryExists(const TCHAR* InDirectory) override;
94 void FindFiles( TArray<FString>& Result, const TCHAR* Filename, bool Files, bool Directories ) override;
95 void FindFilesRecursive( TArray<FString>& FileNames, const TCHAR* StartDirectory, const TCHAR* Filename, bool Files, bool Directories, bool bClearFileNames=true) override;
96 double GetFileAgeSeconds( const TCHAR* Filename ) override;
97 FDateTime GetTimeStamp( const TCHAR* Filename ) override;
98 FDateTime GetAccessTimeStamp( const TCHAR* Filename ) override;
99 void GetTimeStampPair(const TCHAR* PathA, const TCHAR* PathB, FDateTime& OutTimeStampA, FDateTime& OutTimeStampB);
100 bool SetTimeStamp( const TCHAR* Filename, FDateTime Timestamp ) override;
101 virtual FString GetFilenameOnDisk(const TCHAR* Filename) override;
102
103 virtual uint32 Copy( const TCHAR* Dest, const TCHAR* Src, bool Replace = 1, bool EvenIfReadOnly = 0, bool Attributes = 0, FCopyProgress* Progress = nullptr, EFileRead ReadFlags = FILEREAD_None, EFileWrite WriteFlags = FILEWRITE_None ) override;
104 virtual bool MakeDirectory( const TCHAR* Path, bool Tree=0 ) override;
105 virtual bool DeleteDirectory( const TCHAR* Path, bool RequireExists=0, bool Tree=0 ) override;
106
107 virtual FFileStatData GetStatData(const TCHAR* FilenameOrDirectory) override;
108
109 /**
110 * Finds all the files within the given directory, with optional file extension filter.
111 *
112 * @param Directory, the absolute path to the directory to search. Ex: "C:\UE4\Pictures"
113 *
114 * @param FileExtension, If FileExtension is NULL, or an empty string "" then all files are found.
115 * Otherwise FileExtension can be of the form .EXT or just EXT and only files with that extension will be returned.
116 *
117 * @return FoundFiles, All the files that matched the optional FileExtension filter, or all files if none was specified.
118 */
119 virtual void FindFiles(TArray<FString>& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension = nullptr) override;
120
121 /**
122 * Call the Visit function of the visitor once for each file or directory in a single directory. This function does not explore subdirectories.
123 * @param Directory The directory to iterate the contents of.
124 * @param Visitor Visitor to call for each element of the directory
125 * @return false if the directory did not exist or if the visitor returned false.
126 **/
127 bool IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor) override;
128 bool IterateDirectory(const TCHAR* Directory, IPlatformFile::FDirectoryVisitorFunc Visitor) override;
129
130 /**
131 * Call the Visit function of the visitor once for each file or directory in a directory tree. This function explores subdirectories.
132 * @param Directory The directory to iterate the contents of, recursively.
133 * @param Visitor Visitor to call for each element of the directory and each element of all subdirectories.
134 * @return false if the directory did not exist or if the visitor returned false.
135 **/
136 bool IterateDirectoryRecursively(const TCHAR* Directory, IPlatformFile::FDirectoryVisitor& Visitor) override;
137 bool IterateDirectoryRecursively(const TCHAR* Directory, IPlatformFile::FDirectoryVisitorFunc Visitor) override;
138
139 /**
140 * Call the Visit function of the visitor once for each file or directory in a single directory. This function does not explore subdirectories.
141 * @param Directory The directory to iterate the contents of.
142 * @param Visitor Visitor to call for each element of the directory
143 * @return false if the directory did not exist or if the visitor returned false.
144 **/
145 bool IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor) override;
146 bool IterateDirectoryStat(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitorFunc Visitor) override;
147
148 /**
149 * Call the Visit function of the visitor once for each file or directory in a directory tree. This function explores subdirectories.
150 * @param Directory The directory to iterate the contents of, recursively.
151 * @param Visitor Visitor to call for each element of the directory and each element of all subdirectories.
152 * @return false if the directory did not exist or if the visitor returned false.
153 **/
154 bool IterateDirectoryStatRecursively(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitor& Visitor) override;
155 bool IterateDirectoryStatRecursively(const TCHAR* Directory, IPlatformFile::FDirectoryStatVisitorFunc Visitor) override;
156
157 /**
158 * Converts passed in filename to use a relative path.
159 *
160 * @param Filename filename to convert to use a relative path
161 * @return filename using relative path
162 */
163 static FString DefaultConvertToRelativePath( const TCHAR* Filename );
164
165 /**
166 * Converts passed in filename to use a relative path.
167 *
168 * @param Filename filename to convert to use a relative path
169 * @return filename using relative path
170 */
171 FString ConvertToRelativePath( const TCHAR* Filename ) override;
172
173 /**
174 * Converts passed in filename to use an absolute path (for reading)
175 *
176 * @param Filename filename to convert to use an absolute path, safe to pass in already using absolute path
177 * @return filename using absolute path
178 */
179 FString ConvertToAbsolutePathForExternalAppForRead( const TCHAR* Filename ) override;
180
181 /**
182 * Converts passed in filename to use an absolute path (for writing)
183 *
184 * @param Filename filename to convert to use an absolute path, safe to pass in already using absolute path
185 * @return filename using absolute path
186 */
187 FString ConvertToAbsolutePathForExternalAppForWrite( const TCHAR* Filename ) override;
188
189 /**
190 * Returns the size of a file. (Thread-safe)
191 *
192 * @param Filename Platform-independent Unreal filename.
193 * @return File size in bytes or INDEX_NONE if the file didn't exist.
194 **/
195 int64 FileSize( const TCHAR* Filename ) override;
196
197 /**
198 * Sends a message to the file server, and will block until it's complete. Will return
199 * immediately if the file manager doesn't support talking to a server.
200 *
201 * @param Message The string message to send to the server
202 * @return true if the message was sent to server and it returned success, or false if there is no server, or the command failed
203 */
204 virtual bool SendMessageToServer(const TCHAR* Message, IPlatformFile::IFileServerMessageHandler* Handler) override
205 {
206 return GetLowLevel().SendMessageToServer(Message, Handler);
207 }
208
209private:
210 FArchive * CreateFileReaderInternal(const TCHAR* Filename, uint32 ReadFlags, uint32 BufferSize);
211 FArchive* CreateFileWriterInternal(const TCHAR* Filename, uint32 WriteFlags, uint32 BufferSize);
212
213 /**
214 * Helper called from Copy if Progress is available
215 */
216 uint32 CopyWithProgress(const TCHAR* InDestFile, const TCHAR* InSrcFile, bool ReplaceExisting, bool EvenIfReadOnly, bool Attributes, FCopyProgress* Progress, EFileRead ReadFlags, EFileWrite WriteFlags);
217
218 void FindFilesRecursiveInternal( TArray<FString>& FileNames, const TCHAR* StartDirectory, const TCHAR* Filename, bool Files, bool Directories);
219};
220
221
222/*-----------------------------------------------------------------------------
223 FArchiveFileReaderGeneric
224-----------------------------------------------------------------------------*/
225
227{
228public:
229 FArchiveFileReaderGeneric( IFileHandle* InHandle, const TCHAR* InFilename, int64 InSize, uint32 InBufferSize = PLATFORM_FILE_READER_BUFFER_SIZE, uint32 InFlags = FILEREAD_None );
231
232 virtual void Seek( int64 InPos ) override final;
233 virtual int64 Tell() override final
234 {
235 return Pos;
236 }
237 virtual int64 TotalSize() override final
238 {
239 return Size;
240 }
241 virtual bool Close() override final;
242 virtual void Serialize( void* V, int64 Length ) override final;
243 virtual FString GetArchiveName() const override
244 {
245 return Filename;
246 }
247 virtual void FlushCache() override final;
248
249 virtual bool Precache(int64 PrecacheOffset, int64 PrecacheSize) override;
250
251protected:
252 bool InternalPrecache( int64 PrecacheOffset, int64 PrecacheSize );
253 /**
254 * Platform specific seek
255 * @param InPos - Offset from beginning of file to seek to
256 * @return false on failure
257 **/
258 virtual bool SeekLowLevel(int64 InPos);
259 /** Close the file handle **/
260 virtual void CloseLowLevel();
261 /**
262 * Platform specific read
263 * @param Dest - Buffer to fill in
264 * @param CountToRead - Number of bytes to read
265 * @param OutBytesRead - Bytes actually read
266 **/
267 virtual void ReadLowLevel(uint8* Dest, int64 CountToRead, int64& OutBytesRead);
268
269 /** Returns true if the archive should suppress logging in case of error */
270 bool IsSilent() const
271 {
272 return !!(Flags & FILEREAD_Silent);
273 }
274
275 /** Filename for debugging purposes. */
277 int64 Size;
278 int64 Pos;
281 /**
282 * The contract for the BufferWindow and the low level pos is that if we have a BufferWindow and Pos is within it, then the LowLevel Pos is at the end of the BufferWindow
283 * If we do not have a BufferWindow, or Pos is outside of it, then LowLevel Pos is at Pos.
284 */
287 uint32 Flags;
289
290 enum
291 {
292 bPrecacheAsSoonAsPossible = 0 // Setting this to true makes it more likely bytes will be available without waiting due to external Precache calls, but at the cost of a larger number of read requests.
293 };
294
295 friend class FArchiveFileReaderGenericTest;
296};
297
298
299/*-----------------------------------------------------------------------------
300 FArchiveFileWriterGeneric
301-----------------------------------------------------------------------------*/
302
304{
305public:
306 FArchiveFileWriterGeneric( IFileHandle* InHandle, const TCHAR* InFilename, int64 InPos, uint32 InBufferSize = PLATFORM_FILE_WRITER_BUFFER_SIZE, uint32 InFlags = FILEWRITE_None);
308
309 virtual void Seek( int64 InPos ) override final;
310 virtual int64 Tell() override final
311 {
312 return Pos;
313 }
314 virtual int64 TotalSize() override;
315 virtual bool Close() override final;
316 virtual void Serialize( void* V, int64 Length ) override final;
317 virtual void Flush() override final;
318 virtual FString GetArchiveName() const override
319 {
320 return Filename;
321 }
322
323protected:
324 /**
325 * Write any internal buffer to the file handle
326 * @note Doesn't flush the handle itself, so this data may be cached by the OS and not yet written to disk!
327 * @return true if there was buffer data and it was written successfully, false if there was nothing to flush or the write failed
328 */
330 /**
331 * Platform specific seek
332 * @param InPos - Offset from beginning of file to seek to
333 * @return false on failure
334 **/
335 virtual bool SeekLowLevel(int64 InPos);
336 /**
337 * Close the file handle
338 * @return false on failure
339 **/
340 virtual bool CloseLowLevel();
341 /**
342 * Platform specific write
343 * @param Src - Buffer to write out
344 * @param CountToWrite - Number of bytes to write
345 * @return false on failure
346 **/
347 virtual bool WriteLowLevel(const uint8* Src, int64 CountToWrite);
348
349 /**
350 * Logs I/O error
351 * It is important to not call any platform API functions after the error occurred and before
352 * calling this functions as the system error code may get reset and will not be properly
353 * logged in this message.
354 * @param Message Brief description of what went wrong
355 */
356 void LogWriteError(const TCHAR* Message);
357
358 /** Returns true if the archive should suppress logging in case of error */
359 bool IsSilent() const
360 {
361 return !!(Flags & FILEWRITE_Silent);
362 }
363
364 /** Filename for debugging purposes */
366 uint32 Flags;
367 int64 Pos;
372};
#define ALLOW_DEBUG_FILES
Definition Build.h:320
EFileWrite
Definition Enums.h:6335
EFileRead
Definition Enums.h:6327
@ FILEWRITE_None
Definition FileManager.h:16
@ FILEWRITE_Silent
Definition FileManager.h:22
@ FILEREAD_None
Definition FileManager.h:28
@ FILEREAD_Silent
Definition FileManager.h:30
#define PLATFORM_FILE_WRITER_BUFFER_SIZE
#define PLATFORM_FILE_READER_BUFFER_SIZE
#define FORCEINLINE
Definition Platform.h:644
virtual bool SeekLowLevel(int64 InPos)
virtual void FlushCache() override final
virtual void ReadLowLevel(uint8 *Dest, int64 CountToRead, int64 &OutBytesRead)
TUniquePtr< IFileHandle > Handle
FArchiveFileReaderGeneric(IFileHandle *InHandle, const TCHAR *InFilename, int64 InSize, uint32 InBufferSize=PLATFORM_FILE_READER_BUFFER_SIZE, uint32 InFlags=FILEREAD_None)
bool InternalPrecache(int64 PrecacheOffset, int64 PrecacheSize)
virtual FString GetArchiveName() const override
virtual bool Precache(int64 PrecacheOffset, int64 PrecacheSize) override
virtual void CloseLowLevel()
virtual void Seek(int64 InPos) override final
virtual int64 Tell() override final
virtual void Serialize(void *V, int64 Length) override final
virtual bool Close() override final
virtual int64 TotalSize() override final
virtual FString GetArchiveName() const override
virtual int64 TotalSize() override
void LogWriteError(const TCHAR *Message)
virtual int64 Tell() override final
virtual bool WriteLowLevel(const uint8 *Src, int64 CountToWrite)
virtual bool Close() override final
TUniquePtr< IFileHandle > Handle
virtual void Seek(int64 InPos) override final
virtual bool SeekLowLevel(int64 InPos)
virtual void Serialize(void *V, int64 Length) override final
FArchiveFileWriterGeneric(IFileHandle *InHandle, const TCHAR *InFilename, int64 InPos, uint32 InBufferSize=PLATFORM_FILE_WRITER_BUFFER_SIZE, uint32 InFlags=FILEWRITE_None)
virtual bool CloseLowLevel()
virtual void Flush() override final
double GetFileAgeSeconds(const TCHAR *Filename) override
void FindFilesRecursiveInternal(TArray< FString > &FileNames, const TCHAR *StartDirectory, const TCHAR *Filename, bool Files, bool Directories)
bool IterateDirectoryRecursively(const TCHAR *Directory, IPlatformFile::FDirectoryVisitorFunc Visitor) override
int64 FileSize(const TCHAR *Filename) override
FArchive * CreateFileReaderInternal(const TCHAR *Filename, uint32 ReadFlags, uint32 BufferSize)
FString ConvertToAbsolutePathForExternalAppForWrite(const TCHAR *Filename) override
FArchive * CreateFileWriterInternal(const TCHAR *Filename, uint32 WriteFlags, uint32 BufferSize)
FDateTime GetTimeStamp(const TCHAR *Filename) override
FArchive * CreateFileReader(const TCHAR *Filename, uint32 ReadFlags=0) override
bool IsReadOnly(const TCHAR *Filename) override
bool IterateDirectoryStatRecursively(const TCHAR *Directory, IPlatformFile::FDirectoryStatVisitorFunc Visitor) override
static FString DefaultConvertToRelativePath(const TCHAR *Filename)
void GetTimeStampPair(const TCHAR *PathA, const TCHAR *PathB, FDateTime &OutTimeStampA, FDateTime &OutTimeStampB)
virtual void ProcessCommandLineOptions() override
bool IterateDirectoryStatRecursively(const TCHAR *Directory, IPlatformFile::FDirectoryStatVisitor &Visitor) override
bool IterateDirectory(const TCHAR *Directory, IPlatformFile::FDirectoryVisitor &Visitor) override
FORCEINLINE IPlatformFile & GetLowLevel() const
virtual void SetSandboxEnabled(bool bInEnabled) override
virtual bool MakeDirectory(const TCHAR *Path, bool Tree=0) override
bool DirectoryExists(const TCHAR *InDirectory) override
bool IterateDirectoryRecursively(const TCHAR *Directory, IPlatformFile::FDirectoryVisitor &Visitor) override
virtual uint32 Copy(const TCHAR *Dest, const TCHAR *Src, bool Replace=1, bool EvenIfReadOnly=0, bool Attributes=0, FCopyProgress *Progress=nullptr, EFileRead ReadFlags=FILEREAD_None, EFileWrite WriteFlags=FILEWRITE_None) override
bool FileExists(const TCHAR *Filename) override
bool Delete(const TCHAR *Filename, bool RequireExists=0, bool EvenReadOnly=0, bool Quiet=0) override
bool IterateDirectoryStat(const TCHAR *Directory, IPlatformFile::FDirectoryStatVisitor &Visitor) override
virtual bool IsSandboxEnabled() const override
virtual bool DeleteDirectory(const TCHAR *Path, bool RequireExists=0, bool Tree=0) override
virtual FString GetFilenameOnDisk(const TCHAR *Filename) override
void FindFilesRecursive(TArray< FString > &FileNames, const TCHAR *StartDirectory, const TCHAR *Filename, bool Files, bool Directories, bool bClearFileNames=true) override
virtual bool SendMessageToServer(const TCHAR *Message, IPlatformFile::IFileServerMessageHandler *Handler) override
bool IterateDirectory(const TCHAR *Directory, IPlatformFile::FDirectoryVisitorFunc Visitor) override
void FindFiles(TArray< FString > &Result, const TCHAR *Filename, bool Files, bool Directories) override
bool IterateDirectoryStat(const TCHAR *Directory, IPlatformFile::FDirectoryStatVisitorFunc Visitor) override
virtual void FindFiles(TArray< FString > &FoundFiles, const TCHAR *Directory, const TCHAR *FileExtension=nullptr) override
bool Move(const TCHAR *Dest, const TCHAR *Src, bool Replace=1, bool EvenIfReadOnly=0, bool Attributes=0, bool bDoNotRetryOrError=0) override
bool SetTimeStamp(const TCHAR *Filename, FDateTime Timestamp) override
FString ConvertToRelativePath(const TCHAR *Filename) override
FDateTime GetAccessTimeStamp(const TCHAR *Filename) override
FArchive * CreateFileWriter(const TCHAR *Filename, uint32 WriteFlags=0) override
FString ConvertToAbsolutePathForExternalAppForRead(const TCHAR *Filename) override
virtual FFileStatData GetStatData(const TCHAR *FilenameOrDirectory) override
uint32 CopyWithProgress(const TCHAR *InDestFile, const TCHAR *InSrcFile, bool ReplaceExisting, bool EvenIfReadOnly, bool Attributes, FCopyProgress *Progress, EFileRead ReadFlags, EFileWrite WriteFlags)
IPlatformFile & GetPlatformFile()
static FPlatformFileManager & Get()
TFunctionRef< bool(const TCHAR *, const FFileStatData &) FDirectoryStatVisitorFunc)
virtual bool IsSandboxEnabled() const
virtual void SetSandboxEnabled(bool bInEnabled)
virtual bool SendMessageToServer(const TCHAR *Message, IFileServerMessageHandler *Handler)
TFunctionRef< bool(const TCHAR *, bool) FDirectoryVisitorFunc)