Hey,
I was making custom pak file reader (some kind of container), and i have some wrong stuff.
When i write my data into this pak file - it writes fine, but when i'm trying to read it, it reads wrong.
Header stuff reads right, something wrong with file reading
See screenshot, it returns wrong filelen and pos also not full filename
there's code, any idea?
Thanks
I was making custom pak file reader (some kind of container), and i have some wrong stuff.
When i write my data into this pak file - it writes fine, but when i'm trying to read it, it reads wrong.
Header stuff reads right, something wrong with file reading
See screenshot, it returns wrong filelen and pos also not full filename
there's code, any idea?
Code:
typedef struct
{
char id[4];
long dirofs;
long dirlen;
} dpackheader_t;
typedef struct
{
char name[128];
long filepos;
long filelen;
} packfile_t;
void FS_InitFiles ( void )
{
datafile_t pak = datafile_t();
pak.filename = "test.pak";
pak.paknum = 1;
ifstream inputFile( pak.filename, ios::in | ios::binary );
inputFile.seekg( 0, ios::end );
long fileSize = inputFile.tellg();
inputFile.seekg( 0, ios::beg );
byte *pBuffer = new byte[fileSize];
inputFile.read( (char*) pBuffer, fileSize );
//inputFile.close();
//pBuffer[fileSize]='\0';
const byte *pPtr = pBuffer;
dpackheader_t *pHeader = ( dpackheader_t* )pPtr;
pBuffer[sizeof(pHeader->id)]='\0';
pPtr += sizeof( dpackheader_t );
pak.header = dpackheader_t();
// pak.header.id = pHeader->id;//br.ReadChars(4);
strcpy(pak.header.id, pHeader->id);
pak.header.dirofs = pHeader->dirofs; //br.ReadInt32();
pak.header.dirlen = pHeader->dirlen; //br.ReadInt32();
if ( strncmp( pHeader->id, "PACK", 4 ) != 0 )
{
Com_Printf("It isn't .PAK");
return;
}
Com_Printf("id: %s dirofs: %i dirlen: %i\n", pak.header.id, pak.header.dirofs, pak.header.dirlen);
// ================== //
pak.files = new packfile_t[pak.header.dirlen / 64];
inputFile.seekg(pak.header.dirofs, ios::beg);
pBuffer = new byte[pak.header.dirofs];
inputFile.read( (char*) pBuffer, fileSize );
byte *pPtrs = pBuffer;
//pBuffer[sizeof(pHeaders->name)]='\0';
pPtrs += sizeof( packfile_t );
// temp log
fstream myfile;
myfile.open ("t.txt");
//pPtrs -= pHeaders->filelen;
// NOTE: packfile_t is s*, use in sizeof
int k;
for(k = 0; k < pak.header.dirlen / 64; k ++)
{
packfile_t *s = (packfile_t*)pPtrs;
pPtrs += (pak.header.dirlen/64); //sizeof( packfile_t );
pPtrs -= (sizeof(s->filelen) - strlen ( s->name ) - sizeof(s->filepos));
pPtrs += sizeof(s);
pBuffer[sizeof(s->name)]='\0';
Com_Printf("File #%i - filepos: %i filelen - %i filename: %s\n", k, s->filepos, s->filelen, s->name);
pPtrs += sizeof( s );
myfile << str("%s\n", s->name);
}
delete[] pBuffer;
myfile.close();
inputFile.close();
}
Thanks