Porytiles
Loading...
Searching...
No Matches
import_primary_tileset.cpp
Go to the documentation of this file.
2
3#include <memory>
4#include <string>
5
8
9namespace porytiles2 {
10
11Result<void> ImportPrimaryTileset::import(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)) {
15 return std::unexpected{fmt::format("tileset {} does not exist", tileset_name)};
16 }
17
18 // 2. Load the tileset into a `Tileset` aggregate.
19 auto maybe_tileset = tileset_repo_->load(tileset_name);
20 if (!maybe_tileset.has_value()) {
21 // TODO: hook up ChainableError here
22 return std::unexpected{"failed to load tileset"};
23 }
24 const auto tileset = std::move(maybe_tileset.value());
25
26 // 3. If `PorymapTilesetComponent` is empty, bail with error.
27 if (tileset->porymap_component().is_empty()) {
28 return std::unexpected{"PorymapTilesetComponent was empty"};
29 }
30
31 // 4. If `PorytilesTilesetComponent` is not empty, compare with cached checksums in `artifact_checksums.json`. If
32 // any differ, bail with the message "uncompiled changes present in Porytiles asset X."
33 if (!tileset->porytiles_component().is_empty()) {
34 const auto porytiles_keys = tileset_repo_->key_provider().get_porytiles_artifact_keys(tileset_name);
35 const auto mismatched_keys =
36 tileset_repo_->checksum_provider().find_unsynced_tileset_artifacts(tileset_name, porytiles_keys);
37 if (!mismatched_keys.empty()) {
38 return std::unexpected{"uncompiled changes present in Porytiles assets: TODO keys here"};
39 }
40 }
41
42 // 5. If all `PorymapTilesetComponent` checksums match those cached in `artifact_checksums.json`, bail with the
43 // message "nothing to do."
44 const auto porymap_keys = tileset_repo_->key_provider().get_porymap_artifact_keys(tileset_name);
45 if (tileset_repo_->checksum_provider().all_checksums_tileset_match(tileset_name, porymap_keys)) {
46 // TODO: display a nothing_to_do message to the user
47 return {};
48 }
49
50 // 6. Decompile the `PorymapTilesetComponent`, generating a new `PorytilesTilesetComponent`.
51 // TODO: The resulting PorytilesTilesetComponent may be incomplete. E.g., the user may have specified palette
52 // overrides or hints; they will be present on disk. We don't want to clobber them when saving the decompiled
53 // component. So we'll need to pull them from the original component and inject them into this one before
54 // persisting.
55
56 // 7. Perform a patch build.
57 // TODO: add this
58
59 // 8. Persist the `Tileset` (which also caches the checksums).
60 if (const auto save_result = tileset_repo_->save(*tileset); !save_result.has_value()) {
61 auto error_lines = save_result.error().details(PlainTextFormatter{});
62 std::string joined_error;
63 for (std::size_t i = 0; i < error_lines.size(); ++i) {
64 if (i > 0) {
65 joined_error += "\n";
66 }
67 joined_error += error_lines[i];
68 }
69 return std::unexpected{joined_error};
70 }
71
72 return {};
73}
74
75} // namespace porytiles2
Result< void > import(const std::string &tileset_name) const
Imports the primary Tileset with the given tileset name.
TextFormatter implementation that strips all styling from text.
std::expected< T, E > Result
A result with some type T on success, otherwise an error of type E.
Definition result.hpp:25