Porytiles
Loading...
Searching...
No Matches
create_secondary_tileset.cpp
Go to the documentation of this file.
2
3#include <memory>
4#include <string>
5
12
13namespace porytiles {
14
15ChainableResult<void> CreateSecondaryTileset::create(const std::string &tileset_name) const
16{
17 // 1. Error if tileset already exists
18 if (metadata_provider_->exists(tileset_name)) {
19 return FormattableError{
20 std::vector<std::string>{"Cannot create tileset '{}'.", "A tileset with this name already exists."},
21 std::vector<std::vector<FormatParam>>{std::vector{FormatParam{tileset_name, Style::bold}}}};
22 }
23
24 // 2. Resolve partner primary tileset
25 PT_UNWRAP_TILESET_CONFIG_PTR(app_config_, primary_pairing_mode, tileset_name, void);
26 PT_UNWRAP_TILESET_CONFIG_PTR(app_config_, primary_pairing_partners, tileset_name, void);
27
29 paired_primary,
31 tileset_name,
32 primary_pairing_mode,
33 primary_pairing_partners,
34 tileset_repo_,
35 metadata_provider_,
36 layout_metadata_provider_,
37 tileset_manager_,
38 diag_),
39 void,
40 "Failed to resolve partner primary for secondary '{}'.",
41 FormatParam(tileset_name, Style::bold));
42
43 // 3. Create a default secondary PorytilesTilesetComponent via TilesetCreator
45 porytiles_component,
46 creator_->create_sample_secondary_porytiles_component(tileset_name),
47 void,
48 "Failed to create Porytiles source assets for '{}'.",
49 FormatParam(tileset_name, Style::bold));
50
51 // 4. Create blank PorymapTilesetComponent and wrap in Tileset
52 auto porymap_component = std::make_unique<PorymapTilesetComponent>();
53 auto tileset =
54 std::make_unique<Tileset>(tileset_name, std::move(porytiles_component), std::move(porymap_component));
55
56 /*
57 * Layer an override provider that forces the domain config values required for the sample-tileset recompile. The
58 * TilesetCreator bakes rgba_magenta into every sample tile as the transparency color, so we must force
59 * extrinsic_transparency to rgba_magenta regardless of the user's configured value. tiles/pals edit mode are
60 * forced to 'optimize' so the compiler generates fresh Porymap artifacts for the blank tileset. User infra
61 * settings (paths, etc.) are left intact because the override only touches the three fields set below.
62 */
63 auto create_override = std::make_unique<OverrideConfigProvider>(
64 ConfigScopeType::tileset, tileset_name, "create-tileset internal sample compile");
65 create_override->set_extrinsic_transparency(rgba_magenta);
66 create_override->set_tiles_edit_mode(ArtifactEditMode::optimize);
67 create_override->set_pals_edit_mode(ArtifactEditMode::optimize);
68 domain_config_->add_provider(std::move(create_override));
69
70 // 5. Compile (generates minimal valid Porymap assets from the minimal Porytiles component)
72 compiled_tileset,
73 compiler_->compile(*tileset, /*is_secondary=*/true, paired_primary.get()),
74 void,
75 "Compilation failed for '{}'.",
76 FormatParam(tileset_name, Style::bold));
77
78 // 6. Persist managed state for the new secondary tileset
80 tileset_manager_->persist_managed_new(tileset_name, /*is_secondary=*/true),
81 void,
82 "Failed to persist managed state for '{}'.",
83 FormatParam(tileset_name, Style::bold));
84
85 // 7. Save via TilesetRepo (writes all Porymap artifacts)
87 tileset_repo_->save(*compiled_tileset),
88 void,
89 "Failed to save tileset '{}'.",
90 FormatParam(tileset_name, Style::bold));
91
92 // 8. Handle animation code wiring
93 if (!compiled_tileset->porymap_component().anims().empty()) {
95 tileset_manager_->wire_anim_code(tileset_name, /*is_secondary=*/true),
96 void,
97 "Failed to wire animation code for '{}'.",
98 FormatParam(tileset_name, Style::bold));
99 }
100 else {
102 tileset_manager_->remove_wired_anim_code(tileset_name, /*is_secondary=*/true),
103 void,
104 "Failed to remove wired animation code for '{}'.",
105 FormatParam(tileset_name, Style::bold));
106 }
107
108 return {};
109}
110
111} // 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 secondary 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_secondary_porytiles_component(const std::string &tileset_name) const
Creates a new basic secondary 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.
ChainableResult< std::unique_ptr< Tileset > > resolve_partner_primary(const std::string &tileset_name, const ConfigValue< PrimaryPairingMode > &pairing_mode, const ConfigValue< std::vector< std::string > > &partners, const TilesetRepo *tileset_repo, const TilesetMetadataProvider *metadata_provider, const LayoutMetadataProvider *layout_metadata_provider, const PorytilesTilesetManager *tileset_manager, const UserDiagnostics *diag)
Resolves the partner primary tileset for a secondary tileset.
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.
#define PT_UNWRAP_TILESET_CONFIG_PTR(ptr, config, tileset_name, return_type)
Unwraps a tileset-scoped config value via pointer access, returning early if the value is not availab...