init
[csRadar.git] / vpk.h
1 // VPK
2 //=======================================================================================================================
3
4 #pragma pack(push, 1)
5 typedef struct
6 {
7 u32 Signature;
8 u32 Version;
9 u32 TreeSize;
10 u32 FileDataSectionSize;
11 u32 ArchiveMD5SectionSize;
12 u32 OtherMD5SectionSize;
13 u32 SignatureSectionSize;
14 }
15 VPKHeader;
16
17 typedef struct
18 {
19 u32 CRC;
20 u16 PreloadBytes;
21 u16 ArchiveIndex;
22 u32 EntryOffset;
23 u32 EntryLength;
24 u16 Terminator;
25 }
26 VPKDirectoryEntry;
27 #pragma pack(pop)
28
29 void vpk_free( VPKHeader *self )
30 {
31 free( self );
32 }
33
34 VPKDirectoryEntry *vpk_find( VPKHeader *self, const char *asset )
35 {
36 if( !self )
37 return NULL;
38
39 char wbuf[ 512 ];
40 strcpy( wbuf, asset );
41
42 char *ext = csr_findext( wbuf, '.' );
43 *(ext-1) = 0x00;
44 char *fn = csr_findext( wbuf, '/' );
45 *(fn-1) = 0x00;
46 char *dir = wbuf;
47
48 char *pCur = ((char *)self) + sizeof( VPKHeader );
49
50 while( 1 )
51 {
52 if( !*pCur ) break;
53
54 int bExt = !strcmp( ext, pCur );
55
56 while( *( pCur ++ ) ) {};
57 while( 1 )
58 {
59 if( !*pCur ) { pCur ++; break; }
60
61 int bDir = !strcmp( dir, pCur );
62
63 while( *( pCur ++ ) ) {};
64 while( 1 )
65 {
66 if( !*pCur ) { pCur ++; break; }
67
68 const char *vpk_fn = pCur;
69
70 while( *( pCur ++ ) ) {};
71 VPKDirectoryEntry *entry = (VPKDirectoryEntry *)pCur;
72
73 if( !strcmp( vpk_fn, fn ) && bExt && bDir )
74 {
75 return entry;
76 }
77
78 pCur += entry->PreloadBytes + sizeof( VPKDirectoryEntry );
79 }
80
81 if( bDir && bExt ) return NULL;
82 }
83
84 if( bExt ) return NULL;
85 }
86
87 return NULL;
88 }