engine: move studio model truncation into separate function, disable it by default

This commit is contained in:
Alibek Omarov 2024-06-10 13:31:06 +03:00
parent 87840ce91d
commit f047749e61

View file

@ -841,6 +841,22 @@ static studiohdr_t *R_StudioLoadHeader( model_t *mod, const void *buffer )
return (studiohdr_t *)buffer; return (studiohdr_t *)buffer;
} }
static studiohdr_t *Mod_MaybeTruncateStudioTextureData( model_t *mod )
{
#if XASH_LOW_MEMORY
studiohdr_t *phdr = (studiohdr_t *)mod->cache.data;
mod->cache.data = Mem_Realloc( mod->mempool, mod->cache.data, phdr->texturedataindex );
phdr = (studiohdr_t *)mod->cache.data; // get the new pointer on studiohdr
phdr->length = phdr->texturedataindex; // update model size
return phdr;
#else
// NOTE: some mods potentially might require full studio model data
return (studiohdr_t *)mod->cache.data;
#endif
}
/* /*
================= =================
Mod_LoadStudioModel Mod_LoadStudioModel
@ -860,28 +876,18 @@ void Mod_LoadStudioModel( model_t *mod, const void *buffer, qboolean *loaded )
phdr = R_StudioLoadHeader( mod, buffer ); phdr = R_StudioLoadHeader( mod, buffer );
if( !phdr ) return; // bad model if( !phdr ) return; // bad model
if( !Host_IsDedicated() ) if( !Host_IsDedicated( ) && phdr->numtextures == 0 )
{
if( phdr->numtextures == 0 )
{ {
studiohdr_t *thdr; studiohdr_t *thdr;
byte *in, *out; void *buffer2;
void *buffer2 = NULL;
size_t size1, size2;
buffer2 = FS_LoadFile( Mod_StudioTexName( mod->name ), NULL, false ); buffer2 = FS_LoadFile( Mod_StudioTexName( mod->name ), NULL, false );
thdr = R_StudioLoadHeader( mod, buffer2 ); thdr = R_StudioLoadHeader( mod, buffer2 );
if( !thdr ) if( thdr != NULL )
{ {
Con_Printf( S_WARN "Mod_LoadStudioModel: %s missing textures file\n", mod->name ); byte *in, *out;
if( buffer2 ) Mem_Free( buffer2 ); size_t size1, size2;
}
else
{
#if !XASH_DEDICATED
ref.dllFuncs.Mod_StudioLoadTextures( mod, thdr );
#endif
// give space for textures and skinrefs // give space for textures and skinrefs
size1 = thdr->numtextures * sizeof( mstudiotexture_t ); size1 = thdr->numtextures * sizeof( mstudiotexture_t );
@ -899,33 +905,28 @@ void Mod_LoadStudioModel( model_t *mod, const void *buffer, qboolean *loaded )
out = (byte *)phdr + phdr->textureindex; out = (byte *)phdr + phdr->textureindex;
memcpy( out, in, size1 + size2 ); // copy textures + skinrefs memcpy( out, in, size1 + size2 ); // copy textures + skinrefs
phdr->length += size1 + size2; phdr->length += size1 + size2;
Mem_Free( buffer2 ); // release T.mdl
} }
else Con_Printf( S_WARN "%s: %s missing textures file\n", __func__, mod->name );
if( buffer2 )
Mem_Free( buffer2 ); // release T.mdl
} }
else else
{ {
// NOTE: don't modify source buffer because it's used for CRC computing // NOTE: don't modify source buffer because it's used for CRC computing
mod->cache.data = Mem_Calloc( mod->mempool, phdr->length ); mod->cache.data = Mem_Calloc( mod->mempool, phdr->length );
memcpy( mod->cache.data, buffer, phdr->length ); memcpy( mod->cache.data, buffer, phdr->length );
phdr = (studiohdr_t *)mod->cache.data; // get the new pointer on studiohdr phdr = mod->cache.data;
}
#if !XASH_DEDICATED #if !XASH_DEDICATED
if( !Host_IsDedicated( ))
ref.dllFuncs.Mod_StudioLoadTextures( mod, phdr ); ref.dllFuncs.Mod_StudioLoadTextures( mod, phdr );
#endif #endif
// NOTE: we wan't keep raw textures in memory. just cutoff model pointer above texture base // NOTE: we may not want to keep raw textures in memory. just cutoff model pointer above texture base
mod->cache.data = Mem_Realloc( mod->mempool, mod->cache.data, phdr->texturedataindex ); phdr = Mod_MaybeTruncateStudioTextureData( mod );
phdr = (studiohdr_t *)mod->cache.data; // get the new pointer on studiohdr
phdr->length = phdr->texturedataindex; // update model size
}
}
else
{
// just copy model into memory
mod->cache.data = Mem_Calloc( mod->mempool, phdr->length );
memcpy( mod->cache.data, buffer, phdr->length );
phdr = mod->cache.data;
}
// setup bounding box // setup bounding box
if( !VectorCompare( vec3_origin, phdr->bbmin )) if( !VectorCompare( vec3_origin, phdr->bbmin ))