//--------------------------------------------------------------------- Codec::DecodeResult PVRTCCodec::decode(DataStreamPtr& stream) const { // Assume its a pvr 2 header PVRTCTexHeaderV2 headerV2; stream->read(&headerV2, sizeof(PVRTCTexHeaderV2)); stream->seek(0); if (PVR2_MAGIC == headerV2.pvrTag) { return decodeV2(stream); } // Try it as pvr 3 header PVRTCTexHeaderV3 headerV3; stream->read(&headerV3, sizeof(PVRTCTexHeaderV3)); stream->seek(0); if (PVR3_MAGIC == headerV3.version) { return decodeV3(stream); } OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "This is not a PVR2 / PVR3 file!", "PVRTCCodec::decode"); }
//--------------------------------------------------------------------- void GpuProgramManager::loadMicrocodeCache( DataStreamPtr stream ) { mMicrocodeCache.clear(); // write the size of the array size_t sizeOfArray = 0; stream->read(&sizeOfArray, sizeof(size_t)); // loop the array and load it for ( size_t i = 0 ; i < sizeOfArray ; i++ ) { String nameOfShader; // loads the name of the shader size_t stringLength = 0; stream->read(&stringLength, sizeof(size_t)); nameOfShader.resize(stringLength); stream->read(&nameOfShader[0], stringLength); // loads the microcode size_t microcodeLength = 0; stream->read(µcodeLength, sizeof(size_t)); Microcode microcodeOfShader(OGRE_NEW MemoryDataStream(nameOfShader, microcodeLength)); microcodeOfShader->seek(0); stream->read(microcodeOfShader->getPtr(), microcodeLength); mMicrocodeCache.insert(make_pair(nameOfShader, microcodeOfShader)); } }
//--------------------------------------------------------------------- void GpuProgramManager::loadMicrocodeCache( DataStreamPtr stream ) { mMicrocodeCache.clear(); // write the size of the array uint32 sizeOfArray = 0; stream->read(&sizeOfArray, sizeof(uint32)); // loop the array and load it for ( uint32 i = 0 ; i < sizeOfArray ; i++ ) { String nameOfShader; // loads the name of the shader uint32 stringLength = 0; stream->read(&stringLength, sizeof(uint32)); nameOfShader.resize(stringLength); stream->read(&nameOfShader[0], stringLength); // loads the microcode uint32 microcodeLength = 0; stream->read(µcodeLength, sizeof(uint32)); Microcode microcodeOfShader(OGRE_NEW MemoryDataStream(nameOfShader, microcodeLength)); microcodeOfShader->seek(0); stream->read(microcodeOfShader->getPtr(), microcodeLength); mMicrocodeCache.insert(make_pair(nameOfShader, microcodeOfShader)); } // if cache is not modified, mark it as clean. mCacheDirty = false; }
//--------------------------------------------------------------------- Codec::DecodeResult PVRTCCodec::decodeV2(DataStreamPtr& stream) const { PVRTCTexHeaderV2 header; uint32 flags = 0, formatFlags = 0; size_t numFaces = 1; // Assume one face until we know otherwise ImageData *imgData = OGRE_NEW ImageData(); MemoryDataStreamPtr output; // Read the PVRTC header stream->read(&header, sizeof(PVRTCTexHeaderV2)); // Get format flags flags = header.flags; flipEndian(reinterpret_cast<void*>(flags), sizeof(uint32)); formatFlags = flags & PVR_TEXTURE_FLAG_TYPE_MASK; uint32 bitmaskAlpha = header.bitmaskAlpha; flipEndian(reinterpret_cast<void*>(bitmaskAlpha), sizeof(uint32)); if (formatFlags == kPVRTextureFlagTypePVRTC_4 || formatFlags == kPVRTextureFlagTypePVRTC_2) { if (formatFlags == kPVRTextureFlagTypePVRTC_4) { imgData->format = bitmaskAlpha ? PF_PVRTC_RGBA4 : PF_PVRTC_RGB4; } else if (formatFlags == kPVRTextureFlagTypePVRTC_2) { imgData->format = bitmaskAlpha ? PF_PVRTC_RGBA2 : PF_PVRTC_RGB2; } imgData->depth = 1; imgData->width = header.width; imgData->height = header.height; imgData->num_mipmaps = static_cast<ushort>(header.numMipmaps); // PVRTC is a compressed format imgData->flags |= IF_COMPRESSED; } // Calculate total size from number of mipmaps, faces and size imgData->size = Image::calculateSize(imgData->num_mipmaps, numFaces, imgData->width, imgData->height, imgData->depth, imgData->format); // Bind output buffer output.bind(OGRE_NEW MemoryDataStream(imgData->size)); // Now deal with the data void *destPtr = output->getPtr(); stream->read(destPtr, imgData->size); destPtr = static_cast<void*>(static_cast<uchar*>(destPtr)); DecodeResult ret; ret.first = output; ret.second = CodecDataPtr(imgData); return ret; }
//--------------------------------------------------------------------- void Serializer::readBools(DataStreamPtr& stream, bool* pDest, size_t count) { //XXX Nasty Hack to convert 1 byte bools to 4 byte bools # if OGRE_PLATFORM == OGRE_PLATFORM_APPLE char * pTemp = (char *)malloc(1*count); // to hold 1-byte bools stream->read(pTemp, 1 * count); for(int i = 0; i < count; i++) *(bool *)(pDest + i) = *(char *)(pTemp + i); free (pTemp); # else stream->read(pDest, sizeof(bool) * count); # endif //no flipping on 1-byte datatypes }
// OgreScriptBuilder int OgreScriptBuilder::LoadScriptSection(const char *filename) { // Open the script file string scriptFile = filename; DataStreamPtr ds; try { ds = ResourceGroupManager::getSingleton().openResource(scriptFile, ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); } catch(Ogre::Exception e) { LOG("exception upon loading script file: " + e.getFullDescription()); return -1; } // Read the entire file string code; code.resize(ds->size()); ds->read(&code[0], ds->size()); // hash it { char hash_result[250]; memset(hash_result, 0, 249); RoR::CSHA1 sha1; sha1.UpdateHash((uint8_t *)code.c_str(), (uint32_t)code.size()); sha1.Final(); sha1.ReportHash(hash_result, RoR::CSHA1::REPORT_HEX_SHORT); hash = String(hash_result); } return ProcessScriptSection(code.c_str(), filename); }
time_t ScriptSerializerManager::getBinaryTimeStamp(const String& filename) { DataStreamPtr stream = mCacheArchive->open(filename); ScriptBlock::ScriptHeader header; stream->read(reinterpret_cast<char*>(&header), sizeof(ScriptBlock::ScriptHeader)); stream->close(); return header.lastModifiedTime; }
void generateHashFromDataStream(DataStreamPtr &ds, Ogre::String &hash) { size_t location = ds->tell(); // copy whole file into a buffer uint8_t *buf = 0; ds->seek(0); // from start // alloc buffer uint32_t bufSize = ds->size(); buf = (uint8_t *)malloc(bufSize+1); // read into buffer ds->read(buf, bufSize); // and build the hash over it char hash_result[250]; memset(hash_result, 0, 249); { RoR::CSHA1 sha1; sha1.UpdateHash(buf, bufSize); sha1.Final(); sha1.ReportHash(hash_result, RoR::CSHA1::REPORT_HEX_SHORT); } // revert DS to previous position ds->seek(location); // release memory free(buf); buf = 0; hash = String(hash_result); }
size_t FileInterfaceOgre3D::Read(void* buffer, size_t size, Rocket::Core::FileHandle file) { if (!file) return 0; DataStreamPtr stream = *(DataStreamPtr*)(file); return stream->read(buffer, size); }
//--------------------------------------------------------------------- String Serializer::readString(DataStreamPtr& stream, size_t numChars) { assert (numChars <= 255); char str[255]; stream->read(str, numChars); str[numChars] = '\0'; return str; }
//--------------------------------------------------------------------- void Serializer::readFloats(DataStreamPtr& stream, double* pDest, size_t count) { // Read from float, convert to double float* tmp = OGRE_ALLOC_T(float, count, MEMCATEGORY_GENERAL); float* ptmp = tmp; stream->read(tmp, sizeof(float) * count); flipFromLittleEndian(tmp, sizeof(float), count); // Convert to doubles (no cast required) while(count--) { *pDest++ = *ptmp++; } OGRE_FREE(tmp, MEMCATEGORY_GENERAL); }
//----------------------------------------------------------------------------------- void HlmsManager::parseScript(DataStreamPtr& stream, const String& groupName) { vector<char>::type fileData; fileData.resize( stream->size() + 1 ); if( !fileData.empty() ) { stream->read( &fileData[0], stream->size() ); //Add null terminator just in case (to prevent bad input) fileData.back() = '\0'; HlmsJson hlmsJson( this ); hlmsJson.loadMaterials( stream->getName(), groupName, &fileData[0] ); } }
//----------------------------------------------------------------------------------- void HlmsManager::loadMaterials( const String &filename, const String &groupName ) { DataStreamPtr stream = ResourceGroupManager::getSingleton().openResource( filename, groupName ); vector<char>::type fileData; fileData.resize( stream->size() + 1 ); if( !fileData.empty() ) { stream->read( &fileData[0], stream->size() ); //Add null terminator just in case (to prevent bad input) fileData.back() = '\0'; HlmsJson hlmsJson( this ); hlmsJson.loadMaterials( stream->getName(), groupName, &fileData[0] ); } }
//--------------------------------------------------------------------- String Image::getFileExtFromMagic(DataStreamPtr stream) { // read the first 32 bytes or file size, if less size_t magicLen = std::min(stream->size(), (size_t)32); char magicBuf[32]; stream->read(magicBuf, magicLen); // return to start stream->seek(0); Codec* pCodec = Codec::getCodec(magicBuf, magicLen); if(pCodec) return pCodec->getType(); else return BLANKSTRING; }
Script::Script(DataStreamPtr & dataStreamPtr) { m_bufSize = dataStreamPtr->size(); m_scriptBuffer = new char[m_bufSize]; dataStreamPtr->read(m_scriptBuffer, m_bufSize); m_scriptBuffer[m_bufSize] = '\0'; m_luaState = luaL_newstate(); // Parse the Lua script. luaL_dostring(m_luaState, m_scriptBuffer); // Load Lua's std libs. luaL_openlibs(m_luaState); luabind::open(m_luaState); }
/** * @author JoSch * @date 07-12-2005 */ void Fmod3SoundSample::load() throw (RuntimeException) { getSoundResource()->load(); DataStreamPtr stream = getSoundResource()->getDataStream(); stream->seek(0); int len = stream->size(); char *data = new char[len]; stream->read(data, len); unsigned int mode = FSOUND_LOADMEMORY; if (is3d()) { mode |= FSOUND_HW3D | FSOUND_FORCEMONO; } else { mode |= FSOUND_HW2D; } if (isLooping()) { mode |= FSOUND_LOOP_NORMAL; } else { mode |= FSOUND_LOOP_OFF; } mSample = FSOUND_Sample_Load(FSOUND_FREE, data, mode, 0, len); if (mSample == 0 && !is3d() ) { mode |= FSOUND_FORCEMONO; mSample = FSOUND_Sample_Load(FSOUND_FREE, data, mode, 0, len); } delete[] data; if( mSample == NULL ) { int err = FSOUND_GetError(); Throw( RuntimeException, "Fmod Error:" + Ogre::StringConverter::toString(err) + " while loading " + getName() ); } }
//--------------------------------------------------------------------- void Serializer::readInts(DataStreamPtr& stream, unsigned int* pDest, size_t count) { stream->read(pDest, sizeof(unsigned int) * count); flipFromLittleEndian(pDest, sizeof(unsigned int), count); }
//--------------------------------------------------------------------- void Serializer::readFloats(DataStreamPtr& stream, float* pDest, size_t count) { stream->read(pDest, sizeof(float) * count); flipFromLittleEndian(pDest, sizeof(float), count); }
//--------------------------------------------------------------------- Codec::DecodeResult PVRTCCodec::decodeV3(DataStreamPtr& stream) const { PVRTCTexHeaderV3 header; PVRTCMetadata metadata; uint32 flags = 0; size_t numFaces = 1; // Assume one face until we know otherwise ImageData *imgData = OGRE_NEW ImageData(); MemoryDataStreamPtr output; // Read the PVRTC header stream->read(&header, sizeof(PVRTCTexHeaderV3)); // Read the PVRTC metadata if(header.metaDataSize) { stream->read(&metadata, sizeof(PVRTCMetadata)); } // Identify the pixel format switch (header.pixelFormat) { case kPVRTC1_PF_2BPP_RGB: imgData->format = PF_PVRTC_RGB2; break; case kPVRTC1_PF_2BPP_RGBA: imgData->format = PF_PVRTC_RGBA2; break; case kPVRTC1_PF_4BPP_RGB: imgData->format = PF_PVRTC_RGB4; break; case kPVRTC1_PF_4BPP_RGBA: imgData->format = PF_PVRTC_RGBA4; break; case kPVRTC2_PF_2BPP: imgData->format = PF_PVRTC2_2BPP; break; case kPVRTC2_PF_4BPP: imgData->format = PF_PVRTC2_4BPP; break; } // Get format flags flags = header.flags; flipEndian(reinterpret_cast<void*>(flags), sizeof(uint32)); imgData->depth = header.depth; imgData->width = header.width; imgData->height = header.height; imgData->num_mipmaps = static_cast<ushort>(header.mipMapCount); // PVRTC is a compressed format imgData->flags |= IF_COMPRESSED; if(header.numFaces == 6) imgData->flags |= IF_CUBEMAP; if(header.depth > 1) imgData->flags |= IF_3D_TEXTURE; // Calculate total size from number of mipmaps, faces and size imgData->size = Image::calculateSize(imgData->num_mipmaps, numFaces, imgData->width, imgData->height, imgData->depth, imgData->format); // Bind output buffer output.bind(OGRE_NEW MemoryDataStream(imgData->size)); // Now deal with the data void *destPtr = output->getPtr(); uint width = imgData->width; uint height = imgData->height; uint depth = imgData->depth; // All mips for a surface, then each face for(size_t mip = 0; mip <= imgData->num_mipmaps; ++mip) { for(size_t surface = 0; surface < header.numSurfaces; ++surface) { for(size_t i = 0; i < numFaces; ++i) { // Load directly size_t pvrSize = PixelUtil::getMemorySize(width, height, depth, imgData->format); stream->read(destPtr, pvrSize); destPtr = static_cast<void*>(static_cast<uchar*>(destPtr) + pvrSize); } } // Next mip if(width!=1) width /= 2; if(height!=1) height /= 2; if(depth!=1) depth /= 2; } DecodeResult ret; ret.first = output; ret.second = CodecDataPtr(imgData); return ret; }
//--------------------------------------------------------------------- void Serializer::readBools(DataStreamPtr& stream, bool* pDest, size_t count) { static_assert(sizeof(bool) == 1, "add conversion to char for your platform"); stream->read(pDest, sizeof(bool) * count); }
//--------------------------------------------------------------------- bool ETCCodec::decodePKM(DataStreamPtr& stream, DecodeResult& result) const { PKMHeader header; // Read the ETC header stream->read(&header, sizeof(PKMHeader)); if (PKM_MAGIC != FOURCC(header.name[0], header.name[1], header.name[2], header.name[3]) ) // "PKM 10" return false; uint16 width = (header.iWidthMSB << 8) | header.iWidthLSB; uint16 height = (header.iHeightMSB << 8) | header.iHeightLSB; uint16 paddedWidth = (header.iPaddedWidthMSB << 8) | header.iPaddedWidthLSB; uint16 paddedHeight = (header.iPaddedHeightMSB << 8) | header.iPaddedHeightLSB; uint16 type = (header.iTextureTypeMSB << 8) | header.iTextureTypeLSB; ImageData *imgData = OGRE_NEW ImageData(); imgData->depth = 1; imgData->width = width; imgData->height = height; // File version 2.0 supports ETC2 in addition to ETC1 if(header.version[0] == '2' && header.version[1] == '0') { switch (type) { case 0: imgData->format = PF_ETC1_RGB8; break; // GL_COMPRESSED_RGB8_ETC2 case 1: imgData->format = PF_ETC2_RGB8; break; // GL_COMPRESSED_RGBA8_ETC2_EAC case 3: imgData->format = PF_ETC2_RGBA8; break; // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 case 4: imgData->format = PF_ETC2_RGB8A1; break; // Default case is ETC1 default: imgData->format = PF_ETC1_RGB8; break; } } else imgData->format = PF_ETC1_RGB8; // ETC has no support for mipmaps - malideveloper.com has a example // where the load mipmap levels from different external files imgData->num_mipmaps = 0; // ETC is a compressed format imgData->flags |= IF_COMPRESSED; // Calculate total size from number of mipmaps, faces and size imgData->size = (paddedWidth * paddedHeight) >> 1; // Bind output buffer MemoryDataStreamPtr output; output.bind(OGRE_NEW MemoryDataStream(imgData->size)); // Now deal with the data void *destPtr = output->getPtr(); stream->read(destPtr, imgData->size); destPtr = static_cast<void*>(static_cast<uchar*>(destPtr)); DecodeResult ret; ret.first = output; ret.second = CodecDataPtr(imgData); return true; }
//--------------------------------------------------------------------- bool ETCCodec::decodeKTX(DataStreamPtr& stream, DecodeResult& result) const { KTXHeader header; // Read the ETC1 header stream->read(&header, sizeof(KTXHeader)); const uint8 KTXFileIdentifier[12] = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }; if (memcmp(KTXFileIdentifier, &header.identifier, sizeof(KTXFileIdentifier)) != 0 ) return false; if (header.endianness == KTX_ENDIAN_REF_REV) flipEndian(&header.glType, sizeof(uint32), 1); ImageData *imgData = OGRE_NEW ImageData(); imgData->depth = 1; imgData->width = header.pixelWidth; imgData->height = header.pixelHeight; imgData->num_mipmaps = static_cast<ushort>(header.numberOfMipmapLevels - 1); switch(header.glInternalFormat) { case 37492: // GL_COMPRESSED_RGB8_ETC2 imgData->format = PF_ETC2_RGB8; break; case 37496:// GL_COMPRESSED_RGBA8_ETC2_EAC imgData->format = PF_ETC2_RGBA8; break; case 37494: // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 imgData->format = PF_ETC2_RGB8A1; break; case 35986: // ATC_RGB imgData->format = PF_ATC_RGB; break; case 35987: // ATC_RGB_Explicit imgData->format = PF_ATC_RGBA_EXPLICIT_ALPHA; break; case 34798: // ATC_RGB_Interpolated imgData->format = PF_ATC_RGBA_INTERPOLATED_ALPHA; break; case 33777: // DXT 1 imgData->format = PF_DXT1; break; case 33778: // DXT 3 imgData->format = PF_DXT3; break; case 33779: // DXT 5 imgData->format = PF_DXT5; break; default: imgData->format = PF_ETC1_RGB8; break; } imgData->flags = 0; if (header.glType == 0 || header.glFormat == 0) imgData->flags |= IF_COMPRESSED; size_t numFaces = 1; // Assume one face until we know otherwise // Calculate total size from number of mipmaps, faces and size imgData->size = Image::calculateSize(imgData->num_mipmaps, numFaces, imgData->width, imgData->height, imgData->depth, imgData->format); stream->skip(header.bytesOfKeyValueData); // Bind output buffer MemoryDataStreamPtr output; output.bind(OGRE_NEW MemoryDataStream(imgData->size)); // Now deal with the data uchar* destPtr = output->getPtr(); for (uint32 level = 0; level < header.numberOfMipmapLevels; ++level) { uint32 imageSize = 0; stream->read(&imageSize, sizeof(uint32)); stream->read(destPtr, imageSize); destPtr += imageSize; } result.first = output; result.second = CodecDataPtr(imgData); return true; }