Porytiles
Loading...
Searching...
No Matches
debug_commands.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include <unistd.h>
7
8#include "CLI/CLI.hpp"
9#include "fruit/fruit.h"
10
33
34#include "command.hpp"
35
36class DebugPrimaryCompileCommand final : public Command {
37 public:
38 explicit DebugPrimaryCompileCommand(CLI::App &parent_app)
39 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup}
40 {
41 CLI::App &cmd = get_app();
42 cmd.add_option("<tileset-name>", tileset_name_, "Name of the tileset to compile")->required();
43 }
44
45 void Run() override
46 {
47 using namespace porytiles2;
48
49 /*
50 * TODO: once we have more compilation code finished, we should come back and do more dependency injection via
51 * Fruit.
52 */
53 // Use Fruit DI to inject TextFormatter based on no_color flag
54 const bool no_color = !isatty(STDERR_FILENO); // Disable color when stderr is not a terminal
55 fruit::Injector injector{di::get_formatter_component, no_color};
56 auto text_formatter = injector.get<TextFormatter *>();
57
58 // Manually create other services (not yet using DI for these)
59 std::unique_ptr<UserDiagnostics> diag = std::make_unique<StderrStyledUserDiagnostics>(text_formatter);
60 std::unique_ptr<TilePrinter> tile_printer = std::make_unique<AsciiTilePrinter>(text_formatter);
61
62 // Setup layered configuration
63 ProjectTilesetArtifactKeyProvider key_provider{"."};
64 std::vector<std::unique_ptr<ConfigProvider>> providers{};
65 providers.push_back(std::make_unique<YamlFileProvider>(text_formatter, ".", key_provider));
66 providers.push_back(std::make_unique<DefaultProvider>());
67 LazyLayeredConfig config{text_formatter, std::move(providers)};
68
69 // Initialize stateless services
70 PngRgbaImageLoader png_rgba_loader{};
71 PngIndexedImageLoader png_indexed_loader{};
72 PngRgbaImageSaver png_rgba_saver{};
73 PngIndexedImageSaver png_indexed_saver{};
74 JascPalLoader jasc_loader{};
75 JascPalSaver jasc_saver{};
76
77 // Setup primary compiler
78 PrimaryTilesetCompiler compiler{&config, text_formatter, diag.get(), tile_printer.get()};
79
80 // Setup the tileset repository
81 ProjectTilesetArtifactReader artifact_reader{&png_rgba_loader, &png_indexed_loader, &jasc_loader};
82 ProjectTilesetArtifactWriter artifact_writer{&config, ".", &png_rgba_saver, &png_indexed_saver, &jasc_saver};
83 // We already set this up earlier for the Yaml config provider
84 // ProjectTilesetArtifactKeyProvider key_provider{"."};
85 ProjectArtifactChecksumProvider checksum_provider{&key_provider};
86 TilesetRepo repo{&checksum_provider, &key_provider, &artifact_reader, &artifact_writer};
87
88 // Load the tileset
89 auto maybe_tileset = repo.load(tileset_name_);
90 if (!maybe_tileset.has_value()) {
91 diag->fatal(maybe_tileset);
92 return;
93 }
94 const auto tileset = std::move(maybe_tileset.value());
95
96 // Compile the tileset
97 auto compile_result = compiler.compile_patch_tiles_fixed_pals_fixed(*tileset);
98 if (!compile_result.has_value()) {
99 const auto fail_result = ChainableResult<std::unique_ptr<Tileset>>{
100 FormattableError{"failed to compile tileset '{}'", FormatParam{tileset_name_, Style::bold}},
101 compile_result};
102 diag->fatal(fail_result);
103 return;
104 }
105 const auto new_tileset = std::move(compile_result.value());
106
107 // Multi-line fatal print demo
108 // FormattableError proximate{std::vector<std::string>{
109 // "this is line 1 of the error", "this is line 2 of the error", "this is line 3 of the error"}};
110 // FormattableError middle{std::vector<std::string>{
111 // "this is line 1 of the error", "this is line 2 of the error", "this is line 3 of the error"}};
112 // FormattableError root{std::vector<std::string>{
113 // "this is line 1 of the error", "this is line 2 of the error", "this is line 3 of the error"}};
114 // ChainableResult<void> root_result{root};
115 // ChainableResult<void> middle_result{middle, root_result};
116 // ChainableResult<void> prox_result{proximate, middle_result};
117 // diag->fatal(prox_result);
118
119 // Save the tileset back
120 const auto new_tileset_save_result = repo.save(*new_tileset);
121 if (!new_tileset_save_result.has_value()) {
122 diag->fatal(new_tileset_save_result);
123 return;
124 }
125 }
126
127 private:
128 static constexpr auto kCommandName = "debug-compile-primary";
129 static constexpr auto kCommandDesc =
130 "Load a tileset, run it through the compile-primary service, and write it back.";
131 static constexpr auto kCommandGroup = "COMMANDS";
132 std::string tileset_name_;
133};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:18
CLI::App & get_app() const
Definition command.hpp:45
DebugPrimaryCompileCommand(CLI::App &parent_app)
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
An implementation of FilePalLoader that loads palettes from JASC-PAL (Paintshop Pro) pal files.
An implementation of FilePalSaver that saves palettes to JASC-PAL (Paintshop Pro) pal files.
A Config implementation that lazily pulls a config value by consulting multiple priority-ordered back...
An image loader that reads PNG files to create an Image with an index pixel type.
An image saver that saves PNG files from an Image with an index pixel type.
An image loader that reads PNG files to create an Image with an Rgba32 pixel type.
An image saver that saves PNG files from an Image with an Rgba32 pixel type.
Service that compiles a primary Tileset.
Provides a pokeemerald project filesystem-based implementation for TilesetArtifactKeyProvider.
Provides a filesystem-based implementation for TilesetArtifactReader.
Provides a filesystem-based implementation for TilesetArtifactWriter.
Abstract base class for applying text styling with context-aware formatting.
Repository interface for the Tileset aggregate root.
ChainableResult< std::unique_ptr< Tileset > > load(const std::string &name) const
Loads an existing Tileset from storage.