2f33f8312e09809b118f1055197ab0fa784d5cf4
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( path
)) )
79 fprintf( stderr
, "Could not locate pak01_dir.vpk in %i searchpaths. Stock models will not load!", csr_sb_count( fs
->searchpaths
) );
87 searchpaths:\n", fs
->gamedir
, fs
->exedir
, fs
->bindir
, pack_path
);
89 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
91 printf( " %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
;
109 if( fs
->current_archive
)
111 fclose( fs
->current_archive
);
112 fs
->current_archive
= NULL
;
116 char *valve_fs_get( const char *path
)
118 struct valve_filesystem
*fs
= &fs_global
;
120 VPKDirectoryEntry
*entry
;
123 if( (entry
= vpk_find( fs
->vpk
, path
)) )
125 if( entry
->ArchiveIndex
!= fs
->current_idx
)
127 if( fs
->current_archive
)
129 fclose( fs
->current_archive
);
130 fs
->current_archive
= NULL
;
133 fs
->current_idx
= entry
->ArchiveIndex
;
136 if( !fs
->current_archive
)
138 sprintf( pak
, "%scsgo/pak01_%03hu.vpk", fs
->exedir
, fs
->current_idx
);
139 fs
->current_archive
= fopen( pak
, "rb" );
141 if( !fs
->current_archive
)
143 fprintf( stderr
, "Could not locate %s\n", pak
);
148 char *filebuf
= csr_malloc( entry
->EntryLength
);
150 fseek( fs
->current_archive
, entry
->EntryOffset
, SEEK_SET
);
151 if( fread( filebuf
, 1, entry
->EntryLength
, fs
->current_archive
) == entry
->EntryLength
)
163 // Use physical searchpaths
164 char path_buf
[ 512 ];
166 for( int i
= 0; i
< csr_sb_count( fs
->searchpaths
); i
++ )
168 strcpy( path_buf
, fs
->searchpaths
[ i
] );
169 strcat( path_buf
, path
);
172 if( (filebuf
= csr_asset_read( path_buf
)) )