Subversion Repositories spk

Rev

Rev 85 | Rev 101 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
 
2
#include "VirtualFileSystem.h"
3
#include "File_IO.h"
4
#include "CatFile.h"
35 cycrow 5
#include "XspFile.h"
94 cycrow 6
#include "TextDB.h"
1 cycrow 7
 
79 cycrow 8
#include "Packages.h"
94 cycrow 9
#include "Utils/StringList.h"
79 cycrow 10
 
94 cycrow 11
#define DELETELIST(list) if ( list ) { list->MemoryClear(); delete list; }
12
 
35 cycrow 13
namespace SPK {
14
 
94 cycrow 15
CVirtualFileSystem::CVirtualFileSystem() : _lShields(NULL),
16
	_lLasers(NULL),
17
	_lMissiles(NULL),
18
	_lCockpits(NULL),
19
	_lComponentSections(NULL),
20
	_lDummySections(NULL),
21
	_lBodiesSections(NULL)
1 cycrow 22
{
23
	m_bLoaded = false;
35 cycrow 24
	m_pMap = new FileList;
25
	m_pModMap = new FileList;
26
 
27
	m_pTexts = new CTextDB();
28
	m_pModTexts = new CTextDB();
29
 
1 cycrow 30
	m_sAddon = "";
35 cycrow 31
 
32
	m_iLang = 0;
1 cycrow 33
}
34
 
35
CVirtualFileSystem::~CVirtualFileSystem(void)
36
{
35 cycrow 37
	if ( m_pMap )		delete m_pMap;
38
	if ( m_pModMap )	delete m_pModMap;
39
 
40
	if ( m_pTexts )		delete m_pTexts;
41
	if ( m_pModTexts )  delete m_pModTexts;
94 cycrow 42
 
43
	DELETELIST(_lShields);
44
	DELETELIST(_lLasers);
45
	DELETELIST(_lMissiles);
46
	DELETELIST(_lCockpits);
47
	DELETELIST(_lComponentSections);
48
	DELETELIST(_lDummySections);
49
	DELETELIST(_lBodiesSections);
1 cycrow 50
}
51
 
35 cycrow 52
void CVirtualFileSystem::setLanguage(int iLang)
1 cycrow 53
{
35 cycrow 54
	m_iLang = iLang;
55
}
56
 
94 cycrow 57
Utils::String CVirtualFileSystem::firstShield()
58
{
59
	if ( !_lShields || _lShields->empty() ) _updateShields();
60
	return _returnText(_lShields->First());
61
}
62
 
63
Utils::String CVirtualFileSystem::nextShield()
64
{
65
	return _returnText(_lShields->Next());
66
}
67
 
68
Utils::String CVirtualFileSystem::firstComponentSection()
69
{
70
	if ( !_lComponentSections || _lComponentSections->empty() ) _updateComponentSections();
71
	return _returnLine(_lComponentSections->First());
72
}
73
 
74
Utils::String CVirtualFileSystem::nextComponentSection()
75
{
76
	return _returnLine(_lComponentSections->Next());
77
}
78
 
79
Utils::String CVirtualFileSystem::firstDummySection()
80
{
81
	if ( !_lDummySections || _lDummySections->empty() ) _updateDummySections();
82
	return _returnLine(_lDummySections->First());
83
}
84
 
85
Utils::String CVirtualFileSystem::nextDummySection()
86
{
87
	return _returnLine(_lDummySections->Next());
88
}
89
 
90
Utils::String CVirtualFileSystem::firstBodiesSection()
91
{
92
	if ( !_lBodiesSections || _lBodiesSections->empty() ) _updateBodiesSections();
93
	return _returnLine(_lBodiesSections->First());
94
}
95
 
96
Utils::String CVirtualFileSystem::nextBodiesSection()
97
{
98
	return _returnLine(_lBodiesSections->Next());
99
}
100
 
101
std::pair<Utils::String, Utils::String> CVirtualFileSystem::nextLaser()
102
{
103
	return _returnPair(_lLasers->Next());
104
}
105
 
106
std::pair<Utils::String, Utils::String>  CVirtualFileSystem::firstLaser()
107
{
108
	if ( !_lLasers || _lLasers->empty() ) _updateLasers();
109
	return _returnPair(_lLasers->First());
110
}
111
 
112
std::pair<Utils::String, Utils::String>  CVirtualFileSystem::firstMissile()
113
{
114
	if ( !_lMissiles || _lMissiles->empty() ) _updateMissiles();
115
	return _returnPair(_lMissiles->First());
116
}
117
 
118
std::pair<Utils::String, Utils::String> CVirtualFileSystem::nextMissile()
119
{
120
	return _returnPair(_lMissiles->Next());
121
}
122
 
123
Utils::String CVirtualFileSystem::firstCockpit()
124
{
125
	if ( !_lCockpits || _lCockpits->empty() ) _updateCockpits();
126
	return _returnID(_lCockpits->First());
127
}
128
 
129
Utils::String CVirtualFileSystem::nextCockpit()
130
{
131
	return _returnID(_lCockpits->Next());
132
}
133
 
134
std::pair<Utils::String, Utils::String> CVirtualFileSystem::_returnPair(Utils::SStringList *s)
135
{
136
	if ( s && !s->data.empty() && !s->data.left(8).Compare("ReadText") ) return std::make_pair<Utils::String, Utils::String>(s->str, s->data);
137
	if ( s ) return std::make_pair<Utils::String, Utils::String>(s->str, s->str.token(";", -2));
138
	return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
139
}
140
 
141
Utils::String CVirtualFileSystem::_returnText(Utils::SStringList *s)
142
{
143
	if ( s && !s->data.empty() && !s->data.left(8).Compare("ReadText") ) return s->data;
144
	if ( s ) return s->str.token(";", -2);
145
	return Utils::String::Null();
146
}
147
 
148
Utils::String CVirtualFileSystem::_returnID(Utils::SStringList *s)
149
{
150
	if ( s ) return s->str.token(";", -2);
151
	return Utils::String::Null();
152
}
153
 
154
Utils::String CVirtualFileSystem::_returnLine(Utils::SStringList *s)
155
{
156
	if ( s ) return s->str;
157
	return Utils::String::Null();
158
}
159
 
160
 
35 cycrow 161
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, int maxPatch)
162
{
163
	return this->LoadFilesystem(dir, "", maxPatch);
164
}
165
 
