nod/lib/nod.cpp

79 lines
1.9 KiB
C++
Raw Normal View History

2016-03-04 23:04:30 +00:00
#include "nod/nod.hpp"
#include <cstdio>
2016-03-04 23:04:30 +00:00
#include "nod/DiscBase.hpp"
#include "nod/DiscGCN.hpp"
#include "nod/DiscWii.hpp"
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
namespace nod {
2016-03-04 23:04:30 +00:00
logvisor::Module LogModule("nod");
2017-11-13 06:18:53 +00:00
std::unique_ptr<IDiscIO> NewDiscIOISO(SystemStringView path);
std::unique_ptr<IDiscIO> NewDiscIOWBFS(SystemStringView path);
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path, bool& isWii) {
/* Temporary file handle to determine image type */
std::unique_ptr<IFileIO> fio = NewFileIO(path);
if (!fio->exists()) {
2019-07-20 04:21:57 +00:00
LogModule.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}'")), path);
2018-12-08 05:21:47 +00:00
return {};
}
std::unique_ptr<IFileIO::IReadStream> rs = fio->beginReadStream();
if (!rs)
return {};
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
isWii = false;
std::unique_ptr<IDiscIO> discIO;
uint32_t magic = 0;
if (rs->read(&magic, 4) != 4) {
2019-07-20 04:21:57 +00:00
LogModule.report(logvisor::Error, fmt(_SYS_STR("Unable to read magic from '{}'")), path);
2018-12-08 05:21:47 +00:00
return {};
}
2016-01-25 05:55:31 +00:00
2018-12-08 05:21:47 +00:00
if (magic == nod::SBig((uint32_t)'WBFS')) {
discIO = NewDiscIOWBFS(path);
isWii = true;
} else {
rs->seek(0x18, SEEK_SET);
rs->read(&magic, 4);
magic = nod::SBig(magic);
if (magic == 0x5D1C9EA3) {
discIO = NewDiscIOISO(path);
isWii = true;
} else {
rs->read(&magic, 4);
magic = nod::SBig(magic);
if (magic == 0xC2339F3D)
discIO = NewDiscIOISO(path);
2015-06-28 05:43:53 +00:00
}
2018-12-08 05:21:47 +00:00
}
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
if (!discIO) {
2019-07-20 04:21:57 +00:00
LogModule.report(logvisor::Error, fmt(_SYS_STR("'{}' is not a valid image")), path);
2018-12-08 05:21:47 +00:00
return {};
}
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
bool Err = false;
std::unique_ptr<DiscBase> ret;
if (isWii) {
ret = std::unique_ptr<DiscBase>(new DiscWii(std::move(discIO), Err));
if (Err)
2018-12-08 05:21:47 +00:00
return {};
return ret;
2018-12-08 05:21:47 +00:00
}
2015-06-28 05:43:53 +00:00
2018-12-08 05:21:47 +00:00
ret = std::unique_ptr<DiscBase>(new DiscGCN(std::move(discIO), Err));
if (Err)
return {};
return ret;
2015-06-29 08:59:54 +00:00
}
2018-12-08 05:21:47 +00:00
std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path) {
bool isWii;
return OpenDiscFromImage(path, isWii);
2015-06-28 05:43:53 +00:00
}
2018-12-08 05:21:47 +00:00
} // namespace nod