Porytiles
Loading...
Searching...
No Matches
lazy_layered_config.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <functional>
5#include <map>
6#include <memory>
7#include <unordered_map>
8#include <vector>
9
10#include "gsl/pointers"
11
22
23namespace porytiles2 {
24
25/*
26 * NOTE: DO NOT EDIT THIS FILE DIRECTLY. It is AUTO-GENERATED from config_schema.yaml.
27 * To add new config values or make other changes, edit config_schema.yaml and regenerate using
28 * Scripts/generate_config.py.
29 */
30
31/*
32 * TODO: implement a better system for configuration validation. Right now, we rely on each ConfigProvider
33 * implementation to provide validation internally before returning LayerValue::invalid. However, this means certain
34 * config validations will have to be repeated multiple times in every single ConfigProvider impl. This is fine for some
35 * validation. E.g. CommandLineProvider and YamlProvider might have different format validation needs when parsing from
36 * source. Hence they may have different parse_x implementations. However, some values need generic validation, e.g.
37 * num_pals_primary must be less than num_pals_total.
38 */
39
51class LazyLayeredConfig final : public DomainConfig, public AppConfig, public InfraConfig {
52 public:
69 gsl::not_null<TextFormatter *> format, std::vector<std::unique_ptr<ConfigProvider>> &&providers)
70 : format_{format}, providers_{std::move(providers)}
71 {
72 }
73
85 explicit LazyLayeredConfig(std::vector<std::unique_ptr<ConfigProvider>> &&providers)
86 : owned_format_{std::make_unique<PlainTextFormatter>()}, format_{owned_format_.get()},
87 providers_{std::move(providers)}
88 {
89 }
90
91 protected:
92 /*
93 * Domain Config Raw Methods
94 */
95
97 num_tiles_primary_raw(const std::string &tileset) const override;
98
100 num_tiles_total_raw(const std::string &tileset) const override;
101
103 num_metatiles_primary_raw(const std::string &tileset) const override;
104
106 num_metatiles_total_raw(const std::string &tileset) const override;
107
109 num_pals_primary_raw(const std::string &tileset) const override;
110
112 num_pals_total_raw(const std::string &tileset) const override;
113
115 max_map_data_size_raw(const std::string &tileset) const override;
116
118 num_tiles_per_metatile_raw(const std::string &tileset) const override;
119
121 extrinsic_transparency_raw(const std::string &tileset) const override;
122
124 patch_build_enabled_raw(const std::string &tileset) const override;
125
126 /*
127 * App Config Raw Methods
128 */
129
130 /*
131 * Infra Config Raw Methods
132 */
133 [[nodiscard]] ChainableResult<ConfigValue<TilesPalMode>> tiles_pal_mode_raw(const std::string &tileset) const override;
134
135 public:
136
137 /*
138 * LazyLayeredConfig Specific Functionality
139 */
140
150 [[nodiscard]] std::string dump() const;
151
162 void warmup_cache(const std::vector<std::string> &tileset_names) const;
163
164 private:
165 std::unique_ptr<TextFormatter> owned_format_; // Optional owned formatter (when using default ctor)
166 TextFormatter *format_; // Non-owning pointer to formatter
167 // Providers in priority order (highest first)
168 std::vector<std::unique_ptr<ConfigProvider>> providers_;
169
170 // Config value cache
171 mutable std::map<std::string, std::any> cache_;
172 // Config value as string for debug printing
173 mutable std::unordered_map<std::string, std::string> cache_value_strings_;
174
175 // Config source and source_details
176 mutable std::unordered_map<std::string, std::string> source_;
177 mutable std::unordered_map<std::string, std::vector<std::string>> source_details_;
178
187 template <typename T>
188 ChainableResult<ConfigValue<T>> resolve_config_value(
189 const std::string &cache_key, std::function<LayerValue<T>(const ConfigProvider &)> provider_call) const;
190};
191
192} // namespace porytiles2
Interface that defines a complete app layer configuration.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
An interface which config implementations can use to load config values.
Interface that defines a complete domain layer configuration.
Interface that defines a complete infra layer configuration.
A Config implementation that lazily pulls a config value by consulting multiple priority-ordered back...
ChainableResult< ConfigValue< std::size_t > > num_tiles_total_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< Rgba32 > > extrinsic_transparency_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< TilesPalMode > > tiles_pal_mode_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< std::size_t > > num_tiles_per_metatile_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< std::size_t > > num_metatiles_total_raw(const std::string &tileset) const override
LazyLayeredConfig(std::vector< std::unique_ptr< ConfigProvider > > &&providers)
Constructs a LazyLayeredConfig with a default PlainTextFormatter.
ChainableResult< ConfigValue< bool > > patch_build_enabled_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< std::size_t > > max_map_data_size_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< std::size_t > > num_tiles_primary_raw(const std::string &tileset) const override
std::string dump() const
Dumps the current state of the config for debugging purposes.
LazyLayeredConfig(gsl::not_null< TextFormatter * > format, std::vector< std::unique_ptr< ConfigProvider > > &&providers)
Constructs a LazyLayeredConfig with a list of ConfigProviders in priority order, highest to lowest.
ChainableResult< ConfigValue< std::size_t > > num_metatiles_primary_raw(const std::string &tileset) const override
ChainableResult< ConfigValue< std::size_t > > num_pals_primary_raw(const std::string &tileset) const override
void warmup_cache(const std::vector< std::string > &tileset_names) const
Forces all configuration values to be cached immediately for all known tilesets.
ChainableResult< ConfigValue< std::size_t > > num_pals_total_raw(const std::string &tileset) const override
TextFormatter implementation that strips all styling from text.
Abstract base class for applying text styling with context-aware formatting.
A small container that holds an optional-wrapped value, validation state, and metadata about the valu...