summaryrefslogtreecommitdiff
path: root/src/T2DLL/CResFile.cpp
blob: 29b461ba945b36f8b6d825c03da8f8c5ab86de72 (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "StdAfx.h"
#define ATOI_KLUDGE
#include "CPEFile.h"
#include "CResFile.h"
#include "GlobalFunc.h"

CResFile::CResFile() {
	mBuffer = NULL;
	mPos = NULL;
	mIsBinaryMode = false;
	mIsSubPlugin = false;
}

/*virtual*/ CResFile::~CResFile() {
	Reset();
}

BOOL CResFile::OpenResource(HINSTANCE inst, int name, int type) {
	return OpenResource(inst, MAKEINTATOM(name), type);
}

BOOL CResFile::OpenResource(HINSTANCE inst, const char* name, int type) {
	char buf[5], a;
	*((int *) &buf[0]) = type;

	a = buf[0];
	buf[0] = buf[3];
	buf[3] = a;

	a = buf[1];
	buf[1] = buf[2];
	buf[2] = a;

	buf[4] = 0;

	return OpenResource(inst, name, buf);
}

BOOL CResFile::OpenResource(HINSTANCE inst, int name, const char* type) {
	return OpenResource(inst, MAKEINTATOM(name), type);
}

BOOL CResFile::OpenResource(HINSTANCE inst, const char* name, const char* type) {
	HRSRC h = FindResource(inst, name, type);
	if (!h) {
		CString theNameString;
		if (((DWORD) name) < 0x10000)
			theNameString.Format("(%d)", ((DWORD) name) & 0xFFFF);
		else
			theNameString = name;

		CString theTypeString;
		if (((DWORD) type) < 0x10000)
			theTypeString.Format("(%d)", ((DWORD) type) & 0xFFFF);
		else
			theTypeString = type;

		CString str = "CResFile::OpenResource ERROR : " + theNameString + "," + theTypeString + " @" + GetModuleName(inst) + "\n";
		OutputDebugString(str);

		return false;
	}

	return OpenResource(inst, h);
}

BOOL CResFile::OpenResource(HINSTANCE inst, HRSRC rsrc) {
	if (mBuffer)
		Reset();

	mInstance = inst;
	mRsrc = rsrc;

	HANDLE theHandle = LoadResource(inst, rsrc);
	if (!theHandle)
		return false;

	void *theData = LockResource(theHandle);
	mRemaining = SizeofResource(inst, rsrc);
	mBuffer = (char *) malloc(mRemaining + 1);
	memcpy(mBuffer, theData, mRemaining);
	UnlockResource(theHandle);
	FreeResource(theHandle);

	SetupResource();

	return true;
}

BOOL CResFile::OpenResource(const char* path, int name, int type) {
	CFile *file = new CFile;
	if (!file->Open(path, CFile::modeRead)) {
		delete file;
		return false;
	}

	char magic[2];
	file->Read(magic, 2);
	file->Close();
	delete file;

	if (memcmp(magic, "SP", 2) == 0) {
		SUBPLUGINFSENTRY entry;
		CFile *subPluginFile = OpenSubPluginFS(path, name, type, &entry);
		if (!subPluginFile)
			return false;

		mRemaining = entry.spfse_mC;
		mBuffer = (char *) malloc(mRemaining + 1);
		subPluginFile->Read(mBuffer, mRemaining);
		subPluginFile->Close();
		delete subPluginFile;

		mIsSubPlugin = true;
	} else if (memcmp(magic, "MZ", 2) == 0) {
		CPEFile pe;
		if (!pe.Open(path, CFile::modeRead))
			return false;

		char buf[8];
		buf[0] = ((char *) &type)[3];
		buf[1] = ((char *) &type)[2];
		buf[2] = ((char *) &type)[1];
		buf[3] = ((char *) &type)[0];
		buf[4] = 0;

		CString path;
#ifdef HACKS
		unsigned short langID = 1041;
		path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID);
		if (!pe.Seek(path)) {
            langID = 2057;
            path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID);
            if (!pe.Seek(path))
                return false;
        }
#else
        unsigned short langID = GetUserDefaultLangID();
        path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID);
        if (!pe.Seek(path))
            return false;
#endif

		mRemaining = pe.GetLength();
		mBuffer = (char *) malloc(mRemaining + 1);
		pe.Read(mBuffer, mRemaining);
		pe.Close();

		mIsSubPlugin = false;
	}

	SetupResource();
	return true;
}

void CResFile::SetupResource() {
	mBuffer[mRemaining] = 0;
	mPos = mBuffer;

	if (*mPos == 1) {
		mPos++;
		mRemaining--;
		mIsBinaryMode = true;
	} else {
		mIsBinaryMode = false;
	}
}

void CResFile::Reset() {
	if (mBuffer)
		free(mBuffer);

	mBuffer = NULL;
	mPos = NULL;
}

