From dc24fa017bc12a8c350c28e74931865003938ea3 Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Wed, 18 Aug 2021 12:50:18 +0200 Subject: [PATCH] world: replaced m_uuid by uuid property, to make it available in the clients --- server/src/world/world.cpp | 14 +++++++------- server/src/world/world.hpp | 4 ++-- server/src/world/worldloader.cpp | 4 +++- server/src/world/worldsaver.cpp | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/server/src/world/world.cpp b/server/src/world/world.cpp index fa5eb8e4..ceb9e3fc 100644 --- a/server/src/world/world.cpp +++ b/server/src/world/world.cpp @@ -65,7 +65,7 @@ void World::init(const std::shared_ptr& world) World::World(Private) : Object(), - m_uuid{boost::uuids::random_generator()()}, + uuid{this, "uuid", to_string(boost::uuids::random_generator()()), PropertyFlags::ReadOnly | PropertyFlags::NoStore}, name{this, "name", "", PropertyFlags::ReadWrite | PropertyFlags::Store}, scale{this, "scale", WorldScale::H0, PropertyFlags::ReadWrite | PropertyFlags::Store, [this](WorldScale value){ updateScaleRatio(); }}, scaleRatio{this, "scale_ratio", 87, PropertyFlags::ReadWrite | PropertyFlags::Store}, @@ -182,7 +182,6 @@ World::World(Private) : // backup world: const std::filesystem::path worldDir = Traintastic::instance->worldDir(); const std::filesystem::path worldBackupDir = Traintastic::instance->worldBackupDir(); - const std::string uuid{to_string(m_uuid)}; auto dateTimeStr = []() { @@ -200,24 +199,24 @@ World::World(Private) : Log::log(*this, LogMessage::C1007_CREATING_WORLD_BACKUP_DIRECTORY_FAILED_X, ec); } - if(std::filesystem::is_directory(worldDir / uuid)) + if(std::filesystem::is_directory(worldDir / uuid.value())) { std::error_code ec; - std::filesystem::rename(worldDir / uuid, worldBackupDir / uuid += dateTimeStr(), ec); + std::filesystem::rename(worldDir / uuid.value(), worldBackupDir / uuid.value() += dateTimeStr(), ec); if(ec) Log::log(*this, LogMessage::C1006_CREATING_WORLD_BACKUP_FAILED_X, ec); } - if(std::filesystem::is_regular_file(worldDir / uuid += dotCTW)) + if(std::filesystem::is_regular_file(worldDir / uuid.value() += dotCTW)) { std::error_code ec; - std::filesystem::rename(worldDir / uuid += dotCTW, worldBackupDir / uuid += dateTimeStr() += dotCTW, ec); + std::filesystem::rename(worldDir / uuid.value() += dotCTW, worldBackupDir / uuid.value() += dateTimeStr() += dotCTW, ec); if(ec) Log::log(*this, LogMessage::C1006_CREATING_WORLD_BACKUP_FAILED_X, ec); } // save world: - std::filesystem::path savePath = worldDir / uuid; + std::filesystem::path savePath = worldDir / uuid.value(); if(!Traintastic::instance->settings->saveWorldUncompressed) savePath += dotCTW; @@ -231,6 +230,7 @@ World::World(Private) : } }} { + m_interfaceItems.add(uuid); Attributes::addDisplayName(name, DisplayName::Object::name); m_interfaceItems.add(name); Attributes::addEnabled(scale, false); diff --git a/server/src/world/world.hpp b/server/src/world/world.hpp index 175c3554..8b0f4385 100644 --- a/server/src/world/world.hpp +++ b/server/src/world/world.hpp @@ -64,7 +64,6 @@ class World : public Object protected: static void init(const std::shared_ptr& world); - boost::uuids::uuid m_uuid; std::unordered_map> m_objects; void loaded() final; @@ -81,6 +80,7 @@ class World : public Object static std::shared_ptr create(); + Property uuid; Property name; Property scale; Property scaleRatio; @@ -117,7 +117,7 @@ class World : public Object std::string getObjectId() const final { return std::string(classId); } - const boost::uuids::uuid& uuid() const { return m_uuid; } + //const boost::uuids::uuid& uuid() const { return m_uuid; } std::string getUniqueId(std::string_view prefix) const; bool isObject(const std::string&_id) const; diff --git a/server/src/world/worldloader.cpp b/server/src/world/worldloader.cpp index 73466f6e..b8bafc4f 100644 --- a/server/src/world/worldloader.cpp +++ b/server/src/world/worldloader.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "world.hpp" #include "../utils/string.hpp" #include "ctwreader.hpp" @@ -82,7 +83,8 @@ WorldLoader::WorldLoader(std::filesystem::path path) : } } - m_world->m_uuid = boost::uuids::string_generator()(std::string(data["uuid"])); + // check if UUID is valid: + m_world->uuid.setValueInternal(to_string(boost::uuids::string_generator()(std::string(data["uuid"])))); // create a list of all objects m_objects.insert({m_world->getObjectId(), {data, m_world, false}}); diff --git a/server/src/world/worldsaver.cpp b/server/src/world/worldsaver.cpp index 83db8b95..63b097cd 100644 --- a/server/src/world/worldsaver.cpp +++ b/server/src/world/worldsaver.cpp @@ -44,7 +44,7 @@ WorldSaver::WorldSaver(const World& world, std::filesystem::path path) : data.erase("class_id"); } - data["uuid"] = state["uuid"] = to_string(world.m_uuid); + data["uuid"] = state["uuid"] = world.uuid.value(); { json objects = json::array();