载入中...
搜索中...
未找到
BuildingAssemblyProtocol.cpp
浏览该文件的文档.
2#include <cmath>
3#include <limits>
6namespace eve::building {
7using namespace assembly_detail;
8namespace {
9Result<void> checkWirePayload(const Value& value, size_t& remaining, size_t depth = 0) {
10 if (remaining == 0 || (depth >= 64 && (value.isArray() || value.isObject())) ||
11 (value.isDouble() && !std::isfinite(value.asDouble())))
12 return fail<void>("House protocol response exceeds its finite 100000-value / 64-depth budget");
13 --remaining;
14 if (value.isArray()) {
15 for (size_t i = 0; i < value.arraySize(); ++i) {
16 auto result = checkWirePayload(value.at(i), remaining, depth + 1);
17 if (!result.ok()) return result;
18 }
19 } else if (value.isObject()) {
20 for (const auto& key : value.keys()) {
21 auto result = checkWirePayload(*value.find(key), remaining, depth + 1);
22 if (!result.ok()) return result;
23 }
24 }
25 return Result<void>::success();
26}
27} // namespace
28
30 const auto* op = request.find("op");
31 const auto* version = request.find("version");
32 if (!op || !op->isString() || !version || !version->isInt64() || version->asInt() != 1)
33 return fail<Value>("House request requires version 1 and operation");
34 auto name = op->asString();
35 if (name == "encode") {
36 auto value = request.find("value");
37 if (!value) return fail<Value>("Missing value to encode");
38 auto json = value->toJson();
39 if (!json.ok()) return Result<Value>::failure(json.status());
40 return Result<Value>::success(std::move(json).takeValue());
41 }
42 if (name == "decode") {
43 auto json = request.find("json");
44 if (!json || !json->isString()) return fail<Value>("Missing JSON text");
45 return Value::fromJson(json->asString());
46 }
47 if (name == "validateBlueprint") {
48 auto value = request.find("blueprint");
49 if (!value) return fail<Value>("Missing blueprint");
50 auto blueprint = AssemblyService::decodeBlueprint(*value);
51 if (!blueprint.ok()) return Result<Value>::failure(blueprint.status());
52 return AssemblyService::encodeBlueprint(blueprint.value());
53 }
54 if (name == "snapshot") return AssemblyService::snapshotWorld(w);
55 if (name == "restore" || name == "applyReceipt") {
56 auto desired = request.find("state");
57 if (!desired) return fail<Value>("Missing desired state");
58 auto expected = request.find("expected");
59 if (name == "applyReceipt" && !expected) return fail<Value>("Receipt needs expected state");
60 auto result = expected ? AssemblyService::applyReceipt(w, *expected, *desired)
62 if (!result.ok()) return Result<Value>::failure(result.status());
64 }
65 if (name == "capture") {
66 auto id = request.find("blueprintId"), ids = request.find("members"), origin = request.find("pose");
67 if (!id || !id->isString() || !ids || !ids->isArray() || !origin)
68 return fail<Value>("Capture needs blueprintId, members and pose");
69 auto guid = AssetGuid::parse(id->asString());
70 auto pose = parsePose(*origin);
71 std::vector<int> members;
72 const bool poseOk = pose.ok();
73 if (!guid || !poseOk) return fail<Value>("Invalid blueprint identity or capture pose");
74 for (const auto& member : *ids->getIf<Value::Array>()) {
75 if (!member.isInt64() || member.asInt() <= 0 || member.asInt() > std::numeric_limits<int>::max())
76 return fail<Value>("Invalid member key");
77 members.push_back(int(member.asInt()));
78 }
79 auto b = AssemblyService::capture(w, members, *guid, pose.value());
80 if (!b.ok()) return Result<Value>::failure(b.status());
81 return AssemblyService::encodeBlueprint(b.value());
82 }
83 auto id = request.find("assemblyId");
84 if (!id || !id->isString()) return fail<Value>("Missing house identity");
85 auto identity = AssemblyId::parse(id->asString());
86 if (!identity) return fail<Value>("Invalid house identity");
87 if (name == "members") {
89 if (!a.ok()) return Result<Value>::failure(a.status());
91 for (const auto& [key, member] : a.value().members)
92 parts.emplace_back(codec::instanceValue(codec::snapshot(w.buildings().at(member))));
93 return Result<Value>::success(std::move(parts));
94 }
95 auto prepare = [&]() -> Result<PreparedAssembly> {
96 if (name == "remove") return AssemblyService::prepareRemoval(w, *identity);
97 if (name == "remodel") {
98 auto members = request.find("members");
99 if (!members || !members->isArray()) return fail<PreparedAssembly>("Missing remodel members");
100 std::vector<PlacedBuilding> parts;
101 for (const auto& value : *members->getIf<Value::Array>()) {
102 // Canonical instance decoder requires a positive record id; new members use
103 // explicit new=true and discard that temporary wire key before admission.
105 if (!p.ok()) return Result<PreparedAssembly>::failure(p.status());
106 auto runtime = codec::runtime(p.value());
107 if (auto add = value.find("new"); add && add->isBool() && add->asBool()) runtime.instanceId = 0;
108 parts.push_back(std::move(runtime));
109 }
111 }
112 auto poseValue = request.find("pose");
113 if (!poseValue) return fail<PreparedAssembly>("Missing target pose");
114 auto pose = parsePose(*poseValue);
115 if (!pose.ok()) return Result<PreparedAssembly>::failure(pose.status());
116 if (name == "move") return AssemblyService::prepareMove(w, *identity, pose.value());
117 if (name == "place" || name == "preview") {
118 auto blueprint = request.find("blueprint");
119 if (!blueprint) return fail<PreparedAssembly>("Missing blueprint");
120 auto b = AssemblyService::decodeBlueprint(*blueprint);
121 if (!b.ok()) return Result<PreparedAssembly>::failure(b.status());
122 return AssemblyService::preparePlacement(w, b.value(), *identity, pose.value());
123 }
124 return fail<PreparedAssembly>("Unknown house operation", DiagnosticCode::Unsupported);
125 };
126 auto token = prepare();
127 if (!token.ok()) return Result<Value>::failure(token.status());
128 if (name == "preview") {
130 for (const auto& p : token.value().preview()) parts.emplace_back(codec::instanceValue(codec::snapshot(p)));
131 return Result<Value>::success(std::move(parts));
132 }
133 // Reserve the complete wire receipt before publishing the mutation. A script must
134 // never receive ok/null after the native world has already changed.
135 auto after = AssemblyService::snapshotWorld(*token.value().data_->candidate);
136 if (!after.ok()) return Result<Value>::failure(after.status());
137 size_t remaining = 99998; // receipt object and assemblyId
138 auto beforeBudget = checkWirePayload(token.value().data_->before, remaining, 1);
139 if (!beforeBudget.ok()) return Result<Value>::failure(beforeBudget.status());
140 auto afterBudget = checkWirePayload(after.value(), remaining, 1);
141 if (!afterBudget.ok()) return Result<Value>::failure(afterBudget.status());
142 auto committed = AssemblyService::commit(w, std::move(token).takeValue());
143 if (!committed.ok()) return Result<Value>::failure(committed.status());
144 auto receipt = std::move(committed).takeValue();
145 return Result<Value>::success(Value::Object{{"assemblyId", receipt.assembly.format()},
146 {"before", std::move(receipt.before)},
147 {"after", std::move(receipt.after)}});
148}
149} // namespace eve::building
float w
Definition AnimClip.cpp:738
std::vector< eve::artifact::PartView > parts
eve::EntitySpatialPose pose
bool committed
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
const GltfImportRequest & request
std::string id
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanGpuFrameToken token
glm::dvec3 origin
Object::Ptr value
float(ui::Theme::* member)[4]
std::map< std::string, std::span< const std::uint8_t > > members
std::uint32_t depth
uint64_t identity
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
static Result< PreparedAssembly > prepareRemodel(const PlacementWorld &world, AssemblyId id, std::span< const PlacedBuilding > members)
Prepare replacement of a house's complete member set for independent remodelling.
static Result< PreparedAssembly > prepareRemoval(const PlacementWorld &world, AssemblyId id)
Prepare removal of all house members, rejecting external structural dependents. @cost One whole-world...
static Result< void > restoreWorld(PlacementWorld &world, const Value &value)
Validate and stage a complete saved world; failure preserves the old world. @cost Builds a complete r...
static Result< AssemblyReceipt > commit(PlacementWorld &world, PreparedAssembly token)
Consume a prepared operation if world and definition preconditions still match. @cost Compares owning...
static Result< Value > snapshotWorld(const PlacementWorld &world)
Capture the complete logical world, including grid, terrain, curves and assemblies....
static Result< BuildingBlueprint > decodeBlueprint(const Value &value)
Validate and decode an untrusted blueprint without publishing state. @cost Validates all parts/depend...
static Result< void > applyReceipt(PlacementWorld &world, const Value &expected, const Value &desired)
Apply a receipt endpoint only when the current world matches the expected endpoint....
static Result< PreparedAssembly > prepareMove(const PlacementWorld &world, AssemblyId id, AssemblyPose pose)
Prepare a rigid move of the current members, including instance modifications. @cost One whole-world ...
static Result< BuildingAssembly > assembly(const PlacementWorld &world, AssemblyId id)
Query an owning membership snapshot, or NotFound; caller-thread only.
static Result< Value > encodeBlueprint(const BuildingBlueprint &blueprint)
Encode a blueprint in the versioned canonical schema. @cost Copies and validates all parts and curve ...
static Result< PreparedAssembly > preparePlacement(const PlacementWorld &world, const BuildingBlueprint &blueprint, AssemblyId id, AssemblyPose pose)
Prepare one complete instance of a pinned blueprint in the target world. @cost Copies world/house sta...
static Result< BuildingBlueprint > capture(const PlacementWorld &world, std::span< const int > members, eve::AssetGuid id, AssemblyPose origin)
Capture an explicit, structurally closed selection relative to a grid vertex. @cost Copies selected m...
格子型建筑放置世界(脚本可直接操作)。
static std::optional< Id128 > parse(std::string_view text) noexcept
Parses canonical UUID text.
Definition Identity.h:150
EVENGINE_API_FOUNDATION public API.
Definition Json.h:39
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
Result< AssemblyPose > parsePose(const Value &v)
Result< BuildingInstanceSnapshot > parseInstance(const Value &value)
Decode legacy/current instance fields without mutating a world; owning result.
building::PlacedBuilding runtime(const BuildingInstanceSnapshot &value)
Decode a previously validated snapshot into an owning runtime candidate.
BuildingInstanceSnapshot snapshot(const building::PlacedBuilding &placed)
Copy a runtime instance into its canonical owning persistence record.
Value instanceValue(const BuildingInstanceSnapshot &instance)
Encode a complete owning instance; pure, caller-thread, linear in instance metadata.
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
Result< Value > executeHouseRequest(PlacementWorld &w, const Value &request)
Execute the version-1 house authoring protocol used by scripts and tools. @ownership Synchronously bo...
void keys(const Value &value, std::initializer_list< const char * > names)