Subversion Repositories spk

Rev

Go to most recent revision | Details | 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"
5
 
6
CVirtualFileSystem::CVirtualFileSystem()
7
{
8
	m_bLoaded = false;
9
	m_pMap = NULL;
10
	m_sAddon = "";
11
}
12
 
13
CVirtualFileSystem::~CVirtualFileSystem(void)
14
{
15
	if ( m_pMap )
16
		delete m_pMap;
17
}
18
 
19
bool CVirtualFileSystem::LoadFilesystem(CyString &dir, CyString &mod, int maxPatch)
20
{
21
	m_sDir = dir;
22
	m_bLoaded = false;
23
 
24
	if ( m_pMap ) delete m_pMap;
25
	m_pMap = new MAP;
26
 
27
	int number = 1;
28
	while ( CFileIO(dir + "/" + CyString::Number(number).PadNumber(2) + ".cat").Exists() )
29
	{
30
		if ( maxPatch && maxPatch > number ) break;
31
		CyString file = dir + "/" + CyString::Number(number).PadNumber(2);
32
		if ( !CFileIO(file + ".dat").Exists() )
33
			break;
34
 
35
		CCatFile cat;
36
		if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
37
		{
38
			for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
39
			{
40
				(*m_pMap)[CFileIO(c->Data()->sFile).GetFullFilename().ToLower().c_str()] = CyString(file + ".cat").c_str();
41
				m_bLoaded = true;
42
			}
43
		}
44
		++number;
45
	}
46
 
47
	if ( !mod.Empty() )
48
		LoadMod(mod);
49
 
50
	return m_bLoaded;
51
}
52
 
53
bool CVirtualFileSystem::LoadMod(CyString &mod)
54
{
55
	bool loaded = false;
56
	CCatFile cat;
57
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
58
	{
59
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
60
		{
61
			(*m_pMap)[CFileIO(c->Data()->sFile).GetFullFilename().ToLower().c_str()] = mod.c_str();
62
			loaded = true;
63
		}
64
	}
65
 
66
	return loaded;
67
}
68
 
69
CyString CVirtualFileSystem::GetFile(CyString &file)
70
{
71
	if ( !m_pMap ) return NullString;
72
	MAP::iterator itr = m_pMap->find(CFileIO(file).GetFullFilename().ToLower().c_str());
73
	if ( itr == m_pMap->end() ) return NullString;
74
	return itr->second;
75
}
76
 
77
bool CVirtualFileSystem::ExtractGameFile(CyString &file, CyString &to)
78
{
79
	CyString sIn = GetFile(file);
80
	if ( sIn.Empty() ) return false;
81
 
82
	CCatFile catFile;
83
	if ( catFile.Open(sIn, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
84
	{
85
		// check for the file
86
		if ( catFile.ExtractFile(file, to) )
87
			return true;
88
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
89
		{
90
			if ( catFile.ExtractFile(file) )
91
				return true;
92
		}
93
	}
94
	return false;
95
}
96