fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / vpk.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 // Valve pak file directory reader
8
9 // VPK
10 //=======================================================================================================================
11
12 typedef struct VPKHeader VPKHeader;
13 typedef struct VPKDirectoryEntry VPKDirectoryEntry;
14
15 // API
16 // ---
17
18 // There is no load function, reading the header file as binary is enough.
19 VPKDirectoryEntry *vpk_find( VPKHeader *self, const char *asset );
20 void vpk_free( VPKHeader *self );
21
22 #pragma pack(push, 1)
23 struct VPKHeader
24 {
25 u32 Signature;
26 u32 Version;
27 u32 TreeSize;
28 u32 FileDataSectionSize;
29 u32 ArchiveMD5SectionSize;
30 u32 OtherMD5SectionSize;
31 u32 SignatureSectionSize;
32 };
33
34 struct VPKDirectoryEntry
35 {
36 u32 CRC;
37 u16 PreloadBytes;
38 u16 ArchiveIndex;
39 u32 EntryOffset;
40 u32 EntryLength;
41 u16 Terminator;
42 };
43 #pragma pack(pop)
44
45 #ifdef VALVE_IMPLEMENTATION
46
47 void vpk_free( VPKHeader *self )
48 {
49 free( self );
50 }
51
52 VPKDirectoryEntry *vpk_find( VPKHeader *self, const char *asset )
53 {
54 if( !self )
55 return NULL;
56
57 char wbuf[ 512 ];
58 strcpy( wbuf, asset );
59
60 char *ext = csr_findext( wbuf, '.' );
61 *(ext-1) = 0x00;
62 char *fn = csr_findext( wbuf, '/' );
63 *(fn-1) = 0x00;
64 char *dir = wbuf;
65
66 char *pCur = ((char *)self) + sizeof( VPKHeader );
67
68 while( 1 )
69 {
70 if( !*pCur ) break;
71
72 int bExt = !strcmp( ext, pCur );
73
74 while( *( pCur ++ ) ) {};
75 while( 1 )
76 {
77 if( !*pCur ) { pCur ++; break; }
78
79 int bDir = !strcmp( dir, pCur );
80
81 while( *( pCur ++ ) ) {};
82 while( 1 )
83 {
84 if( !*pCur ) { pCur ++; break; }
85
86 const char *vpk_fn = pCur;
87
88 while( *( pCur ++ ) ) {};
89 VPKDirectoryEntry *entry = (VPKDirectoryEntry *)pCur;
90
91 if( !strcmp( vpk_fn, fn ) && bExt && bDir )
92 {
93 return entry;
94 }
95
96 pCur += entry->PreloadBytes + sizeof( VPKDirectoryEntry );
97 }
98
99 if( bDir && bExt ) return NULL;
100 }
101
102 if( bExt ) return NULL;
103 }
104
105 return NULL;
106 }
107
108 #endif