From ca812c181ad8c845d73bd28de8dd187422c544ab Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 2 May 2022 14:50:19 +0100 Subject: [PATCH] the asumptions were of course, incorrect --- build.sh | 2 +- cxr/cxr_valve_bin.h | 64 +++++++++++++++++++++++++++++++++++++++------ cxr/test.c | 2 +- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/build.sh b/build.sh index c5a1a51..a20dc3e 100755 --- a/build.sh +++ b/build.sh @@ -96,7 +96,7 @@ compile $C -O3 \ -lm # This is for testing with asan on linux -compile $C -ggdb -O1 -Wall \ +compile $C -ggdb -O0 -Wall \ -Wno-unused-variable -Wno-unused-function $asan -Werror=vla \ -DCXR_SO -DCXR_DEBUG -DCXR_VALVE_MAP_FILE \ cxr/test.c \ diff --git a/cxr/cxr_valve_bin.h b/cxr/cxr_valve_bin.h index 8d0bf9c..51c492d 100644 --- a/cxr/cxr_valve_bin.h +++ b/cxr/cxr_valve_bin.h @@ -153,6 +153,9 @@ static VPKDirectoryEntry *vpk_find( const char *asset ) if( !fs->directory_tree ) return NULL; + + if( !asset ) + return NULL; char wbuf[ 512 ]; strcpy( wbuf, asset ); @@ -904,11 +907,13 @@ static char *cxr_vpk_read( VPKDirectoryEntry *entry, int stringbuffer ) if( entry->ArchiveIndex == 0x7fff ) { snprintf( pak, 1023, "%scsgo/pak01_dir.vpk", fs->exedir ); + cxr_log( "locate %s\n", pak ); } else { snprintf( pak, 1023, "%scsgo/pak01_%03hu.vpk", fs->exedir, entry->ArchiveIndex ); + cxr_log( "locate %s\n", pak ); } fs->current_archive = fopen( pak, "rb" ); @@ -927,7 +932,9 @@ static char *cxr_vpk_read( VPKDirectoryEntry *entry, int stringbuffer ) * File is stored in the index, after the tree */ if( entry->ArchiveIndex == 0x7fff ) + { offset += fs->vpk.TreeSize + sizeof(VPKHeader); + } /* * Entire file is stored in the preload bytes; @@ -947,13 +954,28 @@ static char *cxr_vpk_read( VPKDirectoryEntry *entry, int stringbuffer ) fseek( fs->current_archive, offset, SEEK_SET ); size_t alloc_size = stringbuffer? length+1: length; - char *filebuf = malloc( alloc_size ); + char *filebuf = malloc( alloc_size ), + *fullfile = filebuf; if( stringbuffer ) - filebuf[length] = 0x00; + fullfile[length] = 0x00; + + if( entry->PreloadBytes ) + { + memcpy( filebuf, ((char *)entry) + sizeof(VPKDirectoryEntry), + entry->PreloadBytes ); + + filebuf += entry->PreloadBytes; + length -= entry->PreloadBytes; + + /* TODO, this can sometimes come from the same index file. + * very small performance gain can be made */ + } if( fread( filebuf, 1, length, fs->current_archive ) == length ) - return filebuf; + { + return fullfile; + } else { /* Invalid read */ @@ -988,7 +1010,7 @@ CXR_API i32 cxr_fs_find( const char *path, fs_locator *locator ) { char **sp = cxr_ab_ptr( &fs->searchpaths, i ); - snprintf( locator->path, 1023, "%s%s", *sp, path ); + snprintf( locator->path, 1024, "%s%s", *sp, path ); if( cxr_file_exists( locator->path ) ) return 1; @@ -1426,9 +1448,8 @@ typedef struct int sznameindex; int flags; int used; - - /* There is some extra unused stuff that was here... - * Luckily since byte offsets are used, structure size doesn't matter */ + int unused1; + int garbage[12]; /* contains some studiomdl pointers */ } mstudiotexture_t; @@ -1590,12 +1611,15 @@ CXR_API valve_model *valve_load_model( const char *relpath ) const char *name = mstudiotexture_pszName( tex ); model->materials[i] = NULL; + + cxr_log( "search: %s\n", name ); for( int j=0; jstudiohdr->numcdtextures; j++ ) { char *cdpath = studiohdr_pCdtexture( model->studiohdr, j ); snprintf( material_path, 1023, "materials/%s%s.vmt", cdpath, name ); cxr_unixpath( material_path ); + cxr_lowercase( material_path ); if( cxr_fs_find( material_path, &locator )) { @@ -1603,6 +1627,7 @@ CXR_API valve_model *valve_load_model( const char *relpath ) break; } } + } u32 i_index = 0, @@ -1729,12 +1754,33 @@ static char *valve_texture_path( const char *path ) return buf; } +static void cxr_str_debug( const char *str ) +{ + cxr_log( "\n=== string debug ( %p ) === \n'", str ); + const char *y = str; + + while(*y) + { + if( *y == '\r' ) + cxr_log( "" ); + else if( *y == '\n' ) + cxr_log( "\n" ); + else + cxr_log( "%c", *y); + y ++; + } + cxr_log( "'\n" ); +} + CXR_API valve_material *valve_load_material( const char *path ) { char *vmt = cxr_fs_get( path, 1 ); if( vmt ) { +#if 0 + cxr_str_debug( vmt ); +#endif valve_material *material = malloc( sizeof(valve_material) ); vdf_node *vmt_root = vdf_from_buffer( vmt ); @@ -1742,8 +1788,9 @@ CXR_API valve_material *valve_load_material( const char *path ) { cxr_log( "Error: vmt has no nodes\n" ); free( vmt ); + free( material ); vdf_free_r( vmt_root ); - return 0; + return NULL; } vdf_node **body = cxr_ab_ptr( &vmt_root->abnodes, 0 ); @@ -1777,6 +1824,7 @@ CXR_API void valve_free_material( valve_material *material ) { free( material->basetexture ); free( material->bumpmap ); + free( material ); } #endif /* CXR_VALVE_BIN_H */ diff --git a/cxr/test.c b/cxr/test.c index 7d82c1e..c4e9db0 100644 --- a/cxr/test.c +++ b/cxr/test.c @@ -7,7 +7,7 @@ int main(int arc, const char *argv[]) "/home/harry/.steam/steam/steamapps/common/" "Counter-Strike Global Offensive/csgo/gameinfo.txt" ) ) { - valve_model *model_test = valve_load_model( "models/pigeon.mdl" ); + valve_model *model_test = valve_load_model("models/props_foliage/urban_tree_giant01.mdl"); if( model_test ) { cxr_log( "Mesh loaded\n" ); -- 2.25.1