engine: common: refactor variable declarations

This commit is contained in:
Alibek Omarov
2026-05-20 17:51:18 +05:00
committed by a1batross
parent e8845fc404
commit c6c1163aa0
44 changed files with 1346 additions and 2203 deletions

View File

@@ -67,9 +67,8 @@ static inline void HPAK_ResourceFromCompat( resource_t *dest, dresource_t *src )
static void HPAK_AddToQueue( const char *name, resource_t *pResource, void *data, file_t *f )
{
hash_pack_queue_t *p;
hash_pack_queue_t *p = Z_Malloc( sizeof( hash_pack_queue_t ));
p = Z_Malloc( sizeof( hash_pack_queue_t ));
p->name = copystring( name );
p->resource = *pResource;
p->size = pResource->nDownloadSize;
@@ -85,9 +84,7 @@ static void HPAK_AddToQueue( const char *name, resource_t *pResource, void *data
void HPAK_FlushHostQueue( void )
{
hash_pack_queue_t *p;
for( p = gp_hpak_queue; p != NULL; p = gp_hpak_queue )
for( hash_pack_queue_t *p = gp_hpak_queue; p != NULL; p = gp_hpak_queue )
{
gp_hpak_queue = p->next;
HPAK_AddLump( false, p->name, &p->resource, p->data, NULL );
@@ -100,10 +97,8 @@ void HPAK_FlushHostQueue( void )
static void HPAK_CreatePak( const char *filename, resource_t *pResource, byte *pData, file_t *fin )
{
int filelocation;
string pakname;
byte md5[16];
file_t *fout;
MD5Context_t ctx = { 0 };
if( COM_StringEmptyOrNULL( filename ))
@@ -117,7 +112,7 @@ static void HPAK_CreatePak( const char *filename, resource_t *pResource, byte *p
Con_Printf( "creating HPAK %s.\n", pakname );
fout = FS_Open( pakname, "wb", true );
file_t *fout = FS_Open( pakname, "wb", true );
if( !fout )
{
Con_DPrintf( S_ERROR "%s: can't write %s.\n", __func__, pakname );
@@ -129,11 +124,9 @@ static void HPAK_CreatePak( const char *filename, resource_t *pResource, byte *p
if( pData == NULL )
{
byte *temp;
int filelocation = FS_Tell( fin );
byte *temp = Z_Malloc( pResource->nDownloadSize );
// there are better ways
filelocation = FS_Tell( fin );
temp = Z_Malloc( pResource->nDownloadSize );
FS_Read( fin, temp, pResource->nDownloadSize );
FS_Seek( fin, filelocation, SEEK_SET );
MD5Update( &ctx, temp, pResource->nDownloadSize );
@@ -173,7 +166,7 @@ static void HPAK_CreatePak( const char *filename, resource_t *pResource, byte *p
FS_Write( fout, pData, hash_pack_info.entries[0].disksize );
}
filelocation = FS_Tell( fout );
int filelocation = FS_Tell( fout );
FS_Write( fout, &hash_pack_info.count, sizeof( hash_pack_info.count ));
FS_Write( fout, &hash_pack_info.entries[0], sizeof( hpak_lump_t ));
@@ -189,9 +182,7 @@ static void HPAK_CreatePak( const char *filename, resource_t *pResource, byte *p
static qboolean HPAK_FindResource( hpak_info_t *hpk, byte *hash, resource_t *pResource )
{
int i;
for( i = 0; i < hpk->count; i++ )
for( int i = 0; i < hpk->count; i++ )
{
if( !memcmp( hpk->entries[i].resource.rgucMD5_hash, hash, 16 ))
{
@@ -206,12 +197,9 @@ static qboolean HPAK_FindResource( hpak_info_t *hpk, byte *hash, resource_t *pRe
void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource, byte *pData, file_t *pFile )
{
int i, j, position, length;
hpak_lump_t *pCurrentEntry = NULL;
string srcname, dstname;
hpak_info_t srcpak, dstpak;
file_t *file_src;
file_t *file_dst;
byte md5[16];
MD5Context_t ctx = { 0 };
@@ -229,11 +217,9 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
if( !pData )
{
byte *temp;
int position = FS_Tell( pFile );
byte *temp = Z_Malloc( pResource->nDownloadSize );
// there are better ways
position = FS_Tell( pFile );
temp = Z_Malloc( pResource->nDownloadSize );
FS_Read( pFile, temp, pResource->nDownloadSize );
FS_Seek( pFile, position, SEEK_SET );
MD5Update( &ctx, temp, pResource->nDownloadSize );
@@ -261,7 +247,7 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
Q_strncpy( srcname, name, sizeof( srcname ));
COM_ReplaceExtension( srcname, ".hpk", sizeof( srcname ));
file_src = FS_Open( srcname, "rb", true );
file_t *file_src = FS_Open( srcname, "rb", true );
if( !file_src )
{
@@ -273,7 +259,7 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
Q_strncpy( dstname, srcname, sizeof( dstname ));
COM_ReplaceExtension( dstname, ".hp2", sizeof( dstname ));
file_dst = FS_Open( dstname, "wb", true );
file_t *file_dst = FS_Open( dstname, "wb", true );
if( !file_dst )
{
@@ -294,7 +280,7 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
return;
}
length = FS_FileLength( file_src );
int length = FS_FileLength( file_src );
FS_Seek( file_src, 0, SEEK_SET ); // rewind to start of file
FS_FileCopy( file_dst, file_src, length );
@@ -329,13 +315,13 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
memcpy( dstpak.entries, srcpak.entries, sizeof( hpak_lump_t ) * srcpak.count );
// check is there are entry with same hash
for( i = 0; i < srcpak.count; i++ )
for( int i = 0; i < srcpak.count; i++ )
{
if( memcmp( md5, srcpak.entries[i].resource.rgucMD5_hash, 16 ) == 0 )
{
pCurrentEntry = &dstpak.entries[i];
for( j = i; j < srcpak.count; j++ )
for( int j = i; j < srcpak.count; j++ )
dstpak.entries[j + 1] = srcpak.entries[j];
}
}
@@ -357,7 +343,7 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
hash_pack_header.infotableofs = FS_Tell( file_dst );
FS_Write( file_dst, &dstpak.count, sizeof( dstpak.count ));
for( i = 0; i < dstpak.count; i++ )
for( int i = 0; i < dstpak.count; i++ )
{
FS_Write( file_dst, &dstpak.entries[i], sizeof( hpak_lump_t ));
}
@@ -380,14 +366,9 @@ void HPAK_AddLump( qboolean bUseQueue, const char *name, resource_t *pResource,
static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean delete )
{
file_t *f;
hpak_lump_t *dataDir;
hpak_header_t hdr;
byte *dataPak;
int i, num_lumps;
MD5Context_t MD5_Hash;
string pakname;
dresource_t *pRes;
byte md5[16];
if( quiet ) HPAK_FlushHostQueue();
@@ -399,7 +380,7 @@ static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean de
Q_strncpy( pakname, filename, sizeof( pakname ));
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f )
{
if( !quiet )
@@ -419,6 +400,8 @@ static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean de
}
FS_Seek( f, hdr.infotableofs, SEEK_SET );
int num_lumps;
FS_Read( f, &num_lumps, sizeof( num_lumps ));
if( num_lumps < 1 || num_lumps > HPAK_MAX_ENTRIES )
@@ -431,12 +414,12 @@ static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean de
if( !quiet ) Con_Printf( "# of Entries: %i\n", num_lumps );
dataDir = Z_Malloc( sizeof( hpak_lump_t ) * num_lumps );
hpak_lump_t *dataDir = Z_Malloc( sizeof( hpak_lump_t ) * num_lumps );
FS_Read( f, dataDir, sizeof( hpak_lump_t ) * num_lumps );
if( !quiet ) Con_Printf( "# Type Size FileName : MD5 Hash\n" );
for( i = 0; i < num_lumps; i++ )
for( int i = 0; i < num_lumps; i++ )
{
if( dataDir[i].disksize < HPAK_ENTRY_MIN_SIZE || dataDir[i].disksize > HPAK_ENTRY_MAX_SIZE )
{
@@ -448,7 +431,7 @@ static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean de
return false;
}
dataPak = Z_Malloc( dataDir[i].disksize );
byte *dataPak = Z_Malloc( dataDir[i].disksize );
FS_Seek( f, dataDir[i].filepos, SEEK_SET );
FS_Read( f, dataPak, dataDir[i].disksize );
@@ -457,7 +440,7 @@ static qboolean HPAK_Validate( const char *filename, qboolean quiet, qboolean de
MD5Update( &MD5_Hash, dataPak, dataDir[i].disksize );
MD5Final( md5, &MD5_Hash );
pRes = &dataDir[i].resource;
dresource_t *pRes = &dataDir[i].resource;
if( !quiet )
{
@@ -508,9 +491,8 @@ void HPAK_CheckIntegrity( const char *filename )
void HPAK_CheckSize( const char *filename )
{
string pakname;
int maxsize;
int maxsize = hpk_maxsize.value;
maxsize = hpk_maxsize.value;
if( maxsize <= 0 ) return;
if( COM_StringEmptyOrNULL( filename ) )
@@ -532,14 +514,11 @@ qboolean HPAK_ResourceForHash( const char *filename, byte *hash, resource_t *pRe
hpak_info_t directory;
hpak_header_t header;
string pakname;
qboolean bFound;
file_t *f;
hash_pack_queue_t *p;
if( COM_StringEmptyOrNULL( filename ))
return false;
for( p = gp_hpak_queue; p != NULL; p = p->next )
for( hash_pack_queue_t *p = gp_hpak_queue; p != NULL; p = p->next )
{
if( !Q_stricmp( p->name, filename ) && !memcmp( p->resource.rgucMD5_hash, hash, 16 ))
{
@@ -552,7 +531,7 @@ qboolean HPAK_ResourceForHash( const char *filename, byte *hash, resource_t *pRe
Q_strncpy( pakname, filename, sizeof( pakname ));
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f ) return false;
FS_Read( f, &header, sizeof( header ));
@@ -580,7 +559,7 @@ qboolean HPAK_ResourceForHash( const char *filename, byte *hash, resource_t *pRe
directory.entries = Z_Malloc( sizeof( hpak_lump_t ) * directory.count );
FS_Read( f, directory.entries, sizeof( hpak_lump_t ) * directory.count );
bFound = HPAK_FindResource( &directory, hash, pResource );
qboolean bFound = HPAK_FindResource( &directory, hash, pResource );
Mem_Free( directory.entries );
FS_Close( f );
@@ -592,7 +571,6 @@ static qboolean HPAK_ResourceForIndex( const char *filename, int index, resource
hpak_header_t header;
hpak_info_t directory;
string pakname;
file_t *f;
if( COM_StringEmptyOrNULL( filename ) )
return false;
@@ -600,7 +578,7 @@ static qboolean HPAK_ResourceForIndex( const char *filename, int index, resource
Q_strncpy( pakname, filename, sizeof( pakname ));
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f )
{
Con_DPrintf( S_ERROR "couldn't open %s.\n", pakname );
@@ -650,14 +628,9 @@ static qboolean HPAK_ResourceForIndex( const char *filename, int index, resource
qboolean HPAK_GetDataPointer( const char *filename, resource_t *pResource, byte **buffer, int *bufsize )
{
byte *tmpbuf;
string pakname;
hpak_header_t header;
hpak_info_t directory;
hpak_lump_t *entry;
hash_pack_queue_t *p;
file_t *f;
int i;
if( COM_StringEmptyOrNULL( filename ))
return false;
@@ -665,13 +638,13 @@ qboolean HPAK_GetDataPointer( const char *filename, resource_t *pResource, byte
if( buffer ) *buffer = NULL;
if( bufsize ) *bufsize = 0;
for( p = gp_hpak_queue; p != NULL; p = p->next )
for( hash_pack_queue_t *p = gp_hpak_queue; p != NULL; p = p->next )
{
if( !Q_stricmp( p->name, filename ) && !memcmp( p->resource.rgucMD5_hash, pResource->rgucMD5_hash, 16 ))
{
if( buffer )
{
tmpbuf = Z_Malloc( p->size );
byte *tmpbuf = Z_Malloc( p->size );
memcpy( tmpbuf, p->data, p->size );
*buffer = tmpbuf;
}
@@ -686,7 +659,7 @@ qboolean HPAK_GetDataPointer( const char *filename, resource_t *pResource, byte
Q_strncpy( pakname, filename, sizeof( pakname ));
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f ) return false;
FS_Read( f, &header, sizeof( header ));
@@ -718,9 +691,9 @@ qboolean HPAK_GetDataPointer( const char *filename, resource_t *pResource, byte
directory.entries = Z_Malloc( sizeof( hpak_lump_t ) * directory.count );
FS_Read( f, directory.entries, sizeof( hpak_lump_t ) * directory.count );
for( i = 0; i < directory.count; i++ )
for( int i = 0; i < directory.count; i++ )
{
entry = &directory.entries[i];
hpak_lump_t *entry = &directory.entries[i];
if( entry->filepos > 0 &&
entry->disksize > 0 &&
@@ -730,7 +703,7 @@ qboolean HPAK_GetDataPointer( const char *filename, resource_t *pResource, byte
if( buffer )
{
tmpbuf = Z_Malloc( entry->disksize );
byte *tmpbuf = Z_Malloc( entry->disksize );
FS_Read( f, tmpbuf, entry->disksize );
*buffer = tmpbuf;
}
@@ -755,11 +728,8 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
{
string read_path;
string save_path;
file_t *file_src;
file_t *file_dst;
hpak_info_t hpak_read;
hpak_info_t hpak_save;
int i, j;
if( COM_StringEmptyOrNULL( name ) || !pResource )
return;
@@ -769,7 +739,7 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
Q_strncpy( read_path, name, sizeof( read_path ));
COM_ReplaceExtension( read_path, ".hpk", sizeof( read_path ));
file_src = FS_Open( read_path, "rb", true );
file_t *file_src = FS_Open( read_path, "rb", true );
if( !file_src )
{
Con_DPrintf( S_ERROR "%s couldn't open.\n", read_path );
@@ -778,7 +748,7 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
Q_strncpy( save_path, read_path, sizeof( save_path ));
COM_ReplaceExtension( save_path, ".hp2", sizeof( save_path ));
file_dst = FS_Open( save_path, "wb", true );
file_t *file_dst = FS_Open( save_path, "wb", true );
if( !file_dst )
{
@@ -845,7 +815,7 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
Con_Printf( "Removing %s from HPAK %s.\n", pResource->szFileName, read_path );
// If there's a collision, we've just corrupted this hpak.
for( i = 0, j = 0; i < hpak_read.count; i++ )
for( int i = 0, j = 0; i < hpak_read.count; i++ )
{
if( !memcmp( hpak_read.entries[i].resource.rgucMD5_hash, pResource->rgucMD5_hash, 16 ))
continue;
@@ -860,7 +830,7 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
hash_pack_header.infotableofs = FS_Tell( file_dst );
FS_Write( file_dst, &hpak_save.count, sizeof( hpak_save.count ));
for( i = 0; i < hpak_save.count; i++ )
for( int i = 0; i < hpak_save.count; i++ )
FS_Write( file_dst, &hpak_save.entries[i], sizeof( hpak_lump_t ));
FS_Seek( file_dst, 0, SEEK_SET );
@@ -877,15 +847,10 @@ void HPAK_RemoveLump( const char *name, resource_t *pResource )
static void HPAK_List_f( void )
{
int nCurrent;
hpak_header_t header;
hpak_info_t directory;
hpak_lump_t *entry;
string lumpname;
string pakname;
const char *type;
const char *size;
file_t *f;
if( Cmd_Argc() != 2 )
{
@@ -899,7 +864,7 @@ static void HPAK_List_f( void )
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
Con_Printf( "Contents for %s.\n", pakname );
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f )
{
Con_DPrintf( S_ERROR "couldn't open %s.\n", pakname );
@@ -938,12 +903,13 @@ static void HPAK_List_f( void )
directory.entries = Z_Malloc( directory.count * sizeof( hpak_lump_t ));
FS_Read( f, directory.entries, directory.count * sizeof( hpak_lump_t ));
for( nCurrent = 0; nCurrent < directory.count; nCurrent++ )
for( int nCurrent = 0; nCurrent < directory.count; nCurrent++ )
{
entry = &directory.entries[nCurrent];
hpak_lump_t *entry = &directory.entries[nCurrent];
const char *type = COM_ResourceTypeFromIndex( entry->resource.type );
const char *size = Q_memprint( entry->resource.nDownloadSize );
COM_FileBase( entry->resource.szFileName, lumpname, sizeof( lumpname ));
type = COM_ResourceTypeFromIndex( entry->resource.type );
size = Q_memprint( entry->resource.nDownloadSize );
Con_Printf( "%i: %10s %s %s\n : %s\n", nCurrent + 1, type, size, lumpname, MD5_Print( entry->resource.rgucMD5_hash ));
}
@@ -955,19 +921,12 @@ static void HPAK_List_f( void )
static void HPAK_Extract_f( void )
{
int nCurrent;
hpak_header_t header;
hpak_info_t directory;
hpak_lump_t *entry;
string lumpname;
string pakname;
string szFileOut;
int nIndex;
byte *pData;
int nDataSize;
const char *type;
const char *size;
file_t *f;
if( Cmd_Argc() != 3 )
{
@@ -990,7 +949,7 @@ static void HPAK_Extract_f( void )
COM_ReplaceExtension( pakname, ".hpk", sizeof( pakname ));
Con_Printf( "Contents for %s.\n", pakname );
f = FS_Open( pakname, "rb", true );
file_t *f = FS_Open( pakname, "rb", true );
if( !f )
{
Con_DPrintf( S_ERROR "couldn't open %s.\n", pakname );
@@ -1029,16 +988,17 @@ static void HPAK_Extract_f( void )
directory.entries = Z_Malloc( directory.count * sizeof( hpak_lump_t ));
FS_Read( f, directory.entries, directory.count * sizeof( hpak_lump_t ));
for( nCurrent = 0; nCurrent < directory.count; nCurrent++ )
for( int nCurrent = 0; nCurrent < directory.count; nCurrent++ )
{
entry = &directory.entries[nCurrent];
hpak_lump_t *entry = &directory.entries[nCurrent];
if( nIndex != -1 && nIndex != nCurrent )
continue;
const char *type = COM_ResourceTypeFromIndex( entry->resource.type );
const char *size = Q_memprint( entry->resource.nDownloadSize );
COM_FileBase( entry->resource.szFileName, lumpname, sizeof( lumpname ) );
type = COM_ResourceTypeFromIndex( entry->resource.type );
size = Q_memprint( entry->resource.nDownloadSize );
Con_Printf( "Extracting %i: %10s %s %s\n", nCurrent + 1, type, size, lumpname );
@@ -1048,8 +1008,9 @@ static void HPAK_Extract_f( void )
continue;
}
nDataSize = entry->disksize;
pData = Z_Malloc( nDataSize + 1 );
int nDataSize = entry->disksize;
byte *pData = Z_Malloc( nDataSize + 1 );
FS_Seek( f, entry->filepos, SEEK_SET );
FS_Read( f, pData, nDataSize );