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.
nativevfs.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 <fstream>
20#include <memory>
21#include <string>
22#include <vector>
23
25
26namespace cqsp::asset {
27class NativeFileSystem;
28class NativeDirectory;
29
30class NativeFile : public IVirtualFile {
31 public:
32 explicit NativeFile(NativeFileSystem* _nfs) : IVirtualFile(), nfs(_nfs), path("") {}
33 NativeFile(NativeFileSystem* _nfs, const std::string& file) : IVirtualFile(), nfs(_nfs), path(file) {}
34
35 ~NativeFile() { file.close(); }
36
37 const std::string& Path() override;
38 uint64_t Size() override;
39
40 void Read(uint8_t* buffer, int bytes) override;
41
42 bool Seek(long offset, Offset origin) override;
43 uint64_t Tell() override;
44
45 IVirtualFileSystem* GetFileSystem() override { return reinterpret_cast<IVirtualFileSystem*>(nfs); }
46
48
49 private:
50 std::string path;
51 std::ifstream file;
52 int size;
53
55};
56
58 public:
59 explicit NativeFileSystem(std::string root);
60 ~NativeFileSystem() = default;
61
62 bool Initialize() override { return true; }
63
64 std::shared_ptr<IVirtualFile> Open(const std::string& path, FileModes) override;
65 void Close(std::shared_ptr<IVirtualFile>&) override;
66 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& dir) override;
67
68 bool IsFile(const std::string& path) override;
69 bool IsDirectory(const std::string& path) override;
70 bool Exists(const std::string& path) override;
71 const std::string& GetRoot() { return root; }
72
73 private:
74 std::string root;
76};
77
79 public:
80 NativeDirectory(NativeFileSystem* _nfs, const std::string& _root) : nfs(_nfs), root(_root) {}
81
82 uint64_t GetSize() override;
83 const std::string& GetRoot() override;
84 std::shared_ptr<IVirtualFile> GetFile(int index, FileModes modes) override;
85 const std::string& GetFilename(int index) override;
87
88 private:
90 std::vector<std::string> paths;
91 std::string root;
93};
94} // 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
Definition: vfs.h:119
The main functionality for this is to read files, so writing to files will not really be supported.
Definition: vfs.h:41
Definition: nativevfs.h:78
friend NativeFileSystem
Definition: nativevfs.h:89
uint64_t GetSize() override
Get number of files in directory
Definition: nativevfs.cpp:138
NativeFileSystem *const nfs
Definition: nativevfs.h:92
std::vector< std::string > paths
Definition: nativevfs.h:90
std::shared_ptr< IVirtualFile > GetFile(int index, FileModes modes) override
Opens file of index.
Definition: nativevfs.cpp:142
std::string root
Definition: nativevfs.h:91
NativeDirectory(NativeFileSystem *_nfs, const std::string &_root)
Definition: nativevfs.h:80
IVirtualFileSystem * GetFileSystem() override
Get the filesystem this refers to.
Definition: nativevfs.cpp:150
const std::string & GetRoot() override
The root directory, relative to the filesystem.
Definition: nativevfs.cpp:140
const std::string & GetFilename(int index) override
Get file name relative to this directory
Definition: nativevfs.cpp:148
Definition: nativevfs.h:30
std::ifstream file
Definition: nativevfs.h:51
IVirtualFileSystem * GetFileSystem() override
Definition: nativevfs.h:45
uint64_t Tell() override
Get current file position
Definition: nativevfs.cpp:136
bool Seek(long offset, Offset origin) override
Goto position of file, offset away from origin
Definition: nativevfs.cpp:119
NativeFile(NativeFileSystem *_nfs, const std::string &file)
Definition: nativevfs.h:33
friend NativeFileSystem
Definition: nativevfs.h:47
std::string path
Definition: nativevfs.h:50
void Read(uint8_t *buffer, int bytes) override
Reads num_bytes of bytes into buffer. Frees memory for the number
Definition: nativevfs.cpp:113
NativeFile(NativeFileSystem *_nfs)
Definition: nativevfs.h:32
NativeFileSystem *const nfs
Definition: nativevfs.h:54
uint64_t Size() override
Size of file in bytes
Definition: nativevfs.cpp:111
~NativeFile()
Definition: nativevfs.h:35
const std::string & Path() override
Get file path relative to the filesystem.
Definition: nativevfs.cpp:109
int size
Definition: nativevfs.h:52
Definition: nativevfs.h:57
friend NativeDirectory
Definition: nativevfs.h:75
NativeFileSystem(std::string root)
Definition: nativevfs.cpp:28
std::shared_ptr< IVirtualDirectory > OpenDirectory(const std::string &dir) override
Lists all the files in the directory. Does not list directories in the list because for our purposes,...
Definition: nativevfs.cpp:68
void Close(std::shared_ptr< IVirtualFile > &) override
Definition: nativevfs.cpp:62
bool Initialize() override
Definition: nativevfs.h:62
std::string root
Definition: nativevfs.h:74
bool Exists(const std::string &path) override
Does the file exist
Definition: nativevfs.cpp:105
bool IsDirectory(const std::string &path) override
Is the file a directory </summary
Definition: nativevfs.cpp:101
bool IsFile(const std::string &path) override
Is the file a file
Definition: nativevfs.cpp:97
const std::string & GetRoot()
Definition: nativevfs.h:71
std::shared_ptr< IVirtualFile > Open(const std::string &path, FileModes) override
Opens file from the filesystem. Returns nullptr if file is invalid
Definition: nativevfs.cpp:30
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
Offset
Definition: vfs.h:30
FileModes
Definition: vfs.h:25
glm::vec3 offset
Definition: starsystemview.cpp:91