166
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, const Utils::String &mod, int maxPatch)
167
{
1 cycrow 168
	m_sDir = dir;
169
 
35 cycrow 170
	this->_clear();
1 cycrow 171
 
172
	int number = 1;
52 cycrow 173
	while ( CFileIO(CyString(dir) + "/" + CyString::Number(number).PadNumber(2) + ".cat").ExistsOld() )
1 cycrow 174
	{
58 cycrow 175
		if ( maxPatch && maxPatch < number ) break;
35 cycrow 176
		CyString file = CyString(dir) + "/" + CyString::Number(number).PadNumber(2);
52 cycrow 177
		if ( !CFileIO(file + ".dat").ExistsOld() )
1 cycrow 178
			break;
179
 
180
		CCatFile cat;
181
		if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
182
		{
43 cycrow 183
			for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
184
				this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), CyString(file + ".cat").ToString(), m_pMap);
1 cycrow 185
				m_bLoaded = true;
186
			}
187
		}
188
		++number;
189
	}
190
 
43 cycrow 191
	// add all the files from the mod
35 cycrow 192
	if ( !mod.empty() )
193
		this->addMod(mod);
194
 
43 cycrow 195
	// now add all the extracted data
196
	this->_addDir(m_sDir, "");
197
 
1 cycrow 198
	return m_bLoaded;
199
}
200
 
