Skip to content

Commit

Permalink
More work compiled anims import
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 14, 2023
1 parent 112b670 commit 1c6df2c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 19 deletions.
5 changes: 3 additions & 2 deletions include/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st

std::pair<CompiledTileset, std::unordered_map<std::size_t, Attributes>>
importCompiledTileset(PtContext &ctx, std::ifstream &metatiles, std::ifstream &attributes,
png::image<png::index_pixel> &tilesheetPng,
std::vector<std::shared_ptr<std::ifstream>> &paletteFiles);
const png::image<png::index_pixel> &tilesheetPng,
const std::vector<std::shared_ptr<std::ifstream>> &paletteFiles,
const std::vector<std::vector<AnimationPng<png::index_pixel>>> &compiledAnims);

} // namespace porytiles

Expand Down
10 changes: 8 additions & 2 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,13 @@ struct CompilerSourcePaths {
return path / "top.png";
}

std::filesystem::path primaryAnim() const
std::filesystem::path primaryAnims() const
{
std::filesystem::path path{primarySourcePath};
return path / "anims";
}

std::filesystem::path secondaryAnim() const
std::filesystem::path secondaryAnims() const
{
std::filesystem::path path{secondarySourcePath};
return path / "anims";
Expand Down Expand Up @@ -776,6 +776,12 @@ struct DecompilerSourcePaths {
return path / "palettes";
}

std::filesystem::path primaryAnims() const
{
std::filesystem::path path{primarySourcePath};
return path / "anims";
}

std::filesystem::path modeBasedSrcPath(DecompilerMode mode) const;
};

Expand Down
96 changes: 86 additions & 10 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,20 @@ static void validateDecompileOutputs(PtContext &ctx, std::filesystem::path &outp
}
}

