From 5535c6c95377888ad7b7e4f3d9a67d6dbf2e1ad0 Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Tue, 30 Apr 2024 06:50:49 +0800 Subject: [PATCH] refactor: refactoring with expected --- src/data/PlayerState.cpp | 80 +++++++++++++++++++++++---------- src/data/PlayerState.h | 4 +- src/data/PlayerStateManager.cpp | 18 ++++---- src/region/ConvexRegion.cpp | 38 ++++++++++++---- src/region/ConvexRegion.h | 4 +- src/region/CuboidRegion.cpp | 24 ++++++---- src/region/CuboidRegion.h | 4 +- src/region/Region.cpp | 48 +++++++++++++------- src/region/Region.h | 8 ++-- src/region/SphereRegion.cpp | 24 ++++++---- src/region/SphereRegion.h | 4 +- src/worldedit/Global.h | 1 + 12 files changed, 171 insertions(+), 86 deletions(-) diff --git a/src/data/PlayerState.cpp b/src/data/PlayerState.cpp index 55b3393..2b4858d 100644 --- a/src/data/PlayerState.cpp +++ b/src/data/PlayerState.cpp @@ -60,30 +60,62 @@ bool PlayerState::setVicePos(WithDim const& v) { return false; } -void PlayerState::serialize(CompoundTag& nbt) const { - ll::reflection::serialize_to(nbt["config"], config).value(); - if (mainPos) ll::reflection::serialize_to(nbt["mainPos"], mainPos->data).value(); - if (vicePos) ll::reflection::serialize_to(nbt["vicePos"], vicePos->data).value(); - if (regionType) ll::reflection::serialize_to(nbt["regionType"], *regionType).value(); - if (region) region->serialize(nbt["region"].emplace()); +ll::Expected<> PlayerState::serialize(CompoundTag& nbt) const { + return ll::reflection::serialize_to(nbt["config"], config) + .and_then([&, this]() { + if (mainPos) + return ll::reflection::serialize_to(nbt["mainPos"], mainPos->data); + return ll::Expected<>{}; + }) + .and_then([&, this]() { + if (vicePos) + return ll::reflection::serialize_to(nbt["vicePos"], vicePos->data); + return ll::Expected<>{}; + }) + .and_then([&, this]() { + if (regionType) + return ll::reflection::serialize_to(nbt["regionType"], *regionType); + return ll::Expected<>{}; + }) + .and_then([&, this]() { + if (region) return region->serialize(nbt["region"].emplace()); + return ll::Expected<>{}; + }); } -void PlayerState::deserialize(CompoundTag const& nbt) { - ll::reflection::deserialize(config, nbt["config"]).value(); - if (nbt.contains("mainPos")) { - mainPos.emplace(); - ll::reflection::deserialize(mainPos->data, nbt["mainPos"]).value(); - setMainPosInternal(); - } - if (nbt.contains("vicePos")) { - vicePos.emplace(); - ll::reflection::deserialize(vicePos->data, nbt["vicePos"]).value(); - setVicePosInternal(); - } - if (nbt.contains("regionType")) { - ll::reflection::deserialize(*regionType, nbt["regionType"]).value(); - } - if (nbt.contains("region")) { - region = Region::create(nbt["region"].get()); - } +ll::Expected<> PlayerState::deserialize(CompoundTag const& nbt) { + return ll::reflection::deserialize(config, nbt["config"]) + .and_then([&, this]() { + ll::Expected<> res; + if (nbt.contains("mainPos")) { + mainPos.emplace(); + res = ll::reflection::deserialize(mainPos->data, nbt["mainPos"]); + if (res) setMainPosInternal(); + } + return res; + }) + .and_then([&, this]() { + ll::Expected<> res; + if (nbt.contains("vicePos")) { + vicePos.emplace(); + res = ll::reflection::deserialize(vicePos->data, nbt["vicePos"]); + if (res) setMainPosInternal(); + } + return res; + }) + .and_then([&, this]() { + if (nbt.contains("regionType")) + return ll::reflection::deserialize(*regionType, nbt["regionType"]); + return ll::Expected<>{}; + }) + .and_then([&, this]() -> ll::Expected<> { + if (nbt.contains("region")) { + if (auto res = Region::create(nbt["region"].get()); res) { + region = std::move(*res); + } else { + return ll::forwardError(res.error()); + } + } + return {}; + }); } } // namespace we \ No newline at end of file diff --git a/src/data/PlayerState.h b/src/data/PlayerState.h index 3596c68..bf7cdf7 100644 --- a/src/data/PlayerState.h +++ b/src/data/PlayerState.h @@ -36,7 +36,7 @@ class PlayerState { bool setMainPos(WithDim const&); bool setVicePos(WithDim const&); - void serialize(CompoundTag&) const; - void deserialize(CompoundTag const&); + ll::Expected<> serialize(CompoundTag&) const; + ll::Expected<> deserialize(CompoundTag const&); }; } // namespace we diff --git a/src/data/PlayerStateManager.cpp b/src/data/PlayerStateManager.cpp index eca104d..2386888 100644 --- a/src/data/PlayerStateManager.cpp +++ b/src/data/PlayerStateManager.cpp @@ -196,10 +196,11 @@ std::shared_ptr PlayerStateManager::get(mce::UUID const& uuid, bool if (playerStates.if_contains(uuid, [&](auto&& p) { res = p.second; })) { return res; } else if (!temp) { - std::optional nbt = storagedState.get(uuid.asString()); - if (nbt) { + if (auto nbt = storagedState.get(uuid.asString()); nbt) { res = std::make_shared(uuid, temp); - res->deserialize(CompoundTag::fromBinaryNbt(*nbt).value()); + CompoundTag::fromBinaryNbt(*nbt).and_then([&](CompoundTag&& tag) { + return res->deserialize(tag); + }); return res; } } @@ -214,12 +215,11 @@ PlayerStateManager::getOrCreate(mce::UUID const& uuid, bool temp) { [&, this](auto&& ctor) { res = std::make_shared(uuid, temp); if (!temp) { - try { - std::optional nbt = storagedState.get(uuid.asString()); - if (nbt) { - res->deserialize(CompoundTag::fromBinaryNbt(*nbt).value()); - } - } catch (...) {} + if (auto nbt = storagedState.get(uuid.asString()); nbt) { + CompoundTag::fromBinaryNbt(*nbt).and_then([&](CompoundTag&& tag) { + return res->deserialize(tag); + }); + } } ctor(uuid, res); } diff --git a/src/region/ConvexRegion.cpp b/src/region/ConvexRegion.cpp index 8d67e4d..b9a4aeb 100644 --- a/src/region/ConvexRegion.cpp +++ b/src/region/ConvexRegion.cpp @@ -3,21 +3,41 @@ #include "worldedit/WorldEdit.h" namespace we { -void ConvexRegion::serialize(CompoundTag& tag) const { - Region::serialize(tag); +ll::Expected<> ConvexRegion::serialize(CompoundTag& tag) const { + auto res = Region::serialize(tag); + if (!res) { + return res; + } auto& vec = tag["indexedVertices"].emplace(); for (auto& v : indexedVertices) { - vec.mList.push_back(ll::reflection::serialize(v.data).value() - ); + if (res) { + if (auto t = ll::reflection::serialize(v.data); t) { + vec.mList.push_back(std::move(*t)); + } else { + res = ll::forwardError(t.error()); + break; + } + } } + return res; } -void ConvexRegion::deserialize(CompoundTag const& tag) { - Region::deserialize(tag); +ll::Expected<> ConvexRegion::deserialize(CompoundTag const& tag) { + auto res = Region::deserialize(tag); + if (!res) { + return res; + } for (auto& v : tag.at("indexedVertices").get().mList) { - addVertexWithIndex( - ll::reflection::deserialize_to(CompoundTagVariant{v}).value() - ); + if (res) { + if (auto t = ll::reflection::deserialize_to(CompoundTagVariant{v}); + t) { + addVertexWithIndex(std::move(*t)); + } else { + res = ll::forwardError(t.error()); + break; + } + } } + return res; } void ConvexRegion::updateBoundingBox() { if (vertices.empty()) { diff --git a/src/region/ConvexRegion.h b/src/region/ConvexRegion.h index c683eed..ab4765d 100644 --- a/src/region/ConvexRegion.h +++ b/src/region/ConvexRegion.h @@ -28,9 +28,9 @@ class ConvexRegion : public Region { public: ConvexRegion(DimensionType, BoundingBox const&); - void serialize(CompoundTag&) const override; + ll::Expected<> serialize(CompoundTag&) const override; - void deserialize(CompoundTag const&) override; + ll::Expected<> deserialize(CompoundTag const&) override; void updateBoundingBox() override; diff --git a/src/region/CuboidRegion.cpp b/src/region/CuboidRegion.cpp index 718aea0..37f99c3 100644 --- a/src/region/CuboidRegion.cpp +++ b/src/region/CuboidRegion.cpp @@ -3,15 +3,23 @@ #include "worldedit/WorldEdit.h" namespace we { -void CuboidRegion::serialize(CompoundTag& tag) const { - Region::serialize(tag); - ll::reflection::serialize_to(tag["mainPos"], mainPos).value(); - ll::reflection::serialize_to(tag["vicePos"], vicePos).value(); +ll::Expected<> CuboidRegion::serialize(CompoundTag& tag) const { + return Region::serialize(tag) + .and_then([&, this]() { + return ll::reflection::serialize_to(tag["mainPos"], mainPos); + }) + .and_then([&, this]() { + return ll::reflection::serialize_to(tag["vicePos"], vicePos); + }); } -void CuboidRegion::deserialize(CompoundTag const& tag) { - Region::deserialize(tag); - ll::reflection::deserialize(mainPos, tag.at("mainPos")).value(); - ll::reflection::deserialize(vicePos, tag.at("vicePos")).value(); +ll::Expected<> CuboidRegion::deserialize(CompoundTag const& tag) { + return Region::deserialize(tag) + .and_then([&, this]() { + return ll::reflection::deserialize(mainPos, tag.at("mainPos")); + }) + .and_then([&, this]() { + return ll::reflection::deserialize(vicePos, tag.at("vicePos")); + }); } void CuboidRegion::updateBoundingBox() { diff --git a/src/region/CuboidRegion.h b/src/region/CuboidRegion.h index c04b623..e117f50 100644 --- a/src/region/CuboidRegion.h +++ b/src/region/CuboidRegion.h @@ -13,9 +13,9 @@ class CuboidRegion : public Region { public: CuboidRegion(DimensionType, BoundingBox const&); - void serialize(CompoundTag&) const override; + ll::Expected<> serialize(CompoundTag&) const override; - void deserialize(CompoundTag const&) override; + ll::Expected<> deserialize(CompoundTag const&) override; void updateBoundingBox() override; diff --git a/src/region/Region.cpp b/src/region/Region.cpp index 5281153..05d86ee 100644 --- a/src/region/Region.cpp +++ b/src/region/Region.cpp @@ -7,12 +7,15 @@ namespace we { -void Region::serialize(CompoundTag& tag) const { - ll::reflection::serialize_to(tag["type"], getType()).value(); - ll::reflection::serialize_to(tag["boundingBox"], boundingBox).value(); - ll::reflection::serialize_to(tag["dim"], dim.id).value(); +ll::Expected<> Region::serialize(CompoundTag& tag) const { + return ll::reflection::serialize_to(tag["type"], getType()) + .and_then([&, this]() { + return ll::reflection::serialize_to(tag["boundingBox"], boundingBox); + }) + .and_then([&, this]() { return ll::reflection::serialize_to(tag["dim"], dim.id); } + ); } -void Region::deserialize(CompoundTag const&) {} +ll::Expected<> Region::deserialize(CompoundTag const&) { return {}; } void Region::forEachBlockInRegion(std::function&& todo) const { boundingBox.forEachPos([todo = std::move(todo), this](BlockPos const& pos) { @@ -91,16 +94,29 @@ Region::create(RegionType type, DimensionType dim, BoundingBox const& box, bool } return res; } - -std::shared_ptr Region::create(CompoundTag const& tag) { - auto res = create( - ll::reflection::deserialize_to(tag["type"]).value(), - ll::reflection::deserialize_to(tag["dim"]).value(), - ll::reflection::deserialize_to(tag["boundingBox"]).value(), - false - ); - res->deserialize(tag); - res->updateBoundingBox(); - return res; +ll::Expected> Region::create(CompoundTag const& tag) { + RegionType type; + int dim; + return ll::reflection::deserialize_to(tag["type"]) + .and_then([&](auto&& r) { + type = r; + return ll::reflection::deserialize_to(tag["dim"]); + }) + .and_then([&](auto&& r) { + dim = r; + return ll::reflection::deserialize_to(tag["boundingBox"]); + }) + .transform([&](auto&& r) { return create(type, dim, r, false); }) + .and_then([&](auto&& r) -> ll::Expected> { + if (auto res = r->deserialize(tag); res) { + return r; + } else { + return ll::forwardError(res.error()); + } + }) + .transform([&](auto&& r) { + r->updateBoundingBox(); + return r; + }); } } // namespace we diff --git a/src/region/Region.h b/src/region/Region.h index 811ec8f..fafff63 100644 --- a/src/region/Region.h +++ b/src/region/Region.h @@ -1,7 +1,7 @@ #pragma once -#include "worldedit/Global.h" #include "RegionType.h" +#include "worldedit/Global.h" #include "utils/GeoContainer.h" @@ -18,7 +18,7 @@ class Region { static std::shared_ptr create(RegionType, DimensionType, BoundingBox const&, bool update = true); - static std::shared_ptr create(CompoundTag const&); + static ll::Expected> create(CompoundTag const&); DimensionType getDim() const { return dim; } @@ -26,9 +26,9 @@ class Region { virtual ~Region() = default; - virtual void serialize(CompoundTag&) const; + virtual ll::Expected<> serialize(CompoundTag&) const; - virtual void deserialize(CompoundTag const&); + virtual ll::Expected<> deserialize(CompoundTag const&); virtual bool needResetVice() const { return true; } diff --git a/src/region/SphereRegion.cpp b/src/region/SphereRegion.cpp index 22942d2..ca60a65 100644 --- a/src/region/SphereRegion.cpp +++ b/src/region/SphereRegion.cpp @@ -4,15 +4,23 @@ #include "worldedit/WorldEdit.h" namespace we { -void SphereRegion::serialize(CompoundTag& tag) const { - Region::serialize(tag); - ll::reflection::serialize_to(tag["center"], center).value(); - ll::reflection::serialize_to(tag["radius"], radius).value(); +ll::Expected<> SphereRegion::serialize(CompoundTag& tag) const { + return Region::serialize(tag) + .and_then([&, this]() { + return ll::reflection::serialize_to(tag["center"], center); + }) + .and_then([&, this]() { + return ll::reflection::serialize_to(tag["radius"], radius); + }); } -void SphereRegion::deserialize(CompoundTag const& tag) { - Region::deserialize(tag); - ll::reflection::deserialize(center, tag.at("center")).value(); - ll::reflection::deserialize(radius, tag.at("radius")).value(); +ll::Expected<> SphereRegion::deserialize(CompoundTag const& tag) { + return Region::deserialize(tag) + .and_then([&, this]() { + return ll::reflection::deserialize(center, tag.at("center")); + }) + .and_then([&, this]() { + return ll::reflection::deserialize(radius, tag.at("radius")); + }); } void SphereRegion::updateBoundingBox() { diff --git a/src/region/SphereRegion.h b/src/region/SphereRegion.h index 7003fe4..75be2b6 100644 --- a/src/region/SphereRegion.h +++ b/src/region/SphereRegion.h @@ -15,9 +15,9 @@ class SphereRegion : public Region { public: SphereRegion(DimensionType, BoundingBox const&); - void serialize(CompoundTag&) const override; + ll::Expected<> serialize(CompoundTag&) const override; - void deserialize(CompoundTag const&) override; + ll::Expected<> deserialize(CompoundTag const&) override; void updateBoundingBox() override; diff --git a/src/worldedit/Global.h b/src/worldedit/Global.h index 36757c0..42500a2 100644 --- a/src/worldedit/Global.h +++ b/src/worldedit/Global.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include