summaryrefslogtreecommitdiff
path: root/hge/resource.cpp
blob: 4937ee1fecdfb8bbd2f5034412f7eef8ae8741e9 (plain) (blame)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** Core functions implementation: resources management
*/

#include "hge_impl.h"

#include <zlib.h>  // the system version is better here. HGE's is out of date.

#define NOCRYPT
//#define NOUNCRYPT
#include "ZLIB/unzip.h"


bool CALL HGE_Impl::Resource_AttachPack(const char *filename, const char *password)
{
	char *szName;
	CResourceList *resItem=res;
	unzFile zip;

	szName=Resource_MakePath(filename);

	while(resItem)
	{
		if(!strcmp(szName,resItem->filename)) return false;
		resItem=resItem->next;
	}

	zip=unzOpen(szName);
	if(!zip) {
		System_Log("Unable to unzip: %s", szName);
		return false;
	}
	unzClose(zip);

	resItem=new CResourceList;
	strcpy(resItem->filename, szName);
	if(password) strcpy(resItem->password, password);
	else resItem->password[0]=0;
	resItem->next=res;
	res=resItem;

	return true;
}

void CALL HGE_Impl::Resource_RemovePack(const char *filename)
{
	char *szName;
	CResourceList *resItem=res, *resPrev=0;

	szName=Resource_MakePath(filename);

	while(resItem)
	{
		if(!strcmp(szName,resItem->filename))
		{
			if(resPrev) resPrev->next=resItem->next;
			else res=resItem->next;
			delete resItem;
			break;
		}

		resPrev=resItem;
		resItem=resItem->next;
	}
}

void CALL HGE_Impl::Resource_RemoveAllPacks()
{
	CResourceList *resItem=res, *resNextItem;

	while(resItem)
	{
		resNextItem=resItem->next;
		delete resItem;
		resItem=resNextItem;
	}

	res=0;
}

void* CALL HGE_Impl::Resource_Load(const char *filename, DWORD *size)
{
	const char *res_err="Can't load resource: %s";

	CResourceList *resItem=res;
	char szName[_MAX_PATH];
	char szZipName[_MAX_PATH];
	unzFile zip;
	unz_file_info file_info;
	int done, i;
	void *ptr;
	FILE *hF;

	if(filename[0]=='\\' || filename[0]=='/' || filename[1]==':') goto _fromfile; // skip absolute paths

	// Load from pack

	strcpy(szName,filename);
	for(i=0; szName[i]; i++) { if(szName[i]=='/') szName[i]='\\'; }

	while(resItem)
	{
		zip=unzOpen(resItem->filename);
		done=unzGoToFirstFile(zip);
		while(done==UNZ_OK)
		{
			unzGetCurrentFileInfo(zip, &file_info, szZipName, sizeof(szZipName), NULL, 0, NULL, 0);
			for(i=0; szZipName[i]; i++)	{ if(szZipName[i]=='/') szZipName[i]='\\'; }
			if(!strcmp(szName,szZipName))
			{
				if(unzOpenCurrentFilePassword(zip, resItem->password[0] ? resItem->password : 0) != UNZ_OK)
				{
					unzClose(zip);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}

				ptr = malloc(file_info.uncompressed_size);
				if(!ptr)
				{
					unzCloseCurrentFile(zip);
					unzClose(zip);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}

				if(unzReadCurrentFile(zip, ptr, file_info.uncompressed_size) < 0)
				{
					unzCloseCurrentFile(zip);
					unzClose(zip);
					free(ptr);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}
				unzCloseCurrentFile(zip);
				unzClose(zip);
				if(size) *size=file_info.uncompressed_size;
				return ptr;
			}

			done=unzGoToNextFile(zip);
		}

		unzClose(zip);
		resItem=resItem->next;
	}

	// Load from file
_fromfile:

	hF = fopen(Resource_MakePath(filename), "rb");
	if(hF == NULL)
	{
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}

	struct stat statbuf;
	if (fstat(fileno(hF), &statbuf) == -1)
	{
		fclose(hF);
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}

	file_info.uncompressed_size = statbuf.st_size;
	ptr = malloc(file_info.uncompressed_size);
	if(!ptr)
	{
		fclose(hF);
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}
	if(fread(ptr, file_info.uncompressed_size, 1, hF) != 1)
	{
		fclose(hF);
		free(ptr);
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}

	fclose(hF);

	if(size) *size=file_info.uncompressed_size;
	return ptr;
}


void CALL HGE_Impl::Resource_Free(void *res)
{
	if(res) free(res);
}

