ByteArray Inflate(ByteArray CompressedSource, int& err, uint32_t ChunkSize = 0x100000 ) 
{
	// Create a dynamic buffer to hold compressed blocks
	vector<unique_ptr<byte> > blocks;

	z_stream strm  = {};
	strm.data_type = Z_BINARY;
	strm.total_in  = strm.avail_in  = (uInt)CompressedSource->size();
	strm.next_in   = CompressedSource->data();

	err = inflateInit2(&strm, (15 + 32)); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib

	while (err == Z_OK || err == Z_BUF_ERROR)
	{
		strm.avail_out = ChunkSize;
		strm.next_out = (byte*)malloc(ChunkSize);
		blocks.emplace_back(strm.next_out);
		err = inflate(&strm, Z_NO_FLUSH);
	}

	if (err != Z_STREAM_END) 
	{
		inflateEnd(&strm);
		return NullFile;
	}

	ASSERT(strm.total_out > 0, "Nothing to decompress");

	Utility::ByteArray byteArray = make_shared<vector<byte> >( strm.total_out );

	// Allocate actual memory for this.
	// copy the bits into that RAM.
	// Free everything else up!!
	void* curDest = byteArray->data();
	size_t remaining = byteArray->size();

	for (size_t i = 0; i < blocks.size(); ++i)
	{
		ASSERT(remaining > 0);

		size_t CopySize = min(remaining, (size_t)ChunkSize);

		memcpy(curDest, blocks[i].get(), CopySize);
		curDest = (byte*)curDest + CopySize;
		remaining -= CopySize;
	}

	inflateEnd(&strm);

	return byteArray;
}
		bool Load( const wstring& fileName )
		{
			Utility::ByteArray ba = Utility::ReadFileSync( fileName );

			if (ba->size() == 0)
			{
				ERROR( "Cannot open file %ls", fileName.c_str() );
				return false;
			}

			LoadFromBinary( fileName.c_str(), ba->data(), ba->size() );

			return true;
		}
Esempio n. 3
0
const ManagedTexture* TextureManager::LoadDDSFromFile( const std::wstring& fileName, bool sRGB )
{
	auto ManagedTex = FindOrLoadTexture(fileName);

	ManagedTexture* ManTex = ManagedTex.first;
	const bool RequestsLoad = ManagedTex.second;

	if (!RequestsLoad)
	{
		ManTex->WaitForLoad();
		return ManTex;
	}

	Utility::ByteArray ba = Utility::ReadFileSync( s_RootPath + fileName );
	if (ba->size() == 0 || !ManTex->CreateDDSFromMemory( ba->data(), ba->size(), sRGB ))
		ManTex->SetToInvalidTexture();

	return ManTex;
}
ByteArray ReadFileHelper(const wstring& fileName)
{
	struct _stat64 fileStat;
	int fileExists = _wstat64(fileName.c_str(), &fileStat);
	if (fileExists == -1)
		return NullFile;

	ifstream& file = ifstream( fileName, ios::in | ios::binary );
	if (!file)
		return NullFile;

	Utility::ByteArray byteArray = make_shared<vector<byte> >( file.seekg(0, ios::end).tellg() );
	file.seekg(0, ios::beg).read( (char*)byteArray->data(), byteArray->size() );
	file.close();

	ASSERT(byteArray->size() == fileStat.st_size);

	return byteArray;
}