mirror of
https://github.com/AxioDL/nod.git
synced 2025-12-08 13:14:59 +00:00
Compare commits
102 Commits
v1.0
...
d14b798b5f
| Author | SHA1 | Date | |
|---|---|---|---|
| d14b798b5f | |||
| 1f110f3549 | |||
| 311d20532e | |||
| 8fdc893c86 | |||
| fdc8be487d | |||
| 3a21961a4e | |||
| da399b5b67 | |||
| 11c734be47 | |||
| 364787604d | |||
| 02c188497a | |||
| d53d677038 | |||
| 5b1b6f6f80 | |||
| 393a11ffb5 | |||
|
|
2783337c36 | ||
|
|
dffcac50c5 | ||
|
|
f147e12356 | ||
|
|
48a2981a93 | ||
|
|
19604b2a3b | ||
|
|
c1a1d1abc8 | ||
|
|
6bf4f07129 | ||
|
|
75fc574f81 | ||
|
|
11a0351d1c | ||
|
|
4ec6c6697b | ||
| ba0c2b7843 | |||
|
|
221bc7c7f2 | ||
| acdadaf963 | |||
|
d658909948
|
|||
|
|
091262ace1 | ||
| 97cfcea14e | |||
|
|
f5c3cbdcd7 | ||
|
|
7ddff919c1 | ||
|
|
4bba7af2c2 | ||
|
|
f443b60bde | ||
|
|
998d6a77c3 | ||
| 55301dd505 | |||
| 793413540d | |||
|
|
f11eb728bf | ||
|
|
a8753e273f | ||
| 63264695b0 | |||
| edc31b2107 | |||
|
|
1b3bb7815d | ||
|
|
89df98ee96 | ||
|
|
5935e84dab | ||
|
df1e450728
|
|||
| c9bf821285 | |||
|
|
18b297e312 | ||
| 255a37216f | |||
| 7da7761afb | |||
|
|
876a2ccf81 | ||
|
|
2171388b9d | ||
|
|
a572439967 | ||
|
|
ac6f2a1ed2 | ||
|
|
37792ba116 | ||
| d9b6be8446 | |||
|
|
47322b9496 | ||
|
|
77013bbd9f | ||
|
|
34b943c40f | ||
|
|
a1284ae065 | ||
|
|
4dd0375cae | ||
|
|
01237372e1 | ||
|
|
d9638cc60d | ||
|
|
0ac7140542 | ||
|
|
a5e9166194 | ||
|
|
ca2aeecc64 | ||
|
|
cd782047c8 | ||
|
|
2b7ea07cae | ||
|
|
ed28576b99 | ||
|
|
95ed2ae7dc | ||
|
|
f1c76a475d | ||
|
|
be8409681f | ||
|
|
3d380fdc3b | ||
|
|
f87b286ff3 | ||
|
|
e964a013fe | ||
|
|
eb6aa30563 | ||
| 1ad101897c | |||
|
|
6f777ebb48 | ||
|
|
42589c3604 | ||
|
|
4d9071bad7 | ||
|
|
d5f5db440c | ||
|
|
51a15e474e | ||
|
|
274a63bb30 | ||
|
|
bab7aab6fa | ||
|
|
5197abc131 | ||
|
|
648c015383 | ||
|
|
bf00fcd10f | ||
|
|
a557f86974 | ||
|
|
34de6276b0 | ||
|
|
3d70a568dc | ||
| 63ae60a967 | |||
|
|
e20fce1e6f | ||
|
|
b5916af702 | ||
|
|
58ceb47b25 | ||
|
|
69e96e3b3c | ||
|
|
27a2cb5998 | ||
|
|
c374038103 | ||
|
|
db1a6f13a2 | ||
|
|
fb2a5c91d2 | ||
| 42ef3a7958 | |||
|
|
d597400f4a | ||
| e99290e3c3 | |||
| 72169e8e77 | |||
| a7c19799e1 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,4 @@
|
||||
[submodule "logvisor"]
|
||||
path = logvisor
|
||||
url = https://github.com/AxioDL/logvisor.git
|
||||
url = ../logvisor.git
|
||||
branch = master
|
||||
|
||||
101
CMakeLists.txt
101
CMakeLists.txt
@@ -1,18 +1,99 @@
|
||||
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) # because of CMAKE_CXX_STANDARD
|
||||
project(nod)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
cmake_minimum_required(VERSION 3.10 FATAL_ERROR) # because of c++17
|
||||
project(nod VERSION 0.1)
|
||||
|
||||
if (MSVC)
|
||||
# Shaddup MSVC
|
||||
add_compile_definitions(UNICODE=1 _UNICODE=1 __SSE__=1
|
||||
_CRT_SECURE_NO_WARNINGS=1 D_SCL_SECURE_NO_WARNINGS=1
|
||||
_SCL_SECURE_NO_DEPRECATE=1 _CRT_NONSTDC_NO_WARNINGS=1
|
||||
_ENABLE_EXTENDED_ALIGNED_STORAGE=1 NOMINMAX=1)
|
||||
add_compile_options(/IGNORE:4221 /wd4018 /wd4800 /wd4005 /wd4311 /wd4068
|
||||
/wd4267 /wd4244 /wd4200 /wd4305 /wd4067 /wd4146 /wd4309 /wd4805 ${VS_OPTIONS})
|
||||
|
||||
add_compile_options(
|
||||
# Disable exceptions
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/EHsc->
|
||||
|
||||
# Disable RTTI
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/GR->
|
||||
|
||||
# Enforce various standards compliant behavior.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/permissive->
|
||||
|
||||
# Enable standard volatile semantics.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/volatile:iso>
|
||||
|
||||
# Reports the proper value for the __cplusplus preprocessor macro.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>
|
||||
|
||||
# Use latest C++ standard.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/std:c++latest>
|
||||
)
|
||||
add_compile_definitions(FMT_EXCEPTIONS=0 _HAS_EXCEPTIONS=0)
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
# Flags for MSVC (not clang-cl)
|
||||
add_compile_options(
|
||||
# Allow constexpr variables to have explicit external linkage.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/Zc:externConstexpr>
|
||||
|
||||
# Assume that new throws exceptions, allowing better code generation.
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/Zc:throwingNew>
|
||||
|
||||
# Link-time Code Generation for Release builds
|
||||
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:/GL>
|
||||
)
|
||||
|
||||
# Link-time Code Generation for Release builds
|
||||
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "/LTCG")
|
||||
set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "/LTCG")
|
||||
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
|
||||
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
|
||||
endif()
|
||||
else()
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
include (CMakePackageConfigHelpers)
|
||||
|
||||
if (NOT TARGET logvisor)
|
||||
add_subdirectory(logvisor)
|
||||
set(LOGVISOR_INCLUDE_DIR logvisor/include)
|
||||
add_subdirectory(logvisor)
|
||||
endif()
|
||||
|
||||
include_directories(include ${LOGVISOR_INCLUDE_DIR})
|
||||
file(GLOB NOD_HEADERS include/nod/*.h*)
|
||||
|
||||
add_subdirectory(lib)
|
||||
add_subdirectory(driver)
|
||||
install(DIRECTORY include/nod DESTINATION include/nod)
|
||||
|
||||
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||
set(version_config_file "${PROJECT_BINARY_DIR}/nodConfigVersion.cmake")
|
||||
set(config_file "${PROJECT_BINARY_DIR}/nodConfig.cmake")
|
||||
set(config_install_dir "lib/cmake/nod")
|
||||
|
||||
# Install the target config files
|
||||
install(
|
||||
EXPORT nodTargets
|
||||
NAMESPACE "nod::"
|
||||
DESTINATION "${config_install_dir}"
|
||||
)
|
||||
|
||||
# Generate version config file
|
||||
write_basic_package_version_file(
|
||||
"${version_config_file}"
|
||||
COMPATIBILITY SameMajorVersion
|
||||
)
|
||||
|
||||
# Generate config file
|
||||
configure_package_config_file(
|
||||
"Config.cmake.in"
|
||||
"${config_file}"
|
||||
INSTALL_DESTINATION "lib/cmake/nod"
|
||||
)
|
||||
|
||||
# Install the config files
|
||||
install(
|
||||
FILES "${config_file}" "${version_config_file}"
|
||||
DESTINATION ${config_install_dir}
|
||||
)
|
||||
endif()
|
||||
4
Config.cmake.in
Normal file
4
Config.cmake.in
Normal file
@@ -0,0 +1,4 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/nodTargets.cmake")
|
||||
check_required_components(nod)
|
||||
@@ -41,12 +41,12 @@ auto progFunc = [&](size_t idx, const nod::SystemString& name, size_t bytes)
|
||||
lastIdx = idx;
|
||||
/* NOD provides I/O wrappers using wchar_t on Windows;
|
||||
* _S() conditionally makes string-literals wide */
|
||||
nod::Printf(_S("\n"));
|
||||
fmt::print(_S("\n"));
|
||||
}
|
||||
if (bytes != -1)
|
||||
nod::Printf(_S("\r%s %" PRISize " B"), name.c_str(), bytes);
|
||||
fmt::print(_S("\r{} {} B"), name, bytes);
|
||||
else
|
||||
nod::Printf(_S("\r%s"), name.c_str());
|
||||
fmt::print(_S("\r{}"), name);
|
||||
fflush(stdout);
|
||||
};
|
||||
|
||||
|
||||
453
driver/main.cpp
453
driver/main.cpp
@@ -1,16 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "logvisor/logvisor.hpp"
|
||||
#include "nod/nod.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
static void printHelp()
|
||||
{
|
||||
fprintf(stderr, "Usage:\n"
|
||||
" nodtool extract [-f] <image-in> [<dir-out>]\n"
|
||||
" nodtool makegcn <fsroot-in> [<image-out>]\n"
|
||||
" nodtool makewii <fsroot-in> [<image-out>]\n"
|
||||
" nodtool mergegcn <fsroot-in> <image-in> [<image-out>]\n"
|
||||
" nodtool mergewii <fsroot-in> <image-in> [<image-out>]\n");
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
#include <nod/DiscBase.hpp>
|
||||
#include <nod/DiscGCN.hpp>
|
||||
#include <nod/DiscWii.hpp>
|
||||
#include <nod/nod.hpp>
|
||||
|
||||
static void printHelp() {
|
||||
fmt::print(stderr, FMT_STRING(
|
||||
"Usage:\n"
|
||||
" nodtool extract [-f] <image-in> [<dir-out>]\n"
|
||||
" nodtool makegcn <fsroot-in> [<image-out>]\n"
|
||||
" nodtool makewii <fsroot-in> [<image-out>]\n"
|
||||
" nodtool mergegcn <fsroot-in> <image-in> [<image-out>]\n"
|
||||
" nodtool mergewii <fsroot-in> <image-in> [<image-out>]\n"));
|
||||
}
|
||||
|
||||
#if NOD_UCS2
|
||||
@@ -25,241 +32,203 @@ int wmain(int argc, wchar_t* argv[])
|
||||
int main(int argc, char* argv[])
|
||||
#endif
|
||||
{
|
||||
if (argc < 3 ||
|
||||
(!strcasecmp(argv[1], _S("makegcn")) && argc < 3) ||
|
||||
(!strcasecmp(argv[1], _S("makewii")) && argc < 3) ||
|
||||
(!strcasecmp(argv[1], _S("mergegcn")) && argc < 4) ||
|
||||
(!strcasecmp(argv[1], _S("mergewii")) && argc < 4))
|
||||
{
|
||||
printHelp();
|
||||
return 1;
|
||||
}
|
||||
if (argc < 3 || (!strcasecmp(argv[1], _SYS_STR("makegcn")) && argc < 3) ||
|
||||
(!strcasecmp(argv[1], _SYS_STR("makewii")) && argc < 3) ||
|
||||
(!strcasecmp(argv[1], _SYS_STR("mergegcn")) && argc < 4) ||
|
||||
(!strcasecmp(argv[1], _SYS_STR("mergewii")) && argc < 4)) {
|
||||
printHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Enable logging to console */
|
||||
logvisor::RegisterStandardExceptions();
|
||||
logvisor::RegisterConsoleLogger();
|
||||
/* Enable logging to console */
|
||||
logvisor::RegisterStandardExceptions();
|
||||
logvisor::RegisterConsoleLogger();
|
||||
|
||||
bool verbose = false;
|
||||
nod::ExtractionContext ctx = {true,
|
||||
[&](const std::string& str, float c) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "Current node: %s, Extraction %g%% Complete\n", str.c_str(), c * 100.f);
|
||||
}};
|
||||
const nod::SystemChar* inDir = nullptr;
|
||||
const nod::SystemChar* outDir = _S(".");
|
||||
bool verbose = false;
|
||||
nod::ExtractionContext ctx = {true, [&](std::string_view str, float c) {
|
||||
if (verbose)
|
||||
fmt::print(stderr, FMT_STRING("Current node: {}, Extraction {:g}% Complete\n"), str,
|
||||
c * 100.f);
|
||||
}};
|
||||
const nod::SystemChar* inDir = nullptr;
|
||||
const nod::SystemChar* outDir = _SYS_STR(".");
|
||||
|
||||
for (int a=2 ; a<argc ; ++a)
|
||||
{
|
||||
if (argv[a][0] == '-' && argv[a][1] == 'f')
|
||||
ctx.force = true;
|
||||
else if (argv[a][0] == '-' && argv[a][1] == 'v')
|
||||
verbose = true;
|
||||
for (int a = 2; a < argc; ++a) {
|
||||
if (argv[a][0] == '-' && argv[a][1] == 'f')
|
||||
ctx.force = true;
|
||||
else if (argv[a][0] == '-' && argv[a][1] == 'v')
|
||||
verbose = true;
|
||||
|
||||
else if (!inDir)
|
||||
inDir = argv[a];
|
||||
else
|
||||
outDir = argv[a];
|
||||
}
|
||||
|
||||
auto progFunc = [&](float prog, const nod::SystemString& name, size_t bytes)
|
||||
{
|
||||
nod::Printf(_S("\r "));
|
||||
if (bytes != -1)
|
||||
nod::Printf(_S("\r%g%% %s %" PRISize " B"), prog * 100.f, name.c_str(), bytes);
|
||||
else
|
||||
nod::Printf(_S("\r%g%% %s"), prog * 100.f, name.c_str());
|
||||
fflush(stdout);
|
||||
};
|
||||
|
||||
if (!strcasecmp(argv[1], _S("extract")))
|
||||
{
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(inDir, isWii);
|
||||
if (!disc)
|
||||
return 1;
|
||||
|
||||
nod::Mkdir(outDir, 0755);
|
||||
|
||||
nod::Partition* dataPart = disc->getDataPartition();
|
||||
if (!dataPart)
|
||||
return 1;
|
||||
|
||||
if (!dataPart->extractToDirectory(outDir, ctx))
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(argv[1], _S("makegcn")))
|
||||
{
|
||||
/* Pre-validate path */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nod::DiscBuilderGCN::CalculateTotalSizeRequired(argv[2]) == -1)
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 4)
|
||||
{
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_S(".iso"));
|
||||
nod::DiscBuilderGCN b(outPath.c_str(), progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
nod::DiscBuilderGCN b(argv[3], progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(argv[1], _S("makewii")))
|
||||
{
|
||||
/* Pre-validate path */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[4]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool dual = false;
|
||||
if (nod::DiscBuilderWii::CalculateTotalSizeRequired(argv[2], dual) == -1)
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 4)
|
||||
{
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_S(".iso"));
|
||||
nod::DiscBuilderWii b(outPath.c_str(), dual, progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
nod::DiscBuilderWii b(argv[3], dual, progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(argv[1], _S("mergegcn")))
|
||||
{
|
||||
/* Pre-validate paths */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as file"), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
|
||||
if (!disc)
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to open image %s"), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
if (isWii)
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("Wii images should be merged with 'mergewii'"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nod::DiscMergerGCN::CalculateTotalSizeRequired(static_cast<nod::DiscGCN&>(*disc), argv[2]) == -1)
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 5)
|
||||
{
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_S(".iso"));
|
||||
nod::DiscMergerGCN b(outPath.c_str(), static_cast<nod::DiscGCN&>(*disc), progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
nod::DiscMergerGCN b(argv[4], static_cast<nod::DiscGCN&>(*disc), progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(argv[1], _S("mergewii")))
|
||||
{
|
||||
/* Pre-validate paths */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode))
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as file"), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
|
||||
if (!disc)
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("unable to open image %s"), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
if (!isWii)
|
||||
{
|
||||
nod::LogModule.report(logvisor::Error, _S("GameCube images should be merged with 'mergegcn'"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool dual = false;
|
||||
if (nod::DiscMergerWii::CalculateTotalSizeRequired(static_cast<nod::DiscWii&>(*disc), argv[2], dual) == -1)
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 5)
|
||||
{
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_S(".iso"));
|
||||
nod::DiscMergerWii b(outPath.c_str(), static_cast<nod::DiscWii&>(*disc), dual, progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
nod::DiscMergerWii b(argv[4], static_cast<nod::DiscWii&>(*disc), dual, progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
}
|
||||
else if (!inDir)
|
||||
inDir = argv[a];
|
||||
else
|
||||
{
|
||||
printHelp();
|
||||
return 1;
|
||||
outDir = argv[a];
|
||||
}
|
||||
|
||||
auto progFunc = [&](float prog, nod::SystemStringView name, size_t bytes) {
|
||||
fmt::print(FMT_STRING(_SYS_STR("\r ")));
|
||||
if (bytes != SIZE_MAX)
|
||||
fmt::print(FMT_STRING(_SYS_STR("\r{:g}% {} {} B")), prog * 100.f, name, bytes);
|
||||
else
|
||||
fmt::print(FMT_STRING(_SYS_STR("\r{:g}% {}")), prog * 100.f, name);
|
||||
fflush(stdout);
|
||||
};
|
||||
|
||||
if (!strcasecmp(argv[1], _SYS_STR("extract"))) {
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(inDir, isWii);
|
||||
if (!disc)
|
||||
return 1;
|
||||
|
||||
nod::Mkdir(outDir, 0755);
|
||||
|
||||
nod::IPartition* dataPart = disc->getDataPartition();
|
||||
if (!dataPart)
|
||||
return 1;
|
||||
|
||||
if (!dataPart->extractToDirectory(outDir, ctx))
|
||||
return 1;
|
||||
} else if (!strcasecmp(argv[1], _SYS_STR("makegcn"))) {
|
||||
/* Pre-validate path */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as directory")), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
nod::LogModule.report(logvisor::Info, _S("Success!"));
|
||||
return 0;
|
||||
}
|
||||
if (!nod::DiscBuilderGCN::CalculateTotalSizeRequired(argv[2]))
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 4) {
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_SYS_STR(".iso"));
|
||||
nod::DiscBuilderGCN b(outPath, progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
} else {
|
||||
nod::DiscBuilderGCN b(argv[3], progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
fmt::print(FMT_STRING("\n"));
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
} else if (!strcasecmp(argv[1], _SYS_STR("makewii"))) {
|
||||
/* Pre-validate path */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as directory")), argv[4]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool dual = false;
|
||||
if (!nod::DiscBuilderWii::CalculateTotalSizeRequired(argv[2], dual))
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 4) {
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_SYS_STR(".iso"));
|
||||
nod::DiscBuilderWii b(outPath.c_str(), dual, progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
} else {
|
||||
nod::DiscBuilderWii b(argv[3], dual, progFunc);
|
||||
ret = b.buildFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
fmt::print(FMT_STRING("\n"));
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
} else if (!strcasecmp(argv[1], _SYS_STR("mergegcn"))) {
|
||||
/* Pre-validate paths */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as directory")), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as file")), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
|
||||
if (!disc) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open image {}")), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
if (isWii) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Wii images should be merged with 'mergewii'")));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!nod::DiscMergerGCN::CalculateTotalSizeRequired(static_cast<nod::DiscGCN&>(*disc), argv[2]))
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 5) {
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_SYS_STR(".iso"));
|
||||
nod::DiscMergerGCN b(outPath.c_str(), static_cast<nod::DiscGCN&>(*disc), progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
} else {
|
||||
nod::DiscMergerGCN b(argv[4], static_cast<nod::DiscGCN&>(*disc), progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
fmt::print(FMT_STRING("\n"));
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
} else if (!strcasecmp(argv[1], _SYS_STR("mergewii"))) {
|
||||
/* Pre-validate paths */
|
||||
nod::Sstat theStat;
|
||||
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as directory")), argv[2]);
|
||||
return 1;
|
||||
}
|
||||
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode)) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {} as file")), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool isWii;
|
||||
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
|
||||
if (!disc) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open image {}")), argv[3]);
|
||||
return 1;
|
||||
}
|
||||
if (!isWii) {
|
||||
nod::LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("GameCube images should be merged with 'mergegcn'")));
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool dual = false;
|
||||
if (!nod::DiscMergerWii::CalculateTotalSizeRequired(static_cast<nod::DiscWii&>(*disc), argv[2], dual))
|
||||
return 1;
|
||||
|
||||
nod::EBuildResult ret;
|
||||
|
||||
if (argc < 5) {
|
||||
nod::SystemString outPath(argv[2]);
|
||||
outPath.append(_SYS_STR(".iso"));
|
||||
nod::DiscMergerWii b(outPath.c_str(), static_cast<nod::DiscWii&>(*disc), dual, progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
} else {
|
||||
nod::DiscMergerWii b(argv[4], static_cast<nod::DiscWii&>(*disc), dual, progFunc);
|
||||
ret = b.mergeFromDirectory(argv[2]);
|
||||
}
|
||||
|
||||
fmt::print(FMT_STRING("\n"));
|
||||
if (ret != nod::EBuildResult::Success)
|
||||
return 1;
|
||||
} else {
|
||||
printHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
nod::LogModule.report(logvisor::Info, FMT_STRING(_SYS_STR("Success!")));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,73 +1,54 @@
|
||||
#ifndef __NOD_DIRECTORY_ENUMERATOR__
|
||||
#define __NOD_DIRECTORY_ENUMERATOR__
|
||||
#pragma once
|
||||
|
||||
#include "Util.hpp"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
struct CaseInsensitiveCompare
|
||||
{
|
||||
bool operator()(const std::string& lhs, const std::string& rhs) const
|
||||
{
|
||||
#if _WIN32
|
||||
if (_stricmp(lhs.c_str(), rhs.c_str()) < 0)
|
||||
#else
|
||||
if (strcasecmp(lhs.c_str(), rhs.c_str()) < 0)
|
||||
#endif
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
namespace nod {
|
||||
|
||||
struct CaseInsensitiveCompare {
|
||||
bool operator()(std::string_view lhs, std::string_view rhs) const {
|
||||
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](char lhs, char rhs) {
|
||||
return std::tolower(static_cast<unsigned char>(lhs)) < std::tolower(static_cast<unsigned char>(rhs));
|
||||
});
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
bool operator()(const std::wstring& lhs, const std::wstring& rhs) const
|
||||
{
|
||||
if (_wcsicmp(lhs.c_str(), rhs.c_str()) < 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool operator()(std::wstring_view lhs, std::wstring_view rhs) const {
|
||||
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](wchar_t lhs, wchar_t rhs) {
|
||||
return std::towlower(lhs) < std::towlower(rhs);
|
||||
});
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
class DirectoryEnumerator
|
||||
{
|
||||
class DirectoryEnumerator {
|
||||
public:
|
||||
enum class Mode
|
||||
{
|
||||
Native,
|
||||
DirsSorted,
|
||||
FilesSorted,
|
||||
DirsThenFilesSorted
|
||||
};
|
||||
struct Entry
|
||||
{
|
||||
SystemString m_path;
|
||||
SystemString m_name;
|
||||
size_t m_fileSz;
|
||||
bool m_isDir;
|
||||
enum class Mode { Native, DirsSorted, FilesSorted, DirsThenFilesSorted };
|
||||
struct Entry {
|
||||
SystemString m_path;
|
||||
SystemString m_name;
|
||||
size_t m_fileSz;
|
||||
bool m_isDir;
|
||||
|
||||
private:
|
||||
friend class DirectoryEnumerator;
|
||||
Entry(SystemString&& path, const SystemChar* name, size_t sz, bool isDir)
|
||||
: m_path(std::move(path)), m_name(name), m_fileSz(sz), m_isDir(isDir) {}
|
||||
};
|
||||
Entry(const SystemString& path, const SystemChar* name, size_t sz, bool isDir)
|
||||
: m_path(path), m_name(name), m_fileSz(sz), m_isDir(isDir) {}
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<Entry> m_entries;
|
||||
std::vector<Entry> m_entries;
|
||||
|
||||
public:
|
||||
DirectoryEnumerator(const SystemString& path, Mode mode=Mode::DirsThenFilesSorted,
|
||||
bool sizeSort=false, bool reverse=false, bool noHidden=false)
|
||||
: DirectoryEnumerator(path.c_str(), mode, sizeSort, reverse, noHidden) {}
|
||||
DirectoryEnumerator(const SystemChar* path, Mode mode=Mode::DirsThenFilesSorted,
|
||||
bool sizeSort=false, bool reverse=false, bool noHidden=false);
|
||||
DirectoryEnumerator(SystemStringView path, Mode mode = Mode::DirsThenFilesSorted, bool sizeSort = false,
|
||||
bool reverse = false, bool noHidden = false);
|
||||
|
||||
operator bool() const {return m_entries.size() != 0;}
|
||||
size_t size() const {return m_entries.size();}
|
||||
std::vector<Entry>::const_iterator begin() const {return m_entries.cbegin();}
|
||||
std::vector<Entry>::const_iterator end() const {return m_entries.cend();}
|
||||
operator bool() const { return m_entries.size() != 0; }
|
||||
size_t size() const { return m_entries.size(); }
|
||||
std::vector<Entry>::const_iterator begin() const { return m_entries.cbegin(); }
|
||||
std::vector<Entry>::const_iterator end() const { return m_entries.cend(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __NOD_DIRECTORY_ENUMERATOR__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,529 +1,469 @@
|
||||
#ifndef __NOD_DISC_BASE__
|
||||
#define __NOD_DISC_BASE__
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <functional>
|
||||
#include "Util.hpp"
|
||||
#include "IDiscIO.hpp"
|
||||
#include "IFileIO.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include "nod/IDiscIO.hpp"
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
using FProgress = std::function<void(float totalProg, const SystemString& fileName, size_t fileBytesXfered)>;
|
||||
namespace nod {
|
||||
|
||||
enum class EBuildResult
|
||||
{
|
||||
Success,
|
||||
Failed,
|
||||
DiskFull
|
||||
};
|
||||
using FProgress = std::function<void(float totalProg, SystemStringView fileName, size_t fileBytesXfered)>;
|
||||
|
||||
enum class PartitionKind : uint32_t
|
||||
{
|
||||
Data,
|
||||
Update,
|
||||
Channel
|
||||
};
|
||||
enum class EBuildResult { Success, Failed, DiskFull };
|
||||
|
||||
enum class PartitionKind : uint32_t { Data, Update, Channel };
|
||||
const SystemChar* getKindString(PartitionKind kind);
|
||||
|
||||
class FSTNode
|
||||
{
|
||||
uint32_t typeAndNameOffset;
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
class FSTNode {
|
||||
uint32_t typeAndNameOffset;
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
|
||||
public:
|
||||
FSTNode(bool isDir, uint32_t nameOff, uint32_t off, uint32_t len)
|
||||
{
|
||||
typeAndNameOffset = nameOff & 0xffffff;
|
||||
typeAndNameOffset |= isDir << 24;
|
||||
typeAndNameOffset = SBig(typeAndNameOffset);
|
||||
offset = SBig(off);
|
||||
length = SBig(len);
|
||||
}
|
||||
inline bool isDir() const {return ((SBig(typeAndNameOffset) >> 24) != 0);}
|
||||
inline uint32_t getNameOffset() const {return SBig(typeAndNameOffset) & 0xffffff;}
|
||||
inline uint32_t getOffset() const {return SBig(offset);}
|
||||
inline uint32_t getLength() const {return SBig(length);}
|
||||
void incrementLength()
|
||||
{
|
||||
uint32_t orig = SBig(length);
|
||||
++orig;
|
||||
length = SBig(orig);
|
||||
}
|
||||
FSTNode(bool isDir, uint32_t nameOff, uint32_t off, uint32_t len) {
|
||||
typeAndNameOffset = nameOff & 0xffffff;
|
||||
typeAndNameOffset |= isDir << 24;
|
||||
typeAndNameOffset = SBig(typeAndNameOffset);
|
||||
offset = SBig(off);
|
||||
length = SBig(len);
|
||||
}
|
||||
bool isDir() const { return ((SBig(typeAndNameOffset) >> 24) != 0); }
|
||||
uint32_t getNameOffset() const { return SBig(typeAndNameOffset) & 0xffffff; }
|
||||
uint32_t getOffset() const { return SBig(offset); }
|
||||
uint32_t getLength() const { return SBig(length); }
|
||||
void incrementLength() {
|
||||
uint32_t orig = SBig(length);
|
||||
++orig;
|
||||
length = SBig(orig);
|
||||
}
|
||||
};
|
||||
|
||||
struct Header
|
||||
{
|
||||
char m_gameID[6];
|
||||
char m_discNum;
|
||||
char m_discVersion;
|
||||
char m_audioStreaming;
|
||||
char m_streamBufSz;
|
||||
char m_unk1[14];
|
||||
uint32_t m_wiiMagic;
|
||||
uint32_t m_gcnMagic;
|
||||
char m_gameTitle[64];
|
||||
char m_disableHashVerification;
|
||||
char m_disableDiscEnc;
|
||||
char m_unk2[0x39e];
|
||||
uint32_t m_debugMonOff;
|
||||
uint32_t m_debugLoadAddr;
|
||||
char m_unk3[0x18];
|
||||
uint32_t m_dolOff;
|
||||
uint32_t m_fstOff;
|
||||
uint32_t m_fstSz;
|
||||
uint32_t m_fstMaxSz;
|
||||
uint32_t m_fstMemoryAddress;
|
||||
uint32_t m_userPosition;
|
||||
uint32_t m_userSz;
|
||||
uint8_t padding1[4];
|
||||
struct Header {
|
||||
char m_gameID[6];
|
||||
char m_discNum;
|
||||
char m_discVersion;
|
||||
char m_audioStreaming;
|
||||
char m_streamBufSz;
|
||||
char m_unk1[14];
|
||||
uint32_t m_wiiMagic;
|
||||
uint32_t m_gcnMagic;
|
||||
char m_gameTitle[64];
|
||||
char m_disableHashVerification;
|
||||
char m_disableDiscEnc;
|
||||
char m_unk2[0x39e];
|
||||
uint32_t m_debugMonOff;
|
||||
uint32_t m_debugLoadAddr;
|
||||
char m_unk3[0x18];
|
||||
uint32_t m_dolOff;
|
||||
uint32_t m_fstOff;
|
||||
uint32_t m_fstSz;
|
||||
uint32_t m_fstMaxSz;
|
||||
uint32_t m_fstMemoryAddress;
|
||||
uint32_t m_userPosition;
|
||||
uint32_t m_userSz;
|
||||
uint8_t padding1[4];
|
||||
|
||||
Header() = default;
|
||||
Header(IDiscIO& dio, bool& err)
|
||||
{
|
||||
auto rs = dio.beginReadStream();
|
||||
if (!rs)
|
||||
{
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
read(*rs);
|
||||
Header() = default;
|
||||
Header(IDiscIO& dio, bool& err) {
|
||||
auto rs = dio.beginReadStream();
|
||||
if (!rs) {
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
read(*rs);
|
||||
}
|
||||
|
||||
void read(IReadStream& s)
|
||||
{
|
||||
memset(this, 0, sizeof(*this));
|
||||
s.read(this, sizeof(*this));
|
||||
m_wiiMagic = SBig(m_wiiMagic);
|
||||
m_gcnMagic = SBig(m_gcnMagic);
|
||||
m_debugMonOff = SBig(m_debugMonOff);
|
||||
m_debugLoadAddr = SBig(m_debugLoadAddr);
|
||||
m_dolOff = SBig(m_dolOff);
|
||||
m_fstOff = SBig(m_fstOff);
|
||||
m_fstSz = SBig(m_fstSz);
|
||||
m_fstMaxSz = SBig(m_fstMaxSz);
|
||||
m_fstMemoryAddress = SBig(m_fstMemoryAddress);
|
||||
m_userPosition = SBig(m_userPosition);
|
||||
m_userSz = SBig(m_userSz);
|
||||
}
|
||||
void read(IReadStream& s) {
|
||||
memset(this, 0, sizeof(*this));
|
||||
s.read(this, sizeof(*this));
|
||||
m_wiiMagic = SBig(m_wiiMagic);
|
||||
m_gcnMagic = SBig(m_gcnMagic);
|
||||
m_debugMonOff = SBig(m_debugMonOff);
|
||||
m_debugLoadAddr = SBig(m_debugLoadAddr);
|
||||
m_dolOff = SBig(m_dolOff);
|
||||
m_fstOff = SBig(m_fstOff);
|
||||
m_fstSz = SBig(m_fstSz);
|
||||
m_fstMaxSz = SBig(m_fstMaxSz);
|
||||
m_fstMemoryAddress = SBig(m_fstMemoryAddress);
|
||||
m_userPosition = SBig(m_userPosition);
|
||||
m_userSz = SBig(m_userSz);
|
||||
}
|
||||
|
||||
void write(IWriteStream& ws) const
|
||||
{
|
||||
Header hs(*this);
|
||||
hs.m_wiiMagic = SBig(hs.m_wiiMagic);
|
||||
hs.m_gcnMagic = SBig(hs.m_gcnMagic);
|
||||
hs.m_debugMonOff = SBig(hs.m_debugMonOff);
|
||||
hs.m_debugLoadAddr = SBig(hs.m_debugLoadAddr);
|
||||
hs.m_dolOff = SBig(hs.m_dolOff);
|
||||
hs.m_fstOff = SBig(hs.m_fstOff);
|
||||
hs.m_fstSz = SBig(hs.m_fstSz);
|
||||
hs.m_fstMaxSz = SBig(hs.m_fstMaxSz);
|
||||
hs.m_fstMemoryAddress = SBig(hs.m_fstMemoryAddress);
|
||||
hs.m_userPosition = SBig(hs.m_userPosition);
|
||||
hs.m_userSz = SBig(hs.m_userSz);
|
||||
ws.write(&hs, sizeof(hs));
|
||||
}
|
||||
void write(IWriteStream& ws) const {
|
||||
Header hs(*this);
|
||||
hs.m_wiiMagic = SBig(hs.m_wiiMagic);
|
||||
hs.m_gcnMagic = SBig(hs.m_gcnMagic);
|
||||
hs.m_debugMonOff = SBig(hs.m_debugMonOff);
|
||||
hs.m_debugLoadAddr = SBig(hs.m_debugLoadAddr);
|
||||
hs.m_dolOff = SBig(hs.m_dolOff);
|
||||
hs.m_fstOff = SBig(hs.m_fstOff);
|
||||
hs.m_fstSz = SBig(hs.m_fstSz);
|
||||
hs.m_fstMaxSz = SBig(hs.m_fstMaxSz);
|
||||
hs.m_fstMemoryAddress = SBig(hs.m_fstMemoryAddress);
|
||||
hs.m_userPosition = SBig(hs.m_userPosition);
|
||||
hs.m_userSz = SBig(hs.m_userSz);
|
||||
ws.write(&hs, sizeof(hs));
|
||||
}
|
||||
};
|
||||
|
||||
/* Currently only kept for dolphin compatibility */
|
||||
struct BI2Header
|
||||
{
|
||||
int32_t m_debugMonitorSize;
|
||||
int32_t m_simMemSize;
|
||||
uint32_t m_argOffset;
|
||||
uint32_t m_debugFlag;
|
||||
uint32_t m_trkAddress;
|
||||
uint32_t m_trkSz;
|
||||
uint32_t m_countryCode;
|
||||
uint32_t m_unk1;
|
||||
uint32_t m_unk2;
|
||||
uint32_t m_unk3;
|
||||
uint32_t m_dolLimit;
|
||||
uint32_t m_unk4;
|
||||
uint8_t padding2[0x1FD0];
|
||||
struct BI2Header {
|
||||
int32_t m_debugMonitorSize;
|
||||
int32_t m_simMemSize;
|
||||
uint32_t m_argOffset;
|
||||
uint32_t m_debugFlag;
|
||||
uint32_t m_trkAddress;
|
||||
uint32_t m_trkSz;
|
||||
uint32_t m_countryCode;
|
||||
uint32_t m_unk1;
|
||||
uint32_t m_unk2;
|
||||
uint32_t m_unk3;
|
||||
uint32_t m_dolLimit;
|
||||
uint32_t m_unk4;
|
||||
uint8_t padding2[0x1FD0];
|
||||
|
||||
void read(IReadStream& rs)
|
||||
{
|
||||
memset(this, 0, sizeof(*this));
|
||||
rs.read(this, sizeof(*this));
|
||||
m_debugMonitorSize = SBig(m_debugMonitorSize);
|
||||
m_simMemSize = SBig(m_simMemSize);
|
||||
m_argOffset = SBig(m_argOffset);
|
||||
m_debugFlag = SBig(m_debugFlag);
|
||||
m_trkAddress = SBig(m_trkAddress);
|
||||
m_trkSz = SBig(m_trkSz);
|
||||
m_countryCode = SBig(m_countryCode);
|
||||
m_unk1 = SBig(m_unk1);
|
||||
m_unk2 = SBig(m_unk2);
|
||||
m_unk3 = SBig(m_unk3);
|
||||
m_dolLimit = SBig(m_dolLimit);
|
||||
m_unk4 = SBig(m_unk4);
|
||||
}
|
||||
void read(IReadStream& rs) {
|
||||
memset(this, 0, sizeof(*this));
|
||||
rs.read(this, sizeof(*this));
|
||||
m_debugMonitorSize = SBig(m_debugMonitorSize);
|
||||
m_simMemSize = SBig(m_simMemSize);
|
||||
m_argOffset = SBig(m_argOffset);
|
||||
m_debugFlag = SBig(m_debugFlag);
|
||||
m_trkAddress = SBig(m_trkAddress);
|
||||
m_trkSz = SBig(m_trkSz);
|
||||
m_countryCode = SBig(m_countryCode);
|
||||
m_unk1 = SBig(m_unk1);
|
||||
m_unk2 = SBig(m_unk2);
|
||||
m_unk3 = SBig(m_unk3);
|
||||
m_dolLimit = SBig(m_dolLimit);
|
||||
m_unk4 = SBig(m_unk4);
|
||||
}
|
||||
|
||||
void write(IWriteStream& ws) const
|
||||
{
|
||||
BI2Header h = *this;
|
||||
h.m_debugMonitorSize = SBig(h.m_debugMonitorSize);
|
||||
h.m_simMemSize = SBig(h.m_simMemSize);
|
||||
h.m_argOffset = SBig(h.m_argOffset);
|
||||
h.m_debugFlag = SBig(h.m_debugFlag);
|
||||
h.m_trkAddress = SBig(h.m_trkAddress);
|
||||
h.m_trkSz = SBig(h.m_trkSz);
|
||||
h.m_countryCode = SBig(h.m_countryCode);
|
||||
h.m_unk1 = SBig(h.m_unk1);
|
||||
h.m_unk2 = SBig(h.m_unk2);
|
||||
h.m_unk3 = SBig(h.m_unk3);
|
||||
h.m_dolLimit = SBig(h.m_dolLimit);
|
||||
h.m_unk4 = SBig(h.m_unk4);
|
||||
ws.write(&h, sizeof(h));
|
||||
}
|
||||
void write(IWriteStream& ws) const {
|
||||
BI2Header h = *this;
|
||||
h.m_debugMonitorSize = SBig(h.m_debugMonitorSize);
|
||||
h.m_simMemSize = SBig(h.m_simMemSize);
|
||||
h.m_argOffset = SBig(h.m_argOffset);
|
||||
h.m_debugFlag = SBig(h.m_debugFlag);
|
||||
h.m_trkAddress = SBig(h.m_trkAddress);
|
||||
h.m_trkSz = SBig(h.m_trkSz);
|
||||
h.m_countryCode = SBig(h.m_countryCode);
|
||||
h.m_unk1 = SBig(h.m_unk1);
|
||||
h.m_unk2 = SBig(h.m_unk2);
|
||||
h.m_unk3 = SBig(h.m_unk3);
|
||||
h.m_dolLimit = SBig(h.m_dolLimit);
|
||||
h.m_unk4 = SBig(h.m_unk4);
|
||||
ws.write(&h, sizeof(h));
|
||||
}
|
||||
};
|
||||
|
||||
struct ExtractionContext;
|
||||
class DiscBase
|
||||
{
|
||||
class IPartition;
|
||||
class DiscBase;
|
||||
|
||||
class Node {
|
||||
public:
|
||||
virtual ~DiscBase() = default;
|
||||
enum class Kind { File, Directory };
|
||||
|
||||
class IPartition
|
||||
{
|
||||
public:
|
||||
virtual ~IPartition() = default;
|
||||
struct DOLHeader
|
||||
{
|
||||
uint32_t textOff[7];
|
||||
uint32_t dataOff[11];
|
||||
uint32_t textStarts[7];
|
||||
uint32_t dataStarts[11];
|
||||
uint32_t textSizes[7];
|
||||
uint32_t dataSizes[11];
|
||||
uint32_t bssStart;
|
||||
uint32_t bssSize;
|
||||
uint32_t entryPoint;
|
||||
};
|
||||
private:
|
||||
friend class IPartition;
|
||||
const IPartition& m_parent;
|
||||
Kind m_kind;
|
||||
|
||||
class Node
|
||||
{
|
||||
public:
|
||||
enum class Kind
|
||||
{
|
||||
File,
|
||||
Directory
|
||||
};
|
||||
private:
|
||||
friend class IPartition;
|
||||
const IPartition& m_parent;
|
||||
Kind m_kind;
|
||||
uint64_t m_discOffset;
|
||||
uint64_t m_discLength;
|
||||
std::string m_name;
|
||||
|
||||
uint64_t m_discOffset;
|
||||
uint64_t m_discLength;
|
||||
std::string m_name;
|
||||
std::vector<Node>::iterator m_childrenBegin;
|
||||
std::vector<Node>::iterator m_childrenEnd;
|
||||
|
||||
std::vector<Node>::iterator m_childrenBegin;
|
||||
std::vector<Node>::iterator m_childrenEnd;
|
||||
|
||||
public:
|
||||
Node(const IPartition& parent, const FSTNode& node, const char* name)
|
||||
: m_parent(parent),
|
||||
m_kind(node.isDir() ? Kind::Directory : Kind::File),
|
||||
m_discOffset(parent.normalizeOffset(node.getOffset())),
|
||||
m_discLength(node.getLength()),
|
||||
m_name(name) {}
|
||||
inline Kind getKind() const {return m_kind;}
|
||||
inline const std::string& getName() const {return m_name;}
|
||||
inline uint64_t size() const {return m_discLength;}
|
||||
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset=0) const
|
||||
{
|
||||
if (m_kind != Kind::File)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to stream a non-file %s", m_name.c_str());
|
||||
return std::unique_ptr<IPartReadStream>();
|
||||
}
|
||||
return m_parent.beginReadStream(m_discOffset + offset);
|
||||
}
|
||||
std::unique_ptr<uint8_t[]> getBuf() const
|
||||
{
|
||||
if (m_kind != Kind::File)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to buffer a non-file %s", m_name.c_str());
|
||||
return std::unique_ptr<uint8_t[]>();
|
||||
}
|
||||
uint8_t* buf = new uint8_t[m_discLength];
|
||||
beginReadStream()->read(buf, m_discLength);
|
||||
return std::unique_ptr<uint8_t[]>(buf);
|
||||
}
|
||||
inline std::vector<Node>::iterator rawBegin() const {return m_childrenBegin;}
|
||||
inline std::vector<Node>::iterator rawEnd() const {return m_childrenEnd;}
|
||||
|
||||
class DirectoryIterator : std::iterator<std::forward_iterator_tag, Node>
|
||||
{
|
||||
friend class Node;
|
||||
std::vector<Node>::iterator m_it;
|
||||
DirectoryIterator(const std::vector<Node>::iterator& it)
|
||||
: m_it(it) {}
|
||||
public:
|
||||
inline bool operator!=(const DirectoryIterator& other) {return m_it != other.m_it;}
|
||||
inline bool operator==(const DirectoryIterator& other) {return m_it == other.m_it;}
|
||||
inline DirectoryIterator& operator++()
|
||||
{
|
||||
if (m_it->m_kind == Kind::Directory)
|
||||
m_it = m_it->rawEnd();
|
||||
else
|
||||
++m_it;
|
||||
return *this;
|
||||
}
|
||||
inline Node& operator*() {return *m_it;}
|
||||
inline Node* operator->() {return &*m_it;}
|
||||
};
|
||||
inline DirectoryIterator begin() const {return DirectoryIterator(m_childrenBegin);}
|
||||
inline DirectoryIterator end() const {return DirectoryIterator(m_childrenEnd);}
|
||||
inline DirectoryIterator find(const std::string& name) const
|
||||
{
|
||||
if (m_kind == Kind::Directory)
|
||||
{
|
||||
DirectoryIterator it=begin();
|
||||
for (; it != end() ; ++it)
|
||||
{
|
||||
if (!it->getName().compare(name))
|
||||
return it;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
bool extractToDirectory(const SystemString& basePath, const ExtractionContext& ctx) const;
|
||||
};
|
||||
protected:
|
||||
Header m_header;
|
||||
BI2Header m_bi2Header;
|
||||
uint64_t m_dolOff;
|
||||
uint64_t m_fstOff;
|
||||
uint64_t m_fstSz;
|
||||
uint64_t m_apploaderSz;
|
||||
std::vector<Node> m_nodes;
|
||||
void parseFST(IPartReadStream& s);
|
||||
|
||||
std::vector<FSTNode> m_buildNodes;
|
||||
std::vector<std::string> m_buildNames;
|
||||
size_t m_buildNameOff = 0;
|
||||
void recursiveBuildNodes(const SystemChar* dirIn, std::function<void(void)> incParents);
|
||||
|
||||
uint64_t m_dolSz;
|
||||
void parseDOL(IPartReadStream& s);
|
||||
|
||||
const DiscBase& m_parent;
|
||||
PartitionKind m_kind;
|
||||
uint64_t m_offset;
|
||||
bool m_isWii;
|
||||
public:
|
||||
mutable size_t m_curNodeIdx = 0;
|
||||
float getProgressFactor() const { return getNodeCount() ? m_curNodeIdx / float(getNodeCount()) : 0.f; }
|
||||
float getProgressFactorMidFile(size_t curByte, size_t totalBytes) const
|
||||
{
|
||||
if (!getNodeCount())
|
||||
return 0.f;
|
||||
|
||||
if (totalBytes)
|
||||
return (m_curNodeIdx + (curByte / float(totalBytes))) / float(getNodeCount());
|
||||
else
|
||||
return m_curNodeIdx / float(getNodeCount());
|
||||
}
|
||||
|
||||
IPartition(const DiscBase& parent, PartitionKind kind, bool isWii, uint64_t offset)
|
||||
: m_parent(parent), m_kind(kind), m_offset(offset), m_isWii(isWii) {}
|
||||
virtual uint64_t normalizeOffset(uint64_t anOffset) const {return anOffset;}
|
||||
inline PartitionKind getKind() const {return m_kind;}
|
||||
inline bool isWii() const {return m_isWii;}
|
||||
inline uint64_t getDiscOffset() const {return m_offset;}
|
||||
virtual std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset=0) const=0;
|
||||
inline std::unique_ptr<IPartReadStream> beginDOLReadStream(uint64_t offset=0) const
|
||||
{return beginReadStream(m_dolOff + offset);}
|
||||
inline std::unique_ptr<IPartReadStream> beginFSTReadStream(uint64_t offset=0) const
|
||||
{return beginReadStream(m_fstOff + offset);}
|
||||
inline std::unique_ptr<IPartReadStream> beginApploaderReadStream(uint64_t offset=0) const
|
||||
{return beginReadStream(0x2440 + offset);}
|
||||
inline const Node& getFSTRoot() const {return m_nodes[0];}
|
||||
inline Node& getFSTRoot() {return m_nodes[0];}
|
||||
bool extractToDirectory(const SystemString& path, const ExtractionContext& ctx);
|
||||
|
||||
inline uint64_t getDOLSize() const {return m_dolSz;}
|
||||
inline std::unique_ptr<uint8_t[]> getDOLBuf() const
|
||||
{
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_dolSz]);
|
||||
beginDOLReadStream()->read(buf.get(), m_dolSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
inline uint64_t getFSTSize() const {return m_fstSz;}
|
||||
inline std::unique_ptr<uint8_t[]> getFSTBuf() const
|
||||
{
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_fstSz]);
|
||||
beginFSTReadStream()->read(buf.get(), m_fstSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
inline uint64_t getApploaderSize() const {return m_apploaderSz;}
|
||||
inline std::unique_ptr<uint8_t[]> getApploaderBuf() const
|
||||
{
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_apploaderSz]);
|
||||
beginApploaderReadStream()->read(buf.get(), m_apploaderSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
inline size_t getNodeCount() const { return m_nodes.size(); }
|
||||
inline const Header& getHeader() const { return m_header; }
|
||||
inline const BI2Header& getBI2() const { return m_bi2Header; }
|
||||
virtual bool extractCryptoFiles(const SystemString& path, const ExtractionContext& ctx) const { return true; }
|
||||
};
|
||||
|
||||
protected:
|
||||
std::unique_ptr<IDiscIO> m_discIO;
|
||||
Header m_header;
|
||||
std::vector<std::unique_ptr<IPartition>> m_partitions;
|
||||
public:
|
||||
DiscBase(std::unique_ptr<IDiscIO>&& dio, bool& err)
|
||||
: m_discIO(std::move(dio)), m_header(*m_discIO, err) {}
|
||||
Node(const IPartition& parent, const FSTNode& node, std::string_view name);
|
||||
Kind getKind() const { return m_kind; }
|
||||
std::string_view getName() const { return m_name; }
|
||||
uint64_t size() const { return m_discLength; }
|
||||
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset = 0) const;
|
||||
std::unique_ptr<uint8_t[]> getBuf() const;
|
||||
std::vector<Node>::iterator rawBegin() const { return m_childrenBegin; }
|
||||
std::vector<Node>::iterator rawEnd() const { return m_childrenEnd; }
|
||||
|
||||
inline const Header& getHeader() const {return m_header;}
|
||||
inline const IDiscIO& getDiscIO() const {return *m_discIO;}
|
||||
inline size_t getPartitonNodeCount(size_t partition = 0) const
|
||||
{
|
||||
if (partition > m_partitions.size())
|
||||
return -1;
|
||||
return m_partitions[partition]->getNodeCount();
|
||||
class DirectoryIterator {
|
||||
friend class Node;
|
||||
std::vector<Node>::iterator m_it;
|
||||
DirectoryIterator(const std::vector<Node>::iterator& it) : m_it(it) {}
|
||||
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = Node;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = Node*;
|
||||
using reference = Node&;
|
||||
|
||||
bool operator==(const DirectoryIterator& other) const { return m_it == other.m_it; }
|
||||
bool operator!=(const DirectoryIterator& other) const { return !operator==(other); }
|
||||
DirectoryIterator& operator++() {
|
||||
if (m_it->m_kind == Kind::Directory)
|
||||
m_it = m_it->rawEnd();
|
||||
else
|
||||
++m_it;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline IPartition* getDataPartition()
|
||||
{
|
||||
for (const std::unique_ptr<IPartition>& part : m_partitions)
|
||||
if (part->getKind() == PartitionKind::Data)
|
||||
return part.get();
|
||||
return nullptr;
|
||||
Node& operator*() { return *m_it; }
|
||||
const Node& operator*() const { return *m_it; }
|
||||
Node* operator->() { return &*m_it; }
|
||||
const Node* operator->() const { return &*m_it; }
|
||||
};
|
||||
DirectoryIterator begin() const { return DirectoryIterator(m_childrenBegin); }
|
||||
DirectoryIterator end() const { return DirectoryIterator(m_childrenEnd); }
|
||||
DirectoryIterator find(std::string_view name) const {
|
||||
if (m_kind == Kind::Directory) {
|
||||
DirectoryIterator it = begin();
|
||||
for (; it != end(); ++it) {
|
||||
if (it->getName() == name)
|
||||
return it;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
inline IPartition* getUpdatePartition()
|
||||
{
|
||||
for (const std::unique_ptr<IPartition>& part : m_partitions)
|
||||
if (part->getKind() == PartitionKind::Update)
|
||||
return part.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline void extractToDirectory(const SystemString& path, const ExtractionContext& ctx)
|
||||
{
|
||||
for (std::unique_ptr<IPartition>& part : m_partitions)
|
||||
part->extractToDirectory(path, ctx);
|
||||
}
|
||||
|
||||
virtual bool extractDiscHeaderFiles(const SystemString& path, const ExtractionContext& ctx) const=0;
|
||||
bool extractToDirectory(SystemStringView basePath, const ExtractionContext& ctx) const;
|
||||
};
|
||||
|
||||
class DiscBuilderBase
|
||||
{
|
||||
friend class DiscMergerWii;
|
||||
class IPartition {
|
||||
public:
|
||||
class PartitionBuilderBase
|
||||
{
|
||||
public:
|
||||
virtual ~PartitionBuilderBase() = default;
|
||||
protected:
|
||||
std::unordered_map<SystemString, std::pair<uint64_t,uint64_t>> m_fileOffsetsSizes;
|
||||
std::vector<FSTNode> m_buildNodes;
|
||||
std::vector<std::string> m_buildNames;
|
||||
size_t m_buildNameOff = 0;
|
||||
virtual uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws)=0;
|
||||
virtual uint32_t packOffset(uint64_t offset) const=0;
|
||||
virtual ~IPartition() = default;
|
||||
struct DOLHeader {
|
||||
uint32_t textOff[7];
|
||||
uint32_t dataOff[11];
|
||||
uint32_t textStarts[7];
|
||||
uint32_t dataStarts[11];
|
||||
uint32_t textSizes[7];
|
||||
uint32_t dataSizes[11];
|
||||
uint32_t bssStart;
|
||||
uint32_t bssSize;
|
||||
uint32_t entryPoint;
|
||||
};
|
||||
|
||||
void recursiveBuildNodesPre(const SystemChar* dirIn);
|
||||
bool recursiveBuildNodes(IPartWriteStream& ws, bool system, const SystemChar* dirIn);
|
||||
|
||||
bool recursiveBuildFST(const SystemChar* dirIn,
|
||||
std::function<void(void)> incParents);
|
||||
|
||||
void recursiveMergeNodesPre(const DiscBase::IPartition::Node* nodeIn, const SystemChar* dirIn);
|
||||
bool recursiveMergeNodes(IPartWriteStream& ws, bool system,
|
||||
const DiscBase::IPartition::Node* nodeIn, const SystemChar* dirIn,
|
||||
const SystemString& keyPath);
|
||||
bool recursiveMergeFST(const DiscBase::IPartition::Node* nodeIn,
|
||||
const SystemChar* dirIn, std::function<void(void)> incParents,
|
||||
const SystemString& keyPath);
|
||||
|
||||
static bool RecursiveCalculateTotalSize(uint64_t& totalSz,
|
||||
const DiscBase::IPartition::Node* nodeIn,
|
||||
const SystemChar* dirIn);
|
||||
|
||||
void addBuildName(const SystemString& str)
|
||||
{
|
||||
SystemUTF8View utf8View(str);
|
||||
m_buildNames.push_back(utf8View.utf8_str());
|
||||
m_buildNameOff += str.size() + 1;
|
||||
}
|
||||
|
||||
DiscBuilderBase& m_parent;
|
||||
PartitionKind m_kind;
|
||||
uint64_t m_dolOffset = 0;
|
||||
uint64_t m_dolSize = 0;
|
||||
bool m_isWii;
|
||||
public:
|
||||
PartitionBuilderBase(DiscBuilderBase& parent, PartitionKind kind, bool isWii)
|
||||
: m_parent(parent), m_kind(kind), m_isWii(isWii)
|
||||
{}
|
||||
virtual std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset)=0;
|
||||
bool buildFromDirectory(IPartWriteStream& ws,
|
||||
const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeBuild(const SystemChar* dirIn,
|
||||
PartitionKind kind, bool isWii);
|
||||
bool mergeFromDirectory(IPartWriteStream& ws,
|
||||
const DiscBase::IPartition* partIn,
|
||||
const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeMerge(const DiscBase::IPartition* partIn,
|
||||
const SystemChar* dirIn);
|
||||
};
|
||||
protected:
|
||||
SystemString m_outPath;
|
||||
std::unique_ptr<IFileIO> m_fileIO;
|
||||
std::vector<std::unique_ptr<PartitionBuilderBase>> m_partitions;
|
||||
int64_t m_discCapacity;
|
||||
Header m_header;
|
||||
BI2Header m_bi2Header;
|
||||
uint64_t m_dolOff;
|
||||
uint64_t m_fstOff;
|
||||
uint64_t m_fstSz;
|
||||
uint64_t m_apploaderSz;
|
||||
std::vector<Node> m_nodes;
|
||||
void parseFST(IPartReadStream& s);
|
||||
|
||||
std::vector<FSTNode> m_buildNodes;
|
||||
std::vector<std::string> m_buildNames;
|
||||
size_t m_buildNameOff = 0;
|
||||
|
||||
uint64_t m_dolSz;
|
||||
void parseDOL(IPartReadStream& s);
|
||||
|
||||
const DiscBase& m_parent;
|
||||
PartitionKind m_kind;
|
||||
uint64_t m_offset;
|
||||
bool m_isWii;
|
||||
|
||||
public:
|
||||
FProgress m_progressCB;
|
||||
size_t m_progressIdx = 0;
|
||||
size_t m_progressTotal = 0;
|
||||
float getProgressFactor() const
|
||||
{
|
||||
return m_progressTotal ? std::min(1.f, m_progressIdx / float(m_progressTotal)) : 0.f;
|
||||
}
|
||||
float getProgressFactorMidFile(size_t curByte, size_t totalBytes) const
|
||||
{
|
||||
if (!m_progressTotal)
|
||||
return 0.f;
|
||||
mutable size_t m_curNodeIdx = 0;
|
||||
float getProgressFactor() const { return getNodeCount() ? m_curNodeIdx / float(getNodeCount()) : 0.f; }
|
||||
float getProgressFactorMidFile(size_t curByte, size_t totalBytes) const {
|
||||
if (!getNodeCount())
|
||||
return 0.f;
|
||||
|
||||
if (totalBytes)
|
||||
return (m_progressIdx + (curByte / float(totalBytes))) / float(m_progressTotal);
|
||||
else
|
||||
return m_progressIdx / float(m_progressTotal);
|
||||
}
|
||||
if (totalBytes)
|
||||
return (m_curNodeIdx + (curByte / float(totalBytes))) / float(getNodeCount());
|
||||
else
|
||||
return m_curNodeIdx / float(getNodeCount());
|
||||
}
|
||||
|
||||
virtual ~DiscBuilderBase() = default;
|
||||
DiscBuilderBase(const SystemChar* outPath,
|
||||
int64_t discCapacity, FProgress progressCB)
|
||||
: m_outPath(outPath), m_fileIO(NewFileIO(outPath, discCapacity)),
|
||||
m_discCapacity(discCapacity), m_progressCB(progressCB) {}
|
||||
DiscBuilderBase(DiscBuilderBase&&) = default;
|
||||
DiscBuilderBase& operator=(DiscBuilderBase&&) = default;
|
||||
IPartition(const DiscBase& parent, PartitionKind kind, bool isWii, uint64_t offset)
|
||||
: m_parent(parent), m_kind(kind), m_offset(offset), m_isWii(isWii) {}
|
||||
virtual uint64_t normalizeOffset(uint64_t anOffset) const { return anOffset; }
|
||||
PartitionKind getKind() const { return m_kind; }
|
||||
bool isWii() const { return m_isWii; }
|
||||
uint64_t getDiscOffset() const { return m_offset; }
|
||||
virtual std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset = 0) const = 0;
|
||||
std::unique_ptr<IPartReadStream> beginDOLReadStream(uint64_t offset = 0) const {
|
||||
return beginReadStream(m_dolOff + offset);
|
||||
}
|
||||
std::unique_ptr<IPartReadStream> beginFSTReadStream(uint64_t offset = 0) const {
|
||||
return beginReadStream(m_fstOff + offset);
|
||||
}
|
||||
std::unique_ptr<IPartReadStream> beginApploaderReadStream(uint64_t offset = 0) const {
|
||||
return beginReadStream(0x2440 + offset);
|
||||
}
|
||||
const Node& getFSTRoot() const { return m_nodes[0]; }
|
||||
Node& getFSTRoot() { return m_nodes[0]; }
|
||||
bool extractToDirectory(SystemStringView path, const ExtractionContext& ctx);
|
||||
|
||||
IFileIO& getFileIO() { return *m_fileIO; }
|
||||
uint64_t getDOLSize() const { return m_dolSz; }
|
||||
std::unique_ptr<uint8_t[]> getDOLBuf() const {
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_dolSz]);
|
||||
beginDOLReadStream()->read(buf.get(), m_dolSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
uint64_t getFSTSize() const { return m_fstSz; }
|
||||
std::unique_ptr<uint8_t[]> getFSTBuf() const {
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_fstSz]);
|
||||
beginFSTReadStream()->read(buf.get(), m_fstSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
uint64_t getApploaderSize() const { return m_apploaderSz; }
|
||||
std::unique_ptr<uint8_t[]> getApploaderBuf() const {
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[m_apploaderSz]);
|
||||
beginApploaderReadStream()->read(buf.get(), m_apploaderSz);
|
||||
return buf;
|
||||
}
|
||||
|
||||
size_t getNodeCount() const { return m_nodes.size(); }
|
||||
const Header& getHeader() const { return m_header; }
|
||||
const BI2Header& getBI2() const { return m_bi2Header; }
|
||||
virtual bool extractCryptoFiles(SystemStringView path, const ExtractionContext& ctx) const { return true; }
|
||||
bool extractSysFiles(SystemStringView path, const ExtractionContext& ctx) const;
|
||||
};
|
||||
|
||||
using Partition = DiscBase::IPartition;
|
||||
using Node = Partition::Node;
|
||||
class DiscBase {
|
||||
public:
|
||||
virtual ~DiscBase() = default;
|
||||
|
||||
}
|
||||
protected:
|
||||
std::unique_ptr<IDiscIO> m_discIO;
|
||||
Header m_header;
|
||||
std::vector<std::unique_ptr<IPartition>> m_partitions;
|
||||
|
||||
#endif // __NOD_DISC_BASE__
|
||||
public:
|
||||
DiscBase(std::unique_ptr<IDiscIO>&& dio, bool& err) : m_discIO(std::move(dio)), m_header(*m_discIO, err) {}
|
||||
|
||||
const Header& getHeader() const { return m_header; }
|
||||
const IDiscIO& getDiscIO() const { return *m_discIO; }
|
||||
size_t getPartitionNodeCount(size_t partition = 0) const {
|
||||
if (partition >= m_partitions.size()) {
|
||||
return -1;
|
||||
}
|
||||
return m_partitions[partition]->getNodeCount();
|
||||
}
|
||||
|
||||
IPartition* getDataPartition() {
|
||||
for (const std::unique_ptr<IPartition>& part : m_partitions)
|
||||
if (part->getKind() == PartitionKind::Data)
|
||||
return part.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IPartition* getUpdatePartition() {
|
||||
for (const std::unique_ptr<IPartition>& part : m_partitions)
|
||||
if (part->getKind() == PartitionKind::Update)
|
||||
return part.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void extractToDirectory(SystemStringView path, const ExtractionContext& ctx) {
|
||||
for (std::unique_ptr<IPartition>& part : m_partitions)
|
||||
part->extractToDirectory(path, ctx);
|
||||
}
|
||||
|
||||
virtual bool extractDiscHeaderFiles(SystemStringView path, const ExtractionContext& ctx) const = 0;
|
||||
};
|
||||
|
||||
class DiscBuilderBase {
|
||||
friend class DiscMergerWii;
|
||||
|
||||
public:
|
||||
class PartitionBuilderBase {
|
||||
public:
|
||||
virtual ~PartitionBuilderBase() = default;
|
||||
|
||||
protected:
|
||||
std::unordered_map<SystemString, std::pair<uint64_t, uint64_t>> m_fileOffsetsSizes;
|
||||
std::vector<FSTNode> m_buildNodes;
|
||||
std::vector<std::string> m_buildNames;
|
||||
size_t m_buildNameOff = 0;
|
||||
virtual uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws) = 0;
|
||||
virtual uint32_t packOffset(uint64_t offset) const = 0;
|
||||
|
||||
void recursiveBuildNodesPre(SystemStringView dirIn);
|
||||
bool recursiveBuildNodes(IPartWriteStream& ws, bool system, SystemStringView dirIn);
|
||||
|
||||
bool recursiveBuildFST(SystemStringView dirIn, std::function<void(void)> incParents, size_t parentDirIdx);
|
||||
|
||||
void recursiveMergeNodesPre(const Node* nodeIn, SystemStringView dirIn);
|
||||
bool recursiveMergeNodes(IPartWriteStream& ws, bool system, const Node* nodeIn, SystemStringView dirIn,
|
||||
SystemStringView keyPath);
|
||||
bool recursiveMergeFST(const Node* nodeIn, SystemStringView dirIn, std::function<void(void)> incParents,
|
||||
SystemStringView keyPath);
|
||||
|
||||
static bool RecursiveCalculateTotalSize(uint64_t& totalSz, const Node* nodeIn, SystemStringView dirIn);
|
||||
|
||||
void addBuildName(SystemStringView str) {
|
||||
SystemUTF8Conv utf8View(str);
|
||||
m_buildNames.emplace_back(utf8View.utf8_str());
|
||||
m_buildNameOff += str.size() + 1;
|
||||
}
|
||||
|
||||
DiscBuilderBase& m_parent;
|
||||
PartitionKind m_kind;
|
||||
uint64_t m_dolOffset = 0;
|
||||
uint64_t m_dolSize = 0;
|
||||
bool m_isWii;
|
||||
|
||||
public:
|
||||
PartitionBuilderBase(DiscBuilderBase& parent, PartitionKind kind, bool isWii)
|
||||
: m_parent(parent), m_kind(kind), m_isWii(isWii) {}
|
||||
virtual std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) = 0;
|
||||
bool buildFromDirectory(IPartWriteStream& ws, SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeBuild(SystemStringView dirIn, PartitionKind kind, bool isWii);
|
||||
bool mergeFromDirectory(IPartWriteStream& ws, const IPartition* partIn, SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeMerge(const IPartition* partIn, SystemStringView dirIn);
|
||||
};
|
||||
|
||||
protected:
|
||||
SystemString m_outPath;
|
||||
std::unique_ptr<IFileIO> m_fileIO;
|
||||
std::vector<std::unique_ptr<PartitionBuilderBase>> m_partitions;
|
||||
int64_t m_discCapacity;
|
||||
|
||||
public:
|
||||
FProgress m_progressCB;
|
||||
size_t m_progressIdx = 0;
|
||||
size_t m_progressTotal = 0;
|
||||
float getProgressFactor() const {
|
||||
return m_progressTotal ? std::min(1.f, m_progressIdx / float(m_progressTotal)) : 0.f;
|
||||
}
|
||||
float getProgressFactorMidFile(size_t curByte, size_t totalBytes) const {
|
||||
if (!m_progressTotal)
|
||||
return 0.f;
|
||||
|
||||
if (totalBytes)
|
||||
return (m_progressIdx + (curByte / float(totalBytes))) / float(m_progressTotal);
|
||||
else
|
||||
return m_progressIdx / float(m_progressTotal);
|
||||
}
|
||||
|
||||
virtual ~DiscBuilderBase() = default;
|
||||
DiscBuilderBase(SystemStringView outPath, int64_t discCapacity, FProgress progressCB)
|
||||
: m_outPath(outPath)
|
||||
, m_fileIO(NewFileIO(outPath, discCapacity))
|
||||
, m_discCapacity(discCapacity)
|
||||
, m_progressCB(std::move(progressCB)) {}
|
||||
DiscBuilderBase(DiscBuilderBase&&) = default;
|
||||
DiscBuilderBase& operator=(DiscBuilderBase&&) = default;
|
||||
|
||||
IFileIO& getFileIO() { return *m_fileIO; }
|
||||
};
|
||||
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,41 +1,36 @@
|
||||
#ifndef __NOD_DISC_GCN__
|
||||
#define __NOD_DISC_GCN__
|
||||
#pragma once
|
||||
|
||||
#include "DiscBase.hpp"
|
||||
#include "nod/DiscBase.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
class DiscBuilderGCN;
|
||||
|
||||
class DiscGCN : public DiscBase
|
||||
{
|
||||
friend class DiscMergerGCN;
|
||||
DiscBuilderGCN makeMergeBuilder(const SystemChar* outPath, FProgress progressCB);
|
||||
class DiscGCN : public DiscBase {
|
||||
friend class DiscMergerGCN;
|
||||
DiscBuilderGCN makeMergeBuilder(SystemStringView outPath, FProgress progressCB);
|
||||
|
||||
public:
|
||||
DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err);
|
||||
bool extractDiscHeaderFiles(const SystemString& path, const ExtractionContext& ctx) const;
|
||||
DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err);
|
||||
bool extractDiscHeaderFiles(SystemStringView path, const ExtractionContext& ctx) const override;
|
||||
};
|
||||
|
||||
class DiscBuilderGCN : public DiscBuilderBase
|
||||
{
|
||||
friend class DiscMergerGCN;
|
||||
class DiscBuilderGCN : public DiscBuilderBase {
|
||||
friend class DiscMergerGCN;
|
||||
|
||||
public:
|
||||
DiscBuilderGCN(const SystemChar* outPath, FProgress progressCB);
|
||||
EBuildResult buildFromDirectory(const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeRequired(const SystemChar* dirIn);
|
||||
DiscBuilderGCN(SystemStringView outPath, FProgress progressCB);
|
||||
EBuildResult buildFromDirectory(SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeRequired(SystemStringView dirIn);
|
||||
};
|
||||
|
||||
class DiscMergerGCN
|
||||
{
|
||||
DiscGCN& m_sourceDisc;
|
||||
DiscBuilderGCN m_builder;
|
||||
class DiscMergerGCN {
|
||||
DiscGCN& m_sourceDisc;
|
||||
DiscBuilderGCN m_builder;
|
||||
|
||||
public:
|
||||
DiscMergerGCN(const SystemChar* outPath, DiscGCN& sourceDisc, FProgress progressCB);
|
||||
EBuildResult mergeFromDirectory(const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeRequired(DiscGCN& sourceDisc, const SystemChar* dirIn);
|
||||
DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB);
|
||||
EBuildResult mergeFromDirectory(SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // __NOD_DISC_GCN__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,41 +1,32 @@
|
||||
#ifndef __NOD_DISC_WII__
|
||||
#define __NOD_DISC_WII__
|
||||
#pragma once
|
||||
|
||||
#include "DiscBase.hpp"
|
||||
#include "nod/DiscBase.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
class DiscBuilderWii;
|
||||
|
||||
class DiscWii : public DiscBase
|
||||
{
|
||||
class DiscWii : public DiscBase {
|
||||
public:
|
||||
DiscWii(std::unique_ptr<IDiscIO>&& dio, bool& err);
|
||||
DiscBuilderWii makeMergeBuilder(const SystemChar* outPath, bool dualLayer, FProgress progressCB);
|
||||
bool extractDiscHeaderFiles(const SystemString& path, const ExtractionContext& ctx) const;
|
||||
DiscWii(std::unique_ptr<IDiscIO>&& dio, bool& err);
|
||||
DiscBuilderWii makeMergeBuilder(SystemStringView outPath, bool dualLayer, FProgress progressCB);
|
||||
bool extractDiscHeaderFiles(SystemStringView path, const ExtractionContext& ctx) const override;
|
||||
};
|
||||
|
||||
class DiscBuilderWii : public DiscBuilderBase
|
||||
{
|
||||
bool m_dualLayer;
|
||||
class DiscBuilderWii : public DiscBuilderBase {
|
||||
public:
|
||||
DiscBuilderWii(const SystemChar* outPath, bool dualLayer, FProgress progressCB);
|
||||
EBuildResult buildFromDirectory(const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeRequired(const SystemChar* dirIn, bool& dualLayer);
|
||||
DiscBuilderWii(SystemStringView outPath, bool dualLayer, FProgress progressCB);
|
||||
EBuildResult buildFromDirectory(SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeRequired(SystemStringView dirIn, bool& dualLayer);
|
||||
};
|
||||
|
||||
class DiscMergerWii
|
||||
{
|
||||
DiscWii& m_sourceDisc;
|
||||
DiscBuilderWii m_builder;
|
||||
class DiscMergerWii {
|
||||
DiscWii& m_sourceDisc;
|
||||
DiscBuilderWii m_builder;
|
||||
|
||||
public:
|
||||
DiscMergerWii(const SystemChar* outPath, DiscWii& sourceDisc,
|
||||
bool dualLayer, FProgress progressCB);
|
||||
EBuildResult mergeFromDirectory(const SystemChar* dirIn);
|
||||
static uint64_t CalculateTotalSizeRequired(DiscWii& sourceDisc, const SystemChar* dirIn,
|
||||
bool& dualLayer);
|
||||
DiscMergerWii(SystemStringView outPath, DiscWii& sourceDisc, bool dualLayer, FProgress progressCB);
|
||||
EBuildResult mergeFromDirectory(SystemStringView dirIn);
|
||||
static std::optional<uint64_t> CalculateTotalSizeRequired(DiscWii& sourceDisc, SystemStringView dirIn, bool& dualLayer);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __NOD_DISC_WII__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,75 +1,69 @@
|
||||
#ifndef __NOD_IDISC_IO__
|
||||
#define __NOD_IDISC_IO__
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if NOD_ATHENA
|
||||
#include <athena/IStreamReader.hpp>
|
||||
#include <athena/IStreamWriter.hpp>
|
||||
#endif
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
|
||||
struct IReadStream
|
||||
{
|
||||
virtual ~IReadStream() = default;
|
||||
virtual uint64_t read(void* buf, uint64_t length)=0;
|
||||
virtual void seek(int64_t offset, int whence=SEEK_SET)=0;
|
||||
virtual uint64_t position() const=0;
|
||||
struct IReadStream {
|
||||
virtual ~IReadStream() = default;
|
||||
virtual uint64_t read(void* buf, uint64_t length) = 0;
|
||||
virtual void seek(int64_t offset, int whence = SEEK_SET) = 0;
|
||||
virtual uint64_t position() const = 0;
|
||||
};
|
||||
|
||||
struct IWriteStream
|
||||
{
|
||||
virtual ~IWriteStream() = default;
|
||||
virtual uint64_t write(const void* buf, uint64_t length)=0;
|
||||
struct IWriteStream {
|
||||
virtual ~IWriteStream() = default;
|
||||
virtual uint64_t write(const void* buf, uint64_t length) = 0;
|
||||
};
|
||||
|
||||
class IDiscIO
|
||||
{
|
||||
class IDiscIO {
|
||||
public:
|
||||
virtual ~IDiscIO() = default;
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream(uint64_t offset=0) const=0;
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset=0) const=0;
|
||||
virtual ~IDiscIO() = default;
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream(uint64_t offset = 0) const = 0;
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset = 0) const = 0;
|
||||
virtual bool hasWiiCrypto() const { return true; } /* NFS overrides this to false */
|
||||
};
|
||||
|
||||
struct IPartReadStream : IReadStream
|
||||
{
|
||||
virtual ~IPartReadStream() = default;
|
||||
struct IPartReadStream : IReadStream {
|
||||
~IPartReadStream() override = default;
|
||||
};
|
||||
|
||||
struct IPartWriteStream : IWriteStream
|
||||
{
|
||||
virtual ~IPartWriteStream() = default;
|
||||
virtual void close()=0;
|
||||
virtual uint64_t position() const=0;
|
||||
struct IPartWriteStream : IWriteStream {
|
||||
~IPartWriteStream() override = default;
|
||||
virtual void close() = 0;
|
||||
virtual uint64_t position() const = 0;
|
||||
};
|
||||
|
||||
#if NOD_ATHENA
|
||||
|
||||
class AthenaPartReadStream : public athena::io::IStreamReader
|
||||
{
|
||||
std::unique_ptr<IPartReadStream> m_rs;
|
||||
public:
|
||||
AthenaPartReadStream(std::unique_ptr<IPartReadStream>&& rs) : m_rs(std::move(rs)) {}
|
||||
class AthenaPartReadStream : public athena::io::IStreamReader {
|
||||
std::unique_ptr<IPartReadStream> m_rs;
|
||||
|
||||
inline void seek(atInt64 off, athena::SeekOrigin origin)
|
||||
{
|
||||
if (origin == athena::Begin)
|
||||
m_rs->seek(off, SEEK_SET);
|
||||
else if (origin == athena::Current)
|
||||
m_rs->seek(off, SEEK_CUR);
|
||||
public:
|
||||
AthenaPartReadStream(std::unique_ptr<IPartReadStream>&& rs) : m_rs(std::move(rs)) {}
|
||||
|
||||
void seek(atInt64 off, athena::SeekOrigin origin) override {
|
||||
if (origin == athena::SeekOrigin::Begin) {
|
||||
m_rs->seek(off, SEEK_SET);
|
||||
} else if (origin == athena::SeekOrigin::Current) {
|
||||
m_rs->seek(off, SEEK_CUR);
|
||||
}
|
||||
inline atUint64 position() const {return m_rs->position();}
|
||||
inline atUint64 length() const {return 0;}
|
||||
inline atUint64 readUBytesToBuf(void* buf, atUint64 sz) {m_rs->read(buf, sz); return sz;}
|
||||
}
|
||||
atUint64 position() const override { return m_rs->position(); }
|
||||
atUint64 length() const override { return 0; }
|
||||
atUint64 readUBytesToBuf(void* buf, atUint64 sz) override {
|
||||
m_rs->read(buf, sz);
|
||||
return sz;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif // __NOD_IDISC_IO__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,87 +1,74 @@
|
||||
#ifndef __NOD_IFILE_IO__
|
||||
#define __NOD_IFILE_IO__
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <stdlib.h>
|
||||
#include "IDiscIO.hpp"
|
||||
#include "Util.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include "nod/IDiscIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
class IFileIO
|
||||
{
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
namespace nod {
|
||||
|
||||
class IFileIO {
|
||||
public:
|
||||
virtual ~IFileIO() = default;
|
||||
virtual bool exists()=0;
|
||||
virtual uint64_t size()=0;
|
||||
virtual ~IFileIO() = default;
|
||||
virtual bool exists() = 0;
|
||||
virtual uint64_t size() = 0;
|
||||
|
||||
struct IWriteStream : nod::IWriteStream
|
||||
{
|
||||
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length)
|
||||
{
|
||||
uint64_t read = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length)
|
||||
{
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
uint64_t readSz = discio.read(buf, thisSz);
|
||||
if (thisSz != readSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to read enough from disc");
|
||||
return read;
|
||||
}
|
||||
if (write(buf, readSz) != readSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to write in file");
|
||||
return read;
|
||||
}
|
||||
length -= thisSz;
|
||||
read += thisSz;
|
||||
}
|
||||
return read;
|
||||
struct IWriteStream : nod::IWriteStream {
|
||||
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length) {
|
||||
uint64_t read = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length) {
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
uint64_t readSz = discio.read(buf, thisSz);
|
||||
if (thisSz != readSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from disc"));
|
||||
return read;
|
||||
}
|
||||
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length, const std::function<void(float)>& prog)
|
||||
{
|
||||
uint64_t read = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
uint64_t total = length;
|
||||
while (length)
|
||||
{
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
uint64_t readSz = discio.read(buf, thisSz);
|
||||
if (thisSz != readSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to read enough from disc");
|
||||
return read;
|
||||
}
|
||||
if (write(buf, readSz) != readSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to write in file");
|
||||
return read;
|
||||
}
|
||||
length -= thisSz;
|
||||
read += thisSz;
|
||||
prog(read / float(total));
|
||||
}
|
||||
return read;
|
||||
if (write(buf, readSz) != readSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to write in file"));
|
||||
return read;
|
||||
}
|
||||
};
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream() const=0;
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const=0;
|
||||
length -= thisSz;
|
||||
read += thisSz;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length, const std::function<void(float)>& prog) {
|
||||
uint64_t read = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
uint64_t total = length;
|
||||
while (length) {
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
uint64_t readSz = discio.read(buf, thisSz);
|
||||
if (thisSz != readSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from disc"));
|
||||
return read;
|
||||
}
|
||||
if (write(buf, readSz) != readSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to write in file"));
|
||||
return read;
|
||||
}
|
||||
length -= thisSz;
|
||||
read += thisSz;
|
||||
prog(read / float(total));
|
||||
}
|
||||
return read;
|
||||
}
|
||||
};
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream() const = 0;
|
||||
virtual std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const = 0;
|
||||
|
||||
struct IReadStream : nod::IReadStream
|
||||
{
|
||||
virtual uint64_t copyToDisc(struct IPartWriteStream& discio, uint64_t length)=0;
|
||||
};
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream() const=0;
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const=0;
|
||||
struct IReadStream : nod::IReadStream {
|
||||
virtual uint64_t copyToDisc(struct IPartWriteStream& discio, uint64_t length) = 0;
|
||||
};
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream() const = 0;
|
||||
virtual std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemString& path, int64_t maxWriteSize=-1);
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemChar* path, int64_t maxWriteSize=-1);
|
||||
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize = -1);
|
||||
|
||||
}
|
||||
|
||||
#endif // __NOD_IFILE_IO__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#ifndef __NOD_UTIL_HPP__
|
||||
#define __NOD_UTIL_HPP__
|
||||
#pragma once
|
||||
|
||||
#if _WIN32 && UNICODE
|
||||
#include <wctype.h>
|
||||
#include <cwctype>
|
||||
#include <direct.h>
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
@@ -11,32 +10,37 @@
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#if defined(WINAPI_FAMILY) && WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP
|
||||
#define WINDOWS_STORE 1
|
||||
#else
|
||||
#include <ctype.h>
|
||||
#define WINDOWS_STORE 0
|
||||
#endif
|
||||
#else
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <sys/file.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/statvfs.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include "logvisor/logvisor.hpp"
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <logvisor/logvisor.hpp>
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4996)
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
|
||||
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
|
||||
#endif
|
||||
|
||||
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
|
||||
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
|
||||
#endif
|
||||
|
||||
#if !defined(S_ISLNK)
|
||||
@@ -47,13 +51,23 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
/* define our own min/max to avoid MSVC BS */
|
||||
template<typename T>
|
||||
inline T min(T a, T b) { return a < b ? a : b; }
|
||||
template<typename T>
|
||||
inline T max(T a, T b) { return a > b ? a : b; }
|
||||
template <typename T>
|
||||
constexpr T min(T a, T b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
template <typename T>
|
||||
constexpr T max(T a, T b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
/* template-based div for flexible typing and avoiding a library call */
|
||||
template <typename T>
|
||||
constexpr auto div(T a, T b) {
|
||||
struct DivTp { T quot, rem; };
|
||||
return DivTp{a / b, a % b};
|
||||
}
|
||||
|
||||
/* Log Module */
|
||||
extern logvisor::Module LogModule;
|
||||
@@ -62,103 +76,98 @@ extern logvisor::Module LogModule;
|
||||
#if _WIN32 && UNICODE
|
||||
#define NOD_UCS2 1
|
||||
typedef struct _stat Sstat;
|
||||
static inline int Mkdir(const wchar_t* path, int) {return _wmkdir(path);}
|
||||
static inline int Stat(const wchar_t* path, Sstat* statout) {return _wstat(path, statout);}
|
||||
static inline int Mkdir(const wchar_t* path, int) { return _wmkdir(path); }
|
||||
static inline int Stat(const wchar_t* path, Sstat* statout) { return _wstat(path, statout); }
|
||||
#else
|
||||
typedef struct stat Sstat;
|
||||
static inline int Mkdir(const char* path, mode_t mode) {return mkdir(path, mode);}
|
||||
static inline int Stat(const char* path, Sstat* statout) {return stat(path, statout);}
|
||||
static inline int Mkdir(const char* path, mode_t mode) { return mkdir(path, mode); }
|
||||
static inline int Stat(const char* path, Sstat* statout) { return stat(path, statout); }
|
||||
#endif
|
||||
|
||||
/* String-converting views */
|
||||
#if NOD_UCS2
|
||||
typedef wchar_t SystemChar;
|
||||
typedef std::wstring SystemString;
|
||||
static inline void ToLower(SystemString& str)
|
||||
{std::transform(str.begin(), str.end(), str.begin(), towlower);}
|
||||
static inline void ToUpper(SystemString& str)
|
||||
{std::transform(str.begin(), str.end(), str.begin(), towupper);}
|
||||
static inline size_t StrLen(const SystemChar* str) {return wcslen(str);}
|
||||
class SystemUTF8View
|
||||
{
|
||||
std::string m_utf8;
|
||||
typedef std::wstring_view SystemStringView;
|
||||
static inline void ToLower(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towlower); }
|
||||
static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towupper); }
|
||||
static inline size_t StrLen(const SystemChar* str) { return wcslen(str); }
|
||||
class SystemUTF8Conv {
|
||||
std::string m_utf8;
|
||||
|
||||
public:
|
||||
explicit SystemUTF8View(const SystemString& str)
|
||||
{
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0, nullptr, nullptr);
|
||||
m_utf8.assign(len, '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), &m_utf8[0], len, nullptr, nullptr);
|
||||
}
|
||||
inline const std::string& utf8_str() {return m_utf8;}
|
||||
explicit SystemUTF8Conv(SystemStringView str) {
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, str.data(), str.size(), nullptr, 0, nullptr, nullptr);
|
||||
m_utf8.assign(len, '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, str.data(), str.size(), &m_utf8[0], len, nullptr, nullptr);
|
||||
}
|
||||
std::string_view utf8_str() const { return m_utf8; }
|
||||
const char* c_str() const { return m_utf8.c_str(); }
|
||||
};
|
||||
class SystemStringView
|
||||
{
|
||||
std::wstring m_sys;
|
||||
class SystemStringConv {
|
||||
std::wstring m_sys;
|
||||
|
||||
public:
|
||||
explicit SystemStringView(const std::string& str)
|
||||
{
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0);
|
||||
m_sys.assign(len, L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), &m_sys[0], len);
|
||||
}
|
||||
inline const std::wstring& sys_str() {return m_sys;}
|
||||
explicit SystemStringConv(std::string_view str) {
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), nullptr, 0);
|
||||
m_sys.assign(len, L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), &m_sys[0], len);
|
||||
}
|
||||
SystemStringView sys_str() const { return m_sys; }
|
||||
const SystemChar* c_str() const { return m_sys.c_str(); }
|
||||
};
|
||||
#ifndef _S
|
||||
#define _S(val) L ## val
|
||||
#ifndef _SYS_STR
|
||||
#define _SYS_STR(val) L##val
|
||||
#endif
|
||||
#else
|
||||
typedef char SystemChar;
|
||||
typedef std::string SystemString;
|
||||
static inline void ToLower(SystemString& str)
|
||||
{std::transform(str.begin(), str.end(), str.begin(), tolower);}
|
||||
static inline void ToUpper(SystemString& str)
|
||||
{std::transform(str.begin(), str.end(), str.begin(), toupper);}
|
||||
static inline size_t StrLen(const SystemChar* str) {return strlen(str);}
|
||||
class SystemUTF8View
|
||||
{
|
||||
const std::string& m_utf8;
|
||||
typedef std::string_view SystemStringView;
|
||||
static inline void ToLower(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), tolower); }
|
||||
static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), toupper); }
|
||||
static inline size_t StrLen(const SystemChar* str) { return strlen(str); }
|
||||
class SystemUTF8Conv {
|
||||
std::string_view m_utf8;
|
||||
|
||||
public:
|
||||
explicit SystemUTF8View(const SystemString& str)
|
||||
: m_utf8(str) {}
|
||||
inline const std::string& utf8_str() {return m_utf8;}
|
||||
explicit SystemUTF8Conv(SystemStringView str) : m_utf8(str) {}
|
||||
std::string_view utf8_str() const { return m_utf8; }
|
||||
const char* c_str() const { return m_utf8.data(); }
|
||||
};
|
||||
class SystemStringView
|
||||
{
|
||||
const std::string& m_sys;
|
||||
class SystemStringConv {
|
||||
SystemStringView m_sys;
|
||||
|
||||
public:
|
||||
explicit SystemStringView(const std::string& str)
|
||||
: m_sys(str) {}
|
||||
inline const std::string& sys_str() {return m_sys;}
|
||||
explicit SystemStringConv(std::string_view str) : m_sys(str) {}
|
||||
SystemStringView sys_str() const { return m_sys; }
|
||||
const SystemChar* c_str() const { return m_sys.data(); }
|
||||
};
|
||||
#ifndef _S
|
||||
#define _S(val) val
|
||||
#ifndef _SYS_STR
|
||||
#define _SYS_STR(val) val
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static inline void Unlink(const SystemChar* file)
|
||||
{
|
||||
static inline void Unlink(const SystemChar* file) {
|
||||
#if NOD_UCS2
|
||||
_wunlink(file);
|
||||
_wunlink(file);
|
||||
#else
|
||||
unlink(file);
|
||||
unlink(file);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int StrCmp(const SystemChar* str1, const SystemChar* str2)
|
||||
{
|
||||
static inline int StrCmp(const SystemChar* str1, const SystemChar* str2) {
|
||||
#if NOD_UCS2
|
||||
return wcscmp(str1, str2);
|
||||
return wcscmp(str1, str2);
|
||||
#else
|
||||
return strcmp(str1, str2);
|
||||
return strcmp(str1, str2);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int StrCaseCmp(const SystemChar* str1, const SystemChar* str2)
|
||||
{
|
||||
static inline int StrCaseCmp(const SystemChar* str1, const SystemChar* str2) {
|
||||
#if NOD_UCS2
|
||||
return _wcsicmp(str1, str2);
|
||||
return _wcsicmp(str1, str2);
|
||||
#else
|
||||
return strcasecmp(str1, str2);
|
||||
return strcasecmp(str1, str2);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -167,79 +176,71 @@ static inline int StrCaseCmp(const SystemChar* str1, const SystemChar* str2)
|
||||
#undef bswap64
|
||||
/* Type-sensitive byte swappers */
|
||||
template <typename T>
|
||||
static inline T bswap16(T val)
|
||||
{
|
||||
static inline T bswap16(T val) {
|
||||
#if __GNUC__
|
||||
return __builtin_bswap16(val);
|
||||
return __builtin_bswap16(val);
|
||||
#elif _WIN32
|
||||
return _byteswap_ushort(val);
|
||||
return _byteswap_ushort(val);
|
||||
#else
|
||||
return (val = (val << 8) | ((val >> 8) & 0xFF));
|
||||
return (val = (val << 8) | ((val >> 8) & 0xFF));
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline T bswap32(T val)
|
||||
{
|
||||
static inline T bswap32(T val) {
|
||||
#if __GNUC__
|
||||
return __builtin_bswap32(val);
|
||||
return __builtin_bswap32(val);
|
||||
#elif _WIN32
|
||||
return _byteswap_ulong(val);
|
||||
return _byteswap_ulong(val);
|
||||
#else
|
||||
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
|
||||
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
|
||||
return val;
|
||||
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
|
||||
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline T bswap64(T val)
|
||||
{
|
||||
static inline T bswap64(T val) {
|
||||
#if __GNUC__
|
||||
return __builtin_bswap64(val);
|
||||
return __builtin_bswap64(val);
|
||||
#elif _WIN32
|
||||
return _byteswap_uint64(val);
|
||||
return _byteswap_uint64(val);
|
||||
#else
|
||||
return ((val & 0xFF00000000000000ULL) >> 56) |
|
||||
((val & 0x00FF000000000000ULL) >> 40) |
|
||||
((val & 0x0000FF0000000000ULL) >> 24) |
|
||||
((val & 0x000000FF00000000ULL) >> 8) |
|
||||
((val & 0x00000000FF000000ULL) << 8) |
|
||||
((val & 0x0000000000FF0000ULL) << 24) |
|
||||
((val & 0x000000000000FF00ULL) << 40) |
|
||||
((val & 0x00000000000000FFULL) << 56);
|
||||
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
|
||||
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
|
||||
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
|
||||
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
static inline int16_t SBig(int16_t val) {return bswap16(val);}
|
||||
static inline uint16_t SBig(uint16_t val) {return bswap16(val);}
|
||||
static inline int32_t SBig(int32_t val) {return bswap32(val);}
|
||||
static inline uint32_t SBig(uint32_t val) {return bswap32(val);}
|
||||
static inline int64_t SBig(int64_t val) {return bswap64(val);}
|
||||
static inline uint64_t SBig(uint64_t val) {return bswap64(val);}
|
||||
static inline int16_t SBig(int16_t val) { return bswap16(val); }
|
||||
static inline uint16_t SBig(uint16_t val) { return bswap16(val); }
|
||||
static inline int32_t SBig(int32_t val) { return bswap32(val); }
|
||||
static inline uint32_t SBig(uint32_t val) { return bswap32(val); }
|
||||
static inline int64_t SBig(int64_t val) { return bswap64(val); }
|
||||
static inline uint64_t SBig(uint64_t val) { return bswap64(val); }
|
||||
|
||||
static inline int16_t SLittle(int16_t val) {return val;}
|
||||
static inline uint16_t SLittle(uint16_t val) {return val;}
|
||||
static inline int32_t SLittle(int32_t val) {return val;}
|
||||
static inline uint32_t SLittle(uint32_t val) {return val;}
|
||||
static inline int64_t SLittle(int64_t val) {return val;}
|
||||
static inline uint64_t SLittle(uint64_t val) {return val;}
|
||||
static inline int16_t SLittle(int16_t val) { return val; }
|
||||
static inline uint16_t SLittle(uint16_t val) { return val; }
|
||||
static inline int32_t SLittle(int32_t val) { return val; }
|
||||
static inline uint32_t SLittle(uint32_t val) { return val; }
|
||||
static inline int64_t SLittle(int64_t val) { return val; }
|
||||
static inline uint64_t SLittle(uint64_t val) { return val; }
|
||||
#else
|
||||
static inline int16_t SLittle(int16_t val) {return bswap16(val);}
|
||||
static inline uint16_t SLittle(uint16_t val) {return bswap16(val);}
|
||||
static inline int32_t SLittle(int32_t val) {return bswap32(val);}
|
||||
static inline uint32_t SLittle(uint32_t val) {return bswap32(val);}
|
||||
static inline int64_t SLittle(int64_t val) {return bswap64(val);}
|
||||
static inline uint64_t SLittle(uint64_t val) {return bswap64(val);}
|
||||
static inline int16_t SLittle(int16_t val) { return bswap16(val); }
|
||||
static inline uint16_t SLittle(uint16_t val) { return bswap16(val); }
|
||||
static inline int32_t SLittle(int32_t val) { return bswap32(val); }
|
||||
static inline uint32_t SLittle(uint32_t val) { return bswap32(val); }
|
||||
static inline int64_t SLittle(int64_t val) { return bswap64(val); }
|
||||
static inline uint64_t SLittle(uint64_t val) { return bswap64(val); }
|
||||
|
||||
static inline int16_t SBig(int16_t val) {return val;}
|
||||
static inline uint16_t SBig(uint16_t val) {return val;}
|
||||
static inline int32_t SBig(int32_t val) {return val;}
|
||||
static inline uint32_t SBig(uint32_t val) {return val;}
|
||||
static inline int64_t SBig(int64_t val) {return val;}
|
||||
static inline uint64_t SBig(uint64_t val) {return val;}
|
||||
static inline int16_t SBig(int16_t val) { return val; }
|
||||
static inline uint16_t SBig(uint16_t val) { return val; }
|
||||
static inline int32_t SBig(int32_t val) { return val; }
|
||||
static inline uint32_t SBig(uint32_t val) { return val; }
|
||||
static inline int64_t SBig(int64_t val) { return val; }
|
||||
static inline uint64_t SBig(uint64_t val) { return val; }
|
||||
#endif
|
||||
|
||||
#ifndef ROUND_UP_32
|
||||
@@ -247,106 +248,77 @@ static inline uint64_t SBig(uint64_t val) {return val;}
|
||||
#define ROUND_UP_16(val) (((val) + 15) & ~15)
|
||||
#endif
|
||||
|
||||
enum class FileLockType
|
||||
{
|
||||
None = 0,
|
||||
Read,
|
||||
Write
|
||||
};
|
||||
static inline FILE* Fopen(const SystemChar* path, const SystemChar* mode, FileLockType lock=FileLockType::None)
|
||||
{
|
||||
enum class FileLockType { None = 0, Read, Write };
|
||||
static inline FILE* Fopen(const SystemChar* path, const SystemChar* mode, FileLockType lock = FileLockType::None) {
|
||||
#if NOD_UCS2
|
||||
FILE* fp = _wfopen(path, mode);
|
||||
if (!fp)
|
||||
return nullptr;
|
||||
FILE* fp = _wfopen(path, mode);
|
||||
if (!fp)
|
||||
return nullptr;
|
||||
#else
|
||||
FILE* fp = fopen(path, mode);
|
||||
if (!fp)
|
||||
return nullptr;
|
||||
FILE* fp = fopen(path, mode);
|
||||
if (!fp)
|
||||
return nullptr;
|
||||
#endif
|
||||
|
||||
if (lock != FileLockType::None)
|
||||
{
|
||||
if (lock != FileLockType::None) {
|
||||
#if _WIN32
|
||||
OVERLAPPED ov = {};
|
||||
LockFileEx((HANDLE)(uintptr_t)_fileno(fp), (lock == FileLockType::Write) ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1, &ov);
|
||||
OVERLAPPED ov = {};
|
||||
LockFileEx((HANDLE)(uintptr_t)_fileno(fp), (lock == FileLockType::Write) ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1,
|
||||
&ov);
|
||||
#else
|
||||
if (flock(fileno(fp), ((lock == FileLockType::Write) ? LOCK_EX : LOCK_SH) | LOCK_NB))
|
||||
LogModule.report(logvisor::Error, "flock %s: %s", path, strerror(errno));
|
||||
if (flock(fileno(fp), ((lock == FileLockType::Write) ? LOCK_EX : LOCK_SH) | LOCK_NB))
|
||||
LogModule.report(logvisor::Error, FMT_STRING("flock {}: {}"), path, strerror(errno));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return fp;
|
||||
return fp;
|
||||
}
|
||||
|
||||
static inline int FSeek(FILE* fp, int64_t offset, int whence)
|
||||
{
|
||||
static inline int FSeek(FILE* fp, int64_t offset, int whence) {
|
||||
#if _WIN32
|
||||
return _fseeki64(fp, offset, whence);
|
||||
return _fseeki64(fp, offset, whence);
|
||||
#elif __APPLE__ || __FreeBSD__
|
||||
return fseeko(fp, offset, whence);
|
||||
return fseeko(fp, offset, whence);
|
||||
#else
|
||||
return fseeko64(fp, offset, whence);
|
||||
return fseeko64(fp, offset, whence);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int64_t FTell(FILE* fp)
|
||||
{
|
||||
static inline int64_t FTell(FILE* fp) {
|
||||
#if _WIN32
|
||||
return _ftelli64(fp);
|
||||
return _ftelli64(fp);
|
||||
#elif __APPLE__ || __FreeBSD__
|
||||
return ftello(fp);
|
||||
return ftello(fp);
|
||||
#else
|
||||
return ftello64(fp);
|
||||
return ftello64(fp);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline bool CheckFreeSpace(const SystemChar* path, size_t reqSz)
|
||||
{
|
||||
static inline bool CheckFreeSpace(const SystemChar* path, size_t reqSz) {
|
||||
#if _WIN32
|
||||
ULARGE_INTEGER freeBytes;
|
||||
wchar_t buf[1024];
|
||||
wchar_t* end;
|
||||
DWORD ret = GetFullPathNameW(path, 1024, buf, &end);
|
||||
if (!ret || ret > 1024)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("GetFullPathNameW %s"), path);
|
||||
return false;
|
||||
}
|
||||
if (end)
|
||||
end[0] = L'\0';
|
||||
if (!GetDiskFreeSpaceExW(buf, &freeBytes, nullptr, nullptr))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("GetDiskFreeSpaceExW %s: %d"), path, GetLastError());
|
||||
return false;
|
||||
}
|
||||
return reqSz < freeBytes.QuadPart;
|
||||
ULARGE_INTEGER freeBytes;
|
||||
wchar_t buf[1024];
|
||||
wchar_t* end;
|
||||
DWORD ret = GetFullPathNameW(path, 1024, buf, &end);
|
||||
if (!ret || ret > 1024) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("GetFullPathNameW {}")), path);
|
||||
return false;
|
||||
}
|
||||
if (end)
|
||||
end[0] = L'\0';
|
||||
if (!GetDiskFreeSpaceExW(buf, &freeBytes, nullptr, nullptr)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("GetDiskFreeSpaceExW {}: {}")), path, GetLastError());
|
||||
return false;
|
||||
}
|
||||
return reqSz < freeBytes.QuadPart;
|
||||
#else
|
||||
struct statvfs svfs;
|
||||
if (statvfs(path, &svfs))
|
||||
{
|
||||
LogModule.report(logvisor::Error, "statvfs %s: %s", path, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return reqSz < svfs.f_frsize * svfs.f_bavail;
|
||||
struct statvfs svfs;
|
||||
if (statvfs(path, &svfs)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("statvfs {}: {}"), path, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return reqSz < svfs.f_frsize * svfs.f_bavail;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if __GNUC__
|
||||
__attribute__((__format__ (__printf__, 1, 2)))
|
||||
#endif
|
||||
static inline void Printf(const SystemChar* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
#if NOD_UCS2
|
||||
vwprintf(fmt, args);
|
||||
#else
|
||||
vprintf(fmt, args);
|
||||
#endif
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // __NOD_UTIL_HPP__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,24 +1,19 @@
|
||||
#ifndef __AES_HPP__
|
||||
#define __AES_HPP__
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
|
||||
class IAES
|
||||
{
|
||||
class IAES {
|
||||
public:
|
||||
virtual ~IAES() = default;
|
||||
virtual void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len)=0;
|
||||
virtual void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len)=0;
|
||||
virtual void setKey(const uint8_t* key)=0;
|
||||
virtual ~IAES() = default;
|
||||
virtual void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len) = 0;
|
||||
virtual void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len) = 0;
|
||||
virtual void setKey(const uint8_t* key) = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IAES> NewAES();
|
||||
|
||||
}
|
||||
|
||||
#endif //__AES_HPP__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,29 +1,21 @@
|
||||
#ifndef __NOD_LIB__
|
||||
#define __NOD_LIB__
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include "logvisor/logvisor.hpp"
|
||||
#include "Util.hpp"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
namespace nod {
|
||||
|
||||
class DiscBase;
|
||||
|
||||
struct ExtractionContext final
|
||||
{
|
||||
bool force : 1;
|
||||
std::function<void(const std::string&, float)> progressCB;
|
||||
struct ExtractionContext final {
|
||||
bool force : 1;
|
||||
std::function<void(std::string_view, float)> progressCB;
|
||||
};
|
||||
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path);
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path, bool& isWii);
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path);
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path, bool& isWii);
|
||||
|
||||
}
|
||||
|
||||
#include "DiscGCN.hpp"
|
||||
#include "DiscWii.hpp"
|
||||
#include "IDiscIO.hpp"
|
||||
|
||||
#endif // __NOD_LIB__
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,21 +1,51 @@
|
||||
if(NOT WIN32)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-multichar")
|
||||
set(PLAT_SRCS FileIOFILE.cpp)
|
||||
else()
|
||||
set(PLAT_SRCS FileIOWin32.cpp)
|
||||
endif()
|
||||
add_library(nod
|
||||
aes.cpp
|
||||
sha1.c
|
||||
DiscBase.cpp
|
||||
DiscGCN.cpp
|
||||
DiscIOISO.cpp
|
||||
DiscIOWBFS.cpp
|
||||
DiscWii.cpp
|
||||
DirectoryEnumerator.cpp
|
||||
nod.cpp
|
||||
${PLAT_SRCS}
|
||||
${NOD_HEADERS})
|
||||
if(NOT MSVC)
|
||||
set_source_files_properties(aes.cpp PROPERTIES COMPILE_FLAGS -maes)
|
||||
aes.cpp
|
||||
sha1.c
|
||||
|
||||
DirectoryEnumerator.cpp
|
||||
DiscBase.cpp
|
||||
DiscGCN.cpp
|
||||
DiscIOISO.cpp
|
||||
DiscIONFS.cpp
|
||||
DiscIOWBFS.cpp
|
||||
DiscWii.cpp
|
||||
nod.cpp
|
||||
|
||||
../include/nod/aes.hpp
|
||||
../include/nod/DirectoryEnumerator.hpp
|
||||
../include/nod/DiscBase.hpp
|
||||
../include/nod/DiscGCN.hpp
|
||||
../include/nod/DiscWii.hpp
|
||||
../include/nod/IDiscIO.hpp
|
||||
../include/nod/IFileIO.hpp
|
||||
../include/nod/nod.hpp
|
||||
../include/nod/sha1.h
|
||||
../include/nod/Util.hpp
|
||||
)
|
||||
|
||||
target_include_directories(nod PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
|
||||
)
|
||||
|
||||
target_link_libraries(nod PUBLIC logvisor)
|
||||
|
||||
if(WIN32)
|
||||
target_sources(nod PRIVATE FileIOWin32.cpp)
|
||||
else()
|
||||
target_compile_options(nod PRIVATE -Wno-multichar)
|
||||
target_sources(nod PRIVATE FileIOFILE.cpp)
|
||||
endif()
|
||||
|
||||
if(NOT MSVC AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL x86_64)
|
||||
set_source_files_properties(aes.cpp PROPERTIES COMPILE_FLAGS -maes)
|
||||
endif()
|
||||
|
||||
# Associate target with export
|
||||
install(
|
||||
TARGETS nod
|
||||
EXPORT nodTargets
|
||||
ARCHIVE DESTINATION "lib"
|
||||
INCLUDES DESTINATION include # This sets the INTERFACE_INCLUDE_DIRECTORIES property of the target.
|
||||
)
|
||||
|
||||
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../include/nod DESTINATION include)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "nod/DirectoryEnumerator.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
@@ -5,279 +7,256 @@
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
#include "nod/DirectoryEnumerator.hpp"
|
||||
namespace nod {
|
||||
|
||||
namespace nod
|
||||
{
|
||||
|
||||
DirectoryEnumerator::DirectoryEnumerator(const SystemChar* path, Mode mode,
|
||||
bool sizeSort, bool reverse, bool noHidden)
|
||||
{
|
||||
Sstat theStat;
|
||||
if (Stat(path, &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
return;
|
||||
DirectoryEnumerator::DirectoryEnumerator(SystemStringView path, Mode mode, bool sizeSort, bool reverse, bool noHidden) {
|
||||
Sstat theStat;
|
||||
if (Stat(path.data(), &theStat) || !S_ISDIR(theStat.st_mode))
|
||||
return;
|
||||
|
||||
#if _WIN32
|
||||
SystemString wc(path);
|
||||
wc += _S("/*");
|
||||
WIN32_FIND_DATAW d;
|
||||
HANDLE dir = FindFirstFileW(wc.c_str(), &d);
|
||||
if (dir == INVALID_HANDLE_VALUE)
|
||||
return;
|
||||
switch (mode)
|
||||
{
|
||||
case Mode::Native:
|
||||
do
|
||||
{
|
||||
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _S('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st))
|
||||
continue;
|
||||
SystemString wc(path);
|
||||
wc += _SYS_STR("/*");
|
||||
WIN32_FIND_DATAW d;
|
||||
HANDLE dir = FindFirstFileW(wc.c_str(), &d);
|
||||
if (dir == INVALID_HANDLE_VALUE)
|
||||
return;
|
||||
switch (mode) {
|
||||
case Mode::Native:
|
||||
do {
|
||||
if (!wcscmp(d.cFileName, _SYS_STR(".")) || !wcscmp(d.cFileName, _SYS_STR("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _SYS_STR('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st))
|
||||
continue;
|
||||
|
||||
size_t sz = 0;
|
||||
bool isDir = false;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
isDir = true;
|
||||
else if (S_ISREG(st.st_mode))
|
||||
sz = st.st_size;
|
||||
else
|
||||
continue;
|
||||
size_t sz = 0;
|
||||
bool isDir = false;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
isDir = true;
|
||||
else if (S_ISREG(st.st_mode))
|
||||
sz = st.st_size;
|
||||
else
|
||||
continue;
|
||||
|
||||
m_entries.push_back(std::move(Entry(std::move(fp), d.cFileName, sz, isDir)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
break;
|
||||
case Mode::DirsThenFilesSorted:
|
||||
case Mode::DirsSorted:
|
||||
{
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
do
|
||||
{
|
||||
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp +=_S('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d.cFileName, Entry(std::move(fp), d.cFileName, 0, true)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
m_entries.push_back(Entry(fp, d.cFileName, sz, isDir));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
break;
|
||||
case Mode::DirsThenFilesSorted:
|
||||
case Mode::DirsSorted: {
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
do {
|
||||
if (!wcscmp(d.cFileName, _SYS_STR(".")) || !wcscmp(d.cFileName, _SYS_STR("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _SYS_STR('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d.cFileName, Entry(fp, d.cFileName, 0, true)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
|
||||
if (mode == Mode::DirsSorted)
|
||||
break;
|
||||
FindClose(dir);
|
||||
dir = FindFirstFileW(wc.c_str(), &d);
|
||||
}
|
||||
case Mode::FilesSorted:
|
||||
{
|
||||
if (mode == Mode::FilesSorted)
|
||||
m_entries.clear();
|
||||
|
||||
if (sizeSort)
|
||||
{
|
||||
std::multimap<size_t, Entry> sort;
|
||||
do
|
||||
{
|
||||
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _S('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(st.st_size, Entry(std::move(fp), d.cFileName, st.st_size, false)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
do
|
||||
{
|
||||
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _S('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d.cFileName, Entry(std::move(fp), d.cFileName, st.st_size, false)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mode == Mode::DirsSorted)
|
||||
break;
|
||||
FindClose(dir);
|
||||
dir = FindFirstFileW(wc.c_str(), &d);
|
||||
}
|
||||
case Mode::FilesSorted: {
|
||||
if (mode == Mode::FilesSorted)
|
||||
m_entries.clear();
|
||||
|
||||
if (sizeSort) {
|
||||
std::multimap<size_t, Entry> sort;
|
||||
do {
|
||||
if (!wcscmp(d.cFileName, _SYS_STR(".")) || !wcscmp(d.cFileName, _SYS_STR("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _SYS_STR('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(st.st_size, Entry(fp, d.cFileName, st.st_size, false)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
} else {
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
do {
|
||||
if (!wcscmp(d.cFileName, _SYS_STR(".")) || !wcscmp(d.cFileName, _SYS_STR("..")))
|
||||
continue;
|
||||
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += _SYS_STR('/');
|
||||
fp += d.cFileName;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d.cFileName, Entry(fp, d.cFileName, st.st_size, false)));
|
||||
} while (FindNextFileW(dir, &d));
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
FindClose(dir);
|
||||
|
||||
#else
|
||||
|
||||
DIR* dir = opendir(path);
|
||||
if (!dir)
|
||||
return;
|
||||
const dirent* d;
|
||||
switch (mode)
|
||||
{
|
||||
case Mode::Native:
|
||||
while ((d = readdir(dir)))
|
||||
{
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st))
|
||||
continue;
|
||||
DIR* dir = opendir(path.data());
|
||||
if (!dir)
|
||||
return;
|
||||
const dirent* d;
|
||||
switch (mode) {
|
||||
case Mode::Native:
|
||||
while ((d = readdir(dir))) {
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st))
|
||||
continue;
|
||||
|
||||
size_t sz = 0;
|
||||
bool isDir = false;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
isDir = true;
|
||||
else if (S_ISREG(st.st_mode))
|
||||
sz = st.st_size;
|
||||
else
|
||||
continue;
|
||||
size_t sz = 0;
|
||||
bool isDir = false;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
isDir = true;
|
||||
else if (S_ISREG(st.st_mode))
|
||||
sz = st.st_size;
|
||||
else
|
||||
continue;
|
||||
|
||||
m_entries.push_back(Entry(std::move(fp), d->d_name, sz, isDir));
|
||||
}
|
||||
break;
|
||||
case Mode::DirsThenFilesSorted:
|
||||
case Mode::DirsSorted:
|
||||
{
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
while ((d = readdir(dir)))
|
||||
{
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d->d_name, Entry(std::move(fp), d->d_name, 0, true)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
|
||||
if (mode == Mode::DirsSorted)
|
||||
break;
|
||||
rewinddir(dir);
|
||||
m_entries.push_back(Entry(fp, d->d_name, sz, isDir));
|
||||
}
|
||||
case Mode::FilesSorted:
|
||||
{
|
||||
if (mode == Mode::FilesSorted)
|
||||
m_entries.clear();
|
||||
|
||||
if (sizeSort)
|
||||
{
|
||||
std::multimap<size_t, Entry> sort;
|
||||
while ((d = readdir(dir)))
|
||||
{
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(st.st_size, Entry(std::move(fp), d->d_name, st.st_size, false)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
while ((d = readdir(dir)))
|
||||
{
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d->d_name, Entry(std::move(fp), d->d_name, st.st_size, false)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it=sort.crbegin() ; it != sort.crend() ; ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
case Mode::DirsThenFilesSorted:
|
||||
case Mode::DirsSorted: {
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
while ((d = readdir(dir))) {
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, 0, true)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
|
||||
if (mode == Mode::DirsSorted)
|
||||
break;
|
||||
rewinddir(dir);
|
||||
[[fallthrough]];
|
||||
}
|
||||
case Mode::FilesSorted: {
|
||||
if (mode == Mode::FilesSorted)
|
||||
m_entries.clear();
|
||||
|
||||
if (sizeSort) {
|
||||
std::multimap<size_t, Entry> sort;
|
||||
while ((d = readdir(dir))) {
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(st.st_size, Entry(fp, d->d_name, st.st_size, false)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
} else {
|
||||
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
||||
while ((d = readdir(dir))) {
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (noHidden && d->d_name[0] == '.')
|
||||
continue;
|
||||
SystemString fp(path);
|
||||
fp += '/';
|
||||
fp += d->d_name;
|
||||
Sstat st;
|
||||
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
||||
continue;
|
||||
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, st.st_size, false)));
|
||||
}
|
||||
|
||||
m_entries.reserve(sort.size());
|
||||
if (reverse)
|
||||
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
||||
m_entries.push_back(std::move(it->second));
|
||||
else
|
||||
for (auto& e : sort)
|
||||
m_entries.push_back(std::move(e.second));
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace nod
|
||||
|
||||
1657
lib/DiscBase.cpp
1657
lib/DiscBase.cpp
File diff suppressed because it is too large
Load Diff
831
lib/DiscGCN.cpp
831
lib/DiscGCN.cpp
@@ -1,468 +1,431 @@
|
||||
#include "nod/DiscGCN.hpp"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "nod/nod.hpp"
|
||||
#include <inttypes.h>
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
#define BUFFER_SZ 0x8000
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
|
||||
class PartitionGCN : public DiscBase::IPartition
|
||||
{
|
||||
class PartitionGCN : public IPartition {
|
||||
public:
|
||||
PartitionGCN(const DiscGCN& parent, uint64_t offset, bool& err)
|
||||
: IPartition(parent, PartitionKind::Data, false, offset)
|
||||
{
|
||||
/* GCN-specific header reads */
|
||||
std::unique_ptr<IPartReadStream> s = beginReadStream(0x0);
|
||||
if (!s)
|
||||
{
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
m_header.read(*s);
|
||||
m_bi2Header.read(*s);
|
||||
m_dolOff = m_header.m_dolOff;
|
||||
m_fstOff = m_header.m_fstOff;
|
||||
m_fstSz = m_header.m_fstSz;
|
||||
uint32_t vals[2];
|
||||
s->seek(0x2440 + 0x14);
|
||||
s->read(vals, 8);
|
||||
m_apploaderSz = 32 + SBig(vals[0]) + SBig(vals[1]);
|
||||
|
||||
/* Yay files!! */
|
||||
parseFST(*s);
|
||||
|
||||
/* Also make DOL header and size handy */
|
||||
s->seek(m_dolOff);
|
||||
parseDOL(*s);
|
||||
PartitionGCN(const DiscGCN& parent, uint64_t offset, bool& err)
|
||||
: IPartition(parent, PartitionKind::Data, false, offset) {
|
||||
/* GCN-specific header reads */
|
||||
std::unique_ptr<IPartReadStream> s = beginReadStream(0x0);
|
||||
if (!s) {
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
m_header.read(*s);
|
||||
m_bi2Header.read(*s);
|
||||
m_dolOff = m_header.m_dolOff;
|
||||
m_fstOff = m_header.m_fstOff;
|
||||
m_fstSz = m_header.m_fstSz;
|
||||
uint32_t vals[2];
|
||||
s->seek(0x2440 + 0x14);
|
||||
s->read(vals, 8);
|
||||
m_apploaderSz = 32 + SBig(vals[0]) + SBig(vals[1]);
|
||||
|
||||
class PartReadStream : public IPartReadStream
|
||||
{
|
||||
const PartitionGCN& m_parent;
|
||||
std::unique_ptr<IReadStream> m_dio;
|
||||
/* Yay files!! */
|
||||
parseFST(*s);
|
||||
|
||||
uint64_t m_offset;
|
||||
size_t m_curBlock = SIZE_MAX;
|
||||
uint8_t m_buf[BUFFER_SZ];
|
||||
/* Also make DOL header and size handy */
|
||||
s->seek(m_dolOff);
|
||||
parseDOL(*s);
|
||||
}
|
||||
|
||||
public:
|
||||
PartReadStream(const PartitionGCN& parent, uint64_t offset, bool& err)
|
||||
: m_parent(parent), m_offset(offset)
|
||||
{
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
m_dio = m_parent.m_parent.getDiscIO().beginReadStream(block * BUFFER_SZ);
|
||||
if (!m_dio)
|
||||
{
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
void seek(int64_t offset, int whence)
|
||||
{
|
||||
if (whence == SEEK_SET)
|
||||
m_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
m_offset += offset;
|
||||
else
|
||||
return;
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
if (block != m_curBlock)
|
||||
{
|
||||
m_dio->seek(block * BUFFER_SZ);
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
}
|
||||
uint64_t position() const {return m_offset;}
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
size_t cacheOffset = m_offset % BUFFER_SZ;
|
||||
uint64_t cacheSize;
|
||||
uint64_t rem = length;
|
||||
uint8_t* dst = (uint8_t*)buf;
|
||||
class PartReadStream : public IPartReadStream {
|
||||
const PartitionGCN& m_parent;
|
||||
std::unique_ptr<IReadStream> m_dio;
|
||||
|
||||
while (rem)
|
||||
{
|
||||
if (block != m_curBlock)
|
||||
{
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
uint64_t m_offset;
|
||||
size_t m_curBlock = SIZE_MAX;
|
||||
uint8_t m_buf[BUFFER_SZ];
|
||||
|
||||
cacheSize = rem;
|
||||
if (cacheSize + cacheOffset > BUFFER_SZ)
|
||||
cacheSize = BUFFER_SZ - cacheOffset;
|
||||
|
||||
memmove(dst, m_buf + cacheOffset, cacheSize);
|
||||
dst += cacheSize;
|
||||
rem -= cacheSize;
|
||||
cacheOffset = 0;
|
||||
++block;
|
||||
}
|
||||
|
||||
m_offset += length;
|
||||
return dst - (uint8_t*)buf;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IPartReadStream>(new PartReadStream(*this, offset, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
DiscGCN::DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err)
|
||||
: DiscBase(std::move(dio), err)
|
||||
{
|
||||
if (err)
|
||||
public:
|
||||
PartReadStream(const PartitionGCN& parent, uint64_t offset, bool& err) : m_parent(parent), m_offset(offset) {
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
m_dio = m_parent.m_parent.getDiscIO().beginReadStream(block * BUFFER_SZ);
|
||||
if (!m_dio) {
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
void seek(int64_t offset, int whence) override {
|
||||
if (whence == SEEK_SET)
|
||||
m_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
m_offset += offset;
|
||||
else
|
||||
return;
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
if (block != m_curBlock) {
|
||||
m_dio->seek(block * BUFFER_SZ);
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
}
|
||||
uint64_t position() const override { return m_offset; }
|
||||
uint64_t read(void* buf, uint64_t length) override {
|
||||
size_t block = m_offset / BUFFER_SZ;
|
||||
size_t cacheOffset = m_offset % BUFFER_SZ;
|
||||
uint64_t cacheSize;
|
||||
uint64_t rem = length;
|
||||
uint8_t* dst = (uint8_t*)buf;
|
||||
|
||||
/* One lone partition for GCN */
|
||||
m_partitions.emplace_back(new PartitionGCN(*this, 0, err));
|
||||
}
|
||||
|
||||
DiscBuilderGCN DiscGCN::makeMergeBuilder(const SystemChar* outPath, FProgress progressCB)
|
||||
{
|
||||
return DiscBuilderGCN(outPath, progressCB);
|
||||
}
|
||||
|
||||
bool DiscGCN::extractDiscHeaderFiles(const SystemString& path, const ExtractionContext& ctx) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
class PartitionBuilderGCN : public DiscBuilderBase::PartitionBuilderBase
|
||||
{
|
||||
uint64_t m_curUser = 0x57058000;
|
||||
|
||||
public:
|
||||
class PartWriteStream : public IPartWriteStream
|
||||
{
|
||||
const PartitionBuilderGCN& m_parent;
|
||||
uint64_t m_offset;
|
||||
std::unique_ptr<IFileIO::IWriteStream> m_fio;
|
||||
|
||||
public:
|
||||
PartWriteStream(const PartitionBuilderGCN& parent, uint64_t offset, bool& err)
|
||||
: m_parent(parent), m_offset(offset)
|
||||
{
|
||||
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(offset);
|
||||
if (!m_fio)
|
||||
err = true;
|
||||
while (rem) {
|
||||
if (block != m_curBlock) {
|
||||
m_dio->read(m_buf, BUFFER_SZ);
|
||||
m_curBlock = block;
|
||||
}
|
||||
void close() {m_fio.reset();}
|
||||
uint64_t position() const {return m_offset;}
|
||||
uint64_t write(const void* buf, uint64_t length)
|
||||
{
|
||||
uint64_t len = m_fio->write(buf, length);
|
||||
m_offset += len;
|
||||
return len;
|
||||
}
|
||||
void seek(size_t off)
|
||||
{
|
||||
m_offset = off;
|
||||
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(off);
|
||||
}
|
||||
};
|
||||
|
||||
PartitionBuilderGCN(DiscBuilderBase& parent)
|
||||
: DiscBuilderBase::PartitionBuilderBase(parent, PartitionKind::Data, false) {}
|
||||
cacheSize = rem;
|
||||
if (cacheSize + cacheOffset > BUFFER_SZ)
|
||||
cacheSize = BUFFER_SZ - cacheOffset;
|
||||
|
||||
uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws)
|
||||
{
|
||||
m_curUser -= reqSz;
|
||||
m_curUser &= 0xfffffffffffffff0;
|
||||
if (m_curUser < 0x30000)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "user area low mark reached");
|
||||
return -1;
|
||||
}
|
||||
static_cast<PartWriteStream&>(ws).seek(m_curUser);
|
||||
return m_curUser;
|
||||
memmove(dst, m_buf + cacheOffset, cacheSize);
|
||||
dst += cacheSize;
|
||||
rem -= cacheSize;
|
||||
cacheOffset = 0;
|
||||
++block;
|
||||
}
|
||||
|
||||
m_offset += length;
|
||||
return dst - (uint8_t*)buf;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<PartReadStream>(*this, offset, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint32_t packOffset(uint64_t offset) const
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset)
|
||||
{
|
||||
bool Err = false;
|
||||
std::unique_ptr<IPartWriteStream> ret = std::make_unique<PartWriteStream>(*this, offset, Err);
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool _build(const std::function<bool(IPartWriteStream&, uint32_t, uint32_t,
|
||||
uint32_t, uint32_t, uint32_t)>& headerFunc,
|
||||
const std::function<bool(IPartWriteStream&)>& bi2Func,
|
||||
const std::function<bool(IPartWriteStream&, size_t&)>& apploaderFunc)
|
||||
{
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0x2440);
|
||||
if (!ws)
|
||||
return false;
|
||||
size_t xferSz = 0;
|
||||
if (!apploaderFunc(*ws, xferSz))
|
||||
return false;
|
||||
|
||||
size_t fstOff = ROUND_UP_32(xferSz);
|
||||
size_t fstSz = sizeof(FSTNode) * m_buildNodes.size();
|
||||
for (size_t i=0 ; i<fstOff-xferSz ; ++i)
|
||||
ws->write("\xff", 1);
|
||||
fstOff += 0x2440;
|
||||
ws->write(m_buildNodes.data(), fstSz);
|
||||
for (const std::string& str : m_buildNames)
|
||||
ws->write(str.data(), str.size()+1);
|
||||
fstSz += m_buildNameOff;
|
||||
fstSz = ROUND_UP_32(fstSz);
|
||||
|
||||
if (fstOff + fstSz >= m_curUser)
|
||||
{
|
||||
LogModule.report(logvisor::Error,
|
||||
"FST flows into user area (one or the other is too big)");
|
||||
return false;
|
||||
}
|
||||
|
||||
ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return false;
|
||||
if (!headerFunc(*ws, m_dolOffset, fstOff, fstSz, m_curUser, 0x57058000 - m_curUser))
|
||||
return false;
|
||||
if (!bi2Func(*ws))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool buildFromDirectory(const SystemChar* dirIn)
|
||||
{
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return false;
|
||||
bool result = DiscBuilderBase::PartitionBuilderBase::buildFromDirectory(*ws, dirIn);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
SystemString dirStr(dirIn);
|
||||
|
||||
/* Check Apploader */
|
||||
SystemString apploaderIn = dirStr + _S("/sys/apploader.img");
|
||||
Sstat apploaderStat;
|
||||
if (Stat(apploaderIn.c_str(), &apploaderStat))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to stat %s"), apploaderIn.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check Boot */
|
||||
SystemString bootIn = dirStr + _S("/sys/boot.bin");
|
||||
Sstat bootStat;
|
||||
if (Stat(bootIn.c_str(), &bootStat))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to stat %s"), bootIn.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check BI2 */
|
||||
SystemString bi2In = dirStr + _S("/sys/bi2.bin");
|
||||
Sstat bi2Stat;
|
||||
if (Stat(bi2In.c_str(), &bi2Stat))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to stat %s"), bi2In.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _build(
|
||||
[this, &bootIn](IPartWriteStream& ws, uint32_t dolOff, uint32_t fstOff, uint32_t fstSz,
|
||||
uint32_t userOff, uint32_t userSz) -> bool
|
||||
{
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(bootIn.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
Header header;
|
||||
header.read(*rs);
|
||||
header.m_dolOff = dolOff;
|
||||
header.m_fstOff = fstOff;
|
||||
header.m_fstSz = fstSz;
|
||||
header.m_fstMaxSz = fstSz;
|
||||
header.m_userPosition = userOff;
|
||||
header.m_userSz = userSz;
|
||||
header.write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, &bi2In](IPartWriteStream& ws) -> bool
|
||||
{
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(bi2In.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
BI2Header bi2;
|
||||
bi2.read(*rs);
|
||||
bi2.write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, &apploaderIn](IPartWriteStream& ws, size_t& xferSz) -> bool
|
||||
{
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(apploaderIn.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
char buf[8192];
|
||||
while (true)
|
||||
{
|
||||
size_t rdSz = rs->read(buf, 8192);
|
||||
if (!rdSz)
|
||||
break;
|
||||
ws.write(buf, rdSz);
|
||||
xferSz += rdSz;
|
||||
if (0x2440 + xferSz >= m_curUser)
|
||||
{
|
||||
LogModule.report(logvisor::Error,
|
||||
"apploader flows into user area (one or the other is too big)");
|
||||
return false;
|
||||
}
|
||||
m_parent.m_progressCB(m_parent.getProgressFactor(), apploaderIn, xferSz);
|
||||
}
|
||||
++m_parent.m_progressIdx;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool mergeFromDirectory(const PartitionGCN* partIn, const SystemChar* dirIn)
|
||||
{
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return false;
|
||||
bool result = DiscBuilderBase::PartitionBuilderBase::mergeFromDirectory(*ws, partIn, dirIn);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
return _build(
|
||||
[this, partIn](IPartWriteStream& ws, uint32_t dolOff, uint32_t fstOff, uint32_t fstSz,
|
||||
uint32_t userOff, uint32_t userSz) -> bool
|
||||
{
|
||||
Header header = partIn->getHeader();
|
||||
header.m_dolOff = dolOff;
|
||||
header.m_fstOff = fstOff;
|
||||
header.m_fstSz = fstSz;
|
||||
header.m_fstMaxSz = fstSz;
|
||||
header.m_userPosition = userOff;
|
||||
header.m_userSz = userSz;
|
||||
header.write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, partIn](IPartWriteStream& ws) -> bool
|
||||
{
|
||||
partIn->getBI2().write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, partIn](IPartWriteStream& ws, size_t& xferSz) -> bool
|
||||
{
|
||||
std::unique_ptr<uint8_t[]> apploaderBuf = partIn->getApploaderBuf();
|
||||
size_t apploaderSz = partIn->getApploaderSize();
|
||||
SystemString apploaderName(_S("<apploader>"));
|
||||
ws.write(apploaderBuf.get(), apploaderSz);
|
||||
xferSz += apploaderSz;
|
||||
if (0x2440 + xferSz >= m_curUser)
|
||||
{
|
||||
LogModule.report(logvisor::Error,
|
||||
"apploader flows into user area (one or the other is too big)");
|
||||
return false;
|
||||
}
|
||||
m_parent.m_progressCB(m_parent.getProgressFactor(), apploaderName, xferSz);
|
||||
++m_parent.m_progressIdx;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
EBuildResult DiscBuilderGCN::buildFromDirectory(const SystemChar* dirIn)
|
||||
{
|
||||
if (!m_fileIO->beginWriteStream())
|
||||
return EBuildResult::Failed;
|
||||
if (!CheckFreeSpace(m_outPath.c_str(), 0x57058000))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("not enough free disk space for %s"), m_outPath.c_str());
|
||||
return EBuildResult::DiskFull;
|
||||
DiscGCN::DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err) : DiscBase(std::move(dio), err) {
|
||||
if (err)
|
||||
return;
|
||||
|
||||
/* One lone partition for GCN */
|
||||
m_partitions.emplace_back(std::make_unique<PartitionGCN>(*this, 0, err));
|
||||
}
|
||||
|
||||
DiscBuilderGCN DiscGCN::makeMergeBuilder(SystemStringView outPath, FProgress progressCB) {
|
||||
return DiscBuilderGCN(outPath, progressCB);
|
||||
}
|
||||
|
||||
bool DiscGCN::extractDiscHeaderFiles(SystemStringView path, const ExtractionContext& ctx) const { return true; }
|
||||
|
||||
class PartitionBuilderGCN : public DiscBuilderBase::PartitionBuilderBase {
|
||||
uint64_t m_curUser = 0x57058000;
|
||||
|
||||
public:
|
||||
class PartWriteStream : public IPartWriteStream {
|
||||
const PartitionBuilderGCN& m_parent;
|
||||
uint64_t m_offset;
|
||||
std::unique_ptr<IFileIO::IWriteStream> m_fio;
|
||||
|
||||
public:
|
||||
PartWriteStream(const PartitionBuilderGCN& parent, uint64_t offset, bool& err)
|
||||
: m_parent(parent), m_offset(offset) {
|
||||
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(offset);
|
||||
if (!m_fio)
|
||||
err = true;
|
||||
}
|
||||
m_progressCB(getProgressFactor(), _S("Preallocating image"), -1);
|
||||
++m_progressIdx;
|
||||
auto ws = m_fileIO->beginWriteStream(0x57058000 - 1);
|
||||
void close() override { m_fio.reset(); }
|
||||
uint64_t position() const override { return m_offset; }
|
||||
uint64_t write(const void* buf, uint64_t length) override {
|
||||
uint64_t len = m_fio->write(buf, length);
|
||||
m_offset += len;
|
||||
return len;
|
||||
}
|
||||
void seek(size_t off) {
|
||||
m_offset = off;
|
||||
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(off);
|
||||
}
|
||||
};
|
||||
|
||||
PartitionBuilderGCN(DiscBuilderBase& parent)
|
||||
: DiscBuilderBase::PartitionBuilderBase(parent, PartitionKind::Data, false) {}
|
||||
|
||||
uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws) override {
|
||||
m_curUser -= reqSz;
|
||||
m_curUser &= 0xfffffffffffffff0;
|
||||
if (m_curUser < 0x30000) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("user area low mark reached"));
|
||||
return -1;
|
||||
}
|
||||
static_cast<PartWriteStream&>(ws).seek(m_curUser);
|
||||
return m_curUser;
|
||||
}
|
||||
|
||||
uint32_t packOffset(uint64_t offset) const override { return offset; }
|
||||
|
||||
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<PartWriteStream>(*this, offset, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
_build(const std::function<bool(IPartWriteStream&, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)>& headerFunc,
|
||||
const std::function<bool(IPartWriteStream&)>& bi2Func,
|
||||
const std::function<bool(IPartWriteStream&, size_t&)>& apploaderFunc) {
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0x2440);
|
||||
if (!ws)
|
||||
return EBuildResult::Failed;
|
||||
ws->write("", 1);
|
||||
return false;
|
||||
size_t xferSz = 0;
|
||||
if (!apploaderFunc(*ws, xferSz))
|
||||
return false;
|
||||
|
||||
PartitionBuilderGCN& pb = static_cast<PartitionBuilderGCN&>(*m_partitions[0]);
|
||||
return pb.buildFromDirectory(dirIn) ? EBuildResult::Success : EBuildResult::Failed;
|
||||
}
|
||||
size_t fstOff = ROUND_UP_32(xferSz);
|
||||
size_t fstSz = sizeof(FSTNode) * m_buildNodes.size();
|
||||
for (size_t i = 0; i < fstOff - xferSz; ++i)
|
||||
ws->write("\xff", 1);
|
||||
fstOff += 0x2440;
|
||||
ws->write(m_buildNodes.data(), fstSz);
|
||||
for (const std::string& str : m_buildNames)
|
||||
ws->write(str.data(), str.size() + 1);
|
||||
fstSz += m_buildNameOff;
|
||||
fstSz = ROUND_UP_32(fstSz);
|
||||
|
||||
uint64_t DiscBuilderGCN::CalculateTotalSizeRequired(const SystemChar* dirIn)
|
||||
{
|
||||
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, false);
|
||||
if (sz == -1)
|
||||
return -1;
|
||||
sz += 0x30000;
|
||||
if (sz > 0x57058000)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("disc capacity exceeded [%" PRIu64 " / %" PRIu64 "]"), sz, 0x57058000);
|
||||
return -1;
|
||||
if (fstOff + fstSz >= m_curUser) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("FST flows into user area (one or the other is too big)"));
|
||||
return false;
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
DiscBuilderGCN::DiscBuilderGCN(const SystemChar* outPath, FProgress progressCB)
|
||||
: DiscBuilderBase(outPath, 0x57058000, progressCB)
|
||||
{
|
||||
PartitionBuilderGCN* partBuilder = new PartitionBuilderGCN(*this);
|
||||
m_partitions.emplace_back(partBuilder);
|
||||
}
|
||||
|
||||
DiscMergerGCN::DiscMergerGCN(const SystemChar* outPath, DiscGCN& sourceDisc, FProgress progressCB)
|
||||
: m_sourceDisc(sourceDisc), m_builder(sourceDisc.makeMergeBuilder(outPath, progressCB))
|
||||
{}
|
||||
|
||||
EBuildResult DiscMergerGCN::mergeFromDirectory(const SystemChar* dirIn)
|
||||
{
|
||||
if (!m_builder.getFileIO().beginWriteStream())
|
||||
return EBuildResult::Failed;
|
||||
if (!CheckFreeSpace(m_builder.m_outPath.c_str(), 0x57058000))
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("not enough free disk space for %s"), m_builder.m_outPath.c_str());
|
||||
return EBuildResult::DiskFull;
|
||||
}
|
||||
m_builder.m_progressCB(m_builder.getProgressFactor(), _S("Preallocating image"), -1);
|
||||
++m_builder.m_progressIdx;
|
||||
auto ws = m_builder.m_fileIO->beginWriteStream(0x57058000 - 1);
|
||||
ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return EBuildResult::Failed;
|
||||
ws->write("", 1);
|
||||
return false;
|
||||
if (!headerFunc(*ws, m_dolOffset, fstOff, fstSz, m_curUser, 0x57058000 - m_curUser))
|
||||
return false;
|
||||
if (!bi2Func(*ws))
|
||||
return false;
|
||||
|
||||
PartitionBuilderGCN& pb = static_cast<PartitionBuilderGCN&>(*m_builder.m_partitions[0]);
|
||||
return pb.mergeFromDirectory(static_cast<PartitionGCN*>(m_sourceDisc.getDataPartition()), dirIn) ?
|
||||
EBuildResult::Success : EBuildResult::Failed;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t DiscMergerGCN::CalculateTotalSizeRequired(DiscGCN& sourceDisc, const SystemChar* dirIn)
|
||||
{
|
||||
uint64_t sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(
|
||||
sourceDisc.getDataPartition(), dirIn);
|
||||
if (sz == -1)
|
||||
return -1;
|
||||
sz += 0x30000;
|
||||
if (sz > 0x57058000)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("disc capacity exceeded [%" PRIu64 " / %" PRIu64 "]"), sz, 0x57058000);
|
||||
return -1;
|
||||
bool buildFromDirectory(SystemStringView dirIn) {
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return false;
|
||||
bool result = DiscBuilderBase::PartitionBuilderBase::buildFromDirectory(*ws, dirIn);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
SystemString dirStr(dirIn);
|
||||
|
||||
/* Check Apploader */
|
||||
SystemString apploaderIn = dirStr + _SYS_STR("/sys/apploader.img");
|
||||
Sstat apploaderStat;
|
||||
if (Stat(apploaderIn.c_str(), &apploaderStat)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {}")), apploaderIn);
|
||||
return false;
|
||||
}
|
||||
return sz;
|
||||
|
||||
/* Check Boot */
|
||||
SystemString bootIn = dirStr + _SYS_STR("/sys/boot.bin");
|
||||
Sstat bootStat;
|
||||
if (Stat(bootIn.c_str(), &bootStat)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {}")), bootIn);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Check BI2 */
|
||||
SystemString bi2In = dirStr + _SYS_STR("/sys/bi2.bin");
|
||||
Sstat bi2Stat;
|
||||
if (Stat(bi2In.c_str(), &bi2Stat)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to stat {}")), bi2In);
|
||||
return false;
|
||||
}
|
||||
|
||||
return _build(
|
||||
[&bootIn](IPartWriteStream& ws, uint32_t dolOff, uint32_t fstOff, uint32_t fstSz, uint32_t userOff,
|
||||
uint32_t userSz) -> bool {
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(bootIn.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
Header header;
|
||||
header.read(*rs);
|
||||
header.m_dolOff = dolOff;
|
||||
header.m_fstOff = fstOff;
|
||||
header.m_fstSz = fstSz;
|
||||
header.m_fstMaxSz = fstSz;
|
||||
header.m_userPosition = userOff;
|
||||
header.m_userSz = userSz;
|
||||
header.write(ws);
|
||||
return true;
|
||||
},
|
||||
[&bi2In](IPartWriteStream& ws) -> bool {
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(bi2In.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
BI2Header bi2;
|
||||
bi2.read(*rs);
|
||||
bi2.write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, &apploaderIn](IPartWriteStream& ws, size_t& xferSz) -> bool {
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(apploaderIn.c_str())->beginReadStream();
|
||||
if (!rs)
|
||||
return false;
|
||||
char buf[8192];
|
||||
while (true) {
|
||||
size_t rdSz = rs->read(buf, 8192);
|
||||
if (!rdSz)
|
||||
break;
|
||||
ws.write(buf, rdSz);
|
||||
xferSz += rdSz;
|
||||
if (0x2440 + xferSz >= m_curUser) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("apploader flows into user area (one or the other is too big)"));
|
||||
return false;
|
||||
}
|
||||
m_parent.m_progressCB(m_parent.getProgressFactor(), apploaderIn, xferSz);
|
||||
}
|
||||
++m_parent.m_progressIdx;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool mergeFromDirectory(const PartitionGCN* partIn, SystemStringView dirIn) {
|
||||
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0);
|
||||
if (!ws)
|
||||
return false;
|
||||
bool result = DiscBuilderBase::PartitionBuilderBase::mergeFromDirectory(*ws, partIn, dirIn);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
return _build(
|
||||
[partIn](IPartWriteStream& ws, uint32_t dolOff, uint32_t fstOff, uint32_t fstSz, uint32_t userOff,
|
||||
uint32_t userSz) -> bool {
|
||||
Header header = partIn->getHeader();
|
||||
header.m_dolOff = dolOff;
|
||||
header.m_fstOff = fstOff;
|
||||
header.m_fstSz = fstSz;
|
||||
header.m_fstMaxSz = fstSz;
|
||||
header.m_userPosition = userOff;
|
||||
header.m_userSz = userSz;
|
||||
header.write(ws);
|
||||
return true;
|
||||
},
|
||||
[partIn](IPartWriteStream& ws) -> bool {
|
||||
partIn->getBI2().write(ws);
|
||||
return true;
|
||||
},
|
||||
[this, partIn](IPartWriteStream& ws, size_t& xferSz) -> bool {
|
||||
std::unique_ptr<uint8_t[]> apploaderBuf = partIn->getApploaderBuf();
|
||||
size_t apploaderSz = partIn->getApploaderSize();
|
||||
SystemString apploaderName(_SYS_STR("<apploader>"));
|
||||
ws.write(apploaderBuf.get(), apploaderSz);
|
||||
xferSz += apploaderSz;
|
||||
if (0x2440 + xferSz >= m_curUser) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("apploader flows into user area (one or the other is too big)"));
|
||||
return false;
|
||||
}
|
||||
m_parent.m_progressCB(m_parent.getProgressFactor(), apploaderName, xferSz);
|
||||
++m_parent.m_progressIdx;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
EBuildResult DiscBuilderGCN::buildFromDirectory(SystemStringView dirIn) {
|
||||
if (!m_fileIO->beginWriteStream())
|
||||
return EBuildResult::Failed;
|
||||
if (!CheckFreeSpace(m_outPath.c_str(), 0x57058000)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("not enough free disk space for {}")), m_outPath);
|
||||
return EBuildResult::DiskFull;
|
||||
}
|
||||
m_progressCB(getProgressFactor(), _SYS_STR("Preallocating image"), -1);
|
||||
++m_progressIdx;
|
||||
{
|
||||
auto ws = m_fileIO->beginWriteStream(0);
|
||||
if (!ws)
|
||||
return EBuildResult::Failed;
|
||||
char zeroBytes[1024] = {};
|
||||
for (uint64_t i = 0; i < 0x57058000; i += 1024)
|
||||
ws->write(zeroBytes, 1024);
|
||||
}
|
||||
|
||||
PartitionBuilderGCN& pb = static_cast<PartitionBuilderGCN&>(*m_partitions[0]);
|
||||
return pb.buildFromDirectory(dirIn) ? EBuildResult::Success : EBuildResult::Failed;
|
||||
}
|
||||
|
||||
std::optional<uint64_t> DiscBuilderGCN::CalculateTotalSizeRequired(SystemStringView dirIn) {
|
||||
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeBuild(dirIn, PartitionKind::Data, false);
|
||||
if (!sz)
|
||||
return sz;
|
||||
*sz += 0x30000;
|
||||
if (sz > 0x57058000) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x57058000);
|
||||
return std::nullopt;
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
DiscBuilderGCN::DiscBuilderGCN(SystemStringView outPath, FProgress progressCB)
|
||||
: DiscBuilderBase(outPath, 0x57058000, progressCB) {
|
||||
m_partitions.emplace_back(std::make_unique<PartitionBuilderGCN>(*this));
|
||||
}
|
||||
|
||||
DiscMergerGCN::DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB)
|
||||
: m_sourceDisc(sourceDisc), m_builder(sourceDisc.makeMergeBuilder(outPath, progressCB)) {}
|
||||
|
||||
EBuildResult DiscMergerGCN::mergeFromDirectory(SystemStringView dirIn) {
|
||||
if (!m_builder.getFileIO().beginWriteStream())
|
||||
return EBuildResult::Failed;
|
||||
if (!CheckFreeSpace(m_builder.m_outPath.c_str(), 0x57058000)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("not enough free disk space for {}")), m_builder.m_outPath);
|
||||
return EBuildResult::DiskFull;
|
||||
}
|
||||
m_builder.m_progressCB(m_builder.getProgressFactor(), _SYS_STR("Preallocating image"), -1);
|
||||
++m_builder.m_progressIdx;
|
||||
{
|
||||
auto ws = m_builder.m_fileIO->beginWriteStream(0);
|
||||
if (!ws)
|
||||
return EBuildResult::Failed;
|
||||
char zeroBytes[1024] = {};
|
||||
for (uint64_t i = 0; i < 0x57058000; i += 1024)
|
||||
ws->write(zeroBytes, 1024);
|
||||
}
|
||||
|
||||
PartitionBuilderGCN& pb = static_cast<PartitionBuilderGCN&>(*m_builder.m_partitions[0]);
|
||||
return pb.mergeFromDirectory(static_cast<PartitionGCN*>(m_sourceDisc.getDataPartition()), dirIn)
|
||||
? EBuildResult::Success
|
||||
: EBuildResult::Failed;
|
||||
}
|
||||
|
||||
std::optional<uint64_t> DiscMergerGCN::CalculateTotalSizeRequired(DiscGCN& sourceDisc, SystemStringView dirIn) {
|
||||
std::optional<uint64_t> sz = DiscBuilderBase::PartitionBuilderBase::CalculateTotalSizeMerge(sourceDisc.getDataPartition(), dirIn);
|
||||
if (!sz)
|
||||
return std::nullopt;
|
||||
*sz += 0x30000;
|
||||
if (sz > 0x57058000) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("disc capacity exceeded [{} / {}]")), *sz, 0x57058000);
|
||||
return std::nullopt;
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,69 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include "nod/Util.hpp"
|
||||
#include "nod/IDiscIO.hpp"
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
namespace nod {
|
||||
|
||||
class DiscIOISO : public IDiscIO {
|
||||
std::unique_ptr<IFileIO> m_fio;
|
||||
|
||||
class DiscIOISO : public IDiscIO
|
||||
{
|
||||
std::unique_ptr<IFileIO> m_fio;
|
||||
public:
|
||||
DiscIOISO(const SystemString& fpin)
|
||||
: m_fio(NewFileIO(fpin)) {}
|
||||
DiscIOISO(SystemStringView fpin) : m_fio(NewFileIO(fpin)) {}
|
||||
|
||||
class ReadStream : public IReadStream
|
||||
{
|
||||
friend class DiscIOISO;
|
||||
std::unique_ptr<IFileIO::IReadStream> fp;
|
||||
ReadStream(std::unique_ptr<IFileIO::IReadStream>&& fpin, bool& err)
|
||||
: fp(std::move(fpin)) { if (!fp) err = true; }
|
||||
public:
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{return fp->read(buf, length);}
|
||||
uint64_t position() const
|
||||
{return fp->position();}
|
||||
void seek(int64_t offset, int whence)
|
||||
{fp->seek(offset, whence);}
|
||||
};
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
class ReadStream : public IReadStream {
|
||||
friend class DiscIOISO;
|
||||
std::unique_ptr<IFileIO::IReadStream> fp;
|
||||
ReadStream(std::unique_ptr<IFileIO::IReadStream>&& fpin, bool& err) : fp(std::move(fpin)) {
|
||||
if (!fp)
|
||||
err = true;
|
||||
}
|
||||
|
||||
class WriteStream : public IWriteStream
|
||||
{
|
||||
friend class DiscIOISO;
|
||||
std::unique_ptr<IFileIO::IWriteStream> fp;
|
||||
WriteStream(std::unique_ptr<IFileIO::IWriteStream>&& fpin, bool& err)
|
||||
: fp(std::move(fpin)) { if (!fp) err = true; }
|
||||
public:
|
||||
uint64_t write(const void* buf, uint64_t length)
|
||||
{
|
||||
return fp->write(buf, length);
|
||||
}
|
||||
};
|
||||
public:
|
||||
uint64_t read(void* buf, uint64_t length) override { return fp->read(buf, length); }
|
||||
uint64_t position() const override { return fp->position(); }
|
||||
void seek(int64_t offset, int whence) override { fp->seek(offset, whence); }
|
||||
};
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
class WriteStream : public IWriteStream {
|
||||
friend class DiscIOISO;
|
||||
std::unique_ptr<IFileIO::IWriteStream> fp;
|
||||
WriteStream(std::unique_ptr<IFileIO::IWriteStream>&& fpin, bool& err) : fp(std::move(fpin)) {
|
||||
if (!fp)
|
||||
err = true;
|
||||
}
|
||||
|
||||
public:
|
||||
uint64_t write(const void* buf, uint64_t length) override { return fp->write(buf, length); }
|
||||
};
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IDiscIO> NewDiscIOISO(const SystemChar* path)
|
||||
{
|
||||
return std::unique_ptr<IDiscIO>(new DiscIOISO(path));
|
||||
}
|
||||
|
||||
}
|
||||
std::unique_ptr<IDiscIO> NewDiscIOISO(SystemStringView path) { return std::make_unique<DiscIOISO>(path); }
|
||||
|
||||
} // namespace nod
|
||||
|
||||
271
lib/DiscIONFS.cpp
Normal file
271
lib/DiscIONFS.cpp
Normal file
@@ -0,0 +1,271 @@
|
||||
#include "nod/IDiscIO.hpp"
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
#include "nod/aes.hpp"
|
||||
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
namespace nod {
|
||||
|
||||
/*
|
||||
* NFS is the image format used to distribute Wii VC games for the Wii U.
|
||||
* It is an LBA format similar to WBFS but adds its own encryption layer.
|
||||
* It logically stores a standard Wii disc image with partitions.
|
||||
*/
|
||||
|
||||
class DiscIONFS : public IDiscIO {
|
||||
std::vector<std::unique_ptr<IFileIO>> files;
|
||||
|
||||
struct NFSHead {
|
||||
uint32_t magic; // EGGS
|
||||
uint32_t version;
|
||||
uint32_t unknown[2]; // Signature, UUID?
|
||||
uint32_t lbaRangeCount;
|
||||
struct {
|
||||
uint32_t startBlock;
|
||||
uint32_t numBlocks;
|
||||
} lbaRanges[61];
|
||||
uint32_t endMagic; // SGGE
|
||||
} nfsHead;
|
||||
|
||||
uint8_t key[16];
|
||||
|
||||
uint32_t calculateNumFiles() const {
|
||||
uint32_t totalBlockCount = 0;
|
||||
for (uint32_t i = 0; i < nfsHead.lbaRangeCount; ++i)
|
||||
totalBlockCount += nfsHead.lbaRanges[i].numBlocks;
|
||||
return (uint64_t(totalBlockCount) * uint64_t(0x8000) +
|
||||
(uint64_t(0x200) + uint64_t(0xF9FFFFF))) / uint64_t(0xFA00000);
|
||||
}
|
||||
|
||||
struct FBO {
|
||||
uint32_t file, block, lblock, offset;
|
||||
};
|
||||
|
||||
FBO logicalToFBO(uint64_t offset) const {
|
||||
auto blockAndRemBytes = nod::div(offset, uint64_t(0x8000)); /* 32768 bytes per block */
|
||||
uint32_t block = UINT32_MAX;
|
||||
for (uint32_t i = 0, physicalBlock = 0; i < nfsHead.lbaRangeCount; ++i) {
|
||||
const auto& range = nfsHead.lbaRanges[i];
|
||||
if (blockAndRemBytes.quot >= range.startBlock && blockAndRemBytes.quot - range.startBlock < range.numBlocks) {
|
||||
block = physicalBlock + (blockAndRemBytes.quot - range.startBlock);
|
||||
break;
|
||||
}
|
||||
physicalBlock += range.numBlocks;
|
||||
}
|
||||
/* This offset has no physical mapping, read zeroes */
|
||||
if (block == UINT32_MAX)
|
||||
return {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX};
|
||||
auto fileAndRemBlocks = nod::div(block, uint32_t(8000)); /* 8000 blocks per file */
|
||||
return {uint32_t(fileAndRemBlocks.quot), uint32_t(fileAndRemBlocks.rem),
|
||||
uint32_t(blockAndRemBytes.quot), uint32_t(blockAndRemBytes.rem)};
|
||||
}
|
||||
|
||||
public:
|
||||
DiscIONFS(SystemStringView fpin, bool& err) {
|
||||
/* Validate file path format */
|
||||
using SignedSize = std::make_signed<SystemString::size_type>::type;
|
||||
const auto dotPos = SignedSize(fpin.rfind(_SYS_STR('.')));
|
||||
const auto slashPos = SignedSize(fpin.find_last_of(_SYS_STR("/\\")));
|
||||
if (fpin.size() <= 4 || dotPos == -1 || dotPos <= slashPos ||
|
||||
fpin.compare(slashPos + 1, 4, _SYS_STR("hif_")) ||
|
||||
fpin.compare(dotPos, fpin.size() - dotPos, _SYS_STR(".nfs"))) {
|
||||
LogModule.report(logvisor::Error,
|
||||
FMT_STRING(_SYS_STR("'{}' must begin with 'hif_' and end with '.nfs' to be accepted as an NFS image")), fpin);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Load key file */
|
||||
const SystemString dir(fpin.begin(), fpin.begin() + slashPos + 1);
|
||||
auto keyFile = NewFileIO(dir + _SYS_STR("../code/htk.bin"))->beginReadStream();
|
||||
if (!keyFile)
|
||||
keyFile = NewFileIO(dir + _SYS_STR("htk.bin"))->beginReadStream();
|
||||
if (!keyFile) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to open '{}../code/htk.bin' or '{}htk.bin'")), dir, dir);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
if (keyFile->read(key, 16) != 16) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to read from '{}../code/htk.bin' or '{}htk.bin'")), dir, dir);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Load header from first file */
|
||||
const SystemString firstPath = fmt::format(FMT_STRING(_SYS_STR("{}hif_{:06}.nfs")), dir, 0);
|
||||
files.push_back(NewFileIO(firstPath));
|
||||
auto rs = files.back()->beginReadStream();
|
||||
if (!rs) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("'{}' does not exist")), firstPath);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
if (rs->read(&nfsHead, 0x200) != 0x200) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to read header from '{}'")), firstPath);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
if (std::memcmp(&nfsHead.magic, "EGGS", 4)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Invalid magic in '{}'")), firstPath);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
nfsHead.lbaRangeCount = SBig(nfsHead.lbaRangeCount);
|
||||
for (uint32_t i = 0; i < nfsHead.lbaRangeCount; ++i) {
|
||||
auto& range = nfsHead.lbaRanges[i];
|
||||
range.startBlock = SBig(range.startBlock);
|
||||
range.numBlocks = SBig(range.numBlocks);
|
||||
}
|
||||
|
||||
/* Ensure remaining files exist */
|
||||
const uint32_t numFiles = calculateNumFiles();
|
||||
files.reserve(numFiles);
|
||||
for (uint32_t i = 1; i < numFiles; ++i) {
|
||||
SystemString path = fmt::format(FMT_STRING(_SYS_STR("{}hif_{:06}.nfs")), dir, i);
|
||||
files.push_back(NewFileIO(path));
|
||||
if (!files.back()->exists()) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("'{}' does not exist")), path);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReadStream : public IReadStream {
|
||||
friend class DiscIONFS;
|
||||
const DiscIONFS& m_parent;
|
||||
std::unique_ptr<IReadStream> m_rs;
|
||||
std::unique_ptr<IAES> m_aes;
|
||||
|
||||
/* Physical address - all UINT32_MAX indicates logical zero block */
|
||||
DiscIONFS::FBO m_physAddr;
|
||||
|
||||
/* Logical address */
|
||||
uint64_t m_offset;
|
||||
|
||||
/* Active file stream and its offset as set in the system.
|
||||
* Block is typically one ahead of the presently decrypted block. */
|
||||
uint32_t m_curFile = UINT32_MAX;
|
||||
uint32_t m_curBlock = UINT32_MAX;
|
||||
|
||||
ReadStream(const DiscIONFS& parent, uint64_t offset, bool& err)
|
||||
: m_parent(parent), m_aes(NewAES()),
|
||||
m_physAddr({UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX}), m_offset(offset) {
|
||||
m_aes->setKey(m_parent.key);
|
||||
setLogicalAddr(offset);
|
||||
}
|
||||
|
||||
uint8_t m_encBuf[0x8000] = {};
|
||||
uint8_t m_decBuf[0x8000] = {};
|
||||
|
||||
void setCurFile(uint32_t curFile) {
|
||||
if (curFile >= m_parent.files.size()) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("Out of bounds NFS file access"));
|
||||
return;
|
||||
}
|
||||
m_curFile = curFile;
|
||||
m_curBlock = UINT32_MAX;
|
||||
m_rs = m_parent.files[m_curFile]->beginReadStream();
|
||||
}
|
||||
|
||||
void setCurBlock(uint32_t curBlock) {
|
||||
m_curBlock = curBlock;
|
||||
m_rs->seek(m_curBlock * 0x8000 + 0x200);
|
||||
}
|
||||
|
||||
void setPhysicalAddr(DiscIONFS::FBO physAddr) {
|
||||
/* If we're just changing the offset, nothing else needs to be done */
|
||||
if (m_physAddr.file == physAddr.file && m_physAddr.block == physAddr.block) {
|
||||
m_physAddr.offset = physAddr.offset;
|
||||
return;
|
||||
}
|
||||
m_physAddr = physAddr;
|
||||
|
||||
/* Set logical zero block */
|
||||
if (m_physAddr.file == UINT32_MAX) {
|
||||
memset(m_decBuf, 0, 0x8000);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Make necessary file and block current with system */
|
||||
if (m_physAddr.file != m_curFile)
|
||||
setCurFile(m_physAddr.file);
|
||||
if (m_physAddr.block != m_curBlock)
|
||||
setCurBlock(m_physAddr.block);
|
||||
|
||||
/* Read block, handling 0x200 overlap case */
|
||||
if (m_physAddr.block == 7999) {
|
||||
m_rs->read(m_encBuf, 0x7E00);
|
||||
setCurFile(m_curFile + 1);
|
||||
m_rs->read(m_encBuf + 0x7E00, 0x200);
|
||||
m_curBlock = 0;
|
||||
} else {
|
||||
m_rs->read(m_encBuf, 0x8000);
|
||||
++m_curBlock;
|
||||
}
|
||||
|
||||
/* Decrypt */
|
||||
const uint32_t ivBuf[] = {0, 0, 0, SBig(m_physAddr.lblock)};
|
||||
m_aes->decrypt((const uint8_t*)ivBuf, m_encBuf, m_decBuf, 0x8000);
|
||||
}
|
||||
|
||||
void setLogicalAddr(uint64_t addr) { setPhysicalAddr(m_parent.logicalToFBO(m_offset)); }
|
||||
|
||||
public:
|
||||
uint64_t read(void* buf, uint64_t length) override {
|
||||
uint64_t rem = length;
|
||||
uint8_t* dst = (uint8_t*)buf;
|
||||
|
||||
/* Perform reads on block boundaries */
|
||||
while (rem) {
|
||||
uint64_t readSize = rem;
|
||||
uint32_t blockOffset = (m_physAddr.offset == UINT32_MAX) ? 0 : m_physAddr.offset;
|
||||
if (readSize + blockOffset > 0x8000)
|
||||
readSize = 0x8000 - blockOffset;
|
||||
|
||||
memmove(dst, m_decBuf + blockOffset, readSize);
|
||||
dst += readSize;
|
||||
rem -= readSize;
|
||||
m_offset += readSize;
|
||||
setLogicalAddr(m_offset);
|
||||
}
|
||||
|
||||
return dst - (uint8_t*)buf;
|
||||
}
|
||||
uint64_t position() const override { return m_offset; }
|
||||
void seek(int64_t offset, int whence) override {
|
||||
if (whence == SEEK_SET)
|
||||
m_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
m_offset += offset;
|
||||
else
|
||||
return;
|
||||
setLogicalAddr(m_offset);
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, offset, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { return {}; }
|
||||
|
||||
bool hasWiiCrypto() const override { return false; }
|
||||
};
|
||||
|
||||
std::unique_ptr<IDiscIO> NewDiscIONFS(SystemStringView path) {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<DiscIONFS>(path, err);
|
||||
if (err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,305 +1,280 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "nod/Util.hpp"
|
||||
#include <cinttypes>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
#include "nod/IDiscIO.hpp"
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
#define ALIGN_LBA(x) (((x)+p->hd_sec_sz-1)&(~(p->hd_sec_sz-1)))
|
||||
namespace nod {
|
||||
|
||||
static uint8_t size_to_shift(uint32_t size)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
while (size)
|
||||
{
|
||||
ret++;
|
||||
size>>=1;
|
||||
}
|
||||
return ret-1;
|
||||
#define ALIGN_LBA(x) (((x) + p->hd_sec_sz - 1) & (~(p->hd_sec_sz - 1)))
|
||||
|
||||
static uint8_t size_to_shift(uint32_t size) {
|
||||
uint8_t ret = 0;
|
||||
while (size) {
|
||||
ret++;
|
||||
size >>= 1;
|
||||
}
|
||||
return ret - 1;
|
||||
}
|
||||
|
||||
class DiscIOWBFS : public IDiscIO
|
||||
{
|
||||
SystemString filepath;
|
||||
class DiscIOWBFS : public IDiscIO {
|
||||
std::unique_ptr<IFileIO> m_fio;
|
||||
|
||||
struct WBFSHead
|
||||
{
|
||||
uint32_t magic;
|
||||
// parameters copied in the partition for easy dumping, and bug reports
|
||||
uint32_t n_hd_sec; // total number of hd_sec in this partition
|
||||
uint8_t hd_sec_sz_s; // sector size in this partition
|
||||
uint8_t wbfs_sec_sz_s; // size of a wbfs sec
|
||||
uint8_t padding3[2];
|
||||
uint8_t disc_table[0]; // size depends on hd sector size
|
||||
};
|
||||
std::unique_ptr<uint8_t[]> wbfsHead;
|
||||
struct WBFSHead {
|
||||
uint32_t magic;
|
||||
// parameters copied in the partition for easy dumping, and bug reports
|
||||
uint32_t n_hd_sec; // total number of hd_sec in this partition
|
||||
uint8_t hd_sec_sz_s; // sector size in this partition
|
||||
uint8_t wbfs_sec_sz_s; // size of a wbfs sec
|
||||
uint8_t padding3[2];
|
||||
uint8_t disc_table[0]; // size depends on hd sector size
|
||||
};
|
||||
std::unique_ptr<uint8_t[]> wbfsHead;
|
||||
|
||||
struct WBFSDiscInfo
|
||||
{
|
||||
uint8_t disc_header_copy[0x100];
|
||||
uint16_t wlba_table[0];
|
||||
};
|
||||
std::unique_ptr<uint8_t[]> wbfsDiscInfo;
|
||||
struct WBFSDiscInfo {
|
||||
uint8_t disc_header_copy[0x100];
|
||||
uint16_t wlba_table[0];
|
||||
};
|
||||
std::unique_ptr<uint8_t[]> wbfsDiscInfo;
|
||||
|
||||
struct WBFS
|
||||
{
|
||||
/* hdsectors, the size of the sector provided by the hosting hard drive */
|
||||
uint32_t hd_sec_sz;
|
||||
uint8_t hd_sec_sz_s; // the power of two of the last number
|
||||
uint32_t n_hd_sec; // the number of hd sector in the wbfs partition
|
||||
struct WBFS {
|
||||
/* hdsectors, the size of the sector provided by the hosting hard drive */
|
||||
uint32_t hd_sec_sz;
|
||||
uint8_t hd_sec_sz_s; // the power of two of the last number
|
||||
uint32_t n_hd_sec; // the number of hd sector in the wbfs partition
|
||||
|
||||
/* standard wii sector (0x8000 bytes) */
|
||||
uint32_t wii_sec_sz;
|
||||
uint8_t wii_sec_sz_s;
|
||||
uint32_t n_wii_sec;
|
||||
uint32_t n_wii_sec_per_disc;
|
||||
/* standard wii sector (0x8000 bytes) */
|
||||
uint32_t wii_sec_sz;
|
||||
uint8_t wii_sec_sz_s;
|
||||
uint32_t n_wii_sec;
|
||||
uint32_t n_wii_sec_per_disc;
|
||||
|
||||
/* The size of a wbfs sector */
|
||||
uint32_t wbfs_sec_sz;
|
||||
uint32_t wbfs_sec_sz_s;
|
||||
uint16_t n_wbfs_sec; // this must fit in 16 bit!
|
||||
uint16_t n_wbfs_sec_per_disc; // size of the lookup table
|
||||
/* The size of a wbfs sector */
|
||||
uint32_t wbfs_sec_sz;
|
||||
uint32_t wbfs_sec_sz_s;
|
||||
uint16_t n_wbfs_sec; // this must fit in 16 bit!
|
||||
uint16_t n_wbfs_sec_per_disc; // size of the lookup table
|
||||
|
||||
uint32_t part_lba;
|
||||
uint32_t part_lba;
|
||||
|
||||
uint16_t max_disc;
|
||||
uint32_t freeblks_lba;
|
||||
uint32_t *freeblks;
|
||||
uint16_t disc_info_sz;
|
||||
uint16_t max_disc;
|
||||
uint32_t freeblks_lba;
|
||||
uint32_t* freeblks;
|
||||
uint16_t disc_info_sz;
|
||||
|
||||
uint32_t n_disc_open;
|
||||
uint32_t n_disc_open;
|
||||
|
||||
} wbfs;
|
||||
} wbfs;
|
||||
|
||||
static int _wbfsReadSector(IFileIO::IReadStream& rs, uint32_t lba, uint32_t count, void* buf)
|
||||
{
|
||||
uint64_t off = lba;
|
||||
off*=512ULL;
|
||||
rs.seek(off, SEEK_SET);
|
||||
if (rs.read(buf, count*512ULL) != count*512ULL)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "error reading disc");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
static int _wbfsReadSector(IFileIO::IReadStream& rs, uint32_t lba, uint32_t count, void* buf) {
|
||||
uint64_t off = lba;
|
||||
off *= 512ULL;
|
||||
rs.seek(off, SEEK_SET);
|
||||
if (rs.read(buf, count * 512ULL) != count * 512ULL) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("error reading disc"));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
DiscIOWBFS(const SystemString& fpin)
|
||||
: filepath(fpin)
|
||||
{
|
||||
/* Temporary file handle to read LBA table */
|
||||
std::unique_ptr<IFileIO> fio = NewFileIO(filepath);
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = fio->beginReadStream();
|
||||
if (!rs)
|
||||
return;
|
||||
DiscIOWBFS(SystemStringView fpin) : m_fio(NewFileIO(fpin)) {
|
||||
/* Temporary file handle to read LBA table */
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = m_fio->beginReadStream();
|
||||
if (!rs)
|
||||
return;
|
||||
|
||||
WBFS* p = &wbfs;
|
||||
WBFSHead tmpHead;
|
||||
if (rs->read(&tmpHead, sizeof(tmpHead)) != sizeof(tmpHead)) {
|
||||
LogModule.report(logvisor::Error, "unable to read WBFS head");
|
||||
return;
|
||||
}
|
||||
unsigned hd_sector_size = 1 << tmpHead.hd_sec_sz_s;
|
||||
unsigned num_hd_sector = SBig(tmpHead.n_hd_sec);
|
||||
WBFS* p = &wbfs;
|
||||
WBFSHead tmpHead;
|
||||
if (rs->read(&tmpHead, sizeof(tmpHead)) != sizeof(tmpHead)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read WBFS head"));
|
||||
return;
|
||||
}
|
||||
unsigned hd_sector_size = 1 << tmpHead.hd_sec_sz_s;
|
||||
unsigned num_hd_sector = SBig(tmpHead.n_hd_sec);
|
||||
|
||||
wbfsHead.reset(new uint8_t[hd_sector_size]);
|
||||
WBFSHead* head = (WBFSHead*)wbfsHead.get();
|
||||
rs->seek(0, SEEK_SET);
|
||||
if (rs->read(head, hd_sector_size) != hd_sector_size) {
|
||||
LogModule.report(logvisor::Error, "unable to read WBFS head");
|
||||
return;
|
||||
}
|
||||
|
||||
//constants, but put here for consistancy
|
||||
p->wii_sec_sz = 0x8000;
|
||||
p->wii_sec_sz_s = size_to_shift(0x8000);
|
||||
p->n_wii_sec = (num_hd_sector/0x8000)*hd_sector_size;
|
||||
p->n_wii_sec_per_disc = 143432*2;//support for double layers discs..
|
||||
p->part_lba = 0;
|
||||
if (_wbfsReadSector(*rs, p->part_lba, 1, head))
|
||||
return;
|
||||
if (hd_sector_size && head->hd_sec_sz_s != size_to_shift(hd_sector_size)) {
|
||||
LogModule.report(logvisor::Error, "hd sector size doesn't match");
|
||||
return;
|
||||
}
|
||||
if (num_hd_sector && head->n_hd_sec != SBig(num_hd_sector)) {
|
||||
LogModule.report(logvisor::Error, "hd num sector doesn't match");
|
||||
return;
|
||||
}
|
||||
p->hd_sec_sz = 1<<head->hd_sec_sz_s;
|
||||
p->hd_sec_sz_s = head->hd_sec_sz_s;
|
||||
p->n_hd_sec = SBig(head->n_hd_sec);
|
||||
|
||||
p->n_wii_sec = (p->n_hd_sec/p->wii_sec_sz)*(p->hd_sec_sz);
|
||||
|
||||
p->wbfs_sec_sz_s = head->wbfs_sec_sz_s;
|
||||
p->wbfs_sec_sz = 1<<p->wbfs_sec_sz_s;
|
||||
p->n_wbfs_sec = p->n_wii_sec >> (p->wbfs_sec_sz_s - p->wii_sec_sz_s);
|
||||
p->n_wbfs_sec_per_disc = p->n_wii_sec_per_disc >> (p->wbfs_sec_sz_s - p->wii_sec_sz_s);
|
||||
p->disc_info_sz = ALIGN_LBA(uint16_t(sizeof(WBFSDiscInfo)) + p->n_wbfs_sec_per_disc*2);
|
||||
|
||||
p->freeblks_lba = (p->wbfs_sec_sz - p->n_wbfs_sec/8)>>p->hd_sec_sz_s;
|
||||
|
||||
p->freeblks = 0; // will alloc and read only if needed
|
||||
p->max_disc = (p->freeblks_lba-1)/(p->disc_info_sz>>p->hd_sec_sz_s);
|
||||
if(p->max_disc > p->hd_sec_sz - sizeof(WBFSHead))
|
||||
p->max_disc = p->hd_sec_sz - sizeof(WBFSHead);
|
||||
|
||||
p->n_disc_open = 0;
|
||||
|
||||
int disc_info_sz_lba = p->disc_info_sz>>p->hd_sec_sz_s;
|
||||
if (head->disc_table[0])
|
||||
{
|
||||
wbfsDiscInfo.reset(new uint8_t[p->disc_info_sz]);
|
||||
if (!wbfsDiscInfo) {
|
||||
LogModule.report(logvisor::Error, "allocating memory");
|
||||
return;
|
||||
}
|
||||
if (_wbfsReadSector(*rs, p->part_lba+1, disc_info_sz_lba, wbfsDiscInfo.get()))
|
||||
return;
|
||||
p->n_disc_open++;
|
||||
//for(i=0;i<p->n_wbfs_sec_per_disc;i++)
|
||||
// printf("%d,",wbfs_ntohs(d->header->wlba_table[i]));
|
||||
}
|
||||
wbfsHead.reset(new uint8_t[hd_sector_size]);
|
||||
WBFSHead* head = (WBFSHead*)wbfsHead.get();
|
||||
rs->seek(0, SEEK_SET);
|
||||
if (rs->read(head, hd_sector_size) != hd_sector_size) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read WBFS head"));
|
||||
return;
|
||||
}
|
||||
|
||||
class ReadStream : public IReadStream
|
||||
{
|
||||
friend class DiscIOWBFS;
|
||||
const DiscIOWBFS& m_parent;
|
||||
std::unique_ptr<IFileIO::IReadStream> fp;
|
||||
uint64_t m_offset;
|
||||
std::unique_ptr<uint8_t[]> m_tmpBuffer;
|
||||
// constants, but put here for consistancy
|
||||
p->wii_sec_sz = 0x8000;
|
||||
p->wii_sec_sz_s = size_to_shift(0x8000);
|
||||
p->n_wii_sec = (num_hd_sector / 0x8000) * hd_sector_size;
|
||||
p->n_wii_sec_per_disc = 143432 * 2; // support for double layers discs..
|
||||
p->part_lba = 0;
|
||||
if (_wbfsReadSector(*rs, p->part_lba, 1, head))
|
||||
return;
|
||||
if (hd_sector_size && head->hd_sec_sz_s != size_to_shift(hd_sector_size)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("hd sector size doesn't match"));
|
||||
return;
|
||||
}
|
||||
if (num_hd_sector && head->n_hd_sec != SBig(num_hd_sector)) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("hd num sector doesn't match"));
|
||||
return;
|
||||
}
|
||||
p->hd_sec_sz = 1 << head->hd_sec_sz_s;
|
||||
p->hd_sec_sz_s = head->hd_sec_sz_s;
|
||||
p->n_hd_sec = SBig(head->n_hd_sec);
|
||||
|
||||
ReadStream(const DiscIOWBFS& parent, std::unique_ptr<IFileIO::IReadStream>&& fpin, uint64_t offset, bool& err)
|
||||
: m_parent(parent),
|
||||
fp(std::move(fpin)),
|
||||
m_offset(offset),
|
||||
m_tmpBuffer(new uint8_t[parent.wbfs.hd_sec_sz]) { if (!fp) err = true; }
|
||||
p->n_wii_sec = (p->n_hd_sec / p->wii_sec_sz) * (p->hd_sec_sz);
|
||||
|
||||
int wbfsReadSector(uint32_t lba, uint32_t count, void* buf)
|
||||
{return DiscIOWBFS::_wbfsReadSector(*fp, lba, count, buf);}
|
||||
p->wbfs_sec_sz_s = head->wbfs_sec_sz_s;
|
||||
p->wbfs_sec_sz = 1 << p->wbfs_sec_sz_s;
|
||||
p->n_wbfs_sec = p->n_wii_sec >> (p->wbfs_sec_sz_s - p->wii_sec_sz_s);
|
||||
p->n_wbfs_sec_per_disc = p->n_wii_sec_per_disc >> (p->wbfs_sec_sz_s - p->wii_sec_sz_s);
|
||||
p->disc_info_sz = ALIGN_LBA(uint16_t(sizeof(WBFSDiscInfo)) + p->n_wbfs_sec_per_disc * 2);
|
||||
|
||||
int wbfsDiscRead(uint32_t offset, uint8_t *data, uint64_t len)
|
||||
{
|
||||
const WBFS* p = &m_parent.wbfs;
|
||||
const WBFSDiscInfo* d = (WBFSDiscInfo*)m_parent.wbfsDiscInfo.get();
|
||||
uint16_t wlba = offset>>(p->wbfs_sec_sz_s-2);
|
||||
uint32_t iwlba_shift = p->wbfs_sec_sz_s - p->hd_sec_sz_s;
|
||||
uint32_t lba_mask = (p->wbfs_sec_sz-1)>>(p->hd_sec_sz_s);
|
||||
uint64_t lba = (offset>>(p->hd_sec_sz_s-2))&lba_mask;
|
||||
uint64_t off = offset&((p->hd_sec_sz>>2)-1);
|
||||
uint16_t iwlba = SBig(d->wlba_table[wlba]);
|
||||
uint64_t len_copied;
|
||||
int err = 0;
|
||||
uint8_t *ptr = data;
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
if (off)
|
||||
{
|
||||
off*=4;
|
||||
err = wbfsReadSector(p->part_lba + (iwlba<<iwlba_shift) + lba, 1, m_tmpBuffer.get());
|
||||
if (err)
|
||||
return err;
|
||||
len_copied = p->hd_sec_sz - off;
|
||||
if (len < len_copied)
|
||||
len_copied = len;
|
||||
memcpy(ptr, m_tmpBuffer.get() + off, len_copied);
|
||||
len -= len_copied;
|
||||
ptr += len_copied;
|
||||
lba++;
|
||||
if (lba>lba_mask && len)
|
||||
{
|
||||
lba=0;
|
||||
iwlba = SBig(d->wlba_table[++wlba]);
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
while (len>=p->hd_sec_sz)
|
||||
{
|
||||
uint32_t nlb = len>>(p->hd_sec_sz_s);
|
||||
p->freeblks_lba = (p->wbfs_sec_sz - p->n_wbfs_sec / 8) >> p->hd_sec_sz_s;
|
||||
|
||||
if (lba + nlb > p->wbfs_sec_sz) // dont cross wbfs sectors..
|
||||
nlb = p->wbfs_sec_sz-lba;
|
||||
err = wbfsReadSector(p->part_lba + (iwlba<<iwlba_shift) + lba, nlb, ptr);
|
||||
if (err)
|
||||
return err;
|
||||
len -= nlb<<p->hd_sec_sz_s;
|
||||
ptr += nlb<<p->hd_sec_sz_s;
|
||||
lba += nlb;
|
||||
if (lba>lba_mask && len)
|
||||
{
|
||||
lba = 0;
|
||||
iwlba = SBig(d->wlba_table[++wlba]);
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (len)
|
||||
{
|
||||
err = wbfsReadSector(p->part_lba + (iwlba<<iwlba_shift) + lba, 1, m_tmpBuffer.get());
|
||||
if (err)
|
||||
return err;
|
||||
memcpy(ptr, m_tmpBuffer.get(), len);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
p->freeblks = 0; // will alloc and read only if needed
|
||||
p->max_disc = (p->freeblks_lba - 1) / (p->disc_info_sz >> p->hd_sec_sz_s);
|
||||
if (p->max_disc > p->hd_sec_sz - sizeof(WBFSHead))
|
||||
p->max_disc = p->hd_sec_sz - sizeof(WBFSHead);
|
||||
|
||||
public:
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{
|
||||
uint8_t extra[4];
|
||||
uint64_t rem_offset = m_offset % 4;
|
||||
if (rem_offset)
|
||||
{
|
||||
uint64_t rem_rem = 4 - rem_offset;
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4), extra, 4))
|
||||
return 0;
|
||||
memcpy(buf, extra + rem_offset, rem_rem);
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4 + 1), (uint8_t*)buf + rem_rem, length - rem_rem))
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4), (uint8_t*)buf, length))
|
||||
return 0;
|
||||
}
|
||||
m_offset += length;
|
||||
return length;
|
||||
}
|
||||
uint64_t position() const {return m_offset;}
|
||||
void seek(int64_t offset, int whence)
|
||||
{
|
||||
if (whence == SEEK_SET)
|
||||
m_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
m_offset += offset;
|
||||
}
|
||||
};
|
||||
p->n_disc_open = 0;
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, NewFileIO(filepath)->beginReadStream(), offset, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
int disc_info_sz_lba = p->disc_info_sz >> p->hd_sec_sz_s;
|
||||
if (head->disc_table[0]) {
|
||||
wbfsDiscInfo.reset(new uint8_t[p->disc_info_sz]);
|
||||
if (!wbfsDiscInfo) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("allocating memory"));
|
||||
return;
|
||||
}
|
||||
if (_wbfsReadSector(*rs, p->part_lba + 1, disc_info_sz_lba, wbfsDiscInfo.get()))
|
||||
return;
|
||||
p->n_disc_open++;
|
||||
// for(i=0;i<p->n_wbfs_sec_per_disc;i++)
|
||||
// printf("%d,",wbfs_ntohs(d->header->wlba_table[i]));
|
||||
}
|
||||
}
|
||||
|
||||
class ReadStream : public IReadStream {
|
||||
friend class DiscIOWBFS;
|
||||
const DiscIOWBFS& m_parent;
|
||||
std::unique_ptr<IFileIO::IReadStream> fp;
|
||||
uint64_t m_offset;
|
||||
std::unique_ptr<uint8_t[]> m_tmpBuffer;
|
||||
|
||||
ReadStream(const DiscIOWBFS& parent, std::unique_ptr<IFileIO::IReadStream>&& fpin, uint64_t offset, bool& err)
|
||||
: m_parent(parent), fp(std::move(fpin)), m_offset(offset), m_tmpBuffer(new uint8_t[parent.wbfs.hd_sec_sz]) {
|
||||
if (!fp)
|
||||
err = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
return std::unique_ptr<IWriteStream>();
|
||||
int wbfsReadSector(uint32_t lba, uint32_t count, void* buf) {
|
||||
return DiscIOWBFS::_wbfsReadSector(*fp, lba, count, buf);
|
||||
}
|
||||
|
||||
int wbfsDiscRead(uint32_t offset, uint8_t* data, uint64_t len) {
|
||||
const WBFS* p = &m_parent.wbfs;
|
||||
const WBFSDiscInfo* d = (WBFSDiscInfo*)m_parent.wbfsDiscInfo.get();
|
||||
uint16_t wlba = offset >> (p->wbfs_sec_sz_s - 2);
|
||||
uint32_t iwlba_shift = p->wbfs_sec_sz_s - p->hd_sec_sz_s;
|
||||
uint32_t lba_mask = (p->wbfs_sec_sz - 1) >> (p->hd_sec_sz_s);
|
||||
uint64_t lba = (offset >> (p->hd_sec_sz_s - 2)) & lba_mask;
|
||||
uint64_t off = offset & ((p->hd_sec_sz >> 2) - 1);
|
||||
uint16_t iwlba = SBig(d->wlba_table[wlba]);
|
||||
uint64_t len_copied;
|
||||
int err = 0;
|
||||
uint8_t* ptr = data;
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
if (off) {
|
||||
off *= 4;
|
||||
err = wbfsReadSector(p->part_lba + (iwlba << iwlba_shift) + lba, 1, m_tmpBuffer.get());
|
||||
if (err)
|
||||
return err;
|
||||
len_copied = p->hd_sec_sz - off;
|
||||
if (len < len_copied)
|
||||
len_copied = len;
|
||||
memcpy(ptr, m_tmpBuffer.get() + off, len_copied);
|
||||
len -= len_copied;
|
||||
ptr += len_copied;
|
||||
lba++;
|
||||
if (lba > lba_mask && len) {
|
||||
lba = 0;
|
||||
iwlba = SBig(d->wlba_table[++wlba]);
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
while (len >= p->hd_sec_sz) {
|
||||
uint32_t nlb = len >> (p->hd_sec_sz_s);
|
||||
|
||||
if (lba + nlb > p->wbfs_sec_sz) // dont cross wbfs sectors..
|
||||
nlb = p->wbfs_sec_sz - lba;
|
||||
err = wbfsReadSector(p->part_lba + (iwlba << iwlba_shift) + lba, nlb, ptr);
|
||||
if (err)
|
||||
return err;
|
||||
len -= nlb << p->hd_sec_sz_s;
|
||||
ptr += nlb << p->hd_sec_sz_s;
|
||||
lba += nlb;
|
||||
if (lba > lba_mask && len) {
|
||||
lba = 0;
|
||||
iwlba = SBig(d->wlba_table[++wlba]);
|
||||
if (!iwlba)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (len) {
|
||||
err = wbfsReadSector(p->part_lba + (iwlba << iwlba_shift) + lba, 1, m_tmpBuffer.get());
|
||||
if (err)
|
||||
return err;
|
||||
memcpy(ptr, m_tmpBuffer.get(), len);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
uint64_t read(void* buf, uint64_t length) override {
|
||||
uint8_t extra[4];
|
||||
uint64_t rem_offset = m_offset % 4;
|
||||
if (rem_offset) {
|
||||
uint64_t rem_rem = 4 - rem_offset;
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4), extra, 4))
|
||||
return 0;
|
||||
memcpy(buf, extra + rem_offset, rem_rem);
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4 + 1), (uint8_t*)buf + rem_rem, length - rem_rem))
|
||||
return 0;
|
||||
} else {
|
||||
if (wbfsDiscRead((uint32_t)(m_offset / 4), (uint8_t*)buf, length))
|
||||
return 0;
|
||||
}
|
||||
m_offset += length;
|
||||
return length;
|
||||
}
|
||||
uint64_t position() const override { return m_offset; }
|
||||
void seek(int64_t offset, int whence) override {
|
||||
if (whence == SEEK_SET)
|
||||
m_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
m_offset += offset;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, m_fio->beginReadStream(), offset, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { return {}; }
|
||||
};
|
||||
|
||||
std::unique_ptr<IDiscIO> NewDiscIOWBFS(const SystemChar* path)
|
||||
{
|
||||
return std::unique_ptr<IDiscIO>(new DiscIOWBFS(path));
|
||||
}
|
||||
|
||||
}
|
||||
std::unique_ptr<IDiscIO> NewDiscIOWBFS(SystemStringView path) { return std::make_unique<DiscIOWBFS>(path); }
|
||||
|
||||
} // namespace nod
|
||||
|
||||
2599
lib/DiscWii.cpp
2599
lib/DiscWii.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,190 +1,159 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "nod/Util.hpp"
|
||||
#include <cinttypes>
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
namespace nod {
|
||||
|
||||
class FileIOFILE : public IFileIO {
|
||||
SystemString m_path;
|
||||
int64_t m_maxWriteSize;
|
||||
|
||||
class FileIOFILE : public IFileIO
|
||||
{
|
||||
SystemString m_path;
|
||||
int64_t m_maxWriteSize;
|
||||
public:
|
||||
FileIOFILE(const SystemString& path, int64_t maxWriteSize)
|
||||
: m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
FileIOFILE(const SystemChar* path, int64_t maxWriteSize)
|
||||
: m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
FileIOFILE(SystemStringView path, int64_t maxWriteSize) : m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
|
||||
bool exists()
|
||||
{
|
||||
FILE* fp = fopen(m_path.c_str(), "rb");
|
||||
if (!fp)
|
||||
return false;
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
bool exists() override {
|
||||
FILE* fp = Fopen(m_path.c_str(), _SYS_STR("rb"));
|
||||
if (!fp)
|
||||
return false;
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t size()
|
||||
{
|
||||
FILE* fp = fopen(m_path.c_str(), "rb");
|
||||
if (!fp)
|
||||
return 0;
|
||||
FSeek(fp, 0, SEEK_END);
|
||||
uint64_t result = FTell(fp);
|
||||
fclose(fp);
|
||||
return result;
|
||||
}
|
||||
uint64_t size() override {
|
||||
FILE* fp = Fopen(m_path.c_str(), _SYS_STR("rb"));
|
||||
if (!fp)
|
||||
return 0;
|
||||
FSeek(fp, 0, SEEK_END);
|
||||
uint64_t result = FTell(fp);
|
||||
fclose(fp);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct WriteStream : public IFileIO::IWriteStream
|
||||
{
|
||||
FILE* fp;
|
||||
int64_t m_maxWriteSize;
|
||||
WriteStream(const SystemString& path, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize)
|
||||
{
|
||||
fp = fopen(path.c_str(), "wb");
|
||||
if (!fp)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
||||
err = true;
|
||||
}
|
||||
}
|
||||
WriteStream(const SystemString& path, uint64_t offset, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize)
|
||||
{
|
||||
fp = fopen(path.c_str(), "ab");
|
||||
if (!fp)
|
||||
goto FailLoc;
|
||||
fclose(fp);
|
||||
fp = fopen(path.c_str(), "r+b");
|
||||
if (!fp)
|
||||
goto FailLoc;
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
return;
|
||||
FailLoc:
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
||||
err = true;
|
||||
}
|
||||
~WriteStream()
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
uint64_t write(const void* buf, uint64_t length)
|
||||
{
|
||||
if (m_maxWriteSize >= 0)
|
||||
{
|
||||
if (FTell(fp) + length > m_maxWriteSize)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("write operation exceeds file's %" PRIi64 "-byte limit"), m_maxWriteSize);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return fwrite(buf, 1, length, fp);
|
||||
}
|
||||
};
|
||||
std::unique_ptr<IWriteStream> beginWriteStream() const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
struct WriteStream : public IFileIO::IWriteStream {
|
||||
FILE* fp;
|
||||
int64_t m_maxWriteSize;
|
||||
WriteStream(SystemStringView path, int64_t maxWriteSize, bool& err) : m_maxWriteSize(maxWriteSize) {
|
||||
fp = Fopen(path.data(), _SYS_STR("wb"));
|
||||
if (!fp) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
|
||||
err = true;
|
||||
}
|
||||
}
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
WriteStream(SystemStringView path, uint64_t offset, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize) {
|
||||
fp = Fopen(path.data(), _SYS_STR("ab"));
|
||||
if (!fp)
|
||||
goto FailLoc;
|
||||
fclose(fp);
|
||||
fp = Fopen(path.data(), _SYS_STR("r+b"));
|
||||
if (!fp)
|
||||
goto FailLoc;
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
return;
|
||||
FailLoc:
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
|
||||
err = true;
|
||||
}
|
||||
~WriteStream() override { fclose(fp); }
|
||||
uint64_t write(const void* buf, uint64_t length) override {
|
||||
if (m_maxWriteSize >= 0) {
|
||||
if (FTell(fp) + length > m_maxWriteSize) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("write operation exceeds file's {}-byte limit")),
|
||||
m_maxWriteSize);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return fwrite(buf, 1, length, fp);
|
||||
}
|
||||
};
|
||||
|
||||
struct ReadStream : public IFileIO::IReadStream
|
||||
{
|
||||
FILE* fp;
|
||||
ReadStream(const SystemString& path, bool& err)
|
||||
{
|
||||
fp = fopen(path.c_str(), "rb");
|
||||
if (!fp)
|
||||
{
|
||||
err = true;
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for reading"), path.c_str());
|
||||
}
|
||||
}
|
||||
ReadStream(const SystemString& path, uint64_t offset, bool& err)
|
||||
: ReadStream(path, err)
|
||||
{
|
||||
if (err)
|
||||
return;
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
}
|
||||
~ReadStream()
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
void seek(int64_t offset, int whence)
|
||||
{
|
||||
FSeek(fp, offset, whence);
|
||||
}
|
||||
uint64_t position() const
|
||||
{
|
||||
return FTell(fp);
|
||||
}
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{
|
||||
return fread(buf, 1, length, fp);
|
||||
}
|
||||
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length)
|
||||
{
|
||||
uint64_t written = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length)
|
||||
{
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
if (read(buf, thisSz) != thisSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to read enough from file");
|
||||
return written;
|
||||
}
|
||||
if (discio.write(buf, thisSz) != thisSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to write enough to disc");
|
||||
return written;
|
||||
}
|
||||
length -= thisSz;
|
||||
written += thisSz;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
};
|
||||
std::unique_ptr<IReadStream> beginReadStream() const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
std::unique_ptr<IWriteStream> beginWriteStream() const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct ReadStream : public IFileIO::IReadStream {
|
||||
FILE* fp;
|
||||
ReadStream(SystemStringView path, bool& err) {
|
||||
fp = Fopen(path.data(), _SYS_STR("rb"));
|
||||
if (!fp) {
|
||||
err = true;
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for reading")), path);
|
||||
}
|
||||
}
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
ReadStream(SystemStringView path, uint64_t offset, bool& err) : ReadStream(path, err) {
|
||||
if (err)
|
||||
return;
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
}
|
||||
~ReadStream() override { fclose(fp); }
|
||||
void seek(int64_t offset, int whence) override { FSeek(fp, offset, whence); }
|
||||
uint64_t position() const override { return FTell(fp); }
|
||||
uint64_t read(void* buf, uint64_t length) override { return fread(buf, 1, length, fp); }
|
||||
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length) override {
|
||||
uint64_t written = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length) {
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
if (read(buf, thisSz) != thisSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from file"));
|
||||
return written;
|
||||
}
|
||||
if (discio.write(buf, thisSz) != thisSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to write enough to disc"));
|
||||
return written;
|
||||
}
|
||||
length -= thisSz;
|
||||
written += thisSz;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream() const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, err));
|
||||
|
||||
if (err)
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemString& path, int64_t maxWriteSize)
|
||||
{
|
||||
return std::unique_ptr<IFileIO>(new FileIOFILE(path, maxWriteSize));
|
||||
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) {
|
||||
return std::make_unique<FileIOFILE>(path, maxWriteSize);
|
||||
}
|
||||
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemChar* path, int64_t maxWriteSize)
|
||||
{
|
||||
return std::unique_ptr<IFileIO>(new FileIOFILE(path, maxWriteSize));
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace nod
|
||||
|
||||
@@ -1,212 +1,207 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "nod/Util.hpp"
|
||||
#include <cinttypes>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "nod/IFileIO.hpp"
|
||||
#include "nod/Util.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include <logvisor/logvisor.hpp>
|
||||
|
||||
namespace nod {
|
||||
|
||||
class FileIOWin32 : public IFileIO {
|
||||
SystemString m_path;
|
||||
int64_t m_maxWriteSize;
|
||||
|
||||
class FileIOWin32 : public IFileIO
|
||||
{
|
||||
SystemString m_path;
|
||||
int64_t m_maxWriteSize;
|
||||
public:
|
||||
FileIOWin32(const SystemString& path, int64_t maxWriteSize)
|
||||
: m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
FileIOWin32(const SystemChar* path, int64_t maxWriteSize)
|
||||
: m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
FileIOWin32(SystemStringView path, int64_t maxWriteSize) : m_path(path), m_maxWriteSize(maxWriteSize) {}
|
||||
|
||||
bool exists()
|
||||
{
|
||||
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
CloseHandle(fp);
|
||||
return true;
|
||||
bool exists() override {
|
||||
#if !WINDOWS_STORE
|
||||
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
#else
|
||||
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
|
||||
#endif
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
CloseHandle(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t size() override {
|
||||
#if !WINDOWS_STORE
|
||||
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
#else
|
||||
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
|
||||
#endif
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
return 0;
|
||||
LARGE_INTEGER sz;
|
||||
if (!GetFileSizeEx(fp, &sz)) {
|
||||
CloseHandle(fp);
|
||||
return 0;
|
||||
}
|
||||
CloseHandle(fp);
|
||||
return sz.QuadPart;
|
||||
}
|
||||
|
||||
struct WriteStream : public IFileIO::IWriteStream {
|
||||
HANDLE fp;
|
||||
int64_t m_maxWriteSize;
|
||||
WriteStream(SystemStringView path, int64_t maxWriteSize, bool& err) : m_maxWriteSize(maxWriteSize) {
|
||||
#if !WINDOWS_STORE
|
||||
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
|
||||
nullptr);
|
||||
#else
|
||||
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, CREATE_ALWAYS, nullptr);
|
||||
#endif
|
||||
if (fp == INVALID_HANDLE_VALUE) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
|
||||
err = true;
|
||||
}
|
||||
}
|
||||
WriteStream(SystemStringView path, uint64_t offset, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize) {
|
||||
#if !WINDOWS_STORE
|
||||
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
|
||||
nullptr);
|
||||
#else
|
||||
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_ALWAYS, nullptr);
|
||||
#endif
|
||||
if (fp == INVALID_HANDLE_VALUE) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
LARGE_INTEGER lioffset;
|
||||
lioffset.QuadPart = offset;
|
||||
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
|
||||
}
|
||||
~WriteStream() override { CloseHandle(fp); }
|
||||
uint64_t write(const void* buf, uint64_t length) override {
|
||||
if (m_maxWriteSize >= 0) {
|
||||
LARGE_INTEGER li = {};
|
||||
LARGE_INTEGER res;
|
||||
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
|
||||
if (res.QuadPart + int64_t(length) > m_maxWriteSize) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("write operation exceeds file's {}-byte limit")),
|
||||
m_maxWriteSize);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD ret = 0;
|
||||
WriteFile(fp, buf, length, &ret, nullptr);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
std::unique_ptr<IWriteStream> beginWriteStream() const override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<WriteStream>(m_path, m_maxWriteSize, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint64_t size()
|
||||
{
|
||||
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
return 0;
|
||||
LARGE_INTEGER sz;
|
||||
if (!GetFileSizeEx(fp, &sz))
|
||||
{
|
||||
CloseHandle(fp);
|
||||
return 0;
|
||||
}
|
||||
CloseHandle(fp);
|
||||
return sz.QuadPart;
|
||||
return ret;
|
||||
}
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<WriteStream>(m_path, offset, m_maxWriteSize, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct WriteStream : public IFileIO::IWriteStream
|
||||
{
|
||||
HANDLE fp;
|
||||
int64_t m_maxWriteSize;
|
||||
WriteStream(const SystemString& path, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize)
|
||||
{
|
||||
fp = CreateFileW(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
|
||||
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
||||
err = true;
|
||||
}
|
||||
}
|
||||
WriteStream(const SystemString& path, uint64_t offset, int64_t maxWriteSize, bool& err)
|
||||
: m_maxWriteSize(maxWriteSize)
|
||||
{
|
||||
fp = CreateFileW(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
|
||||
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
||||
err = true;
|
||||
return;
|
||||
}
|
||||
LARGE_INTEGER lioffset;
|
||||
lioffset.QuadPart = offset;
|
||||
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
|
||||
}
|
||||
~WriteStream()
|
||||
{
|
||||
CloseHandle(fp);
|
||||
}
|
||||
uint64_t write(const void* buf, uint64_t length)
|
||||
{
|
||||
if (m_maxWriteSize >= 0)
|
||||
{
|
||||
LARGE_INTEGER li = {};
|
||||
LARGE_INTEGER res;
|
||||
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
|
||||
if (res.QuadPart + int64_t(length) > m_maxWriteSize)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("write operation exceeds file's %" PRIi64 "-byte limit"), m_maxWriteSize);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
DWORD ret = 0;
|
||||
WriteFile(fp, buf, length, &ret, nullptr);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
std::unique_ptr<IWriteStream> beginWriteStream() const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
struct ReadStream : public IFileIO::IReadStream {
|
||||
HANDLE fp;
|
||||
ReadStream(SystemStringView path, bool& err) {
|
||||
#if !WINDOWS_STORE
|
||||
fp = CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
||||
nullptr);
|
||||
#else
|
||||
fp = CreateFile2(path.data(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
|
||||
#endif
|
||||
if (fp == INVALID_HANDLE_VALUE) {
|
||||
err = true;
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for reading")), path);
|
||||
}
|
||||
}
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
ReadStream(SystemStringView path, uint64_t offset, bool& err) : ReadStream(path, err) {
|
||||
if (err)
|
||||
return;
|
||||
LARGE_INTEGER lioffset;
|
||||
lioffset.QuadPart = offset;
|
||||
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
|
||||
}
|
||||
~ReadStream() override { CloseHandle(fp); }
|
||||
void seek(int64_t offset, int whence) override {
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = offset;
|
||||
SetFilePointerEx(fp, li, nullptr, whence);
|
||||
}
|
||||
uint64_t position() const override {
|
||||
LARGE_INTEGER li = {};
|
||||
LARGE_INTEGER res;
|
||||
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
|
||||
return res.QuadPart;
|
||||
}
|
||||
uint64_t read(void* buf, uint64_t length) override {
|
||||
DWORD ret = 0;
|
||||
ReadFile(fp, buf, length, &ret, nullptr);
|
||||
return ret;
|
||||
}
|
||||
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length) override {
|
||||
uint64_t written = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length) {
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
if (read(buf, thisSz) != thisSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from file"));
|
||||
return written;
|
||||
}
|
||||
if (discio.write(buf, thisSz) != thisSz) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING("unable to write enough to disc"));
|
||||
return written;
|
||||
}
|
||||
length -= thisSz;
|
||||
written += thisSz;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream() const override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<ReadStream>(m_path, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct ReadStream : public IFileIO::IReadStream
|
||||
{
|
||||
HANDLE fp;
|
||||
ReadStream(const SystemString& path, bool& err)
|
||||
{
|
||||
fp = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
err = true;
|
||||
LogModule.report(logvisor::Error, _S("unable to open '%s' for reading"), path.c_str());
|
||||
}
|
||||
}
|
||||
ReadStream(const SystemString& path, uint64_t offset, bool& err)
|
||||
: ReadStream(path, err)
|
||||
{
|
||||
if (err)
|
||||
return;
|
||||
LARGE_INTEGER lioffset;
|
||||
lioffset.QuadPart = offset;
|
||||
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
|
||||
}
|
||||
~ReadStream()
|
||||
{
|
||||
CloseHandle(fp);
|
||||
}
|
||||
void seek(int64_t offset, int whence)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = offset;
|
||||
SetFilePointerEx(fp, li, nullptr, whence);
|
||||
}
|
||||
uint64_t position() const
|
||||
{
|
||||
LARGE_INTEGER li = {};
|
||||
LARGE_INTEGER res;
|
||||
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
|
||||
return res.QuadPart;
|
||||
}
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{
|
||||
DWORD ret = 0;
|
||||
ReadFile(fp, buf, length, &ret, nullptr);
|
||||
return ret;
|
||||
}
|
||||
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length)
|
||||
{
|
||||
uint64_t written = 0;
|
||||
uint8_t buf[0x7c00];
|
||||
while (length)
|
||||
{
|
||||
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
||||
if (read(buf, thisSz) != thisSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to read enough from file");
|
||||
return written;
|
||||
}
|
||||
if (discio.write(buf, thisSz) != thisSz)
|
||||
{
|
||||
LogModule.report(logvisor::Error, "unable to write enough to disc");
|
||||
return written;
|
||||
}
|
||||
length -= thisSz;
|
||||
written += thisSz;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
};
|
||||
std::unique_ptr<IReadStream> beginReadStream() const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
bool Err = false;
|
||||
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
|
||||
bool err = false;
|
||||
auto ret = std::make_unique<ReadStream>(m_path, offset, err);
|
||||
|
||||
if (err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemString& path, int64_t maxWriteSize)
|
||||
{
|
||||
return std::unique_ptr<IFileIO>(new FileIOWin32(path, maxWriteSize));
|
||||
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) {
|
||||
return std::make_unique<FileIOWin32>(path, maxWriteSize);
|
||||
}
|
||||
|
||||
std::unique_ptr<IFileIO> NewFileIO(const SystemChar* path, int64_t maxWriteSize)
|
||||
{
|
||||
return std::unique_ptr<IFileIO>(new FileIOWin32(path, maxWriteSize));
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace nod
|
||||
|
||||
960
lib/aes.cpp
960
lib/aes.cpp
File diff suppressed because it is too large
Load Diff
143
lib/nod.cpp
143
lib/nod.cpp
@@ -1,88 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include "nod/nod.hpp"
|
||||
#include "nod/DiscBase.hpp"
|
||||
|
||||
namespace nod
|
||||
{
|
||||
#include <cstdio>
|
||||
|
||||
#include "nod/DiscBase.hpp"
|
||||
#include "nod/DiscGCN.hpp"
|
||||
#include "nod/DiscWii.hpp"
|
||||
|
||||
namespace nod {
|
||||
|
||||
logvisor::Module LogModule("nod");
|
||||
|
||||
std::unique_ptr<IDiscIO> NewDiscIOISO(const SystemChar* path);
|
||||
std::unique_ptr<IDiscIO> NewDiscIOWBFS(const SystemChar* path);
|
||||
std::unique_ptr<IDiscIO> NewDiscIOISO(SystemStringView path);
|
||||
std::unique_ptr<IDiscIO> NewDiscIOWBFS(SystemStringView path);
|
||||
std::unique_ptr<IDiscIO> NewDiscIONFS(SystemStringView path);
|
||||
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path, bool& isWii)
|
||||
{
|
||||
/* Temporary file handle to determine image type */
|
||||
std::unique_ptr<IFileIO> fio = NewFileIO(path);
|
||||
if (!fio->exists())
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("Unable to open '%s'"), path);
|
||||
return {};
|
||||
}
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = fio->beginReadStream();
|
||||
if (!rs)
|
||||
return {};
|
||||
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()) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to open '{}'")), path);
|
||||
return {};
|
||||
}
|
||||
std::unique_ptr<IFileIO::IReadStream> rs = fio->beginReadStream();
|
||||
if (!rs)
|
||||
return {};
|
||||
|
||||
isWii = false;
|
||||
std::unique_ptr<IDiscIO> discIO;
|
||||
uint32_t magic = 0;
|
||||
if (rs->read(&magic, 4) != 4)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("Unable to read magic from '%s'"), path);
|
||||
return {};
|
||||
}
|
||||
isWii = false;
|
||||
std::unique_ptr<IDiscIO> discIO;
|
||||
uint32_t magic = 0;
|
||||
if (rs->read(&magic, 4) != 4) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to read magic from '{}'")), path);
|
||||
return {};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
using SignedSize = std::make_signed<SystemString::size_type>::type;
|
||||
const auto dotPos = SignedSize(path.rfind(_SYS_STR('.')));
|
||||
const auto slashPos = SignedSize(path.find_last_of(_SYS_STR("/\\")));
|
||||
if (magic == nod::SBig((uint32_t)'WBFS')) {
|
||||
discIO = NewDiscIOWBFS(path);
|
||||
isWii = true;
|
||||
} else if (path.size() > 4 && dotPos != -1 && dotPos > slashPos &&
|
||||
!path.compare(slashPos + 1, 4, _SYS_STR("hif_")) &&
|
||||
!path.compare(dotPos, path.size() - dotPos, _SYS_STR(".nfs"))) {
|
||||
discIO = NewDiscIONFS(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);
|
||||
}
|
||||
}
|
||||
|
||||
if (!discIO)
|
||||
{
|
||||
LogModule.report(logvisor::Error, _S("'%s' is not a valid image"), path);
|
||||
return {};
|
||||
}
|
||||
if (!discIO) {
|
||||
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("'{}' is not a valid image")), path);
|
||||
return {};
|
||||
}
|
||||
|
||||
bool Err = false;
|
||||
std::unique_ptr<DiscBase> ret;
|
||||
if (isWii)
|
||||
{
|
||||
ret = std::unique_ptr<DiscBase>(new DiscWii(std::move(discIO), Err));
|
||||
if (Err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = std::unique_ptr<DiscBase>(new DiscGCN(std::move(discIO), Err));
|
||||
if (Err)
|
||||
return {};
|
||||
bool err = false;
|
||||
std::unique_ptr<DiscBase> ret;
|
||||
if (isWii) {
|
||||
ret = std::make_unique<DiscWii>(std::move(discIO), err);
|
||||
if (err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = std::make_unique<DiscGCN>(std::move(discIO), err);
|
||||
if (err)
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(const SystemChar* path)
|
||||
{
|
||||
bool isWii;
|
||||
return OpenDiscFromImage(path, isWii);
|
||||
}
|
||||
|
||||
std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path) {
|
||||
bool isWii;
|
||||
return OpenDiscFromImage(path, isWii);
|
||||
}
|
||||
|
||||
} // namespace nod
|
||||
|
||||
2
logvisor
2
logvisor
Submodule logvisor updated: f8ab0e03ba...7901fc496d
Reference in New Issue
Block a user