static void importDecompiledAnims(PtContext &ctx, DecompiledTileset &decompTiles, std::filesystem::path animationPath)
static std::vector<std::vector<AnimationPng<png::rgba_pixel>>>
prepareDecompiledAnimsForImport(PtContext &ctx, std::filesystem::path animationPath)
{
std::vector<std::vector<AnimationPng<png::rgba_pixel>>> animations{};

pt_logln(ctx, stderr, "importing animations from {}", animationPath.string());
if (!std::filesystem::exists(animationPath) || !std::filesystem::is_directory(animationPath)) {
pt_logln(ctx, stderr, "path `{}' does not exist, skipping anims import", animationPath.string());
return;
return animations;
}
std::vector<std::filesystem::path> animationDirectories;
std::copy(std::filesystem::directory_iterator(animationPath), std::filesystem::directory_iterator(),
std::back_inserter(animationDirectories));
std::sort(animationDirectories.begin(), animationDirectories.end());
std::vector<std::vector<AnimationPng<png::rgba_pixel>>> animations{};
for (const auto &animDir : animationDirectories) {
if (!std::filesystem::is_directory(animDir)) {
pt_logln(ctx, stderr, "skipping regular file: {}", animDir.string());
Expand Down Expand Up @@ -341,7 +343,76 @@ static void importDecompiledAnims(PtContext &ctx, DecompiledTileset &decompTiles
"found anim frame that was not a png");
}

importAnimTiles(ctx, animations, decompTiles);
return animations;
}

static std::vector<std::vector<AnimationPng<png::index_pixel>>>
prepareCompiledAnimsForImport(PtContext &ctx, std::filesystem::path animationPath)
{
std::vector<std::vector<AnimationPng<png::index_pixel>>> animations{};

pt_logln(ctx, stderr, "importing animations from {}", animationPath.string());
if (!std::filesystem::exists(animationPath) || !std::filesystem::is_directory(animationPath)) {
pt_logln(ctx, stderr, "path `{}' does not exist, skipping anims import", animationPath.string());
return animations;
}
std::vector<std::filesystem::path> animationDirectories;
std::copy(std::filesystem::directory_iterator(animationPath), std::filesystem::directory_iterator(),
std::back_inserter(animationDirectories));
std::sort(animationDirectories.begin(), animationDirectories.end());
for (const auto &animDir : animationDirectories) {
if (!std::filesystem::is_directory(animDir)) {
pt_logln(ctx, stderr, "skipping regular file: {}", animDir.string());
continue;
}

// collate all possible animation frame files
pt_logln(ctx, stderr, "found animation: {}", animDir.string());
std::unordered_map<std::size_t, std::filesystem::path> frames{};
for (const auto &frameFile : std::filesystem::directory_iterator(animDir)) {
std::string fileName = frameFile.path().filename().string();
std::string extension = frameFile.path().extension().string();
if (!std::regex_match(fileName, std::regex("^[0-9][0-9]\\.png$"))) {
pt_logln(ctx, stderr, "skipping file: {}", frameFile.path().string());
continue;
}
std::size_t index = std::stoi(fileName, 0, 10) + 1;
frames.insert(std::pair{index, frameFile.path()});
pt_logln(ctx, stderr, "found frame file: {}, index={}", frameFile.path().string(), index);
}

std::vector<AnimationPng<png::index_pixel>> framePngs{};
if (frames.size() == 0) {
// TODO : better error
throw std::runtime_error{"TODO : error for import decompiled anims"};
// fatalerror_missingRequiredAnimFrameFile(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
// animDir.filename().string(), 0);
}
for (std::size_t i = 1; i <= frames.size(); i++) {
if (!frames.contains(i)) {
// TODO : better error
throw std::runtime_error{"TODO : error for import decompiled anims"};
// fatalerror_missingRequiredAnimFrameFile(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
// animDir.filename().string(), i - 1);
}

try {
// We do this here so if the source is not a PNG, we can catch and give a better error
png::image<png::index_pixel> png{frames.at(i)};
AnimationPng<png::index_pixel> animPng{png, animDir.filename().string(), frames.at(i).filename().string()};
framePngs.push_back(animPng);
}
catch (const std::exception &exception) {
// TODO : better error
throw std::runtime_error{"TODO : error for import decompiled anims"};
// error_animFrameWasNotAPng(ctx.err, animDir.filename().string(), frames.at(i).filename().string());
}
}

animations.push_back(framePngs);
}

return animations;
}

static std::unordered_map<std::size_t, Attributes>
Expand Down Expand Up @@ -460,11 +531,13 @@ static void driveDecompile(PtContext &ctx)
}
paletteFiles.push_back(std::make_shared<std::ifstream>(paletteFile));
}
auto compiledAnims = prepareCompiledAnimsForImport(ctx, ctx.decompilerSrcPaths.primaryAnims());

/*
* Import the compiled tileset into our data types
*/
auto [compiled, attributesMap] = importCompiledTileset(ctx, metatiles, attributes, tilesheetPng, paletteFiles);
auto [compiled, attributesMap] =
importCompiledTileset(ctx, metatiles, attributes, tilesheetPng, paletteFiles, compiledAnims);

