Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
# with:
# xmake-version: branch@master

- uses: actions/cache@v5
- uses: actions/cache@v6
with:
path: |
~/AppData/Local/.xmake
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
# with:
# xmake-version: branch@master

- uses: actions/cache@v5
- uses: actions/cache@v6
with:
path: |
~/AppData/Local/.xmake
Expand Down
28 changes: 23 additions & 5 deletions src/BedrockServerClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@


#include <ll/api/Config.h>
#include <ll/api/event/EventBus.h>
#include <ll/api/event/command/ServerCommandRegisterEvent.h>
#include <ll/api/mod/NativeMod.h>
#include <ll/api/mod/RegisterHelper.h>
#include <ll/api/service/Bedrock.h>
#include <ll/api/utils/ErrorUtils.h>

#ifdef TEST
Expand All @@ -17,12 +20,13 @@ struct BedrockServerClientInterface::Impl {
#ifdef TEST
std::unique_ptr<GeometryGroup> geoTest;
std::vector<GeometryGroup::GeoId> gids;
std::set<ll::event::ListenerPtr> eventListeners;
#endif
};

BedrockServerClientInterface::BedrockServerClientInterface()
: self(*ll::mod::NativeMod::current()),
impl(std::make_unique<Impl>()) {}
: impl(std::make_unique<Impl>()),
self(*ll::mod::NativeMod::current()) {}

BedrockServerClientInterface::~BedrockServerClientInterface() = default;

Expand Down Expand Up @@ -60,6 +64,16 @@ bool BedrockServerClientInterface::load() {
if (!loadConfig()) {
return false;
}
#ifdef TEST
impl->eventListeners.emplace(
ll::event::EventBus::getInstance()
.emplaceListener<ll::event::command::ServerCommandRegisterEvent>([this](auto&&) {
getLogger().info("registering test command");
impl->geoTest = GeometryGroup::createDefault();
test::registerTestCommand(impl->geoTest, impl->gids);
})
);
#endif
return true;
}