43 cycrow 201
 
202
void CVirtualFileSystem::_addDir(const Utils::String &sStart, const Utils::String &sDir)
203
{
204
	CDirIO Dir(sStart + "/" + sDir);
205
	CyStringList *list = Dir.DirList();
206
	if ( list ) {
207
		for ( SStringList *strNode = list->Head(); strNode; strNode = strNode->next ) {
58 cycrow 208
			if ( CDirIO(Dir.Dir(strNode->str)).IsDir() ) _addDir(sStart, sDir + "/" + strNode->str.ToString());
43 cycrow 209
			else if ( CFileIO(strNode->str).CheckFileExtension("cat") ) continue;
210
			else if ( CFileIO(strNode->str).CheckFileExtension("dat") ) continue;
58 cycrow 211
			else this->_addFile(sDir + "/" + strNode->str.ToString(), Dir.File(strNode->str).ToString(), m_pMap);
43 cycrow 212
		}
213
	}
214
}
215
 
216
void CVirtualFileSystem::_addFile(const Utils::String &sFile, const Utils::String &sDest, FileList *pList)
217
{
85 cycrow 218
	(*pList)[sFile.asFilename().removeIf(0, '/').lower()] = sDest.findReplace("//", "/");
43 cycrow 219
}
220
void CVirtualFileSystem::_addModFile(const Utils::String &sFile, const Utils::String &sMod, FileList *pList)
221
{
222
	this->_addFile(sFile, sMod + "::" + sFile, pList);
223
}
224
 
35 cycrow 225
bool CVirtualFileSystem::loadMod(const Utils::String &mod)
1 cycrow 226
{
227
	bool loaded = false;
35 cycrow 228
 
229
	if ( !m_pModMap ) m_pModMap = new FileList;
230
 
1 cycrow 231
	CCatFile cat;
232
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
233
	{
43 cycrow 234
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
235
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pModMap);
35 cycrow 236
			loaded = true;
237
		}
238
	}
239
 
240
	return loaded;
241
}
242
 
243
bool CVirtualFileSystem::addMod(const Utils::String &mod)
244
{
245
	bool loaded = false;
246
 
247
	if ( !m_pMap ) m_pMap = new FileList;
248
 
249
	CCatFile cat;
250
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
251
	{
252
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
253
		{
43 cycrow 254
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pMap);
1 cycrow 255
			loaded = true;
256
		}
257
	}
258
 
259
	return loaded;
260
}
261
 
35 cycrow 262
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 263
{
94 cycrow 264
	if ( iLang <= 0 ) iLang = m_iLang;
265
 
35 cycrow 266
	if ( m_pTexts ) {
267
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
268
	}
269
	if ( m_pModTexts ) {
270
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
271
	}
272
 
273
	return false;
1 cycrow 274
}
275
 
35 cycrow 276
Utils::String CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 277
{
94 cycrow 278
	if ( iLang <= 0 ) iLang = m_iLang;
279
 
35 cycrow 280
	if ( m_pTexts ) {
281
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
282
	}
283
	if ( m_pModTexts ) {
284
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
285
	}
1 cycrow 286
 
35 cycrow 287
	return Utils::String("ReadText") + (long)iPage + "-" + (long)iID;
288
}
43 cycrow 289
 
290
bool CVirtualFileSystem::isFileAvailable(const Utils::String &file)
35 cycrow 291
{
43 cycrow 292
	return !this->_findFile(file).empty();
293
}
294
 
295
Utils::String CVirtualFileSystem::_findFile(const Utils::String &file)
296
{
297
	Utils::String toFile = file.findReplace("\\", "/").lower();
298
 
35 cycrow 299
	if ( m_pModMap && !m_pModMap->empty() ) {
300
		if ( !m_sAddon.empty() ) {
43 cycrow 301
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + "/" + toFile).GetFullFilename().ToLower().c_str());
302
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + "/" + toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 303
			if ( aitr != m_pModMap->end() ) return aitr->second;
304
		}
