载入中...
搜索中...
未找到
BakeRecipe.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <bit>
4#include <cmath>
5#include <limits>
7
8namespace eve::physics {
9namespace {
10Diagnostic invalidRecipe(const char* message) {
12}
13constexpr std::size_t MaxEntries = 64, MaxToken = 256;
14bool token(std::string_view value) {
15 if (value.empty() || value.size() > MaxToken) return false;
16 for (unsigned char c : value)
17 if (c < 33 || c > 126) return false;
18 return true;
19}
20bool digest(std::string_view value) {
21 if (value.size() != 32) return false;
22 for (char c : value)
23 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;
24 return true;
25}
26void append32(std::string& out, std::uint32_t value) {
27 for (unsigned shift = 0; shift < 32; shift += 8) out.push_back(static_cast<char>((value >> shift) & 255));
28}
29void appendText(std::string& out, std::string_view text) {
30 append32(out, static_cast<std::uint32_t>(text.size()));
31 out.append(text);
32}
33struct Reader {
34 std::string_view bytes;
35 std::size_t cursor = 0;
36 bool valid = true;
37 std::string_view take(std::size_t size) {
38 if (size > bytes.size() - cursor) {
39 valid = false;
40 return {};
41 }
42 auto result = bytes.substr(cursor, size);
43 cursor += size;
44 return result;
45 }
46 std::uint32_t word() {
47 auto data = take(4);
48 if (!valid) return 0;
49 std::uint32_t result = 0;
50 for (unsigned i = 0; i < 4; ++i) result |= std::uint32_t(static_cast<unsigned char>(data[i])) << (8 * i);
51 return result;
52 }
53 std::string text() {
54 const auto size = word();
55 if (size > MaxToken) {
56 valid = false;
57 return {};
58 }
59 return std::string(take(size));
60 }
61};
62} // namespace
65 return Result<void>::failure(invalidRecipe("unknown bake coordinates"));
66 coordinates_ = coordinates;
67 return Result<void>::success();
68}
69BakeCoordinates BakeRecipe::coordinates() const noexcept { return coordinates_; }
71 if (!token(revision)) return Result<void>::failure(invalidRecipe("invalid producer revision token"));
72 producer_ = revision;
73 return Result<void>::success();
74}
76 if (!token(revision)) return Result<void>::failure(invalidRecipe("invalid backend revision token"));
77 backend_ = revision;
78 return Result<void>::success();
79}
80Result<void> BakeRecipe::setTiming(float stepSeconds, std::uint32_t sampleStride) {
81 if (!std::isfinite(stepSeconds) || stepSeconds <= 0 || sampleStride == 0)
82 return Result<void>::failure(invalidRecipe("invalid bake timing"));
83 stepSeconds_ = stepSeconds;
84 sampleStride_ = sampleStride;
85 return Result<void>::success();
86}
88 parameters_ = bakedAssetContentKey(bytes);
89 return Result<void>::success();
90}
91Result<void> BakeRecipe::setSource(std::string_view name, std::string_view bytes) {
92 if (!token(name) || (sources_.size() >= MaxEntries && !sources_.contains(std::string(name))))
93 return Result<void>::failure(invalidRecipe("invalid source name or source budget exceeded"));
95 sources_.insert_or_assign(std::string(name), std::move(key));
96 return Result<void>::success();
97}
98Result<void> BakeRecipe::setRandomStream(std::string_view name, std::uint64_t seed) {
99 if (!token(name) || (randomStreams_.size() >= MaxEntries && !randomStreams_.contains(std::string(name))))
100 return Result<void>::failure(invalidRecipe("invalid RNG name or stream budget exceeded"));
101 randomStreams_.insert_or_assign(std::string(name), seed);
102 return Result<void>::success();
103}
105 if (!token(producer_) || !token(backend_) || !digest(parameters_) || !std::isfinite(stepSeconds_) ||
106 stepSeconds_ <= 0 || sampleStride_ == 0 || sources_.empty())
107 return Result<std::string>::failure(invalidRecipe("incomplete bake provenance"));
108 const bool planar = coordinates_ == BakeCoordinates::PlanarPixels;
109 std::string out = planar ? "EVEBREC1" : "EVECREC1";
110 appendText(out, planar ? "planar-pixels-xy" : "metres-rh-y-up");
111 appendText(out, producer_);
112 appendText(out, backend_);
113 out += parameters_;
114 append32(out, std::bit_cast<std::uint32_t>(stepSeconds_));
115 append32(out, sampleStride_);
116 append32(out, static_cast<std::uint32_t>(sources_.size()));
117 for (const auto& [name, key] : sources_) {
118 appendText(out, name);
119 out += key;
120 }
121 append32(out, static_cast<std::uint32_t>(randomStreams_.size()));
122 for (const auto& [name, seed] : randomStreams_) {
123 appendText(out, name);
124 append32(out, static_cast<std::uint32_t>(seed));
125 append32(out, static_cast<std::uint32_t>(seed >> 32));
126 }
127 return Result<std::string>::success(std::move(out));
128}
130 auto failure = [] { return Result<BakeRecipe>::failure(invalidRecipe("invalid bake provenance schema or data")); };
131 if (bytes.size() > 65536) return failure();
132 Reader reader{bytes};
133 const auto schema = reader.take(8);
134 const auto coordinates = reader.text();
136 if (schema == "EVECREC1" && coordinates == "metres-rh-y-up")
138 else if (schema == "EVEBREC1" && coordinates == "planar-pixels-xy")
140 else
141 return failure();
142 recipe.producer_ = reader.text();
143 recipe.backend_ = reader.text();
144 recipe.parameters_ = reader.take(32);
145 recipe.stepSeconds_ = std::bit_cast<float>(reader.word());
146 recipe.sampleStride_ = reader.word();
147 const auto sources = reader.word();
148 if (sources == 0 || sources > MaxEntries) return failure();
149 for (std::uint32_t i = 0; i < sources; ++i) {
150 auto name = reader.text();
151 auto key = reader.take(32);
152 if (!reader.valid || !token(name) || !digest(key) || !recipe.sources_.emplace(name, key).second)
153 return failure();
154 }
155 const auto streams = reader.word();
156 if (streams > MaxEntries) return failure();
157 for (std::uint32_t i = 0; i < streams; ++i) {
158 auto name = reader.text();
159 const auto low = reader.word();
160 const auto high = reader.word();
161 if (!reader.valid || !token(name) ||
162 !recipe.randomStreams_.emplace(name, std::uint64_t(low) | (std::uint64_t(high) << 32)).second)
163 return failure();
164 }
165 if (!reader.valid || reader.cursor != bytes.size()) return failure();
166 auto canonical = recipe.encode();
167 if (!canonical || canonical.value() != bytes) return failure();
168 return Result<BakeRecipe>::success(std::move(recipe));
169}
170Result<void> BakeRecipe::validateSampleTime(std::uint32_t frame, float seconds) const {
171 const double expected = double(frame) * stepSeconds_ * sampleStride_;
172 const double tolerance =
173 std::min(4 * std::numeric_limits<float>::epsilon() * std::max(expected, double(stepSeconds_)),
174 0.25 * double(stepSeconds_) * sampleStride_);
175 if (!std::isfinite(seconds) || stepSeconds_ <= 0 || sampleStride_ == 0 ||
176 expected > std::numeric_limits<float>::max() ||
177 (frame == 0 ? seconds != 0 : std::abs(double(seconds) - expected) > tolerance))
178 return Result<void>::failure(invalidRecipe("sample time does not match bake timing"));
179 return Result<void>::success();
180}
186} // namespace eve::physics
std::uint64_t revision
std::size_t cursor
std::string name
Definition ECS.cpp:154
std::string message
std::int32_t c
Poco::JSON::Object::Ptr result
std::uint64_t bytes
bool valid
OceanGpuFrameToken token
ssq::Object frame
std::vector< std::pair< std::uint64_t, OceanBoatReceipt > > sources
BakeRecipe recipe
Object::Ptr value
std::string text
ScriptStructure out
std::string digest
uint32_t seed
Definition TreeMesh.cpp:276
float size
Definition TreeMesh.cpp:158
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 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
Bounded provenance builder for explicit-coordinate baked recordings. @ownership Owns names and conten...
Definition BakeRecipe.h:21
Result< void > setTiming(float stepSeconds, std::uint32_t sampleStride)
Set the actual finite positive simulation step in seconds and positive ticks per captured sample.
Result< std::string > key() const
Compute the noncryptographic cache key of the complete canonical recipe. @cost Linear in bounded meta...
Result< void > setParameters(std::string_view bytes)
Digest complete parameter bytes, including defaults resolved by the producer. @cost Linear in bytes; ...
Result< std::string > encode() const
Encode complete provenance canonically, sorting dependency and RNG names. @cost Linear in bounded met...
Result< void > setBackend(std::string_view revision)
Set the explicit backend and its revision; never infer it from the player platform.
Result< void > setSource(std::string_view name, std::string_view bytes)
Insert or replace a named dependency digest; names are stable logical IDs, not absolute paths....
Result< void > setRandomStream(std::string_view name, std::uint64_t seed)
Insert or replace one of at most 64 named RNG seeds. Empty stream sets explicitly mean no RNG.
BakeCoordinates coordinates() const noexcept
Current coordinate contract, defaulting to the original metre convention.
Result< void > setProducer(std::string_view revision)
Set an explicit algorithm/build revision token; invalid input leaves the builder unchanged.
static Result< BakeRecipe > decode(std::string_view bytes)
Parse a bounded canonical recipe record, rejecting duplicate names and trailing bytes....
Result< void > setCoordinates(BakeCoordinates coordinates)
Select the producer coordinate contract; unknown values fail without changing the recipe.
Result< void > validateSampleTime(std::uint32_t frame, float seconds) const
Validate a sample against the declared fixed-step/stride grid.
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....
BakeCoordinates
Explicit source coordinates; planar pixels preserve XY axes without an implicit flip.
Definition BakeRecipe.h:11