Subversion Repositories spk

Rev

Rev 43 | Rev 57 | 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"
1 cycrow 6
 
35 cycrow 7
namespace SPK {
8
 
1 cycrow 9
CVirtualFileSystem::CVirtualFileSystem()
10
{
11
	m_bLoaded = false;
35 cycrow 12
	m_pMap = new FileList;
13
	m_pModMap = new FileList;
14
 
15
	m_pTexts = new CTextDB();
16
	m_pModTexts = new CTextDB();
17
 
1 cycrow 18
	m_sAddon = "";
35 cycrow 19
 
20
	m_iLang = 0;
1 cycrow 21
}
22
 
23
CVirtualFileSystem::~CVirtualFileSystem(void)
24
{
35 cycrow 25
	if ( m_pMap )		delete m_pMap;
26
	if ( m_pModMap )	delete m_pModMap;
27
 
28
	if ( m_pTexts )		delete m_pTexts;
29
	if ( m_pModTexts )  delete m_pModTexts;
1 cycrow 30
}
31
 
35 cycrow 32
void CVirtualFileSystem::setLanguage(int iLang)
1 cycrow 33
{
35 cycrow 34
	m_iLang = iLang;
35
}
36
 
37
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, int maxPatch)
38
{
39
	return this->LoadFilesystem(dir, "", maxPatch);
40
}
41
 
42
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, const Utils::String &mod, int maxPatch)
43
{
1 cycrow 44
	m_sDir = dir;
45
 
35 cycrow 46
	this->_clear();
1 cycrow 47
 
48
	int number = 1;
52 cycrow 49
	while ( CFileIO(CyString(dir) + "/" + CyString::Number(number).PadNumber(2) + ".cat").ExistsOld() )
1 cycrow 50
	{
51
		if ( maxPatch && maxPatch > number ) break;
35 cycrow 52
		CyString file = CyString(dir) + "/" + CyString::Number(number).PadNumber(2);
52 cycrow 53
		if ( !CFileIO(file + ".dat").ExistsOld() )
1 cycrow 54
			break;
55
 
56
		CCatFile cat;
57
		if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
58
		{
43 cycrow 59
			for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
60
				this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), CyString(file + ".cat").ToString(), m_pMap);
1 cycrow 61
				m_bLoaded = true;
62
			}
63
		}
64
		++number;
65
	}
66
 
43 cycrow 67
	// add all the files from the mod
35 cycrow 68
	if ( !mod.empty() )
69
		this->addMod(mod);
70
 
43 cycrow 71
	// now add all the extracted data
72
	this->_addDir(m_sDir, "");
73
 
1 cycrow 74
	return m_bLoaded;
75
}
76
 
43 cycrow 77
 
78
void CVirtualFileSystem::_addDir(const Utils::String &sStart, const Utils::String &sDir)
79
{
80
	CDirIO Dir(sStart + "/" + sDir);
81
	CyStringList *list = Dir.DirList();
82
	if ( list ) {
83
		for ( SStringList *strNode = list->Head(); strNode; strNode = strNode->next ) {
84
			if ( CDirIO(Dir.Dir(strNode->str)).IsDir() ) {
85
				_addDir(sStart, sDir + "/" + strNode->str.ToString());
86
			}
87
			else if ( CFileIO(strNode->str).CheckFileExtension("cat") ) continue;
88
			else if ( CFileIO(strNode->str).CheckFileExtension("dat") ) continue;
89
			else {
90
				this->_addFile(Dir.File(strNode->str).ToString(), Dir.File(strNode->str).ToString(), m_pMap);
91
			}
92
		}
93
	}
94
}
95
 
96
void CVirtualFileSystem::_addFile(const Utils::String &sFile, const Utils::String &sDest, FileList *pList)
97
{
98
	(*pList)[sFile.findReplace("\\", "/").findReplace("//", "/").lower()] = sDest.findReplace("//", "/");
99
}
100
void CVirtualFileSystem::_addModFile(const Utils::String &sFile, const Utils::String &sMod, FileList *pList)
101
{
102
	this->_addFile(sFile, sMod + "::" + sFile, pList);
103
}
104
 
