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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,12 +20,12 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) {
file.read(header, REGION_HEADER_SIZE); file.read(header, REGION_HEADER_SIZE);
// avoid of use strcmp_s // 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) { REGION_FORMAT_MAGIC) {
throw std::runtime_error("invalid region file magic number"); throw std::runtime_error("invalid region file magic number");
} }
version = header[8]; version = header[8];
if (uint(version) > REGION_FORMAT_VERSION) { if (static_cast<uint>(version) > REGION_FORMAT_VERSION) {
throw illegal_region_format( throw illegal_region_format(
"region format " + std::to_string(version) + " is not supported" "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( std::shared_ptr<Task> create_converter(
Engine* engine, Engine* engine,
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const runnable& postRunnable const runnable& postRunnable
) { ) {
return WorldConverter::startTask( return WorldConverter::startTask(
folder, worldFiles,
content, content,
lut, lut,
[=]() { [=]() {
@ -67,7 +67,7 @@ void show_convert_request(
Engine* engine, Engine* engine,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable const runnable& postRunnable
) { ) {
guiutil::confirm( guiutil::confirm(
@ -75,7 +75,7 @@ void show_convert_request(
langs::get(L"world.convert-request"), langs::get(L"world.convert-request"),
[=]() { [=]() {
auto converter = auto converter =
create_converter(engine, folder, content, lut, postRunnable); create_converter(engine, worldFiles, content, lut, postRunnable);
menus::show_process_panel( menus::show_process_panel(
engine, converter, L"Converting world..." 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 { try {
auto content = engine->getContent(); auto content = engine->getContent();
auto& packs = engine->getContentPacks(); auto& packs = engine->getContentPacks();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
auto level = World::load(folder, settings, content, packs); auto level = World::load(worldFiles, settings, content, packs);
engine->setScreen( engine->setScreen(
std::make_shared<LevelScreen>(engine, std::move(level)) 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(); auto* content = engine->getContent();
auto worldFiles = std::make_shared<WorldFiles>(
std::shared_ptr<ContentLUT> lut(World::checkIndices(folder, content)); folder, engine->getSettings().debug);
if (lut) { if (auto lut = World::checkIndices(worldFiles, content)) {
if (lut->hasMissingContent()) { if (lut->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine->setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, lut); show_content_missing(engine, lut);
@ -145,7 +145,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
engine, engine,
create_converter( create_converter(
engine, engine,
folder, worldFiles,
content, content,
lut, lut,
[=]() { openWorld(name, false); } [=]() { openWorld(name, false); }
@ -153,14 +153,14 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
L"Converting world..." L"Converting world..."
); );
} else { } else {
show_convert_request(engine, content, lut, folder, [=]() { show_convert_request(engine, content, lut, std::move(worldFiles), [=]() {
openWorld(name, false); openWorld(name, false);
}); });
} }
} }
} else { return;
loadWorld(engine, folder);
} }
loadWorld(engine, std::move(worldFiles));
} }
inline uint64_t str2seed(const std::string& seedstr) { 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( World::World(
WorldInfo info, WorldInfo info,
std::unique_ptr<WorldFiles> worldFiles, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) : info(std::move(info)), ) : info(std::move(info)),
@ -102,12 +102,11 @@ std::unique_ptr<Level> World::create(
} }
std::unique_ptr<Level> World::load( std::unique_ptr<Level> World::load(
const fs::path& directory, const std::shared_ptr<WorldFiles>& worldFilesPtr,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) { ) {
auto worldFilesPtr = std::make_unique<WorldFiles>(directory, settings.debug);
auto worldFiles = worldFilesPtr.get(); auto worldFiles = worldFilesPtr.get();
auto info = worldFiles->readWorldInfo(); auto info = worldFiles->readWorldInfo();
if (!info.has_value()) { if (!info.has_value()) {
@ -156,11 +155,11 @@ std::unique_ptr<Level> World::load(
} }
std::shared_ptr<ContentLUT> World::checkIndices( 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)) { if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content); return ContentLUT::create(worldFiles, indicesFile, content);
} }
return nullptr; return nullptr;
} }

View File

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