18#include "3rdparty/fmt/ranges.h"
32 Debug(misc, level,
" {}", detail);
45 auto *item = group.
GetItem(name);
46 if (item !=
nullptr && item->value.has_value() && !item->value->empty())
return item;
47 this->LogError(full_filename, fmt::format(
"{}.{} field missing.", group.
name, name));
63 if (metadata ==
nullptr) {
64 this->LogError(full_filename,
"Is the file readable for the user running OpenTTD?");
69 item = this->GetMandatoryItem(full_filename, *metadata,
"name");
70 if (item ==
nullptr)
return false;
71 this->name = *item->
value;
73 item = this->GetMandatoryItem(full_filename, *metadata,
"description");
74 if (item ==
nullptr)
return false;
75 this->description[std::string{}] = *item->
value;
77 item = metadata->
GetItem(
"url");
78 if (item !=
nullptr) this->url = *item->
value;
82 if (titem.name.compare(0, 12,
"description.") != 0)
continue;
84 this->description[titem.name.substr(12)] = titem.value.value_or(
"");
87 item = this->GetMandatoryItem(full_filename, *metadata,
"shortname");
88 if (item ==
nullptr)
return false;
89 for (uint i = 0; (*item->
value)[i] !=
'\0' && i < 4; i++) {
90 this->shortname |= ((uint8_t)(*item->
value)[i]) << (i * 8);
94 if (item ==
nullptr)
return false;
96 auto value = consumer.TryReadIntegerBase<uint32_t>(10);
97 bool valid = value.has_value();
98 if (valid) this->version.push_back(*value);
99 if (valid && !consumer.AnyBytesLeft())
break;
100 if (!valid || !consumer.ReadIf(
".")) {
101 this->LogError(full_filename, fmt::format(
"metadata.version field is invalid: {}", *item->
value));
106 item = metadata->
GetItem(
"fallback");
107 this->fallback = (item !=
nullptr && item->
value && *item->
value !=
"0" && *item->
value !=
"false");
120 for (uint i = 0; i < BaseSet<T>::NUM_FILES; i++) {
121 MD5File *file = &this->files[i];
123 item = files !=
nullptr ? files->
GetItem(file_names[i]) :
nullptr;
124 if (item ==
nullptr || (!item->
value.has_value() && !allow_empty_filename)) {
125 this->LogError(full_filename, fmt::format(
"files.{} field missing", file_names[i]));
129 if (!item->
value.has_value()) {
137 const std::string &filename = item->
value.value();
141 item = md5s !=
nullptr ? md5s->
GetItem(filename) :
nullptr;
142 if (item ==
nullptr || !item->
value.has_value()) {
143 this->LogError(full_filename, fmt::format(
"md5s.{} field missing", filename));
147 this->LogError(full_filename, fmt::format(
"md5s.{} is malformed: {}", filename, *item->
value));
152 item = origin !=
nullptr ? origin->
GetItem(filename) :
nullptr;
153 if (item ==
nullptr) item = origin !=
nullptr ? origin->
GetItem(
"default") :
nullptr;
154 if (item ==
nullptr || !item->
value.has_value()) {
155 this->LogError(full_filename, fmt::format(
"origin.{} field missing", filename), 1);
173 this->LogError(full_filename, fmt::format(
"MD5 checksum mismatch for: {}", filename), original_set ? 1 : 0);
179 this->LogError(full_filename, fmt::format(
"File is missing: {}", filename), original_set ? 1 : 0);
187template <
class Tbase_set>
192 auto set = std::make_unique<Tbase_set>();
194 std::string path{ filename, basepath_length };
197 auto psep = path.rfind(PATHSEPCHAR);
198 if (psep != std::string::npos) {
199 path.erase(psep + 1);
204 if (!set->FillSetDetails(ini, path, filename))
return false;
209 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) ||
210 (*existing)->valid_files > set->valid_files) {
212 Debug(misc, 1,
"Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version,
"."),
214 (*existing)->
valid_files > set->valid_files ?
"fewer valid files" :
"lower version");
216 duplicate_sets.push_back(std::move(set));
226 set->CopyCompatibleConfig(**existing);
229 (*existing)->
valid_files < set->valid_files ?
"fewer valid files" :
"lower version");
232 duplicate_sets.push_back(std::move(*existing));
235 *existing = std::move(set);
238 available_sets.push_back(std::move(set));
249template <
class Tbase_set>
252 if (set ==
nullptr) {
266template <
class Tbase_set>
270 return SetSet(
nullptr);
274 if (name == s->name) {
275 return SetSet(s.get());
286template <
class Tbase_set>
289 if (shortname == 0) {
290 return SetSet(
nullptr);
294 if (shortname == s->shortname) {
295 return SetSet(s.get());
305template <
class Tbase_set>
310 fmt::format_to(output_iterator,
"{:>18}: {}", s->name, s->GetDescription({}));
311 int invalid = s->GetNumInvalid();
313 int missing = s->GetNumMissing();
315 fmt::format_to(output_iterator,
" ({} corrupt file{})\n", invalid, invalid == 1 ?
"" :
"s");
317 fmt::format_to(output_iterator,
" (unusable: {} missing file{})\n", missing, missing == 1 ?
"" :
"s");
320 fmt::format_to(output_iterator,
"\n");
323 fmt::format_to(output_iterator,
"\n");
328template <
class Tbase_set> std::optional<std::string_view>
TryGetBaseSetFile(
const ContentInfo &ci,
bool md5sum, std::span<
const std::unique_ptr<Tbase_set>> sets)
330 for (
const auto &s : sets) {
331 if (s->GetNumMissing() != 0)
continue;
333 if (s->shortname != ci.
unique_id)
continue;
334 if (!md5sum)
return s->files[0].filename;
337 for (
const auto &file : s->files) {
340 if (md5 == ci.
md5sum)
return s->files[0].filename;
328template <
class Tbase_set> std::optional<std::string_view>
TryGetBaseSetFile(
const ContentInfo &ci,
bool md5sum, std::span<
const std::unique_ptr<Tbase_set>> sets) {
…}
345template <
class Tbase_set>
356template <
class Tbase_set>
368template <
class Tbase_set>
374 if (s->GetNumMissing() != 0)
continue;
384template <
class Tbase_set>
389 if (index == 0)
return s.get();
399template <
class Tbase_set>
212 Debug(misc, 1,
"Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version,
"."), {
…}
210 (*existing)->valid_files > set->valid_files) {
…}
209 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) || {
…}
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.