7#include "nlohmann/json.hpp"
17std::filesystem::path artifacts_file(
const std::filesystem::path &project_root,
const std::string &tileset_name)
19 return project_root /
"porytiles" /
"tilesets" / tileset_name /
"tileset-manifest.json";
22constexpr std::string_view porytiles_managed_callback_prefix =
"InitTilesetAnim_PorytilesManaged_";
24[[nodiscard]]
bool is_porytiles_managed_callback(
const std::optional<std::string> &callback)
26 if (!callback.has_value()) {
29 return callback->starts_with(porytiles_managed_callback_prefix);
34 const std::string &tileset_name,
35 const std::string &bin_path_base,
36 std::size_t metatile_attr_size)
42 "Failed to append graphics INCBIN declarations for '{}'.",
49 "Failed to append metatiles INCBIN declarations for '{}'.",
61 const auto input_path = artifacts_file(project_root_, tileset_name);
63 if (!std::filesystem::exists(input_path)) {
67 std::ifstream file{input_path};
68 nlohmann::json json_data;
71 const auto version = json_data[
"version"].get<std::uint32_t>();
72 const auto imported = json_data[
"imported"].get<
bool>();
77 json_data[
".tiles"].get<std::string>(),
78 json_data[
".palettes"].get<std::string>(),
79 json_data[
".metatiles"].get<std::string>(),
80 json_data[
".metatileAttributes"].get<std::string>(),
81 json_data[
".callback"].get<std::string>()};
89 const auto manifest_file = artifacts_file(project_root_, tileset_name);
90 std::filesystem::create_directories(manifest_file.parent_path());
91 std::ofstream file{manifest_file};
93 nlohmann::json json_data;
94 json_data[
"version"] = artifacts.
version();
95 json_data[
"imported"] = artifacts.
imported();
98 json_data[
".tiles"] = artifacts.
tiles();
99 json_data[
".palettes"] = artifacts.
palettes();
100 json_data[
".metatiles"] = artifacts.
metatiles();
102 json_data[
".callback"] = artifacts.
callback();
105 file << json_data.dump(2);
111 return std::filesystem::exists(artifacts_file(project_root_, tileset_name));
121 "Failed to read metadata for tileset '{}'.",
125 constexpr std::uint32_t version = 1;
126 const std::optional<std::string> original_callback_value = metadata.callback_func();
130 metadata.tiles_var(),
131 metadata.palettes_var(),
132 metadata.metatiles_var(),
133 metadata.metatile_attributes_var(),
134 original_callback_value.value_or(
"NULL")};
137 write(tileset_name, artifacts);
140 const bool is_secondary = metadata.is_secondary();
147 "Failed to get tileset bin path config for '{}'.",
155 "Failed to get metatile attribute size config for '{}'.",
159 auto incbin_result = append_incbin_declarations(incbin_appender_, tileset_name, bin_path_base, metatile_attr_size);
160 if (!incbin_result.has_value()) {
161 return incbin_result;
176 "Failed to create tileset struct in headers.h for '{}'.",
183 constexpr std::uint32_t version = 1;
192 "Failed to get tileset bin path config for '{}'.",
200 "Failed to get metatile attribute size config for '{}'.",
204 auto incbin_result = append_incbin_declarations(incbin_appender_, tileset_name, bin_path_base, metatile_attr_size);
205 if (!incbin_result.has_value()) {
206 return incbin_result;
220 "Failed to get wire_anim_code config for '{}'.",
224 std::vector<std::string> remark_text;
225 remark_text.emplace_back(
"Config 'tileset.animations.wire_anim_code' is false, removing any existing wiring.");
226 remark_text.emplace_back(
"");
227 remark_text.append_range(should_wire.prettify(diag_->
formatter()));
228 diag_->
remark(
"wire-tileset-animation", remark_text);
236 "Failed to wire tileset_anims include/declaration for '{}'.",
241 const std::string callback_name =
"InitTilesetAnim_PorytilesManaged_" + shorthand;
247 "Failed to update callback in headers.h for '{}'.",
260 "Failed to remove animation includes for '{}'.",
269 "Failed to read metadata for '{}'.",
272 const auto ¤t_callback = metadata.callback_func();
273 if (is_porytiles_managed_callback(current_callback)) {
277 "Failed to update callback to NULL for '{}'.",
#define PT_TRY_ASSIGN_CHAIN_ERR(var, expr, return_type,...)
Unwraps a ChainableResult, chaining a new error message on failure.
#define PT_TRY_CALL_CHAIN_ERR(expr, return_type,...)
Unwraps a void ChainableResult, chaining a new error message on failure.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
Appends INCBIN declarations for Porytiles-managed tileset assets.
ChainableResult< void > append_graphics_declarations(const std::string &tileset_name, const std::string &bin_path_base, std::size_t num_palettes) const
Appends INCBIN declarations for a Porytiles-managed tileset to graphics.h.
ChainableResult< void > append_metatiles_declarations(const std::string &tileset_name, const std::string &bin_path_base, std::size_t metatile_attr_size) const
Appends INCBIN declarations for a Porytiles-managed tileset to metatiles.h.
ChainableResult< ConfigValue< std::string > > tileset_paths_secondary_bin(ConfigScopeType type, const std::string &scope) const
ChainableResult< ConfigValue< std::size_t > > metatile_attr_size(ConfigScopeType type, const std::string &scope) const
ChainableResult< ConfigValue< std::string > > tileset_paths_primary_bin(ConfigScopeType type, const std::string &scope) const
ChainableResult< ConfigValue< bool > > tileset_animations_wire_anim_code(ConfigScopeType type, const std::string &scope) const
void write(const std::string &tileset_name, const TilesetManifest &artifacts) const
Writes an TilesetManifest object to the porytiles utility directory.
ChainableResult< void > persist_managed_new(const std::string &tileset_name, bool is_secondary=false) const override
Persists managed state for a new tileset.
ChainableResult< TilesetManifest > read(const std::string &tileset_name) const
Reads an TilesetManifest object from the porytiles utility directory.
ChainableResult< void > persist_managed_existing(const std::string &tileset_name) const override
Persists managed state for an existing tileset.
ChainableResult< void > remove_wired_anim_code(const std::string &tileset_name, bool is_secondary) const override
Removes wired animation code for a tileset from the project.
ChainableResult< void > wire_anim_code(const std::string &tileset_name, bool is_secondary) const override
Wires animation code for a tileset that already has its manifest persisted.
bool is_porytiles_managed(const std::string &tileset_name) const override
Checks whether a tileset has an tileset-manifest.json file.
ChainableResult< void > wire_include_for_tileset(const std::string &tileset_name, bool is_secondary) const
Adds an #include directive for the tileset's generated animation code.
ChainableResult< void > remove_include_for_tileset(const std::string &tileset_name, bool is_secondary) const
Removes the #include directive for the tileset's generated animation code.
static const Style bold
Bold text formatting.
Stores manifest metadata for Porytiles-managed tilesets.
static TilesetManifest for_created_tileset(std::uint32_t version)
Creates a TilesetManifest for a tileset created from scratch by Porytiles.
bool imported() const
Returns whether this tileset was imported from vanilla pokeemerald.
const std::string & callback() const
Returns the original .callback field value.
const std::string & tiles() const
Returns the original .tiles field value.
const std::string & metatile_attributes() const
Returns the original .metatileAttributes field value.
std::uint32_t version() const
Returns the schema version for format migration support.
const std::string & palettes() const
Returns the original .palettes field value.
const std::string & metatiles() const
Returns the original .metatiles field value.
virtual void remark(const std::string &tag, const std::vector< std::string > &lines) const =0
Display a tagged remark message.
const TextFormatter & formatter() const
constexpr std::size_t num_pals
std::string extract_tileset_shorthand(const std::string &tileset_name)
Extracts the Pascal-case tileset short name from the full name.
@ tileset
Configuration scoped to a specific tileset.
Utility functions for string manipulation and formatting.