载入中...
搜索中...
未找到
ParticleTrackFile.cpp
浏览该文件的文档.
1#include <bit>
2#include <filesystem>
3#include <fstream>
4#include <stdexcept>
8namespace eve::physics {
9namespace {
10constexpr std::uint64_t MaxBytes = 24 + 65536 + 28 * 65536 + 56 * 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 particle 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> ParticleTrack::save(std::string_view path) const {
36 auto recipeBytes = data_->recipe->encode();
37 if (!recipeBytes) return Result<void>::failure(recipeBytes.status());
38 std::string bytes = "EVEPART1";
39 word(bytes, 1);
40 word(bytes, std::bit_cast<std::uint32_t>(duration()));
41 word(bytes, static_cast<std::uint32_t>(capacity()));
42 word(bytes, static_cast<std::uint32_t>(recipeBytes.value().size()));
43 bytes += recipeBytes.value();
44 for (const auto& track : data_->trajectories) {
45 for (auto byte : track.emitter.bytes()) bytes.push_back(static_cast<char>(byte));
46 word(bytes, static_cast<std::uint32_t>(track.birthSerial));
47 word(bytes, static_cast<std::uint32_t>(track.birthSerial >> 32));
48 word(bytes, static_cast<std::uint32_t>(track.keys.size()));
49 for (const auto& key : track.keys) {
50 const auto& p = key.pose;
51 for (float value :
52 {key.seconds, p.x, p.y, p.w, p.h, p.rotation, p.r, p.g, p.b, p.a, p.u0, p.v0, p.u1, p.v1})
53 word(bytes, std::bit_cast<std::uint32_t>(value));
54 }
55 }
58}
60 if (path.empty() || path.find('\0') != std::string_view::npos)
61 return Result<ParticleTrack>::failure(malformed("invalid particle asset path"));
62 std::ifstream file(std::string(path), std::ios::binary | std::ios::ate);
63 if (!file) {
64 std::error_code error;
65 const auto status = std::filesystem::status(std::filesystem::path(path), error);
66 const bool missing =
67 error == std::errc::no_such_file_or_directory || (!error && !std::filesystem::exists(status));
70 "particle asset cannot be opened", std::string(path)));
71 }
72 const auto size = file.tellg();
73 if (size < 56 || static_cast<std::uint64_t>(size) > MaxBytes)
74 return Result<ParticleTrack>::failure(malformed("particle asset exceeds size bounds"));
75 std::string bytes(static_cast<std::size_t>(size), '\0');
76 file.seekg(0);
77 file.read(bytes.data(), static_cast<std::streamsize>(bytes.size()));
78 if (!file) return Result<ParticleTrack>::failure(malformed("particle asset read failed"));
79 const auto payload = std::string_view(bytes).substr(0, bytes.size() - 32);
80 if (bakedAssetContentKey(payload) != std::string_view(bytes).substr(bytes.size() - 32))
82 Diagnostic::error(DiagnosticCode::HashMismatch, "particle asset checksum mismatch", "particleTrack"));
83 try {
84 Reader reader{payload};
85 if (reader.take(8) != "EVEPART1" || reader.word() != 1)
86 return Result<ParticleTrack>::failure(malformed("unknown particle schema/version"));
87 const float duration = reader.real();
88 const auto count = reader.word(), recipeSize = reader.word();
89 if (count > 65536 || !recipeSize || recipeSize > 65536)
90 return Result<ParticleTrack>::failure(malformed("invalid particle asset counts"));
91 auto recipe = BakeRecipe::decode(reader.take(recipeSize));
92 if (!recipe) return Result<ParticleTrack>::failure(recipe.status());
93 const auto recordsStart = reader.cursor;
94 std::uint64_t totalKeys = 0;
95 for (std::uint32_t i = 0; i < count; ++i) {
96 reader.take(24);
97 const auto keys = reader.word();
98 totalKeys += keys;
99 if (keys < 2 || totalKeys > 2000000)
100 return Result<ParticleTrack>::failure(malformed("invalid particle key budget"));
101 reader.take(std::size_t(keys) * 56);
102 }
103 if (reader.cursor != payload.size()) return Result<ParticleTrack>::failure(malformed("trailing particle data"));
104 reader.cursor = recordsStart;
105 std::vector<ParticleTrajectory> candidate;
106 candidate.reserve(count);
107 for (std::uint32_t i = 0; i < count; ++i) {
108 ParticleTrajectory track;
110 const auto encodedId = reader.take(16);
111 for (std::size_t j = 0; j < identity.size(); ++j) identity[j] = static_cast<unsigned char>(encodedId[j]);
113 const auto low = reader.word();
114 track.birthSerial = std::uint64_t(low) | (std::uint64_t(reader.word()) << 32);
115 const auto keys = reader.word();
116 track.keys.reserve(keys);
117 for (std::uint32_t k = 0; k < keys; ++k) {
119 key.seconds = reader.real();
120 auto& p = key.pose;
121 p = {reader.real(), reader.real(), reader.real(), reader.real(), reader.real(),
122 reader.real(), reader.real(), reader.real(), reader.real(), reader.real(),
123 reader.real(), reader.real(), reader.real()};
124 track.keys.push_back(key);
125 }
126 candidate.push_back(std::move(track));
127 }
128 return prepare(duration, std::move(candidate), recipe.value());
129 } catch (const std::exception&) {
130 return Result<ParticleTrack>::failure(malformed("particle asset decoding failed"));
131 }
132}
134 auto expectedBytes = expected.encode();
135 if (!expectedBytes) return Result<ParticleTrack>::failure(expectedBytes.status());
136 auto loaded = load(path);
137 if (!loaded) return loaded;
138 auto actual = loaded.value().recipe()->encode();
139 if (!actual) return Result<ParticleTrack>::failure(actual.status());
140 if (actual.value() != expectedBytes.value())
142 Diagnostic::error(DiagnosticCode::HashMismatch, "particle asset provenance mismatch", "particleTrack"));
143 return loaded;
144}
145} // namespace eve::physics
Value::Object payload
std::size_t cursor
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::string message
wgpu::PopErrorScopeStatus status
std::uint64_t bytes
std::string error
Definition Package.cpp:174
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
float size
Definition TreeMesh.cpp:158
uint64_t identity
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
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:113
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< ParticleTrack > load(std::string_view path)
Load a bounded EVEPART1 v1 asset and validate all trajectories before publication....
float duration() const noexcept
Explicit clip duration including silent intervals.
static Result< ParticleTrack > loadForRecipe(std::string_view path, const BakeRecipe &expected)
Load and require an exact canonical provenance match for cache admission. @cost Linear in asset bytes...
std::size_t capacity() const noexcept
Maximum output extent, equal to the total birth count.
std::shared_ptr< const BakeRecipe > recipe() const noexcept
Owning shared immutable provenance.
static Result< ParticleTrack > prepare(float duration, std::vector< ParticleTrajectory > trajectories, const BakeRecipe &recipe)
Validate and freeze trajectories in deterministic identity order and an explicit clip duration....
Result< void > save(std::string_view path) const
Publish EVEPART1 v1 with provenance and whole-file checksum, without overwriting any target....
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 ...
StrongUuid< detail::PersistentIdTag > PersistentId
Stable instance identity for persistence, networking and process boundaries.
Definition Identity.h:253
Explicit event/sample time and owning presentation value.
One birth's complete trajectory, including keys exactly at birth and death.
std::vector< ParticleTrackKey > keys