VOX
A little voxel engine
Loading...
Searching...
No Matches
Save.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <fstream>
5#include <cstring>
6#include <unordered_map>
7#include <unordered_set>
8
9#include "logger.hpp"
10#include "Chunk.hpp"
11#include "glm/glm.hpp"
12#include "hashes.hpp"
13
17class Save
18{
19public:
20 constexpr static int REGION_SIZE = 32;
21 static const std::filesystem::path SAVE_DIR;
22 static const std::filesystem::path DEFAULT_NAME;
23 static glm::ivec2 toRegionPos(glm::ivec3 chunkPos3D);
24 Save();
25 Save(const std::filesystem::path & path);
26 ~Save();
27
35 void trackChunk(std::shared_ptr<Chunk> chunk);
36
45 void saveRegion(const glm::ivec2 & position);
46
54 std::shared_ptr<Chunk> getChunk(const glm::ivec3 & position);
55private:
56 std::filesystem::path m_save_dir;
57
66 class Region
67 {
68 public:
69 class CorruptedFileException : public std::exception
70 {
71 public:
72 CorruptedFileException(const std::string & message)
73 : m_message(message) {}
74 const char * what() const noexcept override { return m_message.c_str(); }
75 private:
76 std::string m_message;
77 };
78
86 static glm::ivec2 toRelativePos(const glm::ivec3 & chunkPos3D, const glm::ivec2 & region_pos);
87
94 static size_t getOffsetIndex(const glm::ivec2 & position);
95
102 static std::string toFilename(const glm::ivec2 & position);
103
110 static glm::ivec2 nameToRegionPos(const std::filesystem::path & path);
111
112 Region(
113 const std::filesystem::path & region_dir,
114 const glm::ivec2 & position);
115
116 Region(std::filesystem::path file_path);
117
118 ~Region();
119 Region(const Region & other) = delete;
120 Region(Region && other);
121 Region & operator=(const Region & other) = delete;
122 Region & operator=(Region && other);
123
129 glm::ivec2 getPosition() const { return m_position; }
130
136 std::filesystem::path getPath() const { return m_path; }
137
145 std::shared_ptr<Chunk> getChunk(const glm::ivec3 & chunkPos3D);
146
153 void addChunk(const std::shared_ptr<Chunk> & chunk);
154
158 void save();
159
160 private:
161 std::filesystem::path m_path;
162 std::fstream file;
163 glm::ivec2 m_position;
164 bool m_loaded = false;
165 std::unordered_map<glm::ivec3, const std::shared_ptr<Chunk>> m_chunks;
166 struct ChunkOffset
167 {
172 uint32_t offset;
173
178 uint32_t size;
179 };
180 std::unordered_map<glm::ivec2, ChunkOffset> m_offsets;
181
182 void parseOffsetsTable();
183 void clearOffsetsTable();
184 void writeOffsetsTable();
185 void writeChunks();
186 void load();
187 void readChunk(const glm::ivec2 & relative_position);
188
189 void openFile(const std::filesystem::path & path);
190 void createFile(const std::filesystem::path & path);
191 };
192 //first key if the region position
193 // if we have a region entry
194 //then there is a set to list all the chunks in the save from that region
195 //TODO add a way to know if region is full to avoid a list of chunks
196 typedef std::unordered_map<glm::ivec2, Region> RegionMap;
197
198 RegionMap m_regions;
199 std::unordered_set<std::shared_ptr<Chunk>> m_chunkPool;
200
201
202 /****************\
203 * INIT
204 \****************/
205 // open save directory
206 // open a region file
207 void initRegions();
208};
CorruptedFileException(const std::string &message)
Definition: Save.hpp:72
const char * what() const noexcept override
Definition: Save.hpp:74
a class to save and load chunks from the disk
Definition: Save.hpp:18
static const std::filesystem::path SAVE_DIR
Definition: Save.hpp:21
void saveRegion(const glm::ivec2 &position)
Unloads the region and saves it to the disk.
Definition: Save.cpp:67
~Save()
Definition: Save.cpp:26
static const std::filesystem::path DEFAULT_NAME
Definition: Save.hpp:22
void trackChunk(std::shared_ptr< Chunk > chunk)
add chunk to the list of chunks to save
Definition: Save.cpp:51
static glm::ivec2 toRegionPos(glm::ivec3 chunkPos3D)
Definition: SaveRegion.cpp:46
static constexpr int REGION_SIZE
Definition: Save.hpp:20
std::shared_ptr< Chunk > getChunk(const glm::ivec3 &position)
Tries to load a chunk from the disk.
Definition: Save.cpp:76
Save()
Definition: Save.cpp:6