Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/StateRecorderImpl.cpp
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/StateRecorderImpl.h>78JPH_NAMESPACE_BEGIN910void StateRecorderImpl::WriteBytes(const void *inData, size_t inNumBytes)11{12mStream.write((const char *)inData, inNumBytes);13}1415void StateRecorderImpl::Rewind()16{17mStream.seekg(0, std::stringstream::beg);18}1920void StateRecorderImpl::Clear()21{22mStream.clear();23mStream.str({});24}2526void StateRecorderImpl::ReadBytes(void *outData, size_t inNumBytes)27{28if (IsValidating())29{30// Read data in temporary buffer to compare with current value31void *data = JPH_STACK_ALLOC(inNumBytes);32mStream.read((char *)data, inNumBytes);33if (memcmp(data, outData, inNumBytes) != 0)34{35// Mismatch, print error36Trace("Mismatch reading %u bytes", (uint)inNumBytes);37for (size_t i = 0; i < inNumBytes; ++i)38{39int b1 = reinterpret_cast<uint8 *>(outData)[i];40int b2 = reinterpret_cast<uint8 *>(data)[i];41if (b1 != b2)42Trace("Offset %d: %02X -> %02X", i, b1, b2);43}44JPH_BREAKPOINT;45}4647// Copy the temporary data to the final destination48memcpy(outData, data, inNumBytes);49return;50}5152mStream.read((char *)outData, inNumBytes);53}5455bool StateRecorderImpl::IsEqual(StateRecorderImpl &inReference)56{57// Get length of new state58mStream.seekg(0, std::stringstream::end);59std::streamoff this_len = mStream.tellg();60mStream.seekg(0, std::stringstream::beg);6162// Get length of old state63inReference.mStream.seekg(0, std::stringstream::end);64std::streamoff reference_len = inReference.mStream.tellg();65inReference.mStream.seekg(0, std::stringstream::beg);6667// Compare size68bool fail = reference_len != this_len;69if (fail)70{71Trace("Failed to properly recover state, different stream length!");72return false;73}7475// Compare byte by byte76for (std::streamoff i = 0, l = this_len; !fail && i < l; ++i)77{78fail = inReference.mStream.get() != mStream.get();79if (fail)80{81Trace("Failed to properly recover state, different at offset %d!", (int)i);82return false;83}84}8586return true;87}8889JPH_NAMESPACE_END909192