43 cycrow 305
		FileListItr itr = m_pModMap->find(CFileIO(toFile).GetFullFilename().ToLower().c_str());
306
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 307
		if ( itr != m_pModMap->end() ) return itr->second;
308
	}
309
	else if ( m_pMap && !m_pMap->empty() ) {
310
		if ( !m_sAddon.empty() ) {
58 cycrow 311
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
312
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + "/" + file)).fullFilename().lower().c_str());
35 cycrow 313
			if ( aitr != m_pMap->end() ) return aitr->second;
314
		}
315
		FileListItr itr = m_pMap->find(CFileIO(file).GetFullFilename().ToLower().c_str());
316
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).GetFullFilename().ToLower().c_str());
317
		if ( itr != m_pMap->end() ) return itr->second;
318
	}
319
	return "";
320
}
321
 
58 cycrow 322
Utils::String CVirtualFileSystem::_extractFromCat(const Utils::String &sCat, const Utils::String &sFile, const Utils::String &sTo)
35 cycrow 323
{
58 cycrow 324
	CCatFile catFile;
325
	if ( catFile.Open(sCat, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
326
	{
327
		// check for the file
328
		if ( catFile.ExtractFile(sFile, sTo) ) return sTo;
329
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR ) {
330
			if ( catFile.ExtractFile(sFile) ) return sFile;
331
		}
332
	}
333
 
334
	return "";
335
}
336
 
337
Utils::String CVirtualFileSystem::_extract(const Utils::String &sFile, const Utils::String &sTo)
338
{
339
	// check if we need to unpack the file
340
	if ( CCatFile::CheckPackedExtension(sFile) ) {
341
		CFileIO File(sFile);
342
		C_File f(CyString(File.fullFilename()));
343
		if ( !f.readFromFile(File) ) return "";
344
		if ( !f.UnPCKFile() ) return "";
345
		if ( !f.WriteToFile(sTo) ) return "";
346
		return sTo;
347
	}
348
	return sFile;
349
}
350
 
351
Utils::String CVirtualFileSystem::ExtractGameFile(const Utils::String &file, const Utils::String &to)
352
{
43 cycrow 353
	Utils::String sIn = _findFile(file);
354
	Utils::String sFile = file;
355
 
58 cycrow 356
	if ( sIn.empty() ) return "";
35 cycrow 357
 
43 cycrow 358
	if ( sIn.isin("::") ) {
359
		sFile = sIn.token("::", 2);
360
		sIn = sIn.token("::", 1);
58 cycrow 361
		return _extractFromCat(sIn, sFile, to);
43 cycrow 362
	}
58 cycrow 363
 
364
	return _extract(sIn, to);
1 cycrow 365
}
366
 
35 cycrow 367
Utils::String CVirtualFileSystem::_convertExtension(const Utils::String &sFile)
368
{
369
	CFileIO File(sFile);
370
	if ( File.CheckFileExtension("pck") ) {
371
		return File.ChangeFileExtension("xml").ToString();
372
	}
373
	else if ( File.CheckFileExtension("xml") ) {
374
		return File.ChangeFileExtension("pck").ToString();
375
	}
376
	else if ( File.CheckFileExtension("pbb") ) {
377
		return File.ChangeFileExtension("bob").ToString();
378
	}
379
	else if ( File.CheckFileExtension("bob") ) {
380
		return File.ChangeFileExtension("pbb").ToString();
381
	}
382
	else if ( File.CheckFileExtension("pbd") ) {
383
		return File.ChangeFileExtension("bod").ToString();
384
	}
385
	else if ( File.CheckFileExtension("bod") ) {
386
		return File.ChangeFileExtension("pbd").ToString();
387
	}
388
 
389
	return sFile;
390
}
391
 
392
CyStringList *CVirtualFileSystem::GetTShipsEntries()
393
{
82 cycrow 394
	Utils::String sTo = this->ExtractGameFile("types/tships.pck", CPackages::tempDirectory() + "/tships.txt");
58 cycrow 395
	if ( !sTo.empty() ) {
396
		CFileIO TShips(sTo);
52 cycrow 397
		if ( TShips.exists() )
35 cycrow 398
		{
399
			CyStringList *lines = TShips.ReadLinesStr();
400
			if ( lines )
401
			{
402
				// remove any commands, blank lines, and start
403
				// and put the id as the data
404
				int entries = -1;
405
				for ( SStringList *str = lines->Head(); str; str = str->next )
406
				{
407
					str->str.RemoveFirstSpace();
408
					str->str.RemoveChar(9);
409
					str->str.RemoveChar('\r');
410
					str->str.RemoveChar('\n');
411
					if ( str->str.Empty() )
412
					{
413
						str->remove = true;
414
						continue;
415
					}
416
 
417
					if ( str->str[0] == '/' )
418
					{
419
						str->remove = true;
420
						continue;
421
					}
422
 
423
					if ( entries == -1 )
424
					{
425
						entries = str->str.GetToken(";", 2, 2).ToInt();
426
						str->remove = true;
427
						continue;
428
					}
429
 
430
					// hopefully we now have a valid tships line
431
					int num = -1;
432
					while ( str->data.Empty() )
433
						str->data = str->str.GetToken(";", num--, (num + 2));
434
				}
435
			}
436
			// remove the tmp file
52 cycrow 437
			TShips.remove();
35 cycrow 438
 
439
			if ( lines )
440
				lines->RemoveMarked();
441
			return lines;
442
		}
443
	}
444
 
445
	return NULL;
446
}
447
 
448
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType)
449
{
450
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
451
}
452
 
