2008-07-12 17:40:22 +00:00
|
|
|
// 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"
|
2008-08-24 16:25:52 +00:00
|
|
|
#include "Common.h"
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "FileSystemGCWii.h"
|
|
|
|
#include "StringUtil.h"
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2008-09-21 15:04:22 +00:00
|
|
|
CFileSystemGCWii::CFileSystemGCWii(const IVolume *_rVolume)
|
2008-07-12 17:40:22 +00:00
|
|
|
: IFileSystem(_rVolume),
|
|
|
|
m_Initialized(false),
|
|
|
|
m_OffsetShift(0)
|
|
|
|
{
|
|
|
|
m_Initialized = InitFileSystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CFileSystemGCWii::~CFileSystemGCWii()
|
2008-09-21 14:51:40 +00:00
|
|
|
{
|
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
bool CFileSystemGCWii::IsInitialized() const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
return m_Initialized;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-28 16:49:58 +00:00
|
|
|
u64 CFileSystemGCWii::GetFileSize(const char* _rFullPath) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
2008-09-22 20:35:44 +00:00
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
|
|
|
if (pFileInfo != NULL)
|
2008-09-22 20:35:44 +00:00
|
|
|
return pFileInfo->m_FileSize;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
const char* CFileSystemGCWii::GetFileName(u64 _Address) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
if ((m_FileInfoVector[i].m_Offset <= _Address) &&
|
|
|
|
((m_FileInfoVector[i].m_Offset + m_FileInfoVector[i].m_FileSize) > _Address))
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
return m_FileInfoVector[i].m_FullPath;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
return NULL;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-28 16:49:58 +00:00
|
|
|
u64 CFileSystemGCWii::ReadFile(const char* _rFullPath, u8* _pBuffer, size_t _MaxBufferSize) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
2008-09-22 20:35:44 +00:00
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
if (pFileInfo == NULL)
|
2008-09-22 20:35:44 +00:00
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
if (pFileInfo->m_FileSize > _MaxBufferSize)
|
2008-09-22 20:35:44 +00:00
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-21 15:04:22 +00:00
|
|
|
m_rVolume->Read(pFileInfo->m_Offset, pFileInfo->m_FileSize, _pBuffer);
|
2008-09-22 20:35:44 +00:00
|
|
|
return pFileInfo->m_FileSize;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
bool CFileSystemGCWii::ExportFile(const char* _rFullPath, const char* _rExportFilename) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
size_t filesize = GetFileSize(_rFullPath);
|
|
|
|
|
|
|
|
if (filesize == 0)
|
2008-09-22 20:35:44 +00:00
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
u8* buffer = new u8[filesize];
|
|
|
|
|
|
|
|
if (!ReadFile(_rFullPath, buffer, filesize))
|
|
|
|
{
|
|
|
|
delete[] buffer;
|
2008-09-22 20:35:44 +00:00
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE* f = fopen(_rExportFilename, "wb");
|
|
|
|
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fwrite(buffer, filesize, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
delete[] buffer;
|
2008-09-22 20:35:44 +00:00
|
|
|
return true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buffer;
|
2008-09-22 20:35:44 +00:00
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
bool CFileSystemGCWii::ExportAllFiles(const char* _rFullPath) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
u32 CFileSystemGCWii::Read32(u64 _Offset) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
u32 Temp = 0;
|
2008-09-21 15:04:22 +00:00
|
|
|
m_rVolume->Read(_Offset, 4, (u8*)&Temp);
|
2008-09-22 20:35:44 +00:00
|
|
|
return Common::swap32(Temp);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFileSystemGCWii::GetStringFromOffset(u64 _Offset, char* Filename) const
|
|
|
|
{
|
2008-09-21 15:04:22 +00:00
|
|
|
m_rVolume->Read(_Offset, 255, (u8*)Filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
size_t CFileSystemGCWii::GetFileList(std::vector<const SFileInfo *> &_rFilenames) const
|
2008-09-21 14:51:40 +00:00
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
if (_rFilenames.size())
|
|
|
|
PanicAlert("GetFileList : input list has contents?");
|
2008-09-21 14:51:40 +00:00
|
|
|
_rFilenames.clear();
|
2008-09-24 19:10:56 +00:00
|
|
|
_rFilenames.reserve(m_FileInfoVector.size());
|
2008-09-18 03:18:19 +00:00
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
2008-09-22 20:35:44 +00:00
|
|
|
_rFilenames.push_back(&m_FileInfoVector[i]);
|
2008-09-18 03:18:19 +00:00
|
|
|
return m_FileInfoVector.size();
|
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
const SFileInfo* CFileSystemGCWii::FindFileInfo(const char* _rFullPath) const
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
if (!strcasecmp(m_FileInfoVector[i].m_FullPath, _rFullPath))
|
|
|
|
return &m_FileInfoVector[i];
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
return NULL;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
bool CFileSystemGCWii::InitFileSystem()
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
if (Read32(0x18) == 0x5D1C9EA3)
|
|
|
|
{
|
2008-10-17 11:30:14 +00:00
|
|
|
m_OffsetShift = 2; // Wii file system
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
else if (Read32(0x1c) == 0xC2339F3D)
|
|
|
|
{
|
2008-10-17 11:30:14 +00:00
|
|
|
m_OffsetShift = 0; // GC file system
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read the whole FST
|
2008-09-28 16:49:58 +00:00
|
|
|
u64 FSTOffset = (u64)Read32(0x424) << m_OffsetShift;
|
2008-07-12 17:40:22 +00:00
|
|
|
// 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())
|
|
|
|
{
|
2008-09-24 19:10:56 +00:00
|
|
|
if (m_FileInfoVector.size())
|
|
|
|
PanicAlert("Wtf?");
|
2008-07-12 17:40:22 +00:00
|
|
|
u64 NameTableOffset = FSTOffset;
|
|
|
|
|
2008-09-24 19:10:56 +00:00
|
|
|
m_FileInfoVector.reserve(Root.m_FileSize);
|
2008-09-21 14:51:40 +00:00
|
|
|
for (u32 i = 0; i < Root.m_FileSize; i++)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
SFileInfo sfi;
|
2008-07-12 17:40:22 +00:00
|
|
|
u64 Offset = FSTOffset + (i * 0xC);
|
2008-09-22 20:35:44 +00:00
|
|
|
sfi.m_NameOffset = Read32(Offset + 0x0);
|
|
|
|
sfi.m_Offset = (u64)Read32(Offset + 0x4) << m_OffsetShift;
|
|
|
|
sfi.m_FileSize = Read32(Offset + 0x8);
|
2008-09-21 14:51:40 +00:00
|
|
|
|
|
|
|
m_FileInfoVector.push_back(sfi);
|
2008-07-12 17:40:22 +00:00
|
|
|
NameTableOffset += 0xC;
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildFilenames(1, m_FileInfoVector.size(), NULL, NameTableOffset);
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
return true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2008-09-22 20:35:44 +00:00
|
|
|
size_t CFileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const char* _szDirectory, u64 _NameTableOffset)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
size_t CurrentIndex = _FirstIndex;
|
|
|
|
|
|
|
|
while (CurrentIndex < _LastIndex)
|
|
|
|
{
|
2008-09-22 20:35:44 +00:00
|
|
|
SFileInfo *rFileInfo = &m_FileInfoVector[CurrentIndex];
|
2008-09-21 14:51:40 +00:00
|
|
|
u64 uOffset = _NameTableOffset + (rFileInfo->m_NameOffset & 0xFFFFFF);
|
2008-07-12 17:40:22 +00:00
|
|
|
char filename[512];
|
2008-09-22 20:35:44 +00:00
|
|
|
memset(filename, 0, sizeof(filename));
|
2008-07-12 17:40:22 +00:00
|
|
|
GetStringFromOffset(uOffset, filename);
|
|
|
|
|
|
|
|
// check next index
|
2008-09-21 14:51:40 +00:00
|
|
|
if (rFileInfo->IsDirectory())
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
// this is a directory, build up the new szDirectory
|
|
|
|
if (_szDirectory != NULL)
|
2008-09-21 14:51:40 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s%s\\", _szDirectory, filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
else
|
2008-09-21 14:51:40 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s\\", filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-21 14:51:40 +00:00
|
|
|
CurrentIndex = BuildFilenames(CurrentIndex + 1, rFileInfo->m_FileSize, rFileInfo->m_FullPath, _NameTableOffset);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is a filename
|
|
|
|
if (_szDirectory != NULL)
|
2008-09-21 14:51:40 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s%s", _szDirectory, filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
else
|
2008-09-21 14:51:40 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s", filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
CurrentIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:35:44 +00:00
|
|
|
return CurrentIndex;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-09-22 20:35:44 +00:00
|
|
|
|
2008-10-17 11:30:14 +00:00
|
|
|
} // namespace
|