nod/lib/NOD.cpp

87 lines
1.9 KiB
C++
Raw Normal View History

2015-06-28 05:43:53 +00:00
#include <stdio.h>
2015-07-02 18:33:55 +00:00
#include "NOD/NOD.hpp"
#include "NOD/DiscBase.hpp"
2015-06-28 05:43:53 +00:00
2015-07-22 19:01:28 +00:00
#if _WIN32
#define fseeko _fseeki64
#endif
2015-06-28 05:43:53 +00:00
namespace NOD
{
LogVisor::LogModule LogModule("NODLib");
2015-07-02 18:33:55 +00:00
std::unique_ptr<IDiscIO> NewDiscIOISO(const SystemChar* path);
std::unique_ptr<IDiscIO> NewDiscIOWBFS(const SystemChar* path);
2015-06-28 05:43:53 +00:00
2015-07-02 18:33:55 +00:00
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path, bool& isWii)
2015-06-28 05:43:53 +00:00
{
/* Temporary file handle to determine image type */
2015-07-02 20:37:07 +00:00
#if NOD_UCS2
FILE* fp = _wfopen(path, L"rb");
#else
2015-06-28 05:43:53 +00:00
FILE* fp = fopen(path, "rb");
2015-07-02 20:37:07 +00:00
#endif
2015-06-28 05:43:53 +00:00
if (!fp)
{
2015-07-04 22:02:37 +00:00
LogModule.report(LogVisor::Error, _S("Unable to open '%s'"), path);
2015-06-28 05:43:53 +00:00
return std::unique_ptr<DiscBase>();
}
isWii = false;
std::unique_ptr<IDiscIO> discIO;
uint32_t magic;
fread(&magic, 1, 4, fp);
if (magic == NOD::SBig((uint32_t)'WBFS'))
{
fclose(fp);
discIO = NewDiscIOWBFS(path);
isWii = true;
}
else
{
fseeko(fp, 0x18, SEEK_SET);
2015-06-28 05:43:53 +00:00
fread(&magic, 1, 4, fp);
magic = NOD::SBig(magic);
if (magic == 0x5D1C9EA3)
{
fclose(fp);
discIO = NewDiscIOISO(path);
isWii = true;
}
else
{
fread(&magic, 1, 4, fp);
magic = NOD::SBig(magic);
if (magic == 0xC2339F3D)
{
fclose(fp);
discIO = NewDiscIOISO(path);
}
2015-07-02 18:33:55 +00:00
else
fclose(fp);
2015-06-28 05:43:53 +00:00
}
}
if (!discIO)
{
2015-07-04 22:02:37 +00:00
LogModule.report(LogVisor::Error, _S("'%s' is not a valid image"), path);
2015-06-28 05:43:53 +00:00
return std::unique_ptr<DiscBase>();
}
if (isWii)
return std::unique_ptr<DiscBase>(new DiscWii(std::move(discIO)));
return std::unique_ptr<DiscBase>(new DiscGCN(std::move(discIO)));
}
2015-07-02 18:33:55 +00:00
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path)
2015-06-29 08:59:54 +00:00
{
bool isWii;
return OpenDiscFromImage(path, isWii);
}
2015-06-28 05:43:53 +00:00
}