GroupFilter.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/GroupFilter.h>
  6. #include <Jolt/Core/StreamIn.h>
  7. #include <Jolt/Core/StreamOut.h>
  8. #include <Jolt/Core/Factory.h>
  9. JPH_NAMESPACE_BEGIN
  10. JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT_BASE(GroupFilter)
  11. {
  12. JPH_ADD_BASE_CLASS(GroupFilter, SerializableObject)
  13. }
  14. void GroupFilter::SaveBinaryState(StreamOut &inStream) const
  15. {
  16. inStream.Write(GetRTTI()->GetHash());
  17. }
  18. void GroupFilter::RestoreBinaryState(StreamIn &inStream)
  19. {
  20. // RTTI hash is read in sRestoreFromBinaryState
  21. }
  22. GroupFilter::GroupFilterResult GroupFilter::sRestoreFromBinaryState(StreamIn &inStream)
  23. {
  24. GroupFilterResult result;
  25. // Read the type of the group filter
  26. uint32 hash;
  27. inStream.Read(hash);
  28. if (inStream.IsEOF() || inStream.IsFailed())
  29. {
  30. result.SetError("Failed to read type hash");
  31. return result;
  32. }
  33. // Get the RTTI for the group filter
  34. const RTTI *rtti = Factory::sInstance->Find(hash);
  35. if (rtti == nullptr)
  36. {
  37. result.SetError("Failed to create instance of group filter");
  38. return result;
  39. }
  40. // Construct and read the data of the group filter
  41. Ref<GroupFilter> group_filter = reinterpret_cast<GroupFilter *>(rtti->CreateObject());
  42. if (group_filter == nullptr)
  43. {
  44. result.SetError("Failed to create instance of group filter");
  45. return result;
  46. }
  47. group_filter->RestoreBinaryState(inStream);
  48. if (inStream.IsEOF() || inStream.IsFailed())
  49. {
  50. result.SetError("Failed to restore group filter");
  51. return result;
  52. }
  53. result.Set(group_filter);
  54. return result;
  55. }
  56. JPH_NAMESPACE_END