BsFileSerializer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "BsFileSerializer.h"
  2. #include "BsException.h"
  3. #include "BsIReflectable.h"
  4. #include "BsBinarySerializer.h"
  5. #include "BsPath.h"
  6. #include "BsFileSystem.h"
  7. #include <numeric>
  8. using namespace std::placeholders;
  9. namespace BansheeEngine
  10. {
  11. FileEncoder::FileEncoder(const Path& fileLocation)
  12. :mWriteBuffer(nullptr)
  13. {
  14. mWriteBuffer = (UINT8*)bs_alloc<ScratchAlloc>(WRITE_BUFFER_SIZE);
  15. Path parentDir = fileLocation.getDirectory();
  16. if (!FileSystem::exists(parentDir))
  17. FileSystem::createDir(parentDir);
  18. mOutputStream.open(fileLocation.toString().c_str(), std::ios::out | std::ios::binary);
  19. }
  20. FileEncoder::~FileEncoder()
  21. {
  22. bs_free<ScratchAlloc>(mWriteBuffer);
  23. mOutputStream.close();
  24. mOutputStream.clear();
  25. }
  26. void FileEncoder::encode(IReflectable* object)
  27. {
  28. UINT64 curPos = (UINT64)mOutputStream.tellp();
  29. mOutputStream.seekp(sizeof(UINT32), std::ios_base::cur);
  30. BinarySerializer bs;
  31. int totalBytesWritten = 0;
  32. bs.encode(object, mWriteBuffer, WRITE_BUFFER_SIZE, &totalBytesWritten, std::bind(&FileEncoder::flushBuffer, this, _1, _2, _3));
  33. mOutputStream.seekp(curPos);
  34. mOutputStream.write((char*)&totalBytesWritten, sizeof(totalBytesWritten));
  35. mOutputStream.seekp(totalBytesWritten, std::ios_base::cur);
  36. }
  37. UINT8* FileEncoder::flushBuffer(UINT8* bufferStart, int bytesWritten, UINT32& newBufferSize)
  38. {
  39. mOutputStream.write((const char*)bufferStart, bytesWritten);
  40. return bufferStart;
  41. }
  42. FileDecoder::FileDecoder(const Path& fileLocation)
  43. {
  44. mInputStream.open(fileLocation.toString().c_str(), std::ios::in | std::ios::ate | std::ios::binary);
  45. std::streamoff fileSize = mInputStream.tellg();
  46. if (fileSize > std::numeric_limits<UINT32>::max())
  47. {
  48. BS_EXCEPT(InternalErrorException,
  49. "File size is larger that UINT32 can hold. Ask a programmer to use a bigger data type.");
  50. }
  51. mInputStream.seekg(0, std::ios::beg);
  52. }
  53. FileDecoder::~FileDecoder()
  54. {
  55. mInputStream.close();
  56. mInputStream.clear();
  57. }
  58. std::shared_ptr<IReflectable> FileDecoder::decode()
  59. {
  60. if (mInputStream.eof())
  61. return nullptr;
  62. UINT32 objectSize = 0;
  63. mInputStream.read((char*)&objectSize, sizeof(objectSize));
  64. UINT8* readBuffer = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)objectSize); // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
  65. mInputStream.read((char*)readBuffer, objectSize);
  66. BinarySerializer bs;
  67. std::shared_ptr<IReflectable> object = bs.decode(readBuffer, objectSize);
  68. bs_free<ScratchAlloc>(readBuffer);
  69. return object;
  70. }
  71. void FileDecoder::skip()
  72. {
  73. if (mInputStream.eof())
  74. return;
  75. UINT32 objectSize = 0;
  76. mInputStream.read((char*)&objectSize, sizeof(objectSize));
  77. mInputStream.seekg(objectSize, std::ios::cur);
  78. }
  79. }