载入中...
搜索中...
未找到
ProcgenSceneSink.cpp
浏览该文件的文档.
2#include <unordered_map>
3#include <unordered_set>
4
5namespace eve {
7 const std::vector<ProcgenInstanceDesc>& current, uint64_t currentRevision, const ProcgenInstanceDelta& delta) {
9 if (delta.baseRevision == 0 || delta.targetRevision == 0 || currentRevision != delta.baseRevision ||
10 delta.targetRevision != delta.baseRevision + 1)
12 "procedural scene delta revision is stale", "baseRevision"));
13
14 std::unordered_map<std::string, eve::ProcgenInstanceDesc> staged;
15 std::unordered_map<uint64_t, std::string> stagedPointIds;
16 for (const auto& instance : current) {
17 if (instance.id.empty() || !staged.emplace(instance.id, instance).second ||
18 (instance.sourcePointId && !stagedPointIds.emplace(instance.sourcePointId, instance.id).second))
19 return Output::failure(Diagnostic::error(
20 DiagnosticCode::Conflict, "committed procedural snapshot contains ambiguous identities", "current"));
21 }
22 std::unordered_set<std::string> removedIds;
23 removedIds.reserve(delta.removedPointIds.size() + delta.removed.size());
24 for (const auto pointId : delta.removedPointIds) {
25 const auto found = stagedPointIds.find(pointId);
26 if (pointId == 0 || found == stagedPointIds.end() || !removedIds.insert(found->second).second)
28 "procedural scene delta removes an unknown source PointId",
29 "removedPointIds"));
30 }
31 for (const auto& id : delta.removed)
32 if (id.empty() || !removedIds.insert(id).second)
33 return Output::failure(eve::Diagnostic::error(
34 eve::DiagnosticCode::Conflict, "procedural scene delta repeats a removed identity", "removed"));
35 for (const auto& id : removedIds) {
36 const auto found = staged.find(id);
37 if (found == staged.end())
38 return Output::failure(eve::Diagnostic::error(
39 eve::DiagnosticCode::Conflict, "procedural scene delta removes an unknown identity", "removed"));
40 if (found->second.sourcePointId != 0) stagedPointIds.erase(found->second.sourcePointId);
41 staged.erase(found);
42 }
43 for (const auto& instance : delta.updated) {
44 if (instance.id.empty() || instance.sourcePointId == 0)
45 return Output::failure(
47 "procedural scene delta updates require an id and source PointId", "updated"));
48 const auto identity = stagedPointIds.find(instance.sourcePointId);
49 if (identity == stagedPointIds.end())
50 return Output::failure(eve::Diagnostic::error(
51 eve::DiagnosticCode::Conflict, "procedural scene delta updates an unknown identity", "updated"));
52 if (identity->second != instance.id && staged.find(instance.id) != staged.end())
53 return Output::failure(
55 "procedural scene delta update renames to a duplicate identity", "updated"));
56 staged.erase(identity->second);
57 staged.emplace(instance.id, instance);
58 identity->second = instance.id;
59 }
60 for (const auto& instance : delta.added) {
61 if (instance.id.empty() || instance.sourcePointId == 0 ||
62 stagedPointIds.find(instance.sourcePointId) != stagedPointIds.end() ||
63 !staged.emplace(instance.id, instance).second)
65 "procedural scene delta adds a duplicate identity", "added"));
66 stagedPointIds.emplace(instance.sourcePointId, instance.id);
67 }
68 if (!delta.targetPointOrder.empty() && !delta.targetOrder.empty())
69 return Output::failure(eve::Diagnostic::error(
71 "procedural scene delta cannot mix PointId and compatibility target orders", "targetPointOrder"));
72 std::vector<std::string> resolvedOrder;
73 if (!delta.targetPointOrder.empty()) {
74 resolvedOrder.reserve(delta.targetPointOrder.size());
75 std::unordered_set<uint64_t> orderedPointIds;
76 for (const auto pointId : delta.targetPointOrder) {
77 const auto found = stagedPointIds.find(pointId);
78 if (pointId == 0 || found == stagedPointIds.end() || !orderedPointIds.insert(pointId).second)
79 return Output::failure(eve::Diagnostic::error(
81 "procedural scene delta target order has an unknown or duplicate source PointId",
82 "targetPointOrder"));
83 resolvedOrder.push_back(found->second);
84 }
85 } else {
86 resolvedOrder = delta.targetOrder;
87 }
88 if (staged.size() != resolvedOrder.size())
89 return Output::failure(eve::Diagnostic::error(
90 eve::DiagnosticCode::Conflict, "procedural scene delta target order is incomplete", "targetOrder"));
91 std::vector<eve::ProcgenInstanceDesc> target;
92 target.reserve(resolvedOrder.size());
93 for (const auto& id : resolvedOrder) {
94 auto found = staged.find(id);
95 if (found == staged.end())
97 "procedural scene delta target order has unknown identity",
98 "targetOrder"));
99 target.push_back(found->second);
100 staged.erase(found);
101 }
102 if (!staged.empty())
103 return Output::failure(eve::Diagnostic::error(
104 eve::DiagnosticCode::Conflict, "procedural scene delta target order repeats an identity", "targetOrder"));
105 return Output::success(std::move(target));
106}
107} // namespace eve
LogicalId target
bool found
double current
uint64_t identity
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Result< std::vector< ProcgenInstanceDesc > > buildProcgenInstanceDeltaSnapshot(const std::vector< ProcgenInstanceDesc > &current, uint64_t currentRevision, const ProcgenInstanceDelta &delta)
Build an owning target snapshot from one exact revision and identity delta; does not publish state.
Identity-based, revision-checked mutation of one procedural scene batch.
std::vector< uint64_t > removedPointIds
Stable source PointIds removed from the target batch.
std::vector< std::string > targetOrder
Compatibility target order addressed directly by Scene instance id.
std::vector< ProcgenInstanceDesc > updated
std::vector< std::string > removed
Compatibility removals addressed directly by Scene instance id.
std::vector< uint64_t > targetPointOrder
Exact target order addressed by stable source PointId.
std::vector< ProcgenInstanceDesc > added