453
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType, const Utils::String &sTo)
454
{
79 cycrow 455
	Utils::String to = this->ExtractGameFile(sFile, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 456
	if ( !to.empty() ) {
457
		CFileIO File(to);
35 cycrow 458
 
459
		C_File *f = pPackage->AddFile(CFileIO(sTo).GetFilename(), CFileIO(sTo).GetDir(), iFileType);
460
		if ( f ) {
79 cycrow 461
			if ( f->ReadFromFile(CPackages::GetTempDirectory() + "tmp.dat") ) {
52 cycrow 462
				File.remove();
35 cycrow 463
				return f;
464
			}
465
		}
466
 
52 cycrow 467
		File.remove();
35 cycrow 468
	}
469
 
470
	return NULL;
471
}
472
 
473
Utils::String CVirtualFileSystem::getTShipsEntry(const Utils::String &sId)
474
{
475
	CyStringList *ships = this->GetTShipsEntries();
476
	if ( ships )
477
	{
478
		SStringList *node = ships->FindData(CyString(sId).upper());
479
		if ( node )
480
			return node->str.ToString();
481
	}
482
	return "";
483
}
484
 
485
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
486
{
487
	//TODO: check for better finding of files in map
488
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
489
		Utils::String str = itr->first;
490
 
491
		if ( !m_sAddon.empty() ) {
492
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
493
				continue;
494
		}
495
		else {
496
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") && !str.left(8).Compare("addon\t\\") && !str.left(8).Compare("addon/t/") )
497
				continue;
498
		}
499
 
79 cycrow 500
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 501
		if ( !sTo.empty() ) {
502
			pPackage->AddTextFromFile(sTo, textId);
503
			CFileIO::Remove(sTo);
35 cycrow 504
		}
505
	}
506
}
507
 
508
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
509
{
510
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
511
}
512
void CVirtualFileSystem::updateTexts(int iPage)
513
{
514
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
515
}
516
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
517
{
518
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
519
}
520
void CVirtualFileSystem::updateModTexts(int iPage)
521
{
522
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
523
}
524
 
