OpenTTD Source 20250529-master-g10c159a79f
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 <http://www.gnu.org/licenses/>.
6 */
7
12#include "base_media_base.h"
13#include "debug.h"
14#include "ini_type.h"
15#include "string_func.h"
16#include "error_func.h"
18#include "3rdparty/fmt/ranges.h"
19
20extern void CheckExternalFiles();
21
28template <class T>
29void BaseSet<T>::LogError(std::string_view full_filename, std::string_view detail, int level) const
30{
31 Debug(misc, level, "Loading base {}set details failed: {}", BaseSet<T>::SET_TYPE, full_filename);
32 Debug(misc, level, " {}", detail);
33}
34
42template <class T>
43const IniItem *BaseSet<T>::GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const
44{
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));
48 return nullptr;
49}
50
59template <class T>
60bool BaseSet<T>::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename)
61{
62 const IniGroup *metadata = ini.GetGroup("metadata");
63 if (metadata == nullptr) {
64 this->LogError(full_filename, "Is the file readable for the user running OpenTTD?");
65 return false;
66 }
67 const IniItem *item;
68
69 item = this->GetMandatoryItem(full_filename, *metadata, "name");
70 if (item == nullptr) return false;
71 this->name = *item->value;
72
73 item = this->GetMandatoryItem(full_filename, *metadata, "description");
74 if (item == nullptr) return false;
75 this->description[std::string{}] = *item->value;
76
77 item = metadata->GetItem("url");
78 if (item != nullptr) this->url = *item->value;
79
80 /* Add the translations of the descriptions too. */
81 for (const IniItem &titem : metadata->items) {
82 if (titem.name.compare(0, 12, "description.") != 0) continue;
83
84 this->description[titem.name.substr(12)] = titem.value.value_or("");
85 }
86
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);
91 }
92
93 item = this->GetMandatoryItem(full_filename, *metadata, "version");
94 if (item == nullptr) return false;
95 for (StringConsumer consumer{*item->value};;) {
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));
102 return false;
104 }
105
106 item = metadata->GetItem("fallback");
107 this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false");
108
109 /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
110 const IniGroup *files = ini.GetGroup("files");
111 const IniGroup *md5s = ini.GetGroup("md5s");
112 const IniGroup *origin = ini.GetGroup("origin");
113 auto file_names = BaseSet<T>::GetFilenames();
114 bool original_set =
115 std::byteswap(this->shortname) == 'TTDD' || // TTD DOS graphics, TTD DOS music
116 std::byteswap(this->shortname) == 'TTDW' || // TTD WIN graphics, TTD WIN music
117 std::byteswap(this->shortname) == 'TTDO' || // TTD sound
118 std::byteswap(this->shortname) == 'TTOD'; // TTO music
119
120 for (uint i = 0; i < BaseSet<T>::NUM_FILES; i++) {
121 MD5File *file = &this->files[i];
122 /* Find the filename first. */
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]));
126 return false;
127 }
128
129 if (!item->value.has_value()) {
130 file->filename.clear();
131 /* If we list no file, that file must be valid */
132 this->valid_files++;
133 this->found_files++;
134 continue;
135 }
136
137 const std::string &filename = item->value.value();
138 file->filename = path + filename;
139
140 /* Then find the MD5 checksum */
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));
144 return false;
145 }
146 if (!ConvertHexToBytes(*item->value, file->hash)) {
147 this->LogError(full_filename, fmt::format("md5s.{} is malformed: {}", filename, *item->value));
148 return false;
149 }
150
151 /* Then find the warning message when the file's missing */
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);
156 file->missing_warning.clear();
157 } else {
158 file->missing_warning = item->value.value();
159 }
160
161 file->check_result = T::CheckMD5(file, BASESET_DIR);
162 switch (file->check_result) {
164 break;
165
167 this->valid_files++;
168 this->found_files++;
169 break;
170
172 /* This is normal for original sample.cat, which either matches with orig_dos or orig_win. */
173 this->LogError(full_filename, fmt::format("MD5 checksum mismatch for: {}", filename), original_set ? 1 : 0);
174 this->found_files++;
175 break;
176
178 /* Missing files is normal for the original basesets. Use lower debug level */
179 this->LogError(full_filename, fmt::format("File is missing: {}", filename), original_set ? 1 : 0);
180 break;
181 }
182 }
183
184 return true;
185}
186
187template <class Tbase_set>
188bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &)
189{
190 bool ret = false;
191 Debug(misc, 1, "Checking {} for base {} set", filename, BaseSet<Tbase_set>::SET_TYPE);
192
193 Tbase_set *set = new Tbase_set();
194 IniFile ini{};
195 std::string path{ filename, basepath_length };
196 ini.LoadFromDisk(path, BASESET_DIR);
197
198 auto psep = path.rfind(PATHSEPCHAR);
199 if (psep != std::string::npos) {
200 path.erase(psep + 1);
201 } else {
202 path.clear();
205 if (set->FillSetDetails(ini, path, filename)) {
206 Tbase_set *duplicate = nullptr;
207 for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
208 if (c->name == set->name || c->shortname == set->shortname) {
209 duplicate = c;
210 break;
211 }
212 }
213 if (duplicate != nullptr) {
214 /* The more complete set takes precedence over the version number. */
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");
222 } else {
223 Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
224 while (*prev != duplicate) prev = &(*prev)->next;
225
226 *prev = set;
227 set->next = duplicate->next;
228
229 /* Keep baseset configuration, if compatible */
230 set->CopyCompatibleConfig(*duplicate);
231
232 /* If the duplicate set is currently used (due to rescanning this can happen)
233 * update the currently used set to the new one. This will 'lie' about the
234 * version number until a new game is started which isn't a big problem */
236
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");
240 duplicate->next = BaseMedia<Tbase_set>::duplicate_sets;
242 ret = true;
243 }
244 } else {
245 Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
246 while (*last != nullptr) last = &(*last)->next;
247
248 *last = set;
249 ret = true;
250 }
251 if (ret) {
252 Debug(misc, 1, "Adding {} ({}) as base {} set", set->name, fmt::join(set->version, "."), BaseSet<Tbase_set>::SET_TYPE);
253 }
254 } else {
255 delete set;
256 }
257
258 return ret;
259}
260
266template <class Tbase_set>
267/* static */ bool BaseMedia<Tbase_set>::SetSet(const Tbase_set *set)
268{
269 if (set == nullptr) {
270 if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
271 } else {
273 }
275 return true;
276}
277
283template <class Tbase_set>
284/* static */ bool BaseMedia<Tbase_set>::SetSetByName(const std::string &name)
285{
286 if (name.empty()) {
287 return SetSet(nullptr);
288 }
289
290 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
291 if (name == s->name) {
292 return SetSet(s);
293 }
294 }
295 return false;
296}
297
303template <class Tbase_set>
304/* static */ bool BaseMedia<Tbase_set>::SetSetByShortname(uint32_t shortname)
305{
306 if (shortname == 0) {
307 return SetSet(nullptr);
308 }
309
310 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
311 if (shortname == s->shortname) {
312 return SetSet(s);
313 }
314 }
315 return false;
316}
317
322template <class Tbase_set>
323/* static */ void BaseMedia<Tbase_set>::GetSetsList(std::back_insert_iterator<std::string> &output_iterator)
324{
325 fmt::format_to(output_iterator, "List of {} sets:\n", BaseSet<Tbase_set>::SET_TYPE);
326 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
327 fmt::format_to(output_iterator, "{:>18}: {}", s->name, s->GetDescription({}));
328 int invalid = s->GetNumInvalid();
329 if (invalid != 0) {
330 int missing = s->GetNumMissing();
331 if (missing == 0) {
332 fmt::format_to(output_iterator, " ({} corrupt file{})\n", invalid, invalid == 1 ? "" : "s");
333 } else {
334 fmt::format_to(output_iterator, " (unusable: {} missing file{})\n", missing, missing == 1 ? "" : "s");
335 }
336 } else {
337 fmt::format_to(output_iterator, "\n");
338 }
339 }
340 fmt::format_to(output_iterator, "\n");
341}
342
344
345template <class Tbase_set> std::optional<std::string_view> TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s)
346{
347 for (; s != nullptr; s = s->next) {
348 if (s->GetNumMissing() != 0) continue;
349
350 if (s->shortname != ci.unique_id) continue;
351 if (!md5sum) return s->files[0].filename;
352
353 MD5Hash md5;
354 for (const auto &file : s->files) {
355 md5 ^= file.hash;
356 }
357 if (md5 == ci.md5sum) return s->files[0].filename;
358 }
359 return std::nullopt;
360}
361
362template <class Tbase_set>
363/* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo &ci, bool md5sum)
364{
365 return TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::available_sets).has_value() ||
367}
368
373template <class Tbase_set>
375{
376 int n = 0;
377 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
378 if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
379 n++;
380 }
381 return n;
382}
383
388template <class Tbase_set>
390{
391 int n = 0;
392 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
393 if (s == BaseMedia<Tbase_set>::used_set) return n;
394 if (s->GetNumMissing() != 0) continue;
395 n++;
396 }
397 return -1;
398}
399
404template <class Tbase_set>
405/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
406{
407 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
408 if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
409 if (index == 0) return s;
410 index--;
411 }
412 FatalError("Base{}::GetSet(): index {} out of range", BaseSet<Tbase_set>::SET_TYPE, index);
413}
414
419template <class Tbase_set>
420/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
421{
423}
424
429template <class Tbase_set>
Generic functions for replacing base data (graphics, sounds).
std::optional< std::string_view > TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition gfxinit.cpp:113
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.
Base for all base media (graphics, sounds)
static const Tbase_set * GetUsedSet()
Return the used set.
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 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 * GetSet(int index)
Get the name of the graphics set at the specified index.
static Tbase_set * GetAvailableSets()
Return the available sets.
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
static bool SetSetByName(const std::string &name)
Set the set to be used.
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:95
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.
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.
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:51
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
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:118
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:186
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.