summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/Src/FileSystemGCWii.cpp
blob: e92d1109d5764ba5be99f6cd0f49d179c0d28b28 (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
// Copyright (C) 2003-2008 Dolphin Project.

// 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, version 2.0.

// 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 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include "stdafx.h"

#include <string>

#include "FileSystemGCWii.h"
#include "StringUtil.h"

namespace DiscIO
{
CFileSystemGCWii::CFileSystemGCWii(const IVolume& _rVolume)
	: IFileSystem(_rVolume),
	m_Initialized(false),
	m_OffsetShift(0)
{
	m_Initialized = InitFileSystem();
}


CFileSystemGCWii::~CFileSystemGCWii()
{}


bool
CFileSystemGCWii::IsInitialized()
{
	return(m_Initialized);
}


size_t
CFileSystemGCWii::GetFileSize(const char* _rFullPath)
{
	if (!m_Initialized)
	{
		return(0);
	}

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo != NULL)
	{
		return(pFileInfo->m_FileSize);
	}

	return(0);
}


const char*
CFileSystemGCWii::GetFileName(u64 _Address)
{
	for (size_t i = 0; i < m_FileInfoVector.size(); i++)
	{
		if ((m_FileInfoVector[i].m_Offset <= _Address) &&
		    ((m_FileInfoVector[i].m_Offset + m_FileInfoVector[i].m_FileSize) > _Address))
		{
			return(m_FileInfoVector[i].m_FullPath);
		}
	}

	return(NULL);
}


size_t
CFileSystemGCWii::ReadFile(const char* _rFullPath, u8* _pBuffer, size_t _MaxBufferSize)
{
	if (!m_Initialized)
	{
		return(0);
	}

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo == NULL)
	{
		return(0);
	}

	if (pFileInfo->m_FileSize > _MaxBufferSize)
	{
		return(0);
	}

	m_rVolume.Read(pFileInfo->m_Offset, pFileInfo->m_FileSize, _pBuffer);
	return(pFileInfo->m_FileSize);
}


bool
CFileSystemGCWii::ExportFile(const char* _rFullPath, const char* _rExportFilename)
{
	size_t filesize = GetFileSize(_rFullPath);

	if (filesize == 0)
	{
		return(false);
	}

	u8* buffer = new u8[filesize];

	if (!ReadFile(_rFullPath, buffer, filesize))
	{
		delete[] buffer;
		return(false);
	}

	FILE* f = fopen(_rExportFilename, "wb");

	if (f)
	{
		fwrite(buffer, filesize, 1, f);
		fclose(f);
		delete[] buffer;
		return(true);
	}

	delete[] buffer;
	return(false);
}


bool
CFileSystemGCWii::ExportAllFiles(const char* _rFullPath)
{
	return(false);
}


u32
CFileSystemGCWii::Read32(u64 _Offset) const
{
	u32 Temp = 0;
	m_rVolume.Read(_Offset, 4, (u8*)&Temp);
	return(Common::swap32(Temp));
}


void CFileSystemGCWii::GetStringFromOffset(u64 _Offset, char* Filename) const
{
	m_rVolume.Read(_Offset, 255, (u8*)Filename);
}


const CFileSystemGCWii::SFileInfo*
CFileSystemGCWii::FindFileInfo(const char* _rFullPath) const
{
	for (size_t i = 0; i < m_FileInfoVector.size(); i++)
	{
		#ifdef _WIN32
		if (!_stricmp(m_FileInfoVector[i].m_FullPath, _rFullPath))
		#else
		if (!strcasecmp(m_FileInfoVector[i].m_FullPath, _rFullPath)) //TODO(Sonic): Shouldn't this work in all platforms?
		#endif
		{
			return(&m_FileInfoVector[i]);
		}
	}

	return(NULL);
}


bool
CFileSystemGCWii::InitFileSystem()
{
	if (Read32(0x18) == 0x5D1C9EA3)
	{
		m_OffsetShift = 2;
	}
	else if (Read32(0x1c) == 0xC2339F3D)
	{
		m_OffsetShift = 0;
	}
	else
	{
		return(false);
	}

	// read the whole FST
	u32 FSTOffset = Read32(0x424) << m_OffsetShift;
	// u32 FSTSize     = Read32(0x428);
	// u32 FSTMaxSize  = Read32(0x42C);


	// read all fileinfos
	SFileInfo Root;
	Root.m_NameOffset = Read32(FSTOffset + 0x0);
	Root.m_Offset     = (u64)Read32(FSTOffset + 0x4) << m_OffsetShift;
	Root.m_FileSize   = Read32(FSTOffset + 0x8);

	if (Root.IsDirectory())
	{
		m_FileInfoVector.resize(Root.m_FileSize);
		u64 NameTableOffset = FSTOffset;

		for (size_t i = 0; i < m_FileInfoVector.size(); i++)
		{
			u64 Offset = FSTOffset + (i * 0xC);
			m_FileInfoVector[i].m_NameOffset = Read32(Offset + 0x0);
			m_FileInfoVector[i].m_Offset     = (u64)Read32(Offset + 0x4) << m_OffsetShift;
			m_FileInfoVector[i].m_FileSize   = Read32(Offset + 0x8);
			NameTableOffset += 0xC;
		}

		BuildFilenames(1, m_FileInfoVector.size(), NULL, NameTableOffset);
	}

	return(true);
}


// __________________________________________________________________________________________________
//
// Changed this stuff from C++ string to C strings for speed in debug mode. Doesn't matter in release, but
// std::string is SLOW in debug mode.
size_t
CFileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const char* _szDirectory, u64 _NameTableOffset)
{
	size_t CurrentIndex = _FirstIndex;

	while (CurrentIndex < _LastIndex)
	{
		SFileInfo& rFileInfo = m_FileInfoVector[CurrentIndex];
		u64 uOffset = _NameTableOffset + (rFileInfo.m_NameOffset & 0xFFFFFF);
		char filename[512];
		GetStringFromOffset(uOffset, filename);

		// check next index
		if (rFileInfo.IsDirectory())
		{
			// this is a directory, build up the new szDirectory
			if (_szDirectory != NULL)
			{
				CharArrayFromFormat(rFileInfo.m_FullPath, "%s%s\\", _szDirectory, filename);
			}
			else
			{
				CharArrayFromFormat(rFileInfo.m_FullPath, "%s\\", filename);
			}

			CurrentIndex = BuildFilenames(CurrentIndex + 1, rFileInfo.m_FileSize, rFileInfo.m_FullPath, _NameTableOffset);
		}
		else
		{
			// this is a filename
			if (_szDirectory != NULL)
			{
				CharArrayFromFormat(rFileInfo.m_FullPath, "%s%s", _szDirectory, filename);
			}
			else
			{
				CharArrayFromFormat(rFileInfo.m_FullPath, "%s", filename);
			}

			CurrentIndex++;
		}
	}

	return(CurrentIndex);
}
} // namespace