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);
93 item = this->GetMandatoryItem(full_filename, *metadata,
"version");
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>
193 Tbase_set *set =
new Tbase_set();
195 std::string path{ filename, basepath_length };
198 auto psep = path.rfind(PATHSEPCHAR);
199 if (psep != std::string::npos) {
200 path.erase(psep + 1);
205 if (set->FillSetDetails(ini, path, filename)) {
206 Tbase_set *duplicate =
nullptr;
208 if (c->name == set->name || c->shortname == set->shortname) {
213 if (duplicate !=
nullptr) {
215 if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
216 duplicate->valid_files > set->valid_files) {
217 Debug(misc, 1,
"Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version,
"."),
219 duplicate->
valid_files > set->valid_files ?
"less valid files" :
"lower version");
224 while (*prev != duplicate) prev = &(*prev)->next;
227 set->next = duplicate->next;
230 set->CopyCompatibleConfig(*duplicate);
237 Debug(misc, 1,
"Removing {} ({}) as base {} set (duplicate, {})", duplicate->name, fmt::join(duplicate->version,
"."),
239 duplicate->
valid_files < set->valid_files ?
"less valid files" :
"lower version");
246 while (*last !=
nullptr) last = &(*last)->next;
266template <
class Tbase_set>
269 if (set ==
nullptr) {
283template <
class Tbase_set>
287 return SetSet(
nullptr);
291 if (name == s->name) {
303template <
class Tbase_set>
306 if (shortname == 0) {
307 return SetSet(
nullptr);
311 if (shortname == s->shortname) {
322template <
class Tbase_set>
327 fmt::format_to(output_iterator,
"{:>18}: {}", s->name, s->GetDescription({}));
328 int invalid = s->GetNumInvalid();
330 int missing = s->GetNumMissing();
332 fmt::format_to(output_iterator,
" ({} corrupt file{})\n", invalid, invalid == 1 ?
"" :
"s");
334 fmt::format_to(output_iterator,
" (unusable: {} missing file{})\n", missing, missing == 1 ?
"" :
"s");
337 fmt::format_to(output_iterator,
"\n");
340 fmt::format_to(output_iterator,
"\n");
347 for (; s !=
nullptr; s = s->next) {
348 if (s->GetNumMissing() != 0)
continue;
350 if (s->shortname != ci.
unique_id)
continue;
351 if (!md5sum)
return s->files[0].filename;
354 for (
const auto &file : s->files) {
357 if (md5 == ci.
md5sum)
return s->files[0].filename;
362template <
class Tbase_set>
373template <
class Tbase_set>
388template <
class Tbase_set>
394 if (s->GetNumMissing() != 0)
continue;
404template <
class Tbase_set>
409 if (index == 0)
return s;
419template <
class Tbase_set>
429template <
class Tbase_set>
208 if (c->name == set->name || c->shortname == set->shortname) {
…}
206 Tbase_set *duplicate =
nullptr; {
…}
205 if (set->FillSetDetails(ini, path, filename)) {
…}
101 this->LogError(full_filename, fmt::format(
"metadata.version field is invalid: {}", *item->
value)); {
…}
100 if (!valid || !consumer.ReadIf(
".")) {
…}
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.