Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
vfs.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <map>
20#include <memory>
21#include <string>
22#include <vector>
23
24namespace cqsp::asset {
26 Text = 0, // Opens in text mode
27 Binary = 1 << 0 // If true, file is opened in binary mode, if false, file is opened in text mode
28};
29
30enum class Offset { Beg, End, Cur };
31
32class IVirtualFile;
33class IVirtualDirectory;
34class IVirtualFileSystem;
35
36typedef std::shared_ptr<IVirtualFile> IVirtualFilePtr;
37typedef std::shared_ptr<IVirtualDirectory> IVirtualDirectoryPtr;
38typedef std::shared_ptr<IVirtualFileSystem> IVirtualFileSystemPtr;
39
45 public:
48
49 virtual bool Initialize() = 0;
53 virtual IVirtualFilePtr Open(const std::string& path, FileModes modes = Text) = 0;
54
59 virtual void Close(IVirtualFilePtr&) = 0;
60
65 virtual IVirtualDirectoryPtr OpenDirectory(const std::string& path) = 0;
66
70 virtual bool IsFile(const std::string& path) = 0;
71
75 virtual bool IsDirectory(const std::string& path) = 0;
76
80 virtual bool Exists(const std::string& path) = 0;
81};
82
89 public:
91 virtual ~IVirtualDirectory() {}
92
96 virtual uint64_t GetSize() = 0;
97
102 virtual const std::string& GetRoot() = 0;
103
107 virtual IVirtualFilePtr GetFile(int index, FileModes modes = Text) = 0;
108
112 virtual const std::string& GetFilename(int index) = 0;
113
118};
119
121 public:
123 virtual ~IVirtualFile() {}
124
128 virtual uint64_t Size() = 0;
129
135 virtual void Read(uint8_t* buffer, int num_bytes) = 0;
136
140 virtual bool Seek(long offset, Offset origin = Offset::Cur) = 0;
141
145 virtual uint64_t Tell() = 0;
146
150 virtual const std::string& Path() = 0;
151
153};
154
159 public:
164 void AddMountPoint(const std::string& path, IVirtualFileSystem* fs);
168 IVirtualFilePtr Open(const std::string& path, FileModes mode = FileModes::Text);
169
173 IVirtualFilePtr Open(const std::string& mount, const std::string& path, FileModes mode = FileModes::Text);
174
178 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& path);
179
183 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& mount, const std::string& path);
184
185 bool IsFile(const std::string& path);
186 bool IsFile(const std::string& mount, const std::string& path);
187 bool IsDirectory(const std::string& path);
188 bool IsDirectory(const std::string& mount, const std::string& path);
189 bool Exists(const std::string& path);
190 bool Exists(const std::string& mount, const std::string& path);
191
192 private:
193 std::map<std::string, IVirtualFileSystem*> mount_points;
194};
195
196// These functions feel like a hack
197
203std::vector<uint8_t> ReadAllFromVFile(IVirtualFile*);
204
209
215inline std::string GetFilename(const std::string& path) {
216 size_t last = path.find_last_of("/\\");
217 if (last == std::string::npos) {
218 // It's probably the whole thing
219 return path;
220 }
221 return path.substr(last + 1);
222}
223
224inline std::string GetParentPath(const std::string& path) {
225 size_t last = path.find_last_of("/\\");
226
227 if (last == std::string::npos) {
228 return "";
229 }
230 return path.substr(0, last);
231}
232} // namespace cqsp::asset
Meant to list all the files and sub files in the directory. This is sort of a replacement for directo...
Definition: vfs.h:88
virtual uint64_t GetSize()=0
Get number of files in directory
IVirtualDirectory()
Definition: vfs.h:90
virtual IVirtualFileSystem * GetFileSystem()=0
Get the filesystem this refers to.
virtual const std::string & GetRoot()=0
The root directory, relative to the filesystem.
virtual const std::string & GetFilename(int index)=0
Get file name relative to this directory
virtual IVirtualFilePtr GetFile(int index, FileModes modes=Text)=0
Opens file of index.
virtual ~IVirtualDirectory()
Definition: vfs.h:91
Definition: vfs.h:120
virtual uint64_t Size()=0
Size of file in bytes
virtual IVirtualFileSystem * GetFileSystem()=0
IVirtualFile()
Definition: vfs.h:122
virtual void Read(uint8_t *buffer, int num_bytes)=0
Reads num_bytes of bytes into buffer. Frees memory for the number
virtual ~IVirtualFile()
Definition: vfs.h:123
virtual const std::string & Path()=0
Get file path relative to the filesystem.
virtual uint64_t Tell()=0
Get current file position
virtual bool Seek(long offset, Offset origin=Offset::Cur)=0
Goto position of file, offset away from origin
The main functionality for this is to read files, so writing to files will not really be supported.
Definition: vfs.h:44
virtual IVirtualFilePtr Open(const std::string &path, FileModes modes=Text)=0
Opens file from the filesystem. Returns nullptr if file is invalid
virtual bool IsDirectory(const std::string &path)=0
Is the file a directory </summary
virtual IVirtualDirectoryPtr OpenDirectory(const std::string &path)=0
Lists all the files in the directory and subdirectories. Returns nullptr if path does not exist or is...
virtual bool Exists(const std::string &path)=0
Does the file exist
virtual bool IsFile(const std::string &path)=0
Is the file a file
virtual ~IVirtualFileSystem()
Definition: vfs.h:47
IVirtualFileSystem()
Definition: vfs.h:46
virtual void Close(IVirtualFilePtr &)=0
Closes the file.
Definition: vfs.h:158
IVirtualFilePtr Open(const std::string &path, FileModes mode=FileModes::Text)
Opens file.
Definition: vfs.cpp:34
void AddMountPoint(const std::string &path, IVirtualFileSystem *fs)
Definition: vfs.cpp:30
~VirtualMounter()
Definition: vfs.cpp:24
bool Exists(const std::string &path)
Definition: vfs.cpp:110
bool IsFile(const std::string &path)
Definition: vfs.cpp:75
bool IsDirectory(const std::string &path)
Definition: vfs.cpp:94
std::map< std::string, IVirtualFileSystem * > mount_points
Definition: vfs.h:193
std::shared_ptr< IVirtualDirectory > OpenDirectory(const std::string &path)
Opens directory
Definition: vfs.cpp:51
lib tracy tracy public file(GLOB_RECURSE CPP_FILES *.cpp) list(FILTER CPP_FILES EXCLUDE REGEX ".*main.cpp$") file(GLOB_RECURSE H_FILES *.h) set(SOURCE_FILES $
Definition: CMakeLists.txt:22
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
std::vector< uint8_t > ReadAllFromVFile(IVirtualFile *file)
Reads all the data from the virtual file.
Definition: vfs.cpp:126
std::string GetParentPath(const std::string &path)
Definition: vfs.h:224
std::string GetFilename(const std::string &path)
Gets filename from path.
Definition: vfs.h:215
std::string ReadAllFromVFileToString(IVirtualFile *file)
Don't really want this, but ah well, it cannot be helped.
Definition: vfs.cpp:134
std::shared_ptr< IVirtualFileSystem > IVirtualFileSystemPtr
Definition: vfs.h:38
std::shared_ptr< IVirtualFile > IVirtualFilePtr
Definition: vfs.h:34
Offset
Definition: vfs.h:30
FileModes
Definition: vfs.h:25
@ Text
Definition: vfs.h:26
@ Binary
Definition: vfs.h:27
std::shared_ptr< IVirtualDirectory > IVirtualDirectoryPtr
Definition: vfs.h:37