aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/base/base_file_manager.cpp
blob: 1cb0464a1fbee5939d6fe61393dc1ab6374884ca (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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#include "engines/wintermute/dcgf.h"
#include "engines/wintermute/base/base_file_manager.h"
#include "engines/wintermute/utils/string_util.h"
#include "engines/wintermute/utils/path_util.h"
#include "engines/wintermute/base/file/base_disk_file.h"
#include "engines/wintermute/base/file/base_save_thumb_file.h"
#include "engines/wintermute/base/file/base_file_entry.h"
#include "engines/wintermute/base/file/base_package.h"
#include "engines/wintermute/base/file/BPkgFile.h"
#include "engines/wintermute/base/file/base_resources.h"
#include "engines/wintermute/base/base_registry.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/dcpackage.h"
#include "engines/wintermute/utils/utils.h"
#include "engines/wintermute/platform_osystem.h"
#include "engines/wintermute/wintermute.h"
#include "common/str.h"
#include "common/textconsole.h"
#include "common/util.h"
#include "common/config-manager.h"
#include "common/system.h"
#include "common/fs.h"
#include "common/file.h"
#include "common/savefile.h"

namespace WinterMute {

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
CBFileManager::CBFileManager(CBGame *inGame): CBBase(inGame) {
	_basePath = NULL;

	initPaths();
	registerPackages();
}


//////////////////////////////////////////////////////////////////////
CBFileManager::~CBFileManager() {
	cleanup();
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::cleanup() {
	// delete registered paths
	for (uint32 i = 0; i < _singlePaths.size(); i++)
		delete [] _singlePaths[i];
	_singlePaths.clear();

	for (uint32 i = 0; i < _packagePaths.size(); i++)
		delete [] _packagePaths[i];
	_packagePaths.clear();


	// delete file entries
	_filesIter = _files.begin();
	while (_filesIter != _files.end()) {
		delete _filesIter->_value;
		_filesIter++;
	}
	_files.clear();

	// close open files
	for (uint32 i = 0; i < _openFiles.size(); i++) {
		delete _openFiles[i];
	}
	_openFiles.clear();


	// delete packages
	for (uint32 i = 0; i < _packages.size(); i++)
		delete _packages[i];
	_packages.clear();

	delete[] _basePath;
	_basePath = NULL;

	return STATUS_OK;
}



#define MAX_FILE_SIZE 10000000
//////////////////////////////////////////////////////////////////////
byte *CBFileManager::readWholeFile(const Common::String &filename, uint32 *size, bool mustExist) {
	byte *buffer = NULL;

	Common::SeekableReadStream *file = openFile(filename);
	if (!file) {
		if (mustExist) _gameRef->LOG(0, "Error opening file '%s'", filename.c_str());
		return NULL;
	}

	/*
	if (File->GetSize() > MAX_FILE_SIZE) {
	    _gameRef->LOG(0, "File '%s' exceeds the maximum size limit (%d bytes)", Filename, MAX_FILE_SIZE);
	    CloseFile(File);
	    return NULL;
	}
	*/


	buffer = new byte[file->size() + 1];
	if (buffer == NULL) {
		_gameRef->LOG(0, "Error allocating buffer for file '%s' (%d bytes)", filename.c_str(), file->size() + 1);
		closeFile(file);
		return NULL;
	}

	if (file->read(buffer, (uint32)file->size()) != (uint32)file->size()) {
		_gameRef->LOG(0, "Error reading file '%s'", filename.c_str());
		closeFile(file);
		delete [] buffer;
		return NULL;
	};

	buffer[file->size()] = '\0';
	if (size != NULL) *size = file->size();
	closeFile(file);

	return buffer;
}

Common::SeekableReadStream *CBFileManager::loadSaveGame(const Common::String &filename) {
	Common::SaveFileManager *saveMan = g_wintermute->getSaveFileMan();
	Common::InSaveFile *file = saveMan->openForLoading(filename);
	return file;
}

//////////////////////////////////////////////////////////////////////////
bool CBFileManager::saveFile(const Common::String &filename, byte *buffer, uint32 bufferSize, bool compressed, byte *prefixBuffer, uint32 prefixSize) {
	// TODO
	warning("Implement SaveFile");

	Common::SaveFileManager *saveMan = g_wintermute->getSaveFileMan();
	Common::OutSaveFile *file = saveMan->openForSaving(filename);
	file->write(prefixBuffer, prefixSize);
	file->write(buffer, bufferSize);
	file->finalize();
	delete file;
#if 0
	RestoreCurrentDir();

	CBUtils::CreatePath(filename, false);

	FILE *f = fopen(filename, "wb");
	if (!f) {
		_gameRef->LOG(0, "Error opening file '%s' for writing.", filename);
		return STATUS_FAILED;
	}

	if (PrefixBuffer && PrefixSize) {
		fwrite(PrefixBuffer, PrefixSize, 1, f);
	}

	if (Compressed) {
		uint32 CompSize = BufferSize + (BufferSize / 100) + 12; // 1% extra space
		byte *CompBuffer = new byte[CompSize];
		if (!CompBuffer) {
			_gameRef->LOG(0, "Error allocating compression buffer while saving '%s'", filename);
			Compressed = false;
		} else {
			if (compress(CompBuffer, (uLongf *)&CompSize, Buffer, BufferSize) == Z_OK) {
				uint32 magic = DCGF_MAGIC;
				fwrite(&magic, sizeof(uint32), 1, f);
				magic = COMPRESSED_FILE_MAGIC;
				fwrite(&magic, sizeof(uint32), 1, f);

				uint32 DataOffset = 5 * sizeof(uint32);
				fwrite(&DataOffset, sizeof(uint32), 1, f);

				fwrite(&CompSize, sizeof(uint32), 1, f);
				fwrite(&BufferSize, sizeof(uint32), 1, f);

				fwrite(CompBuffer, CompSize, 1, f);
			} else {
				_gameRef->LOG(0, "Error compressing data while saving '%s'", filename);
				Compressed = false;
			}

			delete [] CompBuffer;
		}
	}

	if (!Compressed) fwrite(Buffer, BufferSize, 1, f);

	fclose(f);
#endif
	return STATUS_OK;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::requestCD(int cd, char *packageFile, const char *filename) {
	// unmount all non-local packages
	for (uint32 i = 0; i < _packages.size(); i++) {
		if (_packages[i]->_cD > 0) _packages[i]->close();
	}


	return STATUS_FAILED;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::addPath(TPathType type, const Common::String &path) {
	if (path.c_str() == NULL || strlen(path.c_str()) < 1) return STATUS_FAILED;

	bool slashed = (path[path.size() - 1] == '\\' || path[path.size() - 1] == '/');

	char *buffer = new char [strlen(path.c_str()) + 1 + (slashed ? 0 : 1)];
	if (buffer == NULL) return STATUS_FAILED;

	strcpy(buffer, path.c_str());
	if (!slashed) strcat(buffer, "\\");
	//CBPlatform::strlwr(buffer);

	switch (type) {
	case PATH_SINGLE:
		_singlePaths.push_back(buffer);
		break;
	case PATH_PACKAGE:
		_packagePaths.push_back(buffer);
		break;
	}

	return STATUS_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CBFileManager::reloadPaths() {
	// delete registered paths
	for (uint32 i = 0; i < _singlePaths.size(); i++)
		delete [] _singlePaths[i];
	_singlePaths.clear();

	for (uint32 i = 0; i < _packagePaths.size(); i++)
		delete [] _packagePaths[i];
	_packagePaths.clear();

	return initPaths();
}


#define TEMP_BUFFER_SIZE 32768
//////////////////////////////////////////////////////////////////////////
bool CBFileManager::initPaths() {
	restoreCurrentDir();

	AnsiString pathList;
	int numPaths;

	// single files paths
	pathList = _gameRef->_registry->readString("Resource", "CustomPaths", "");
	numPaths = CBUtils::strNumEntries(pathList.c_str(), ';');

	for (int i = 0; i < numPaths; i++) {
		char *path = CBUtils::strEntry(i, pathList.c_str(), ';');
		if (path && strlen(path) > 0) {
			addPath(PATH_SINGLE, path);
		}
		delete[] path;
		path = NULL;
	}
	addPath(PATH_SINGLE, ".\\");


	// package files paths
	addPath(PATH_PACKAGE, "./");

	pathList = _gameRef->_registry->readString("Resource", "PackagePaths", "");
	numPaths = CBUtils::strNumEntries(pathList.c_str(), ';');

	for (int i = 0; i < numPaths; i++) {
		char *path = CBUtils::strEntry(i, pathList.c_str(), ';');
		if (path && strlen(path) > 0) {
			addPath(PATH_PACKAGE, path);
		}
		delete[] path;
		path = NULL;
	}
	addPath(PATH_PACKAGE, "data");

	return STATUS_OK;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::registerPackages() {
	restoreCurrentDir();

	_gameRef->LOG(0, "Scanning packages...");
	debugC(kWinterMuteDebugFileAccess, "Scanning packages");

	Common::ArchiveMemberList files;
	SearchMan.listMatchingMembers(files, "*.dcp");

	for (Common::ArchiveMemberList::iterator it = files.begin(); it != files.end(); it++) {
		registerPackage((*it)->getName().c_str());
	}
#if 0
	AnsiString extension = AnsiString(PACKAGE_EXTENSION);

	for (int i = 0; i < _packagePaths.getSize(); i++) {
		boost::filesystem::path absPath = boost::filesystem::syste_complete(_packagePaths[i]);

		//_gameRef->LOG(0, "Scanning: %s", absPath.string().c_str());
		//printf("Scanning: %s\n", absPath.string().c_str());

		if (!exists(absPath)) continue;

		// scan files
		boost::filesystem::directory_iterator endIter;
		for (boost::filesystem::directory_iterator dit(absPath); dit != endIter; ++dit) {
			if (!is_directory((*dit).status())) {
				AnsiString fileName = (*dit).path().string();

				if (!IsValidPackage(fileName)) continue;

				warning("%s", fileName.c_str());
				//printf("%s\n", fileName.c_str());
				if (!StringUtil::CompareNoCase(extension, PathUtil::GetExtension(fileName))) continue;
				warning("Registered");
				RegisterPackage(absPath.string().c_str(), dit->path().filename().string().c_str());
			}
		}
	}
#endif
	debugC(kWinterMuteDebugFileAccess, "  Registered %d files in %d package(s)", _files.size(), _packages.size());
	_gameRef->LOG(0, "  Registered %d files in %d package(s)", _files.size(), _packages.size());

	return STATUS_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CBFileManager::registerPackage(const Common::String &filename , bool searchSignature) {
//	FILE *f = fopen(filename, "rb");
	Common::File *package = new Common::File();
	package->open(filename);
	if (!package->isOpen()) {
		_gameRef->LOG(0, "  Error opening package file '%s'. Ignoring.", filename.c_str());
		return STATUS_OK;
	}

	uint32 absoluteOffset = 0;
	bool boundToExe = false;

	if (searchSignature) {
		uint32 offset;
		if (!findPackageSignature(package, &offset)) {
			delete package;
			return STATUS_OK;
		} else {
			package->seek(offset, SEEK_SET);
			absoluteOffset = offset;
			boundToExe = true;
		}
	}

	TPackageHeader hdr;
	hdr.readFromStream(package);
//	package->read(&hdr, sizeof(TPackageHeader), 1, f);
	if (hdr.Magic1 != PACKAGE_MAGIC_1 || hdr.Magic2 != PACKAGE_MAGIC_2 || hdr.PackageVersion > PACKAGE_VERSION) {
		_gameRef->LOG(0, "  Invalid header in package file '%s'. Ignoring.", filename.c_str());
		delete package;
		return STATUS_OK;
	}

	if (hdr.PackageVersion != PACKAGE_VERSION) {
		_gameRef->LOG(0, "  Warning: package file '%s' is outdated.", filename.c_str());
	}

	// new in v2
	if (hdr.PackageVersion == PACKAGE_VERSION) {
		uint32 dirOffset;
		dirOffset = package->readUint32LE();
		dirOffset += absoluteOffset;
		package->seek(dirOffset, SEEK_SET);
	}

	for (uint32 i = 0; i < hdr.NumDirs; i++) {
		CBPackage *pkg = new CBPackage(_gameRef);
		if (!pkg) return STATUS_FAILED;

		pkg->_boundToExe = boundToExe;

		// read package info
		byte nameLength = package->readByte();
		pkg->_name = new char[nameLength];
		package->read(pkg->_name, nameLength);
		pkg->_cD = package->readByte();
		pkg->_priority = hdr.Priority;

		if (!hdr.MasterIndex) pkg->_cD = 0; // override CD to fixed disk
		_packages.push_back(pkg);


		// read file entries
		uint32 NumFiles = package->readUint32LE();

		for (uint32 j = 0; j < NumFiles; j++) {
			char *name;
			uint32 offset, length, compLength, flags, timeDate1, timeDate2;

			nameLength = package->readByte();
			name = new char[nameLength];
			package->read(name, nameLength);

			// v2 - xor name
			if (hdr.PackageVersion == PACKAGE_VERSION) {
				for (int k = 0; k < nameLength; k++) {
					((byte *)name)[k] ^= 'D';
				}
			}

			// some old version of ProjectMan writes invalid directory entries
			// so at least prevent strupr from corrupting memory
			name[nameLength - 1] = '\0';


			CBPlatform::strupr(name);

			offset = package->readUint32LE();
			offset += absoluteOffset;
			length = package->readUint32LE();
			compLength = package->readUint32LE();
			flags = package->readUint32LE();

			if (hdr.PackageVersion == PACKAGE_VERSION) {
				timeDate1 = package->readUint32LE();
				timeDate2 = package->readUint32LE();
			}
			_filesIter = _files.find(name);
			if (_filesIter == _files.end()) {
				CBFileEntry *file = new CBFileEntry(_gameRef);
				file->_package = pkg;
				file->_offset = offset;
				file->_length = length;
				file->_compressedLength = compLength;
				file->_flags = flags;

				_files[name] = file;
			} else {
				// current package has lower CD number or higher priority, than the registered
				if (pkg->_cD < _filesIter->_value->_package->_cD || pkg->_priority > _filesIter->_value->_package->_priority) {
					_filesIter->_value->_package = pkg;
					_filesIter->_value->_offset = offset;
					_filesIter->_value->_length = length;
					_filesIter->_value->_compressedLength = compLength;
					_filesIter->_value->_flags = flags;
				}
			}
			delete [] name;
		}
	}


	delete package;
	return STATUS_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CBFileManager::isValidPackage(const AnsiString &fileName) const {
	AnsiString plainName = PathUtil::getFileNameWithoutExtension(fileName);

	// check for device-type specific packages
	if (StringUtil::startsWith(plainName, "xdevice_", true)) {
		return StringUtil::compareNoCase(plainName, "xdevice_" + _gameRef->getDeviceType());
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
Common::File *CBFileManager::openPackage(const Common::String &name) {
	//TODO: Is it really necessary to do this when we have the ScummVM-system?

	//RestoreCurrentDir();

	Common::File *ret = new Common::File();
	char filename[MAX_PATH_LENGTH];

	for (uint32 i = 0; i < _packagePaths.size(); i++) {
		sprintf(filename, "%s%s.%s", _packagePaths[i], name.c_str(), PACKAGE_EXTENSION);
		ret->open(filename);
		if (ret->isOpen()) {
			return ret;
		}
	}

	sprintf(filename, "%s.%s", name.c_str(), PACKAGE_EXTENSION);
	ret->open(filename);
	if (ret->isOpen()) {
		return ret;
	}
	warning("CBFileManager::OpenPackage - Couldn't load file %s", name.c_str());
	delete ret;
	return NULL;
}


//////////////////////////////////////////////////////////////////////////
Common::File *CBFileManager::openSingleFile(const Common::String &name) {
	restoreCurrentDir();

	Common::File *ret = NULL;
	char filename[MAX_PATH_LENGTH];

	for (uint32 i = 0; i < _singlePaths.size(); i++) {
		sprintf(filename, "%s%s", _singlePaths[i], name.c_str());
		ret->open(filename);
		if (ret->isOpen())
			return ret;
	}

	// didn't find in search paths, try to open directly
	ret->open(name);
	if (ret->isOpen()) {
		return ret;
	} else {
		delete ret;
		return NULL;
	}
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::getFullPath(const Common::String &filename, char *fullname) {
	restoreCurrentDir();

	Common::File f;
	bool found = false;

	for (uint32 i = 0; i < _singlePaths.size(); i++) {
		sprintf(fullname, "%s%s", _singlePaths[i], filename.c_str());
		f.open(fullname);
		if (f.isOpen()) {
			f.close();
			found = true;
			break;
		}
	}

	if (!found) {
		f.open(filename.c_str());
		if (f.isOpen()) {
			f.close();
			found = true;
			strcpy(fullname, filename.c_str());
		}
	}

	return found;
}


//////////////////////////////////////////////////////////////////////////
CBFileEntry *CBFileManager::getPackageEntry(const Common::String &filename) {
	char *upc_name = new char[strlen(filename.c_str()) + 1];
	strcpy(upc_name, filename.c_str());
	CBPlatform::strupr(upc_name);

	CBFileEntry *ret = NULL;
	_filesIter = _files.find(upc_name);
	if (_filesIter != _files.end()) ret = _filesIter->_value;

	delete [] upc_name;

	return ret;
}

bool CBFileManager::hasFile(const Common::String &filename) {
	//TODO: Do this in a much simpler fashion
	Common::SeekableReadStream *stream = openFile(filename, true, false);
	if (!stream) {
		return false;
	}
	delete stream;
	return true;
}

//////////////////////////////////////////////////////////////////////////
Common::SeekableReadStream *CBFileManager::openFile(const Common::String &filename, bool absPathWarning, bool keepTrackOf) {
	if (strcmp(filename.c_str(), "") == 0) return NULL;
	//_gameRef->LOG(0, "open file: %s", filename);
	/*#ifdef __WIN32__
	    if (_gameRef->_debugDebugMode && _gameRef->_debugAbsolutePathWarning && AbsPathWarning) {
	        char Drive[_MAX_DRIVE];
	        _splitpath(filename, Drive, NULL, NULL, NULL);
	        if (Drive[0] != '\0') {
	            _gameRef->LOG(0, "WARNING: Referencing absolute path '%s'. The game will NOT work on another computer.", filename);
	        }
	    }
	#endif*/

	Common::SeekableReadStream *file = openFileRaw(filename);
	if (file && keepTrackOf) _openFiles.push_back(file);
	return file;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::closeFile(Common::SeekableReadStream *File) {
	for (uint32 i = 0; i < _openFiles.size(); i++) {
		if (_openFiles[i] == File) {
			delete _openFiles[i];
			_openFiles.remove_at(i);
			return STATUS_OK;
		}
	}
	return STATUS_FAILED;
}


//////////////////////////////////////////////////////////////////////////
Common::SeekableReadStream *CBFileManager::openFileRaw(const Common::String &filename) {
	restoreCurrentDir();

	Common::SeekableReadStream *ret = NULL;

	if (scumm_strnicmp(filename.c_str(), "savegame:", 9) == 0) {
		CBSaveThumbFile *SaveThumbFile = new CBSaveThumbFile(_gameRef);
		if (DID_SUCCEED(SaveThumbFile->open(filename))) {
			ret = SaveThumbFile->getMemStream();
		} 
		delete SaveThumbFile;
		return ret;
	}



	ret = openDiskFile(filename, this);
	if (ret) return ret;

	ret = openPkgFile(filename, this);
	if (ret) return ret;

	ret = CBResources::getFile(filename);
	if (ret) return ret;

	warning("BFileManager::OpenFileRaw - Failed to open %s", filename.c_str());
	return NULL;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::restoreCurrentDir() {
	if (!_basePath) return STATUS_OK;
	else {
		/*if (!chdir(_basePath)) return STATUS_OK;
		else return STATUS_FAILED;*/
		warning("CBFileManager::RestoreCurrentDir - ignored");
		return STATUS_OK;
	}
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::setBasePath(const Common::String &path) {
	cleanup();

	if (path.c_str()) {
		_basePath = new char[path.size() + 1];
		strcpy(_basePath, path.c_str());
	}

	initPaths();
	registerPackages();

	return STATUS_OK;
}


//////////////////////////////////////////////////////////////////////////
bool CBFileManager::findPackageSignature(Common::File *f, uint32 *offset) {
	byte buf[32768];

	byte signature[8];
	((uint32 *)signature)[0] = PACKAGE_MAGIC_1;
	((uint32 *)signature)[1] = PACKAGE_MAGIC_2;

	uint32 fileSize = (uint32)f->size();
	uint32 startPos = 1024 * 1024;
	uint32 bytesRead = startPos;

	while (bytesRead < fileSize - 16) {
		uint32 toRead = MIN((unsigned int)32768, fileSize - bytesRead);
		f->seek((int32)startPos, SEEK_SET);
		uint32 actuallyRead = f->read(buf, toRead);
		if (actuallyRead != toRead) return false;

		for (uint32 i = 0; i < toRead - 8; i++)
			if (!memcmp(buf + i, signature, 8)) {
				*offset =  startPos + i;
				return true;
			}

		bytesRead = bytesRead + toRead - 16;
		startPos = startPos + toRead - 16;

	}
	return false;

}

} // end of namespace WinterMute