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-2023 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 {
25namespace asset {
27 Text = 0, // Opens in text mode
28 Binary = 1 << 0 // If true, file is opened in binary mode, if false, file is opened in text mode
29};
30
31enum class Offset { Beg, End, Cur };
32
33class IVirtualFile;
34class IVirtualDirectory;
35typedef std::shared_ptr<IVirtualFile> IVirtualFilePtr;
36typedef std::shared_ptr<IVirtualDirectory> IVirtualDirectoryPtr;
37
43 public:
46
47 virtual bool Initialize() = 0;
51 virtual IVirtualFilePtr Open(const std::string& path, FileModes modes = Text) = 0;
52
57 virtual void Close(IVirtualFilePtr&) = 0;
58
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
156 public:
158 void AddMountPoint(const std::string& path, IVirtualFileSystem* fs);
162 IVirtualFilePtr Open(const std::string& path, FileModes mode = FileModes::Text);
163
167 IVirtualFilePtr Open(const std::string& mount, const std::string& path, FileModes mode = FileModes::Text);
168
172 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& path);
173
177 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& mount, const std::string& path);
178
179 bool IsFile(const std::string& path);
180 bool IsFile(const std::string& mount, const std::string& path);
181 bool IsDirectory(const std::string& path);
182 bool IsDirectory(const std::string& mount, const std::string& path);
183 bool Exists(const std::string& path);
184 bool Exists(const std::string& mount, const std::string& path);
185
186 private:
187 std::map<std::string, IVirtualFileSystem*> mount_points;
188};
189
190// These functions feel like a hack
191
197std::vector<uint8_t> ReadAllFromVFile(IVirtualFile*);
198
203
209inline std::string GetFilename(const std::string& path) {
210 size_t last = path.find_last_of("/\\");
211 if (last == std::string::npos) {
212 // It's probably the whole thing
213 return path;
214 }
215 return path.substr(last + 1);
216}
217
218inline std::string GetParentPath(const std::string& path) {
219 size_t last = path.find_last_of("/\\");
220
221 if (last == std::string::npos) {
222 return "";
223 }
224 return path.substr(0, last);
225}
226} // namespace asset
227} // namespace cqsp
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:42
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:45
IVirtualFileSystem()
Definition: vfs.h:44
virtual void Close(IVirtualFilePtr &)=0
Closes the file.
Definition: vfs.h:155
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:187
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
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:218
std::string GetFilename(const std::string &path)
Gets filename from path.
Definition: vfs.h:209
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:34
Offset
Definition: vfs.h:31
FileModes
Definition: vfs.h:26
@ Text
Definition: vfs.h:27
@ Binary
Definition: vfs.h:28
std::shared_ptr< IVirtualDirectory > IVirtualDirectoryPtr
Definition: vfs.h:36
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: clientctx.h:21
glm::vec3 offset
Definition: starsystemview.cpp:72