Porytiles
Loading...
Searching...
No Matches
metatiles_header_provider.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4#include <filesystem>
5#include <fstream>
6#include <string>
7
10
11namespace {
12
13using namespace porytiles;
14
15const std::filesystem::path metatiles_rel_path = std::filesystem::path{"src"} / "data" / "tilesets" / "metatiles.h";
16
24[[nodiscard]] LayerValue<std::size_t>
25detect_attr_size(const std::filesystem::path &metatiles_path, const TextFormatter *format)
26{
27 if (!std::filesystem::exists(metatiles_path)) {
29 }
30
31 std::ifstream in{metatiles_path};
32 if (!in.is_open()) {
34 }
35
36 bool found_u16 = false;
37 bool found_u32 = false;
38 std::string line;
39
40 while (std::getline(in, line)) {
41 if (line.find("gMetatileAttributes_") == std::string::npos) {
42 continue;
43 }
44
45 if (line.find("const u16") != std::string::npos) {
46 found_u16 = true;
47 }
48 if (line.find("const u32") != std::string::npos) {
49 found_u32 = true;
50 }
51 }
52
53 if (!found_u16 && !found_u32) {
55 }
56
57 const std::string source_info = metatiles_path.string();
58
59 if (found_u16 && found_u32) {
61 format->format(
62 "Mixed u16/u32 attribute declarations found in '{}'.",
63 FormatParam{metatiles_path.string(), Style::bold}),
64 source_info);
65 }
66
67 const std::size_t attr_size = found_u32 ? 4 : 2;
68 return LayerValue<std::size_t>::valid(attr_size, "MetatilesHeaderProvider", source_info);
69}
70
71} // namespace
72
73namespace porytiles {
74
76{
77 return "MetatilesHeaderProvider";
78}
79
81MetatilesHeaderProvider::metatile_attr_size(ConfigScopeType /*type*/, const std::string & /*scope*/) const
82{
83 if (!cached_result_.has_value()) {
84 cached_result_ = detect_attr_size(project_root_ / metatiles_rel_path, format_);
85 }
86 return cached_result_.value();
87}
88
89} // namespace porytiles
A text parameter with associated styling for formatted output.
LayerValue< std::size_t > metatile_attr_size(ConfigScopeType type, const std::string &scope) const override
std::string name() const override
Gets the name of this ConfigProvider, useful for diagnostic purposes.
Abstract base class for applying text styling with context-aware formatting.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
ConfigScopeType
Specifies the scope type for configuration value lookups.
A small container that holds an optional-wrapped value, validation state, and metadata about the valu...
static LayerValue valid(T val, std::string source_key, std::string source_info)
Creates a LayerValue representing a valid configuration value.
static LayerValue not_provided()
Creates a LayerValue representing that the provider does not supply this configuration.
static LayerValue invalid(std::string error, std::string source_info)
Creates a LayerValue representing an invalid configuration value.