525
void CVirtualFileSystem::_clear()
526
{
527
	m_bLoaded = false;
528
 
529
	if ( m_pMap ) delete m_pMap;
530
	m_pMap = new FileList;
531
	if ( m_pModMap ) delete m_pModMap;
532
	m_pModMap = new FileList;
533
}
534
 
535
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
536
{
537
	// read all text files
538
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
539
		Utils::String str = itr->first;
540
 
541
		if ( !m_sAddon.empty() ) {
542
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
543
				continue;
544
		}
545
		else {
546
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") )
547
				continue;
548
		}
549
 
79 cycrow 550
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 551
		if ( !sTo.empty() ) {
35 cycrow 552
			// add all texts into the map, id=(pageid, tid) data=text
57 cycrow 553
			Utils::String baseFile = CFileIO(str).baseName();
35 cycrow 554
			int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
555
 
556
			if ( m_iLang && lang != m_iLang ) continue;
557
			// open the text file to add
58 cycrow 558
			pTextList->parseTextFile(iFromPage, iToPage, sTo, lang);
35 cycrow 559
		}
560
	}
561
}
562
 
78 cycrow 563
void CVirtualFileSystem::clearMods(bool bIncludeStandard)
35 cycrow 564
{
78 cycrow 565
	if ( bIncludeStandard ) {
566
		if ( m_pTexts ) delete m_pTexts;
567
		m_pTexts = new CTextDB();
568
	}
569
 
35 cycrow 570
	if ( m_pModTexts ) delete m_pModTexts;
571
	m_pModTexts = new CTextDB();
572
}
573
 
574
bool CVirtualFileSystem::loadShipData(CyStringList *list)
575
{
576
	bool ret = false;
577
 
79 cycrow 578
	Utils::String sTo = this->ExtractGameFile("types/TShips.pck", CPackages::tempDirectory() + "tmp.dat");
58 cycrow 579
	if ( !sTo.empty() ) {
580
		CFileIO File(sTo);
35 cycrow 581
		CyStringList *lines = File.ReadLinesStr();
582
		if ( lines )
583
		{
584
			bool readFirst = false;
585
			for ( SStringList *str = lines->Head(); str; str = str->next )
586
			{
587
				if ( str->str.Empty() )
588
					continue;
589
				str->str.RemoveChar('\r');
590
				str->str.RemoveChar(9);
591
				str->str.RemoveFirstSpace();
592
				if ( str->str.Empty() )
593
					continue;
594
				if ( str->str[0] == '/' || str->str[0] == '#' )
595
					continue;
596
 
597
				if ( !readFirst )
598
					readFirst = true;
599
				else
600
				{
601
					CyString t = str->str.GetToken(";", -2);
602
					while ( t.Right(1) == ";" )
603
						t.Truncate((int)t.Length() - 1);
604
					list->PushBack(t, str->str);
605
				}
606
			}
607
 
608
			delete lines;
609
			ret = true;
610
		}
611
 
52 cycrow 612
		File.remove();
35 cycrow 613
	}
614
 
615
	return ret;
616
}
617
 
