fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / vfilesys.h
1 // This software is not affiliated with Valve Corporation
2 // We are not affiliated, associated, authorized, endorsed by, or in any way officially
3 // connected with Valve Corporation, or any of its subsidiaries or its affiliates.
4 //
5 // All trademarks are property of their respective owners
6
7 // Abstract Valve file system
8 //=======================================================================================================================
9
10 // Initialize game directories / pakfile
11 void fs_set_gameinfo( const char *path );
12 void fs_exit(void);
13
14 // Read asset as binary in full. Will search VPK, then searchpaths, or return NULL if not found
15 char *valve_fs_get( const char *path );
16
17 // Implementation
18 //=======================================================================================================================
19
20 #ifdef VALVE_IMPLEMENTATION
21
22 struct valve_filesystem
23 {
24 char gamedir[ 512 ];
25 char exedir[ 512 ];
26 char bindir[ 512 ]; // TODO: This is currently not set?
27
28 VPKHeader *vpk;
29 char **searchpaths;
30
31 FILE *current_archive;
32 u16 current_idx;
33 }
34 fs_global;
35
36 void fs_set_gameinfo( const char *path )
37 {
38 struct valve_filesystem *fs = &fs_global;
39
40 vdf_node *info = vdf_open_file( path );
41 if( !info )
42 return;
43
44 // Set gamedir
45 strcpy( fs->gamedir, path );
46 csr_downlvl( fs->gamedir );
47
48 // Set exe dir
49 strcpy( fs->exedir, fs->gamedir );
50 csr_downlvl( fs->exedir );
51
52 // Get all search paths from file
53 vdf_node *search_paths = vdf_next(vdf_next(vdf_next( info, "GameInfo", NULL ), "FileSystem", NULL ), "SearchPaths", NULL );
54
55 kv_foreach( search_paths, "Game", kv_game )
56 {
57 if( kv_game[0] == '|' ) continue; //TODO: deal with engine replacements?? maybe??
58
59 char *buf;
60 if( csr_path_is_abs( kv_game ) )
61 {
62 buf = csr_malloc( strlen( kv_game ) + 2 );
63 strcpy( buf, kv_game );
64 strcat( buf, "/" );
65 }
66 else
67 {
68 buf = csr_malloc( strlen( fs->exedir ) + strlen( kv_game ) + 2 );
69 strcpy( buf, fs->exedir );
70 strcat( buf, kv_game );
71 strcat( buf, "/" );
72 }
73
74 fs->searchpaths = csr_sb_reserve( fs->searchpaths, 1, sizeof( char * ) );
75 fs->searchpaths[ csr_sb_count( fs->searchpaths ) ] = buf;
76 csr_sb_use( fs->searchpaths );
77 }
78
79 vdf_free_r( info );
80
81 // Look for pak01_dir
82 char pack_path[512];
83 for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
84 {
85 strcpy( pack_path, fs->searchpaths[i] );
86 strcat( pack_path, "pak01_dir.vpk" );
87
88 if( (fs->vpk = (VPKHeader *)csr_asset_read( pack_path )) )
89 {
90 break;
91 }
92 }
93
94 if( !fs->vpk )
95 {
96 log_error( "Could not locate pak01_dir.vpk in %i searchpaths. Stock models will not load!\n", csr_sb_count( fs->searchpaths ) );
97 }
98
99 log_info( "fs_info:\n" );
100 log_info( " gamedir: %s\n", fs->gamedir );
101 log_info( " exedir: %s\n", fs->exedir );
102 log_info( " bin: %s\n", fs->bindir );
103 log_info( " pack: %s\n", pack_path );
104 log_info( " searchpaths:\n" );
105
106 for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
107 {
108 log_info( " '%s'\n", fs->searchpaths[i] );
109 }
110 }
111
112 void fs_exit(void)
113 {
114 struct valve_filesystem *fs = &fs_global;
115
116 for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
117 {
118 free( fs->searchpaths[ i ] );
119 }
120 csr_sb_free( fs->searchpaths );
121 fs->searchpaths = NULL;
122
123 if( fs->vpk )
124 {
125 vpk_free( fs->vpk );
126 fs->vpk = NULL;
127 }
128
129 if( fs->current_archive )
130 {
131 fclose( fs->current_archive );
132 fs->current_archive = NULL;
133 }
134 }
135
136 char *valve_fs_get( const char *path )
137 {
138 struct valve_filesystem *fs = &fs_global;
139
140 VPKDirectoryEntry *entry;
141 char pak[ 533 ];
142
143 if( fs->vpk )
144 {
145 if( (entry = vpk_find( fs->vpk, path )) )
146 {
147 if( entry->ArchiveIndex != fs->current_idx )
148 {
149 if( fs->current_archive )
150 {
151 fclose( fs->current_archive );
152 fs->current_archive = NULL;
153 }
154
155 fs->current_idx = entry->ArchiveIndex;
156 }
157
158 if( !fs->current_archive )
159 {
160 sprintf( pak, "%scsgo/pak01_%03hu.vpk", fs->exedir, fs->current_idx );
161 fs->current_archive = fopen( pak, "rb" );
162
163 if( !fs->current_archive )
164 {
165 log_error( "Could not locate %s\n", pak );
166 return NULL;
167 }
168 }
169
170 char *filebuf = csr_malloc( entry->EntryLength );
171
172 fseek( fs->current_archive, entry->EntryOffset, SEEK_SET );
173 if( fread( filebuf, 1, entry->EntryLength, fs->current_archive ) == entry->EntryLength )
174 {
175 return filebuf;
176 }
177 else
178 {
179 free( filebuf );
180 return NULL;
181 }
182 }
183 }
184
185 // Use physical searchpaths
186 char path_buf[ 512 ];
187
188 for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ )
189 {
190 strcpy( path_buf, fs->searchpaths[ i ] );
191 strcat( path_buf, path );
192
193 char *filebuf;
194 if( (filebuf = csr_asset_read( path_buf )) )
195 {
196 return filebuf;
197 }
198 }
199
200 return NULL;
201 }
202
203 #endif