35 cycrow 105
bool CVirtualFileSystem::loadMod(const Utils::String &mod)
1 cycrow 106
{
107
	bool loaded = false;
35 cycrow 108
 
109
	if ( !m_pModMap ) m_pModMap = new FileList;
110
 
1 cycrow 111
	CCatFile cat;
112
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
113
	{
43 cycrow 114
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
115
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pModMap);
35 cycrow 116
			loaded = true;
117
		}
118
	}
119
 
120
	return loaded;
121
}
122
 
123
bool CVirtualFileSystem::addMod(const Utils::String &mod)
124
{
125
	bool loaded = false;
126
 
127
	if ( !m_pMap ) m_pMap = new FileList;
128
 
129
	CCatFile cat;
130
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
131
	{
132
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
133
		{
43 cycrow 134
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pMap);
1 cycrow 135
			loaded = true;
136
		}
137
	}
138
 
139
	return loaded;
140
}
141
 
35 cycrow 142
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 143
{
35 cycrow 144
	if ( m_pTexts ) {
145
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
146
	}
147
	if ( m_pModTexts ) {
148
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
149
	}
150
 
151
	return false;
1 cycrow 152
}
153
 
35 cycrow 154
Utils::String CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 155
{
35 cycrow 156
	if ( m_pTexts ) {
157
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
158
	}
159
	if ( m_pModTexts ) {
160
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
161
	}
1 cycrow 162
 
35 cycrow 163
	return Utils::String("ReadText") + (long)iPage + "-" + (long)iID;
164
}
43 cycrow 165
 
166
bool CVirtualFileSystem::isFileAvailable(const Utils::String &file)
35 cycrow 167
{
43 cycrow 168
	return !this->_findFile(file).empty();
169
}
170
 
171
Utils::String CVirtualFileSystem::_findFile(const Utils::String &file)
172
{
173
	Utils::String toFile = file.findReplace("\\", "/").lower();
174
 
35 cycrow 175
	if ( m_pModMap && !m_pModMap->empty() ) {
176
		if ( !m_sAddon.empty() ) {
43 cycrow 177
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + "/" + toFile).GetFullFilename().ToLower().c_str());
178
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + "/" + toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 179
			if ( aitr != m_pModMap->end() ) return aitr->second;
180
		}
43 cycrow 181
		FileListItr itr = m_pModMap->find(CFileIO(toFile).GetFullFilename().ToLower().c_str());
182
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 183
		if ( itr != m_pModMap->end() ) return itr->second;
184
	}
185
	else if ( m_pMap && !m_pMap->empty() ) {
186
		if ( !m_sAddon.empty() ) {
43 cycrow 187
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + "/" + toFile).GetFullFilename().ToLower().c_str());
35 cycrow 188
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + "/" + file)).GetFullFilename().ToLower().c_str());
189
			if ( aitr != m_pMap->end() ) return aitr->second;
190
		}
191
		FileListItr itr = m_pMap->find(CFileIO(file).GetFullFilename().ToLower().c_str());
192
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).GetFullFilename().ToLower().c_str());
193
		if ( itr != m_pMap->end() ) return itr->second;
194
	}
195
	return "";
196
}
197
 
