Plasma Engine  2.0
Loading...
Searching...
No Matches
FileSystemModel.h
1#pragma once
2
3#include <ToolsFoundation/ToolsFoundationDLL.h>
4
5#if PL_ENABLED(PL_SUPPORTS_DIRECTORY_WATCHER) && PL_ENABLED(PL_SUPPORTS_FILE_ITERATORS)
6
7# include <Foundation/Application/Config/FileSystemConfig.h>
8# include <Foundation/Configuration/Singleton.h>
9# include <Foundation/Threading/LockedObject.h>
10# include <Foundation/Types/UniquePtr.h>
11# include <ToolsFoundation/FileSystem/DataDirPath.h>
12# include <ToolsFoundation/FileSystem/Declarations.h>
13
14class plFileSystemWatcher;
15struct plFileSystemWatcherEvent;
16struct plFileStats;
17
19struct PL_TOOLSFOUNDATION_DLL plFolderChangedEvent
20{
21 enum class Type
22 {
23 None,
24 FolderAdded,
25 FolderRemoved,
26 ModelReset,
27 };
28
29 plFolderChangedEvent() = default;
30 plFolderChangedEvent(const plDataDirPath& file, Type type);
31
32 plDataDirPath m_Path;
33 Type m_Type = Type::None;
34};
35
37struct PL_TOOLSFOUNDATION_DLL plFileChangedEvent
38{
39 enum class Type
40 {
41 None,
42 FileAdded,
43 FileChanged,
44 DocumentLinked,
45 DocumentUnlinked,
46 FileRemoved,
47 ModelReset
48 };
49
50 plFileChangedEvent() = default;
51 plFileChangedEvent(const plDataDirPath& file, plFileStatus status, Type type);
52
53 plDataDirPath m_Path;
54 plFileStatus m_Status;
55 Type m_Type = Type::None;
56};
57
63class PL_TOOLSFOUNDATION_DLL plFileSystemModel
64{
65 PL_DECLARE_SINGLETON(plFileSystemModel);
66
67public:
70
73
74public:
76 static bool IsSameFile(const plStringView sAbsolutePathA, const plStringView sAbsolutePathB);
77
79 static plUInt64 HashFile(plStreamReader& ref_inputStream, plStreamWriter* pPassThroughStream);
80
81public:
84
85 plFileSystemModel();
86 ~plFileSystemModel();
87
92 void Initialize(const plApplicationFileSystemConfig& fileSystemConfig, FilesMap&& referencedFiles, FoldersMap&& referencedFolders);
93
97 void Deinitialize(FilesMap* out_pReferencedFiles = nullptr, FoldersMap* out_pReferencedFolders = nullptr);
98
100 void MainThreadTick();
101
102 const plApplicationFileSystemConfig& GetFileSystemConfig() const { return m_FileSystemConfig; }
103 plArrayPtr<const plString> GetDataDirectoryRoots() const { return m_DataDirRoots.GetArrayPtr(); }
104
108
111 const LockedFiles GetFiles() const;
112
115 const LockedFolders GetFolders() const;
116
121 plResult FindFile(plStringView sPath, plFileStatus& out_stat) const;
122
126 plResult FindFile(plDelegate<bool(const plDataDirPath&, const plFileStatus&)> visitor) const;
127
131
135 void NotifyOfChange(plStringView sAbsolutePath);
136
139 void CheckFolder(plStringView sAbsolutePath);
140
142 void CheckFileSystem();
143
147
152 plResult LinkDocument(plStringView sAbsolutePath, const plUuid& documentId);
153
157 plResult UnlinkDocument(plStringView sAbsolutePath);
158
163 plResult ReadDocument(plStringView sAbsolutePath, const plDelegate<void(const plFileStatus&, plStreamReader&)>& callback);
164
169 plResult HashFile(plStringView sAbsolutePath, plFileStatus& out_stat);
170
172
173public:
176
177private:
178 void SetAllStatusUnknown();
179 void RemoveStaleFileInfos();
180
181 void OnAssetWatcherEvent(const plFileSystemWatcherEvent& e);
182 plFileStatus HandleSingleFile(plDataDirPath absolutePath, bool bRecurseIntoFolders);
183 plFileStatus HandleSingleFile(plDataDirPath absolutePath, const plFileStats& FileStat, bool bRecurseIntoFolders);
184
185 void RemoveFileOrFolder(const plDataDirPath& absolutePath, bool bRecurseIntoFolders);
186
187 void MarkFileLocked(plStringView sAbsolutePath);
188
189 void FireFileChangedEvent(const plDataDirPath& file, plFileStatus fileStatus, plFileChangedEvent::Type type);
190 void FireFolderChangedEvent(const plDataDirPath& file, plFolderChangedEvent::Type type);
191
192private:
193 // Immutable data after Initialize
194 plApplicationFileSystemConfig m_FileSystemConfig;
195 plDynamicArray<plString> m_DataDirRoots;
197 plEventSubscriptionID m_WatcherSubscription = {};
198
199 // Actual file system data
200 mutable plMutex m_FilesMutex;
201 plAtomicBool m_bInitialized = false;
202
203 FilesMap m_ReferencedFiles; // Absolute path to stat map
204 FoldersMap m_ReferencedFolders; // Absolute path to status map
205 plSet<plString> m_LockedFiles;
206 plMap<plString, plFileStatus> m_TransiendFiles; // Absolute path to stat for files outside the data directories.
207};
208
209#endif
Definition FileSystemConfig.h:8
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
An atomic boolean variable. This is just a wrapper around an atomic int32 for convenience.
Definition AtomicInteger.h:100
A reference to a file or folder inside a data directory.
Definition DataDirPath.h:18
Definition DynamicArray.h:81
Definition Event.h:177
Provides access to an object while managing a lock (e.g. a mutex) that ensures that during its lifeti...
Definition LockedObject.h:7
Definition Map.h:408
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Definition Set.h:238
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Holds the stats for a file.
Definition OSFile.h:34
Information about a single file on disk. The file might be a document or any other file found in the ...
Definition Declarations.h:20
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54