载入中...
搜索中...
未找到
ActionPrefabBlock.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <utility>
5
6namespace eve::action {
7namespace {
8
9Result<std::string> requiredText(const Value::Object& payload, std::string_view key) {
10 const auto found = payload.find(std::string(key));
11 if (found == payload.end() || !found->second.getIf<std::string>())
13 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab field must be text", std::string(key)));
14 if (found->second.getIf<std::string>()->empty())
16 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab field must not be empty", std::string(key)));
17 return Result<std::string>::success(*found->second.getIf<std::string>());
18}
19
20Result<double> optionalNumber(const Value::Object& payload, std::string_view key, double fallback) {
21 const auto found = payload.find(std::string(key));
22 if (found == payload.end()) return Result<double>::success(fallback);
23 double value = 0.0;
24 if (const auto* decimal = found->second.getIf<double>())
25 value = *decimal;
26 else if (const auto* integer = found->second.getIf<std::int64_t>())
27 value = static_cast<double>(*integer);
28 else
30 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab duration must be numeric", std::string(key)));
31 if (!std::isfinite(value))
33 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab duration must be finite", std::string(key)));
35}
36
37} // namespace
38
39std::string_view prefabSpawnLifecycleName(PrefabSpawnLifecycle lifecycle) noexcept {
40 switch (lifecycle) {
41 case PrefabSpawnLifecycle::RecycleOnBlockExit: return "recycle_on_block_exit";
42 case PrefabSpawnLifecycle::CustomDuration: return "custom_duration";
43 case PrefabSpawnLifecycle::Independent: return "independent";
44 }
45 return "recycle_on_block_exit";
46}
47
50 auto uri = requiredText(payload, "uri");
52 candidate.uri = std::move(uri).takeValue();
53
54 std::string lifecycle = "recycle_on_block_exit";
55 if (const auto found = payload.find("lifecycle"); found != payload.end()) {
56 const auto* value = found->second.getIf<std::string>();
57 if (!value)
59 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab lifecycle must be text", "lifecycle"));
61 }
62 if (lifecycle == "recycle_on_block_exit")
64 else if (lifecycle == "custom_duration")
66 else if (lifecycle == "independent")
68 else
70 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown prefab lifecycle", "lifecycle"));
71
72 auto duration = optionalNumber(payload, "customDurationSeconds", 0.0);
74 if (payload.contains("customDurationSeconds") && duration.value() <= 0.0)
76 DiagnosticCode::InvalidArgument, "custom prefab duration must be positive", "customDurationSeconds"));
78 if (!payload.contains("customDurationSeconds"))
80 DiagnosticCode::InvalidArgument, "custom prefab duration must be positive", "customDurationSeconds"));
81 auto converted = Duration::fromSeconds(duration.value());
82 if (!converted) return Result<ActionPrefabSpawnBinding>::failure(converted.status());
83 candidate.customDuration = std::move(converted).takeValue();
84 }
85
88 candidate.spatial = std::move(spatial).takeValue();
89 return Result<ActionPrefabSpawnBinding>::success(std::move(candidate));
90}
91
92} // namespace eve::action
double value
Typed payload contract for prefab-spawn action states.
Value::Object payload
float duration
std::uint32_t key
bool found
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
static Result< Duration > fromSeconds(double seconds)
Convert finite seconds to the nearest nanosecond.
Definition Time.cpp:16
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
std::map< std::string, Value > Object
Definition Value.h:34
std::string_view prefabSpawnLifecycleName(PrefabSpawnLifecycle lifecycle) noexcept
Return the stable serialized spelling for a prefab lifecycle.
PrefabSpawnLifecycle
Authority responsible for ending a spawned prefab instance.
@ CustomDuration
The action block recycles the instance after customDuration.
@ RecycleOnBlockExit
The action block recycles the instance on Exit or interruption.
@ Independent
Ownership is transferred to the prefab instance service after spawning.
Fully validated, owning settings for one prefab-spawn state block.
static Result< ActionPrefabSpawnBinding > fromPayload(const Value::Object &payload)
Decode a prefab-spawn payload without mutating runtime state.
static Result< ActionSpatialBinding > fromPayload(const Value::Object &payload)
Decode and validate the common spatial fields in an owning payload.