198
bool CVirtualFileSystem::ExtractGameFile(const Utils::String &file, const Utils::String &to)
199
{
43 cycrow 200
	Utils::String sIn = _findFile(file);
201
	Utils::String sFile = file;
202
 
35 cycrow 203
	if ( sIn.empty() ) return false;
204
 
43 cycrow 205
	if ( sIn.isin("::") ) {
206
		sFile = sIn.token("::", 2);
207
		sIn = sIn.token("::", 1);
208
	}
209
 
1 cycrow 210
	CCatFile catFile;
211
	if ( catFile.Open(sIn, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
212
	{
213
		// check for the file
214
		if ( catFile.ExtractFile(file, to) )
215
			return true;
216
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
217
		{
218
			if ( catFile.ExtractFile(file) )
219
				return true;
220
		}
221
	}
222
	return false;
223
}
224
 
35 cycrow 225
Utils::String CVirtualFileSystem::_convertExtension(const Utils::String &sFile)
226
{
227
	CFileIO File(sFile);
228
	if ( File.CheckFileExtension("pck") ) {
229
		return File.ChangeFileExtension("xml").ToString();
230
	}
231
	else if ( File.CheckFileExtension("xml") ) {
232
		return File.ChangeFileExtension("pck").ToString();
233
	}
234
	else if ( File.CheckFileExtension("pbb") ) {
235
		return File.ChangeFileExtension("bob").ToString();
236
	}
237
	else if ( File.CheckFileExtension("bob") ) {
238
		return File.ChangeFileExtension("pbb").ToString();
239
	}
240
	else if ( File.CheckFileExtension("pbd") ) {
241
		return File.ChangeFileExtension("bod").ToString();
242
	}
243
	else if ( File.CheckFileExtension("bod") ) {
244
		return File.ChangeFileExtension("pbd").ToString();
245
	}
246
 
247
	return sFile;
248
}
249
 
250
CyStringList *CVirtualFileSystem::GetTShipsEntries()
251
{
252
	if ( this->ExtractGameFile("types/tships.pck", "tships.txt") ) {
253
		CFileIO TShips("tships.txt");
52 cycrow 254
		if ( TShips.exists() )
35 cycrow 255
		{
256
			CyStringList *lines = TShips.ReadLinesStr();
257
			if ( lines )
258
			{
259
				// remove any commands, blank lines, and start
260
				// and put the id as the data
261
				int entries = -1;
262
				for ( SStringList *str = lines->Head(); str; str = str->next )
263
				{
264
					str->str.RemoveFirstSpace();
265
					str->str.RemoveChar(9);
266
					str->str.RemoveChar('\r');
267
					str->str.RemoveChar('\n');
268
					if ( str->str.Empty() )
269
					{
270
						str->remove = true;
271
						continue;
272
					}
273
 
274
					if ( str->str[0] == '/' )
275
					{
276
						str->remove = true;
277
						continue;
278
					}
279
 
280
					if ( entries == -1 )
281
					{
282
						entries = str->str.GetToken(";", 2, 2).ToInt();
283
						str->remove = true;
284
						continue;
285
					}
286
 
287
					// hopefully we now have a valid tships line
288
					int num = -1;
289
					while ( str->data.Empty() )
290
						str->data = str->str.GetToken(";", num--, (num + 2));
291
				}
292
			}
293
			// remove the tmp file
52 cycrow 294
			TShips.remove();
35 cycrow 295
 
296
			if ( lines )
297
				lines->RemoveMarked();
298
			return lines;
299
		}
300
	}
301
 
302
	return NULL;
303
}
304
 
305
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType)
306
{
307
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
308
}
309
 
310
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType, const Utils::String &sTo)
311
{
312
 
313
	if ( this->ExtractGameFile(sFile, "tmp") ) {
314
		CFileIO File("tmp");
315
 
316
		C_File *f = pPackage->AddFile(CFileIO(sTo).GetFilename(), CFileIO(sTo).GetDir(), iFileType);
317
		if ( f ) {
318
			if ( f->ReadFromFile("tmp") ) {
52 cycrow 319
				File.remove();
35 cycrow 320
				return f;
321
			}
322
		}
323
 
52 cycrow 324
		File.remove();
35 cycrow 325
	}
326
 
327
	return NULL;
328
}
329
 