// this is from PhysicsFS originally ( http://icculus.org/physfs/ )
//  (also zlib-licensed.)
static int locateOneElement(char *buf)
{
	char *ptr = NULL;
	DIR *dirp = NULL;
	struct dirent *dent = NULL;

	if (access(buf, F_OK) == 0)
		return 1;  /* quick rejection: exists in current case. */

	ptr = strrchr(buf, '/');  /* find entry at end of path. */
	if (ptr == NULL)
	{
		dirp = opendir(".");
		ptr = buf;
	}
	else
	{
		*ptr = '\0';
		dirp = opendir(buf);
		*ptr = '/';
		ptr++;  /* point past dirsep to entry itself. */
	}

	while ((dent = readdir(dirp)) != NULL)
	{
		if (strcasecmp(dent->d_name, ptr) == 0)
		{
			strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
			closedir(dirp);
			return 1;
		}
	}

	/* no match at all... */
	closedir(dirp);
	return 0;
}

static int locateCorrectCase(char *buf)
{
	char *ptr = buf;

	while ((ptr = strchr(ptr + 1, '/')))
	{
		*ptr = '\0';  /* block this path section off */
		if (!locateOneElement(buf))
		{
			*ptr = '/'; /* restore path separator */
			return -2;  /* missing element in path. */
		}
		*ptr = '/'; /* restore path separator */
	}

	/* check final element... */
	return locateOneElement(buf) ? 0 : -1;
}

char* CALL HGE_Impl::Resource_MakePath(const char *filename)
{
	int i;

	if(!filename)
		strcpy(szTmpFilename, szAppPath);
	else if(filename[0]=='\\' || filename[0]=='/' || filename[1]==':')
		strcpy(szTmpFilename, filename);
	else
	{
		strcpy(szTmpFilename, szAppPath);
		if(filename) strcat(szTmpFilename, filename);
	}

	for(i=0; szTmpFilename[i]; i++) { if(szTmpFilename[i]=='\\') szTmpFilename[i]='/'; }

	locateCorrectCase(szTmpFilename);

	return szTmpFilename;
}

// !!! FIXME: kinda messy, and probably doesn't get all the corner cases right.
bool HGE_Impl::_WildcardMatch(const char *str, const char *wildcard)
{
	if ((str == NULL) || (wildcard == NULL))
		return false;

	while ((*str) && (*wildcard))
	{
		const char wildch = *wildcard;
		const char strch = *str;
		if (wildch == '?')
			; // okay.
		else if (wildch == '*')
		{
			do {
				wildcard++;
			} while (((*wildcard == '*') || (*wildcard == '?')) && (*wildcard != '\0'));
			const char newwild = *wildcard;
			if (newwild == '\0') return true;
			const char *ptr = str;
			while (*ptr)  // find the greediest match possible...
			{
				if (*ptr == newwild)
					str = ptr;
				ptr++;
			}
		}
		else if ( (toupper(strch)) != (toupper(wildch)) )
		{
			return false;
		}

		str++;
		wildcard++;
	}

	while (*wildcard == '*')
		wildcard++;

	return ((*str == '\0') && (*wildcard == '\0'));
}

bool HGE_Impl::_PrepareFileEnum(const char *wildcard)
{
	if(hSearch) { closedir(hSearch); hSearch=0; }
	char *madepath = Resource_MakePath(wildcard);
	const char *fname = strrchr(madepath, '/');
	const char *dir = NULL;
	if (fname == NULL) {
		dir = ".";
		fname = madepath;
	} else {
		dir = madepath;
		char *ptr = (char *) fname;
		*ptr = '\0';  // split dir and filename.
		fname++;
	}

	strcpy(szSearchDir, dir);
	strcpy(szSearchWildcard, fname);

	hSearch=opendir(dir);
	return (hSearch!=0);
}

char *HGE_Impl::_DoEnumIteration(const bool wantdir)
{
	if(!hSearch) return 0;
	while (true)
	{
		struct dirent *dent = readdir(hSearch);
		if(dent == NULL) { closedir(hSearch); hSearch=0; return 0; }
		if ((strcmp(dent->d_name, ".") == 0) || (strcmp(dent->d_name, "..") == 0))
			continue;
		if (!_WildcardMatch(dent->d_name, szSearchWildcard))
			continue;
		char fullpath[_MAX_PATH];
		snprintf(fullpath, sizeof (fullpath), "%s/%s", szSearchDir, dent->d_name);
		struct stat statbuf;
		if (stat(fullpath, &statbuf) == -1)  // this follows symlinks.
			continue;
		const bool isdir = ((S_ISDIR(statbuf.st_mode)) != 0);
		if (isdir == wantdir)  // this treats pipes, devs, etc, as "files" ...
		{
			strcpy(szSearchResult, dent->d_name);
			return szSearchResult;
		}
	}
	return 0;
}

char* CALL HGE_Impl::Resource_EnumFiles(const char *wildcard)
{
	if(wildcard)
	{
		if (!_PrepareFileEnum(wildcard))
			return 0;
	}
	return _DoEnumIteration(false);
}

char* CALL HGE_Impl::Resource_EnumFolders(const char *wildcard)
{
	if(wildcard)
	{
		if (!_PrepareFileEnum(wildcard))
			return 0;
	}
	return _DoEnumIteration(true);
}