[nostalgia/tools] Add NostalgiaPack library

This commit is contained in:
2019-06-16 22:42:03 -05:00
parent 5ceb0e5f83
commit 5f41a2665b
7 changed files with 87 additions and 150 deletions

View File

@@ -1,12 +1,12 @@
add_library(
Pack SHARED
NostalgiaPack SHARED
imgconv.cpp
pack.cpp
)
target_link_libraries(
Pack
NostalgiaPack
Qt5::Widgets
OxClArgs
OxFS
@@ -18,7 +18,7 @@ target_link_libraries(
install(
TARGETS
Pack
NostalgiaPack
LIBRARY DESTINATION
${NOSTALGIA_DIST_LIB}/nostalgia
)

View File

@@ -56,11 +56,13 @@ namespace {
return colors.size();
}
ox::Error pngToGba(QString argInPath, int argTiles, int argBpp) {
ox::Error err = 0;
[[nodiscard]] std::vector<char> pngToGba(QString argInPath, int argTiles, int argBpp) {
QImage src(argInPath);
if (src.isNull()) {
return {};
}
if (argTiles == 0) {
argTiles = (src.width() * src.height()) / 64;
}
@@ -70,9 +72,8 @@ ox::Error pngToGba(QString argInPath, int argTiles, int argBpp) {
QMap<QRgb, int> colors;
const auto imgDataBuffSize = sizeof(core::GbaImageData) + 1 + argTiles * 64;
auto imgDataBuff = std::make_unique<uint8_t[]>(imgDataBuffSize);
memset(imgDataBuff.get(), 0, imgDataBuffSize);
auto id = reinterpret_cast<core::GbaImageData*>(imgDataBuff.get());
std::vector<char> imgDataBuff(imgDataBuffSize);
auto id = new (imgDataBuff.data()) core::GbaImageData;
id->header.bpp = argBpp;
id->header.tileCount = argTiles;
int colorId = 0;
@@ -82,7 +83,7 @@ ox::Error pngToGba(QString argInPath, int argTiles, int argBpp) {
for (int y = 0; y < src.height(); y++) {
auto destI = pointToIdx(src.width(), x, y);
if (destI <= argTiles * 64) {
auto c = src.pixel(x, y);
const auto c = src.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = colorId;
@@ -107,7 +108,8 @@ ox::Error pngToGba(QString argInPath, int argTiles, int argBpp) {
auto colorId = colors[key];
id->pal[colorId] = toGbaColor(key);
}
return err;
return imgDataBuff;
}
}

View File

@@ -13,6 +13,6 @@
namespace nostalgia {
ox::Error pngToGba(QString argInPath, int argTiles, int argBpp = -1);
[[nodiscard]] std::vector<char> pngToGba(QString argInPath, int argTiles, int argBpp = -1);
}

View File

@@ -14,11 +14,18 @@
namespace nostalgia {
namespace {
[[nodiscard]] static constexpr bool endsWith(std::string_view str, std::string_view ending) {
return str.size() >= ending.size() && str.substr(str.size() - ending.size()) == ending;
}
// stub for now
/**
* Convert path references to inodes to save space
* @param buff buffer holding file
* @return error
* stub for now
*/
ox::Error pathToInode(std::vector<char>*) {
return OxError(0);
}
@@ -28,6 +35,34 @@ ox::Error toMetalClaw(std::vector<char>*) {
return OxError(0);
}
// claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete
ox::Error transformClaw(ox::FileSystem32 *dest, std::string path) {
// copy
dest->ls(path.c_str(), [dest, path](const char *name, ox::InodeId_t) {
auto stat = dest->stat(path.c_str());
oxReturnError(stat.error);
if (stat.value.fileType == ox::FileType_Directory) {
const auto dir = path + name + '/';
oxReturnError(transformClaw(dest, dir));
} else {
// do transforms
if (endsWith(path, ".claw")) {
// load file
std::vector<char> buff(stat.value.size);
oxReturnError(dest->read(path.c_str(), buff.data(), buff.size()));
// do transformations
oxReturnError(pathToInode(&buff));
oxReturnError(toMetalClaw(&buff));
// write file to dest
oxReturnError(dest->write(path.c_str(), buff.data(), buff.size()));
}
}
return OxError(0);
});
return OxError(0);
}
ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path) {
// copy
src->ls(path.c_str(), [src, dest, path](const char *name, ox::InodeId_t) {
@@ -38,16 +73,19 @@ ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path)
oxReturnError(dest->mkdir(dir.c_str()));
oxReturnError(copy(src, dest, dir));
} else {
std::vector<char> buff(stat.value.size);
// load file
oxReturnError(src->read(path.c_str(), buff.data(), buff.size()));
std::vector<char> buff;
// do transforms
if (endsWith(path, ".claw")) {
oxReturnError(pathToInode(&buff));
oxReturnError(toMetalClaw(&buff));
} else if (endsWith(path, ".png")) {
if (endsWith(path, ".png")) {
// load file from full path and transform
const auto fullPath = src->basePath() + path;
oxReturnError(pngToGba(fullPath.c_str(), 0, 0));
buff = pngToGba(fullPath.c_str(), 0, 0);
if (!buff.size()) {
return OxError(1);
}
} else {
// load file
buff.resize(stat.value.size);
oxReturnError(src->read(path.c_str(), buff.data(), buff.size()));
}
// write file to dest
oxReturnError(dest->write(path.c_str(), buff.data(), buff.size()));
@@ -58,3 +96,11 @@ ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path)
}
}
ox::Error pack(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path) {
oxReturnError(copy(src, dest, path));
oxReturnError(transformClaw(dest, path));
return OxError(0);
}
}

View File

@@ -12,6 +12,6 @@
namespace nostalgia {
ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path = "/");
ox::Error pack(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path = "/");
}