BsFileSerializer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "BsFileSerializer.h"
  2. #include "BsException.h"
  3. #include "BsIReflectable.h"
  4. #include "BsBinarySerializer.h"
  5. #include "BsFileSystem.h"
  6. #include "BsDebug.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(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. if (mOutputStream.fail())
  20. {
  21. LOGWRN("Failed to save file: \"" + fileLocation.toString() + "\". Error: " + strerror(errno) + ".");
  22. }
  23. }
  24. FileEncoder::~FileEncoder()
  25. {
  26. bs_free(mWriteBuffer);
  27. mOutputStream.close();
  28. mOutputStream.clear();
  29. }
  30. void FileEncoder::encode(IReflectable* object)
  31. {
  32. UINT64 curPos = (UINT64)mOutputStream.tellp();
  33. mOutputStream.seekp(sizeof(UINT32), std::ios_base::cur);
  34. BinarySerializer bs;
  35. UINT32 totalBytesWritten = 0;
  36. bs.encode(object, mWriteBuffer, WRITE_BUFFER_SIZE, &totalBytesWritten, std::bind(&FileEncoder::flushBuffer, this, _1, _2, _3));
  37. mOutputStream.seekp(curPos);
  38. mOutputStream.write((char*)&totalBytesWritten, sizeof(totalBytesWritten));
  39. mOutputStream.seekp(totalBytesWritten, std::ios_base::cur);
  40. }
  41. UINT8* FileEncoder::flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize)
  42. {
  43. mOutputStream.write((const char*)bufferStart, bytesWritten);
  44. return bufferStart;
  45. }
  46. FileDecoder::FileDecoder(const Path& fileLocation)
  47. {
  48. mInputStream.open(fileLocation.toString().c_str(), std::ios::in | std::ios::ate | std::ios::binary);
  49. if (mInputStream.fail())
  50. {
  51. LOGWRN("Failed to open file: \"" + fileLocation.toString() + "\". Error: " + strerror(errno) + ".");
  52. }
  53. std::streamoff fileSize = mInputStream.tellg();
  54. if (fileSize > std::numeric_limits<UINT32>::max())
  55. {
  56. BS_EXCEPT(InternalErrorException,
  57. "File size is larger that UINT32 can hold. Ask a programmer to use a bigger data type.");
  58. }
  59. mInputStream.seekg(0, std::ios::beg);
  60. }
  61. FileDecoder::~FileDecoder()
  62. {
  63. mInputStream.close();
  64. mInputStream.clear();
  65. }
  66. std::shared_ptr<IReflectable> FileDecoder::decode()
  67. {
  68. if (mInputStream.fail() || mInputStream.eof())
  69. return nullptr;
  70. UINT32 objectSize = 0;
  71. mInputStream.read((char*)&objectSize, sizeof(objectSize));
  72. UINT8* readBuffer = (UINT8*)bs_alloc((UINT32)objectSize); // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
  73. mInputStream.read((char*)readBuffer, objectSize);
  74. BinarySerializer bs;
  75. std::shared_ptr<IReflectable> object = bs.decode(readBuffer, objectSize);
  76. bs_free(readBuffer);
  77. return object;
  78. }
  79. void FileDecoder::skip()
  80. {
  81. if (mInputStream.eof())
  82. return;
  83. UINT32 objectSize = 0;
  84. mInputStream.read((char*)&objectSize, sizeof(objectSize));
  85. mInputStream.seekg(objectSize, std::ios::cur);
  86. }
  87. }