ByteArrayStream.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef ASSETBUILDER_BYTEARRAYSTREAM
  9. #define ASSETBUILDER_BYTEARRAYSTREAM
  10. #include <AzCore/IO/GenericStreams.h>
  11. #include <AzFramework/Asset/AssetProcessorMessages.h>
  12. #include <QByteArray>
  13. namespace AssetProcessor
  14. {
  15. //! Wrap a QByteArray (which has an int-interface) in a GenericStream.
  16. class ByteArrayStream
  17. : public AZ::IO::GenericStream
  18. {
  19. public:
  20. ByteArrayStream();
  21. ByteArrayStream(QByteArray* other); // attach to external
  22. ByteArrayStream(const char* data, unsigned int length); // const attach to read-only buffer
  23. bool IsOpen() const override
  24. {
  25. return true;
  26. }
  27. bool CanSeek() const override
  28. {
  29. return true;
  30. }
  31. virtual bool CanRead() const override { return true; }
  32. virtual bool CanWrite() const override { return true; }
  33. void Seek(AZ::IO::OffsetType bytes, SeekMode mode) override;
  34. AZ::IO::SizeType Read(AZ::IO::SizeType bytes, void* oBuffer) override;
  35. AZ::IO::SizeType Write(AZ::IO::SizeType bytes, const void* iBuffer) override;
  36. AZ::IO::SizeType WriteFromStream(AZ::IO::SizeType bytes, AZ::IO::GenericStream *inputStream) override;
  37. AZ::IO::SizeType GetCurPos() const override;
  38. AZ::IO::SizeType GetLength() const override;
  39. QByteArray GetArray() const; // bytearrays are copy-on-write so retrieving it is akin to retreiving a refcounted object, its cheap to 'copy'
  40. void Reserve(int amount); // for performance.
  41. private:
  42. AZ::IO::SizeType PrepareForWrite(AZ::IO::SizeType bytes);
  43. QByteArray* m_activeArray;
  44. QByteArray m_ownArray; // used when not constructed around an attached array
  45. bool m_usingOwnArray = true; // if false, its been attached
  46. int m_currentPos = 0; // the byte array underlying has only ints :(
  47. bool m_readOnly = false;
  48. };
  49. // Pack any serializable type into a QByteArray
  50. // note that this is not a specialization of the AZFramework version of this function
  51. // because C++ does not support partial specialization of function templates, only classes.
  52. template <class Message>
  53. bool PackMessage(const Message& message, QByteArray& buffer)
  54. {
  55. ByteArrayStream byteStream(&buffer);
  56. return AZ::Utils::SaveObjectToStream(byteStream, AZ::DataStream::ST_BINARY, &message, message.RTTI_GetType());
  57. }
  58. // Unpack any serializable type from a QByteArray
  59. // note that this is not a specialization of the AZFramework version of this function
  60. // because C++ does not support partial specialization of function templates, only classes.
  61. template <class Message>
  62. bool UnpackMessage(const QByteArray& buffer, Message& message)
  63. {
  64. ByteArrayStream byteStream(buffer.constData(), buffer.size());
  65. // we expect network messages to be pristine - so if there's any error, don't allow it!
  66. // also do not allow it to load assets just becuase they're in fields
  67. AZ::ObjectStream::FilterDescriptor filterToUse(&AZ::Data::AssetFilterNoAssetLoading, AZ::ObjectStream::FILTERFLAG_STRICT);
  68. return AZ::Utils::LoadObjectFromStreamInPlace(byteStream, message, nullptr, filterToUse);
  69. }
  70. }
  71. #endif // ASSETBUILDER_BYTEARRAYSTREAM