Porytiles
Loading...
Searching...
No Matches
project_primary_tileset_importer.cpp
Go to the documentation of this file.
2
3#include <format>
4
10
11namespace porytiles {
12
13ChainableResult<std::unique_ptr<PorymapTilesetComponent>>
15{
16 auto porymap_component = std::make_unique<PorymapTilesetComponent>();
17
18 // Step 1: Get artifact paths from metadata provider
19 // This resolves INCBIN variable names to actual file paths by parsing graphics.h, metatiles.h, etc.
21 artifact_paths,
22 metadata_provider_->artifact_paths_for(tileset_name),
23 std::unique_ptr<PorymapTilesetComponent>,
24 format_->format("Failed to get artifact paths for tileset '{}'.", FormatParam{tileset_name, Style::bold}));
25
26 // Step 2: Parse metatiles.bin
28 metatile_entries,
29 parse_metatiles_bin(project_root_ / artifact_paths.metatiles_path()),
30 std::unique_ptr<PorymapTilesetComponent>,
31 "Failed to parse metatiles.bin.");
32
33 for (auto &entry : metatile_entries) {
34 porymap_component->push_back_tilemap_entry(std::move(entry));
35 }
36
37 // Step 3: Parse metatile_attributes.bin (dispatch on attribute size for correct format)
39 attributes,
40 metatile_attr_size_ == attr::bytes_per_attr_firered
41 ? parse_firered_metatile_attributes(project_root_ / artifact_paths.metatile_attributes_path())
42 : parse_emerald_metatile_attributes(project_root_ / artifact_paths.metatile_attributes_path()),
43 std::unique_ptr<PorymapTilesetComponent>,
44 "Failed to parse metatile_attributes.bin.");
45
46 for (auto &attr : attributes) {
47 porymap_component->push_back_attribute(std::move(attr));
48 }
49
50 // Step 4: Load tiles.png (strip all extensions like .4bpp.smol, then add .png)
51 auto tiles_png_path = strip_all_extensions(artifact_paths.tiles_path());
52 tiles_png_path += ".png";
53
55 tiles_image,
56 load_indexed_png(project_root_ / tiles_png_path, *png_loader_),
57 std::unique_ptr<PorymapTilesetComponent>,
58 "Failed to load tiles.png.");
59
60 porymap_component->tiles_png(*tiles_image);
61
62 // Step 5: Load palettes from the discovered palette paths
63 const auto &palette_paths = artifact_paths.palette_paths();
64 for (std::size_t i = 0; i < palette_paths.size() && i < pal::num_pals; ++i) {
65 // Convert .gbapal path to .pal by stripping all extensions and adding .pal
66 auto pal_path = strip_all_extensions(palette_paths[i]);
67 pal_path += ".pal";
68
70 palette,
71 load_porymap_palette(project_root_ / pal_path, *pal_loader_),
72 std::unique_ptr<PorymapTilesetComponent>,
73 format_->format("Failed to load palette {}.", FormatParam{pal_filename(i), Style::bold}));
74
75 porymap_component->set_pal(i, std::move(palette));
76 }
77
78 // Step 6: Import animations using ProjectVanillaAnimImporter
79 // Animation import failure is non-fatal - tileset may not have animations
80 ProjectVanillaAnimImporter anim_importer{project_root_, format_, diag_};
81
82 auto anims_result = anim_importer.import_animations(tileset_name);
83 if (!anims_result.has_value()) {
85 FormattableError{"Failed to import animations for '{}'.", FormatParam{tileset_name, Style::bold}},
86 anims_result};
87 }
88 for (auto &anim : anims_result.value() | std::views::values) {
89 porymap_component->add_anim(std::move(anim));
90 }
91
92 // Note: The returned animations will have frames populated but NO key frames.
93 // Key frame extraction is done later by AnimDecompiler in the domain layer.
94
95 return porymap_component;
96}
97
98} // namespace porytiles
#define PT_TRY_ASSIGN_CHAIN_ERR(var, expr, return_type,...)
Unwraps a ChainableResult, chaining a new error message on failure.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
ChainableResult< std::unique_ptr< PorymapTilesetComponent > > import_porymap_component_from_vanilla(const std::string &tileset_name) const override
Reads vanilla Porymap artifacts from a pokeemerald project into a PorymapTilesetComponent.
ChainableResult< ProjectTilesetArtifactPaths > artifact_paths_for(const std::string &tileset_name) const
Resolves artifact paths for a tileset by parsing INCBIN declarations.
Imports vanilla animation metadata and frame data as IndexPixel tiles.
static const Style bold
Bold text formatting.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
constexpr std::size_t bytes_per_attr_firered
constexpr std::size_t num_pals
Definition palette.hpp:21
ChainableResult< std::vector< MetatileAttribute > > parse_firered_metatile_attributes(const std::filesystem::path &path)
Parses a metatile_attributes.bin file for FireRed format.
ChainableResult< Palette< Rgba32, pal::max_size > > load_porymap_palette(const std::filesystem::path &path, const FilePalLoader &loader)
Loads a Porymap palette file (e.g., 00.pal).
ChainableResult< std::vector< MetatileAttribute > > parse_emerald_metatile_attributes(const std::filesystem::path &path)
Parses a metatile_attributes.bin file for Emerald format.
ChainableResult< std::vector< TilemapEntry > > parse_metatiles_bin(const std::filesystem::path &path)
Parses a metatiles.bin file into TilemapEntry objects.
std::filesystem::path strip_all_extensions(const std::filesystem::path &path)
Strips all extensions from a path, returning the path with only the stem.
ChainableResult< std::unique_ptr< Image< IndexPixel > > > load_indexed_png(const std::filesystem::path &path, const PngIndexedImageLoader &loader)
Loads an indexed PNG file (e.g., tiles.png).
Utility functions for string manipulation and formatting.