This commit is contained in:
MihailRis 2024-08-11 18:21:12 +03:00
parent 3d3da1cdcd
commit 4f2448daed
9 changed files with 46 additions and 35 deletions

View File

@ -7,6 +7,8 @@
#include "files/files.hpp"
#include "items/ItemDef.hpp"
#include "voxels/Block.hpp"
#include "world/World.hpp"
#include "files/WorldFiles.hpp"
#include "Content.hpp"
ContentLUT::ContentLUT(
@ -24,8 +26,14 @@ static constexpr size_t get_entries_count(
}
std::shared_ptr<ContentLUT> ContentLUT::create(
const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& filename, const Content* content
) {
auto worldInfo = worldFiles->readWorldInfo();
if (!worldInfo.has_value()) {
return nullptr;
}
auto root = files::read_json(filename);
auto blocklist = root->list("blocks");
auto itemlist = root->list("items");

View File

@ -17,6 +17,8 @@ struct contententry {
std::string name;
};
struct WorldFiles;
template <typename T, class U>
class ContentUnitLUT {
std::vector<T> indices;
@ -100,7 +102,9 @@ public:
ContentLUT(const ContentIndices* indices, size_t blocks, size_t items);
static std::shared_ptr<ContentLUT> create(
const fs::path& filename, const Content* content
const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& filename,
const Content* content
);
inline bool hasContentReorder() const {

View File

@ -32,11 +32,11 @@ public:
};
WorldConverter::WorldConverter(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
std::shared_ptr<ContentLUT> lut
)
: wfile(std::make_unique<WorldFiles>(folder)),
: wfile(worldFiles),
lut(std::move(lut)),
content(content) {
fs::path regionsFolder =
@ -56,13 +56,13 @@ WorldConverter::~WorldConverter() {
}
std::shared_ptr<Task> WorldConverter::startTask(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,
bool multithreading
) {
auto converter = std::make_shared<WorldConverter>(folder, content, lut);
auto converter = std::make_shared<WorldConverter>(worldFiles, content, lut);
if (!multithreading) {
converter->setOnComplete([=]() {
converter->write();

View File

@ -22,7 +22,7 @@ struct convert_task {
};
class WorldConverter : public Task {
std::unique_ptr<WorldFiles> wfile;
std::shared_ptr<WorldFiles> wfile;
std::shared_ptr<ContentLUT> const lut;
const Content* const content;
std::queue<convert_task> tasks;
@ -33,7 +33,7 @@ class WorldConverter : public Task {
void convertRegion(const fs::path& file) const;
public:
WorldConverter(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
std::shared_ptr<ContentLUT> lut
);
@ -52,7 +52,7 @@ public:
uint getWorkDone() const override;
static std::shared_ptr<Task> startTask(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,

View File

@ -34,7 +34,6 @@ class WorldFiles {
bool doWriteLights = true;
fs::path getWorldFile() const;
fs::path getIndicesFile() const;
fs::path getPacksFile() const;
void writeWorldInfo(const WorldInfo& info);
@ -45,6 +44,7 @@ public:
~WorldFiles();
fs::path getPlayerFile() const;
fs::path getIndicesFile() const;
fs::path getResourcesFile() const;
void createDirectories();

View File

@ -20,12 +20,12 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) {
file.read(header, REGION_HEADER_SIZE);
// avoid of use strcmp_s
if (std::string(header, strlen(REGION_FORMAT_MAGIC)) !=
if (std::string(header, std::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) {
if (static_cast<uint>(version) > REGION_FORMAT_VERSION) {
throw illegal_region_format(
"region format " + std::to_string(version) + " is not supported"
);

View File

@ -44,13 +44,13 @@ void EngineController::deleteWorld(const std::string& name) {
std::shared_ptr<Task> create_converter(
Engine* engine,
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& postRunnable
) {
return WorldConverter::startTask(
folder,
worldFiles,
content,
lut,
[=]() {
@ -67,7 +67,7 @@ void show_convert_request(
Engine* engine,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable
) {
guiutil::confirm(
@ -75,7 +75,7 @@ void show_convert_request(
langs::get(L"world.convert-request"),
[=]() {
auto converter =
create_converter(engine, folder, content, lut, postRunnable);
create_converter(engine, worldFiles, content, lut, postRunnable);
menus::show_process_panel(
engine, converter, L"Converting world..."
);
@ -106,13 +106,13 @@ static bool loadWorldContent(Engine* engine, const fs::path& folder) {
});
}
static void loadWorld(Engine* engine, const fs::path& folder) {
static void loadWorld(Engine* engine, const std::shared_ptr<WorldFiles>& worldFiles) {
try {
auto content = engine->getContent();
auto& packs = engine->getContentPacks();
auto& settings = engine->getSettings();
auto level = World::load(folder, settings, content, packs);
auto level = World::load(worldFiles, settings, content, packs);
engine->setScreen(
std::make_shared<LevelScreen>(engine, std::move(level))
);
@ -133,9 +133,9 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
}
auto* content = engine->getContent();
std::shared_ptr<ContentLUT> lut(World::checkIndices(folder, content));
if (lut) {
auto worldFiles = std::make_shared<WorldFiles>(
folder, engine->getSettings().debug);
if (auto lut = World::checkIndices(worldFiles, content)) {
if (lut->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, lut);
@ -145,7 +145,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
engine,
create_converter(
engine,
folder,
worldFiles,
content,
lut,
[=]() { openWorld(name, false); }
@ -153,14 +153,14 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
L"Converting world..."
);
} else {
show_convert_request(engine, content, lut, folder, [=]() {
show_convert_request(engine, content, lut, std::move(worldFiles), [=]() {
openWorld(name, false);
});
}
}
} else {
loadWorld(engine, folder);
return;
}
loadWorld(engine, std::move(worldFiles));
}
inline uint64_t str2seed(const std::string& seedstr) {

View File

@ -27,7 +27,7 @@ world_load_error::world_load_error(const std::string& message)
World::World(
WorldInfo info,
std::unique_ptr<WorldFiles> worldFiles,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::vector<ContentPack>& packs
) : info(std::move(info)),
@ -102,12 +102,11 @@ std::unique_ptr<Level> World::create(
}
std::unique_ptr<Level> World::load(
const fs::path& directory,
const std::shared_ptr<WorldFiles>& worldFilesPtr,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs
) {
auto worldFilesPtr = std::make_unique<WorldFiles>(directory, settings.debug);
auto worldFiles = worldFilesPtr.get();
auto info = worldFiles->readWorldInfo();
if (!info.has_value()) {
@ -156,11 +155,11 @@ std::unique_ptr<Level> World::load(
}
std::shared_ptr<ContentLUT> World::checkIndices(
const fs::path& directory, const Content* content
const std::shared_ptr<WorldFiles>& worldFiles, const Content* content
) {
fs::path indicesFile = directory / fs::path("indices.json");
fs::path indicesFile = worldFiles->getIndicesFile();
if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content);
return ContentLUT::create(worldFiles, indicesFile, content);
}
return nullptr;
}

View File

@ -63,11 +63,11 @@ class World {
void writeResources(const Content* content);
public:
std::unique_ptr<WorldFiles> wfile;
std::shared_ptr<WorldFiles> wfile;
World(
WorldInfo info,
std::unique_ptr<WorldFiles> wfile,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::vector<ContentPack>& packs
);
@ -86,7 +86,7 @@ public:
/// @param content current Content instance
/// @return ContentLUT if world convert required else nullptr
static std::shared_ptr<ContentLUT> checkIndices(
const fs::path& directory, const Content* content
const std::shared_ptr<WorldFiles>& worldFiles, const Content* content
);
/// @brief Create new world
@ -110,7 +110,7 @@ public:
);
/// @brief Load an existing world
/// @param directory root world directory
/// @param worldFiles world files manager
/// @param settings current engine settings
/// @param content current engine Content instance
/// with all world content-packs applied
@ -118,7 +118,7 @@ public:
/// @return Level instance containing World instance
/// @throws world_load_error on world.json load error
static std::unique_ptr<Level> load(
const fs::path& directory,
const std::shared_ptr<WorldFiles>& worldFiles,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs