[nostalgia/tools/pack] Add verify, cleanup linking options, cleanup error handling

This commit is contained in:
Gary Talent 2019-06-22 01:29:55 -05:00
parent 04929302d7
commit 6a5b16c644
4 changed files with 24 additions and 19 deletions

View File

@ -5,13 +5,6 @@ add_executable(nost-pack pack.cpp)
target_link_libraries(
nost-pack
Qt5::Widgets
OxClArgs
OxFS
OxStd
OxMetalClaw
NostalgiaCommon
NostalgiaCore
NostalgiaPack
)

View File

@ -54,7 +54,9 @@ ox::Error run(ClArgs args) {
ox::FileSystem32::format(buff.data(), buff.size());
ox::PassThroughFS src(argSrc.c_str());
ox::FileSystem32 dst(ox::FileStore32(buff.data(), buff.size()));
oxReturnError(nostalgia::pack(&src, &dst));
auto err = nostalgia::pack(&src, &dst);
oxAssert(err, "pack failed");
oxReturnError(err);
return OxError(0);
}

View File

@ -6,7 +6,7 @@ add_library(
)
target_link_libraries(
NostalgiaPack
NostalgiaPack PUBLIC
Qt5::Widgets
OxClArgs
OxFS

View File

@ -6,6 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <iostream>
#include <string_view>
#include <vector>
@ -63,21 +64,29 @@ ox::Error transformClaw(ox::FileSystem32 *dest, std::string path) {
return OxError(0);
}
[[nodiscard]] ox::Error verifyFile(ox::FileSystem32 *fs, const std::string &path, const std::vector<char> &expected) noexcept {
std::vector<char> buff(expected.size());
oxReturnError(fs->read(path.c_str(), buff.data(), buff.size()));
return buff == expected ? 0 : OxError(1);
}
ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path) {
std::cout << "copying directory: " << path << '\n';
// copy
src->ls(path.c_str(), [src, dest, path](const char *name, ox::InodeId_t) {
auto [stat, err] = src->stat(path.c_str());
return src->ls(path.c_str(), [src, dest, path](const char *name, ox::InodeId_t) {
std::cout << "reading " << name << '\n';
const auto currentFile = path + name;
auto [stat, err] = src->stat((currentFile).c_str());
oxReturnError(err);
if (stat.fileType == ox::FileType_Directory) {
const auto dir = path + name + '/';
oxReturnError(dest->mkdir(dir.c_str()));
oxReturnError(copy(src, dest, dir));
oxReturnError(dest->mkdir(currentFile.c_str(), true));
oxReturnError(copy(src, dest, currentFile + '/'));
} else {
std::vector<char> buff;
// do transforms
if (endsWith(path, ".png")) {
if (endsWith(currentFile, ".png")) {
// load file from full path and transform
const auto fullPath = src->basePath() + path;
const auto fullPath = src->basePath() + currentFile;
buff = pngToGba(fullPath.c_str(), 0, 0);
if (!buff.size()) {
return OxError(1);
@ -85,14 +94,15 @@ ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path)
} else {
// load file
buff.resize(stat.size);
oxReturnError(src->read(path.c_str(), buff.data(), buff.size()));
oxReturnError(src->read(currentFile.c_str(), buff.data(), buff.size()));
}
// write file to dest
oxReturnError(dest->write(path.c_str(), buff.data(), buff.size()));
std::cout << "writing " << currentFile << '\n';
oxReturnError(dest->write(currentFile.c_str(), buff.data(), buff.size()));
oxReturnError(verifyFile(dest, currentFile, buff));
}
return OxError(0);
});
return OxError(0);
}
}