载入中...
搜索中...
未找到
ActionPrefabBlock.cpp
浏览该文件的文档.
2#include <cmath>
3#include <string_view>
4#include <utility>
6
7// ActionPrefabBlock.cpp
8namespace eve::action {
9namespace {
10
11Result<std::string> requiredText(const Value::Object& payload, std::string_view key) {
12 const auto found = payload.find(std::string(key));
13 if (found == payload.end() || !found->second.getIf<std::string>())
15 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab field must be text", std::string(key)));
16 if (found->second.getIf<std::string>()->empty())
18 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab field must not be empty", std::string(key)));
19 return Result<std::string>::success(*found->second.getIf<std::string>());
20}
21
22Result<double> optionalNumber(const Value::Object& payload, std::string_view key, double fallback) {
23 const auto found = payload.find(std::string(key));
24 if (found == payload.end()) return Result<double>::success(fallback);
25 double value = 0.0;
26 if (const auto* decimal = found->second.getIf<double>())
27 value = *decimal;
28 else if (const auto* integer = found->second.getIf<std::int64_t>())
29 value = static_cast<double>(*integer);
30 else
32 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab duration must be numeric", std::string(key)));
33 if (!std::isfinite(value))
35 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab duration must be finite", std::string(key)));
37}
38
39} // namespace
40
41std::string_view prefabSpawnLifecycleName(PrefabSpawnLifecycle lifecycle) noexcept {
42 switch (lifecycle) {
43 case PrefabSpawnLifecycle::RecycleOnBlockExit: return "recycle_on_block_exit";
44 case PrefabSpawnLifecycle::CustomDuration: return "custom_duration";
45 case PrefabSpawnLifecycle::Independent: return "independent";
46 }
47 return "recycle_on_block_exit";
48}
49
52 auto uri = requiredText(payload, "uri");
54 candidate.uri = std::move(uri).takeValue();
55
56 std::string lifecycle = "recycle_on_block_exit";
57 if (const auto found = payload.find("lifecycle"); found != payload.end()) {
58 const auto* value = found->second.getIf<std::string>();
59 if (!value)
61 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab lifecycle must be text", "lifecycle"));
63 }
64 if (lifecycle == "recycle_on_block_exit")
66 else if (lifecycle == "custom_duration")
68 else if (lifecycle == "independent")
70 else
72 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown prefab lifecycle", "lifecycle"));
73
74 auto duration = optionalNumber(payload, "customDurationSeconds", 0.0);
76 if (payload.contains("customDurationSeconds") && duration.value() <= 0.0)
78 DiagnosticCode::InvalidArgument, "custom prefab duration must be positive", "customDurationSeconds"));
80 if (!payload.contains("customDurationSeconds"))
82 DiagnosticCode::InvalidArgument, "custom prefab duration must be positive", "customDurationSeconds"));
83 auto converted = Duration::fromSeconds(duration.value());
84 if (!converted) return Result<ActionPrefabSpawnBinding>::failure(converted.status());
85 candidate.customDuration = std::move(converted).takeValue();
86 }
87
90 candidate.spatial = std::move(spatial).takeValue();
91 return Result<ActionPrefabSpawnBinding>::success(std::move(candidate));
92}
93
94} // namespace eve::action
95
96// ActionVfxBlock.cpp
97namespace eve::action {
98namespace {
99
100Result<double> numeric(const Value::Object& payload, std::string_view key, double fallback) {
101 const auto found = payload.find(std::string(key));
102 if (found == payload.end()) return Result<double>::success(fallback);
103 double value = 0.0;
104 if (const auto* integer = found->second.getIf<std::int64_t>())
105 value = static_cast<double>(*integer);
106 else if (const auto* decimal = found->second.getIf<double>())
107 value = *decimal;
108 else
110 Diagnostic::error(DiagnosticCode::InvalidArgument, "VFX field must be numeric", std::string(key)));
111 if (!std::isfinite(value))
113 Diagnostic::error(DiagnosticCode::InvalidArgument, "VFX field must be finite", std::string(key)));
115}
116
117} // namespace
118
120 ActionVfxBinding candidate;
121 const auto uri = payload.find("uri");
122 if (uri == payload.end() || !uri->second.getIf<std::string>() || uri->second.getIf<std::string>()->empty())
124 Diagnostic::error(DiagnosticCode::InvalidArgument, "VFX URI must be non-empty text", "uri"));
125 candidate.uri = *uri->second.getIf<std::string>();
126
129 candidate.spatial = std::move(spatial).takeValue();
130
131 if (const auto stop = payload.find("stopBehavior"); stop != payload.end()) {
132 const auto* value = stop->second.getIf<std::string>();
133 if (!value)
135 Diagnostic::error(DiagnosticCode::InvalidArgument, "VFX stopBehavior must be text", "stopBehavior"));
136 if (*value == "stop_emitting")
138 else if (*value == "clear_immediately")
140 else
143 "VFX stopBehavior must be stop_emitting or clear_immediately", "stopBehavior"));
144 }
145 if (const auto synced = payload.find("playbackRateSynced"); synced != payload.end()) {
146 const auto* value = synced->second.getIf<bool>();
147 if (!value)
149 DiagnosticCode::InvalidArgument, "VFX playbackRateSynced must be boolean", "playbackRateSynced"));
150 candidate.playbackRateSynced = *value;
151 }
152
153 auto clipStart = numeric(payload, "clipStartTime", 0.0);
154 if (!clipStart) return Result<ActionVfxBinding>::failure(clipStart.status());
155 auto clipEnd = numeric(payload, "clipEndTime", 0.5);
156 if (!clipEnd) return Result<ActionVfxBinding>::failure(clipEnd.status());
157 if (clipStart.value() < 0.0)
159 DiagnosticCode::InvalidArgument, "VFX clipStartTime must be non-negative", "clipStartTime"));
160 if (clipEnd.value() <= clipStart.value())
162 DiagnosticCode::InvalidArgument, "VFX clipEndTime must be greater than clipStartTime", "clipEndTime"));
163 candidate.clipStartTime = clipStart.value();
164 candidate.clipEndTime = clipEnd.value();
165
166 auto lifetime = numeric(payload, "lifetimeSeconds", 0.0);
168 if (shape == ActionVfxShape::Instant && lifetime.value() <= 0.0)
170 DiagnosticCode::InvalidArgument, "instant VFX lifetimeSeconds must be positive", "lifetimeSeconds"));
171 if (shape == ActionVfxShape::State && lifetime.value() < 0.0)
173 DiagnosticCode::InvalidArgument, "VFX lifetimeSeconds must be non-negative", "lifetimeSeconds"));
174 candidate.lifetimeSeconds = lifetime.value();
175 return Result<ActionVfxBinding>::success(std::move(candidate));
176}
177
178} // namespace eve::action
Typed payload contract for prefab-spawn action states.
Value::Object payload
Typed VFX action-block payload contract.
float duration
SQObjectPtr key
Definition ECS.cpp:155
ShaderImageInput shape
std::weak_ptr< const void > lifetime
Object::Ptr value
bool found
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result< Duration > fromSeconds(double seconds)
Convert finite seconds to the nearest nanosecond.
Definition Time.cpp:17
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
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.
ActionVfxShape
Authored VFX block shape used to select lifetime validation.
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.
Owning validated VFX settings shared by runtime and editor preview.
ActionSpatialBinding spatial
ActionVfxStopBehavior stopBehavior
static Result< ActionVfxBinding > fromPayload(const Value::Object &payload, ActionVfxShape shape)
Decode one dynamic VFX payload transactionally.