[nostalgia/tools/pack] Implement the long stubbed out pathToInode replacement

This commit is contained in:
Gary Talent 2022-07-09 03:07:04 -05:00
parent 522bb42e8f
commit e8a3f5ea9a
3 changed files with 62 additions and 23 deletions

View File

@ -39,7 +39,7 @@ static ox::Error run(const ox::ClArgs &args) noexcept {
oxReturnError(dst.resize()); oxReturnError(dst.resize());
oxRequire(dstSize, dst.size()); oxRequire(dstSize, dst.size());
oxOutf("new size: {}\n", dstSize); oxOutf("new size: {} bytes\n", dstSize);
buff.resize(dstSize); buff.resize(dstSize);
oxReturnError(writeFileBuff(argDst, buff)); oxReturnError(writeFileBuff(argDst, buff));

View File

@ -15,3 +15,10 @@ install(
ARCHIVE DESTINATION ARCHIVE DESTINATION
lib/nostalgia lib/nostalgia
) )
install(
FILES
pack.hpp
DESTINATION
include/nostalgia/tools/pack
)

View File

@ -4,52 +4,82 @@
#include <ox/claw/read.hpp> #include <ox/claw/read.hpp>
#include <ox/fs/fs.hpp> #include <ox/fs/fs.hpp>
#include <ox/mc/write.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/model/modelvalue.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/typeconv.hpp> #include <nostalgia/core/typeconv.hpp>
#include <nostalgia/core/typestore.hpp>
#include "pack.hpp" #include "pack.hpp"
namespace nostalgia { namespace nostalgia {
static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
auto &o = *obj;
auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>());
auto &data = o["data"].get<ox::ModelUnion>();
ox::String path;
switch (type) {
case ox::FileAddressType::Path:
path = data["path"].get<ox::String>();
break;
case ox::FileAddressType::ConstPath:
path = data["constPath"].get<ox::String>();
break;
case ox::FileAddressType::Inode:
case ox::FileAddressType::None:
return OxError(0);
}
oxRequire(s, dest->stat(path.c_str()));
oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode)));
return data.set(2, s.inode);
}
/** /**
* Convert path references in Claw data to inodes to save space * Convert path references in Claw data to inodes to save space
* @param buff buffer holding file * @param buff buffer holding file
* @return error * @return error
* stub for now * stub for now
*/ */
static ox::Result<ox::Buffer> pathToInode(const ox::Buffer &buff) noexcept { static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
return std::move(buff); for (auto &f : *obj) {
} auto &v = f->value;
if (v.type() != ox::ModelValue::Type::Object) {
// just strip header for now... continue;
static ox::Result<ox::Buffer> toMetalClaw(const ox::Buffer &buff) noexcept {
oxRequire(hdr, ox::readClawHeader(buff));
if (hdr.fmt != ox::ClawFormat::Metal) {
return OxError(1, "Cannot convert from Claw to MetalClaw, data portion must already be MetalClaw");
} }
return ox::stripClawHeader(buff); auto &o = v.get<ox::ModelObject>();
if (o.typeName() == "net.drinkingtea.ox.FileAddress" && o.typeVersion() == 1) {
oxReturnError(pathToInode(dest, &o));
} else {
oxReturnError(transformObj(dest, &o));
}
}
return OxError(0);
} }
static ox::Error doTransformations(ox::FileSystem *dest, const ox::String &filePath) noexcept { static ox::Error doTransformations(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &filePath) noexcept {
if (filePath.endsWith(".ng") || filePath.endsWith(".npal")) { if (filePath.endsWith(".ng") || filePath.endsWith(".npal")) {
// load file // load file
oxRequireM(buff, dest->read(filePath.c_str())); oxRequire(s, dest->stat(filePath.c_str()));
oxRequireM(buff, dest->read(s.inode));
if (filePath.endsWith(".ng")) { if (filePath.endsWith(".ng")) {
oxReturnError(core::convertBuffToBuff<core::CompactTileSheet>(buff, ox::ClawFormat::Metal).moveTo(&buff)); oxReturnError(core::convertBuffToBuff<core::CompactTileSheet>(buff, ox::ClawFormat::Metal).moveTo(&buff));
} }
oxRequireM(obj, ox::readClaw(ts, buff));
// do transformations // do transformations
oxReturnError(pathToInode(buff).moveTo(&buff)); oxReturnError(transformObj(dest, &obj));
oxReturnError(toMetalClaw(buff).moveTo(&buff)); oxReturnError(ox::writeMC(&obj).moveTo(&buff));
// write file to dest // write file to dest
oxReturnError(dest->write(filePath.c_str(), buff.data(), buff.size())); oxReturnError(dest->write(s.inode, buff.data(), buff.size()));
} }
return OxError(0); return OxError(0);
} }
// claw file transformations are broken out because path to inode // claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete // transformations need to be done after the copy to the new FS is complete
static ox::Error transformClaw(ox::FileSystem *dest, const ox::String &path) noexcept { static ox::Error transformClaw(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &path) noexcept {
// copy // copy
oxTracef("pack::transformClaw", "path: {}", path); oxTracef("pack::transformClaw", "path: {}", path);
oxRequire(fileList, dest->ls(path)); oxRequire(fileList, dest->ls(path));
@ -58,9 +88,9 @@ static ox::Error transformClaw(ox::FileSystem *dest, const ox::String &path) noe
oxRequire(stat, dest->stat(filePath.c_str())); oxRequire(stat, dest->stat(filePath.c_str()));
if (stat.fileType == ox::FileType::Directory) { if (stat.fileType == ox::FileType::Directory) {
const auto dir = path + name + '/'; const auto dir = path + name + '/';
oxReturnError(transformClaw(dest, dir)); oxReturnError(transformClaw(ts, dest, dir));
} else { } else {
oxReturnError(doTransformations(dest, filePath)); oxReturnError(doTransformations(ts, dest, filePath));
} }
} }
return OxError(0); return OxError(0);
@ -75,9 +105,9 @@ static ox::Error verifyFile(ox::FileSystem *fs, const ox::String &path, const ox
struct VerificationPair { struct VerificationPair {
ox::String path; ox::String path;
ox::Buffer buff; ox::Buffer buff;
VerificationPair(const ox::String &pPath, const ox::Buffer &pBuff) noexcept { VerificationPair(const ox::String &pPath, ox::Buffer pBuff) noexcept:
path = pPath; path(pPath),
buff = pBuff; buff(std::move(pBuff)) {
} }
}; };
@ -115,7 +145,9 @@ static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, const ox::Strin
ox::Error pack(ox::FileSystem *src, ox::FileSystem *dest) noexcept { ox::Error pack(ox::FileSystem *src, ox::FileSystem *dest) noexcept {
oxReturnError(copy(src, dest, "/")); oxReturnError(copy(src, dest, "/"));
oxReturnError(transformClaw(dest, "/")); core::TypeStore ts(src);
oxReturnError(ox::buildTypeDef<core::CompactTileSheet>(&ts));
oxReturnError(transformClaw(&ts, dest, "/"));
return OxError(0); return OxError(0);
} }