BsFileSerializer.cpp 3.0 KB

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