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 using the resolved schema's binary layout
39 attributes,
40 parse_metatile_attributes(project_root_ / artifact_paths.metatile_attributes_path(), *schema_),
41 std::unique_ptr<PorymapTilesetComponent>,
42 "Failed to parse metatile_attributes.bin.");
43
44 for (auto &attribute : attributes) {
45 porymap_component->push_back_attribute(std::move(attribute));
46 }
47
48 // Step 4: Load tiles.png (strip all extensions like .4bpp.smol, then add .png)
49 auto tiles_png_path = strip_all_extensions(artifact_paths.tiles_path());
50 tiles_png_path += ".png";
51
53 tiles_image,
54 load_indexed_png(project_root_ / tiles_png_path, *png_loader_),
55 std::unique_ptr<PorymapTilesetComponent>,
56 "Failed to load tiles.png.");
57
58 porymap_component->tiles_png(*tiles_image);
59
60 // Step 5: Load palettes from the discovered palette paths
61 const auto &palette_paths = artifact_paths.palette_paths();
62 for (std::size_t i = 0; i < palette_paths.size() && i < palette::num_palettes; ++i) {
63 // Convert .gbapal path to .pal by stripping all extensions and adding .pal
64 auto palette_path = strip_all_extensions(palette_paths[i]);
65 palette_path += ".pal";
66
68 palette,
69 load_porymap_palette(project_root_ / palette_path, *palette_loader_),
70 std::unique_ptr<PorymapTilesetComponent>,
71 format_->format("Failed to load palette {}.", FormatParam{palette_filename(i), Style::bold}));
72
73 porymap_component->set_palette(i, std::move(palette));
74 }
75
76 // Step 6: Import animations using ProjectVanillaAnimImporter
77 // Animation import failure is non-fatal - tileset may not have animations
78 ProjectVanillaAnimImporter anim_importer{project_root_, format_, diag_};
79
80 auto anims_result = anim_importer.import_animations(tileset_name);
81 if (!anims_result.has_value()) {
83 FormattableError{"Failed to import animations for '{}'.", FormatParam{tileset_name, Style::bold}},
84 anims_result};
85 }
86 for (auto &anim : anims_result.value() | std::views::values) {
87 porymap_component->add_anim(std::move(anim));
88 }
89
90 // Note: The returned animations will have frames populated but NO key frames.
91 // Key frame extraction is done later by AnimDecompiler in the domain layer.
92
93 return porymap_component;
94}
95
96} // 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:57
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 num_palettes
Definition palette.hpp:21
ChainableResult< Palette< Rgba32, palette::max_size > > load_porymap_palette(const std::filesystem::path &path, const FilePaletteLoader &loader)
Loads a Porymap palette file (e.g., 00.pal).
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::vector< MetatileAttribute > > parse_metatile_attributes(const std::filesystem::path &path, const Schema &schema)
Parses a metatile_attributes.bin file according to a metatile attribute schema.
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.