Porytiles
Loading...
Searching...
No Matches
create_primary_tileset.cpp
Go to the documentation of this file.
2
3#include <memory>
4#include <string>
5
13
14namespace porytiles {
15
16ChainableResult<void> CreatePrimaryTileset::create(const std::string &tileset_name) const
17{
18 // 1. Error if tileset already exists
19 if (metadata_provider_->exists(tileset_name)) {
20 return FormattableError{
21 std::vector<std::string>{"Cannot create tileset '{}'.", "A tileset with this name already exists."},
22 std::vector<std::vector<FormatParam>>{std::vector{FormatParam{tileset_name, Style::bold}}}};
23 }
24
25 // 2. Create a default PorytilesTilesetComponent via PrimaryTilesetCreator
27 porytiles_component,
28 creator_->create_sample_primary_porytiles_component(tileset_name),
29 void,
30 "Failed to create Porytiles source assets for '{}'.",
31 FormatParam(tileset_name, Style::bold));
32
33 // 3. Create blank PorymapTilesetComponent and wrap in Tileset
34 auto porymap_component = std::make_unique<PorymapTilesetComponent>();
35 auto tileset =
36 std::make_unique<Tileset>(tileset_name, std::move(porytiles_component), std::move(porymap_component));
37
38 /*
39 * Layer an override provider that forces the domain config values required for the sample-tileset recompile. The
40 * TilesetCreator bakes rgba_magenta into every sample tile as the transparency color, so we must force
41 * extrinsic_transparency to rgba_magenta regardless of the user's configured value. tiles/pals edit mode are
42 * forced to 'optimize' so the compiler generates fresh Porymap artifacts for the blank tileset. User infra
43 * settings (paths, etc.) are left intact because the override only touches the three fields set below.
44 */
45 auto create_override = std::make_unique<OverrideConfigProvider>(
46 ConfigScopeType::tileset, tileset_name, "create-tileset internal sample compile");
47 create_override->set_extrinsic_transparency(rgba_magenta);
48 create_override->set_tiles_edit_mode(ArtifactEditMode::optimize);
49 create_override->set_pals_edit_mode(ArtifactEditMode::optimize);
50 domain_config_->add_provider(std::move(create_override));
51
52 // 4. Compile (generates minimal valid Porymap assets from the minimal Porytiles component)
54 compiled_tileset,
55 compiler_->compile(*tileset, /*is_secondary=*/false),
56 void,
57 "Compilation failed for '{}'.",
58 FormatParam(tileset_name, Style::bold));
59
60 // 5. Persist managed state for the new tileset
61 // This creates the headers.h entry and tileset-manifest.json BEFORE saving assets
63 tileset_manager_->persist_managed_new(tileset_name),
64 void,
65 "Failed to persist managed state for '{}'.",
66 FormatParam(tileset_name, Style::bold));
67
68 // 6. Save via TilesetRepo (writes all Porymap artifacts)
70 tileset_repo_->save(*compiled_tileset),
71 void,
72 "Failed to save tileset '{}'.",
73 FormatParam(tileset_name, Style::bold));
74
75 // 7. Handle animation code wiring
76 if (!compiled_tileset->porymap_component().anims().empty()) {
77 // Tileset has animations - wire the generated code
79 tileset_manager_->wire_anim_code(tileset_name, /*is_secondary=*/false),
80 void,
81 "Failed to wire animation code for '{}'.",
82 FormatParam(tileset_name, Style::bold));
83 }
84 else {
85 // Tileset has no animations - remove any stale wiring
87 tileset_manager_->remove_wired_anim_code(tileset_name, /*is_secondary=*/false),
88 void,
89 "Failed to remove wired animation code for '{}'.",
90 FormatParam(tileset_name, Style::bold));
91 }
92
93 return {};
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.
#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.
ChainableResult< void > create(const std::string &tileset_name) const
Creates a new primary Tileset with the given name.
virtual void add_provider(std::unique_ptr< ConfigProvider > provider)=0
Prepends a ConfigProvider to the provider chain at highest priority.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
virtual ChainableResult< void > wire_anim_code(const std::string &tileset_name, bool is_secondary) const =0
Wires animation code for a tileset that already has its manifest persisted.
virtual ChainableResult< void > persist_managed_new(const std::string &tileset_name, bool is_secondary=false) const =0
Persists managed state for a new tileset.
virtual ChainableResult< void > remove_wired_anim_code(const std::string &tileset_name, bool is_secondary) const =0
Removes wired animation code for a tileset from the project.
static const Style bold
Bold text formatting.
ChainableResult< std::unique_ptr< Tileset > > compile(const Tileset &tileset, bool is_secondary=false, const Tileset *paired_primary=nullptr) const
Compiles the given Tileset, producing a new Tileset with compiled Porymap assets.
ChainableResult< std::unique_ptr< PorytilesTilesetComponent > > create_sample_primary_porytiles_component(const std::string &tileset_name) const
Creates a new basic primary PorytilesTilesetComponent with some default assets.
virtual bool exists(const std::string &tileset_name) const =0
Checks whether a tileset exists in the backing store for the given tileset name.
ChainableResult< void > save(const Tileset &tileset) const
Persists a given Tileset and caches new artifact checksums.
constexpr Rgba32 rgba_magenta
Definition rgba32.hpp:138
@ optimize
The Porytiles1 behavior; artifact is cleared and packed optimally.
@ tileset
Configuration scoped to a specific tileset.