Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
vfs_tree.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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 <map>
31
32#include "lib/file/file_system.h" // CFileInfo
33#include "lib/file/common/file_loader.h" // PIFileLoader
34#include "lib/file/common/real_directory.h" // PRealDirectory
36
38{
39public:
40 VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& provider);
41
42 const VfsPath& Name() const
43 {
44 return m_name;
45 }
46
47 size_t Size() const
48 {
49 return m_size;
50 }
51
52 time_t MTime() const
53 {
54 return m_mtime;
55 }
56
57 size_t Priority() const
58 {
59 return m_priority;
60 }
61
62 const PIFileLoader& Loader() const
63 {
64 return m_loader;
65 }
66
67private:
69 size_t m_size;
70 time_t m_mtime;
71
72 size_t m_priority;
73
75};
76
77
79{
80 /**
81 * remove all files with a lower priority than @p file
82 * and do the same for all subdirectories recursively.
83 * @return true if the directory is empty afterwards
84 **/
85 bool DeleteTree(const VfsFile& file);
86
87public:
88 typedef std::map<VfsPath, VfsFile> VfsFiles;
89 typedef std::map<VfsPath, VfsDirectory> VfsSubdirectories;
90
92
93 /**
94 * remove the given file or subdirectory according to the priority of the
95 * passed .DELETED file.
96 * CAUTION: Invalidates all previously returned pointers of the file or
97 * subdirectory (and contents) if those have lower priority
98 * than @p file.
99 **/
100 void DeleteSubtree(const VfsFile& file);
101
102 /**
103 * @return address of existing or newly inserted file.
104 **/
105 VfsFile* AddFile(const VfsFile& file);
106
107 /**
108 * @return address of existing or newly inserted subdirectory.
109 **/
111
112 /**
113 * remove the given file from the virtual directory (no effect on
114 * the physical file). no effect if the file does not exist.
115 **/
116 void RemoveFile(const VfsPath& name);
117
118 /**
119 * @return file with the given name.
120 * (note: non-const to allow changes to the file)
121 **/
122 VfsFile* GetFile(const VfsPath& name);
123
124 /**
125 * @return subdirectory with the given name.
126 * (note: non-const to allow changes to the subdirectory)
127 **/
129
130 // note: exposing only iterators wouldn't enable callers to reserve space.
131
132 const VfsFiles& Files() const
133 {
134 return m_files;
135 }
136
138 {
139 return m_subdirectories;
140 }
141
142 /**
143 * side effect: the next ShouldPopulate() will return true.
144 **/
145 void SetAssociatedDirectory(const PRealDirectory& realDirectory);
146
148 {
149 return m_realDirectory;
150 }
151
152 /**
153 * @return whether this directory should be populated from its
154 * AssociatedDirectory(). note that calling this is a promise to
155 * do so if true is returned -- the flag is reset immediately.
156 **/
157 bool ShouldPopulate();
158
159 /**
160 * ensure the next ShouldPopulate returns true.
161 **/
162 void RequestRepopulate();
163
164 /**
165 * empty file and subdirectory lists (e.g. when rebuilding VFS).
166 * CAUTION: this invalidates all previously returned pointers.
167 **/
168 void Clear();
169
170private:
173
176};
177
178
179/**
180 * @return a string containing file attributes (location, size, timestamp) and name.
181 **/
182extern std::wstring FileDescription(const VfsFile& file);
183
184/**
185 * @return a string holding each files' description (one per line).
186 **/
187extern std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel);
188
189/**
190 * append each directory's files' description to the given string.
191 **/
192void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel);
193
194#endif // #ifndef INCLUDED_VFS_TREE
Definition: path.h:80
Definition: vfs_tree.h:79
void SetAssociatedDirectory(const PRealDirectory &realDirectory)
side effect: the next ShouldPopulate() will return true.
Definition: vfs_tree.cpp:173
VfsFile * GetFile(const VfsPath &name)
Definition: vfs_tree.cpp:156
VfsFile * AddFile(const VfsFile &file)
Definition: vfs_tree.cpp:120
std::map< VfsPath, VfsFile > VfsFiles
Definition: vfs_tree.h:88
VfsFiles m_files
Definition: vfs_tree.h:171
void RemoveFile(const VfsPath &name)
remove the given file from the virtual directory (no effect on the physical file).
Definition: vfs_tree.cpp:150
void Clear()
empty file and subdirectory lists (e.g.
Definition: vfs_tree.cpp:199
void DeleteSubtree(const VfsFile &file)
remove the given file or subdirectory according to the priority of the passed .DELETED file.
Definition: vfs_tree.cpp:88
std::map< VfsPath, VfsDirectory > VfsSubdirectories
Definition: vfs_tree.h:89
bool m_shouldPopulate
Definition: vfs_tree.h:175
VfsDirectory * AddSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:142
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:102
VfsDirectory * GetSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:164
const VfsSubdirectories & Subdirectories() const
Definition: vfs_tree.h:137
VfsSubdirectories m_subdirectories
Definition: vfs_tree.h:172
PRealDirectory m_realDirectory
Definition: vfs_tree.h:174
const VfsFiles & Files() const
Definition: vfs_tree.h:132
bool ShouldPopulate()
Definition: vfs_tree.cpp:182
VfsDirectory()
Definition: vfs_tree.cpp:47
void RequestRepopulate()
ensure the next ShouldPopulate returns true.
Definition: vfs_tree.cpp:193
const PRealDirectory & AssociatedDirectory() const
Definition: vfs_tree.h:147
Definition: vfs_tree.h:38
size_t Priority() const
Definition: vfs_tree.h:57
const VfsPath & Name() const
Definition: vfs_tree.h:42
time_t MTime() const
Definition: vfs_tree.h:52
const PIFileLoader & Loader() const
Definition: vfs_tree.h:62
size_t Size() const
Definition: vfs_tree.h:47
VfsPath m_name
Definition: vfs_tree.h:68
size_t m_size
Definition: vfs_tree.h:69
time_t m_mtime
Definition: vfs_tree.h:70
VfsFile(const VfsPath &name, size_t size, time_t mtime, size_t priority, const PIFileLoader &provider)
Definition: vfs_tree.cpp:38
PIFileLoader m_loader
Definition: vfs_tree.h:74
size_t m_priority
Definition: vfs_tree.h:72
std::shared_ptr< IFileLoader > PIFileLoader
Definition: file_loader.h:39
std::shared_ptr< RealDirectory > PRealDirectory
Definition: real_directory.h:73
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:242
std::wstring FileDescriptions(const VfsDirectory &directory, size_t indentLevel)
Definition: vfs_tree.cpp:222
std::wstring FileDescription(const VfsFile &file)
Definition: vfs_tree.cpp:210