Skip to content

Commit

Permalink
little bit of cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
afaure42 authored and SaumonDesMers committed Nov 15, 2024
1 parent 395ce66 commit 143cce9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/app/world/Save/Save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Save::Save()
if (std::filesystem::exists(m_save_dir))
initRegions();
else
ret = std::filesystem::create_directories(m_save_dir);
if (ret == false)
throw std::runtime_error("Save: Save: error creating save directory: " + m_save_dir.string());
{
if(!std::filesystem::create_directories(m_save_dir))
throw std::runtime_error("Save: Save: error creating save directory: " + m_save_dir.string());
}
}

Save::Save(const std::filesystem::path & path)
:m_save_dir(path)
{
bool ret = false;

initRegions();
}
Expand Down
22 changes: 19 additions & 3 deletions src/app/world/Save/Save.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ class Save
*/
static size_t getOffsetIndex(const glm::ivec2 & position);

/**
* @brief converts a region pos to a filname with the format r.x.z.ftmc
*
* @param position
* @return std::string
*/
static std::string toFilename(const glm::ivec2 & position);

/**
* @brief extracts the region position from a filename
*
* @param path
* @return glm::ivec2
*/
static glm::ivec2 nameToRegionPos(const std::filesystem::path & path);

Region(
const std::filesystem::path & region_dir,
const glm::ivec2 & position);
Expand Down Expand Up @@ -163,9 +179,9 @@ class Save
};
std::unordered_map<glm::ivec2, ChunkOffset> m_offsets;

void parseOffsets();
void clearOffsets();
void writeOffsets();
void parseOffsetsTable();
void clearOffsetsTable();
void writeOffsetsTable();
void writeChunks();
void load();
void readChunk(const glm::ivec2 & relative_position);
Expand Down
50 changes: 29 additions & 21 deletions src/app/world/Save/SaveRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ static inline size_t byteSize(size_t size)
return size * 4096;
}

std::string Save::Region::toFilename(const glm::ivec2 & position)
{
return "r." + std::to_string(position.x) + "." + std::to_string(position.y) + ".ftmc";
}

glm::ivec2 Save::Region::nameToRegionPos(const std::filesystem::path & path)
{
std::string name = path.filename().string();
//name format is r.x.z.ftmc
//todo check format
name = name.substr(2); //remove r
name = name.substr(0, name.find_last_of('.')); //remove extension

//now we have x.z
std::string x = name.substr(0, name.find('.'));
std::string z = name.substr(name.find('.') + 1);

return {std::stoi(x), std::stoi(z)};
}

glm::ivec2 Save::toRegionPos( glm::ivec3 chunkPos3D)
{
glm::ivec2 ret;
Expand Down Expand Up @@ -53,9 +73,7 @@ Save::Region::Region(
{

//create filename
std::string filename = "r."
+ std::to_string(m_position.x) + "."
+ std::to_string(m_position.y) + ".ftmc";
std::string filename = toFilename(position);

m_path = region_dir / filename;

Expand All @@ -73,22 +91,12 @@ Save::Region::Region(std::filesystem::path file_path)
: m_path(file_path)
{
//parse position
std::string name = file_path.filename().string();
//name format is r.x.z.ftmc
//todo check format
name = name.substr(2); //remove r
name = name.substr(0, name.find_last_of('.')); //remove extension

//now we have x.z
std::string x = name.substr(0, name.find('.'));
std::string z = name.substr(name.find('.') + 1);

m_position = glm::ivec2(std::stoi(x), std::stoi(z));
m_position = nameToRegionPos(m_path);

if (std::filesystem::file_size(m_path) < 8192)
throw CorruptedFileException("Save: Region: file too small");
openFile();
parseOffsets();
parseOffsetsTable();
file.close();
}

Expand Down Expand Up @@ -122,22 +130,22 @@ Save::Region::~Region()
void Save::Region::save()
{
openFile();
clearOffsets();
clearOffsetsTable();
writeChunks();
writeOffsets();
writeOffsetsTable();

m_chunks.clear();
m_loaded = false;
file.close();
}

void Save::Region::clearOffsets()
void Save::Region::clearOffsetsTable()
{
file.seekp(0);
file.write(std::string(8192, '\0').c_str(), 8192);
}

void Save::Region::writeOffsets()
void Save::Region::writeOffsetsTable()
{
std::array<char, 8192> table_buffer = {};
char buffer[8];
Expand All @@ -159,7 +167,7 @@ void Save::Region::writeOffsets()
throw std::runtime_error("Save: Region: WriteOffsets: error writing");
}

void Save::Region::parseOffsets()
void Save::Region::parseOffsetsTable()
{
// read all 1024 offsets ( 32 * 32 )
char buffer[8];
Expand Down Expand Up @@ -259,7 +267,7 @@ void Save::Region::addChunk(const std::shared_ptr<Chunk> & chunk)
void Save::Region::load()
{
openFile();
parseOffsets();
parseOffsetsTable();
for(auto & [pos, offset] : m_offsets)
readChunk(pos);
m_loaded = true;
Expand Down

0 comments on commit 143cce9

Please sign in to comment.