Porytiles
Loading...
Searching...
No Matches
create_primary_tileset.cpp
Go to the documentation of this file.
2
3#include <memory>
4#include <string>
5
10
11namespace porytiles2 {
12
13Result<void> CreatePrimaryTileset::create(const std::string &tileset_name) const
14{
15 // 1. Check if the primary tileset already exists. If so, abort with an error message.
16 if (tileset_repo_->exists(tileset_name)) {
17 return std::unexpected{"tileset already exists"};
18 }
19
20 // 2. Initialize a `PorytilesTilesetComponent` with default assets.
21 auto maybe_porytiles_component = asset_generator_->generate();
22 if (!maybe_porytiles_component.has_value()) {
23 return std::unexpected{maybe_porytiles_component.error()};
24 }
25 auto porytiles_component = std::move(maybe_porytiles_component.value());
26
27 // 3. Initialize a blank `PorymapTilesetComponent`, to be filled later.
28 auto porymap_component = std::make_unique<PorymapTilesetComponent>();
29
30 // 4. Initialize a `Tileset` aggregate with the components.
31 Tileset tileset{tileset_name, std::move(porytiles_component), std::move(porymap_component)};
32
33 // 5. Compile the `Tileset`, generating a new modified `Tileset`.
34 auto maybe_new_tileset = compiler_->compile(tileset);
35 if (!maybe_new_tileset.has_value()) {
36 auto error_lines = maybe_new_tileset.error().details(PlainTextFormatter{});
37 std::string joined_error;
38 for (std::size_t i = 0; i < error_lines.size(); ++i) {
39 if (i > 0) {
40 joined_error += "\n";
41 }
42 joined_error += error_lines[i];
43 }
44 return std::unexpected{joined_error};
45 }
46 const auto new_tileset = std::move(maybe_new_tileset.value());
47
48 // 6. Update the source and header files.
49 // TODO: this should use some kind of capable C source modification utility
50 // if (const auto header_update_result = file_modifier_->append_tileset_declarations(tileset_name);
51 // !header_update_result.has_value()) {
52 // return std::unexpected{header_update_result.error()};
53 // }
54
55 // 7. Persist the new `Tileset` (which also caches the checksums).
56 if (const auto save_result = tileset_repo_->save(*new_tileset); !save_result.has_value()) {
57 auto error_lines = save_result.error().details(PlainTextFormatter{});
58 std::string joined_error;
59 for (std::size_t i = 0; i < error_lines.size(); ++i) {
60 if (i > 0) {
61 joined_error += "\n";
62 }
63 joined_error += error_lines[i];
64 }
65 return std::unexpected{joined_error};
66 }
67
68 return {};
69}
70
71} // namespace porytiles2
Result< void > create(const std::string &tileset_name) const
Creates the primary Tileset with the given tileset name.
TextFormatter implementation that strips all styling from text.
A complete tileset containing both Porytiles and Porymap components.
Definition tileset.hpp:14
std::expected< T, E > Result
A result with some type T on success, otherwise an error of type E.
Definition result.hpp:25