6#include <unordered_map>
7#include <unordered_set>
23 static glm::ivec2
toRegionPos(glm::ivec3 chunkPos3D);
25 Save(
const std::filesystem::path & path);
54 std::shared_ptr<Chunk>
getChunk(
const glm::ivec3 & position);
56 std::filesystem::path m_save_dir;
73 : m_message(message) {}
74 const char *
what() const noexcept
override {
return m_message.c_str(); }
76 std::string m_message;
86 static glm::ivec2 toRelativePos(
const glm::ivec3 & chunkPos3D,
const glm::ivec2 & region_pos);
94 static size_t getOffsetIndex(
const glm::ivec2 & position);
102 static std::string toFilename(
const glm::ivec2 & position);
110 static glm::ivec2 nameToRegionPos(
const std::filesystem::path & path);
113 const std::filesystem::path & region_dir,
114 const glm::ivec2 & position);
116 Region(std::filesystem::path file_path);
119 Region(
const Region & other) =
delete;
120 Region(Region && other);
121 Region & operator=(
const Region & other) =
delete;
122 Region & operator=(Region && other);
129 glm::ivec2 getPosition()
const {
return m_position; }
136 std::filesystem::path getPath()
const {
return m_path; }
145 std::shared_ptr<Chunk> getChunk(
const glm::ivec3 & chunkPos3D);
153 void addChunk(
const std::shared_ptr<Chunk> & chunk);
161 std::filesystem::path m_path;
163 glm::ivec2 m_position;
164 bool m_loaded =
false;
165 std::unordered_map<glm::ivec3, const std::shared_ptr<Chunk>> m_chunks;
180 std::unordered_map<glm::ivec2, ChunkOffset> m_offsets;
182 void parseOffsetsTable();
183 void clearOffsetsTable();
184 void writeOffsetsTable();
187 void readChunk(
const glm::ivec2 & relative_position);
189 void openFile(
const std::filesystem::path & path);
190 void createFile(
const std::filesystem::path & path);
196 typedef std::unordered_map<glm::ivec2, Region> RegionMap;
199 std::unordered_set<std::shared_ptr<Chunk>> m_chunkPool;
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