载入中...
搜索中...
未找到
WorldSnapshot.cpp
浏览该文件的文档.
35eve::Result<eve::SnapshotEnvelope> World::snapshot(const eve::SnapshotHashProvider& hashProvider) const {
37 return Result<SnapshotEnvelope>::failure(Diagnostic::error(DiagnosticCode::PreconditionViolation,
42 eve::DiagnosticCode::PreconditionViolation, "Cannot snapshot a destroyed or uninitialized physics world",
56 std::string(kWorld2DType), std::move(schema).takeValue(), eve::SchemaVersion(kSnapshot2DVersion), instanceId_,
64 eve::DiagnosticCode::PreconditionViolation, "Cannot restore a destroyed or uninitialized physics world",
75 DiagnosticCode::Conflict, "physics snapshot belongs to a different world instance", "snapshot.instanceId"));
82 if (!object || !hasExactFields(*object, {"bodies", "joints", "allowSleep", "gravityX", "gravityY", "meter", "tick",
85 eve::DiagnosticCode::ParseError, "2D physics snapshot payload has unknown or missing fields", "payload"));
98 eve::DiagnosticCode::InvalidArgument, "physics snapshot meter must be positive", "payload.meter"));
105 auto prepared = prepareWorld2D(bodies.value(), gravityX.value(), gravityY.value(), meter.value(),
110 return Result<void>::failure(Diagnostic::error(DiagnosticCode::ParseError, "Planar joints must be an array"));
114 auto jointRestore = WorldSnapshotAccess::restoreJoints2D(*prepared.value(), *jointValues, nextJointHandleIndex_);
122eve::Result<eve::SnapshotEnvelope> World3D::snapshot(const eve::SnapshotHashProvider& hashProvider) const {
125 return Result<SnapshotEnvelope>::failure(Diagnostic::error(DiagnosticCode::PreconditionViolation,
130 DiagnosticCode::PreconditionViolation, "Cannot snapshot while stepping", "physics.world3d.snapshot"));
133 eve::DiagnosticCode::PreconditionViolation, "Cannot snapshot a destroyed or uninitialized 3D physics world",
141 auto payload = make3DPayload(*this, bodies, std::vector<Shape3D*>(shapes_.begin(), shapes_.end()),
145 std::string(kWorld3DType), std::move(schema).takeValue(), eve::SchemaVersion(kSnapshotVersion), instanceId_,
151 if (rigidBackendKind_ != RigidBackend3D::Box3D) return restorePortable(snapshotValue, hashProvider);
157 eve::DiagnosticCode::PreconditionViolation, "Cannot restore a destroyed or uninitialized 3D physics world",
181 for (auto& joint : *legacyJoints.getIf<Value::Array>()) joint.getIf<Value::Object>()->erase("parameters");
193 Diagnostic::error(DiagnosticCode::ParseError, "Legacy snapshot has unexpected transmission fields"));
209 bodyObject->emplace("dynamics", Value(Value::Object{{"linearDamping", Value(defaults.linearDamping)},
222 eve::DiagnosticCode::ParseError, "3D physics snapshot payload has unknown or missing fields", "payload"));
235 if (!snapshotShapes || !snapshotShapes->isArray() || !snapshotJoints || !snapshotJoints->isArray())
237 eve::DiagnosticCode::ParseError, "3D physics snapshot topology fields must be arrays", "payload.shapes"));
242 prepareRuntimeHandleRefresh(bodies.value().size(), snapshotShapes->arraySize(), snapshotJoints->arraySize());
255 return Result<void>::failure(Diagnostic::error(DiagnosticCode::ParseError, "Couplings must be an array"));
256 auto preparedCouplings = WorldSnapshotAccess::restoreCouplings3D(*prepared.value(), *couplings);
271 eve::DiagnosticCode::InvariantViolation, "2D physics body handle space cannot represent restored links",
281eve::Result<void> World3D::prepareRuntimeHandleRefresh(std::size_t bodyCount, std::size_t shapeCount,
283 const auto bodyAvailable = static_cast<std::uint64_t>(PhysicsBodyHandle::invalidIndex) - nextBodyHandleIndex_;
284 const auto shapeAvailable = static_cast<std::uint64_t>(PhysicsShapeHandle::invalidIndex) - nextShapeHandleIndex_;
285 const auto jointAvailable = static_cast<std::uint64_t>(PhysicsJointHandle::invalidIndex) - nextJointHandleIndex_;
289 eve::DiagnosticCode::InvariantViolation, "3D physics handle space cannot represent restored links",
Backend-neutral, observable fixed-step contract for physics domains.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static constexpr index_type invalidIndex
Reserved index value shared by all invalid handles.
Definition RuntimeHandle.h:124
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:175
constexpr std::uint64_t value() const noexcept
Returns the underlying value at an explicit protocol boundary.
Definition StrongUint64.h:26
PhysicsJointHandle nextJointRuntimeHandle()
Internal: next generation-qualified joint handle.
Definition World3D.cpp:416
SimulationObservation simulationObservation() const noexcept
Snapshot of completed backend steps and logical simulation time.
Definition World3DSimulation.cpp:65
eve::Result< eve::SnapshotEnvelope > snapshot(const eve::SnapshotHashProvider &hashProvider) const
Captures a versioned, integrity-checked 3D world snapshot.
Definition WorldSnapshot.cpp:122
eve::Result< void > restore(const eve::SnapshotEnvelope &snapshot, const eve::SnapshotHashProvider &hashProvider)
Restores a verified snapshot without exposing partial state.
Definition WorldSnapshot.cpp:149
PhysicsBodyHandle nextBodyRuntimeHandle()
Internal: next generation-qualified body handle.
Definition World3D.cpp:404
eve::SimulationTick simulationTick() const noexcept
Current deterministic tick; save data should persist this value.
Definition World3D.h:134
void clearContactEvents()
Clears all contact and trigger buffers before the next step.
Definition World3DContacts.cpp:14
PhysicsShapeHandle nextShapeRuntimeHandle()
Internal: next generation-qualified shape handle.
Definition World3D.cpp:410
SimulationObservation simulationObservation() const noexcept
Snapshot of completed backend steps and logical simulation time.
Definition World.cpp:433
eve::SimulationTick simulationTick() const noexcept
Current deterministic tick; save data should persist this value.
Definition World.h:126
eve::Result< void > restore(const eve::SnapshotEnvelope &snapshot, const eve::SnapshotHashProvider &hashProvider)
Restores a verified snapshot without exposing partial state.
Definition WorldSnapshot.cpp:60
b2World * raw()
Exposes the underlying Box2D world for tightly-scoped backend integration.
Definition World.h:417
PhysicsBodyHandle nextBodyRuntimeHandle()
Definition World.cpp:472
void clearContactEvents()
Clears collected begin/end contact and impact event buffers.
Definition World.cpp:760
eve::Result< eve::SnapshotEnvelope > snapshot(const eve::SnapshotHashProvider &hashProvider) const
Captures a versioned, integrity-checked world snapshot.
Definition WorldSnapshot.cpp:35
constexpr std::string_view kWorld2DSchema
Definition WorldSnapshotCodec.h:24
Result< float > readFloat(const Value::Object &object, std::string_view name)
Reads a required finite number representable by float.
Definition WorldSnapshotParsing.cpp:66
eve::Result< std::unique_ptr< World3D > > prepareWorld3D(const std::vector< Body3DState > &states, const eve::Value::Array &shapes, const eve::Value::Array &joints, float gravityX, float gravityY, float gravityZ, const SimulationObservation &observation, eve::PersistentId instanceId, bool legacyParameters, bool allowSleep)
Definition WorldSnapshotCodec.cpp:601
constexpr std::uint64_t kSnapshotVersion
Definition WorldSnapshotCodec.h:27
const Value * field(const Value::Object &object, std::string_view name)
Borrow an optional field only during the current synchronous codec call. @ownership Object owns the r...
Definition WorldSnapshotParsing.cpp:17
eve::Value shapesTopologyValue(const std::vector< Shape3D * > &shapes)
Definition WorldSnapshotCodec.cpp:350
eve::Value make2DPayload(const World &world, const std::vector< Body * > &bodies)
Definition WorldSnapshotCodec.cpp:397
constexpr std::string_view kWorld3DSchema
Definition WorldSnapshotCodec.h:26
eve::Value jointsTopologyValue(const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:360
bool hasExactFields(const Value::Object &object, std::initializer_list< std::string_view > expected)
Internal exact-field schema predicate.
Definition WorldSnapshotParsing.cpp:9
constexpr std::string_view kWorld3DType
Definition WorldSnapshotCodec.h:25
eve::Value make3DPayload(const World3D &world, const std::vector< Body3D * > &bodies, const std::vector< Shape3D * > &shapes, const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:419
constexpr std::string_view kWorld2DType
Definition WorldSnapshotCodec.h:23
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
Definition WorldSnapshotParsing.cpp:75
eve::Result< void > matchBodyIds(const std::vector< BodyState > &states, std::set< int > &ids)
Definition WorldSnapshotCodec.h:55
eve::Result< eve::LogicalId > snapshotSchema(std::string_view text)
Definition WorldSnapshotCodec.cpp:33
Result< void > migratePlanarV2(Value &payload)
Migrate previous schema with explicit defaults for previously omitted state.
Definition PlanarMaterialSnapshot.cpp:102
eve::Result< std::unique_ptr< World > > prepareWorld2D(const std::vector< Body2DState > &states, float gravityX, float gravityY, float meter, const SimulationObservation &observation, eve::PersistentId instanceId)
Definition WorldSnapshotCodec.cpp:472
eve::Result< std::vector< Body2DState > > parseBodies2D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:440
eve::Result< void > checkEnvelope(const eve::SnapshotEnvelope &snapshot, std::string_view type, std::string_view schema)
Definition WorldSnapshotCodec.cpp:41
eve::Result< std::vector< Body3DState > > parseBodies3D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:456
constexpr std::uint64_t kSnapshot2DVersion
Definition WorldSnapshotCodec.h:28
eve::Result< ObservationState > parseObservation(const eve::Value::Object &object, const eve::SnapshotEnvelope &snapshot)
Definition WorldSnapshotCodec.cpp:56
@ InvalidArgument
@ PreconditionViolation
@ InvariantViolation
@ ParseError
@ Conflict
@ Applied
Result< SnapshotEnvelope > makeSnapshotEnvelope(std::string type, LogicalId schema, SchemaVersion schemaVersion, PersistentId instanceId, Revision revision, SimulationTick tick, Value payload, const SnapshotHashProvider &hashProvider)
Construct and seal a snapshot envelope.
Definition Snapshot.cpp:103
Result< void > verifySnapshotEnvelope(const SnapshotEnvelope &snapshot, const SnapshotHashProvider &hashProvider)
Verify an envelope's content hash without modifying it.
Definition Snapshot.cpp:63
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
static Result< void > restoreJoints2D(World &candidate, const Value::Array &joints, std::uint32_t firstHandleIndex)
Rebuild validated joint definitions inside a detached candidate, allocating fresh runtime handles.
Definition JointSnapshotCodec.cpp:169
static Result< void > restoreCouplings3D(World3D &world, const Value::Array &values)
Rebuild transmissions after candidate handles and native joints are ready.
Definition Coupling3DSnapshot.cpp:81
static Result< void > prepareRuntimeLinks(const World3D &live, World3D &prepared)
Allocate fresh 3D links entirely in the detached candidate before publishing topology.
Definition WorldSnapshotAccess.h:140
static Result< Value > captureCouplings3D(const World3D &world)
Capture transmissions with stable source ids and current unwrapped phase.
Definition Coupling3DSnapshot.cpp:34
static Result< Value > captureJoints2D(const World &world)
Capture owning joint definitions; rejects unregistered backend joints.
Definition JointSnapshotCodec.cpp:67
static void adopt(World &live, World &prepared)
Definition WorldSnapshotAccess.h:154