载入中...
搜索中...
未找到
RoadNetworkCodec.cpp
浏览该文件的文档.
2#include <cmath>
3#include <limits>
4#include <utility>
5namespace eve::procgen::road {
6namespace {
8#include "procgen/road/RoadValueReaders.inc"
9bool readSmallInt(const RoadCodecValue&, const char*, int, int, int&);
10RoadCodecValue encodeStyle(std::uint32_t, const RoadStyle&);
11bool decodeStyle(const RoadCodecValue&, std::uint32_t&, RoadStyle&);
12#include "procgen/road/RoadConnectionCodec.inc"
13#include "procgen/road/RoadPointCodec.inc"
14#include "procgen/road/RoadRecordCodec.inc"
15#include "procgen/road/RoadStyleCodec.inc"
16template <class T>
17Result<T> invalid(const char* message) {
19}
20bool withinBudget(const eve::Value& value, std::size_t depth, std::size_t& elements, std::size_t& bytes) {
21 if (depth > 64 || ++elements > 262144) return false;
22 if (const auto* text = value.getIf<std::string>()) bytes += text->size();
23 if (const auto* array = value.getIf<eve::Value::Array>()) {
24 if (array->size() > 262144 - elements) return false;
25 for (const auto& item : *array)
26 if (!withinBudget(item, depth + 1, elements, bytes)) return false;
27 }
28 if (const auto* object = value.getIf<eve::Value::Object>()) {
29 if (object->size() > 262144 - elements) return false;
30 for (const auto& [key, item] : *object) {
31 bytes += key.size();
32 if (bytes > 8u * 1024u * 1024u || !withinBudget(item, depth + 1, elements, bytes)) return false;
33 }
34 }
35 return bytes <= 8u * 1024u * 1024u;
36}
37} // namespace
39 auto valid = network.validate();
40 if (!valid.ok()) return Result<eve::Value>::failure(valid.status());
41 if (network.nodes().size() > 65536 || network.edges().size() > 65536 || network.laneLinks().size() > 262144 ||
42 network.blockedLaneLinks().size() > 262144)
43 return invalid<eve::Value>("Road snapshot exceeds the graph budget");
44 for (const auto& edge : network.edges())
45 if (edge.controlPoints.size() > 4096) return invalid<eve::Value>("Road snapshot exceeds the marker budget");
46 RoadCodecValue::Array nodes;
47 nodes.reserve(network.nodes().size());
48 for (const auto& node : network.nodes()) nodes.push_back(encodeNodeRecord(node));
49
50 RoadCodecValue::Array edges;
51 edges.reserve(network.edges().size());
52 for (const auto& edge : network.edges()) edges.push_back(encodeEdgeRecord(edge, {}, {}));
53
54 RoadCodecValue::Array laneLinks;
55 laneLinks.reserve(network.laneLinks().size());
56 for (const auto& link : network.laneLinks()) laneLinks.push_back(encodeLink(link));
57 RoadCodecValue::Array blockedLaneLinks;
58 blockedLaneLinks.reserve(network.blockedLaneLinks().size());
59 for (const auto& link : network.blockedLaneLinks()) blockedLaneLinks.push_back(encodeLink(link));
60
61 eve::Value snapshot = RoadCodecValue::Object{
62 {"schema", "eve.procgen.roadNetwork"}, {"schemaVersion", std::int64_t{1}},
63 {"nodes", std::move(nodes)}, {"edges", std::move(edges)},
64 {"laneLinks", std::move(laneLinks)}, {"blockedLaneLinks", std::move(blockedLaneLinks)}};
65 std::size_t elements = 0, bytes = 0;
66 if (!withinBudget(snapshot, 0, elements, bytes))
67 return invalid<eve::Value>("Road snapshot exceeds the value budget");
68 return Result<eve::Value>::success(std::move(snapshot));
69}
71 std::size_t elements = 0, bytes = 0;
72 if (!withinBudget(snapshot, 0, elements, bytes))
73 return invalid<RoadNetwork>("Road snapshot exceeds the value budget");
74 const auto* schemaValue = field(snapshot, "schema");
75 const auto* versionValue = field(snapshot, "schemaVersion");
76 const auto* nodesValue = field(snapshot, "nodes");
77 const auto* edgesValue = field(snapshot, "edges");
78 const auto* linksValue = field(snapshot, "laneLinks");
79 const auto* blockedValue = field(snapshot, "blockedLaneLinks");
80 const auto* schema = schemaValue ? schemaValue->getIf<std::string>() : nullptr;
81 const auto* version = versionValue ? versionValue->getIf<std::int64_t>() : nullptr;
82 const auto* nodes = nodesValue ? nodesValue->getIf<RoadCodecValue::Array>() : nullptr;
83 const auto* edges = edgesValue ? edgesValue->getIf<RoadCodecValue::Array>() : nullptr;
84 const auto* links = linksValue ? linksValue->getIf<RoadCodecValue::Array>() : nullptr;
85 const auto* blocked = blockedValue ? blockedValue->getIf<RoadCodecValue::Array>() : nullptr;
86 if (!schema || *schema != "eve.procgen.roadNetwork" || !version || *version != 1 || !nodes || !edges || !links ||
87 (blockedValue && !blocked))
88 return invalid<RoadNetwork>("Road snapshot schema is unsupported");
89 constexpr std::size_t maximumNodes = 65536u, maximumEdges = 65536u, maximumLinks = 262144u;
90 if (nodes->size() > maximumNodes || edges->size() > maximumEdges || links->size() > maximumLinks ||
91 (blocked && blocked->size() > maximumLinks))
92 return invalid<RoadNetwork>("Road snapshot exceeds the graph budget");
93
95 for (const auto& encoded : *nodes) {
97 if (!decodeNodeRecord(encoded, node)) return invalid<RoadNetwork>("Road snapshot contains an invalid node");
98 auto restored = candidate.restoreNode(node);
99 if (!restored.ok()) return Result<RoadNetwork>::failure(restored.status());
100 }
101 for (const auto& encoded : *edges) {
103 std::vector<procgen::road::RoadLaneConnection> embeddedLinks;
104 if (!decodeEdgeRecord(encoded, edge, embeddedLinks, nullptr) || !embeddedLinks.empty())
105 return invalid<RoadNetwork>("Road snapshot contains an invalid edge");
106 auto restored = candidate.restoreEdge(std::move(edge));
107 if (!restored.ok()) return Result<RoadNetwork>::failure(restored.status());
108 }
109 for (const auto& encoded : *links) {
111 if (!decodeLink(encoded, link)) return invalid<RoadNetwork>("Road snapshot contains an invalid lane link");
112 auto restored = candidate.addLaneLink(link);
113 if (!restored.ok()) return Result<RoadNetwork>::failure(restored.status());
114 }
115 if (blocked) {
116 for (const auto& encoded : *blocked) {
118 if (!decodeLink(encoded, link))
119 return invalid<RoadNetwork>("Road snapshot contains an invalid blocked lane link");
120 auto restored = candidate.blockLaneLink(link);
121 if (!restored.ok()) return Result<RoadNetwork>::failure(restored.status());
122 if (restored.value())
123 return invalid<RoadNetwork>("Road snapshot marks the same lane link active and blocked");
124 }
125 }
126 auto valid = candidate.validate();
127 if (!valid.ok()) return Result<RoadNetwork>::failure(valid.status());
128 return Result<RoadNetwork>::success(std::move(candidate));
129}
130} // namespace eve::procgen::road
SQObjectPtr key
Definition ECS.cpp:155
std::string message
PhysicsJointLink link
std::vector< Edge > edges
std::map< std::string, Node > nodes
Snapshot snapshot
std::uint64_t bytes
bool valid
Object::Ptr value
const RoadNode * node
std::string text
std::size_t elements
Json object
std::uint32_t depth
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
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Owning directed road graph with lane connectivity.
Definition RoadNetwork.h:19
const std::vector< RoadEdge > & edges() const noexcept
Edges.
Result< void > addLaneLink(RoadLaneConnection link)
Register one legal turn inside a shared junction node.
Result< void > restoreNode(RoadNode node)
Restore a node with an explicit stable id for undo/import paths.
Result< void > validate() const
Validate graph, endpoint and lane-link invariants without mutation.
Result< bool > blockLaneLink(RoadLaneConnection link)
Persistently forbid one exact legal lane connection and remove it if currently active.
const std::vector< RoadLaneConnection > & laneLinks() const noexcept
Lane links.
const std::vector< RoadLaneConnection > & blockedLaneLinks() const noexcept
Borrow deterministic persistent turn prohibitions until the next network mutation.
Result< void > restoreEdge(RoadEdge edge)
Restore an edge with an explicit stable id for undo/import paths.
const std::vector< RoadNode > & nodes() const noexcept
Nodes.
Result< int > invalid(std::string message)
Invalid.
Result< eve::Value > captureRoadNetwork(const RoadNetwork &network)
Capture validated, owning author data using the editor-compatible roadNetwork schema v1....
Result< RoadNetwork > restoreRoadNetwork(const eve::Value &snapshot)
Decode schema v1 privately; unknown fields are ignored and unsupported versions rejected.
bool readSmallInt(const EditorValue &value, const char *name, int minimum, int maximum, int &output)
Directed centerline edge with optional reverse lanes.
Definition RoadTypes.h:286
std::vector< RoadControlPoint > controlPoints
Definition RoadTypes.h:290
One legal lane-to-lane connection inside a junction.
Definition RoadTypes.h:315
Junction node owned by a road network.
Definition RoadTypes.h:275