16#include "3rdparty/fmt/ranges.h"
30 Debug(misc, level,
" {}", detail);
43 auto *item = group.
GetItem(name);
44 if (item !=
nullptr && item->value.has_value() && !item->value->empty())
return item;
45 this->LogError(full_filename, fmt::format(
"{}.{} field missing.", group.
name, name));
61 if (metadata ==
nullptr) {
62 this->LogError(full_filename,
"Is the file readable for the user running OpenTTD?");
67 item = this->GetMandatoryItem(full_filename, *metadata,
"name");
68 if (item ==
nullptr)
return false;
69 this->name = *item->
value;
71 item = this->GetMandatoryItem(full_filename, *metadata,
"description");
72 if (item ==
nullptr)
return false;
73 this->description[std::string{}] = *item->
value;
75 item = metadata->
GetItem(
"url");
76 if (item !=
nullptr) this->url = *item->
value;
80 if (!titem.name.starts_with(
"description."))
continue;
82 this->description[titem.name.substr(12)] = titem.value.value_or(
"");
85 item = this->GetMandatoryItem(full_filename, *metadata,
"shortname");
86 if (item ==
nullptr)
return false;
87 for (uint i = 0; (*item->
value)[i] !=
'\0' && i < 4; i++) {
88 this->shortname |= ((uint8_t)(*item->
value)[i]) << (i * 8);
91 item = this->GetMandatoryItem(full_filename, *metadata,
"version");
92 if (item ==
nullptr)
return false;
94 auto value = consumer.TryReadIntegerBase<uint32_t>(10);
95 bool valid = value.has_value();
96 if (valid) this->version.push_back(*value);
97 if (valid && !consumer.AnyBytesLeft())
break;
98 if (!valid || !consumer.ReadIf(
".")) {
99 this->LogError(full_filename, fmt::format(
"metadata.version field is invalid: {}", *item->
value));
104 item = metadata->
GetItem(
"fallback");
105 this->fallback = (item !=
nullptr && item->
value && *item->
value !=
"0" && *item->
value !=
"false");
118 for (uint i = 0; i < BaseSet<T>::NUM_FILES; i++) {
119 MD5File *file = &this->files[i];
121 item = files !=
nullptr ? files->
GetItem(file_names[i]) :
nullptr;
122 if (item ==
nullptr || (!item->
value.has_value() && !allow_empty_filename)) {
123 this->LogError(full_filename, fmt::format(
"files.{} field missing", file_names[i]));
127 if (!item->
value.has_value()) {
135 const std::string &filename = item->
value.value();
139 item = md5s !=
nullptr ? md5s->
GetItem(filename) :
nullptr;
140 if (item ==
nullptr || !item->
value.has_value()) {
141 this->LogError(full_filename, fmt::format(
"md5s.{} field missing", filename));
145 this->LogError(full_filename, fmt::format(
"md5s.{} is malformed: {}", filename, *item->
value));
150 item = origin !=
nullptr ? origin->
GetItem(filename) :
nullptr;
151 if (item ==
nullptr) item = origin !=
nullptr ? origin->
GetItem(
"default") :
nullptr;
152 if (item ==
nullptr || !item->
value.has_value()) {
153 this->LogError(full_filename, fmt::format(
"origin.{} field missing", filename), 1);
171 this->LogError(full_filename, fmt::format(
"MD5 checksum mismatch for: {}", filename), original_set ? 1 : 0);
177 this->LogError(full_filename, fmt::format(
"File is missing: {}", filename), original_set ? 1 : 0);
185template <
class Tbase_set>
190 auto set = std::make_unique<Tbase_set>();
192 std::string path{ filename, basepath_length };
195 auto psep = path.rfind(PATHSEPCHAR);
196 if (psep != std::string::npos) {
197 path.erase(psep + 1);
202 if (!set->FillSetDetails(ini, path, filename))
return false;
207 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) ||
208 (*existing)->valid_files > set->valid_files) {
210 Debug(misc, 1,
"Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version,
"."),
212 (*existing)->
valid_files > set->valid_files ?
"fewer valid files" :
"lower version");
214 duplicate_sets.push_back(std::move(set));
224 set->CopyCompatibleConfig(**existing);
227 (*existing)->
valid_files < set->valid_files ?
"fewer valid files" :
"lower version");
230 duplicate_sets.push_back(std::move(*existing));
233 *existing = std::move(set);
236 available_sets.push_back(std::move(set));
247template <
class Tbase_set>
250 if (set ==
nullptr) {
264template <
class Tbase_set>
268 return SetSet(
nullptr);
272 if (name == s->name) {
273 return SetSet(s.get());
284template <
class Tbase_set>
287 if (shortname == 0) {
288 return SetSet(
nullptr);
292 if (shortname == s->shortname) {
293 return SetSet(s.get());
303template <
class Tbase_set>
308 fmt::format_to(output_iterator,
"{:>18}: {}", s->name, s->GetDescription({}));
309 int invalid = s->GetNumInvalid();
311 int missing = s->GetNumMissing();
313 fmt::format_to(output_iterator,
" ({} corrupt file{})\n", invalid, invalid == 1 ?
"" :
"s");
315 fmt::format_to(output_iterator,
" (unusable: {} missing file{})\n", missing, missing == 1 ?
"" :
"s");
318 fmt::format_to(output_iterator,
"\n");
321 fmt::format_to(output_iterator,
"\n");
326template <
class Tbase_set> std::optional<std::string_view>
TryGetBaseSetFile(
const ContentInfo &ci,
bool md5sum, std::span<
const std::unique_ptr<Tbase_set>> sets)
328 for (
const auto &s : sets) {
329 if (s->GetNumMissing() != 0)
continue;
331 if (s->shortname != ci.
unique_id)
continue;
332 if (!md5sum)
return s->files[0].filename;
335 for (
const auto &file : s->files) {
338 if (md5 == ci.
md5sum)
return s->files[0].filename;
343template <
class Tbase_set>
354template <
class Tbase_set>
366template <
class Tbase_set>
372 if (s->GetNumMissing() != 0)
continue;
382template <
class Tbase_set>
387 if (index == 0)
return s.get();
397template <
class Tbase_set>
constexpr enable_if_t< is_integral_v< T >, T > byteswap(T x) noexcept
Custom implementation of std::byteswap; remove once we build with C++23.
Parse data from a string / buffer.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Error reporting related functions.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Types related to reading/writing '*.ini' files.
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Functions related to low-level strings.
Information about a single base set.
uint valid_files
Number of the files that could be found and are valid.
void LogError(std::string_view full_filename, std::string_view detail, int level=0) const
Log error from reading basesets.
const IniItem * GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const
Try to read a single piece of metadata and return nullptr if it doesn't exist.
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
Container for all important information about a piece of content.
uint32_t unique_id
Unique ID; either GRF ID or shortname.
MD5Hash md5sum
The MD5 checksum.
Ini file that supports both loading and saving.
A group within an ini file.
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
std::string name
name of group
std::list< IniItem > items
all items in the group
A single "line" in an ini file.
std::optional< std::string > value
The value of this item.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Structure holding filename and MD5 information about a single file.
std::string missing_warning
warning when this file is missing
ChecksumResult check_result
cached result of md5 check
@ CR_MATCH
The file did exist and the md5 checksum did match.
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
@ CR_NO_FILE
The file did not exist.
@ CR_UNKNOWN
The file has not been checked yet.
MD5Hash hash
md5 sum of the file
std::string filename
filename
Basic types related to the content on the content server.