载入中...
搜索中...
未找到
DeformTrackFile.cpp
浏览该文件的文档.
1#include <bit>
2#include <filesystem>
3#include <fstream>
4#include <stdexcept>
8namespace eve::physics {
9namespace {
10constexpr std::uint64_t MaxBytes = 36 + 65536 + 4 * 6291456 + 8 * 1048576 + 16 * 2000000 + 32;
11Diagnostic malformed(const char* message) {
13}
14void word(std::string& bytes, std::uint32_t value) {
15 for (unsigned shift = 0; shift < 32; shift += 8) bytes.push_back(static_cast<char>((value >> shift) & 255));
16}
17struct Reader {
18 std::string_view bytes;
19 std::size_t cursor = 0;
20 std::string_view take(std::size_t count) {
21 if (count > bytes.size() - cursor) throw std::runtime_error("truncated deformation asset");
22 auto value = bytes.substr(cursor, count);
23 cursor += count;
24 return value;
25 }
26 std::uint32_t word() {
27 const auto data = take(4);
28 std::uint32_t value = 0;
29 for (unsigned i = 0; i < 4; ++i) value |= std::uint32_t(static_cast<unsigned char>(data[i])) << (i * 8);
30 return value;
31 }
32 float real() { return std::bit_cast<float>(word()); }
33};
34} // namespace
35Result<void> DeformTrack::save(std::string_view path) const {
36 auto recipeBytes = data_->recipe->encode();
37 if (!recipeBytes) return Result<void>::failure(recipeBytes.status());
38 const auto topology = this->topology();
39 std::string bytes = "EVEDEFM1";
40 word(bytes, 1);
42 word(bytes, static_cast<std::uint32_t>(topology.vertexCount()));
43 word(bytes, static_cast<std::uint32_t>(topology.indices().size()));
44 word(bytes, static_cast<std::uint32_t>(topology.uv().size()));
45 word(bytes, static_cast<std::uint32_t>(frameCount()));
46 word(bytes, static_cast<std::uint32_t>(recipeBytes.value().size()));
47 bytes += recipeBytes.value();
48 for (auto index : topology.indices()) word(bytes, index);
49 for (auto uv : topology.uv()) {
50 word(bytes, std::bit_cast<std::uint32_t>(uv.u));
51 word(bytes, std::bit_cast<std::uint32_t>(uv.v));
52 }
53 for (const auto& frame : data_->frames) {
54 word(bytes, std::bit_cast<std::uint32_t>(frame.seconds));
55 for (const auto point : frame.positions)
56 for (float value : {point.x, point.y, point.z}) word(bytes, std::bit_cast<std::uint32_t>(value));
57 }
60}
62 if (path.empty() || path.find('\0') != std::string_view::npos)
63 return Result<DeformTrack>::failure(malformed("invalid deformation asset path"));
64 std::ifstream file(std::string(path), std::ios::binary | std::ios::ate);
65 if (!file) {
66 std::error_code error;
67 const auto status = std::filesystem::status(std::filesystem::path(path), error);
68 const bool missing =
69 error == std::errc::no_such_file_or_directory || (!error && !std::filesystem::exists(status));
72 "deformation asset cannot be opened", std::string(path)));
73 }
74 const auto size = file.tellg();
75 if (size < 68 || static_cast<std::uint64_t>(size) > MaxBytes)
76 return Result<DeformTrack>::failure(malformed("deformation asset exceeds size bounds"));
77 std::string bytes(static_cast<std::size_t>(size), '\0');
78 file.seekg(0);
79 file.read(bytes.data(), static_cast<std::streamsize>(bytes.size()));
80 if (!file) return Result<DeformTrack>::failure(malformed("deformation asset read failed"));
81 const auto payload = std::string_view(bytes).substr(0, bytes.size() - 32);
82 if (bakedAssetContentKey(payload) != std::string_view(bytes).substr(bytes.size() - 32))
84 Diagnostic::error(DiagnosticCode::HashMismatch, "deformation asset checksum mismatch", "deformTrack"));
85 try {
86 Reader reader{payload};
87 if (reader.take(8) != "EVEDEFM1" || reader.word() != 1)
88 return Result<DeformTrack>::failure(malformed("unknown deformation schema/version"));
89 const auto arity = reader.word(), vertices = reader.word(), indices = reader.word(), uvCount = reader.word();
90 const auto frames = reader.word(), recipeSize = reader.word();
91 if ((arity != 2 && arity != 3) || !vertices || vertices > 1048576 || !indices || indices > 6291456 ||
92 indices % arity || (uvCount != 0 && uvCount != vertices) || !frames ||
93 std::uint64_t(frames) * vertices > 2000000 || !recipeSize || recipeSize > 65536)
94 return Result<DeformTrack>::failure(malformed("invalid deformation asset counts"));
95 const auto expected =
96 36ull + recipeSize + 4ull * indices + 8ull * uvCount + std::uint64_t(frames) * (4 + 12ull * vertices);
97 if (expected != payload.size())
98 return Result<DeformTrack>::failure(malformed("truncated or trailing deformation data"));
99 auto recipe = BakeRecipe::decode(reader.take(recipeSize));
100 if (!recipe) return Result<DeformTrack>::failure(recipe.status());
101 std::vector<std::uint32_t> connectivity;
102 connectivity.reserve(indices);
103 for (std::uint32_t i = 0; i < indices; ++i) connectivity.push_back(reader.word());
104 std::vector<DeformUV> uv;
105 uv.reserve(uvCount);
106 for (std::uint32_t i = 0; i < uvCount; ++i) uv.push_back({reader.real(), reader.real()});
108 vertices, std::move(connectivity), std::move(uv));
109 if (!topology) return Result<DeformTrack>::failure(topology.status());
110 std::vector<DeformFrame> candidate;
111 candidate.reserve(frames);
112 for (std::uint32_t f = 0; f < frames; ++f) {
113 DeformFrame frame{reader.real(), topology.value(), {}};
114 frame.positions.reserve(vertices);
115 for (std::uint32_t v = 0; v < vertices; ++v)
116 frame.positions.push_back({reader.real(), reader.real(), reader.real()});
117 candidate.push_back(std::move(frame));
118 }
119 return prepare(std::move(candidate), recipe.value());
120 } catch (const std::exception&) {
121 return Result<DeformTrack>::failure(malformed("deformation asset decoding failed"));
122 }
123}
125 auto expectedBytes = expected.encode();
126 if (!expectedBytes) return Result<DeformTrack>::failure(expectedBytes.status());
127 auto loaded = load(path);
128 if (!loaded) return loaded;
129 auto actual = loaded.value().recipe()->encode();
130 if (!actual) return Result<DeformTrack>::failure(actual.status());
131 if (actual.value() != expectedBytes.value())
133 Diagnostic::error(DiagnosticCode::HashMismatch, "deformation asset provenance mismatch", "deformTrack"));
134 return loaded;
135}
136} // namespace eve::physics
Value::Object payload
std::size_t cursor
float uv
std::string message
wgpu::PopErrorScopeStatus status
std::vector< std::uint32_t > indices
float v
std::uint64_t bytes
ssq::Object frame
std::string error
Definition Package.cpp:174
float f
std::string path
Definition PlayHost.cpp:111
std::unordered_set< std::string > loaded
Definition Plugins.cpp:24
std::string file
Object::Ptr value
int count
std::array< double, 2 > vertices[3]
float size
Definition TreeMesh.cpp:158
uint32_t index
glm::vec3 point
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 failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Bounded provenance builder for explicit-coordinate baked recordings. @ownership Owns names and conten...
Definition BakeRecipe.h:21
Result< std::string > encode() const
Encode complete provenance canonically, sorting dependency and RNG names. @cost Linear in bounded met...
static Result< BakeRecipe > decode(std::string_view bytes)
Parse a bounded canonical recipe record, rejecting duplicate names and trailing bytes....
static Result< DeformTopology > prepare(DeformPrimitive primitive, std::size_t vertexCount, std::vector< std::uint32_t > indices, std::vector< DeformUV > uv)
Validate bounded connectivity and freeze it with a deterministic content identity.
std::span< const DeformUV > uv() const noexcept
Borrow immutable UVs; empty means no UV channel.
DeformPrimitive primitive() const noexcept
Primitive interpretation of the index stream.
std::size_t vertexCount() const noexcept
Number of stable vertex slots.
std::span< const std::uint32_t > indices() const noexcept
Borrow immutable ordered connectivity, bounded by this shared storage lifetime.
std::size_t frameCount() const noexcept
Number of immutable frames.
std::shared_ptr< const BakeRecipe > recipe() const noexcept
Return shared immutable provenance including explicit coordinates.
DeformTopology topology() const
Return a cheap owning copy of the fixed connectivity and UVs.
static Result< DeformTrack > loadForRecipe(std::string_view path, const BakeRecipe &expected)
Load and compare complete canonical expected provenance before cache admission. @cost Linear in asset...
static Result< DeformTrack > prepare(std::vector< DeformFrame > frames, const BakeRecipe &recipe)
Validate matching topology, positions, coordinates and fixed sample grid, then freeze all frames.
static Result< DeformTrack > load(std::string_view path)
Load bounded EVEDEFM1 v1 and validate all data before publishing a clip. @cost Linear in asset size p...
Result< void > save(std::string_view path) const
Publish EVEDEFM1 v1 with topology, provenance, frames and whole-file checksum; never overwrite....
std::string word
eve::Diagnostic Diagnostic
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
std::string bakedAssetContentKey(std::string_view source)
Stable noncryptographic 128-bit content digest, encoded as 32 lowercase hexadecimal bytes....
Result< void > publishBakedAsset(std::string_view path, std::string_view bytes)
Publish complete baked asset bytes atomically without replacing an existing path. @ownership Borrows ...
Owning snapshot with actual connectivity, stable indexed positions and explicit sample time.
Definition DeformTrack.h:15