Changed: bnp_manager_plugin file handles to CIFile/COFile to use serial system

This commit is contained in:
Krolock 2012-01-11 23:52:18 +01:00
parent 551f71375d
commit 7c6d141108

View file

@ -64,9 +64,8 @@ void BNPFileHandle::releaseInstance()
// *************************************************************************** // ***************************************************************************
bool BNPFileHandle::unpack(const string &dirName, const vector<string>& fileList) bool BNPFileHandle::unpack(const string &dirName, const vector<string>& fileList)
{ {
FILE *bnp = fopen (m_openedBNPFile.c_str(), "rb"); CIFile bnp;
FILE *out; if ( !bnp.open(m_openedBNPFile) )
if (bnp == NULL)
return false; return false;
TPackedFilesList::iterator it_files = m_packedFiles.begin(); TPackedFilesList::iterator it_files = m_packedFiles.begin();
@ -78,27 +77,17 @@ bool BNPFileHandle::unpack(const string &dirName, const vector<string>& fileList
{ {
string filename = dirName + "/" + it_files->m_name; string filename = dirName + "/" + it_files->m_name;
out = fopen (filename.c_str(), "wb"); COFile out;
if (out != NULL) if ( out.open(filename) )
{ {
nlfseek64 (bnp, it_files->m_pos, SEEK_SET); bnp.seek(it_files->m_pos, IStream::begin);
uint8 *ptr = new uint8[it_files->m_size]; uint8 *ptr = new uint8[it_files->m_size];
if (fread (ptr, it_files->m_size, 1, bnp) != 1) bnp.serialBuffer(ptr,it_files->m_size);
{ out.serialBuffer(ptr,it_files->m_size);
nlwarning("%s read error", filename.c_str());
return false;
}
if (fwrite (ptr, it_files->m_size, 1, out) != 1)
{
nlwarning("%s write error", filename.c_str());
return false;
}
fclose (out);
delete [] ptr; delete [] ptr;
} }
} }
} }
fclose (bnp);
return true; return true;
} }
// *************************************************************************** // ***************************************************************************
@ -109,42 +98,32 @@ bool BNPFileHandle::readHeader(const std::string &filePath)
m_openedBNPFile = filePath; m_openedBNPFile = filePath;
FILE *f = fopen (filePath.c_str(), "rb"); CIFile bnp;
if (f == NULL) if ( !bnp.open (filePath) )
{ {
nlwarning("Could not open file!"); nlwarning("Could not open file!");
return false; return false;
} }
nlfseek64 (f, 0, SEEK_END); bnp.seek(0, IStream::end);
uint32 nFileSize=CFile::getFileSize (filePath ); uint32 nFileSize=CFile::getFileSize (filePath );
nlfseek64 (f, nFileSize-sizeof(uint32), SEEK_SET); bnp.seek(nFileSize-sizeof(uint32), IStream::begin);
uint32 nOffsetFromBegining; uint32 nOffsetFromBegining;
if (fread (&nOffsetFromBegining, sizeof(uint32), 1, f) != 1) bnp.serial(nOffsetFromBegining);
{
fclose (f);
return false;
}
#ifdef NL_BIG_ENDIAN #ifdef NL_BIG_ENDIAN
NLMISC_BSWAP32(nOffsetFromBegining); NLMISC_BSWAP32(nOffsetFromBegining);
#endif #endif
if (nlfseek64 (f, nOffsetFromBegining, SEEK_SET) != 0) if ( !bnp.seek (nOffsetFromBegining, IStream::begin) )
{ {
nlwarning("Could not read offset from begining"); nlwarning("Could not read offset from begining");
fclose (f);
return false; return false;
} }
uint32 nNbFile; uint32 nNbFile;
if (fread (&nNbFile, sizeof(uint32), 1, f) != 1) bnp.serial(nNbFile);
{
nlwarning("Could not read number of files!");
fclose (f);
return false;
}
#ifdef NL_BIG_ENDIAN #ifdef NL_BIG_ENDIAN
NLMISC_BSWAP32(nNbFile); NLMISC_BSWAP32(nNbFile);
@ -153,44 +132,28 @@ bool BNPFileHandle::readHeader(const std::string &filePath)
for (uint32 i = 0; i < nNbFile; ++i) for (uint32 i = 0; i < nNbFile; ++i)
{ {
uint8 nStringSize; uint8 nStringSize;
uint32 fileSize;
uint32 filePos;
char sName[256]; char sName[256];
if (fread (&nStringSize, 1, 1, f) != 1) bnp.serial(nStringSize);
{ bnp.serialBuffer( (uint8*)sName, nStringSize);
nlwarning("Error reading packed filename!");
fclose (f);
return false;
}
if (fread (sName, 1, nStringSize, f) != nStringSize)
{
fclose (f);
return false;
}
sName[nStringSize] = 0; sName[nStringSize] = 0;
PackedFile tmpPackedFile; PackedFile tmpPackedFile;
tmpPackedFile.m_name = sName; tmpPackedFile.m_name = sName;
tmpPackedFile.m_path = m_openedBNPFile; tmpPackedFile.m_path = m_openedBNPFile;
if (fread (&tmpPackedFile.m_size, sizeof(uint32), 1, f) != 1)
{ bnp.serial(fileSize);
nlwarning("Error reading packed file size!"); tmpPackedFile.m_size = fileSize;
fclose (f);
return false;
}
#ifdef NL_BIG_ENDIAN #ifdef NL_BIG_ENDIAN
NLMISC_BSWAP32(tmpBNPFile.Size); NLMISC_BSWAP32(tmpBNPFile.Size);
#endif #endif
if (fread (&tmpPackedFile.m_pos, sizeof(uint32), 1, f) != 1) bnp.serial(filePos);
{ tmpPackedFile.m_pos = filePos;
nlwarning("Error reading packed file position!");
fclose (f);
return false;
}
#ifdef NL_BIG_ENDIAN #ifdef NL_BIG_ENDIAN
NLMISC_BSWAP32(tmpBNPFile.Pos); NLMISC_BSWAP32(tmpBNPFile.Pos);
#endif #endif
m_packedFiles.push_back (tmpPackedFile); m_packedFiles.push_back (tmpPackedFile);
} }
fclose (f);
return true; return true;
} }
// *************************************************************************** // ***************************************************************************
@ -211,51 +174,23 @@ void BNPFileHandle::list(TPackedFilesList& FileList)
// *************************************************************************** // ***************************************************************************
bool BNPFileHandle::writeHeader( const std::string &filePath, uint32 offset ) bool BNPFileHandle::writeHeader( const std::string &filePath, uint32 offset )
{ {
FILE *f = fopen (filePath.c_str(), "ab"); COFile bnp;
if (f == NULL) return false; if ( !bnp.open(filePath, true) ) return false;
uint32 nNbFile = (uint32)m_packedFiles.size(); uint32 nNbFile = (uint32)m_packedFiles.size();
if (fwrite (&nNbFile, sizeof(uint32), 1, f) != 1) bnp.serial(nNbFile);
{
fclose(f);
return false;
}
for (uint32 i = 0; i < nNbFile; ++i) for (uint32 i = 0; i < nNbFile; ++i)
{ {
uint8 nStringSize = (uint8)m_packedFiles[i].m_name.size(); uint8 nStringSize = (uint8)m_packedFiles[i].m_name.size();
if (fwrite (&nStringSize, 1, 1, f) != 1) bnp.serial( nStringSize );
{ bnp.serialBuffer( (uint8*)m_packedFiles[i].m_name.c_str(), nStringSize );
fclose(f); bnp.serial(m_packedFiles[i].m_size);
return false; bnp.serial(m_packedFiles[i].m_pos);
} }
if (fwrite (m_packedFiles[i].m_name.c_str(), 1, nStringSize, f) != nStringSize) bnp.serial(offset);
{
fclose(f);
return false;
}
if (fwrite (&m_packedFiles[i].m_size, sizeof(uint32), 1, f) != 1)
{
fclose(f);
return false;
}
if (fwrite (&m_packedFiles[i].m_pos, sizeof(uint32), 1, f) != 1)
{
fclose(f);
return false;
}
}
if (fwrite (&offset, sizeof(uint32), 1, f) != 1)
{
fclose(f);
return false;
}
fclose (f);
return true; return true;
} }
// *************************************************************************** // ***************************************************************************