Skip to content

Commit

Permalink
engine: mod_bmodel: rewrite .lit file loader using normal stream io f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
a1batross committed Jan 8, 2025
1 parent 24460a7 commit 34ceb57
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions engine/common/mod_bmodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,8 @@ static qboolean Mod_LoadLitfile( model_t *mod, const char *ext, size_t expected_
char modelname[64], path[64];
int iCompare;
fs_offset_t datasize;
byte *in;
file_t *f;
uint hdr[2];

COM_FileBase( mod->name, modelname, sizeof( modelname ));
Q_snprintf( path, sizeof( path ), "maps/%s.%s", modelname, ext );
Expand All @@ -1791,49 +1792,52 @@ static qboolean Mod_LoadLitfile( model_t *mod, const char *ext, size_t expected_
if( iCompare < 0 ) // this may happens if level-designer used -onlyents key for hlcsg
Con_Printf( S_WARN "%s probably is out of date\n", path );

in = FS_LoadFile( path, &datasize, false );
f = FS_Open( path, "rb", false );

if( !in )
if( !f )
{
Con_Printf( S_ERROR "couldn't load %s\n", path );
return false;
}

if( datasize <= 8 ) // header + version
datasize = FS_FileLength( f );

// skip header bytes
datasize -= 8;

if( datasize != expected_size )
{
Con_Printf( S_ERROR "%s is too short\n", path );
Con_Printf( S_ERROR "%s has mismatched size (%li should be %zu)\n", path, (long)datasize, expected_size );
goto cleanup_and_error;
}

if( LittleLong( ((uint *)in)[0] ) != IDDELUXEMAPHEADER )
if( FS_Read( f, hdr, sizeof( hdr )) != sizeof( hdr ))
{
Con_Printf( S_ERROR "%s is corrupted\n", path );
Con_Printf( S_ERROR "failed reading header from %s\n", path );
goto cleanup_and_error;
}

if( LittleLong( ((uint *)in)[1] ) != DELUXEMAP_VERSION )
if( LittleLong( hdr[0] ) != IDDELUXEMAPHEADER )
{
Con_Printf( S_ERROR "has %s mismatched version (%u should be %u)\n", path, LittleLong( ((uint *)in)[1] ), DELUXEMAP_VERSION );
Con_Printf( S_ERROR "%s is corrupted\n", path );
goto cleanup_and_error;
}

// skip header bytes
datasize -= 8;

if( datasize != expected_size )
if( LittleLong( hdr[1] ) != DELUXEMAP_VERSION )
{
Con_Printf( S_ERROR "%s has mismatched size (%li should be %zu)\n", path, (long)datasize, expected_size );
Con_Printf( S_ERROR "has %s mismatched version (%u should be %u)\n", path, LittleLong( hdr[1] ), DELUXEMAP_VERSION );
goto cleanup_and_error;
}

*out = Mem_Malloc( mod->mempool, datasize );
memcpy( *out, in + 8, datasize );
*outsize = datasize;
Mem_Free( in );

FS_Read( f, *out, datasize );
FS_Close( f );
return true;

cleanup_and_error:
Mem_Free( in );
FS_Close( f );
return false;
}

Expand Down

0 comments on commit 34ceb57

Please sign in to comment.