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 Read = 0,
29 Write = 1 << 1
30};
31
32enum class Offset { Beg, End, Cur };
33
34class IVirtualFile;
35class IVirtualDirectory;
36class IVirtualFileSystem;
37
38typedef std::shared_ptr<IVirtualFile> IVirtualFilePtr;
39typedef std::shared_ptr<IVirtualDirectory> IVirtualDirectoryPtr;
40typedef std::shared_ptr<IVirtualFileSystem> IVirtualFileSystemPtr;
41
47 public:
50
51 virtual bool Initialize() = 0;
55 virtual IVirtualFilePtr Open(const std::string& path, FileModes modes = Text) = 0;
56
61 virtual void Close(IVirtualFilePtr&) = 0;
62
67 virtual IVirtualDirectoryPtr OpenDirectory(const std::string& path) = 0;
68
72 virtual bool IsFile(const std::string& path) = 0;
73
77 virtual bool IsDirectory(const std::string& path) = 0;
78
82 virtual bool Exists(const std::string& path) = 0;
83};
84
91 public:
93 virtual ~IVirtualDirectory() {}
94
98 virtual uint64_t GetSize() = 0;
99
104 virtual const std::string& GetRoot() = 0;
105
109 virtual IVirtualFilePtr GetFile(int index, FileModes modes = Text) = 0;
110
114 virtual const std::string& GetFilename(int index) = 0;
115
120};
121
123 public:
125 virtual ~IVirtualFile() {}
126
130 virtual uint64_t Size() = 0;
131
137 virtual void Read(uint8_t* buffer, int num_bytes) = 0;
138
142 virtual bool Seek(long offset, Offset origin = Offset::Cur) = 0;
143
147 virtual uint64_t Tell() = 0;
148
152 virtual const std::string& Path() = 0;
153
155};
156
161 public:
166 void AddMountPoint(const std::string& path, IVirtualFileSystem* fs);
170 IVirtualFilePtr Open(const std::string& path, FileModes mode = FileModes::Text);
171
175 IVirtualFilePtr Open(const std::string& mount, const std::string& path, FileModes mode = FileModes::Text);
176
180 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& path);
181
185 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& mount, const std::string& path);
186
187 bool IsFile(const std::string& path);
188 bool IsFile(const std::string& mount, const std::string& path);
189 bool IsDirectory(const std::string& path);
190 bool IsDirectory(const std::string& mount, const std::string& path);
191 bool Exists(const std::string& path);
192 bool Exists(const std::string& mount, const std::string& path);
193
194 private:
195 std::map<std::string, IVirtualFileSystem*> mount_points;
196};
197
198// These functions feel like a hack
199
205std::vector<uint8_t> ReadAllFromVFile(IVirtualFile*);
206
211
217inline std::string GetFilename(const std::string& path) {
218 size_t last = path.find_last_of("/\\");
219 if (last == std::string::npos) {
220 // It's probably the whole thing
221 return path;
222 }
223 return path.substr(last + 1);
224}
225
226inline std::string GetParentPath(const std::string& path) {
227 size_t last = path.find_last_of("/\\");
228
229 if (last == std::string::npos) {
230 return "";
231 }
232 return path.substr(0, last);
233}
234} // 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:90
virtual uint64_t GetSize()=0
Get number of files in directory
IVirtualDirectory()
Definition: vfs.h:92
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:93
Definition: vfs.h:122
virtual uint64_t Size()=0
Size of file in bytes
virtual IVirtualFileSystem * GetFileSystem()=0
IVirtualFile()
Definition: vfs.h:124
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:125
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:46
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:49
IVirtualFileSystem()
Definition: vfs.h:48
virtual void Close(IVirtualFilePtr &)=0
Closes the file.
Definition: vfs.h:160
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:195
std::shared_ptr< IVirtualDirectory > OpenDirectory(const std::string &path)
Opens directory
Definition: vfs.cpp:51
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:226
std::string GetFilename(const std::string &path)
Gets filename from path.
Definition: vfs.h:217
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:40
std::shared_ptr< IVirtualFile > IVirtualFilePtr
Definition: vfs.h:36
Offset
Definition: vfs.h:32
FileModes
Definition: vfs.h:25
@ Write
Definition: vfs.h:29
@ Text
Definition: vfs.h:26
@ Binary
Definition: vfs.h:27
@ Read
Definition: vfs.h:28
std::shared_ptr< IVirtualDirectory > IVirtualDirectoryPtr
Definition: vfs.h:39
std::string file
Definition: recipeviewer.cpp:41