Skip to content

Commit

Permalink
refactor: refactoring with expected
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Apr 29, 2024
1 parent be94e75 commit 5535c6c
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 86 deletions.
80 changes: 56 additions & 24 deletions src/data/PlayerState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,62 @@ bool PlayerState::setVicePos(WithDim<BlockPos> 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<CompoundTag>());
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<CompoundTag>());
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<CompoundTag>());
}
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<CompoundTag>()); res) {
region = std::move(*res);
} else {
return ll::forwardError(res.error());
}
}
return {};
});
}
} // namespace we
4 changes: 2 additions & 2 deletions src/data/PlayerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PlayerState {
bool setMainPos(WithDim<BlockPos> const&);
bool setVicePos(WithDim<BlockPos> const&);

void serialize(CompoundTag&) const;
void deserialize(CompoundTag const&);
ll::Expected<> serialize(CompoundTag&) const;
ll::Expected<> deserialize(CompoundTag const&);
};
} // namespace we
18 changes: 9 additions & 9 deletions src/data/PlayerStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ std::shared_ptr<PlayerState> PlayerStateManager::get(mce::UUID const& uuid, bool
if (playerStates.if_contains(uuid, [&](auto&& p) { res = p.second; })) {
return res;
} else if (!temp) {
std::optional<std::string> nbt = storagedState.get(uuid.asString());
if (nbt) {
if (auto nbt = storagedState.get(uuid.asString()); nbt) {
res = std::make_shared<PlayerState>(uuid, temp);
res->deserialize(CompoundTag::fromBinaryNbt(*nbt).value());
CompoundTag::fromBinaryNbt(*nbt).and_then([&](CompoundTag&& tag) {
return res->deserialize(tag);
});
return res;
}
}
Expand All @@ -214,12 +215,11 @@ PlayerStateManager::getOrCreate(mce::UUID const& uuid, bool temp) {
[&, this](auto&& ctor) {
res = std::make_shared<PlayerState>(uuid, temp);
if (!temp) {
try {
std::optional<std::string> 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);
}
Expand Down
38 changes: 29 additions & 9 deletions src/region/ConvexRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListTag>();
for (auto& v : indexedVertices) {
vec.mList.push_back(ll::reflection::serialize<CompoundTagVariant>(v.data).value()
);
if (res) {
if (auto t = ll::reflection::serialize<CompoundTagVariant>(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<ListTag>().mList) {
addVertexWithIndex(
ll::reflection::deserialize_to<BlockPos>(CompoundTagVariant{v}).value()
);
if (res) {
if (auto t = ll::reflection::deserialize_to<BlockPos>(CompoundTagVariant{v});
t) {
addVertexWithIndex(std::move(*t));
} else {
res = ll::forwardError(t.error());
break;
}
}
}
return res;
}
void ConvexRegion::updateBoundingBox() {
if (vertices.empty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/region/ConvexRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 16 additions & 8 deletions src/region/CuboidRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/region/CuboidRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
48 changes: 32 additions & 16 deletions src/region/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(BlockPos const&)>&& todo) const {
boundingBox.forEachPos([todo = std::move(todo), this](BlockPos const& pos) {
Expand Down Expand Up @@ -91,16 +94,29 @@ Region::create(RegionType type, DimensionType dim, BoundingBox const& box, bool
}
return res;
}

std::shared_ptr<Region> Region::create(CompoundTag const& tag) {
auto res = create(
ll::reflection::deserialize_to<RegionType>(tag["type"]).value(),
ll::reflection::deserialize_to<int>(tag["dim"]).value(),
ll::reflection::deserialize_to<BoundingBox>(tag["boundingBox"]).value(),
false
);
res->deserialize(tag);
res->updateBoundingBox();
return res;
ll::Expected<std::shared_ptr<Region>> Region::create(CompoundTag const& tag) {
RegionType type;
int dim;
return ll::reflection::deserialize_to<RegionType>(tag["type"])
.and_then([&](auto&& r) {
type = r;
return ll::reflection::deserialize_to<int>(tag["dim"]);
})
.and_then([&](auto&& r) {
dim = r;
return ll::reflection::deserialize_to<BoundingBox>(tag["boundingBox"]);
})
.transform([&](auto&& r) { return create(type, dim, r, false); })
.and_then([&](auto&& r) -> ll::Expected<std::shared_ptr<Region>> {
if (auto res = r->deserialize(tag); res) {
return r;
} else {
return ll::forwardError(res.error());
}
})
.transform([&](auto&& r) {
r->updateBoundingBox();
return r;
});
}
} // namespace we
8 changes: 4 additions & 4 deletions src/region/Region.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "worldedit/Global.h"
#include "RegionType.h"
#include "worldedit/Global.h"

#include "utils/GeoContainer.h"

Expand All @@ -18,17 +18,17 @@ class Region {
static std::shared_ptr<Region>
create(RegionType, DimensionType, BoundingBox const&, bool update = true);

static std::shared_ptr<Region> create(CompoundTag const&);
static ll::Expected<std::shared_ptr<Region>> create(CompoundTag const&);

DimensionType getDim() const { return dim; }

int getHeighest(Pos2d) const;

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; }

Expand Down
24 changes: 16 additions & 8 deletions src/region/SphereRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/region/SphereRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/worldedit/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ll/api/reflection/Deserialization.h>
#include <ll/api/reflection/Serialization.h>
#include <ll/api/schedule/Scheduler.h>
#include <ll/api/Expected.h>
#include <ll/api/utils/HashUtils.h>
#include <mc/math/Vec2.h>
#include <mc/math/Vec3.h>
Expand Down

0 comments on commit 5535c6c

Please sign in to comment.