94 cycrow 618
Utils::CStringList *CVirtualFileSystem::_updateList(const Utils::String &typesFile, int iTextPos)
619
{
620
	Utils::CStringList *list = new Utils::CStringList();
35 cycrow 621
 
94 cycrow 622
	Utils::String file = this->ExtractGameFile("types\\" + typesFile + ".pck", CPackages::tempDirectory() + "/" + typesFile + ".txt");
623
 
624
	CFileIO f(file);
625
 
626
	if ( f.exists() && f.startRead() ) {
627
		int itemsLeft = -1;
628
 
629
		while(!f.atEnd()) {
630
			// read the next line
631
			Utils::String line = f.readEndOfLine();
632
 
633
			// remove the unneeded characters
634
			line.removeChar('\r');
635
			line.removeChar(9);
636
			line.removeFirstSpace();
637
 
638
			// blank line ? skip it
639
			if ( line.empty() ) continue;
640
			// skip anything starting with / as its a comment
641
			if ( line[0] == '/' ) continue;
642
 
643
			// first line is the count
644
			if ( itemsLeft == -1 )
645
				itemsLeft = line.token(";", 2);
646
			else {
647
				if ( iTextPos == 0 )
648
					list->pushBack(line, line);
649
				else if ( iTextPos == -1 ) 
650
					list->pushBack(line, line.token(";", -2));
651
				else
652
					list->pushBack(line, this->findText(m_iLang, TEXTPAGE_OBJECTS, line.token(";", iTextPos).toLong()));
653
				--itemsLeft;
654
			}
655
		}
656
 
657
		f.close();
658
		f.remove();
659
	}
660
 
661
	return list;
662
}
663
 
664
void CVirtualFileSystem::_updateShields()
665
{
666
	DELETELIST(_lShields);
667
	_lShields = _updateList("TShields", 7);
668
}
669
 
670
void CVirtualFileSystem::_updateLasers()
671
{
672
	DELETELIST(_lLasers);
673
	_lLasers = _updateList("TLaser", 7);
674
}
675
 
676
void CVirtualFileSystem::_updateMissiles()
677
{
678
	DELETELIST(_lMissiles);
679
	_lMissiles = _updateList("TMissiles", 7);
680
}
681
 
682
void CVirtualFileSystem::_updateCockpits()
683
{
684
	DELETELIST(_lCockpits);
685
	_lCockpits = _updateList("TCockpits", -1);
686
}
687
 
688
void CVirtualFileSystem::_updateComponentSections()
689
{
690
	DELETELIST(_lComponentSections);
691
 
692
	_lComponentSections = new Utils::CStringList();
693
 
694
	Utils::String file = this->ExtractGameFile("types\\Components.pck", CPackages::tempDirectory() + "/Components.txt");
695
 
696
	CFileIO f(file);
697
 
698
	if ( f.exists() && f.startRead() ) {
699
		int sectionRemaining = 0;
700
		int itemsRemaining = 0;
701
 
702
		while(!f.atEnd()) {
703
			// read the next line
704
			Utils::String line = f.readEndOfLine();
705
 
706
			// remove the unneeded characters
707
			line.removeChar('\r');
708
			line.removeChar(9);
709
			line.removeFirstSpace();
710
 
711
			// blank line ? skip it
712
			if ( line.empty() ) continue;
713
			// skip anything starting with / as its a comment
714
			if ( line[0] == '/' ) continue;
715
 
716
			if ( itemsRemaining )
717
				--itemsRemaining;
718
			else if ( !sectionRemaining )
719
			{
720
				sectionRemaining = line.token(";", 2).toLong();
721
				Utils::String section = line.token(";", 1);
722
				if ( !section.empty() )
723
					_lComponentSections->pushBack(section, section);
724
			}
725
			else if ( !itemsRemaining )
726
			{
727
				itemsRemaining = line.token(";", 2).toLong();
728
				--sectionRemaining;
729
			}
730
		}
731
 
732
		f.close();
733
		f.remove();
734
	}
735
 
736
	if ( _lComponentSections->empty() )
737
	{
738
		_lComponentSections->pushBack("SCTYPE_LASER", "SCTYPE_LASER");
739
		_lComponentSections->pushBack("SCTYPE_COCKPIT", "SCTYPE_COCKPIT");
740
		_lComponentSections->pushBack("SCTYPE_DOCKING", "SCTYPE_DOCKING");
741
	}
742
}
743
 
