Mercurial > hg > dmlib
annotate src/dmres.c @ 1046:7e54b2d08ce7
Add special debug error message function/macro that can be
disabled and compiled out.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 01 Mar 2015 18:03:08 +0200 |
parents | 1f37239172ad |
children | 509e6ed3a24e |
rev | line source |
---|---|
0 | 1 /* |
2 * dmlib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
863 | 5 * (C) Copyright 2003-2015 Tecnic Software productions (TNSP) |
0 | 6 */ |
7 #include "dmres.h" | |
8 #include <time.h> | |
9 | |
719
eb4bd1f7b679
Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
10 #ifdef DM_USE_PACKFS |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
11 # ifdef DM_USE_ZLIB |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
12 # include <zlib.h> |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
13 # else |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
14 # include "dmzlib.h" |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
15 # endif |
719
eb4bd1f7b679
Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
16 #endif |
eb4bd1f7b679
Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
17 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
18 #ifdef DM_USE_STDIO |
989 | 19 # include <sys/types.h> |
20 # include <sys/stat.h> | |
21 # include <unistd.h> | |
22 # include <dirent.h> | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
23 #endif |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
24 |
0 | 25 |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
26 DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags) |
0 | 27 { |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
28 DMResource *node = dmMalloc0(sizeof(DMResource)); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
29 if (node == NULL) |
0 | 30 return NULL; |
31 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
32 node->lib = lib; |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
33 node->filename = dm_strdup(filename); |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
34 node->rawSize = size; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
35 node->flags = flags; |
1041 | 36 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
37 return node; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
38 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
39 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
40 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
41 void dmResourceFreeResData(DMResource *node) |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
42 { |
1044
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
43 if (node->resData != NULL) |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
44 { |
1044
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
45 if (node->rops != NULL && |
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
46 node->rops->free != NULL) |
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
47 node->rops->free(node); |
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
48 else |
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
49 dmFree(node->resData); |
1ec6ae223941
Sanitize resource data freeing.
Matti Hamalainen <ccr@tnsp.org>
parents:
1041
diff
changeset
|
50 node->resData = NULL; |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
51 } |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
52 node->flags &= ~DMF_LOADED_RES; |
0 | 53 } |
54 | |
55 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
56 void dmResourceFreeRawData(DMResource *node) |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
57 { |
731
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
58 if ((node->flags & DMF_UNALLOCATED) == 0) |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
59 { |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
60 dmFree(node->rawData); |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
61 node->rawData = NULL; |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
62 node->flags &= ~DMF_LOADED_RAW; |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
63 } |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
64 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
65 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
66 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
67 void dmResourceFree(DMResource *node) |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
68 { |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
69 if (node != NULL) |
0 | 70 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
71 dmResourceFreeResData(node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
72 dmResourceFreeRawData(node); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
73 dmFree(node->filename); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
74 dmFree(node); |
0 | 75 } |
76 } | |
77 | |
78 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
79 void dmResourceInsert(DMResourceLib *lib, DMResource * node) |
0 | 80 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
81 if (lib == NULL || node == NULL) |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
82 return; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
83 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
84 node->lib = lib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
85 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
86 if (lib->resources != NULL) |
0 | 87 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
88 node->prev = lib->resources->prev; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
89 lib->resources->prev->next = node; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
90 lib->resources->prev = node; |
0 | 91 } |
92 else | |
93 { | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
94 lib->resources = node->prev = node; |
0 | 95 } |
96 | |
97 node->next = NULL; | |
98 } | |
99 | |
100 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
101 void dmResourceDelete(DMResourceLib *lib, DMResource * node) |
0 | 102 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
103 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
104 return; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
105 |
0 | 106 if (node->prev) |
107 node->prev->next = node->next; | |
108 | |
109 if (node->next) | |
110 node->next->prev = node->prev; | |
111 else | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
112 lib->resources->prev = node->prev; |
0 | 113 |
114 node->prev = node->next = NULL; | |
115 } | |
116 | |
117 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
118 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename) |
0 | 119 { |
120 DMResource *node, *found = NULL; | |
121 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
122 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
123 return NULL; |
0 | 124 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
125 dmMutexLock(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
126 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
127 for (node = lib->resources; node != NULL; node = node->next) |
0 | 128 { |
129 if (strcmp(node->filename, filename) == 0) | |
130 { | |
131 found = node; | |
132 break; | |
133 } | |
134 } | |
135 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
136 dmMutexUnlock(lib->mutex); |
0 | 137 |
138 return found; | |
139 } | |
140 | |
141 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
142 #ifdef DM_USE_STDIO |
0 | 143 /* Basic stdio file routines |
144 */ | |
145 static int dm_stdio_fopen(DMResource *handle) | |
146 { | |
147 char *rfilename = dm_strdup_printf("%s%s", DMRES_DATA_PATH, handle->filename); | |
148 if (rfilename == NULL) | |
149 return DMERR_MALLOC; | |
150 | |
151 handle->fh = fopen(rfilename, "rb"); | |
152 dmFree(rfilename); | |
153 | |
154 handle->error = dmGetErrno(); | |
155 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN; | |
156 } | |
157 | |
158 | |
159 static void dm_stdio_fclose(DMResource * f) | |
160 { | |
161 if (f->fh != NULL) | |
162 { | |
163 fclose(f->fh); | |
164 f->fh = NULL; | |
165 } | |
166 } | |
167 | |
168 | |
169 static int dm_stdio_ferror(DMResource * f) | |
170 { | |
171 return f->error; | |
172 } | |
173 | |
174 | |
1036 | 175 static Sint64 dm_stdio_ftell(DMResource * f) |
0 | 176 { |
1036 | 177 return DM_FTELL64(f->fh); |
178 } | |
179 | |
180 | |
181 static int dm_stdio_fseek(DMResource *f, const Sint64 pos, const int whence) | |
182 { | |
183 int ret = DM_FSEEK64(f->fh, pos, whence); | |
0 | 184 f->error = dmGetErrno(); |
185 return ret; | |
186 } | |
187 | |
188 | |
732 | 189 static int dm_stdio_freset(DMResource * f) |
190 { | |
191 if (f->fh != NULL) | |
1036 | 192 return dm_stdio_fseek(f, 0, SEEK_SET); |
732 | 193 else |
194 return DMERR_OK; | |
195 } | |
196 | |
197 | |
0 | 198 static off_t dm_stdio_fsize(DMResource *f) |
199 { | |
200 off_t savePos, fileSize; | |
201 | |
202 // Check if the size is cached | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
203 if (f->rawSize != 0) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
204 return f->rawSize; |
0 | 205 |
206 // Get file size | |
1036 | 207 savePos = dm_stdio_ftell(f); |
208 if (dm_stdio_fseek(f, 0, SEEK_END) != 0) | |
0 | 209 return -1; |
210 | |
1036 | 211 fileSize = dm_stdio_ftell(f); |
212 if (dm_stdio_fseek(f, savePos, SEEK_SET) != 0) | |
0 | 213 return -1; |
214 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
215 f->rawSize = fileSize; |
0 | 216 return fileSize; |
217 } | |
218 | |
219 | |
220 static BOOL dm_stdio_feof(DMResource * f) | |
221 { | |
222 return feof(f->fh); | |
223 } | |
224 | |
225 | |
226 static int dm_stdio_fgetc(DMResource * f) | |
227 { | |
228 int ret = fgetc(f->fh); | |
229 f->error = dmGetErrno(); | |
230 return ret; | |
231 } | |
232 | |
233 | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
234 static int dm_stdio_fputc(int v, DMResource * f) |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
235 { |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
236 int ret = fputc(v, f->fh); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
237 f->error = dmGetErrno(); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
238 return ret; |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
239 } |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
240 |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
241 |
0 | 242 static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource * f) |
243 { | |
244 size_t ret = fread(ptr, size, nmemb, f->fh); | |
245 f->error = dmGetErrno(); | |
246 return ret; | |
247 } | |
248 | |
249 | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
250 static size_t dm_stdio_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
251 { |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
252 size_t ret = fwrite(ptr, size, nmemb, f->fh); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
253 f->error = dmGetErrno(); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
254 return ret; |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
255 } |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
256 |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
257 |
0 | 258 static int dm_stdio_preload(DMResource *handle) |
259 { | |
260 int ret = dm_stdio_fopen(handle); | |
261 if (ret != DMERR_OK) | |
262 return ret; | |
263 | |
264 dm_stdio_fsize(handle); | |
265 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
266 handle->rawData = dmMalloc(handle->rawSize); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
267 if (handle->rawData == NULL) |
0 | 268 return DMERR_MALLOC; |
269 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
270 if (dm_stdio_fread(handle->rawData, sizeof(Uint8), handle->rawSize, handle) != handle->rawSize) |
0 | 271 return DMERR_FREAD; |
272 | |
273 return DMERR_OK; | |
274 } | |
275 | |
276 | |
277 DMResourceOps dfStdioFileOps = | |
278 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
279 "Stdio", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
280 |
732 | 281 dm_stdio_freset, |
0 | 282 dm_stdio_ferror, |
283 dm_stdio_fseek, | |
284 dm_stdio_fsize, | |
285 dm_stdio_ftell, | |
286 dm_stdio_feof, | |
287 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
288 dm_stdio_fputc, |
0 | 289 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
290 dm_stdio_fwrite, |
0 | 291 |
292 dm_stdio_fopen, | |
293 dm_stdio_fclose, | |
294 dm_stdio_preload | |
295 }; | |
296 | |
297 DMResourceOps dfStdioFHOps = | |
298 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
299 "StdioFH", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
300 |
732 | 301 dm_stdio_freset, |
0 | 302 dm_stdio_ferror, |
303 dm_stdio_fseek, | |
304 dm_stdio_fsize, | |
305 dm_stdio_ftell, | |
306 dm_stdio_feof, | |
307 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
308 dm_stdio_fputc, |
0 | 309 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
310 dm_stdio_fwrite, |
0 | 311 |
312 NULL, | |
313 NULL, | |
314 NULL | |
315 }; | |
316 #endif | |
317 | |
318 | |
319 // Some mingw/windows headers define these as macros, which is bad for us | |
320 #ifdef __WIN32 | |
321 #undef ferror | |
322 #undef feof | |
323 #endif | |
324 | |
325 | |
326 /* | |
327 * PACK file routines | |
328 */ | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
329 #ifdef DM_USE_PACKFS |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
330 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
331 #ifdef DM_USE_ZLIB |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
332 |
995
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
333 #define DMRES_TMPBUF_SIZE (128 * 1024) |
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
334 |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
335 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) |
0 | 336 { |
337 int res = DMERR_OK, cres, cdataLeft; | |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
338 Uint8 * cbuffer = NULL; |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
339 z_stream zstr; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
340 BOOL zinit = FALSE; |
0 | 341 |
342 // Allocate a structures and buffers | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
343 if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL) |
0 | 344 { |
345 res = DMERR_MALLOC; | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
346 goto out; |
0 | 347 } |
348 | |
349 // Initialize decompression | |
1038 | 350 memset(&zstr, 0, sizeof(zstr)); |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
351 zstr.next_out = handle->rawData; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
352 zstr.avail_out = handle->rawSize; |
0 | 353 cdataLeft = node->length; |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
354 |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
355 if (inflateInit(&zstr) != Z_OK) |
0 | 356 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
357 res = dmErrorDBG(DMERR_INIT_FAIL, |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
358 "Could not initialize zlib stream decompression.\n"); |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
359 goto out; |
0 | 360 } |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
361 zinit = TRUE; |
0 | 362 |
363 // Uncompress the data | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
364 while (cdataLeft > 0 && zstr.avail_out > 0 && cres == Z_OK) |
0 | 365 { |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
366 zstr.avail_in = fread( |
0 | 367 cbuffer, sizeof(Uint8), |
995
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
368 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
369 handle->lib->packFile->file); |
0 | 370 |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
371 cdataLeft -= zstr.avail_in; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
372 zstr.next_in = cbuffer; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
373 cres = inflate(&zstr, Z_FULL_FLUSH); |
0 | 374 } |
375 | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
376 |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
377 out: |
0 | 378 // Cleanup |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
379 if (zinit) |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
380 inflateEnd(&zstr); |
0 | 381 |
382 dmFree(cbuffer); | |
383 return res; | |
384 } | |
385 | |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
386 #else |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
387 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
388 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
389 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
390 DMZLibContext ctx; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
391 Uint8 *inBuf = NULL; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
392 int ret; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
393 |
1016 | 394 // Allocate buuffer for compressed data |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
395 if ((inBuf = dmMalloc(node->length)) == NULL) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
396 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
397 ret = DMERR_MALLOC; |
1010 | 398 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
399 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
400 |
1016 | 401 // Read compressed data |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
402 if (fread(inBuf, sizeof(Uint8), node->length, handle->lib->packFile->file) != node->length) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
403 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
404 ret = DMERR_FREAD; |
1010 | 405 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
406 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
407 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
408 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
409 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
410 |
1016 | 411 // Initialize decompression structures |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
412 ctx.zbuffer = inBuf; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
413 ctx.zbufferEnd = inBuf + node->length; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
414 |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
415 ctx.zout = ctx.zoutStart = handle->rawData; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
416 ctx.zoutEnd = handle->rawData + node->size; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
417 ctx.expandable = FALSE; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
418 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
419 // Attempt decompression |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
420 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) |
1010 | 421 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
422 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
423 if ((ret = dmZLibDecode(&ctx)) != DMERR_OK) |
1010 | 424 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
425 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
426 handle->rawData = ctx.zoutStart; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
427 handle->rawSize = ctx.zout - ctx.zoutStart; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
428 |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
429 out: |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
430 dmFree(inBuf); |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
431 return ret; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
432 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
433 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
434 #endif |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
435 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
436 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
437 static int dm_pack_preload(DMResource *handle) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
438 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
439 DMPackEntry *node; |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
440 int ret = DMERR_OK; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
441 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
442 if (handle->lib == NULL || handle->lib->packFile == NULL) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
443 return DMERR_NULLPTR; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
444 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
445 // Search PACK nodelist for file |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
446 if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
447 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
448 ret = dmErrorDBG(DMERR_NOT_FOUND, |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
449 "Entry '%s' not found in PACK file.\n", |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
450 handle->filename); |
1010 | 451 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
452 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
453 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
454 // Seek to entry |
1036 | 455 if (DM_FSEEK64(handle->lib->packFile->file, node->offset, SEEK_SET) == -1) |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
456 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
457 ret = dmErrorDBG(DMERR_FSEEK, |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
458 "Could not seek node position in PACK file.\n"); |
1010 | 459 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
460 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
461 |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
462 // Allocate memory for the node |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
463 if ((handle->rawData = dmMalloc(node->size)) == NULL) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
464 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
465 ret = dmErrorDBG(DMERR_MALLOC, |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
466 "Failed to allocate node data for '%s' (%d bytes).\n", |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
467 handle->filename, node->size); |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
468 goto out; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
469 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
470 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
471 // Check if the entry is compressed |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
472 if (handle->flags & DMF_COMPRESSED) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
473 { |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
474 if ((ret = dm_pack_decompress(handle, node)) != DMERR_OK) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
475 goto out; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
476 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
477 if (handle->rawSize != node->size) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
478 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
479 ret = dmErrorDBG(DMERR_COMPRESSION, |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
480 "Decompressed data size for '%s' does not match size stored in PACK entry (%d <> %d).\n", |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
481 handle->filename, handle->rawSize, node->size); |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
482 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
483 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
484 else |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
485 { |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
486 if (node->size != node->length) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
487 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
488 ret = dmErrorDBG(DMERR_INVALID_DATA, |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
489 "Node '%s' raw size and length fields differ for uncompressed node: %d <> %d.\n", |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
490 handle->filename, node->size, node->length); |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
491 goto out; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
492 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
493 if (fread(handle->rawData, sizeof(Uint8), node->size, handle->lib->packFile->file) != node->size) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
494 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
495 ret = dmErrorDBG(DMERR_FREAD, |
1045 | 496 "Error reading raw node data '%s', %d bytes.\n", |
497 handle->filename, node->size); | |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
498 goto out; |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
499 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
500 } |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
501 |
1010 | 502 out: |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
503 return ret; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
504 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
505 |
0 | 506 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
507 static int dm_pack_fopen(DMResource * f) |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
508 { |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
509 if ((f->flags & DMF_LOADED_RAW) == 0) |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
510 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
511 int ret = dm_pack_preload(f); |
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
512 if (ret == DMERR_OK) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
513 f->flags |= DMF_LOADED_RAW; |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
514 |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
515 return ret; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
516 } |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
517 else |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
518 return DMERR_OK; |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
519 } |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
520 |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
521 |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
522 static void dm_pack_fclose(DMResource * f) |
0 | 523 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
524 if ((f->flags & DMF_PERSIST) == 0) |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
525 dmResourceFreeRawData(f); |
0 | 526 } |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
527 #endif |
0 | 528 |
529 | |
732 | 530 static int dm_mem_freset(DMResource * f) |
531 { | |
532 f->rawOffset = 0; | |
533 return DMERR_OK; | |
534 } | |
535 | |
536 | |
0 | 537 static int dm_mem_ferror(DMResource * f) |
538 { | |
539 return f->error; | |
540 } | |
541 | |
542 | |
1036 | 543 static int dm_mem_fseek(DMResource * f, const Sint64 offset, const int whence) |
0 | 544 { |
545 off_t newPos; | |
546 | |
547 // Calculate the new position | |
548 switch (whence) | |
549 { | |
550 case SEEK_SET: | |
551 newPos = offset; | |
552 break; | |
553 | |
554 case SEEK_CUR: | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
555 newPos = f->rawOffset + offset; |
0 | 556 break; |
557 | |
558 case SEEK_END: | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
559 newPos = f->rawSize + offset; |
0 | 560 break; |
561 | |
562 default: | |
563 return -1; | |
564 } | |
565 | |
566 // Set the new position | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
567 f->rawOffset = newPos; |
0 | 568 |
569 // Check the new position | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
570 if (newPos < 0 && (size_t) newPos >= f->rawSize) |
0 | 571 return -1; |
572 | |
573 return 0; | |
574 } | |
575 | |
576 | |
577 static off_t dm_mem_fsize(DMResource * f) | |
578 { | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
579 return f->rawSize; |
0 | 580 } |
581 | |
582 | |
1036 | 583 static Sint64 dm_mem_ftell(DMResource * f) |
0 | 584 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
585 return f->rawOffset; |
0 | 586 } |
587 | |
588 | |
589 static BOOL dm_mem_feof(DMResource * f) | |
590 { | |
591 // Check for EOF | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
592 if ((size_t) f->rawOffset <= f->rawSize) |
0 | 593 return FALSE; |
594 else | |
595 return TRUE; | |
596 } | |
597 | |
598 | |
599 static int dm_mem_fgetc(DMResource * f) | |
600 { | |
601 // Check for EOF | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
602 if ((size_t) f->rawOffset < f->rawSize) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
603 return f->rawData[f->rawOffset++]; |
0 | 604 else |
605 return EOF; | |
606 } | |
607 | |
608 | |
609 static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource * f) | |
610 { | |
611 size_t length = (size * nmemb); | |
612 | |
613 // Check if we can read the whole chunk | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
614 if (((size_t) f->rawOffset + length) >= f->rawSize) |
0 | 615 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
616 nmemb = (f->rawSize - f->rawOffset) / size; |
0 | 617 length = size * nmemb; |
618 } | |
619 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
620 memcpy(buf, f->rawData + f->rawOffset, length); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
621 f->rawOffset += length; |
0 | 622 return nmemb; |
623 } | |
624 | |
625 | |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
626 static int dm_mem_fputc(int ch, DMResource * f) |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
627 { |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
628 // Check for EOF |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
629 if ((size_t) f->rawOffset < f->rawSize) |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
630 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
631 f->rawData[f->rawOffset++] = ch; |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
632 return ch; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
633 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
634 else |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
635 return EOF; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
636 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
637 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
638 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
639 static size_t dm_mem_fwrite(void *buf, size_t size, size_t nmemb, DMResource * f) |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
640 { |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
641 size_t length = (size * nmemb); |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
642 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
643 // Check if we can write the whole chunk |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
644 if (((size_t) f->rawOffset + length) >= f->rawSize) |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
645 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
646 nmemb = (f->rawSize - f->rawOffset) / size; |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
647 length = size * nmemb; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
648 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
649 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
650 if (length > 0) |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
651 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
652 memcpy(f->rawData + f->rawOffset, buf, length); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
653 f->rawOffset += length; |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
654 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
655 return nmemb; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
656 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
657 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
658 |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
659 #ifdef DM_USE_PACKFS |
0 | 660 DMResourceOps dfPackFileOps = |
661 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
662 "PackFS", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
663 |
732 | 664 dm_mem_freset, |
0 | 665 dm_mem_ferror, |
666 dm_mem_fseek, | |
667 dm_mem_fsize, | |
668 dm_mem_ftell, | |
669 dm_mem_feof, | |
670 dm_mem_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
671 NULL, |
0 | 672 dm_mem_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
673 NULL, |
0 | 674 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
675 dm_pack_fopen, |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
676 dm_pack_fclose, |
732 | 677 dm_pack_preload, |
0 | 678 }; |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
679 #endif |
0 | 680 |
681 | |
682 DMResourceOps dfMemIOFileOps = | |
683 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
684 "MemIO", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
685 |
732 | 686 dm_mem_freset, |
0 | 687 dm_mem_ferror, |
688 dm_mem_fseek, | |
689 dm_mem_fsize, | |
690 dm_mem_ftell, | |
691 dm_mem_feof, | |
692 dm_mem_fgetc, | |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
693 dm_mem_fputc, |
0 | 694 dm_mem_fread, |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
695 dm_mem_fwrite, |
0 | 696 |
697 NULL, | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
698 dmResourceFree, |
0 | 699 NULL |
700 }; | |
701 | |
702 | |
703 /* FS file handling functions. These functions call the actual | |
704 * functions depending on where the file is located. | |
705 */ | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
706 static int dmResourcePreload(DMResource *handle) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
707 { |
733 | 708 int ret = DMERR_OK; |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
709 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
710 // Check if we want to preload raw data? |
737
897646df1de1
Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents:
736
diff
changeset
|
711 if ((handle->lib->flags & DRF_PRELOAD_RAW) || |
897646df1de1
Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents:
736
diff
changeset
|
712 handle->rops == NULL || handle->rops->load == NULL) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
713 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
714 if (handle->flags & DMF_LOADED_RAW) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
715 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
716 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
717 if (handle->fops->preload != NULL) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
718 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
719 ret = handle->fops->preload(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
720 if (ret == DMERR_OK) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
721 handle->flags |= DMF_LOADED_RAW | DMF_PERSIST; |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
722 } |
733 | 723 else |
724 ret = DMERR_INIT_FAIL; | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
725 |
732 | 726 dmfreset(handle); |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
727 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
728 |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
729 // Check if resource data is to be preloaded |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
730 if (handle->lib->flags & DRF_PRELOAD_RES) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
731 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
732 if (handle->flags & DMF_LOADED_RES) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
733 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
734 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
735 if (handle->rops != NULL && |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
736 handle->rops->load != NULL) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
737 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
738 if ((ret = handle->fops->fopen(handle)) == DMERR_OK) |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
739 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
740 ret = handle->rops->load(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
741 handle->fops->fclose(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
742 } |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
743 |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
744 if (ret == DMERR_OK) |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
745 handle->flags |= DMF_LOADED_RES; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
746 } |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
747 |
732 | 748 dmfreset(handle); |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
749 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
750 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
751 return ret; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
752 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
753 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
754 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
755 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **phandle) |
0 | 756 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
757 DMResource *handle; |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
758 int ret; |
0 | 759 |
760 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
761 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 762 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
763 #ifdef DM_USE_STDIO |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
764 if (lib->flags & DRF_USE_STDIO) |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
765 { |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
766 // Hmm.. does not exist? Fall back to a stdio file |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
767 *phandle = handle = dmResourceNew(lib, filename, 0, 0); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
768 if (handle == NULL) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
769 return DMERR_MALLOC; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
770 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
771 handle->fops = &dfStdioFileOps; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
772 } |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
773 else |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
774 return DMERR_INIT_FAIL; |
0 | 775 #else |
776 // Stdio not enabled, fail | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
777 return DMERR_INIT_FAIL; |
0 | 778 #endif |
779 } | |
780 | |
781 // Check if the data is preloaded | |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
782 if ((ret = handle->fops->fopen(handle)) == DMERR_OK) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
783 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
784 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
785 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
786 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
787 handle->flags &= ~DMF_TEMPORARY; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
788 dmResourceInsert(lib, handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
789 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
790 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
791 else |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
792 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
793 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
794 dmResourceFree(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
795 *phandle = handle = NULL; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
796 } |
0 | 797 |
737
897646df1de1
Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents:
736
diff
changeset
|
798 dmfreset(handle); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
799 return ret; |
0 | 800 } |
801 | |
802 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
803 int dmf_create_memio(DMResourceLib *lib, const char *filename, |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
804 Uint8 *buf, const size_t size, DMResource **phandle) |
0 | 805 { |
806 DMResource *handle; | |
807 | |
808 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
809 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 810 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
811 if ((*phandle = handle = dmResourceNew( |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
812 lib, filename, size, DMF_LOADED_RAW | DMF_UNALLOCATED)) == NULL) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
813 return DMERR_MALLOC; |
0 | 814 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
815 handle->fops = &dfMemIOFileOps; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
816 handle->rawData = buf; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
817 dmResourceInsert(lib, handle); |
0 | 818 } |
819 | |
820 // Increase refcount | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
821 dmResourceRef(handle); |
732 | 822 dmfreset(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
823 return DMERR_OK; |
0 | 824 } |
825 | |
826 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
827 #ifdef DM_USE_STDIO |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
828 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle) |
0 | 829 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
830 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
831 if ((*phandle = handle = dmResourceNew(NULL, filename, 0, 0)) == NULL) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
832 return DMERR_MALLOC; |
0 | 833 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
834 handle->fops = &dfStdioFileOps; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
835 handle->fh = fopen(filename, mode); |
0 | 836 handle->error = dmGetErrno(); |
837 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
838 if (handle->fh == NULL) |
0 | 839 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
840 dmResourceFree(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
841 return handle->error; |
0 | 842 } |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
843 |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
844 dmResourceRef(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
845 return DMERR_OK; |
0 | 846 } |
847 | |
848 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
849 int dmf_create_stdio_stream(FILE *fh, DMResource **phandle) |
0 | 850 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
851 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
852 if ((*phandle = handle = dmResourceNew(NULL, "", 0, 0)) == NULL) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
853 return DMERR_MALLOC; |
0 | 854 |
855 handle->fops = &dfStdioFHOps; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
856 handle->fh = fh; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
857 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
858 return DMERR_OK; |
0 | 859 } |
860 #endif | |
861 | |
862 | |
863 void dmf_close(DMResource * f) | |
864 { | |
865 if (f == NULL) | |
866 return; | |
867 | |
868 if (f->fops->fclose != NULL) | |
869 f->fops->fclose(f); | |
870 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
871 dmResourceUnref(f); |
0 | 872 } |
873 | |
874 | |
732 | 875 int dmfreset(DMResource *f) |
876 { | |
877 if (f == NULL) | |
878 return DMERR_NULLPTR; | |
879 | |
880 if (f->fops == NULL || f->fops->freset == NULL) | |
881 return DMERR_OK; | |
882 | |
883 return f->fops->freset(f); | |
884 } | |
885 | |
0 | 886 int dmferror(DMResource * f) |
887 { | |
888 f->atime = time(NULL); | |
889 return f->fops->ferror(f); | |
890 } | |
891 | |
1036 | 892 int dmfseek(DMResource * f, Sint64 offset, int whence) |
0 | 893 { |
894 f->atime = time(NULL); | |
895 return f->fops->fseek(f, offset, whence); | |
896 } | |
897 | |
898 off_t dmfsize(DMResource * f) | |
899 { | |
900 f->atime = time(NULL); | |
901 return f->fops->fsize(f); | |
902 } | |
903 | |
904 off_t dmftell(DMResource * f) | |
905 { | |
906 f->atime = time(NULL); | |
907 return f->fops->ftell(f); | |
908 } | |
909 | |
910 BOOL dmfeof(DMResource * f) | |
911 { | |
912 f->atime = time(NULL); | |
913 return f->fops->feof(f); | |
914 } | |
915 | |
916 int dmfgetc(DMResource * f) | |
917 { | |
918 f->atime = time(NULL); | |
919 return f->fops->fgetc(f); | |
920 } | |
921 | |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
922 int dmfputc(int v, DMResource * f) |
72
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
923 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
924 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
925 return f->fops->fputc(v, f); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
926 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
927 |
0 | 928 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource * f) |
929 { | |
930 f->atime = time(NULL); | |
931 return f->fops->fread(ptr, size, nmemb, f); | |
932 } | |
933 | |
72
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
934 size_t dmfwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
935 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
936 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
937 return f->fops->fwrite(ptr, size, nmemb, f); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
938 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
939 |
235 | 940 char *dmfgets(char *s, int size, DMResource * f) |
941 { | |
942 char *p = s, c; | |
943 int n = 0; | |
944 | |
945 while ((c = f->fops->fgetc(f)) != EOF) | |
946 { | |
237
6d9d43bb68eb
Oops, another silly issue in dmfgets().
Matti Hamalainen <ccr@tnsp.org>
parents:
236
diff
changeset
|
947 n++; |
235 | 948 if (c == '\n') |
949 break; | |
950 else | |
951 if (n < size - 1) | |
952 *p++ = c; | |
953 } | |
954 *p = 0; | |
955 | |
236
8189be40ed7c
Oops, dmfgets() had a nasty think-o and counted the remaining buffer space
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
956 return (n > 0) ? s : NULL; |
235 | 957 } |
958 | |
959 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
960 int dmResourceRef(DMResource *node) |
0 | 961 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
962 if (node->lib != NULL) dmMutexLock(node->lib->mutex); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
963 node->atime = time(NULL); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
964 node->refcount++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
965 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); |
0 | 966 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
967 return node->refcount; |
0 | 968 } |
969 | |
970 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
971 int dmResourceUnref(DMResource *node) |
0 | 972 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
973 if (node->lib != NULL) dmMutexLock(node->lib->mutex); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
974 node->refcount--; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
975 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); |
0 | 976 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
977 return node->refcount; |
0 | 978 } |
979 | |
980 | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
981 #ifdef DM_USE_STDIO |
727 | 982 static int dmResourcesLoadDirectory(DMResourceLib *lib, const char *path) |
0 | 983 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
984 int ret = DMERR_OK; |
630 | 985 struct dirent *dh; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
986 DIR *hdir = opendir(path); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
987 if (hdir == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
988 return dmGetErrno(); |
0 | 989 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
990 dmMutexLock(lib->mutex); |
0 | 991 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
992 do |
0 | 993 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
994 DMResource *node = NULL; |
630 | 995 dh = readdir(hdir); |
996 if (dh != NULL) | |
0 | 997 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
998 struct stat sbuf; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
999 char *fname = dm_strdup_printf("%s/%s", path, dh->d_name); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1000 if (stat(fname, &sbuf) == -1) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1001 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1002 ret = dmErrorDBG(dmGetErrno(), |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1003 "Could not stat() %s, #%d: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1004 fname, ret, dmErrorStr(ret)); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1005 dmFree(fname); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1006 goto out; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1007 } |
0 | 1008 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1009 if (S_ISREG(sbuf.st_mode)) |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1010 node = dmResourceNew(lib, dh->d_name, sbuf.st_size, 0); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1011 } |
630 | 1012 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1013 if (node != NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1014 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1015 node->fops = &dfStdioFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1016 dmResourceInsert(lib, node); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1017 } |
630 | 1018 } while (dh != NULL); |
0 | 1019 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1020 out: |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1021 dmMutexUnlock(lib->mutex); |
0 | 1022 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1023 #ifdef __WIN32 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1024 #else |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1025 closedir(hdir); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1026 #endif |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1027 |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1028 return ret; |
0 | 1029 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1030 #endif |
0 | 1031 |
1032 | |
1033 /* Resources subsystem initialization and shutdown routines | |
1034 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1035 int dmResourcesInit(DMResourceLib **plib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)) |
0 | 1036 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1037 DMResourceLib *lib; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1038 BOOL initialized = FALSE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1039 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1040 // Allocate the resource library structure |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1041 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1042 return DMERR_MALLOC; |
0 | 1043 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1044 // Basic data |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1045 lib->mutex = dmCreateMutex(); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1046 lib->flags = flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1047 lib->resPath = dm_strdup((path != NULL) ? path : DMRES_DATA_PATH); |
0 | 1048 |
107 | 1049 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1050 #ifdef DM_USE_PACKFS |
734 | 1051 if (flags & DRF_USE_PACK) |
0 | 1052 { |
1053 int ret; | |
1054 DMPackEntry *node; | |
1055 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1056 lib->packFilename = dm_strdup((filename != NULL) ? filename : DMRES_DATA_PACK); |
0 | 1057 |
1058 // Initialize PACK, open as read-only | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1059 ret = dmPackOpen(lib->packFilename, &lib->packFile, TRUE); |
0 | 1060 if (ret != DMERR_OK) |
1061 { | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1062 if ((flags & DRF_USE_STDIO) == 0) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1063 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1064 return dmErrorDBG(DMERR_INIT_FAIL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1065 "Error opening PACK file '%s', #%d: %s\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1066 lib->packFilename, ret, dmErrorStr(ret)); |
0 | 1067 } |
734 | 1068 else |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1069 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1070 // Non-fatal |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1071 dmErrorDBG(DMERR_INIT_FAIL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1072 "Failed to open PACK, falling back to STDIO, '%s' %d: %s\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1073 lib->packFilename, ret, dmErrorStr(ret)); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1074 } |
0 | 1075 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1076 else |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1077 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1078 // Initialize resources from a PACK file |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1079 for (node = lib->packFile->entries; node != NULL; node = node->next) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1080 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1081 DMResource *res = dmResourceNew(lib, node->filename, node->size, node->flags & DMF_PACK_MASK); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1082 if (res == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1083 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1084 return dmErrorDBG(DMERR_INIT_FAIL, |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1085 "Could not allocate memory for resource node '%s' [0x%08x], %d bytes.\n", |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1086 node->filename, node->flags, node->size); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1087 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1088 |
736
ea42faa02a9a
Set packfile fileops in the appropriate handles.
Matti Hamalainen <ccr@tnsp.org>
parents:
734
diff
changeset
|
1089 res->fops = &dfPackFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1090 dmResourceInsert(lib, res); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1091 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1092 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1093 initialized = TRUE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1094 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1095 } |
0 | 1096 #endif |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1097 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1098 #ifdef DM_USE_STDIO |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1099 if (!initialized && (flags & DRF_USE_STDIO)) |
0 | 1100 { |
1101 // Initialize resources from a resource directory | |
727 | 1102 int ret = dmResourcesLoadDirectory(lib, lib->resPath); |
0 | 1103 if (ret != DMERR_OK) |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1104 return ret; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1105 initialized = TRUE; |
0 | 1106 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1107 #endif |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1108 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1109 if (!initialized) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1110 return DMERR_INIT_FAIL; |
0 | 1111 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1112 // Okay, classify resources |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1113 if (lib->resources != NULL && classifier != NULL) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1114 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1115 DMResource *node; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1116 for (node = lib->resources; node != NULL; node = node->next) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1117 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1118 int ret = classifier(node); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1119 if (ret != DMERR_OK) |
107 | 1120 return ret; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1121 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1122 } |
0 | 1123 |
1124 // Initialization complete | |
1125 return DMERR_OK; | |
1126 } | |
1127 | |
1128 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1129 int dmResourcesClose(DMResourceLib *lib) |
0 | 1130 { |
1131 DMResource *node; | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1132 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1133 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1134 return DMERR_NULLPTR; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1135 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1136 dmMutexLock(lib->mutex); |
0 | 1137 |
1138 // Shutdown possible subsystems | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
1139 #ifdef DM_USE_PACKFS |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1140 if (lib->flags & DRF_USE_PACK) |
0 | 1141 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1142 int ret = dmPackClose(lib->packFile); |
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1143 if (ret != DMERR_OK) |
0 | 1144 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1145 dmErrorDBG(ret, "Error closing PACK, #%i: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1146 ret, dmErrorStr(ret)); |
0 | 1147 } |
1148 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1149 dmFree(lib->packFilename); |
0 | 1150 } |
1151 #endif | |
1152 | |
1153 // Free resource entries | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1154 node = lib->resources; |
0 | 1155 while (node != NULL) |
1156 { | |
1157 DMResource *next = node->next; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1158 dmResourceFree(node); |
0 | 1159 node = next; |
1160 } | |
1161 | |
1162 // Etc. | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1163 dmFree(lib->resPath); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1164 dmMutexUnlock(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1165 dmDestroyMutex(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1166 return DMERR_OK; |
0 | 1167 } |
1168 | |
1169 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1170 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total) |
0 | 1171 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1172 int ret = DMERR_OK; |
0 | 1173 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1174 dmMutexLock(lib->mutex); |
0 | 1175 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1176 // Initialize preloading |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1177 if (lib->preload == NULL || start) |
0 | 1178 { |
1179 DMResource *node; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1180 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1181 lib->preload = lib->resources; |
0 | 1182 *loaded = 0; |
1183 *total = 0; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1184 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1185 // Calculate total number of resources to be preloaded |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1186 if (lib->flags & (DRF_PRELOAD_RAW | DRF_PRELOAD_RES)) |
0 | 1187 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1188 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1189 (*total)++; |
1190 } | |
1191 } | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1192 else |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1193 if (lib->preload != NULL) |
0 | 1194 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1195 // Attempt to preload the resource |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1196 if ((ret = dmResourcePreload(lib->preload)) != DMERR_OK) |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1197 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1198 dmErrorDBG(ret, "Error preloading '%s', %d: %s\n", |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1199 lib->preload->filename, ret, dmErrorStr(ret)); |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1200 goto error; |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1201 } |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1202 |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1203 (*loaded)++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1204 lib->preload = lib->preload->next; |
0 | 1205 } |
1206 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1207 dmMutexUnlock(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1208 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1209 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1210 error: |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1211 dmMutexUnlock(lib->mutex); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1212 return ret; |
0 | 1213 } |
1214 | |
1215 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1216 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags) |
0 | 1217 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1218 DMResource *node; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1219 const int stamp = time(NULL); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1220 dmMutexLock(lib->mutex); |
0 | 1221 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1222 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1223 { |
1224 // Check if node has refcount of 0 and is | |
1225 // not marked as persistent resource | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1226 if (node->refcount == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1227 (node->flags & DMF_PERSIST) == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1228 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW))) |
0 | 1229 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1230 if (((flags & DMPRUNE_ATIME) && stamp - node->atime >= agems) || |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1231 ((flags & DMPRUNE_MTIME) && stamp - node->mtime >= agems)) |
0 | 1232 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1233 dmResourceFreeResData(node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1234 dmResourceFreeRawData(node); |
0 | 1235 } |
1236 } | |
1237 } | |
1238 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1239 dmMutexUnlock(lib->mutex); |
0 | 1240 } |
1241 | |
1242 | |
1243 /* Helper resource access routines | |
1244 */ | |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
439
diff
changeset
|
1245 int dmf_read_str(DMResource *f, void *s, size_t l) |
0 | 1246 { |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
439
diff
changeset
|
1247 return dmfread(s, 1, l, f) == l; |
0 | 1248 } |
1249 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1250 BOOL dmf_read_byte(DMResource *f, Uint8 *val) |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1251 { |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1252 int tmp = dmfgetc(f); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1253 *val = tmp; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1254 return tmp != EOF; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1255 } |
0 | 1256 |
1257 #define DM_DEFINE_FUNC(xname, xtype, xmacro) \ | |
1258 BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \ | |
1259 xtype result; \ | |
1260 if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \ | |
1261 return FALSE; \ | |
1262 *v = DM_ ## xmacro ## _TO_NATIVE (result); \ | |
1263 return TRUE; \ | |
1264 } | |
1265 | |
1266 DM_DEFINE_FUNC(le16, Uint16, LE16) | |
1267 DM_DEFINE_FUNC(le32, Uint32, LE32) | |
1268 | |
1269 DM_DEFINE_FUNC(be16, Uint16, BE16) | |
1270 DM_DEFINE_FUNC(be32, Uint32, BE32) | |
1271 | |
1272 DM_DEFINE_FUNC(le64, Uint64, LE64) | |
1273 DM_DEFINE_FUNC(be64, Uint64, BE64) |