char* LoadTextFileAsBuffer(const char* fileName) { char* returnedBuffer = NULL; IFile* fileHandle = behaviac::CFileManager::GetInstance()->FileOpen(fileName, CFileSystem::EOpenAccess_Read); if (fileHandle) { uint32_t fileSize = (uint32_t)fileHandle->GetSize(); char* fileBuffer = (char*)BEHAVIAC_MALLOC_WITHTAG(fileSize + 1, "TextFileBuffer"); if (fileHandle->Read(fileBuffer, fileSize) == fileSize) { fileBuffer[fileSize] = '\0'; returnedBuffer = fileBuffer; } else { BEHAVIAC_ASSERT(0, "Fail to read the text file : %s\n", fileName); } behaviac::CFileManager::GetInstance()->FileClose(fileHandle); } else { BEHAVIAC_ASSERT(0, "Fail to open the text file : %s\n", fileName); } return returnedBuffer; }
// Returns Information about the File associated with the specified File Handle. VFS_BOOL VFS_File_GetInfo( VFS_Handle hFile, VFS_EntityInfo& Info ) { // Not initialized yet? if( !IsInit() ) { SetLastError( VFS_ERROR_NOT_INITIALIZED_YET ); return VFS_FALSE; } // Invalid Handle Value? if( hFile == VFS_INVALID_HANDLE_VALUE ) { SetLastError( VFS_ERROR_INVALID_PARAMETER ); return VFS_FALSE; } // Get the File Pointer. IFile* pFile = ( IFile* )( VFS_DWORD )hFile; // Fill the Entity Information Structure. Info.bArchived = pFile->IsArchived(); Info.eType = VFS_FILE; Info.lSize = pFile->GetSize(); Info.strPath = pFile->GetFileName(); return VFS_Util_GetName( Info.strPath, Info.strName ); }
int main(int argc, char** argv) { _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); IFile::OpenMode mode = IFile::O_ReadOnly; assert(argc>1); IFile *pFile = OpenDiskFile(argv[1], mode); BlockManager* pMgr = new BlockManager(pFile, mode==IFile::O_Truncate, offset_type(1024)); BlockFS *pFS = new BlockFS(pMgr, mode); std::vector<std::string> names; pFS->ExportFileNames(names); for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) { IFile* pUnpackedFile = pFS->OpenFileInPackage(it->c_str()); IFile* pTemp = OpenDiskFile(it->c_str(), IFile::O_Truncate); offset_type length = pUnpackedFile->GetSize(); char* buff = new char[(size_t)length.offset]; printf("Unpacking %s\n", it->c_str()); pUnpackedFile->Read(buff, length); pTemp->Write(buff, length); delete []buff; delete pTemp; delete pUnpackedFile; } delete pFS; delete pMgr; delete pFile; }
char* Workspace::ReadFileToBuffer(const char* file, uint32_t& bufferSize) { IFile* fp = behaviac::CFileManager::GetInstance()->FileOpen(file, CFileSystem::EOpenAccess_Read); if (!fp) { return 0; } //fp->Seek(0, CFileSystem::ESeekMoveMode_End); uint32_t fileSize = (uint32_t)fp->GetSize(); bufferSize = fileSize + 1; char* pBuffer = 0; for (int i = 0; i < kFileBuffers; ++i) { FileBuffer_t& fileBuffer = this->m_fileBuffers[i]; BEHAVIAC_ASSERT(fileBuffer.offset == 0 || fileBuffer.offset < fileBuffer.length); if (fileBuffer.start == 0) { //to allocate extra 10k int fileBufferLength = bufferSize + 10 * 1024; const int kBufferLength = 100 * 1024; if (fileBufferLength < kBufferLength) { fileBufferLength = kBufferLength; } fileBuffer.start = (char*)BEHAVIAC_MALLOC(fileBufferLength); fileBuffer.length = fileBufferLength; BEHAVIAC_ASSERT(fileBuffer.offset == 0); pBuffer = fileBuffer.start; fileBuffer.offset += bufferSize; BEHAVIAC_ASSERT(fileBuffer.offset < fileBuffer.length); break; } else if (bufferSize < fileBuffer.length - fileBuffer.offset) { pBuffer = fileBuffer.start + fileBuffer.offset; fileBuffer.offset += bufferSize; BEHAVIAC_ASSERT(fileBuffer.offset < fileBuffer.length); break; } } BEHAVIAC_ASSERT(pBuffer); fp->Read(pBuffer, sizeof(char) * fileSize); pBuffer[fileSize] = 0; behaviac::CFileManager::GetInstance()->FileClose(fp); return pBuffer; }
int main(int argc, char* argv[]) { _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); if(argc < 3) { printf("usage: %s directory package", argv[0]); return 0; } IFile::OpenMode mode = IFile::O_Write; if(!PathFileExistsA(argv[1])) mode = IFile::O_Truncate; IFile *pFile = OpenDiskFile(argv[1], mode); BlockManager* pMgr = new BlockManager(pFile, mode==IFile::O_Truncate, offset_type(1024)); BlockFS *pFS = new BlockFS(pMgr, mode); std::vector<BlockFileEntry> vecEntries = pFS->GetEntries(); std::map<std::string, MD5Index> mapEntries; for(std::vector<BlockFileEntry>::iterator it = vecEntries.begin(); it != vecEntries.end(); ++it) { if(std::string("")!=it->name) { assert(mapEntries.find(it->name)==mapEntries.end()); mapEntries[it->name] = it->index; } } std::vector<std::string> add; ScanFileRecursively(argv[2], pFS, mapEntries, add); for(std::map<std::string, MD5Index>::iterator it = mapEntries.begin(); it != mapEntries.end(); ++it) pFS->RemoveFile(it->first.c_str()); for(std::vector<std::string>::iterator it = add.begin(); it != add.end(); ++it) { IFile* pTemp = OpenDiskFile(it->c_str(), IFile::O_ReadOnly); offset_type length = pTemp->GetSize(); char* buff = new char[(size_t)length.offset]; pTemp->Read(buff, length); printf("adding %s, %d bytes\n", it->c_str(), length); pFS->AddFile(it->c_str(), buff, length, length > pFS->GetBlockDataSize()); delete[]buff; delete pTemp; } delete pFS; delete pMgr; delete pFile; return 0; }
char* Workspace::ReadFileToBuffer(const char* file) { IFile* fp = behaviac::CFileManager::GetInstance()->FileOpen(file, CFileSystem::EOpenAccess_Read); if (!fp) { return 0; } //fp->Seek(0, CFileSystem::ESeekMoveMode_End); uint32_t fileSize = (uint32_t)fp->GetSize(); BEHAVIAC_ASSERT(m_fileBufferTop < kFileBufferDepth - 1, "please increase kFileBufferDepth"); uint32_t offset = m_fileBufferOffset[m_fileBufferTop++]; uint32_t offsetNew = offset + fileSize + 1; BEHAVIAC_ASSERT(m_fileBufferTop < kFileBufferDepth - 1, "please increase kFileBufferDepth"); m_fileBufferOffset[m_fileBufferTop] = offsetNew; if (m_fileBuffer == 0 || offsetNew > m_fileBufferLength) { //to allocate extra 10k m_fileBufferLength = offsetNew + 10 * 1024; if (m_fileBufferLength < 50 * 1024) { m_fileBufferLength = 50 * 1024; } m_fileBuffer = (char*)BEHAVIAC_REALLOC(m_fileBuffer, m_fileBufferLength); } BEHAVIAC_ASSERT(m_fileBuffer); BEHAVIAC_ASSERT(offsetNew < m_fileBufferLength); char* pBuffer = m_fileBuffer + offset; fp->Read(pBuffer, sizeof(char) * fileSize); pBuffer[fileSize] = 0; behaviac::CFileManager::GetInstance()->FileClose(fp); return pBuffer; }
VFS_LONG VFS_File_GetSize( VFS_Handle hFile ) { // Not initialized yet? if( !IsInit() ) { SetLastError( VFS_ERROR_NOT_INITIALIZED_YET ); return VFS_INVALID_LONG_VALUE; } // Invalid Handle Value? if( hFile == VFS_INVALID_HANDLE_VALUE ) { SetLastError( VFS_ERROR_INVALID_PARAMETER ); return VFS_INVALID_LONG_VALUE; } // Get the File Pointer. IFile* pFile = ( IFile* )( VFS_DWORD )hFile; return pFile->GetSize(); }
void CLandscape::Load( const char * szFileName ) { if ( !szFileName ) return; IFile * pFile = NULL; g_pVfs->OpenFile( &pFile, szFileName, VFS_READ | VFS_BINARY ); if ( !pFile ) return; byte * pHeightMap = NEW byte [ pFile->GetSize() ]; if ( pHeightMap ) g_pVfs->Read( pFile, pHeightMap, pFile->GetSize() ); g_pVfs->CloseFile( &pFile ); //m_vSize.Set( 128, 128 ); m_nTileCount = 16; m_pTiles = NEW CLandTile [ m_nTileCount ]; for ( uint y = 0; y < 4; ++y ) { for ( uint x = 0; x < 4; ++x ) { uint nOffset = ( 129 * y * CLandTile::SIDE_CELL_COUNT ) + ( CLandTile::SIDE_CELL_COUNT * x ); uint nTile = y * 4 + x; m_pTiles[ nTile ].Create( pHeightMap + nOffset, 129 ); } } /* // nrml for ( uint nTri = 0; nTri < m_nTriangleCount; ++nTri ) { int nOffset = nTri * 3; TVertex & v1 = pVertices[ m_pIndices[ nOffset++ ] ]; TVertex & v2 = pVertices[ m_pIndices[ nOffset++ ] ]; TVertex & v3 = pVertices[ m_pIndices[ nOffset ] ]; vec3 vNormal = Cross( v1.vPos - v3.vPos, v1.vPos - v2.vPos ); vNormal.Normalize(); /*v1.vNormal = vNormal; v2.vNormal = vNormal; v3.vNormal = vNormal;* }*/ DEL_ARRAY( pHeightMap ); m_pVDecl = g_pRenderer->CreateVertexDecl(); if ( m_pVDecl ) { m_pVDecl->SetStride( sizeof( TLandVertex ) ); m_pVDecl->AddAttr( "vPos", NULL, TYPE_VEC3, 0 ); m_pVDecl->AddAttr( "vTex0", NULL, TYPE_VEC2, sizeof( vec3 ) ); } }
//--------------------------------------------------------------------------------------------------------- void Checking::OnRequestResourceList( BohgeEngine::IBaseHandle* h ) { if ( IWebProperty::HS_OK != h->GetStatusCode() ) { DEBUGLOG("Can not connect server!\r\n"); } else { ResourceList Reslist; ASSERT( IWebProperty::JS_FINISH == h->GetState() ); JsonReader webreader; webreader.Parse( static_cast<IPostHandle*>(h)->GetResult() ); JsonEntryInfo webinfo; webreader.Read( webinfo ); m_LocalPath = string( DefaultResource::RESOURCE_PATH ) + webinfo.m_Bundle + PLANTFORMDELIMITER; string infopath = m_LocalPath + DefaultResource::LITE_INFO_FILE; if ( webinfo.m_isValid ) { IFile* file = IIOSystem::Instance()->FileFactory( infopath ); file->OpenFile( IFile::AT_READ ); if ( !file->isOpen() )//如果没打卡,则从新下载相应资源 { _AllResource( Reslist, webinfo ); } else//如果打开了则对比一下 { eastl::string locallif; locallif.resize( file->GetSize() ); file->ReadFile( &locallif[0], file->GetSize() ); file->CloseFile(); IIOSystem::Instance()->RecycleBin( file ); JsonReader loacalreader; if( loacalreader.Parse( locallif ) )//可能本地json被损坏导致无法读取 { loacalreader.Read( m_LocalResourceList ); _CompareResource( Reslist, webinfo ); } else { DEBUGLOG("Resource list is invaild"); _AllResource( Reslist, webinfo ); } } } else { ASSERT( false && "Application's version is not valiable!\n" ); } if ( !Reslist.empty() ) { DEBUGLOG("Need download resource\n"); m_pJsonWriter->Open( DefaultResource::APP_VERSION_INT, infopath ); _DownloadResources( Reslist ); } else { m_pMutex->Lock(); m_isFinish = true; m_ReleaseList.push_back( h ); m_pMutex->Unlock(); } } }
void ScanFileRecursively(const std::string dir, BlockFS* pFS, std::map<std::string, MD5Index>& mapEntries, std::vector<std::string>& add) { WIN32_FIND_DATAA finddata; std::string searchstring = dir + "/*"; HANDLE hfine = FindFirstFileA(searchstring.c_str(), &finddata); if (hfine != INVALID_HANDLE_VALUE) { do { if (finddata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { if(strcmp(finddata.cFileName, ".svn")!=0&& strcmp(finddata.cFileName, ".")!=0&& strcmp(finddata.cFileName, "..")!=0) ScanFileRecursively(dir + "/" + finddata.cFileName, pFS, mapEntries, add); continue; } std::string name = dir; name += "/"; name += finddata.cFileName; std::transform(name.begin(), name.end(), name.begin(), tolower); std::map<std::string, MD5Index>::iterator it = mapEntries.find(name); if(it != mapEntries.end()) { IFile* pTemp = OpenDiskFile(name.c_str(), IFile::O_ReadOnly); offset_type length = pTemp->GetSize(); if(it->second.size==length) { char* buff = new char[(size_t)length.offset]; pTemp->Read(buff, length); std::transform(name.begin(), name.end(), name.begin(), tolower); printf("checking %s, %d bytes\n", name.c_str(), length); unsigned char ucCheckSum[16]; GetMD5CheckSum((unsigned char*)buff, (unsigned)length.offset, ucCheckSum); if(!memcmp(ucCheckSum, it->second.md5, sizeof(ucCheckSum))) { printf("file %s, ignored update\n", it->first.c_str()); mapEntries.erase(it); } else { printf("file %s, need update\n", it->first.c_str()); add.push_back(it->first); } //pFS->AddFile(name.c_str(), buff, length, length > pFS->GetBlockDataSize()); delete[]buff; } else { printf("file %s, with different size %d, original %d", name.c_str(), length, it->second.size); add.push_back(name); } delete pTemp; } else { printf("file %s will be added\n",name.c_str()); add.push_back(name); } } while (FindNextFileA(hfine, &finddata)); } FindClose(hfine); }