载入中...
搜索中...
未找到
ArtifactProvider.cpp
浏览该文件的文档.
2#include "common/ValueRead.h"
3
4#include "common/Capability.h"
5
6#include <algorithm>
7#include <charconv>
8#include <limits>
9#include <memory>
10#include <type_traits>
11#include <unordered_set>
12#include <utility>
13
14namespace eve::map {
15
16const eve::Value* member(const eve::Value::Object& object, const char* name) noexcept {
17 const auto found = object.find(name);
18 return found == object.end() ? nullptr : &found->second;
19}
20
21bool readString(const eve::Value::Object& object, const char* name, std::string& output) {
23 if (!text) return false;
24 output = std::move(text.value());
25 return true;
26}
27
28bool readU64(const eve::Value::Object& object, const char* name, std::uint64_t& output) {
29 auto parsed = eve::scanUnsignedStringMember(object, name);
30 if (!parsed) return false;
31 output = parsed.value().value;
32 return parsed.value().consumed == parsed.value().size;
33}
34
35bool readI64(const eve::Value::Object& object, const char* name, std::int64_t& output) {
36 const auto* value = member(object, name);
37 if (!value) return false;
38 auto parsed = eve::readInt64Value(*value);
39 if (!parsed) return false;
40 output = parsed.value();
41 return true;
42}
43
44bool hasSupportedVersion(const eve::Value::Object& object) noexcept {
45 const auto* encoded = member(object, "version");
46 const auto* version = encoded ? encoded->getIf<std::int64_t>() : nullptr;
47 return version && *version == 1;
48}
49
51public:
54 void commit() noexcept override {
55 if (!owner_) return;
56 owner_->commit(std::move(record_));
57 owner_ = nullptr;
58 }
59 void rollback() noexcept override {
60 owner_ = nullptr;
61 record_ = {};
62 }
63
64private:
65 MapArtifactProvider* owner_ = nullptr;
66 MapArtifactRecord record_;
67};
69 const eve::artifact::PublicationView& publication) {
70 if (failPrepare_)
72 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "map artifact prepare failure was injected"));
73 if (publication.id.isNil() || publication.buildKey.empty())
75 eve::DiagnosticCode::InvalidArgument, "map publication requires identity and build key"));
76 if (find(publication.id) != nullptr)
78 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "map artifact identity is already published"));
79
80 const eve::artifact::PartView* topology = nullptr;
81 for (const auto& part : publication.parts) {
82 if (part.kind == eve::artifact::PartKind::Grid && (part.role == "topology" || topology == nullptr))
83 topology = &part;
84 }
85 if (!topology || topology->width <= 0 || topology->height <= 0 ||
86 topology->cells.size() !=
87 static_cast<std::size_t>(topology->width) * static_cast<std::size_t>(topology->height))
88 return eve::Result<std::unique_ptr<eve::artifact::PreparedPublication>>::failure(eve::Diagnostic::error(
89 eve::DiagnosticCode::PreconditionViolation, "map publication requires a complete grid topology"));
90 if (nextIndex_ == MapArtifactHandle::invalidIndex)
92 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "map artifact handle index exhausted"));
93
95 record.id = publication.id;
96 record.buildKey = std::string(publication.buildKey);
97 record.handle = MapArtifactHandle(nextIndex_, 1);
98 record.width = topology->width;
99 record.height = topology->height;
100 try {
101 record.cells.assign(topology->cells.begin(), topology->cells.end());
102 records_.reserve(records_.size() + 1);
104 std::make_unique<MapArtifactStage>(*this, std::move(record)));
105 } catch (...) {
107 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "map artifact prepare allocation failed"));
108 }
109}
110
111void MapArtifactProvider::commit(MapArtifactRecord record) noexcept {
112 records_.push_back(std::move(record));
113 ++nextIndex_;
114}
115
117 const auto found =
118 std::find_if(records_.begin(), records_.end(), [id](const auto& record) { return record.id == id; });
119 return found == records_.end() ? nullptr : &*found;
120}
121
122std::optional<std::uint32_t> MapArtifactProvider::cell(eve::PersistentId id, std::int32_t x,
123 std::int32_t y) const noexcept {
124 const auto* record = find(id);
125 if (!record || x < 0 || y < 0 || x >= record->width || y >= record->height) return std::nullopt;
126 return record
127 ->cells[static_cast<std::size_t>(y) * static_cast<std::size_t>(record->width) + static_cast<std::size_t>(x)];
128}
129
131 records_.clear();
132 nextIndex_ = 0;
133 failPrepare_ = false;
134}
135
137 try {
138 eve::Value::Array records;
139 records.reserve(records_.size());
140 for (const auto& record : records_) {
142 cells.reserve(record.cells.size());
143 for (const auto value : record.cells) cells.emplace_back(std::int64_t(value));
145 item.emplace("artifactId", eve::Value(record.id.format()));
146 item.emplace("buildKey", eve::Value(record.buildKey));
147 item.emplace("handle", eve::Value(std::to_string(record.handle.packed())));
148 item.emplace("width", eve::Value(std::int64_t(record.width)));
149 item.emplace("height", eve::Value(std::int64_t(record.height)));
150 item.emplace("cells", eve::Value(std::move(cells)));
151 records.emplace_back(std::move(item));
152 }
154 state.emplace("provider", eve::Value("map.cpu-registry"));
155 state.emplace("version", eve::Value(std::int64_t(1)));
156 state.emplace("records", eve::Value(std::move(records)));
158 } catch (...) {
160 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "map artifact snapshot allocation failed"));
161 }
162}
163
165 if (!records_.empty())
167 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "map artifact restore requires an empty registry"));
168 const auto* object = state.getIf<eve::Value::Object>();
169 const auto* provider = object ? member(*object, "provider") : nullptr;
170 const auto* providerName = provider ? provider->getIf<std::string>() : nullptr;
171 const auto* encodedRecords = object ? member(*object, "records") : nullptr;
172 const auto* records = encodedRecords ? encodedRecords->getIf<eve::Value::Array>() : nullptr;
173 if (!object || !providerName || *providerName != "map.cpu-registry" || !hasSupportedVersion(*object) || !records)
175 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "invalid map artifact provider state"));
176
177 std::vector<MapArtifactRecord> candidate;
178 std::unordered_set<eve::PersistentId> identities;
179 std::uint32_t next = 0;
180 try {
181 candidate.reserve(records->size());
182 for (const eve::Value& encoded : *records) {
183 const auto* item = encoded.getIf<eve::Value::Object>();
184 std::string idText;
185 std::string buildKey;
186 std::uint64_t packed = 0;
187 std::int64_t width = 0;
188 std::int64_t height = 0;
189 const auto* encodedCells = item ? member(*item, "cells") : nullptr;
190 const auto* cells = encodedCells ? encodedCells->getIf<eve::Value::Array>() : nullptr;
191 if (!item || !readString(*item, "artifactId", idText) || !readString(*item, "buildKey", buildKey) ||
192 !readU64(*item, "handle", packed) || !readI64(*item, "width", width) ||
193 !readI64(*item, "height", height) || width <= 0 || height <= 0 || !cells)
195 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "invalid map artifact record"));
196 const auto parsedId = eve::PersistentId::parse(idText);
197 const auto handle = MapArtifactHandle::fromPacked(packed);
198 if (!parsedId || parsedId->isNil() || handle.isInvalid() ||
199 width > std::numeric_limits<std::int32_t>::max() || height > std::numeric_limits<std::int32_t>::max() ||
200 !identities.emplace(*parsedId).second ||
201 static_cast<std::uint64_t>(width) * static_cast<std::uint64_t>(height) != cells->size())
203 eve::DiagnosticCode::ParseError, "invalid map artifact dimensions or identity"));
205 record.id = *parsedId;
206 record.buildKey = std::move(buildKey);
207 record.handle = handle;
208 record.width = static_cast<std::int32_t>(width);
209 record.height = static_cast<std::int32_t>(height);
210 record.cells.reserve(cells->size());
211 for (const eve::Value& value : *cells) {
212 const auto* cell = value.getIf<std::int64_t>();
213 if (!cell || *cell < 0 || *cell > std::numeric_limits<std::uint32_t>::max())
215 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "invalid map topology cell"));
216 record.cells.push_back(static_cast<std::uint32_t>(*cell));
217 }
218 next = std::max(next, handle.index() + 1u);
219 candidate.push_back(std::move(record));
220 }
221 } catch (...) {
223 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "map artifact restore allocation failed"));
224 }
225 records_.swap(candidate);
226 nextIndex_ = next;
228}
229
234
235void registerMapArtifactProvider() { eve::cap::provide<eve::artifact::IMapArtifactAdapter>(&mapArtifactProvider()); }
236
237} // namespace eve::map
double y
double x
std::string output
ScriptComponentRecord record
Definition ECS.cpp:228
std::string name
Definition ECS.cpp:154
std::uint32_t height
std::uint32_t width
std::shared_ptr< OceanGpuSpectrum > owner
Topology topology
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
PrimitiveHandle handle
std::set< std::string > identities
Object::Ptr value
bool found
std::string text
Cell cell
float(ui::Theme::* member)[4]
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
static constexpr RuntimeHandle fromPacked(std::uint64_t value) noexcept
Reconstructs a handle from an explicit packed boundary value.
static constexpr index_type invalidIndex
Reserved index value shared by all invalid handles.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
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
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
A prepared consumer mutation with a non-throwing visibility boundary.
Id128 public API.
Definition Identity.h:111
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:175
static std::optional< Id128 > parse(std::string_view text) noexcept
Parses canonical UUID text.
Definition Identity.h:150
Real map-module provider for generated grid publication.
void clear() noexcept
Remove all records and reset the local handle allocator.
eve::Result< std::unique_ptr< eve::artifact::PreparedPublication > > prepare(const eve::artifact::PublicationView &publication) override
Stage a topology grid and copy its cells before commit.
const MapArtifactRecord * find(eve::PersistentId id) const noexcept
Find a committed map artifact; the pointer is borrowed. @ownership Borrowed from this provider; calle...
eve::Result< void > restoreState(const eve::Value &state) override
Replace the registry from validated topology state.
eve::Result< eve::Value > snapshotState() const override
Export map identity, build keys, handles and topology cells.
std::optional< std::uint32_t > cell(eve::PersistentId id, std::int32_t x, std::int32_t y) const noexcept
Return one semantic cell, or empty for an invalid coordinate.
void commit() noexcept override
Make the prepared mutation observable; the operation cannot fail.
void rollback() noexcept override
Discard the prepared mutation; idempotent and non-throwing.
MapArtifactStage(MapArtifactProvider &owner, MapArtifactRecord record)
Map-owned CPU registry provider for generated grid artifacts.
放置世界:格子占用(多通道)+ 地形语义 + 已放置建筑实例。 行为由 PlacementSystem 提供;本类暴露便于脚本绑定的薄封装方法。 坐标换算统一走 eve::grid(支持 rectang...
eve::RuntimeHandle< MapArtifactHandleTag > MapArtifactHandle
Generation-qualified handle for a map artifact record.
bool readI64(const eve::Value::Object &object, const char *name, std::int64_t &output)
void registerMapArtifactProvider()
Register the map provider in the common capability registry.
MapArtifactProvider & mapArtifactProvider() noexcept
Return the process-owned map artifact provider singleton.
bool readU64(const eve::Value::Object &object, const char *name, std::uint64_t &output)
bool readString(const eve::Value::Object &object, const char *name, std::string &output)
bool hasSupportedVersion(const eve::Value::Object &object) noexcept
Result< json::UnsignedIntegerScan > scanUnsignedStringMember(const Value::Object &object, std::string_view name)
Scan a required nonempty decimal-string field using from_chars semantics.
Definition Value.cpp:104
Result< std::string > readNonEmptyStringMember(const Value::Object &object, std::string_view name)
Read a required nonempty string field as an independent owning value.
Definition Value.cpp:33
Result< std::int64_t > readInt64Value(const Value &value)
Read an Int64 without narrowing or numeric coercion.
Definition Value.cpp:57
Borrowed immutable view of one generated leaf.
Borrowed immutable view of one complete generated publication.
std::span< const PartView > parts
Queryable map record containing the copied semantic topology.