BOOL CResFile::Token(char* outStr) {
	while (*mPos == ' ' || *mPos == '\t' || *mPos == '\n' || *mPos == '\r' || *mPos == ',' || *mPos == '(') {
		if (*mPos == '(') {
			while (*mPos != ')')
				mPos++;
		}
		mPos++;
	}

	if (*mPos == '\0') {
#line 180
		_ASSERT(false);
		return false;
	}

	char *p = outStr;
	int len = 0;

	while (*mPos != ' ' && *mPos != '\t' && *mPos != '\n' && *mPos != '\r' && *mPos != ',' && *mPos != '(' && *mPos != '\0') {
		*p = *mPos;
		p++;
		mPos++;
		len++;
#line 192
		_ASSERT(len != 256);
	}

	*p = 0;
	return true;
}

BOOL CResFile::operator>>(unsigned char& out) {
#line 200
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 204
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((unsigned char *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		out = atoi(buf);
	}

	return true;
}

BOOL CResFile::operator>>(int& out) {
#line 222
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 226
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((int *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		out = atoi(buf);
	}

	return true;
}

BOOL CResFile::operator>>(short& out) {
#line 244
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 248
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((short *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		out = atoi(buf);
	}

	return true;
}

BOOL CResFile::operator>>(long& out) {
#line 266
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 270
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((long *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		out = atol(buf);
	}

	return true;
}

BOOL CResFile::operator>>(unsigned int& out) {
#line 288
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 292
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((unsigned int *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		sscanf(buf, "%u", &out);
	}

	return true;
}

BOOL CResFile::operator>>(unsigned long& out) {
#line 310
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 314
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((unsigned long *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		if (!Token(buf))
			return false;

		sscanf(buf, "%u", &out);
	}

	return true;
}

BOOL CResFile::operator>>(unsigned short& out) {
#line 332
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 336
		_ASSERT(mRemaining >= 4);
		mRemaining -= 4;
		out = *((unsigned short *) mPos);
		mPos += 4;
	} else {
		char buf[256];
		unsigned int val;
		if (!Token(buf))
			return false;

		sscanf(buf, "%u", &val);
		out = val;
	}

	return true;
}

BOOL CResFile::operator>>(POINT& outPoint) {
#line 356
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
		if (!(*this >> outPoint.y)) return false;
		if (!(*this >> outPoint.x)) return false;
	} else {
		if (!(*this >> outPoint.x)) return false;
		if (!(*this >> outPoint.y)) return false;
	}

	return true;
}

BOOL CResFile::operator>>(RECT& outRect) {
#line 377
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
		if (!(*this >> outRect.top)) return false;
		if (!(*this >> outRect.left)) return false;
		if (!(*this >> outRect.bottom)) return false;
		if (!(*this >> outRect.right)) return false;
	} else {
		if (!(*this >> outRect.left)) return false;
		if (!(*this >> outRect.top)) return false;
		if (!(*this >> outRect.right)) return false;
		if (!(*this >> outRect.bottom)) return false;
	}

	return true;
}

BOOL CResFile::operator>>(SIZE& outSize) {
#line 406
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
		if (!(*this >> outSize.cy)) return false;
		if (!(*this >> outSize.cx)) return false;
	} else {
		if (!(*this >> outSize.cx)) return false;
		if (!(*this >> outSize.cy)) return false;
	}

	return true;
}

BOOL CResFile::operator>>(char* outStr) {
#line 427
	_ASSERT(mBuffer);

	if (mIsBinaryMode) {
#line 431
		_ASSERT(mRemaining >= 1);
		mRemaining--;
		int len = mPos[0] + 1;
		mPos++;

#line 435
		_ASSERT(mRemaining >= len);
		mRemaining -= len;
		memcpy(outStr, mPos, len);
		mPos += len;
	} else {
		char buf[256];
		char *p = buf;

		while (*mPos == ' ' || *mPos == '\t' || *mPos == '\n' || *mPos == '\r' || *mPos == ',' || *mPos == '(') {
			if (*mPos == '(') {
				while (*mPos != ')')
					mPos++;
			}
			mPos++;
		}

		if (*mPos == '\0')
			return false;

		while (*mPos != '\n' && *mPos != '\r' && *mPos != '\0') {
			*p = *mPos;
			p++;
			mPos++;
		}

		*p = 0;
		strcpy(outStr, buf);
	}

	return true;
}

BOOL CResFile::operator>>(CString& outStr) {
#line 469
	_ASSERT(mBuffer);
	char buf[256];
	*this >> buf;
	outStr = buf;
	return true;
}

void CResFile::SkipDescriptor() {
	if (mIsBinaryMode) {
#line 481
		_ASSERT(mRemaining >= 1);
		mRemaining--;
		mPos++;
	}
}

void CResFile::End() {
#line 489
	_ASSERT(mRemaining == 0);
}