99524cc1ed81ff2725e05221799f6151f6cf1a46
5 #include "common/wadres.h"
6 #include "common/streams.h"
16 static int n_resources
;
17 static Reader
*wads
[MAX_WADS
];
18 static Entry resources
[MAX_RESOURCES
];
20 static int check_header (Reader
*r
) {
24 stream_read(ident
, 4, 1, r
);
25 return (memcmp(ident
, "IWAD", 4) == 0) || (memcmp(ident
, "PWAD", 4) == 0);
28 int WADRES_addwad (Reader
*r
) {
30 if (n_wads
< MAX_WADS
&& check_header(r
)) {
39 static int WADRES_addresource (const Entry
*e
) {
41 for (int i
= 0; i
< n_resources
; ++i
) {
42 if (cp866_strncasecmp(resources
[i
].name
, e
->name
, 8) == 0) {
43 memcpy(&resources
[i
], e
, sizeof(Entry
));
47 if (n_resources
< MAX_RESOURCES
) {
48 memcpy(&resources
[n_resources
], e
, sizeof(Entry
));
50 return n_resources
- 1;
55 static int WADRES_read (Reader
*r
) {
56 r
->setpos(r
, 4); // skip magic
57 int32_t n
= stream_read32(r
);
58 int32_t dir
= stream_read32(r
);
61 for (int32_t i
= 0; ok
&& i
< n
; ++i
) {
63 e
.offset
= stream_read32(r
);
64 e
.size
= stream_read32(r
);
65 stream_read(e
.name
, 8, 1, r
);
66 ok
= WADRES_addresource(&e
) != -1;
71 int WADRES_rehash (void) {
73 for (int i
= 0; i
< n_wads
; ++i
) {
74 if (!WADRES_read(wads
[i
])) {
81 int WADRES_find (const char name
[8]) {
82 int i
= n_resources
- 1;
83 while (i
>= 0 && cp866_strncasecmp(name
, resources
[i
].name
, 8) != 0) {
89 int WADRES_maxids (void) {
93 Reader
*WADRES_getbasereader (int id
) {
94 assert(id
>= 0 && id
< n_resources
);
95 return wads
[resources
[id
].f
];
98 long WADRES_getoffset (int id
) {
99 assert(id
>= 0 && id
< n_resources
);
100 return resources
[id
].offset
;
103 long WADRES_getsize (int id
) {
104 assert(id
>= 0 && id
< n_resources
);
105 return resources
[id
].size
;
108 void WADRES_getname (int id
, char *name
) {
109 assert(id
>= 0 && id
< n_resources
);
110 strncpy(name
, resources
[id
].name
, 8);
113 void WADRES_getdata (int id
, void *data
) {
114 assert(id
>= 0 && id
< n_resources
);
115 Reader
*r
= wads
[resources
[id
].f
];
116 long pos
= r
->getpos(r
);
117 r
->setpos(r
, resources
[id
].offset
);
118 stream_read(data
, resources
[id
].size
, 1, r
);