the asumptions were of course, incorrect
[convexer.git] / cxr / cxr_valve_bin.h
index 8d0bf9c33c6702daa9ee5c5c7f0a91813c3b4f9e..51c492d1158ad928e983fa73fa2016b10fada820 100644 (file)
@@ -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; j<model->studiohdr->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( "<CR>" );
+      else if( *y == '\n' )
+         cxr_log( "<LF>\n" );
+      else
+         cxr_log( "%c", *y);
+      y ++;
+   }
+   cxr_log( "<EOF>'\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 */