Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
vfs_tree.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * 'tree' of VFS directories and files
25 */
26
27#ifndef INCLUDED_VFS_TREE
28#define INCLUDED_VFS_TREE
29
30#include "lib/file/common/file_loader.h" // PIFileLoader
31#include "lib/file/common/real_directory.h" // PRealDirectory
33
34#include <ctime>
35#include <map>
36#include <string>
37
39{
40public:
41 VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& provider);
42
43 const VfsPath& Name() const
44 {
45 return m_name;
46 }
47
48 size_t Size() const
49 {
50 return m_size;
51 }
52
53 time_t MTime() const
54 {
55 return m_mtime;
56 }
57
58 size_t Priority() const
59 {
60 return m_priority;
61 }
62
63 const PIFileLoader& Loader() const
64 {
65 return m_loader;
66 }
67
68private:
70 size_t m_size;
71 time_t m_mtime;
72
73 size_t m_priority;
74
76};
77
78
80{
81 /**
82 * remove all files with a lower priority than @p file
83 * and do the same for all subdirectories recursively.
84 * @return true if the directory is empty afterwards
85 **/
86 bool DeleteTree(const VfsFile& file);
87
88public:
89 typedef std::map<VfsPath, VfsFile> VfsFiles;
90 typedef std::map<VfsPath, VfsDirectory> VfsSubdirectories;
91
93
94 /**
95 * remove the given file or subdirectory according to the priority of the
96 * passed .DELETED file.
97 * CAUTION: Invalidates all previously returned pointers of the file or
98 * subdirectory (and contents) if those have lower priority
99 * than @p file.
100 **/
101 void DeleteSubtree(const VfsFile& file);
102
103 /**
104 * @return address of existing or newly inserted file.
105 **/
106 VfsFile* AddFile(const VfsFile& file);
107
108 /**
109 * @return address of existing or newly inserted subdirectory.
110 **/
112
113 /**
114 * remove the given file from the virtual directory (no effect on
115 * the physical file). no effect if the file does not exist.
116 **/
117 void RemoveFile(const VfsPath& name);
118
119 /**
120 * @return file with the given name.
121 * (note: non-const to allow changes to the file)
122 **/
123 VfsFile* GetFile(const VfsPath& name);
124
125 /**
126 * @return subdirectory with the given name.
127 * (note: non-const to allow changes to the subdirectory)
128 **/
130
131 // note: exposing only iterators wouldn't enable callers to reserve space.
132
133 const VfsFiles& Files() const
134 {
135 return m_files;
136 }
137
139 {
140 return m_subdirectories;
141 }
142
143 /**
144 * side effect: the next ShouldPopulate() will return true.
145 **/
146 void SetAssociatedDirectory(const PRealDirectory& realDirectory);
147
149 {
150 return m_realDirectory;
151 }
152
153 /**
154 * @return whether this directory should be populated from its
155 * AssociatedDirectory(). note that calling this is a promise to
156 * do so if true is returned -- the flag is reset immediately.
157 **/
158 bool ShouldPopulate();
159
160 /**
161 * ensure the next ShouldPopulate returns true.
162 **/
163 void RequestRepopulate();
164
165 /**
166 * empty file and subdirectory lists (e.g. when rebuilding VFS).
167 * CAUTION: this invalidates all previously returned pointers.
168 **/
169 void Clear();
170
171private:
174
177};
178
179
180/**
181 * @return a string containing file attributes (location, size, timestamp) and name.
182 **/
183extern std::wstring FileDescription(const VfsFile& file);
184
185/**
186 * @return a string holding each files' description (one per line).
187 **/
188extern std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel);
189
190/**
191 * append each directory's files' description to the given string.
192 **/
193void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel);
194
195#endif // #ifndef INCLUDED_VFS_TREE
Definition: path.h:84
Definition: vfs_tree.h:80
void SetAssociatedDirectory(const PRealDirectory &realDirectory)
side effect: the next ShouldPopulate() will return true.
Definition: vfs_tree.cpp:179
VfsFile * GetFile(const VfsPath &name)
Definition: vfs_tree.cpp:162
VfsFile * AddFile(const VfsFile &file)
Definition: vfs_tree.cpp:126
std::map< VfsPath, VfsFile > VfsFiles
Definition: vfs_tree.h:89
VfsFiles m_files
Definition: vfs_tree.h:172
void RemoveFile(const VfsPath &name)
remove the given file from the virtual directory (no effect on the physical file).
Definition: vfs_tree.cpp:156
void Clear()
empty file and subdirectory lists (e.g.
Definition: vfs_tree.cpp:205
void DeleteSubtree(const VfsFile &file)
remove the given file or subdirectory according to the priority of the passed .DELETED file.
Definition: vfs_tree.cpp:94
std::map< VfsPath, VfsDirectory > VfsSubdirectories
Definition: vfs_tree.h:90
bool m_shouldPopulate
Definition: vfs_tree.h:176
VfsDirectory * AddSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:148
bool DeleteTree(const VfsFile &file)
remove all files with a lower priority than file and do the same for all subdirectories recursively.
Definition: vfs_tree.cpp:108
VfsDirectory * GetSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:170
const VfsSubdirectories & Subdirectories() const
Definition: vfs_tree.h:138
VfsSubdirectories m_subdirectories
Definition: vfs_tree.h:173
PRealDirectory m_realDirectory
Definition: vfs_tree.h:175
const VfsFiles & Files() const
Definition: vfs_tree.h:133
bool ShouldPopulate()
Definition: vfs_tree.cpp:188
VfsDirectory()
Definition: vfs_tree.cpp:53
void RequestRepopulate()
ensure the next ShouldPopulate returns true.
Definition: vfs_tree.cpp:199
const PRealDirectory & AssociatedDirectory() const
Definition: vfs_tree.h:148
Definition: vfs_tree.h:39
size_t Priority() const
Definition: vfs_tree.h:58
const VfsPath & Name() const
Definition: vfs_tree.h:43
time_t MTime() const
Definition: vfs_tree.h:53
const PIFileLoader & Loader() const
Definition: vfs_tree.h:63
size_t Size() const
Definition: vfs_tree.h:48
VfsPath m_name
Definition: vfs_tree.h:69
size_t m_size
Definition: vfs_tree.h:70
time_t m_mtime
Definition: vfs_tree.h:71
VfsFile(const VfsPath &name, size_t size, time_t mtime, size_t priority, const PIFileLoader &provider)
Definition: vfs_tree.cpp:44
PIFileLoader m_loader
Definition: vfs_tree.h:75
size_t m_priority
Definition: vfs_tree.h:73
std::shared_ptr< IFileLoader > PIFileLoader
Definition: file_loader.h:44
std::shared_ptr< RealDirectory > PRealDirectory
Definition: real_directory.h:80
void DirectoryDescriptionR(std::wstring &descriptions, const VfsDirectory &directory, size_t indentLevel)
append each directory's files' description to the given string.
Definition: vfs_tree.cpp:248
std::wstring FileDescriptions(const VfsDirectory &directory, size_t indentLevel)
Definition: vfs_tree.cpp:228
std::wstring FileDescription(const VfsFile &file)
Definition: vfs_tree.cpp:216