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-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 <fstream>
20#include <memory>
21#include <string>
22#include <vector>
23
25
26namespace cqsp {
27namespace asset {
28class NativeFileSystem;
29class NativeDirectory;
30
31class NativeFile : public IVirtualFile {
32 public:
33 explicit NativeFile(NativeFileSystem* _nfs) : IVirtualFile(), nfs(_nfs), path("") {}
34 NativeFile(NativeFileSystem* _nfs, const std::string& file) : IVirtualFile(), nfs(_nfs), path(file) {}
35
36 ~NativeFile() { file.close(); }
37
38 const std::string& Path() override;
39 uint64_t Size() override;
40
41 void Read(uint8_t* buffer, int bytes) override;
42
43 bool Seek(long offset, Offset origin) override;
44 uint64_t Tell() override;
45
46 IVirtualFileSystem* GetFileSystem() override { return reinterpret_cast<IVirtualFileSystem*>(nfs); }
47
49
50 private:
51 std::string path;
52 std::ifstream file;
53 int size;
54
56};
57
59 public:
60 explicit NativeFileSystem(std::string root);
61 ~NativeFileSystem() = default;
62
63 bool Initialize() override { return true; }
64
65 std::shared_ptr<IVirtualFile> Open(const std::string& path, FileModes) override;
66 void Close(std::shared_ptr<IVirtualFile>&) override;
67 std::shared_ptr<IVirtualDirectory> OpenDirectory(const std::string& dir) override;
68
69 bool IsFile(const std::string& path) override;
70 bool IsDirectory(const std::string& path) override;
71 bool Exists(const std::string& path) override;
72 const std::string& GetRoot() { return root; }
73
74 private:
75 std::string root;
77};
78
80 public:
81 NativeDirectory(NativeFileSystem* _nfs, const std::string& _root) : nfs(_nfs), root(_root) {}
82
83 uint64_t GetSize() override;
84 const std::string& GetRoot() override;
85 std::shared_ptr<IVirtualFile> GetFile(int index, FileModes modes) override;
86 const std::string& GetFilename(int index) override;
88
89 private:
91 std::vector<std::string> paths;
92 std::string root;
94};
95} // namespace asset
96} // 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
Definition: vfs.h:120
The main functionality for this is to read files, so writing to files will not really be supported.
Definition: vfs.h:42
Definition: nativevfs.h:79
std::shared_ptr< IVirtualFile > GetFile(int index, FileModes modes) override
Opens file of index.
Definition: nativevfs.cpp:142
friend NativeFileSystem
Definition: nativevfs.h:90
uint64_t GetSize() override
Get number of files in directory
Definition: nativevfs.cpp:138
NativeFileSystem *const nfs
Definition: nativevfs.h:93
std::vector< std::string > paths
Definition: nativevfs.h:91
std::string root
Definition: nativevfs.h:92
NativeDirectory(NativeFileSystem *_nfs, const std::string &_root)
Definition: nativevfs.h:81
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:31
std::ifstream file
Definition: nativevfs.h:52
IVirtualFileSystem * GetFileSystem() override
Definition: nativevfs.h:46
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:34
friend NativeFileSystem
Definition: nativevfs.h:48
std::string path
Definition: nativevfs.h:51
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:33
NativeFileSystem *const nfs
Definition: nativevfs.h:55
uint64_t Size() override
Size of file in bytes
Definition: nativevfs.cpp:111
~NativeFile()
Definition: nativevfs.h:36
const std::string & Path() override
Get file path relative to the filesystem.
Definition: nativevfs.cpp:109
int size
Definition: nativevfs.h:53
Definition: nativevfs.h:58
friend NativeDirectory
Definition: nativevfs.h:76
NativeFileSystem(std::string root)
Definition: nativevfs.cpp:27
void Close(std::shared_ptr< IVirtualFile > &) override
Definition: nativevfs.cpp:62
bool Initialize() override
Definition: nativevfs.h:63
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:29
std::string root
Definition: nativevfs.h:75
bool Exists(const std::string &path) override
Does the file exist
Definition: nativevfs.cpp:105
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
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:72
Offset
Definition: vfs.h:31
FileModes
Definition: vfs.h:26
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