Expand All @@ -68,9 +82,13 @@ bool BedrockServerClientInterface::enable() {
loadConfig();
}
#ifdef TEST
impl->geoTest = GeometryGroup::createDefault();
test::registerTestCommand(impl->geoTest, impl->gids);
std::thread([this] {
#ifdef LL_PLAT_C
if (ll::service::getLevel()) {
impl->geoTest = GeometryGroup::createDefault();
test::registerTestCommand(impl->geoTest, impl->gids);
}
#endif
std::thread([] {
auto geo = bsci::GeometryGroup::createDefault();
GeometryGroup::GeoId eee{};
auto gid = geo->circle(0, BlockPos{0, 90, 0}.center(), Vec3{1, 1, 1}.normalize(), 8);
Expand Down
2 changes: 1 addition & 1 deletion src/bsci/GeometryGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "mc/world/phys/AABB.h"

#include <mc/deps/core/math/Color.h>
#include <mc/deps/core/utility/AutomaticID.h>
#include <mc/world/level/dimension/DimensionType.h>

namespace bsci {
class GeometryGroup {
Expand Down
81 changes: 37 additions & 44 deletions src/bsci/debug_draw/DebugDrawingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@
#include <mc/network/LoopbackPacketSender.h>
#include <mc/network/MinecraftPacketIds.h>
#include <mc/network/NetEventCallback.h>
#include <mc/network/packet/DebugDrawerPacket.h>
#include <mc/network/packet/DebugDrawerPacketPayload.h>
#include <mc/network/packet/LevelChunkPacket.h>
#include <mc/network/packet/ShapeDataPayload.h>
#include <mc/network/packet/PrimitiveShapeDataPayload.h>
#include <mc/network/packet/PrimitiveShapesPacket.h>
#include <mc/network/packet/PrimitiveShapesPacketPayload.h>
#include <mc/world/level/ChunkPos.h>


TextDataPayload::TextDataPayload() = default;
TextDataPayload::TextDataPayload(TextDataPayload const& cp) { mText = cp.mText; };
// ShapeDataPayload::ShapeDataPayload() = default;
ShapeDataPayload::ShapeDataPayload() { mNetworkId = 0; };
// DebugDrawerPacketPayload::DebugDrawerPacketPayload() = default;
// DebugDrawerPacketPayload::DebugDrawerPacketPayload(DebugDrawerPacketPayload const&) = default;

namespace bsci {
static std::atomic<uint64_t> nextId_{UINT64_MAX};

constexpr size_t shapeDisplayRadius = 48;

class DebugDrawingHandler::Impl {
public:
using PacketPair = std::pair<GeoId, std::vector<std::weak_ptr<DebugDrawerPacket>>>;
using PacketPair = std::pair<GeoId, std::vector<std::weak_ptr<PrimitiveShapesPacket>>>;
struct Hook;
size_t id{};
ll::ConcurrentDenseMap<GeoId, std::vector<std::shared_ptr<DebugDrawerPacket>>>
ll::ConcurrentDenseMap<GeoId, std::vector<std::shared_ptr<PrimitiveShapesPacket>>>
geoPackets; // bool = true 表示启用ParticleSpawner
ll::ConcurrentDenseMap<std::pair<ChunkPos, int>, std::vector<PacketPair>>
chunkPackets; // 不应包含使用ParticleSpawner的GeoId
Expand All @@ -57,17 +49,17 @@ class DebugDrawingHandler::Impl {
}

void
addPacket(GeoId geoId, std::shared_ptr<DebugDrawerPacket>& packet, int dimId, Vec3 const& pos) {
addPacket(GeoId geoId, std::shared_ptr<PrimitiveShapesPacket>& packet, int dimId, Vec3 const& pos) {
if (packet) {
geoPackets.emplace(geoId, std::vector<std::shared_ptr<DebugDrawerPacket>>{packet});
geoPackets.emplace(geoId, std::vector<std::shared_ptr<PrimitiveShapesPacket>>{packet});
auto chunkPos = ChunkPos(pos);
auto key = std::make_pair(chunkPos, dimId);
auto [iter, inserted] = chunkPackets.try_emplace(key);

if (inserted) {
iter->second.emplace_back(
geoId,
std::vector<std::weak_ptr<DebugDrawerPacket>>{packet}
std::vector<std::weak_ptr<PrimitiveShapesPacket>>{packet}
);
} else {
auto it = std::lower_bound(
Expand All @@ -77,13 +69,13 @@ class DebugDrawingHandler::Impl {
compareByGeoId
);
iter->second
.emplace(it, geoId, std::vector<std::weak_ptr<DebugDrawerPacket>>{packet});
.emplace(it, geoId, std::vector<std::weak_ptr<PrimitiveShapesPacket>>{packet});
}
}
// else {
// geoGroup.emplace(
// geoId,
// std::make_pair(std::vector<std::shared_ptr<DebugDrawerPacket>>{}, true)
// std::make_pair(std::vector<std::shared_ptr<PrimitiveShapesPacket>>{}, true)
// );
// }
}
Expand All @@ -110,7 +102,7 @@ LL_TYPE_INSTANCE_HOOK(
const auto& dimId = (int)*levelChunkPacket.mDimensionId;
auto key = std::make_pair(chunkPos, dimId);

std::vector<std::shared_ptr<DebugDrawerPacket>> _chunkPackets;
std::vector<std::shared_ptr<PrimitiveShapesPacket>> _chunkPackets;

{
std::lock_guard l{listMutex};
Expand Down Expand Up @@ -167,11 +159,11 @@ GeometryGroup::GeoId DebugDrawingHandler::line(
Vec3 offset = end - begin;
double len = offset.length();
if (len <= shapeDisplayRadius + 0.5) { // 防止浮点误差导致的无限递归
auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Line;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Line;
shape.mLocation = begin;
shape.mColor = color;
shape.mDimensionId = dim;
Expand Down Expand Up @@ -212,11 +204,11 @@ GeometryGroup::GeoId DebugDrawingHandler::box(
if ((box.max - box.min).lengthSqr() >= shapeDisplayRadius * shapeDisplayRadius)
return Base::box(dim, box, color, thickness);

auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Box;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Box;
shape.mLocation = (box.min + box.max) / 2;
shape.mColor = color;
shape.mDimensionId = dim;
Expand Down Expand Up @@ -246,11 +238,11 @@ GeometryGroup::GeoId DebugDrawingHandler::circle(
return Base::circle(dim, center, normal, radius, color, thickness);
}

auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Circle;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Circle;
shape.mRotation = normal;
shape.mLocation = center;
shape.mScale = radius;
Expand Down Expand Up @@ -280,11 +272,11 @@ GeometryGroup::GeoId DebugDrawingHandler::sphere(
}


auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Sphere;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Sphere;
shape.mLocation = center;
shape.mScale = radius;
shape.mColor = color;
Expand Down Expand Up @@ -315,12 +307,12 @@ GeometryGroup::GeoId DebugDrawingHandler::arrow(
Vec3 offset = end - begin;
double len = offset.length();
if (len <= shapeDisplayRadius + 0.5) { // 防止浮点误差导致的无限递归
auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
auto const& config = BedrockServerClientInterface::getInstance().getConfig().debugDraw;
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Arrow;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Arrow;
shape.mLocation = begin;
shape.mColor = color;
shape.mDimensionId = dim;
Expand Down Expand Up @@ -362,11 +354,11 @@ GeometryGroup::GeoId DebugDrawingHandler::text(
mce::Color const& color,
std::optional<float> scale
) {
auto packet = std::make_shared<DebugDrawerPacket>();
auto packet = std::make_shared<PrimitiveShapesPacket>();
packet->setSerializationMode(SerializationMode::CerealOnly);
ShapeDataPayload shape;
PrimitiveShapeDataPayload shape;
shape.mNetworkId = nextId_.fetch_sub(1);
shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Text;
shape.mShapeType = ::ScriptModuleMinecraft::ScriptPrimitiveShapeType::Text;
shape.mLocation = pos;
shape.mScale = scale;
shape.mColor = color;
Expand All @@ -388,7 +380,7 @@ bool DebugDrawingHandler::remove(GeoId id) {
if (id.value == 0) {
return false;
}
std::vector<std::shared_ptr<DebugDrawerPacket>> removePackets;
std::vector<std::shared_ptr<PrimitiveShapesPacket>> removePackets;
impl->geoPackets.erase_if(id, [this, id, &removePackets](auto&& iter) {
for (auto& packet : iter.second) {
if (packet && !packet->mShapes->empty() && (*packet->mShapes)[0].mLocation->has_value()
Expand Down Expand Up @@ -430,7 +422,7 @@ GeometryGroup::GeoId DebugDrawingHandler::merge(std::span<GeoId> ids) {
}
ll::ConcurrentDenseMap<std::pair<ChunkPos, int>,
std::vector<GeoId>> temMap; // 用来处理包的合并
std::vector<std::shared_ptr<DebugDrawerPacket>> newPackets; // 合并后的包队列
std::vector<std::shared_ptr<PrimitiveShapesPacket>> newPackets; // 合并后的包队列

// 移出旧id的geoPackets
for (auto& id : ids) {
Expand Down Expand Up @@ -466,7 +458,7 @@ GeometryGroup::GeoId DebugDrawingHandler::merge(std::span<GeoId> ids) {
for (auto& [key, data] : temMap) {
impl->chunkPackets.modify_if(key, [this, &data, &newId](auto&& iter) {
// 合并包
std::vector<std::weak_ptr<DebugDrawerPacket>> pkts;
std::vector<std::weak_ptr<PrimitiveShapesPacket>> pkts;
for (auto& id : data) {
auto it = std::lower_bound(
iter.second.begin(),
Expand All @@ -484,7 +476,7 @@ GeometryGroup::GeoId DebugDrawingHandler::merge(std::span<GeoId> ids) {
}
}
// 添加新的包
std::pair<GeoId, std::vector<std::weak_ptr<DebugDrawerPacket>>> newPair =
std::pair<GeoId, std::vector<std::weak_ptr<PrimitiveShapesPacket>>> newPair =
std::make_pair(newId, std::move(pkts));
auto it = std::lower_bound(
iter.second.begin(),
Expand All @@ -508,7 +500,7 @@ bool DebugDrawingHandler::shift(GeoId id, Vec3 const& v) {
return impl->geoPackets.modify_if(id, [this, id, v](auto&& iter) {
ll::ConcurrentDenseMap<
std::pair<ChunkPos, int>,
std::vector<std::weak_ptr<DebugDrawerPacket>>>
std::vector<std::weak_ptr<PrimitiveShapesPacket>>>
temMap; // 用来处理shape跨区块

std::erase_if(iter.second, [&temMap, &v](auto&& packet) {
Expand All @@ -527,15 +519,16 @@ bool DebugDrawingHandler::shift(GeoId id, Vec3 const& v) {
if (std::holds_alternative<ArrowDataPayload>(*shape.mExtraDataPayload)) {
std::get<ArrowDataPayload>(*shape.mExtraDataPayload)
.mEndLocation->value() += v;
} else if (std::holds_alternative<LineDataPayload>(*shape.mExtraDataPayload
} else if (std::holds_alternative<LineDataPayload>(
*shape.mExtraDataPayload
)) {
*std::get<LineDataPayload>(*shape.mExtraDataPayload).mEndLocation += v;
}
}
}
auto newChunkPos = ChunkPos((*packet->mShapes)[0].mLocation->value());
auto [it, inserted] = temMap.try_emplace(std::make_pair(newChunkPos, dimId));
it->second.emplace_back(std::weak_ptr<DebugDrawerPacket>(packet));
it->second.emplace_back(std::weak_ptr<PrimitiveShapesPacket>(packet));
return false;
}
return true;
Expand Down
15 changes: 12 additions & 3 deletions src/bsci/particle/ParticleSpawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include <mc/world/level/dimension/Dimension.h>


MolangScriptArg::MolangScriptArg() = default;
MolangMemberArray::MolangMemberArray() = default;
MolangMemberArray::MolangMemberArray(MolangMemberArray const&) = default;
SpawnParticleEffectPacket::SpawnParticleEffectPacket(SpawnParticleEffectPacket const&) = default;
// MolangScriptArg::MolangScriptArg() = default;
// MolangVariableMap::MolangVariableMap(MolangVariableMap const& rhs) {
// mMapFromVariableIndexToVariableArrayOffset =
// rhs.mMapFromVariableIndexToVariableArrayOffset; mVariables = {}; for (auto& ptr :
Expand Down Expand Up @@ -211,8 +214,14 @@ GeometryGroup::GeoId ParticleSpawner::merge(std::span<GeoId> ids) {
std::vector<GeoId> res;
res.reserve(ids.size());
for (auto const& sid : ids) {
if (!impl->geoGroup.erase_if(sid, [this, &res](auto&& iter) {
res.append_range(std::move(iter.second));
if (!impl->geoGroup.erase_if(sid, [&res](auto&& iter) {
auto& source = iter.second;
res.reserve(res.size() + source.size());
res.insert(
res.end(),
std::make_move_iterator(source.begin()),
std::make_move_iterator(source.end())
);
return true;
})) {
res.push_back(sid);
Expand Down
Loading