Mercurial > hg > dmlib
annotate dmres.h @ 731:0e164aaa7409
Work on getting the resource subsystem working again ..
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 21 Apr 2013 19:46:52 +0300 |
parents | 3d813c81f33c |
children | 2f02d9b4caa3 |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2011-2012 Tecnic Software productions (TNSP) | |
6 */ | |
7 #ifndef DMRES_H | |
8 #define DMRES_H | |
9 | |
10 #include "dmlib.h" | |
11 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
12 #ifdef DM_USE_PACKFS |
0 | 13 #include "dmpack.h" |
14 #endif | |
15 | |
16 #ifdef __cplusplus | |
17 extern "C" { | |
18 #endif | |
19 | |
20 | |
21 /* Constants | |
22 */ | |
23 enum | |
24 { | |
25 DMPRUNE_ATIME = 0x0001, | |
26 DMPRUNE_MTIME = 0x0002, | |
27 }; | |
28 | |
29 enum | |
30 { | |
31 DRF_USE_PACK = 0x0001, | |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
32 DRF_USE_STDIO = 0x0002, |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
33 DRF_PRELOAD_RAW = 0x0004, |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
34 DRF_PRELOAD_RES = 0x0008, |
0 | 35 }; |
36 | |
37 enum | |
38 { | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
39 DMF_PERSIST = 0x0001, // Persist loaded RAW resource |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
40 DMF_TEMPORARY = 0x0002, |
731
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
41 DMF_UNALLOCATED = 0x0004, // The raw data is not allocated, so do not free it |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
42 DMF_LOADED_RAW = 0x1000, // Raw data has been loaded |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
43 DMF_LOADED_RES = 0x2000, // Resource has been loaded |
0 | 44 }; |
45 | |
46 | |
47 /* Typedefs and structures | |
48 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
49 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
50 struct DMResourceDataOps; |
0 | 51 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
52 struct DMResource; |
0 | 53 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
54 typedef struct DMResource |
0 | 55 { |
56 // Timestamps (in seconds from time()) | |
57 int mtime, // When resource was loaded | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
58 atime; // Last accessed (dmResourceRef()/unref) |
0 | 59 int refcount; // Reference count |
60 | |
61 int flags; // Resource flags (DMF_*) | |
62 char *filename; | |
63 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
64 // Raw data (or mem data) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
65 size_t rawSize; // Size of data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
66 off_t rawOffset; // Current offset in data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
67 Uint8 *rawData; // Pointer to data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
68 |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
69 // Decoded resource data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
70 void *resData; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
71 size_t resSize; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
72 struct DMResourceDataOps *rops; |
0 | 73 |
74 int error; // Error code | |
75 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
76 #ifdef DM_USE_STDIO |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
77 FILE * fh; // File handle for stdio |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
78 #endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
79 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
80 struct DMResourceOps *fops; // Pointer to file handling functions struct |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
81 struct DMResourceLib *lib; // Pointer to the resource library |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
82 struct DMResource *next, *prev; |
0 | 83 } DMResource; |
84 | |
85 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
86 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
87 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
88 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
89 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
90 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
91 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
92 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
93 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
94 typedef struct DMResourceLib |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
95 { |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
96 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
97 char *resPath; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
98 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
99 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
100 DMMutex *mutex; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
101 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
102 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
104 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
105 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 } DMResourceLib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
107 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
108 |
0 | 109 typedef struct DMResourceOps |
110 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
111 char *name; |
0 | 112 int (*ferror)(DMResource *); |
113 int (*fseek)(DMResource *, const off_t, const int); | |
114 off_t (*fsize)(DMResource *); | |
115 off_t (*ftell)(DMResource *); | |
116 BOOL (*feof)(DMResource *); | |
117 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
118 int (*fputc)(int, DMResource *); |
0 | 119 size_t (*fread)(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
120 size_t (*fwrite)(void *, const size_t, const size_t, DMResource *); |
0 | 121 |
122 int (*fopen)(DMResource *); | |
123 void (*fclose)(DMResource *); | |
124 int (*preload)(DMResource *); | |
125 } DMResourceOps; | |
126 | |
127 | |
128 /* Functions | |
129 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
130 int dmResourcesInit(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
131 int dmResourcesClose(DMResourceLib *lib); |
0 | 132 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
133 void dmResourcesPrune(DMResourceLib *lib, const int agems, int const flags); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
134 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 135 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
136 DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
137 void dmResourceFree(DMResource *node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
138 void dmResourceInsert(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
139 void dmResourceDelete(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
140 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
141 int dmResourceRef(DMResource *); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
142 int dmResourceUnref(DMResource *); |
0 | 143 |
144 | |
145 // Opening and closing resources | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
146 int dmf_open(DMResourceLib *lib, const char *, DMResource **handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
147 int dmf_create_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len, DMResource **phandle); |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
148 #ifdef DM_USE_STDIO |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
149 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
150 int dmf_create_stdio_stream(FILE *, DMResource **phandle); |
0 | 151 #endif |
152 void dmf_close(DMResource *); | |
153 | |
154 | |
155 // Basic resource access functions | |
156 int dmferror(DMResource *); | |
157 int dmfseek(DMResource *, const off_t, const int); | |
158 off_t dmfsize(DMResource *); | |
159 off_t dmftell(DMResource *); | |
160 BOOL dmfeof(DMResource *); | |
161 int dmfgetc(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
162 int dmfputc(int, DMResource *); |
0 | 163 size_t dmfread(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
164 size_t dmfwrite(void *, const size_t, const size_t, DMResource *); |
235 | 165 char * dmfgets(char *s, int size, DMResource * f); |
0 | 166 |
167 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
168 // Helper functions for endianess based reading etc |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
169 int dmf_read_str(DMResource *, void *, size_t); |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
170 BOOL dmf_read_byte(DMResource *, Uint8 *); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
171 |
0 | 172 BOOL dmf_read_be16(DMResource *, Uint16 *); |
173 BOOL dmf_read_be32(DMResource *, Uint32 *); | |
174 BOOL dmf_read_le16(DMResource *, Uint16 *); | |
175 BOOL dmf_read_le32(DMResource *, Uint32 *); | |
176 | |
177 #ifdef DM_HAVE_64BIT | |
178 BOOL dmf_read_be64(DMResource *, Uint64 *); | |
179 BOOL dmf_read_le64(DMResource *, Uint64 *); | |
180 #endif | |
181 | |
182 | |
183 #ifdef __cplusplus | |
184 } | |
185 #endif | |
186 | |
187 #endif // DMRES_H |