|
|
@@ -12,63 +12,94 @@ using namespace std::placeholders;
|
|
|
|
|
|
namespace BansheeEngine
|
|
|
{
|
|
|
- FileSerializer::FileSerializer()
|
|
|
+ FileEncoder::FileEncoder(const Path& fileLocation)
|
|
|
+ :mWriteBuffer(nullptr)
|
|
|
{
|
|
|
mWriteBuffer = (UINT8*)bs_alloc<ScratchAlloc>(WRITE_BUFFER_SIZE);
|
|
|
+
|
|
|
+ Path parentDir = fileLocation.getDirectory();
|
|
|
+ if (!FileSystem::exists(parentDir))
|
|
|
+ FileSystem::createDir(parentDir);
|
|
|
+
|
|
|
+ mOutputStream.open(fileLocation.toString().c_str(), std::ios::out | std::ios::binary);
|
|
|
}
|
|
|
|
|
|
- FileSerializer::~FileSerializer()
|
|
|
+ FileEncoder::~FileEncoder()
|
|
|
{
|
|
|
bs_free<ScratchAlloc>(mWriteBuffer);
|
|
|
+
|
|
|
+ mOutputStream.close();
|
|
|
+ mOutputStream.clear();
|
|
|
}
|
|
|
|
|
|
- void FileSerializer::encode(IReflectable* object, const Path& fileLocation)
|
|
|
+ void FileEncoder::encode(IReflectable* object)
|
|
|
{
|
|
|
- Path parentDir = fileLocation.getDirectory();
|
|
|
- if (!FileSystem::exists(parentDir))
|
|
|
- FileSystem::createDir(parentDir);
|
|
|
-
|
|
|
- mOutputStream.open(fileLocation.toString().c_str(), std::ios::out | std::ios::binary);
|
|
|
+ UINT64 curPos = (UINT64)mOutputStream.tellp();
|
|
|
+ mOutputStream.seekp(sizeof(UINT32), std::ios_base::cur);
|
|
|
|
|
|
BinarySerializer bs;
|
|
|
int totalBytesWritten = 0;
|
|
|
- bs.encode(object, mWriteBuffer, WRITE_BUFFER_SIZE, &totalBytesWritten, std::bind(&FileSerializer::flushBuffer, this, _1, _2, _3));
|
|
|
+ bs.encode(object, mWriteBuffer, WRITE_BUFFER_SIZE, &totalBytesWritten, std::bind(&FileEncoder::flushBuffer, this, _1, _2, _3));
|
|
|
|
|
|
- mOutputStream.close();
|
|
|
- mOutputStream.clear();
|
|
|
+ mOutputStream.seekp(curPos);
|
|
|
+ mOutputStream.write((char*)&totalBytesWritten, sizeof(totalBytesWritten));
|
|
|
+ mOutputStream.seekp(totalBytesWritten, std::ios_base::cur);
|
|
|
+ }
|
|
|
+
|
|
|
+ UINT8* FileEncoder::flushBuffer(UINT8* bufferStart, int bytesWritten, UINT32& newBufferSize)
|
|
|
+ {
|
|
|
+ mOutputStream.write((const char*)bufferStart, bytesWritten);
|
|
|
+
|
|
|
+ return bufferStart;
|
|
|
}
|
|
|
|
|
|
- std::shared_ptr<IReflectable> FileSerializer::decode(const Path& fileLocation)
|
|
|
+ FileDecoder::FileDecoder(const Path& fileLocation)
|
|
|
{
|
|
|
mInputStream.open(fileLocation.toString().c_str(), std::ios::in | std::ios::ate | std::ios::binary);
|
|
|
-
|
|
|
+
|
|
|
std::streamoff fileSize = mInputStream.tellg();
|
|
|
- if(fileSize > std::numeric_limits<UINT32>::max())
|
|
|
+ if (fileSize > std::numeric_limits<UINT32>::max())
|
|
|
{
|
|
|
- BS_EXCEPT(InternalErrorException,
|
|
|
+ BS_EXCEPT(InternalErrorException,
|
|
|
"File size is larger that UINT32 can hold. Ask a programmer to use a bigger data type.");
|
|
|
}
|
|
|
|
|
|
- UINT8* readBuffer = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)fileSize); // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
|
|
|
-
|
|
|
mInputStream.seekg(0, std::ios::beg);
|
|
|
- mInputStream.read((char*)readBuffer, fileSize);
|
|
|
-
|
|
|
- BinarySerializer bs;
|
|
|
- std::shared_ptr<IReflectable> object = bs.decode(readBuffer, (UINT32)fileSize);
|
|
|
+ }
|
|
|
|
|
|
+ FileDecoder::~FileDecoder()
|
|
|
+ {
|
|
|
mInputStream.close();
|
|
|
mInputStream.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ std::shared_ptr<IReflectable> FileDecoder::decode()
|
|
|
+ {
|
|
|
+ if (mInputStream.eof())
|
|
|
+ return nullptr;
|
|
|
+
|
|
|
+ UINT32 objectSize = 0;
|
|
|
+ mInputStream.read((char*)&objectSize, sizeof(objectSize));
|
|
|
+
|
|
|
+ UINT8* readBuffer = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)objectSize); // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
|
|
|
+ mInputStream.read((char*)readBuffer, objectSize);
|
|
|
+
|
|
|
+ BinarySerializer bs;
|
|
|
+ std::shared_ptr<IReflectable> object = bs.decode(readBuffer, objectSize);
|
|
|
|
|
|
bs_free<ScratchAlloc>(readBuffer);
|
|
|
|
|
|
return object;
|
|
|
}
|
|
|
|
|
|
- UINT8* FileSerializer::flushBuffer(UINT8* bufferStart, int bytesWritten, UINT32& newBufferSize)
|
|
|
+ void FileDecoder::skip()
|
|
|
{
|
|
|
- mOutputStream.write((const char*)bufferStart, bytesWritten);
|
|
|
+ if (mInputStream.eof())
|
|
|
+ return;
|
|
|
|
|
|
- return bufferStart;
|
|
|
+ UINT32 objectSize = 0;
|
|
|
+ mInputStream.read((char*)&objectSize, sizeof(objectSize));
|
|
|
+
|
|
|
+ mInputStream.seekg(objectSize, std::ios::cur);
|
|
|
}
|
|
|
}
|