format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+20 -19
View File
@@ -1,6 +1,9 @@
#include "WorldConverter.hpp"
#include "WorldFiles.hpp"
#include <iostream>
#include <memory>
#include <stdexcept>
#include <utility>
#include "../content/ContentLUT.hpp"
#include "../data/dynamic.hpp"
@@ -9,11 +12,7 @@
#include "../objects/Player.hpp"
#include "../util/ThreadPool.hpp"
#include "../voxels/Chunk.hpp"
#include <memory>
#include <iostream>
#include <stdexcept>
#include <utility>
#include "WorldFiles.hpp"
namespace fs = std::filesystem;
@@ -22,8 +21,9 @@ static debug::Logger logger("world-converter");
class ConverterWorker : public util::Worker<convert_task, int> {
std::shared_ptr<WorldConverter> converter;
public:
ConverterWorker(std::shared_ptr<WorldConverter> converter)
: converter(std::move(converter)) {}
ConverterWorker(std::shared_ptr<WorldConverter> converter)
: converter(std::move(converter)) {
}
int operator()(const std::shared_ptr<convert_task>& task) override {
converter->convert(*task);
@@ -33,18 +33,20 @@ public:
WorldConverter::WorldConverter(
const fs::path& folder,
const Content* content,
const Content* content,
std::shared_ptr<ContentLUT> lut
) : wfile(std::make_unique<WorldFiles>(folder)),
lut(std::move(lut)),
content(content)
{
fs::path regionsFolder = wfile->getRegions().getRegionsFolder(REGION_LAYER_VOXELS);
)
: wfile(std::make_unique<WorldFiles>(folder)),
lut(std::move(lut)),
content(content) {
fs::path regionsFolder =
wfile->getRegions().getRegionsFolder(REGION_LAYER_VOXELS);
if (!fs::is_directory(regionsFolder)) {
logger.error() << "nothing to convert";
return;
}
tasks.push(convert_task {convert_task_type::player, wfile->getPlayerFile()});
tasks.push(convert_task {convert_task_type::player, wfile->getPlayerFile()}
);
for (const auto& file : fs::directory_iterator(regionsFolder)) {
tasks.push(convert_task {convert_task_type::region, file.path()});
}
@@ -55,7 +57,7 @@ WorldConverter::~WorldConverter() {
std::shared_ptr<Task> WorldConverter::startTask(
const fs::path& folder,
const Content* content,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,
bool multithreading
@@ -70,7 +72,7 @@ std::shared_ptr<Task> WorldConverter::startTask(
}
auto pool = std::make_shared<util::ThreadPool<convert_task, int>>(
"converter-pool",
[=](){return std::make_shared<ConverterWorker>(converter);},
[=]() { return std::make_shared<ConverterWorker>(converter); },
[=](int&) {}
);
auto& converterTasks = converter->tasks;
@@ -111,8 +113,7 @@ void WorldConverter::convertPlayer(const fs::path& file) const {
}
void WorldConverter::convert(const convert_task& task) const {
if (!fs::is_regular_file(task.file))
return;
if (!fs::is_regular_file(task.file)) return;
switch (task.type) {
case convert_task_type::region:
+7 -9
View File
@@ -1,13 +1,13 @@
#ifndef FILES_WORLD_CONVERTER_HPP_
#define FILES_WORLD_CONVERTER_HPP_
#include <queue>
#include <memory>
#include <filesystem>
#include <memory>
#include <queue>
#include "../typedefs.hpp"
#include "../delegates.hpp"
#include "../interfaces/Task.hpp"
#include "../typedefs.hpp"
namespace fs = std::filesystem;
@@ -15,9 +15,7 @@ class Content;
class ContentLUT;
class WorldFiles;
enum class convert_task_type {
region, player
};
enum class convert_task_type { region, player };
struct convert_task {
convert_task_type type;
@@ -37,7 +35,7 @@ class WorldConverter : public Task {
public:
WorldConverter(
const fs::path& folder,
const Content* content,
const Content* content,
std::shared_ptr<ContentLUT> lut
);
~WorldConverter();
@@ -56,11 +54,11 @@ public:
static std::shared_ptr<Task> startTask(
const fs::path& folder,
const Content* content,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,
bool multithreading
);
};
#endif // FILES_WORLD_CONVERTER_HPP_
#endif // FILES_WORLD_CONVERTER_HPP_
+27 -29
View File
@@ -1,5 +1,13 @@
#include "WorldFiles.hpp"
#include <cassert>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
#include "../coders/byte_utils.hpp"
#include "../coders/json.hpp"
#include "../constants.hpp"
@@ -11,11 +19,11 @@
#include "../items/ItemDef.hpp"
#include "../lighting/Lightmap.hpp"
#include "../maths/voxmaths.hpp"
#include "../objects/Player.hpp"
#include "../objects/EntityDef.hpp"
#include "../objects/Player.hpp"
#include "../physics/Hitbox.hpp"
#include "../typedefs.hpp"
#include "../settings.hpp"
#include "../typedefs.hpp"
#include "../util/data_io.hpp"
#include "../voxels/Block.hpp"
#include "../voxels/Chunk.hpp"
@@ -23,24 +31,16 @@
#include "../window/Camera.hpp"
#include "../world/World.hpp"
#include <cassert>
#include <iostream>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <cstring>
#include <utility>
#define WORLD_FORMAT_MAGIC ".VOXWLD"
static debug::Logger logger("world-files");
WorldFiles::WorldFiles(const fs::path& directory) : directory(directory), regions(directory) {
WorldFiles::WorldFiles(const fs::path& directory)
: directory(directory), regions(directory) {
}
WorldFiles::WorldFiles(const fs::path& directory, const DebugSettings& settings)
: WorldFiles(directory)
{
: WorldFiles(directory) {
generatorTestMode = settings.generatorTestMode.get();
doWriteLights = settings.doWriteLights.get();
regions.generatorTestMode = generatorTestMode;
@@ -55,23 +55,23 @@ void WorldFiles::createDirectories() {
}
fs::path WorldFiles::getPlayerFile() const {
return directory/fs::path("player.json");
return directory / fs::path("player.json");
}
fs::path WorldFiles::getResourcesFile() const {
return directory/fs::path("resources.json");
return directory / fs::path("resources.json");
}
fs::path WorldFiles::getWorldFile() const {
return directory/fs::path(WORLD_FILE);
return directory / fs::path(WORLD_FILE);
}
fs::path WorldFiles::getIndicesFile() const {
return directory/fs::path("indices.json");
return directory / fs::path("indices.json");
}
fs::path WorldFiles::getPacksFile() const {
return directory/fs::path("packs.list");
return directory / fs::path("packs.list");
}
void WorldFiles::write(const World* world, const Content* content) {
@@ -84,7 +84,7 @@ void WorldFiles::write(const World* world, const Content* content) {
if (generatorTestMode) {
return;
}
writeIndices(content->getIndices());
regions.write();
}
@@ -99,8 +99,10 @@ void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
files::write_string(packsFile, ss.str());
}
template<class T>
static void write_indices(const ContentUnitIndices<T>& indices, dynamic::List& list) {
template <class T>
static void write_indices(
const ContentUnitIndices<T>& indices, dynamic::List& list
) {
size_t count = indices.count();
for (size_t i = 0; i < count; i++) {
list.put(indices.get(i)->name);
@@ -131,9 +133,7 @@ bool WorldFiles::readWorldInfo(World* world) {
}
static void read_resources_data(
const Content* content,
const dynamic::List_sptr& list,
ResourceType type
const Content* content, const dynamic::List_sptr& list, ResourceType type
) {
const auto& indices = content->getIndices(type);
for (size_t i = 0; i < list->size(); i++) {
@@ -169,12 +169,11 @@ bool WorldFiles::readResourcesData(const Content* content) {
}
static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
auto prefix = id+":";
auto prefix = id + ":";
auto blocks = root->list("blocks");
for (uint i = 0; i < blocks->size(); i++) {
auto name = blocks->str(i);
if (name.find(prefix) != 0)
continue;
if (name.find(prefix) != 0) continue;
auto value = blocks->getValueWriteable(i);
*value = CORE_AIR;
}
@@ -182,8 +181,7 @@ static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
auto items = root->list("items");
for (uint i = 0; i < items->size(); i++) {
auto name = items->str(i);
if (name.find(prefix) != 0)
continue;
if (name.find(prefix) != 0) continue;
auto value = items->getValueWriteable(i);
*value = CORE_EMPTY;
}
+12 -14
View File
@@ -1,19 +1,17 @@
#ifndef FILES_WORLD_FILES_HPP_
#define FILES_WORLD_FILES_HPP_
#include "WorldRegions.hpp"
#include "files.hpp"
#include "../typedefs.hpp"
#include "../content/ContentPack.hpp"
#include "../voxels/Chunk.hpp"
#include <vector>
#include <string>
#include <memory>
#include <filesystem>
#include <glm/glm.hpp>
#include <memory>
#include <string>
#include <vector>
#include "../content/ContentPack.hpp"
#include "../typedefs.hpp"
#include "../voxels/Chunk.hpp"
#include "WorldRegions.hpp"
#include "files.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/hash.hpp"
@@ -41,8 +39,8 @@ class WorldFiles {
void writeWorldInfo(const World* world);
void writeIndices(const ContentIndices* indices);
public:
WorldFiles(const fs::path &directory);
WorldFiles(const fs::path &directory, const DebugSettings& settings);
WorldFiles(const fs::path& directory);
WorldFiles(const fs::path& directory, const DebugSettings& settings);
~WorldFiles();
fs::path getPlayerFile() const;
@@ -75,4 +73,4 @@ public:
}
};
#endif // FILES_WORLD_FILES_HPP_
#endif // FILES_WORLD_FILES_HPP_
+101 -55
View File
@@ -1,5 +1,9 @@
#include "WorldRegions.hpp"
#include <cstring>
#include <utility>
#include <vector>
#include "../coders/byte_utils.hpp"
#include "../coders/rle.hpp"
#include "../data/dynamic.hpp"
@@ -7,10 +11,6 @@
#include "../maths/voxmaths.hpp"
#include "../util/data_io.hpp"
#include <cstring>
#include <utility>
#include <vector>
#define REGION_FORMAT_MAGIC ".VOXREG"
regfile::regfile(fs::path filename) : file(std::move(filename)) {
@@ -18,15 +18,16 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) {
throw std::runtime_error("incomplete region file header");
char header[REGION_HEADER_SIZE];
file.read(header, REGION_HEADER_SIZE);
// avoid of use strcmp_s
if (std::string(header, strlen(REGION_FORMAT_MAGIC)) != REGION_FORMAT_MAGIC) {
if (std::string(header, strlen(REGION_FORMAT_MAGIC)) !=
REGION_FORMAT_MAGIC) {
throw std::runtime_error("invalid region file magic number");
}
version = header[8];
if (uint(version) > REGION_FORMAT_VERSION) {
throw illegal_region_format(
"region format "+std::to_string(version)+" is not supported"
"region format " + std::to_string(version) + " is not supported"
);
}
}
@@ -39,7 +40,7 @@ std::unique_ptr<ubyte[]> regfile::read(int index, uint32_t& length) {
file.seekg(table_offset + index * 4);
file.read(reinterpret_cast<char*>(&offset), 4);
offset = dataio::read_int32_big(reinterpret_cast<const ubyte*>(&offset), 0);
if (offset == 0){
if (offset == 0) {
return nullptr;
}
@@ -52,9 +53,11 @@ std::unique_ptr<ubyte[]> regfile::read(int index, uint32_t& length) {
}
WorldRegion::WorldRegion()
: chunksData(std::make_unique<std::unique_ptr<ubyte[]>[]>(REGION_CHUNKS_COUNT)),
sizes(std::make_unique<uint32_t[]>(REGION_CHUNKS_COUNT))
{}
: chunksData(
std::make_unique<std::unique_ptr<ubyte[]>[]>(REGION_CHUNKS_COUNT)
),
sizes(std::make_unique<uint32_t[]>(REGION_CHUNKS_COUNT)) {
}
WorldRegion::~WorldRegion() = default;
@@ -88,13 +91,14 @@ uint WorldRegion::getChunkDataSize(uint x, uint z) {
}
WorldRegions::WorldRegions(const fs::path& directory) : directory(directory) {
for (size_t i = 0; i < sizeof(layers)/sizeof(RegionsLayer); i++) {
for (size_t i = 0; i < sizeof(layers) / sizeof(RegionsLayer); i++) {
layers[i].layer = i;
}
layers[REGION_LAYER_VOXELS].folder = directory/fs::path("regions");
layers[REGION_LAYER_LIGHTS].folder = directory/fs::path("lights");
layers[REGION_LAYER_INVENTORIES].folder = directory/fs::path("inventories");
layers[REGION_LAYER_ENTITIES].folder = directory/fs::path("entities");
layers[REGION_LAYER_VOXELS].folder = directory / fs::path("regions");
layers[REGION_LAYER_LIGHTS].folder = directory / fs::path("lights");
layers[REGION_LAYER_INVENTORIES].folder =
directory / fs::path("inventories");
layers[REGION_LAYER_ENTITIES].folder = directory / fs::path("entities");
}
WorldRegions::~WorldRegions() = default;
@@ -121,10 +125,12 @@ WorldRegion* WorldRegions::getOrCreateRegion(int x, int z, int layer) {
return region;
}
std::unique_ptr<ubyte[]> WorldRegions::compress(const ubyte* src, size_t srclen, size_t& len) {
std::unique_ptr<ubyte[]> WorldRegions::compress(
const ubyte* src, size_t srclen, size_t& len
) {
auto buffer = bufferPool.get();
auto bytes = buffer.get();
len = extrle::encode(src, srclen, bytes);
auto data = std::make_unique<ubyte[]>(len);
for (size_t i = 0; i < len; i++) {
@@ -133,7 +139,9 @@ std::unique_ptr<ubyte[]> WorldRegions::compress(const ubyte* src, size_t srclen,
return data;
}
std::unique_ptr<ubyte[]> WorldRegions::decompress(const ubyte* src, size_t srclen, size_t dstlen) {
std::unique_ptr<ubyte[]> WorldRegions::decompress(
const ubyte* src, size_t srclen, size_t dstlen
) {
auto decompressed = std::make_unique<ubyte[]>(dstlen);
extrle::decode(src, srclen, decompressed.get());
return decompressed;
@@ -157,8 +165,10 @@ std::unique_ptr<ubyte[]> WorldRegions::readChunkData(
return rfile->read(chunkIndex, length);
}
/// @brief Read missing chunks data (null pointers) from region file
void WorldRegions::fetchChunks(WorldRegion* region, int x, int z, regfile* file) {
/// @brief Read missing chunks data (null pointers) from region file
void WorldRegions::fetchChunks(
WorldRegion* region, int x, int z, regfile* file
) {
auto* chunks = region->getChunks();
uint32_t* sizes = region->getSizes();
@@ -226,7 +236,8 @@ regfile_ptr WorldRegions::getRegFile(glm::ivec3 coord, bool create) {
}
regfile_ptr WorldRegions::createRegFile(glm::ivec3 coord) {
fs::path file = layers[coord[2]].folder/getRegionFilename(coord[0], coord[1]);
fs::path file =
layers[coord[2]].folder / getRegionFilename(coord[0], coord[1]);
if (!fs::exists(file)) {
return nullptr;
}
@@ -261,8 +272,8 @@ fs::path WorldRegions::getRegionFilename(int x, int z) const {
return fs::path(std::to_string(x) + "_" + std::to_string(z) + ".bin");
}
void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){
fs::path filename = layers[layer].folder/getRegionFilename(x, z);
void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry) {
fs::path filename = layers[layer].folder / getRegionFilename(x, z);
glm::ivec3 regcoord(x, z, layer);
if (auto regfile = getRegFile(regcoord, false)) {
@@ -272,29 +283,31 @@ void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){
regfile.reset();
closeRegFile(regcoord);
}
char header[REGION_HEADER_SIZE] = REGION_FORMAT_MAGIC;
header[8] = REGION_FORMAT_VERSION;
header[9] = 0; // flags
header[9] = 0; // flags
std::ofstream file(filename, std::ios::out | std::ios::binary);
file.write(header, REGION_HEADER_SIZE);
size_t offset = REGION_HEADER_SIZE;
char intbuf[4]{};
uint offsets[REGION_CHUNKS_COUNT]{};
char intbuf[4] {};
uint offsets[REGION_CHUNKS_COUNT] {};
auto* region = entry->getChunks();
uint32_t* sizes = entry->getSizes();
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
ubyte* chunk = region[i].get();
if (chunk == nullptr){
if (chunk == nullptr) {
offsets[i] = 0;
} else {
offsets[i] = offset;
size_t compressedSize = sizes[i];
dataio::write_int32_big(compressedSize, reinterpret_cast<ubyte*>(intbuf), 0);
dataio::write_int32_big(
compressedSize, reinterpret_cast<ubyte*>(intbuf), 0
);
offset += 4 + compressedSize;
file.write(intbuf, 4);
@@ -302,13 +315,15 @@ void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){
}
}
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
dataio::write_int32_big(offsets[i], reinterpret_cast<ubyte*>(intbuf), 0);
dataio::write_int32_big(
offsets[i], reinterpret_cast<ubyte*>(intbuf), 0
);
file.write(intbuf, 4);
}
}
void WorldRegions::writeRegions(int layer) {
for (auto& it : layers[layer].regions){
for (auto& it : layers[layer].regions) {
WorldRegion* region = it.second.get();
if (region->getChunks() == nullptr || !region->isUnsaved()) {
continue;
@@ -318,7 +333,14 @@ void WorldRegions::writeRegions(int layer) {
}
}
void WorldRegions::put(int x, int z, int layer, std::unique_ptr<ubyte[]> data, size_t size, bool rle) {
void WorldRegions::put(
int x,
int z,
int layer,
std::unique_ptr<ubyte[]> data,
size_t size,
bool rle
) {
if (rle) {
size_t compressedSize;
auto compressed = compress(data.get(), size, compressedSize);
@@ -333,7 +355,9 @@ void WorldRegions::put(int x, int z, int layer, std::unique_ptr<ubyte[]> data, s
region->put(localX, localZ, data.release(), size);
}
static std::unique_ptr<ubyte[]> write_inventories(Chunk* chunk, uint& datasize) {
static std::unique_ptr<ubyte[]> write_inventories(
Chunk* chunk, uint& datasize
) {
auto& inventories = chunk->inventories;
ByteBuilder builder;
builder.putInt32(inventories.size());
@@ -343,7 +367,7 @@ static std::unique_ptr<ubyte[]> write_inventories(Chunk* chunk, uint& datasize)
auto bytes = json::to_binary(map.get(), true);
builder.putInt32(bytes.size());
builder.put(bytes.data(), bytes.size());
}
}
auto datavec = builder.data();
datasize = builder.size();
auto data = std::make_unique<ubyte[]>(datasize);
@@ -352,7 +376,7 @@ static std::unique_ptr<ubyte[]> write_inventories(Chunk* chunk, uint& datasize)
}
/// @brief Store chunk data (voxels and lights) in region (existing or new)
void WorldRegions::put(Chunk* chunk, std::vector<ubyte> entitiesData){
void WorldRegions::put(Chunk* chunk, std::vector<ubyte> entitiesData) {
assert(chunk != nullptr);
if (!chunk->flags.lighted) {
return;
@@ -365,20 +389,32 @@ void WorldRegions::put(Chunk* chunk, std::vector<ubyte> entitiesData){
int regionX, regionZ, localX, localZ;
calc_reg_coords(chunk->x, chunk->z, regionX, regionZ, localX, localZ);
put(chunk->x, chunk->z, REGION_LAYER_VOXELS,
chunk->encode(), CHUNK_DATA_LEN, true);
put(chunk->x,
chunk->z,
REGION_LAYER_VOXELS,
chunk->encode(),
CHUNK_DATA_LEN,
true);
// Writing lights cache
if (doWriteLights && chunk->flags.lighted) {
put(chunk->x, chunk->z, REGION_LAYER_LIGHTS,
chunk->lightmap.encode(), LIGHTMAP_DATA_LEN, true);
put(chunk->x,
chunk->z,
REGION_LAYER_LIGHTS,
chunk->lightmap.encode(),
LIGHTMAP_DATA_LEN,
true);
}
// Writing block inventories
if (!chunk->inventories.empty()) {
uint datasize;
auto data = write_inventories(chunk, datasize);
put(chunk->x, chunk->z, REGION_LAYER_INVENTORIES,
std::move(data), datasize, false);
put(chunk->x,
chunk->z,
REGION_LAYER_INVENTORIES,
std::move(data),
datasize,
false);
}
// Writing entities
if (!entitiesData.empty()) {
@@ -386,12 +422,16 @@ void WorldRegions::put(Chunk* chunk, std::vector<ubyte> entitiesData){
for (size_t i = 0; i < entitiesData.size(); i++) {
data[i] = entitiesData[i];
}
put(chunk->x, chunk->z, REGION_LAYER_ENTITIES,
std::move(data), entitiesData.size(), false);
put(chunk->x,
chunk->z,
REGION_LAYER_ENTITIES,
std::move(data),
entitiesData.size(),
false);
}
}
std::unique_ptr<ubyte[]> WorldRegions::getChunk(int x, int z){
std::unique_ptr<ubyte[]> WorldRegions::getChunk(int x, int z) {
uint32_t size;
auto* data = getData(x, z, REGION_LAYER_VOXELS, size);
if (data == nullptr) {
@@ -400,7 +440,7 @@ std::unique_ptr<ubyte[]> WorldRegions::getChunk(int x, int z){
return decompress(data, size, CHUNK_DATA_LEN);
}
/// @brief Get cached lights for chunk at x,z
/// @brief Get cached lights for chunk at x,z
/// @return lights data or nullptr
std::unique_ptr<light_t[]> WorldRegions::getLights(int x, int z) {
uint32_t size;
@@ -433,7 +473,7 @@ chunk_inventories_map WorldRegions::fetchInventories(int x, int z) {
return meta;
}
dynamic::Map_sptr WorldRegions::fetchEntities(int x, int z) {
dynamic::Map_sptr WorldRegions::fetchEntities(int x, int z) {
uint32_t bytesSize;
const ubyte* data = getData(x, z, REGION_LAYER_ENTITIES, bytesSize);
if (data == nullptr) {
@@ -444,7 +484,7 @@ chunk_inventories_map WorldRegions::fetchInventories(int x, int z) {
return nullptr;
}
return map;
}
}
void WorldRegions::processRegionVoxels(int x, int z, const regionproc& func) {
if (getRegion(x, z, REGION_LAYER_VOXELS)) {
@@ -465,7 +505,12 @@ void WorldRegions::processRegionVoxels(int x, int z, const regionproc& func) {
}
data = decompress(data.get(), length, CHUNK_DATA_LEN);
if (func(data.get())) {
put(gx, gz, REGION_LAYER_VOXELS, std::move(data), CHUNK_DATA_LEN, true);
put(gx,
gz,
REGION_LAYER_VOXELS,
std::move(data),
CHUNK_DATA_LEN,
true);
}
}
}
@@ -475,7 +520,6 @@ fs::path WorldRegions::getRegionsFolder(int layer) const {
return layers[layer].folder;
}
void WorldRegions::write() {
for (auto& layer : layers) {
fs::create_directories(layer.folder);
@@ -483,14 +527,16 @@ void WorldRegions::write() {
}
}
bool WorldRegions::parseRegionFilename(const std::string& name, int& x, int& z) {
bool WorldRegions::parseRegionFilename(
const std::string& name, int& x, int& z
) {
size_t sep = name.find('_');
if (sep == std::string::npos || sep == 0 || sep == name.length()-1) {
if (sep == std::string::npos || sep == 0 || sep == name.length() - 1) {
return false;
}
try {
x = std::stoi(name.substr(0, sep));
z = std::stoi(name.substr(sep+1));
z = std::stoi(name.substr(sep + 1));
} catch (std::invalid_argument& err) {
return false;
} catch (std::out_of_range& err) {
+40 -28
View File
@@ -1,20 +1,19 @@
#ifndef FILES_WORLD_REGIONS_HPP_
#define FILES_WORLD_REGIONS_HPP_
#include "files.hpp"
#include <condition_variable>
#include <filesystem>
#include <functional>
#include <glm/glm.hpp>
#include <memory>
#include <mutex>
#include <unordered_map>
#include "../data/dynamic_fwd.hpp"
#include "../typedefs.hpp"
#include "../util/BufferPool.hpp"
#include "../voxels/Chunk.hpp"
#include "../data/dynamic_fwd.hpp"
#include <mutex>
#include <memory>
#include <functional>
#include <filesystem>
#include <unordered_map>
#include <condition_variable>
#include <glm/glm.hpp>
#include "files.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/hash.hpp"
@@ -35,8 +34,9 @@ inline constexpr uint MAX_OPEN_REGION_FILES = 16;
class illegal_region_format : public std::runtime_error {
public:
illegal_region_format(const std::string& message)
: std::runtime_error(message) {}
illegal_region_format(const std::string& message)
: std::runtime_error(message) {
}
};
class WorldRegion {
@@ -83,14 +83,14 @@ class regfile_ptr {
regfile* file;
std::condition_variable* cv;
public:
regfile_ptr(
regfile* file,
std::condition_variable* cv
) : file(file), cv(cv) {}
regfile_ptr(regfile* file, std::condition_variable* cv)
: file(file), cv(cv) {
}
regfile_ptr(const regfile_ptr&) = delete;
regfile_ptr(std::nullptr_t) : file(nullptr), cv(nullptr) {}
regfile_ptr(std::nullptr_t) : file(nullptr), cv(nullptr) {
}
bool operator==(std::nullptr_t) const {
return file == nullptr;
@@ -123,8 +123,7 @@ class WorldRegions {
std::condition_variable regFilesCv;
RegionsLayer layers[4] {};
util::BufferPool<ubyte> bufferPool {
std::max(CHUNK_DATA_LEN, LIGHTMAP_DATA_LEN) * 2
};
std::max(CHUNK_DATA_LEN, LIGHTMAP_DATA_LEN) * 2};
WorldRegion* getRegion(int x, int z, int layer);
WorldRegion* getOrCreateRegion(int x, int z, int layer);
@@ -134,22 +133,28 @@ class WorldRegions {
/// @param srclen length of the source buffer
/// @param len (out argument) length of result buffer
/// @return compressed bytes array
std::unique_ptr<ubyte[]> compress(const ubyte* src, size_t srclen, size_t& len);
std::unique_ptr<ubyte[]> compress(
const ubyte* src, size_t srclen, size_t& len
);
/// @brief Decompress buffer with extrle
/// @param src compressed buffer
/// @param srclen length of compressed buffer
/// @param dstlen max expected length of source buffer
/// @return decompressed bytes array
std::unique_ptr<ubyte[]> decompress(const ubyte* src, size_t srclen, size_t dstlen);
std::unique_ptr<ubyte[]> decompress(
const ubyte* src, size_t srclen, size_t dstlen
);
std::unique_ptr<ubyte[]> readChunkData(int x, int y, uint32_t& length, regfile* file);
std::unique_ptr<ubyte[]> readChunkData(
int x, int y, uint32_t& length, regfile* file
);
void fetchChunks(WorldRegion* region, int x, int y, regfile* file);
ubyte* getData(int x, int z, int layer, uint32_t& size);
regfile_ptr getRegFile(glm::ivec3 coord, bool create=true);
regfile_ptr getRegFile(glm::ivec3 coord, bool create = true);
void closeRegFile(glm::ivec3 coord);
regfile_ptr useRegFile(glm::ivec3 coord);
regfile_ptr createRegFile(glm::ivec3 coord);
@@ -174,19 +179,26 @@ public:
/// @brief Put all chunk data to regions
void put(Chunk* chunk, std::vector<ubyte> entitiesData);
/// @brief Store data in specified region
/// @brief Store data in specified region
/// @param x chunk.x
/// @param z chunk.z
/// @param layer regions layer
/// @param data target data
/// @param size data size
/// @param rle compress with ext-RLE
void put(int x, int z, int layer, std::unique_ptr<ubyte[]> data, size_t size, bool rle);
void put(
int x,
int z,
int layer,
std::unique_ptr<ubyte[]> data,
size_t size,
bool rle
);
std::unique_ptr<ubyte[]> getChunk(int x, int z);
std::unique_ptr<light_t[]> getLights(int x, int z);
chunk_inventories_map fetchInventories(int x, int z);
dynamic::Map_sptr fetchEntities(int x, int z);
dynamic::Map_sptr fetchEntities(int x, int z);
void processRegionVoxels(int x, int z, const regionproc& func);
@@ -202,4 +214,4 @@ public:
static bool parseRegionFilename(const std::string& name, int& x, int& y);
};
#endif // FILES_WORLD_REGIONS_HPP_
#endif // FILES_WORLD_REGIONS_HPP_
+44 -45
View File
@@ -1,13 +1,13 @@
#include "engine_paths.hpp"
#include <stack>
#include <sstream>
#include <filesystem>
#include <algorithm>
#include <filesystem>
#include <sstream>
#include <stack>
#include <utility>
#include "../util/stringutil.hpp"
#include "../typedefs.hpp"
#include "../util/stringutil.hpp"
#include "WorldFiles.hpp"
const fs::path SCREENSHOTS_FOLDER {"screenshots"};
@@ -16,7 +16,7 @@ const fs::path CONTROLS_FILE {"controls.toml"};
const fs::path SETTINGS_FILE {"settings.toml"};
void EnginePaths::prepare() {
fs::path contentFolder = userfiles/fs::path(CONTENT_FOLDER);
fs::path contentFolder = userfiles / fs::path(CONTENT_FOLDER);
if (!fs::is_directory(contentFolder)) {
fs::create_directories(contentFolder);
}
@@ -31,7 +31,7 @@ fs::path EnginePaths::getResources() const {
}
fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
fs::path folder = userfiles/fs::path(SCREENSHOTS_FOLDER);
fs::path folder = userfiles / fs::path(SCREENSHOTS_FOLDER);
if (!fs::is_directory(folder)) {
fs::create_directory(folder);
}
@@ -44,17 +44,21 @@ fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
ss << std::put_time(&tm, format);
std::string datetimestr = ss.str();
fs::path filename = folder/fs::u8path("screenshot-"+datetimestr+"."+ext);
fs::path filename =
folder / fs::u8path("screenshot-" + datetimestr + "." + ext);
uint index = 0;
while (fs::exists(filename)) {
filename = folder/fs::u8path("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
filename = folder / fs::u8path(
"screenshot-" + datetimestr + "-" +
std::to_string(index) + "." + ext
);
index++;
}
return filename;
}
fs::path EnginePaths::getWorldsFolder() {
return userfiles/fs::path("worlds");
return userfiles / fs::path("worlds");
}
fs::path EnginePaths::getWorldFolder() {
@@ -62,45 +66,44 @@ fs::path EnginePaths::getWorldFolder() {
}
fs::path EnginePaths::getWorldFolder(const std::string& name) {
return getWorldsFolder()/fs::path(name);
return getWorldsFolder() / fs::path(name);
}
fs::path EnginePaths::getControlsFile() {
return userfiles/fs::path(CONTROLS_FILE);
return userfiles / fs::path(CONTROLS_FILE);
}
fs::path EnginePaths::getSettingsFile() {
return userfiles/fs::path(SETTINGS_FILE);
return userfiles / fs::path(SETTINGS_FILE);
}
std::vector<fs::path> EnginePaths::scanForWorlds() {
std::vector<fs::path> folders;
fs::path folder = getWorldsFolder();
if (!fs::is_directory(folder))
return folders;
if (!fs::is_directory(folder)) return folders;
for (const auto& entry : fs::directory_iterator(folder)) {
if (!entry.is_directory()) {
continue;
}
const fs::path& worldFolder = entry.path();
fs::path worldFile = worldFolder/fs::u8path(WorldFiles::WORLD_FILE);
fs::path worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE);
if (!fs::is_regular_file(worldFile)) {
continue;
}
folders.push_back(worldFolder);
}
std::sort(folders.begin(), folders.end(), [](fs::path a, fs::path b) {
a = a/fs::u8path(WorldFiles::WORLD_FILE);
b = b/fs::u8path(WorldFiles::WORLD_FILE);
a = a / fs::u8path(WorldFiles::WORLD_FILE);
b = b / fs::u8path(WorldFiles::WORLD_FILE);
return fs::last_write_time(a) > fs::last_write_time(b);
});
return folders;
}
bool EnginePaths::isWorldNameUsed(const std::string& name) {
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
return fs::exists(EnginePaths::getWorldsFolder() / fs::u8path(name));
}
void EnginePaths::setUserfiles(fs::path folder) {
@@ -125,8 +128,7 @@ static fs::path toCanonic(fs::path path) {
while (true) {
parts.push(path.filename().u8string());
path = path.parent_path();
if (path.empty())
break;
if (path.empty()) break;
}
path = fs::u8path("");
while (!parts.empty()) {
@@ -150,38 +152,38 @@ fs::path EnginePaths::resolve(const std::string& path, bool throwErr) {
throw files_access_error("no entry point specified");
}
std::string prefix = path.substr(0, separator);
std::string filename = path.substr(separator+1);
std::string filename = path.substr(separator + 1);
filename = toCanonic(fs::u8path(filename)).u8string();
if (prefix == "res" || prefix == "core") {
return resources/fs::u8path(filename);
return resources / fs::u8path(filename);
}
if (prefix == "user") {
return userfiles/fs::u8path(filename);
return userfiles / fs::u8path(filename);
}
if (prefix == "world") {
return worldFolder/fs::u8path(filename);
return worldFolder / fs::u8path(filename);
}
if (contentPacks) {
for (auto& pack : *contentPacks) {
if (pack.id == prefix) {
return pack.folder/fs::u8path(filename);
return pack.folder / fs::u8path(filename);
}
}
}
if (throwErr) {
throw files_access_error("unknown entry point '"+prefix+"'");
throw files_access_error("unknown entry point '" + prefix + "'");
}
return fs::path(filename);
}
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
}
fs::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
fs::path file = root.path / fs::u8path(filename);
if (fs::exists(file)) {
@@ -192,7 +194,7 @@ fs::path ResPaths::find(const std::string& filename) const {
}
std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
if (fs::exists(root.path / fs::path(filename))) {
return root.name + ":" + filename;
@@ -200,30 +202,29 @@ std::string ResPaths::findRaw(const std::string& filename) const {
}
auto resDir = mainRoot;
if (fs::exists(resDir / fs::path(filename))) {
return "core:"+filename;
return "core:" + filename;
}
throw std::runtime_error("could not to find file "+util::quote(filename));
throw std::runtime_error("could not to find file " + util::quote(filename));
}
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) const {
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
) const {
std::vector<std::string> entries;
for (int i = roots.size()-1; i >= 0; i--) {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
fs::path folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder))
continue;
if (!fs::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(folder)) {
auto name = entry.path().filename().u8string();
entries.emplace_back(root.name+":"+folderName+"/"+name);
entries.emplace_back(root.name + ":" + folderName + "/" + name);
}
}
{
fs::path folder = mainRoot / fs::u8path(folderName);
if (!fs::is_directory(folder))
return entries;
if (!fs::is_directory(folder)) return entries;
for (const auto& entry : fs::directory_iterator(folder)) {
auto name = entry.path().filename().u8string();
entries.emplace_back("core:"+folderName+"/"+name);
entries.emplace_back("core:" + folderName + "/" + name);
}
}
return entries;
@@ -231,19 +232,17 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) con
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
std::vector<fs::path> entries;
for (int i = roots.size()-1; i >= 0; i--) {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
fs::path folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder))
continue;
if (!fs::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(folder)) {
entries.push_back(entry.path());
}
}
{
fs::path folder = mainRoot / fs::u8path(folderName);
if (!fs::is_directory(folder))
return entries;
if (!fs::is_directory(folder)) return entries;
for (const auto& entry : fs::directory_iterator(folder)) {
entries.push_back(entry.path());
}
+10 -12
View File
@@ -1,10 +1,10 @@
#ifndef FILES_ENGINE_PATHS_HPP_
#define FILES_ENGINE_PATHS_HPP_
#include <filesystem>
#include <stdexcept>
#include <string>
#include <vector>
#include <stdexcept>
#include <filesystem>
#include "../content/ContentPack.hpp"
@@ -12,12 +12,13 @@ namespace fs = std::filesystem;
class files_access_error : public std::runtime_error {
public:
files_access_error(const std::string& msg) : std::runtime_error(msg) {}
files_access_error(const std::string& msg) : std::runtime_error(msg) {
}
};
class EnginePaths {
fs::path userfiles {"."};
fs::path resources {"res"};
fs::path resources {"res"};
fs::path worldFolder;
std::vector<ContentPack>* contentPacks = nullptr;
public:
@@ -25,7 +26,7 @@ public:
fs::path getUserfiles() const;
fs::path getResources() const;
fs::path getScreenshotFile(const std::string& ext);
fs::path getWorldsFolder();
fs::path getWorldFolder();
@@ -41,7 +42,7 @@ public:
std::vector<fs::path> scanForWorlds();
fs::path resolve(const std::string& path, bool throwErr=true);
fs::path resolve(const std::string& path, bool throwErr = true);
};
struct PathsRoot {
@@ -53,11 +54,8 @@ class ResPaths {
fs::path mainRoot;
std::vector<PathsRoot> roots;
public:
ResPaths(
fs::path mainRoot,
std::vector<PathsRoot> roots
);
ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots);
fs::path find(const std::string& filename) const;
std::string findRaw(const std::string& filename) const;
std::vector<fs::path> listdir(const std::string& folder) const;
@@ -66,4 +64,4 @@ public:
const fs::path& getMainRoot() const;
};
#endif // FILES_ENGINE_PATHS_HPP_
#endif // FILES_ENGINE_PATHS_HPP_
+39 -32
View File
@@ -1,24 +1,25 @@
#include "files.hpp"
#include "../coders/commons.hpp"
#include "../coders/json.hpp"
#include "../coders/toml.hpp"
#include "../coders/gzip.hpp"
#include "../util/stringutil.hpp"
#include "../data/dynamic.hpp"
#include <stdint.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdint.h>
#include <stdexcept>
#include "../coders/commons.hpp"
#include "../coders/gzip.hpp"
#include "../coders/json.hpp"
#include "../coders/toml.hpp"
#include "../data/dynamic.hpp"
#include "../util/stringutil.hpp"
namespace fs = std::filesystem;
files::rafile::rafile(const fs::path& filename)
: file(filename, std::ios::binary | std::ios::ate) {
if (!file) {
throw std::runtime_error("could not to open file "+filename.string());
throw std::runtime_error("could not to open file " + filename.string());
}
filelength = file.tellg();
file.seekg(0);
@@ -36,19 +37,21 @@ void files::rafile::read(char* buffer, std::streamsize size) {
file.read(buffer, size);
}
bool files::write_bytes(const fs::path& filename, const ubyte* data, size_t size) {
bool files::write_bytes(
const fs::path& filename, const ubyte* data, size_t size
) {
std::ofstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
if (!output.is_open()) return false;
output.write((const char*)data, size);
output.close();
return true;
}
uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t size) {
uint files::append_bytes(
const fs::path& filename, const ubyte* data, size_t size
) {
std::ofstream output(filename, std::ios::binary | std::ios::app);
if (!output.is_open())
return 0;
if (!output.is_open()) return 0;
uint position = output.tellp();
output.write((const char*)data, size);
output.close();
@@ -57,17 +60,17 @@ uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t siz
bool files::read(const fs::path& filename, char* data, size_t size) {
std::ifstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
if (!output.is_open()) return false;
output.read(data, size);
output.close();
return true;
}
std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& length) {
std::unique_ptr<ubyte[]> files::read_bytes(
const fs::path& filename, size_t& length
) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return nullptr;
if (!input.is_open()) return nullptr;
input.seekg(0, std::ios_base::end);
length = input.tellg();
input.seekg(0, std::ios_base::beg);
@@ -80,8 +83,7 @@ std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& len
std::vector<ubyte> files::read_bytes(const fs::path& filename) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return {};
if (!input.is_open()) return {};
input.seekg(0, std::ios_base::end);
size_t length = input.tellg();
input.seekg(0, std::ios_base::beg);
@@ -95,10 +97,11 @@ std::vector<ubyte> files::read_bytes(const fs::path& filename) {
std::string files::read_string(const fs::path& filename) {
size_t size;
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
std::unique_ptr<ubyte[]> bytes(read_bytes(filename, size));
if (bytes == nullptr) {
throw std::runtime_error("could not to load file '"+
filename.string()+"'");
throw std::runtime_error(
"could not to load file '" + filename.string() + "'"
);
}
return std::string((const char*)bytes.get(), size);
}
@@ -112,11 +115,15 @@ bool files::write_string(const fs::path& filename, const std::string content) {
return true;
}
bool files::write_json(const fs::path& filename, const dynamic::Map* obj, bool nice) {
bool files::write_json(
const fs::path& filename, const dynamic::Map* obj, bool nice
) {
return files::write_string(filename, json::stringify(obj, nice, " "));
}
bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj, bool compression) {
bool files::write_binary_json(
const fs::path& filename, const dynamic::Map* obj, bool compression
) {
auto bytes = json::to_binary(obj, compression);
return files::write_bytes(filename, bytes.data(), bytes.size());
}
@@ -128,7 +135,7 @@ std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) {
std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) {
size_t size;
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
std::unique_ptr<ubyte[]> bytes(files::read_bytes(file, size));
return json::from_binary(bytes.get(), size);
}
@@ -139,16 +146,16 @@ std::shared_ptr<dynamic::Map> files::read_toml(const fs::path& file) {
std::vector<std::string> files::read_list(const fs::path& filename) {
std::ifstream file(filename);
if (!file) {
throw std::runtime_error("could not to open file "+filename.u8string());
throw std::runtime_error(
"could not to open file " + filename.u8string()
);
}
std::vector<std::string> lines;
std::string line;
while (std::getline(file, line)) {
util::trim(line);
if (line.length() == 0)
continue;
if (line[0] == '#')
continue;
if (line.length() == 0) continue;
if (line[0] == '#') continue;
lines.push_back(line);
}
return lines;
+11 -7
View File
@@ -1,11 +1,12 @@
#ifndef FILES_FILES_HPP_
#define FILES_FILES_HPP_
#include <filesystem>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include <filesystem>
#include "../typedefs.hpp"
namespace fs = std::filesystem;
@@ -43,16 +44,19 @@ namespace files {
bool write_string(const fs::path& filename, const std::string content);
/// @brief Write dynamic data to the JSON file
/// @param nice if true, human readable format will be used, otherwise minimal
bool write_json(const fs::path& filename, const dynamic::Map* obj, bool nice=true);
/// @param nice if true, human readable format will be used, otherwise
/// minimal
bool write_json(
const fs::path& filename, const dynamic::Map* obj, bool nice = true
);
/// @brief Write dynamic data to the binary JSON file
/// (see src/coders/binary_json_spec.md)
/// @param compressed use gzip compression
bool write_binary_json(
const fs::path& filename,
const dynamic::Map* obj,
bool compressed=false
const dynamic::Map* obj,
bool compressed = false
);
bool read(const fs::path&, char* data, size_t size);
+24 -19
View File
@@ -1,15 +1,15 @@
#include "settings_io.hpp"
#include "../window/Events.hpp"
#include "../window/input.hpp"
#include "../coders/toml.hpp"
#include "../coders/json.hpp"
#include "../debug/Logger.hpp"
#include "../settings.hpp"
#include <memory>
#include <utility>
#include "../coders/json.hpp"
#include "../coders/toml.hpp"
#include "../debug/Logger.hpp"
#include "../settings.hpp"
#include "../window/Events.hpp"
#include "../window/input.hpp"
static debug::Logger logger("settings_io");
struct SectionsBuilder {
@@ -19,16 +19,17 @@ struct SectionsBuilder {
SectionsBuilder(
std::unordered_map<std::string, Setting*>& map,
std::vector<Section>& sections
) : map(map), sections(sections) {
)
: map(map), sections(sections) {
}
void section(std::string name) {
sections.push_back(Section {std::move(name), {}});
}
void add(const std::string& name, Setting* setting, bool writeable=true) {
Section& section = sections.at(sections.size()-1);
map[section.name+"."+name] = setting;
void add(const std::string& name, Setting* setting, bool writeable = true) {
Section& section = sections.at(sections.size() - 1);
map[section.name + "." + name] = setting;
section.keys.push_back(name);
}
};
@@ -82,7 +83,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
dynamic::Value SettingsHandler::getValue(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
throw std::runtime_error("setting '" + name + "' does not exist");
}
auto setting = found->second;
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
@@ -94,14 +95,14 @@ dynamic::Value SettingsHandler::getValue(const std::string& name) const {
} else if (auto string = dynamic_cast<StringSetting*>(setting)) {
return string->get();
} else {
throw std::runtime_error("type is not implemented for '"+name+"'");
throw std::runtime_error("type is not implemented for '" + name + "'");
}
}
std::string SettingsHandler::toString(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
throw std::runtime_error("setting '" + name + "' does not exist");
}
auto setting = found->second;
return setting->toString();
@@ -110,7 +111,7 @@ std::string SettingsHandler::toString(const std::string& name) const {
Setting* SettingsHandler::getSetting(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
throw std::runtime_error("setting '" + name + "' does not exist");
}
return found->second;
}
@@ -119,7 +120,7 @@ bool SettingsHandler::has(const std::string& name) const {
return map.find(name) != map.end();
}
template<class T>
template <class T>
static void set_numeric_value(T* setting, const dynamic::Value& value) {
if (auto num = std::get_if<integer_t>(&value)) {
setting->set(*num);
@@ -132,10 +133,12 @@ static void set_numeric_value(T* setting, const dynamic::Value& value) {
}
}
void SettingsHandler::setValue(const std::string& name, const dynamic::Value& value) {
void SettingsHandler::setValue(
const std::string& name, const dynamic::Value& value
) {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
throw std::runtime_error("setting '" + name + "' does not exist");
}
auto setting = found->second;
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
@@ -157,7 +160,9 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va
throw std::runtime_error("not implemented for type");
}
} else {
throw std::runtime_error("type is not implement - setting '"+name+"'");
throw std::runtime_error(
"type is not implement - setting '" + name + "'"
);
}
}
+5 -5
View File
@@ -1,12 +1,12 @@
#ifndef FILES_SETTINGS_IO_HPP_
#define FILES_SETTINGS_IO_HPP_
#include "../data/dynamic.hpp"
#include <string>
#include <memory>
#include <vector>
#include <string>
#include <unordered_map>
#include <vector>
#include "../data/dynamic.hpp"
class Setting;
struct EngineSettings;
@@ -31,4 +31,4 @@ public:
std::vector<Section>& getSections();
};
#endif // FILES_SETTINGS_IO_HPP_
#endif // FILES_SETTINGS_IO_HPP_