Porytiles
Loading...
Searching...
No Matches
compile_primary_tileset.cpp
Go to the documentation of this file.
2
3#include <expected>
4#include <memory>
5
8
9namespace porytiles2 {
10
11ChainableResult<void> CompilePrimaryTileset::compile(const std::string &tileset_name) const
12{
13 // 1. Check if the primary tileset exists. If not, abort with error.
14 if (!tileset_repo_->exists(tileset_name)) {
16 FormattableError{"tileset '{}' does not exist", FormatParam{tileset_name, Style::bold}}};
17 }
18
19 // 2. Load the tileset into a `Tileset` aggregate.
20 auto maybe_tileset = tileset_repo_->load(tileset_name);
21 if (!maybe_tileset.has_value()) {
23 FormattableError{fmt::format("failed to load tileset '{}'", tileset_name)}, maybe_tileset};
24 }
25 const auto tileset = std::move(maybe_tileset.value());
26
27 // 3. If `PorytilesTilesetComponent` is empty, bail with error.
28 if (tileset->porytiles_component().is_empty()) {
29 return ChainableResult<void>{FormattableError{"PorytilesTilesetComponent was empty"}};
30 }
31
32 // 4. If `PorymapTilesetComponent` is not empty, compare with cached checksums in `artifact_checksums.json`. If any
33 // differ, bail with the message "unimported changes present in Porymap asset X."
34 if (!tileset->porymap_component().is_empty()) {
35 const auto porymap_keys = tileset_repo_->key_provider().get_porymap_artifact_keys(tileset_name);
36 const auto mismatched_keys =
37 tileset_repo_->checksum_provider().find_unsynced_tileset_artifacts(tileset_name, porymap_keys);
38 if (!mismatched_keys.empty()) {
40 FormattableError{"unimported changes present in Porymap assets: TODO keys here"}};
41 }
42 }
43
44 // 5. If all `PorytilesTilesetComponent` checksums match those cached in `artifact_checksums.json`, bail with the
45 // message "nothing to do."
46 const auto porytiles_keys = tileset_repo_->key_provider().get_porytiles_artifact_keys(tileset_name);
47 if (tileset_repo_->checksum_provider().all_checksums_tileset_match(tileset_name, porytiles_keys)) {
48 // TODO: display a nothing_to_do message to the user
49 return {};
50 }
51
52 // 6. Compile the `Tileset`, generating a new modified `Tileset`.
53 auto maybe_new_tileset = compiler_->compile(*tileset);
54 if (!maybe_new_tileset.has_value()) {
55 return ChainableResult<void>{FormattableError{maybe_new_tileset.error()}};
56 }
57 const auto new_tileset = std::move(maybe_new_tileset.value());
58
59 // 7. Persist the `Tileset` (which also caches the checksums).
60 if (const auto save_result = tileset_repo_->save(*new_tileset); !save_result.has_value()) {
61 return ChainableResult<void>{FormattableError{save_result.error()}};
62 }
63
64 return {};
65}
66
67} // namespace porytiles2
virtual std::vector< ArtifactKey > find_unsynced_tileset_artifacts(const std::string &tileset_name, const std::vector< ArtifactKey > &artifact_keys) const
Finds all artifacts for the given Tileset with unsynced changes compared to cached checksums.
virtual bool all_checksums_tileset_match(const std::string &tileset_name, const std::vector< ArtifactKey > &artifact_keys) const
Checks if all artifact checksums for the given Tileset match their cached values.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
ChainableResult< void > compile(const std::string &tileset_name) const
Compiles the primary Tileset with the given tileset name.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
ChainableResult< std::unique_ptr< Tileset > > compile(const Tileset &tileset)
virtual std::vector< ArtifactKey > get_porymap_artifact_keys(const std::string &tileset_name) const
Gets the keys for all Porymap artifacts present in the given Tileset.
virtual std::vector< ArtifactKey > get_porytiles_artifact_keys(const std::string &tileset_name) const
Gets the keys for all Porytiles artifacts present in the given Tileset.
ChainableResult< void > save(const Tileset &tileset) const
Persists a given Tileset and caches new artifact checksums.
bool exists(const std::string &name) const
Checks if the given Tileset exists in the backing store.
ChainableResult< std::unique_ptr< Tileset > > load(const std::string &name) const
Loads an existing Tileset from storage.
const ArtifactChecksumProvider & checksum_provider() const
Gets a reference to the ArtifactChecksumProvider for this repo.
const TilesetArtifactKeyProvider & key_provider() const
Gets a reference to the TilesetArtifactKeyProvider for this repo.
@ bold
Bold text formatting.