1 // Abstract Valve file system
2 //=======================================================================================================================
4 struct valve_filesystem
8 char bindir
[ 512 ]; // TODO: This is currently not set?
13 FILE *current_archive
;
18 void fs_set_gameinfo( const char *path
)
20 struct valve_filesystem
*fs
= &fs_global
;
22 vdf_node
*info
= vdf_open_file( path
);
27 strcpy( fs
->gamedir
, path
);
28 csr_path_winunix( fs
->gamedir
);
29 *csr_findext( fs
->gamedir
, '/' ) = 0x00;
32 strcpy( fs
->exedir
, fs
->gamedir
);
33 strcat( fs
->exedir
, "../" );
35 // Get all search paths from file
36 vdf_node
*search_paths
= vdf_next(vdf_next(vdf_next( info
, "GameInfo", NULL
), "FileSystem", NULL
), "SearchPaths", NULL
);
38 kv_foreach( search_paths
, "Game", kv_game
)
40 if( kv_game
[0] == '|' ) continue; //TODO: deal with engine replacements?? maybe??
43 if( csr_path_is_abs( kv_game
) )
45 buf
= csr_malloc( strlen( kv_game
) + 2 );
46 strcpy( buf
, kv_game
);
51 buf
= csr_malloc( strlen( fs
->exedir
) + strlen( kv_game
) + 2 );
52 strcpy( buf
, fs
->exedir
);
53 strcat( buf
, kv_game
);
57 fs
->searchpaths
= csr_sb_reserve( fs
->searchpaths
, 1, sizeof( char * ) );
58 fs
->searchpaths
[ csr_sb_count( fs
->searchpaths
) ] = buf
;
59 csr_sb_use( fs
->searchpaths
);
66 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
68 strcpy( pack_path
, fs
->searchpaths
[i
] );
69 strcat( pack_path
, "pak01_dir.vpk" );
71 if( (fs
->vpk
= (VPKHeader
*)csr_asset_read( pack_path
)) )
79 log_error( "Could not locate pak01_dir.vpk in %i searchpaths. Stock models will not load!", csr_sb_count( fs
->searchpaths
) );
82 log_info( "fs_info:\n" );
83 log_info( " gamedir: %s\n", fs
->gamedir
);
84 log_info( " exedir: %s\n", fs
->exedir
);
85 log_info( " bin: %s\n", fs
->bindir
);
86 log_info( " pack: %s\n", pack_path
);
87 log_info( " searchpaths:\n" );
89 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
91 log_info( " '%s'\n", fs
->searchpaths
[i
] );
97 struct valve_filesystem
*fs
= &fs_global
;
99 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
101 free( fs
->searchpaths
[ i
] );
103 csr_sb_free( fs
->searchpaths
);
104 fs
->searchpaths
= NULL
;
112 if( fs
->current_archive
)
114 fclose( fs
->current_archive
);
115 fs
->current_archive
= NULL
;
119 char *valve_fs_get( const char *path
)
121 struct valve_filesystem
*fs
= &fs_global
;
123 VPKDirectoryEntry
*entry
;
128 if( (entry
= vpk_find( fs
->vpk
, path
)) )
130 if( entry
->ArchiveIndex
!= fs
->current_idx
)
132 if( fs
->current_archive
)
134 fclose( fs
->current_archive
);
135 fs
->current_archive
= NULL
;
138 fs
->current_idx
= entry
->ArchiveIndex
;
141 if( !fs
->current_archive
)
143 sprintf( pak
, "%scsgo/pak01_%03hu.vpk", fs
->exedir
, fs
->current_idx
);
144 fs
->current_archive
= fopen( pak
, "rb" );
146 if( !fs
->current_archive
)
148 log_error( "Could not locate %s\n", pak
);
153 char *filebuf
= csr_malloc( entry
->EntryLength
);
155 fseek( fs
->current_archive
, entry
->EntryOffset
, SEEK_SET
);
156 if( fread( filebuf
, 1, entry
->EntryLength
, fs
->current_archive
) == entry
->EntryLength
)
168 // Use physical searchpaths
169 char path_buf
[ 512 ];
171 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
173 strcpy( path_buf
, fs
->searchpaths
[ i
] );
174 strcat( path_buf
, path
);
177 if( (filebuf
= csr_asset_read( path_buf
)) )