fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / vfilesys.h
index 2f33f8312e09809b118f1055197ab0fa784d5cf4..fc4cec805886c749f37df2cdadb29f5677eeaae3 100644 (file)
@@ -1,6 +1,24 @@
+// This software is not affiliated with Valve Corporation
+//   We are not affiliated, associated, authorized, endorsed by, or in any way officially 
+//   connected with Valve Corporation, or any of its subsidiaries or its affiliates. 
+// 
+//   All trademarks are property of their respective owners
+
 // Abstract Valve file system 
 //=======================================================================================================================
 
+// Initialize game directories / pakfile
+void fs_set_gameinfo( const char *path );
+void fs_exit(void);
+
+// Read asset as binary in full. Will search VPK, then searchpaths, or return NULL if not found
+char *valve_fs_get( const char *path );
+
+// Implementation
+//=======================================================================================================================
+
+#ifdef VALVE_IMPLEMENTATION
+
 struct valve_filesystem
 {
        char gamedir[ 512 ];
@@ -25,12 +43,11 @@ void fs_set_gameinfo( const char *path )
        
        // Set gamedir
        strcpy( fs->gamedir, path );
-       csr_path_winunix( fs->gamedir );
-       *csr_findext( fs->gamedir, '/' ) = 0x00;
+       csr_downlvl( fs->gamedir );
        
        // Set exe dir
        strcpy( fs->exedir, fs->gamedir );
-       strcat( fs->exedir, "../" );
+       csr_downlvl( fs->exedir );
        
        // Get all search paths from file
        vdf_node *search_paths = vdf_next(vdf_next(vdf_next( info, "GameInfo", NULL ), "FileSystem", NULL ), "SearchPaths", NULL );
@@ -68,7 +85,7 @@ void fs_set_gameinfo( const char *path )
                strcpy( pack_path, fs->searchpaths[i] );
                strcat( pack_path, "pak01_dir.vpk" );
        
-               if( (fs->vpk = (VPKHeader *)csr_asset_read( path )) )
+               if( (fs->vpk = (VPKHeader *)csr_asset_read( pack_path )) )
                {
                        break;
                }
@@ -76,19 +93,19 @@ void fs_set_gameinfo( const char *path )
        
        if( !fs->vpk )
        {
-               fprintf( stderr, "Could not locate pak01_dir.vpk in %i searchpaths. Stock models will not load!", csr_sb_count( fs->searchpaths ) );
+               log_error( "Could not locate pak01_dir.vpk in %i searchpaths. Stock models will not load!\n", csr_sb_count( fs->searchpaths ) );
        }
 
-       printf( "fs_info:\n\
-  gamedir: %s\n\
-   exedir: %s\n\
-      bin: %s\n\
-     pack: %s\n\
-  searchpaths:\n", fs->gamedir, fs->exedir, fs->bindir, pack_path );
+       log_info( "fs_info:\n" );
+       log_info( "  gamedir: %s\n", fs->gamedir );
+       log_info( "   exedir: %s\n", fs->exedir );
+       log_info( "      bin: %s\n", fs->bindir );
+       log_info( "     pack: %s\n", pack_path );
+       log_info( "  searchpaths:\n" );
   
        for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
        {
-               printf( "    %s\n", fs->searchpaths[i] );  
+               log_info( "    '%s'\n", fs->searchpaths[i] );  
        }
 }
 
@@ -103,8 +120,11 @@ void fs_exit(void)
        csr_sb_free( fs->searchpaths );
        fs->searchpaths = NULL;
 
-       vpk_free( fs->vpk );
-       fs->vpk = NULL;
+       if( fs->vpk )
+       {
+               vpk_free( fs->vpk );
+               fs->vpk = NULL;
+       }
        
        if( fs->current_archive )
        {
@@ -120,61 +140,64 @@ char *valve_fs_get( const char *path )
        VPKDirectoryEntry *entry;
        char pak[ 533 ];
 
-       if( (entry = vpk_find( fs->vpk, path )) )
+       if( fs->vpk )
        {
-               if( entry->ArchiveIndex != fs->current_idx )
+               if( (entry = vpk_find( fs->vpk, path )) )
                {
-                       if( fs->current_archive )
+                       if( entry->ArchiveIndex != fs->current_idx )
                        {
-                               fclose( fs->current_archive );
-                               fs->current_archive = NULL;
+                               if( fs->current_archive )
+                               {
+                                       fclose( fs->current_archive );
+                                       fs->current_archive = NULL;
+                               }
+                               
+                               fs->current_idx = entry->ArchiveIndex;
                        }
                        
-                       fs->current_idx = entry->ArchiveIndex;
-               }
-               
-               if( !fs->current_archive )
-               {
-                       sprintf( pak, "%scsgo/pak01_%03hu.vpk", fs->exedir, fs->current_idx );
-                       fs->current_archive = fopen( pak, "rb" );
-                       
                        if( !fs->current_archive )
                        {
-                               fprintf( stderr, "Could not locate %s\n", pak );
+                               sprintf( pak, "%scsgo/pak01_%03hu.vpk", fs->exedir, fs->current_idx );
+                               fs->current_archive = fopen( pak, "rb" );
+                               
+                               if( !fs->current_archive )
+                               {
+                                       log_error( "Could not locate %s\n", pak );
+                                       return NULL;
+                               }
+                       }
+                       
+                       char *filebuf = csr_malloc( entry->EntryLength );
+                       
+                       fseek( fs->current_archive, entry->EntryOffset, SEEK_SET );
+                       if( fread( filebuf, 1, entry->EntryLength, fs->current_archive ) == entry->EntryLength )
+                       {
+                               return filebuf;
+                       }
+                       else
+                       {
+                               free( filebuf );
                                return NULL;
                        }
                }
-               
-               char *filebuf = csr_malloc( entry->EntryLength );
-               
-               fseek( fs->current_archive, entry->EntryOffset, SEEK_SET );
-               if( fread( filebuf, 1, entry->EntryLength, fs->current_archive ) == entry->EntryLength )
-               {
-                       return filebuf;
-               }
-               else
-               {
-                       free( filebuf );
-                       return NULL;
-               }
        }
-       else
+       
+       // Use physical searchpaths
+       char path_buf[ 512 ];
+       
+       for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
        {
-               // Use physical searchpaths
-               char path_buf[ 512 ];
+               strcpy( path_buf, fs->searchpaths[ i ] );
+               strcat( path_buf, path );
                
-               for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
+               char *filebuf;
+               if( (filebuf = csr_asset_read( path_buf )) )
                {
-                       strcpy( path_buf, fs->searchpaths[ i ] );
-                       strcat( path_buf, path );
-                       
-                       char *filebuf;
-                       if( (filebuf = csr_asset_read( path_buf )) )
-                       {
-                               return filebuf;
-                       }
+                       return filebuf;
                }
-               
-               return NULL;
        }
+       
+       return NULL;
 }
+
+#endif