summaryrefslogtreecommitdiff
path: root/src/libs/resource/getres.c
blob: 39e24a9d0f04ff005f8b00e4bb5b8d0473a5f7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "options.h"
#include "port.h"
#include "resintrn.h"
#include "libs/memlib.h"
#include "libs/log.h"
#include "libs/uio/charhashtable.h"

const char *_cur_resfile_name;
// When a file is being loaded, _cur_resfile_name is set to its name.
// At other times, it is NULL.

ResourceDesc *
lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res)
{
	return (ResourceDesc *) CharHashTable_find (idx->map, res);
}

void
loadResourceDesc (ResourceDesc *desc)
{
	desc->vtable->loadFun (desc->fname, &desc->resdata);
}

void *
LoadResourceFromPath (const char *path, ResourceLoadFileFun *loadFun)
{
	uio_Stream *stream;
	unsigned long dataLen;
	void *resdata;

	stream = res_OpenResFile (contentDir, path, "rb");
	if (stream == NULL)
	{
		log_add (log_Warning, "Warning: Can't open '%s'", path);
		return NULL;
	}

	dataLen = LengthResFile (stream);
	log_add (log_Info, "\t'%s' -- %lu bytes", path, dataLen);
	
	if (dataLen == 0)
	{
		log_add (log_Warning, "Warning: Trying to load empty file '%s'.", path);
		goto err;
	}

	_cur_resfile_name = path;
	resdata = (*loadFun) (stream, dataLen);
	_cur_resfile_name = NULL;
	res_CloseResFile (stream);

	return resdata;

err:
	res_CloseResFile (stream);
	return NULL;
}

const char *
res_GetResourceType (RESOURCE res)
{
	RESOURCE_INDEX resourceIndex;
	ResourceDesc *desc;
	
	if (res == NULL_RESOURCE)
	{
		log_add (log_Warning, "Trying to get type of null resource");
		return NULL;
	}
	
	resourceIndex = _get_current_index_header ();
	desc = lookupResourceDesc (resourceIndex, res);
	if (desc == NULL)
	{
		log_add (log_Warning, "Trying to get type of undefined resource '%s'",
				res);
		return NULL;
	}
	
	return desc->vtable->resType;
}
	

// Get a resource by its resource ID.
void *
res_GetResource (RESOURCE res)
{
	RESOURCE_INDEX resourceIndex;
	ResourceDesc *desc;
	
	if (res == NULL_RESOURCE)
	{
		log_add (log_Warning, "Trying to get null resource");
		return NULL;
	}
	
	resourceIndex = _get_current_index_header ();

	desc = lookupResourceDesc (resourceIndex, res);
	if (desc == NULL)
	{
		log_add (log_Warning, "Trying to get undefined resource '%s'",
				 res);
		return NULL;
	}

	if (desc->resdata.ptr == NULL)
		loadResourceDesc (desc);
	if (desc->resdata.ptr != NULL)
		++desc->refcount;

	return desc->resdata.ptr;
			// May still be NULL, if the load failed.
}

DWORD
res_GetIntResource (RESOURCE res)
{
	RESOURCE_INDEX resourceIndex;
	ResourceDesc *desc;
	
	if (res == NULL_RESOURCE)
	{
		log_add (log_Warning, "Trying to get null resource");
		return 0;
	}
	
	resourceIndex = _get_current_index_header ();

	desc = lookupResourceDesc (resourceIndex, res);
	if (desc == NULL)
	{
		log_add (log_Warning, "Trying to get undefined resource '%s'",
				 res);
		return 0;
	}

	return desc->resdata.num;
}

BOOLEAN
res_GetBooleanResource (RESOURCE res)
{
	return (res_GetIntResource (res) != 0);
}

// NB: this function appears to be never called!
void
res_FreeResource (RESOURCE res)
{
	ResourceDesc *desc;
	ResourceFreeFun *freeFun;

	desc = lookupResourceDesc (_get_current_index_header(), res);
	if (desc == NULL)
	{
		log_add (log_Debug, "Warning: trying to free an unrecognised "
				"resource.");
		return;
	}

	if (desc->refcount > 0)
		--desc->refcount;
	else
		log_add (log_Debug, "Warning: freeing an unreferenced resource.");
	if (desc->refcount > 0)
		return; // Still references left

	freeFun = desc->vtable->freeFun;
	if (freeFun == NULL)
	{
		log_add (log_Debug, "Warning: trying to free a non-heap resource.");
		return;
	}
	
	if (desc->resdata.ptr == NULL)
	{
		log_add (log_Debug, "Warning: trying to free not loaded "
				"resource.");
		return;
	}

	(*freeFun) (desc->resdata.ptr);
	desc->resdata.ptr = NULL;
}

// By calling this function the caller will be responsible of unloading
// the resource. If res_GetResource() get called again for this
// resource, a NEW copy will be loaded, regardless of whether a detached
// copy still exists.
void *
res_DetachResource (RESOURCE res)
{
	ResourceDesc *desc;
	ResourceFreeFun *freeFun;
	void *result;

	desc = lookupResourceDesc (_get_current_index_header(), res);
	if (desc == NULL)
	{
		log_add (log_Debug, "Warning: trying to detach from an unrecognised "
				"resource.");
		return NULL;
	}
	
	freeFun = desc->vtable->freeFun;
	if (freeFun == NULL)
	{
		log_add (log_Debug, "Warning: trying to detach from a non-heap resource.");
		return NULL;
	}
	
	if (desc->resdata.ptr == NULL)
	{
		log_add (log_Debug, "Warning: trying to detach from a not loaded "
				"resource.");
		return NULL;
	}

	if (desc->refcount > 1)
	{
		log_add (log_Debug, "Warning: trying to detach a resource referenced "
				"%u times", desc->refcount);
		return NULL;
	}

	result = desc->resdata.ptr;
	desc->resdata.ptr = NULL;
	desc->refcount = 0;

	return result;
}

BOOLEAN
FreeResourceData (void *data)
{
	HFree (data);
	return TRUE;
}