2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
// 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 "Common.h"
|
2009-09-12 09:09:47 +00:00
|
|
|
#include "FileUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#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() const
|
|
|
|
{
|
|
|
|
return m_Initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 CFileSystemGCWii::GetFileSize(const char* _rFullPath) const
|
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
2009-09-13 22:03:18 +00:00
|
|
|
if (pFileInfo != NULL && !pFileInfo->IsDirectory())
|
2008-12-08 05:30:24 +00:00
|
|
|
return pFileInfo->m_FileSize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CFileSystemGCWii::GetFileName(u64 _Address) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 22:03:18 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 CFileSystemGCWii::ReadFile(const char* _rFullPath, u8* _pBuffer, size_t _MaxBufferSize) const
|
|
|
|
{
|
|
|
|
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) const
|
|
|
|
{
|
2008-12-20 18:46:49 +00:00
|
|
|
size_t filesize = (size_t) GetFileSize(_rFullPath);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2009-09-13 22:03:18 +00:00
|
|
|
|
|
|
|
void CFileSystemGCWii::ExportApploader(const char* _rExportFolder) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-09-13 09:03:38 +00:00
|
|
|
char exportName[512];
|
2009-09-13 22:03:18 +00:00
|
|
|
|
|
|
|
// Extract Apploader
|
|
|
|
// ------------------
|
|
|
|
// Apploader code size
|
|
|
|
u32 AppSize = Read32(0x2440 + 0x14) << m_OffsetShift;
|
|
|
|
AppSize += Read32(0x2440 + 0x18) << m_OffsetShift; // + apptrailer size
|
|
|
|
AppSize += 0x20; // + the header = Apploader size! :')
|
|
|
|
DEBUG_LOG(DISCIO,"AppSize -> %x",AppSize);
|
|
|
|
|
|
|
|
char* buffer = new char[AppSize];
|
|
|
|
m_rVolume->Read(0x2440, AppSize, (u8*)buffer);
|
|
|
|
sprintf(exportName, "%s/apploader.ldr", _rExportFolder);
|
|
|
|
FILE* AppFile = fopen(exportName,"w");
|
|
|
|
if (!AppFile)
|
2009-09-13 09:03:38 +00:00
|
|
|
{
|
2009-09-13 22:03:18 +00:00
|
|
|
PanicAlert("Failed to create %s! canceling further extraction",exportName);
|
|
|
|
return;
|
2009-09-13 09:03:38 +00:00
|
|
|
}
|
2009-09-13 22:03:18 +00:00
|
|
|
fwrite(buffer, 1, AppSize, AppFile);
|
|
|
|
fclose(AppFile);
|
|
|
|
//delete[] buffer; buffer = 0;
|
|
|
|
|
|
|
|
/* TODO : This part crashes with Wii games :/
|
|
|
|
// Extract dol(bootfile)
|
|
|
|
// ---------------------
|
|
|
|
u32 DolOffset = Read32(0x420) << m_OffsetShift;
|
|
|
|
u32 DolSize = 0, offset = 0, size = 0, max = 0;
|
|
|
|
// note DacoTaco : thank you shuffle and discscrubber :P . find it kinda of pointless to include
|
|
|
|
// the discscrubber just for the GetDolSize function so its copy pasta time ...
|
|
|
|
// TODO: fix boot.dol size. or gc-tool is again silly with size or we are wrong (more likely :/)
|
|
|
|
|
|
|
|
// Iterate through the 7 code segments
|
|
|
|
for (u8 i = 0; i < 7; i++)
|
2009-09-13 09:03:38 +00:00
|
|
|
{
|
2009-09-13 22:03:18 +00:00
|
|
|
m_rVolume->Read(DolOffset + 0x00 + i * 4, 4, (u8*)&offset);
|
|
|
|
m_rVolume->Read(DolOffset + 0x90 + i * 4, 4, (u8*)&size);
|
|
|
|
if (offset + size > DolSize)
|
|
|
|
DolSize = offset + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the 11 data segments
|
|
|
|
for (u8 i = 0; i < 11; i++)
|
|
|
|
{
|
|
|
|
m_rVolume->Read(DolOffset + 0x1c + i * 4, 4, (u8*)&offset);
|
|
|
|
m_rVolume->Read(DolOffset + 0xac + i * 4, 4, (u8*)&size);
|
|
|
|
if (offset + size > DolSize)
|
|
|
|
DolSize = offset + size;
|
2009-09-13 09:03:38 +00:00
|
|
|
}
|
2009-09-13 22:03:18 +00:00
|
|
|
// Add header to size
|
|
|
|
DolSize += 0x40;
|
|
|
|
buffer = new char[DolSize];
|
|
|
|
m_rVolume->Read(DolOffset, DolSize, (u8*)&buffer);
|
|
|
|
sprintf(exportName, "%s/boot.dol", _rExportFolder);
|
|
|
|
FILE* DolFile = fopen(exportName, "w");
|
|
|
|
if (!DolFile)
|
2009-09-12 09:09:47 +00:00
|
|
|
{
|
2009-09-13 22:03:18 +00:00
|
|
|
PanicAlert("Failed to create %s! canceling further extraction",exportName);
|
|
|
|
return;
|
2009-09-12 09:09:47 +00:00
|
|
|
}
|
2009-09-13 22:03:18 +00:00
|
|
|
fwrite(buffer, 1, DolSize, DolFile);
|
|
|
|
fclose(DolFile);
|
|
|
|
delete[] buffer;
|
|
|
|
*/
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CFileSystemGCWii::GetFileList(std::vector<const SFileInfo *> &_rFilenames) const
|
|
|
|
{
|
|
|
|
if (_rFilenames.size())
|
|
|
|
PanicAlert("GetFileList : input list has contents?");
|
|
|
|
_rFilenames.clear();
|
|
|
|
_rFilenames.reserve(m_FileInfoVector.size());
|
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
|
|
|
_rFilenames.push_back(&m_FileInfoVector[i]);
|
|
|
|
return m_FileInfoVector.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SFileInfo* CFileSystemGCWii::FindFileInfo(const char* _rFullPath) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
|
|
|
{
|
|
|
|
if (!strcasecmp(m_FileInfoVector[i].m_FullPath, _rFullPath))
|
|
|
|
return &m_FileInfoVector[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFileSystemGCWii::InitFileSystem()
|
|
|
|
{
|
|
|
|
if (Read32(0x18) == 0x5D1C9EA3)
|
|
|
|
{
|
|
|
|
m_OffsetShift = 2; // Wii file system
|
|
|
|
}
|
|
|
|
else if (Read32(0x1c) == 0xC2339F3D)
|
|
|
|
{
|
|
|
|
m_OffsetShift = 0; // GC file system
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the whole FST
|
|
|
|
u64 FSTOffset = (u64)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())
|
|
|
|
{
|
|
|
|
if (m_FileInfoVector.size())
|
|
|
|
PanicAlert("Wtf?");
|
|
|
|
u64 NameTableOffset = FSTOffset;
|
|
|
|
|
2009-01-24 01:50:23 +00:00
|
|
|
m_FileInfoVector.reserve((unsigned int)Root.m_FileSize);
|
2008-12-08 05:30:24 +00:00
|
|
|
for (u32 i = 0; i < Root.m_FileSize; i++)
|
|
|
|
{
|
|
|
|
SFileInfo sfi;
|
|
|
|
u64 Offset = FSTOffset + (i * 0xC);
|
|
|
|
sfi.m_NameOffset = Read32(Offset + 0x0);
|
|
|
|
sfi.m_Offset = (u64)Read32(Offset + 0x4) << m_OffsetShift;
|
|
|
|
sfi.m_FileSize = Read32(Offset + 0x8);
|
|
|
|
|
|
|
|
m_FileInfoVector.push_back(sfi);
|
|
|
|
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];
|
|
|
|
memset(filename, 0, sizeof(filename));
|
|
|
|
GetStringFromOffset(uOffset, filename);
|
|
|
|
|
|
|
|
// check next index
|
|
|
|
if (rFileInfo->IsDirectory())
|
|
|
|
{
|
|
|
|
// this is a directory, build up the new szDirectory
|
|
|
|
if (_szDirectory != NULL)
|
2009-03-03 10:21:13 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s%s/", _szDirectory, filename);
|
2008-12-08 05:30:24 +00:00
|
|
|
else
|
2009-03-03 10:21:13 +00:00
|
|
|
CharArrayFromFormat(rFileInfo->m_FullPath, "%s/", filename);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-12-20 18:46:49 +00:00
|
|
|
CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t) rFileInfo->m_FileSize, rFileInfo->m_FullPath, _NameTableOffset);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
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
|