载入中...
搜索中...
未找到
AudioSerialization.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <utility>
5
6namespace eve::audio_editing {
7namespace {
8
9const EditorValue* field(const EditorValue& value, const char* key) {
10 const auto* object = value.getIf<EditorValue::Object>();
11 if (!object) return nullptr;
12 const auto found = object->find(key);
13 return found == object->end() ? nullptr : &found->second;
14}
15
16} // namespace
17
20 for (const auto& [path, value] : values_) properties[path] = value;
22 root["schemaVersion"] = 1;
23 root["properties"] = EditorValue(std::move(properties));
24 return EditorValue(std::move(root));
25}
26
28 const EditorValue* versionValue = field(snapshot, "schemaVersion");
29 const EditorValue* propertiesValue = field(snapshot, "properties");
30 const auto* version = versionValue ? versionValue->getIf<int64_t>() : nullptr;
31 const auto* properties = propertiesValue ? propertiesValue->getIf<EditorValue::Object>() : nullptr;
32 if (!version || *version != 1 || !properties)
33 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.audio.snapshot-format"),
34 "Audio source snapshot requires schemaVersion 1 and properties");
35 auto candidate = defaults();
36 const PropertySchema schemaValue = sourceSchema();
37 for (const auto& [path, value] : *properties) {
38 auto descriptor = schemaValue.find(PropertyPath(path));
39 if (!descriptor)
40 return eve::editing::failed<void>(EditorStatus::Unsupported, RuleId("editor.audio.snapshot-property"),
41 "Audio source snapshot contains unknown property: " + path);
42 auto valid = validatePropertyValue(*descriptor, value);
43 if (!valid.ok()) return valid;
44 candidate[path] = value;
45 }
46 values_ = std::move(candidate);
49 return eve::editing::applied<void>();
50}
51
52std::vector<EditorDiagnostic> AudioSourceTarget::validate() const {
53 std::vector<EditorDiagnostic> diagnostics;
54 const auto text = [&](const char* path) { return *values_.at(path).getIf<std::string>(); };
55 const auto number = [&](const char* path) { return *values_.at(path).getIf<double>(); };
56 if (text("clip.asset").empty())
57 diagnostics.push_back(eve::editing::ruleDiagnostic(
58 eve::DiagnosticCode::PreconditionViolation, RuleId("editor.audio.clip-required"),
59 DiagnosticSeverity::Error, "Audio source requires a clip asset"));
60 if (number("spatial.reference-distance") > number("spatial.maximum-distance"))
61 diagnostics.push_back(eve::editing::ruleDiagnostic(
63 RuleId("editor.audio.attenuation-range"), DiagnosticSeverity::Error,
64 "Reference distance exceeds maximum attenuation distance"));
65 const bool looping = *values_.at("play.loop").getIf<bool>();
66 const double loopStart = number("play.loop-start");
67 const double loopEnd = number("play.loop-end");
68 if (looping && loopEnd > 0.0 && loopStart >= loopEnd)
69 diagnostics.push_back(eve::editing::ruleDiagnostic(
70 eve::DiagnosticCode::PreconditionViolation, RuleId("editor.audio.loop-range"),
71 DiagnosticSeverity::Error, "Loop start must be earlier than loop end"));
72 const auto& direction = *values_.at("spatial.direction").getIf<EditorValue::Array>();
73 double lengthSquared = 0.0;
74 for (const EditorValue& component : direction) {
75 const double value = *component.getIf<double>();
76 lengthSquared += value * value;
77 }
78 if (lengthSquared < 1e-12)
79 diagnostics.push_back(eve::editing::ruleDiagnostic(
80 eve::DiagnosticCode::PreconditionViolation, RuleId("editor.audio.direction"),
81 DiagnosticSeverity::Warning,
82 "Directional audio source has a zero direction vector"));
83 if (text("mixer.bus").empty())
84 diagnostics.push_back(eve::editing::ruleDiagnostic(
85 eve::DiagnosticCode::PreconditionViolation, RuleId("editor.audio.bus-required"),
86 DiagnosticSeverity::Error, "Audio source requires a mixer bus route"));
87 return diagnostics;
88}
89
90} // namespace eve::audio_editing
double value
int root
Definition AnimSmr.cpp:119
std::string descriptor
std::uint32_t key
std::string text
std::map< std::string, PropertyRule > properties
bool valid
std::string path
Definition PlayHost.cpp:110
RoadLaneDirection direction
double number
bool found
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Result< void > loadSnapshot(const EditorValue &snapshot)
Atomically load a versioned source snapshot.
EditorValue snapshotValue() const override
Capture deterministic source settings for scene/prefab persistence.
std::vector< EditorDiagnostic > validate() const
Report missing assets and inconsistent attenuation/loop settings.
void bumpRevision()
Advance the revision without touching the dirty region.
virtual void clearDirtyRegion()=0
Clears dirty region.
Dotted property path (layout.size) compared and persisted as text.
Definition EditingIds.h:78
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
const T * getIf() const
Return an immediately borrowed typed value.
editing::Value EditorValue
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build a common diagnostic carrying an open editing rule identity.
const EditorValue * field(const EditorValue &value, const char *name)
EVENGINE_API_PLATFORM public API.
PropertyDescriptorLookup find(const PropertyPath &path) const
Finds find.