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
|
|
12 #ifdef DMRES_PACKFS
|
|
13 #include <zlib.h>
|
|
14 #include "dmpack.h"
|
|
15 #endif
|
|
16
|
|
17 #ifdef __cplusplus
|
|
18 extern "C" {
|
|
19 #endif
|
|
20
|
|
21
|
|
22 /* Constants
|
|
23 */
|
|
24 enum
|
|
25 {
|
|
26 DMPRUNE_ATIME = 0x0001,
|
|
27 DMPRUNE_MTIME = 0x0002,
|
|
28 };
|
|
29
|
|
30 enum
|
|
31 {
|
|
32 DRF_USE_PACK = 0x0001,
|
|
33 };
|
|
34
|
|
35 enum
|
|
36 {
|
|
37 DMF_PRELOAD = 0x0001, // Preload this resource
|
|
38 DMF_PERSIST = 0x0002, // Persist loaded resource (only freed at shutdown/explicit prune)
|
|
39 //DMF_STREAM = 0x0004, // This resource is streamed
|
|
40
|
|
41 DMF_LOADED = 0x1000, // Resource has been loaded
|
|
42 };
|
|
43
|
|
44
|
|
45 /* Typedefs and structures
|
|
46 */
|
|
47 struct DMResourceOps;
|
|
48
|
|
49 typedef struct _DMResource
|
|
50 {
|
|
51 // Timestamps (in seconds from time())
|
|
52 int mtime, // When resource was loaded
|
|
53 atime; // Last accessed (dmres_ref()/unref)
|
|
54 int refcount; // Reference count
|
|
55
|
|
56 int flags; // Resource flags (DMF_*)
|
|
57 char *filename;
|
|
58
|
|
59 size_t dataSize; // Size of data
|
|
60 off_t dataOffset; // Current offset in data
|
|
61 Uint8 *data; // Pointer to data
|
|
62
|
|
63 int error; // Error code
|
|
64
|
|
65 struct DMResourceOps *fops; // Pointer to file handling functions struct
|
|
66 struct _DMResource *next, *prev;
|
|
67
|
|
68 FILE * fh;
|
|
69 } DMResource;
|
|
70
|
|
71
|
|
72 typedef struct DMResourceOps
|
|
73 {
|
|
74 int (*ferror)(DMResource *);
|
|
75 int (*fseek)(DMResource *, const off_t, const int);
|
|
76 off_t (*fsize)(DMResource *);
|
|
77 off_t (*ftell)(DMResource *);
|
|
78 BOOL (*feof)(DMResource *);
|
|
79 int (*fgetc)(DMResource *);
|
|
80 size_t (*fread)(void *, const size_t, const size_t, DMResource *);
|
|
81
|
|
82 int (*fopen)(DMResource *);
|
|
83 void (*fclose)(DMResource *);
|
|
84 int (*preload)(DMResource *);
|
|
85 } DMResourceOps;
|
|
86
|
|
87
|
|
88 /* Functions
|
|
89 */
|
|
90 int dmres_init(const char *filename, const char *path, int flags);
|
|
91 void dmres_close(void);
|
|
92
|
|
93 void dmres_prune(int agems, int flags);
|
|
94 int dmres_preload();
|
|
95
|
|
96 int dmres_load_resfile(const char *filename);
|
|
97 int dmres_write_resfile(const char *filename);
|
|
98
|
|
99 DMResource * dmres_find(const char *filename);
|
|
100 int dmres_ref(DMResource *);
|
|
101 int dmres_unref(DMResource *);
|
|
102
|
|
103
|
|
104 // Opening and closing resources
|
|
105 DMResource * dmf_open(const char *);
|
|
106 DMResource * dmf_open_memio(const char *, Uint8 *buf, size_t len);
|
|
107 #ifdef DMRES_STDIO
|
|
108 DMResource * dmf_create_stdio(const char *);
|
|
109 DMResource * dmf_create_stdio_stream(FILE *);
|
|
110 #endif
|
|
111 void dmf_close(DMResource *);
|
|
112
|
|
113
|
|
114 // Basic resource access functions
|
|
115 int dmferror(DMResource *);
|
|
116 int dmfseek(DMResource *, const off_t, const int);
|
|
117 off_t dmfsize(DMResource *);
|
|
118 off_t dmftell(DMResource *);
|
|
119 BOOL dmfeof(DMResource *);
|
|
120 int dmfgetc(DMResource *);
|
|
121 size_t dmfread(void *, const size_t, const size_t, DMResource *);
|
|
122
|
|
123
|
|
124 // Specialized functions for endianess based reading etc
|
|
125 int dmf_read_str(DMResource *, Uint8 *, size_t);
|
|
126 BOOL dmf_read_be16(DMResource *, Uint16 *);
|
|
127 BOOL dmf_read_be32(DMResource *, Uint32 *);
|
|
128 BOOL dmf_read_le16(DMResource *, Uint16 *);
|
|
129 BOOL dmf_read_le32(DMResource *, Uint32 *);
|
|
130
|
|
131 #ifdef DM_HAVE_64BIT
|
|
132 BOOL dmf_read_be64(DMResource *, Uint64 *);
|
|
133 BOOL dmf_read_le64(DMResource *, Uint64 *);
|
|
134 #endif
|
|
135
|
|
136
|
|
137 #ifdef __cplusplus
|
|
138 }
|
|
139 #endif
|
|
140
|
|
141 #endif // DMRES_H
|