/*
* Close file stream objects
Expand Down Expand Up @@ -547,7 +620,8 @@ static void driveCompile(PtContext &ctx)

DecompiledTileset decompiledPrimaryTiles =
importLayeredTilesFromPngs(ctx, primaryAttributesMap, bottomPrimaryPng, middlePrimaryPng, topPrimaryPng);
importDecompiledAnims(ctx, decompiledPrimaryTiles, ctx.compilerSrcPaths.primaryAnim());
auto primaryAnimations = prepareDecompiledAnimsForImport(ctx, ctx.compilerSrcPaths.primaryAnims());
importAnimTiles(ctx, primaryAnimations, decompiledPrimaryTiles);
auto partnerPrimaryTiles = compile(ctx, decompiledPrimaryTiles);

pt_logln(ctx, stderr, "importing secondary tiles from {}", ctx.compilerSrcPaths.secondarySourcePath);
Expand All @@ -563,11 +637,12 @@ static void driveCompile(PtContext &ctx)
"errors generated during secondary attributes import");
}

DecompiledTileset decompiledTiles =
DecompiledTileset decompiledSecondaryTiles =
importLayeredTilesFromPngs(ctx, secondaryAttributesMap, bottomPng, middlePng, topPng);
importDecompiledAnims(ctx, decompiledTiles, ctx.compilerSrcPaths.secondaryAnim());
auto secondaryAnimations = prepareDecompiledAnimsForImport(ctx, ctx.compilerSrcPaths.secondaryAnims());
importAnimTiles(ctx, secondaryAnimations, decompiledSecondaryTiles);
ctx.compilerContext.pairedPrimaryTileset = std::move(partnerPrimaryTiles);
compiledTiles = compile(ctx, decompiledTiles);
compiledTiles = compile(ctx, decompiledSecondaryTiles);
ctx.compilerContext.resultTileset = std::move(compiledTiles);
}
else {
Expand All @@ -585,7 +660,8 @@ static void driveCompile(PtContext &ctx)

DecompiledTileset decompiledTiles =
importLayeredTilesFromPngs(ctx, primaryAttributesMap, bottomPng, middlePng, topPng);
importDecompiledAnims(ctx, decompiledTiles, ctx.compilerSrcPaths.primaryAnim());
auto animations = prepareDecompiledAnimsForImport(ctx, ctx.compilerSrcPaths.primaryAnims());
importAnimTiles(ctx, animations, decompiledTiles);
compiledTiles = compile(ctx, decompiledTiles);
ctx.compilerContext.resultTileset = std::move(compiledTiles);
}
Expand Down
13 changes: 8 additions & 5 deletions src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ static RGBA32 parseJascLine(const ErrorsAndWarnings &err, const std::string &jas
}

static std::vector<GBAPalette> importCompiledPalettes(PtContext &ctx,
std::vector<std::shared_ptr<std::ifstream>> &paletteFiles)
const std::vector<std::shared_ptr<std::ifstream>> &paletteFiles)
{
std::vector<GBAPalette> palettes{};

Expand Down Expand Up @@ -824,16 +824,17 @@ importCompiledAnimations(PtContext &ctx, const std::vector<std::vector<Animation

std::pair<CompiledTileset, std::unordered_map<std::size_t, Attributes>>
importCompiledTileset(PtContext &ctx, std::ifstream &metatiles, std::ifstream &attributes,
png::image<png::index_pixel> &tilesheetPng,
std::vector<std::shared_ptr<std::ifstream>> &paletteFiles)
const png::image<png::index_pixel> &tilesheetPng,
const std::vector<std::shared_ptr<std::ifstream>> &paletteFiles,
const std::vector<std::vector<AnimationPng<png::index_pixel>>> &compiledAnims)
{
CompiledTileset tileset{};

tileset.tiles = importCompiledTiles(ctx, tilesheetPng);
tileset.palettes = importCompiledPalettes(ctx, paletteFiles);
auto attributesMap = importCompiledMetatileAttributes(ctx, attributes);
tileset.assignments = importCompiledMetatiles(ctx, metatiles, attributesMap);
//tileset.anims = importCompiledAnimations(ctx);
tileset.anims = importCompiledAnimations(ctx, compiledAnims);

return {tileset, attributesMap};
}
Expand Down Expand Up @@ -1354,8 +1355,10 @@ TEST_CASE("importCompiledTileset should import a triple layer pokeemerald tilese
std::filesystem::path paletteFile = decompileCtx.decompilerSrcPaths.primaryPalettes() / filename.str();
paletteFiles.push_back(std::make_shared<std::ifstream>(paletteFile));
}
// TODO : actually test anims import
auto [importedTileset, attributesMap] =
porytiles::importCompiledTileset(decompileCtx, metatiles, attributes, tilesheetPng, paletteFiles);
porytiles::importCompiledTileset(decompileCtx, metatiles, attributes, tilesheetPng, paletteFiles,
std::vector<std::vector<porytiles::AnimationPng<png::index_pixel>>>{});
metatiles.close();
attributes.close();
std::for_each(paletteFiles.begin(), paletteFiles.end(),
Expand Down

0 comments on commit 1c6df2c

Please sign in to comment.