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;
34typedef std::shared_ptr<IVirtualFile> IVirtualFilePtr;
35typedef std::shared_ptr<IVirtualDirectory> IVirtualDirectoryPtr;
36
42 public:
45
46 virtual bool Initialize() = 0;
50 virtual IVirtualFilePtr Open(const std::string& path, FileModes modes = Text) = 0;
51
56 virtual void Close(IVirtualFilePtr&) = 0;
57
64 virtual IVirtualDirectoryPtr OpenDirectory(const std::string& path) = 0;
65
69 virtual bool IsFile(const std::string& path) = 0;
70
74 virtual bool IsDirectory(const std::string& path) = 0;
75
79 virtual bool Exists(const std::string& path) = 0;
80};
81
88 public:
90 virtual ~IVirtualDirectory() {}
91
95 virtual uint64_t GetSize() = 0;
96
101 virtual const std::string& GetRoot() = 0;
102
106 virtual IVirtualFilePtr GetFile(int index, FileModes modes = Text) = 0;
107
111 virtual const std::string& GetFilename(int index) = 0;
112
117};
118
120 public:
122 virtual ~IVirtualFile() {}
123
127 virtual uint64_t Size() = 0;
128
134 virtual void Read(uint8_t* buffer, int num_bytes) = 0;
135
139 virtual bool Seek(long offset, Offset origin = Offset::Cur) = 0;
140
144 virtual uint64_t Tell() = 0;
145
149 virtual const std::string& Path() = 0;
150
152};
153
155 public:
157 void AddMountPoint(const std::string& path, IVirtualFileSystem* fs);
161 IVirtualFilePtr Open(const std::string& path, FileModes mode = FileModes::Text);
162
166 IVirtualFilePtr Open(const std::string& mount, const std::string& path, FileModes mode = FileModes::Text);
167
171 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& path);
172
176 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& mount, const std::string& path);
177
178 bool IsFile(const std::string& path);
179 bool IsFile(const std::string& mount, const std::string& path);
180 bool IsDirectory(const std::string& path);
181 bool IsDirectory(const std::string& mount, const std::string& path);
182 bool Exists(const std::string& path);
183 bool Exists(const std::string& mount, const std::string& path);
184
185 private:
186 std::map<std::string, IVirtualFileSystem*> mount_points;
187};
188
189// These functions feel like a hack
190
196std::vector<uint8_t> ReadAllFromVFile(IVirtualFile*);
197
202
208inline std::string GetFilename(const std::string& path) {
209 size_t last = path.find_last_of("/\\");
210 if (last == std::string::npos) {
211 // It's probably the whole thing
212 return path;
213 }
214 return path.substr(last + 1);
215}
216
217inline std::string GetParentPath(const std::string& path) {
218 size_t last = path.find_last_of("/\\");
219
220 if (last == std::string::npos) {
221 return "";
222 }
223 return path.substr(0, last);
224}
225} // 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:87
virtual uint64_t GetSize()=0
Get number of files in directory
IVirtualDirectory()
Definition: vfs.h:89
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:90
Definition: vfs.h:119
virtual uint64_t Size()=0
Size of file in bytes
virtual IVirtualFileSystem * GetFileSystem()=0
IVirtualFile()
Definition: vfs.h:121
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:122
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:41
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. Does not list directories in the list because for our purposes,...
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:44
IVirtualFileSystem()
Definition: vfs.h:43
virtual void Close(IVirtualFilePtr &)=0
Closes the file.
Definition: vfs.h:154
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:186
std::shared_ptr< IVirtualDirectory > OpenDirectory(const std::string &path)
Opens directory
Definition: vfs.cpp:51
lib tracy 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:217
std::string GetFilename(const std::string &path)
Gets filename from path.
Definition: vfs.h:208
std::string ReadAllFromVFileToString(IVirtualFile *file)
Don't really want this, but ah well, it cannot be helped.
Definition: vfs.cpp:134
std::shared_ptr< IVirtualFile > IVirtualFilePtr
Definition: vfs.h:33
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:35
glm::vec3 offset
Definition: starsystemview.cpp:92