OpenTTD Source 20260218-master-g2123fca5ea
base_media_func.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "base_media_base.h"
11#include "debug.h"
12#include "ini_type.h"
13#include "string_func.h"
14#include "error_func.h"
16#include "3rdparty/fmt/ranges.h"
17
18extern void CheckExternalFiles();
19
26template <class T>
27void BaseSet<T>::LogError(std::string_view full_filename, std::string_view detail, int level) const
28{
29 Debug(misc, level, "Loading base {}set details failed: {}", BaseSet<T>::SET_TYPE, full_filename);
30 Debug(misc, level, " {}", detail);
31}
32
41template <class T>
42const IniItem *BaseSet<T>::GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const
43{
44 auto *item = group.GetItem(name);
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));
47 return nullptr;
48}
49
58template <class T>
59bool BaseSet<T>::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename)
60{
61 const IniGroup *metadata = ini.GetGroup("metadata");
62 if (metadata == nullptr) {
63 this->LogError(full_filename, "Is the file readable for the user running OpenTTD?");
64 return false;
65 }
66 const IniItem *item;
67
68 item = this->GetMandatoryItem(full_filename, *metadata, "name");
69 if (item == nullptr) return false;
70 this->name = *item->value;
71
72 item = this->GetMandatoryItem(full_filename, *metadata, "description");
73 if (item == nullptr) return false;
74 this->description[std::string{}] = *item->value;
75
76 item = metadata->GetItem("url");
77 if (item != nullptr) this->url = *item->value;
78
79 /* Add the translations of the descriptions too. */
80 for (const IniItem &titem : metadata->items) {
81 if (!titem.name.starts_with("description.")) continue;
82
83 this->description[titem.name.substr(12)] = titem.value.value_or("");
84 }
85
86 item = this->GetMandatoryItem(full_filename, *metadata, "shortname");
87 if (item == nullptr) return false;
88 for (uint i = 0; (*item->value)[i] != '\0' && i < 4; i++) {
89 this->shortname |= ((uint8_t)(*item->value)[i]) << (i * 8);
90 }
91
92 item = this->GetMandatoryItem(full_filename, *metadata, "version");
93 if (item == nullptr) return false;
94 for (StringConsumer consumer{*item->value};;) {
95 auto value = consumer.TryReadIntegerBase<uint32_t>(10);
96 bool valid = value.has_value();
97 if (valid) this->version.push_back(*value);
98 if (valid && !consumer.AnyBytesLeft()) break;
99 if (!valid || !consumer.ReadIf(".")) {
100 this->LogError(full_filename, fmt::format("metadata.version field is invalid: {}", *item->value));
101 return false;
102 }
103 }
104
105 item = metadata->GetItem("fallback");
106 this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false");
107
108 /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
109 const IniGroup *files = ini.GetGroup("files");
110 const IniGroup *md5s = ini.GetGroup("md5s");
111 const IniGroup *origin = ini.GetGroup("origin");
112 auto file_names = BaseSet<T>::GetFilenames();
113 bool original_set =
114 std::byteswap(this->shortname) == 'TTDD' || // TTD DOS graphics, TTD DOS music
115 std::byteswap(this->shortname) == 'TTDW' || // TTD WIN graphics, TTD WIN music
116 std::byteswap(this->shortname) == 'TTDO' || // TTD sound
117 std::byteswap(this->shortname) == 'TTOD'; // TTO music
118
119 for (uint i = 0; i < BaseSet<T>::NUM_FILES; i++) {
120 MD5File *file = &this->files[i];
121 /* Find the filename first. */
122 item = files != nullptr ? files->GetItem(file_names[i]) : nullptr;
123 if (item == nullptr || (!item->value.has_value() && !allow_empty_filename)) {
124 this->LogError(full_filename, fmt::format("files.{} field missing", file_names[i]));
125 return false;
126 }
127
128 if (!item->value.has_value()) {
129 file->filename.clear();
130 /* If we list no file, that file must be valid */
131 this->valid_files++;
132 this->found_files++;
133 continue;
134 }
135
136 const std::string &filename = item->value.value();
137 file->filename = path + filename;
138
139 /* Then find the MD5 checksum */
140 item = md5s != nullptr ? md5s->GetItem(filename) : nullptr;
141 if (item == nullptr || !item->value.has_value()) {
142 this->LogError(full_filename, fmt::format("md5s.{} field missing", filename));
143 return false;
144 }
145 if (!ConvertHexToBytes(*item->value, file->hash)) {
146 this->LogError(full_filename, fmt::format("md5s.{} is malformed: {}", filename, *item->value));
147 return false;
148 }
149
150 /* Then find the warning message when the file's missing */
151 item = origin != nullptr ? origin->GetItem(filename) : nullptr;
152 if (item == nullptr) item = origin != nullptr ? origin->GetItem("default") : nullptr;
153 if (item == nullptr || !item->value.has_value()) {
154 this->LogError(full_filename, fmt::format("origin.{} field missing", filename), 1);
155 file->missing_warning.clear();
156 } else {
157 file->missing_warning = item->value.value();
158 }
159
160 file->check_result = T::CheckMD5(file, BASESET_DIR);
161 switch (file->check_result) {
163 break;
164
166 this->valid_files++;
167 this->found_files++;
168 break;
169
171 /* This is normal for original sample.cat, which either matches with orig_dos or orig_win. */
172 this->LogError(full_filename, fmt::format("MD5 checksum mismatch for: {}", filename), original_set ? 1 : 0);
173 this->found_files++;
174 break;
175
177 /* Missing files is normal for the original basesets. Use lower debug level */
178 this->LogError(full_filename, fmt::format("File is missing: {}", filename), original_set ? 1 : 0);
179 break;
180 }
181 }
182
183 return true;
184}
185
186template <class Tbase_set>
187bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &)
188{
189 Debug(misc, 1, "Checking {} for base {} set", filename, BaseSet<Tbase_set>::SET_TYPE);
190
191 auto set = std::make_unique<Tbase_set>();
192 IniFile ini{};
193 std::string path{ filename, basepath_length };
194 ini.LoadFromDisk(path, BASESET_DIR);
195
196 auto psep = path.rfind(PATHSEPCHAR);
197 if (psep != std::string::npos) {
198 path.erase(psep + 1);
199 } else {
200 path.clear();
201 }
202
203 if (!set->FillSetDetails(ini, path, filename)) return false;
204
205 auto existing = std::ranges::find_if(BaseMedia<Tbase_set>::available_sets, [&set](const auto &c) { return c->name == set->name || c->shortname == set->shortname; });
206 if (existing != std::end(BaseMedia<Tbase_set>::available_sets)) {
207 /* The more complete set takes precedence over the version number. */
208 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) ||
209 (*existing)->valid_files > set->valid_files) {
211 Debug(misc, 1, "Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version, "."),
213 (*existing)->valid_files > set->valid_files ? "fewer valid files" : "lower version");
215 duplicate_sets.push_back(std::move(set));
216 return false;
218
219 /* If the duplicate set is currently used (due to rescanning this can happen)
220 * update the currently used set to the new one. This will 'lie' about the
221 * version number until a new game is started which isn't a big problem */
222 if (BaseMedia<Tbase_set>::used_set == existing->get()) BaseMedia<Tbase_set>::used_set = set.get();
223
224 /* Keep baseset configuration, if compatible */
225 set->CopyCompatibleConfig(**existing);
226
227 Debug(misc, 1, "Removing {} ({}) as base {} set (duplicate, {})", (*existing)->name, fmt::join((*existing)->version, "."), BaseSet<Tbase_set>::SET_TYPE,
228 (*existing)->valid_files < set->valid_files ? "fewer valid files" : "lower version");
229
230 /* Existing set is worse, move it to duplicates and replace with the current set. */
231 duplicate_sets.push_back(std::move(*existing));
232
233 Debug(misc, 1, "Adding {} ({}) as base {} set", set->name, fmt::join(set->version, "."), BaseSet<Tbase_set>::SET_TYPE);
234 *existing = std::move(set);
235 } else {
236 Debug(grf, 1, "Adding {} ({}) as base {} set", set->name, set->version, BaseSet<Tbase_set>::SET_TYPE);
237 available_sets.push_back(std::move(set));
238 }
239
240 return true;
241}
242
248template <class Tbase_set>
249/* static */ bool BaseMedia<Tbase_set>::SetSet(const Tbase_set *set)
250{
251 if (set == nullptr) {
252 if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
253 } else {
255 }
257 return true;
258}
259
265template <class Tbase_set>
266/* static */ bool BaseMedia<Tbase_set>::SetSetByName(const std::string &name)
267{
268 if (name.empty()) {
269 return SetSet(nullptr);
270 }
271
272 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
273 if (name == s->name) {
274 return SetSet(s.get());
275 }
276 }
277 return false;
278}
279
285template <class Tbase_set>
286/* static */ bool BaseMedia<Tbase_set>::SetSetByShortname(uint32_t shortname)
287{
288 if (shortname == 0) {
289 return SetSet(nullptr);
290 }
291
292 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
293 if (shortname == s->shortname) {
294 return SetSet(s.get());
295 }
296 }
297 return false;
298}
299
304template <class Tbase_set>
305/* static */ void BaseMedia<Tbase_set>::GetSetsList(std::back_insert_iterator<std::string> &output_iterator)
306{
307 fmt::format_to(output_iterator, "List of {} sets:\n", BaseSet<Tbase_set>::SET_TYPE);
308 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
309 fmt::format_to(output_iterator, "{:>18}: {}", s->name, s->GetDescription({}));
310 int invalid = s->GetNumInvalid();
311 if (invalid != 0) {
312 int missing = s->GetNumMissing();
313 if (missing == 0) {
314 fmt::format_to(output_iterator, " ({} corrupt file{})\n", invalid, invalid == 1 ? "" : "s");
315 } else {
316 fmt::format_to(output_iterator, " (unusable: {} missing file{})\n", missing, missing == 1 ? "" : "s");
317 }
318 } else {
319 fmt::format_to(output_iterator, "\n");
320 }
321 }
322 fmt::format_to(output_iterator, "\n");
323}
324
326
327template <class Tbase_set> std::optional<std::string_view> TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span<const std::unique_ptr<Tbase_set>> sets)
328{
329 for (const auto &s : sets) {
330 if (s->GetNumMissing() != 0) continue;
331
332 if (s->shortname != ci.unique_id) continue;
333 if (!md5sum) return s->files[0].filename;
334
335 MD5Hash md5;
336 for (const auto &file : s->files) {
337 md5 ^= file.hash;
338 }
339 if (md5 == ci.md5sum) return s->files[0].filename;
340 }
341 return std::nullopt;
342}
343
344template <class Tbase_set>
345/* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo &ci, bool md5sum)
346{
347 return TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::GetAvailableSets()).has_value() ||
349}
350
355template <class Tbase_set>
357{
358 return std::ranges::count_if(BaseMedia<Tbase_set>::GetAvailableSets(), [](const auto &set) {
359 return set.get() == BaseMedia<Tbase_set>::used_set || set->GetNumMissing() == 0;
360 });
361}
362
367template <class Tbase_set>
369{
370 int n = 0;
371 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
372 if (s.get() == BaseMedia<Tbase_set>::used_set) return n;
373 if (s->GetNumMissing() != 0) continue;
374 n++;
375 }
376 return -1;
377}
378
384template <class Tbase_set>
385/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
386{
387 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
388 if (s.get() != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
389 if (index == 0) return s.get();
390 index--;
391 }
392 FatalError("Base{}::GetSet(): index {} out of range", BaseSet<Tbase_set>::SET_TYPE, index);
393}
394
399template <class Tbase_set>
400/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
401{
403}
Generic functions for replacing base data (graphics, sounds).
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition gfxinit.cpp:112
std::optional< std::string_view > TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span< const std::unique_ptr< Tbase_set > > sets)
Check whether there's a base set matching some information.
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.
static const Tbase_set * GetUsedSet()
Return the used set.
static std::vector< std::unique_ptr< Tbase_set > > available_sets
All available sets.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
static bool SetSetByShortname(uint32_t shortname)
Set the set to be used.
static void GetSetsList(std::back_insert_iterator< std::string > &output_iterator)
Returns a list with the sets.
static std::span< const std::unique_ptr< Tbase_set > > GetAvailableSets()
Return the available sets.
static int GetNumSets()
Count the number of available graphics sets.
static bool HasSet(const ContentInfo &ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
static const Tbase_set * used_set
The currently used set.
static std::vector< std::unique_ptr< GraphicsSet > > duplicate_sets
static const Tbase_set * GetSet(int index)
Get the base set at a specified index.
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
static bool SetSetByName(const std::string &name)
Set the set to be used.
static std::span< const std::unique_ptr< Tbase_set > > GetDuplicateSets()
Return the duplicate sets.
static bool SetSet(const Tbase_set *set)
Set the set to be used.
Parse data from a string / buffer.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
Error reporting related functions.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:96
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.
Definition string.cpp:570
Parse strings.
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.
std::vector< uint32_t > version
uint32_t shortname
Four letter short variant of the name.
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.
Definition ini_type.h:86
A group within an ini file.
Definition ini_type.h:34
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:50
std::string name
name of group
Definition ini_type.h:37
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
A single "line" in an ini file.
Definition ini_type.h:23
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
std::string name
The name of this item.
Definition ini_type.h:24
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:117
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:184
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.