330
Utils::String CVirtualFileSystem::getTShipsEntry(const Utils::String &sId)
331
{
332
	CyStringList *ships = this->GetTShipsEntries();
333
	if ( ships )
334
	{
335
		SStringList *node = ships->FindData(CyString(sId).upper());
336
		if ( node )
337
			return node->str.ToString();
338
	}
339
	return "";
340
}
341
 
342
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
343
{
344
	//TODO: check for better finding of files in map
345
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
346
		Utils::String str = itr->first;
347
 
348
		if ( !m_sAddon.empty() ) {
349
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
350
				continue;
351
		}
352
		else {
353
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") && !str.left(8).Compare("addon\t\\") && !str.left(8).Compare("addon/t/") )
354
				continue;
355
		}
356
 
357
		if ( this->ExtractGameFile(str, "tmp") ) {
358
			pPackage->AddTextFromFile("tmp", textId);
52 cycrow 359
			CFileIO::Remove("tmp");
35 cycrow 360
		}
361
	}
362
}
363
 
364
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
365
{
366
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
367
}
368
void CVirtualFileSystem::updateTexts(int iPage)
369
{
370
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
371
}
372
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
373
{
374
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
375
}
376
void CVirtualFileSystem::updateModTexts(int iPage)
377
{
378
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
379
}
380
 
381
void CVirtualFileSystem::_clear()
382
{
383
	m_bLoaded = false;
384
 
385
	if ( m_pMap ) delete m_pMap;
386
	m_pMap = new FileList;
387
	if ( m_pModMap ) delete m_pModMap;
388
	m_pModMap = new FileList;
389
}
390
 
391
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
392
{
393
	// read all text files
394
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
395
		Utils::String str = itr->first;
396
 
397
		if ( !m_sAddon.empty() ) {
398
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
399
				continue;
400
		}
401
		else {
402
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") )
403
				continue;
404
		}
405
 
406
		if ( this->ExtractGameFile(str, "tmp") ) {
407
			// add all texts into the map, id=(pageid, tid) data=text
408
			Utils::String baseFile = CFileIO(str).GetBaseName().ToString();
409
			int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
410
 
411
			if ( m_iLang && lang != m_iLang ) continue;
412
			// open the text file to add
413
			pTextList->parseTextFile(iFromPage, iToPage, "tmp", lang);
414
		}
415
	}
416
}
417
 
418
void CVirtualFileSystem::clearMods()
419
{
420
	if ( m_pTexts ) delete m_pTexts;
421
	m_pTexts = new CTextDB();
422
	if ( m_pModTexts ) delete m_pModTexts;
423
	m_pModTexts = new CTextDB();
424
}
425
 
426
bool CVirtualFileSystem::loadShipData(CyStringList *list)
427
{
428
	bool ret = false;
429
 
430
	if ( this->ExtractGameFile("types/TShips.pck", "tmp") ) {
431
		CFileIO File("tmp");
432
		CyStringList *lines = File.ReadLinesStr();
433
		if ( lines )
434
		{
435
			bool readFirst = false;
436
			for ( SStringList *str = lines->Head(); str; str = str->next )
437
			{
438
				if ( str->str.Empty() )
439
					continue;
440
				str->str.RemoveChar('\r');
441
				str->str.RemoveChar(9);
442
				str->str.RemoveFirstSpace();
443
				if ( str->str.Empty() )
444
					continue;
445
				if ( str->str[0] == '/' || str->str[0] == '#' )
446
					continue;
447
 
448
				if ( !readFirst )
449
					readFirst = true;
450
				else
451
				{
452
					CyString t = str->str.GetToken(";", -2);
453
					while ( t.Right(1) == ";" )
454
						t.Truncate((int)t.Length() - 1);
455
					list->PushBack(t, str->str);
456
				}
457
			}
458
 
459
			delete lines;
460
			ret = true;
461
		}
462
 
52 cycrow 463
		File.remove();
35 cycrow 464
	}
465
 
466
	return ret;
467
}
468
 
469
 
470
} //NAMESPACE