16#include "3rdparty/fmt/ranges.h"
30 Debug(misc, level,
" {}", detail);
45 if (item !=
nullptr && item->value.has_value() && !item->value->empty())
return item;
46 this->
LogError(full_filename, fmt::format(
"{}.{} field missing.", group.
name,
name));
58 shortname = shortname.substr(0, 4);
60 for (
size_t i = 0; i < shortname.size(); i++) {
61 encoded |=
static_cast<uint8_t
>(shortname[i]) << (i * 8);
78 if (metadata ==
nullptr) {
79 this->
LogError(full_filename,
"Is the file readable for the user running OpenTTD?");
85 if (item ==
nullptr)
return false;
89 if (item ==
nullptr)
return false;
92 item = metadata->
GetItem(
"url");
93 if (item !=
nullptr) this->
url = *item->
value;
97 if (!titem.
name.starts_with(
"description."))
continue;
102 item = this->GetMandatoryItem(full_filename, *metadata,
"shortname");
103 if (item ==
nullptr)
return false;
106 item = this->GetMandatoryItem(full_filename, *metadata,
"version");
107 if (item ==
nullptr)
return false;
109 auto value = consumer.TryReadIntegerBase<uint32_t>(10);
110 bool valid = value.has_value();
111 if (valid) this->version.push_back(*value);
112 if (valid && !consumer.AnyBytesLeft())
break;
113 if (!valid || !consumer.ReadIf(
".")) {
114 this->LogError(full_filename, fmt::format(
"metadata.version field is invalid: {}", *item->
value));
119 item = metadata->
GetItem(
"fallback");
120 this->fallback = (item !=
nullptr && item->
value && *item->
value !=
"0" && *item->
value !=
"false");
133 for (uint i = 0; i < BaseSet<T>::NUM_FILES; i++) {
134 MD5File *file = &this->files[i];
136 item = files !=
nullptr ? files->
GetItem(file_names[i]) :
nullptr;
137 if (item ==
nullptr || (!item->
value.has_value() && !allow_empty_filename)) {
138 this->LogError(full_filename, fmt::format(
"files.{} field missing", file_names[i]));
142 if (!item->
value.has_value()) {
150 const std::string &filename = item->
value.value();
154 item = md5s !=
nullptr ? md5s->
GetItem(filename) :
nullptr;
155 if (item ==
nullptr || !item->
value.has_value()) {
156 this->LogError(full_filename, fmt::format(
"md5s.{} field missing", filename));
160 this->LogError(full_filename, fmt::format(
"md5s.{} is malformed: {}", filename, *item->
value));
165 item = origin !=
nullptr ? origin->
GetItem(filename) :
nullptr;
166 if (item ==
nullptr) item = origin !=
nullptr ? origin->
GetItem(
"default") :
nullptr;
167 if (item ==
nullptr || !item->
value.has_value()) {
168 this->LogError(full_filename, fmt::format(
"origin.{} field missing", filename), 1);
186 this->LogError(full_filename, fmt::format(
"MD5 checksum mismatch for: {}", filename), original_set ? 1 : 0);
192 this->LogError(full_filename, fmt::format(
"File is missing: {}", filename), original_set ? 1 : 0);
200template <
class Tbase_set>
205 auto set = std::make_unique<Tbase_set>();
207 std::string path{ filename, basepath_length };
210 auto psep = path.rfind(PATHSEPCHAR);
211 if (psep != std::string::npos) {
212 path.erase(psep + 1);
217 if (!set->FillSetDetails(ini, path, filename))
return false;
222 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) ||
223 (*existing)->valid_files > set->valid_files) {
225 Debug(misc, 1,
"Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version,
"."),
227 (*existing)->valid_files > set->valid_files ?
"fewer valid files" :
"lower version");
239 set->CopyCompatibleConfig(**existing);
242 (*existing)->valid_files < set->valid_files ?
"fewer valid files" :
"lower version");
248 *existing = std::move(set);
251 available_sets.push_back(std::move(set));
262template <
class Tbase_set>
265 if (set ==
nullptr) {
279template <
class Tbase_set>
287 if (name == s->name) {
299template <
class Tbase_set>
302 if (shortname == 0) {
307 if (shortname == s->shortname) {
318template <
class Tbase_set>
323 fmt::format_to(output_iterator,
"{:>18}: {}", s->name, s->GetDescription({}));
324 int invalid = s->GetNumInvalid();
326 int missing = s->GetNumMissing();
328 fmt::format_to(output_iterator,
" ({} corrupt file{})\n", invalid, invalid == 1 ?
"" :
"s");
330 fmt::format_to(output_iterator,
" (unusable: {} missing file{})\n", missing, missing == 1 ?
"" :
"s");
333 fmt::format_to(output_iterator,
"\n");
336 fmt::format_to(output_iterator,
"\n");
341template <
class Tbase_set> std::optional<std::string_view>
TryGetBaseSetFile(
const ContentInfo &ci,
bool md5sum, std::span<
const std::unique_ptr<Tbase_set>> sets)
343 for (
const auto &s : sets) {
344 if (s->GetNumMissing() != 0)
continue;
346 if (s->shortname != ci.
unique_id)
continue;
347 if (!md5sum)
return s->files[0].filename;
350 for (
const auto &file : s->files) {
353 if (md5 == ci.
md5sum)
return s->files[0].filename;
358template <
class Tbase_set>
369template <
class Tbase_set>
381template <
class Tbase_set>
387 if (s->GetNumMissing() != 0)
continue;
398template <
class Tbase_set>
403 if (index == 0)
return s.get();
413template <
class Tbase_set>
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
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.
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.
std::string url
URL for information about the base set.
TranslatedStrings description
Description of the base set.
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
static constexpr std::string_view SET_TYPE
BaseSet type name.
std::string name
The name of the base 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.
std::string name
The name 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
@ NoFile
The file did not exist.
@ Mismatch
The file did exist, just the md5 checksum did not match.
@ Match
The file did exist and the md5 checksum did match.
@ 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.