2016-03-04 23:04:30 +00:00
|
|
|
#include "nod/nod.hpp"
|
2019-08-30 07:52:22 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
2016-03-04 23:04:30 +00:00
|
|
|
#include "nod/DiscBase.hpp"
|
2019-08-30 07:52:22 +00:00
|
|
|
#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 {
|
2015-07-04 06:02:12 +00:00
|
|
|
|
2016-03-04 23:04:30 +00:00
|
|
|
logvisor::Module LogModule("nod");
|
2015-07-04 06:02:12 +00:00
|
|
|
|
2021-06-22 19:16:56 +00:00
|
|
|
std::unique_ptr<IDiscIO> NewDiscIOISO(std::string_view path);
|
|
|
|
std::unique_ptr<IDiscIO> NewDiscIOWBFS(std::string_view path);
|
|
|
|
std::unique_ptr<IDiscIO> NewDiscIONFS(std::string_view path);
|
2015-06-28 05:43:53 +00:00
|
|
|
|
2021-06-22 19:16:56 +00:00
|
|
|
std::unique_ptr<DiscBase> OpenDiscFromImage(std::string_view path, bool& isWii) {
|
2018-12-08 05:21:47 +00:00
|
|
|
/* Temporary file handle to determine image type */
|
|
|
|
std::unique_ptr<IFileIO> fio = NewFileIO(path);
|
|
|
|
if (!fio->exists()) {
|
2021-06-22 19:16:56 +00:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("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) {
|
2021-06-22 19:16:56 +00:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("Unable to read magic from '{}'"), path);
|
2018-12-08 05:21:47 +00:00
|
|
|
return {};
|
|
|
|
}
|
2016-01-25 05:55:31 +00:00
|
|
|
|
2021-06-22 19:16:56 +00:00
|
|
|
using SignedSize = std::make_signed<std::string::size_type>::type;
|
|
|
|
const auto dotPos = SignedSize(path.rfind('.'));
|
|
|
|
const auto slashPos = SignedSize(path.find_last_of("/\\"));
|
2018-12-08 05:21:47 +00:00
|
|
|
if (magic == nod::SBig((uint32_t)'WBFS')) {
|
|
|
|
discIO = NewDiscIOWBFS(path);
|
|
|
|
isWii = true;
|
2019-11-24 03:24:33 +00:00
|
|
|
} else if (path.size() > 4 && dotPos != -1 && dotPos > slashPos &&
|
2021-06-22 19:16:56 +00:00
|
|
|
!path.compare(slashPos + 1, 4, "hif_") &&
|
|
|
|
!path.compare(dotPos, path.size() - dotPos, ".nfs")) {
|
2019-11-24 03:24:33 +00:00
|
|
|
discIO = NewDiscIONFS(path);
|
|
|
|
isWii = true;
|
2018-12-08 05:21:47 +00:00
|
|
|
} 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) {
|
2021-06-22 19:16:56 +00:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("'{}' is not a valid image"), path);
|
2018-12-08 05:21:47 +00:00
|
|
|
return {};
|
|
|
|
}
|
2015-06-28 05:43:53 +00:00
|
|
|
|
2019-08-30 09:02:35 +00:00
|
|
|
bool err = false;
|
2018-12-08 05:21:47 +00:00
|
|
|
std::unique_ptr<DiscBase> ret;
|
|
|
|
if (isWii) {
|
2021-06-22 19:16:56 +00:00
|
|
|
ret = std::make_unique<DiscWii>(std::move(discIO), err);
|
2019-11-24 23:47:48 +00:00
|
|
|
if (err)
|
|
|
|
return {};
|
2017-02-05 06:19:34 +00:00
|
|
|
return ret;
|
2018-12-08 05:21:47 +00:00
|
|
|
}
|
2015-06-28 05:43:53 +00:00
|
|
|
|
2021-06-22 19:16:56 +00:00
|
|
|
ret = std::make_unique<DiscGCN>(std::move(discIO), err);
|
2019-11-24 23:47:48 +00:00
|
|
|
if (err)
|
|
|
|
return {};
|
2018-12-08 05:21:47 +00:00
|
|
|
return ret;
|
2015-06-29 08:59:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 19:16:56 +00:00
|
|
|
std::unique_ptr<DiscBase> OpenDiscFromImage(std::string_view path) {
|
2018-12-08 05:21:47 +00:00
|
|
|
bool isWii;
|
2021-06-22 19:16:56 +00:00
|
|
|
return OpenDiscFromImage(path, isWii);
|
2015-06-28 05:43:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:21:47 +00:00
|
|
|
} // namespace nod
|