GroupFilter.cpp 1.6 KB

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