744
Utils::CStringList *CVirtualFileSystem::_updateSectionList(const Utils::String &sFile, bool singleEntry)
745
{
746
	Utils::CStringList *list = new Utils::CStringList();
747
 
748
	Utils::String file = this->ExtractGameFile("types\\" + sFile + ".pck", CPackages::tempDirectory() + "/" + sFile + ".txt");
749
 
750
	CFileIO f(file);
751
 
752
	if ( f.exists() && f.startRead() ) {
753
		int sectionRemaining = 0;
754
 
755
		while(!f.atEnd()) {
756
			// read the next line
757
			Utils::String line = f.readEndOfLine();
758
 
759
			// remove the unneeded characters
760
			line.removeChar('\r');
761
			line.removeChar(9);
762
			line.removeFirstSpace();
763
 
764
			// blank line ? skip it
765
			if ( line.empty() ) continue;
766
			// skip anything starting with / as its a comment
767
			if ( line[0] == '/' ) continue;
768
 
769
			if ( !sectionRemaining )
770
			{
771
				sectionRemaining = line.token(";", 2).toLong();
772
				Utils::String section = line.token(";", 1);
773
				if ( !section.empty() )
774
					list->pushBack(section, section);
775
			}
776
			else {
777
				if ( singleEntry )
778
					sectionRemaining -= (line.countToken(";") - 1);
779
				else 
780
					--sectionRemaining;
781
			}
782
		}
783
 
784
		f.close();
785
		f.remove();
786
	}
787
 
788
	return list;
789
}
790
 
791
void CVirtualFileSystem::_updateDummySections()
792
{
793
	DELETELIST(_lDummySections);
794
 
795
	_lDummySections = _updateSectionList("Dummies", false);
796
 
797
	if ( _lDummySections->empty() )
798
	{
799
		_lDummySections->pushBack("SDTYPE_ANIMATED", "SDTYPE_ANIMATED");
800
		_lDummySections->pushBack("SDTYPE_DOCK", "SDTYPE_DOCK");
801
		_lDummySections->pushBack("SDTYPE_DOORWAY", "SDTYPE_DOORWAY");
802
		_lDummySections->pushBack("SDTYPE_GUN", "SDTYPE_GUN");
803
		_lDummySections->pushBack("SDTYPE_CONNECTION", "SDTYPE_CONNECTION");
804
	}
805
}
806
 
807
void CVirtualFileSystem::_updateBodiesSections()
808
{
809
	DELETELIST(_lBodiesSections);
810
 
811
	_lBodiesSections = _updateSectionList("Bodies", true);
812
 
813
	if ( _lBodiesSections->empty() )
814
	{
815
		_lBodiesSections->pushBack("SBTYPE_2D", "SBTYPE_2D");
816
		_lBodiesSections->pushBack("SBTYPE_FACECAMERA", "SBTYPE_FACECAMERA");
817
		_lBodiesSections->pushBack("SBTYPE_2D2", "SBTYPE_2D2");
818
		_lBodiesSections->pushBack("SBTYPE_2DY", "SBTYPE_2DY");
819
		_lBodiesSections->pushBack("SBTYPE_LOGO", "SBTYPE_LOGO");
820
		_lBodiesSections->pushBack("SBTYPE_FC", "SBTYPE_FC");
821
		_lBodiesSections->pushBack("SBTYPE_TURRET", "SBTYPE_TURRET");
822
		_lBodiesSections->pushBack("SBTYPE_JET", "SBTYPE_JET");
823
		_lBodiesSections->pushBack("SBTYPE_RADAR", "SBTYPE_RADAR");
824
		_lBodiesSections->pushBack("SBTYPE_CONTAINER", "SBTYPE_CONTAINER");
825
		_lBodiesSections->pushBack("SBTYPE_DISPLAY", "SBTYPE_DISPLAY");
826
		_lBodiesSections->pushBack("SBTYPE_DOCKPOINT", "SBTYPE_DOCKPOINT");
827
		_lBodiesSections->pushBack("SBTYPE_SMALLJET", "SBTYPE_SMALLJET");
828
	}
829
}
830
 
35 cycrow 831
} //NAMESPACE