OpenTTD Source: src/base_media_func.h Source File

OpenTTD Source 15.0-RC2

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
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"
17#include "core/string_consumer.hpp"
18#include "3rdparty/fmt/ranges.h"
19
20extern void CheckExternalFiles();
21
28template <class T>
29 void 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>
43 const 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>
60 bool 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.starts_with("description.")) 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;
103 }
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) {
163 case MD5File::CR_UNKNOWN:
164 break;
165
166 case MD5File::CR_MATCH:
167 this->valid_files++;
168 this->found_files++;
169 break;
170
171 case MD5File::CR_MISMATCH:
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
177 case MD5File::CR_NO_FILE:
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>
188 bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &)
189{
190 Debug(misc, 1, "Checking {} for base {} set", filename, BaseSet<Tbase_set>::SET_TYPE);
191
192 auto set = std::make_unique<Tbase_set>();
193 IniFile ini{};
194 std::string path{ filename, basepath_length };
195 ini.LoadFromDisk(path, BASESET_DIR);
196
197 auto psep = path.rfind(PATHSEPCHAR);
198 if (psep != std::string::npos) {
199 path.erase(psep + 1);
200 } else {
201 path.clear();
202 }
203
204 if (!set->FillSetDetails(ini, path, filename)) return false;
205
206 auto existing = std::ranges::find_if(BaseMedia<Tbase_set>::available_sets, [&set](const auto &c) { return c->name == set->name || c->shortname == set->shortname; });
207 if (existing != std::end(BaseMedia<Tbase_set>::available_sets)) {
208 /* The more complete set takes precedence over the version number. */
209 if (((*existing)->valid_files == set->valid_files && (*existing)->version >= set->version) ||
210 (*existing)->valid_files > set->valid_files) {
211
212 Debug(misc, 1, "Not adding {} ({}) as base {} set (duplicate, {})", set->name, fmt::join(set->version, "."),
213 BaseSet<Tbase_set>::SET_TYPE,
214 (*existing)->valid_files > set->valid_files ? "fewer valid files" : "lower version");
215
216 duplicate_sets.push_back(std::move(set));
217 return false;
218 }
219
220 /* If the duplicate set is currently used (due to rescanning this can happen)
221 * update the currently used set to the new one. This will 'lie' about the
222 * version number until a new game is started which isn't a big problem */
223 if (BaseMedia<Tbase_set>::used_set == existing->get()) BaseMedia<Tbase_set>::used_set = set.get();
224
225 /* Keep baseset configuration, if compatible */
226 set->CopyCompatibleConfig(**existing);
227
228 Debug(misc, 1, "Removing {} ({}) as base {} set (duplicate, {})", (*existing)->name, fmt::join((*existing)->version, "."), BaseSet<Tbase_set>::SET_TYPE,
229 (*existing)->valid_files < set->valid_files ? "fewer valid files" : "lower version");
230
231 /* Existing set is worse, move it to duplicates and replace with the current set. */
232 duplicate_sets.push_back(std::move(*existing));
233
234 Debug(misc, 1, "Adding {} ({}) as base {} set", set->name, fmt::join(set->version, "."), BaseSet<Tbase_set>::SET_TYPE);
235 *existing = std::move(set);
236 } else {
237 Debug(grf, 1, "Adding {} ({}) as base {} set", set->name, set->version, BaseSet<Tbase_set>::SET_TYPE);
238 available_sets.push_back(std::move(set));
239 }
240
241 return true;
242}
243
249template <class Tbase_set>
250/* static */ bool BaseMedia<Tbase_set>::SetSet(const Tbase_set *set)
251{
252 if (set == nullptr) {
253 if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
254 } else {
255 BaseMedia<Tbase_set>::used_set = set;
256 }
257 CheckExternalFiles();
258 return true;
259}
260
266template <class Tbase_set>
267/* static */ bool BaseMedia<Tbase_set>::SetSetByName(const std::string &name)
268{
269 if (name.empty()) {
270 return SetSet(nullptr);
271 }
272
273 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
274 if (name == s->name) {
275 return SetSet(s.get());
276 }
277 }
278 return false;
279}
280
286template <class Tbase_set>
287/* static */ bool BaseMedia<Tbase_set>::SetSetByShortname(uint32_t shortname)
288{
289 if (shortname == 0) {
290 return SetSet(nullptr);
291 }
292
293 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
294 if (shortname == s->shortname) {
295 return SetSet(s.get());
296 }
297 }
298 return false;
299}
300
305template <class Tbase_set>
306/* static */ void BaseMedia<Tbase_set>::GetSetsList(std::back_insert_iterator<std::string> &output_iterator)
307{
308 fmt::format_to(output_iterator, "List of {} sets:\n", BaseSet<Tbase_set>::SET_TYPE);
309 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
310 fmt::format_to(output_iterator, "{:>18}: {}", s->name, s->GetDescription({}));
311 int invalid = s->GetNumInvalid();
312 if (invalid != 0) {
313 int missing = s->GetNumMissing();
314 if (missing == 0) {
315 fmt::format_to(output_iterator, " ({} corrupt file{})\n", invalid, invalid == 1 ? "" : "s");
316 } else {
317 fmt::format_to(output_iterator, " (unusable: {} missing file{})\n", missing, missing == 1 ? "" : "s");
318 }
319 } else {
320 fmt::format_to(output_iterator, "\n");
321 }
322 }
323 fmt::format_to(output_iterator, "\n");
324}
325
326#include "network/core/tcp_content_type.h"
327
328 template <class Tbase_set> std::optional<std::string_view> TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span<const std::unique_ptr<Tbase_set>> sets)
329{
330 for (const auto &s : sets) {
331 if (s->GetNumMissing() != 0) continue;
332
333 if (s->shortname != ci.unique_id) continue;
334 if (!md5sum) return s->files[0].filename;
335
336 MD5Hash md5;
337 for (const auto &file : s->files) {
338 md5 ^= file.hash;
339 }
340 if (md5 == ci.md5sum) return s->files[0].filename;
341 }
342 return std::nullopt;
343}
344
345template <class Tbase_set>
346/* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo &ci, bool md5sum)
347{
348 return TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::GetAvailableSets()).has_value() ||
349 TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::GetDuplicateSets()).has_value();
350}
351
356template <class Tbase_set>
357/* static */ int BaseMedia<Tbase_set>::GetNumSets()
358{
359 return std::ranges::count_if(BaseMedia<Tbase_set>::GetAvailableSets(), [](const auto &set) {
360 return set.get() == BaseMedia<Tbase_set>::used_set || set->GetNumMissing() == 0;
361 });
362}
363
368template <class Tbase_set>
369/* static */ int BaseMedia<Tbase_set>::GetIndexOfUsedSet()
370{
371 int n = 0;
372 for (const auto &s : BaseMedia<Tbase_set>::available_sets) {
373 if (s.get() == BaseMedia<Tbase_set>::used_set) return n;
374 if (s->GetNumMissing() != 0) continue;
375 n++;
376 }
377 return -1;
378}
379
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{
402 return BaseMedia<Tbase_set>::used_set;
403}
base_media_base.h
Generic functions for replacing base data (graphics, sounds).
CheckExternalFiles
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition gfxinit.cpp:113
TryGetBaseSetFile
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.
Definition base_media_func.h:328
std::byteswap
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.
Definition bitmath_func.hpp:330
BaseMedia
Base for all base media (graphics, sounds)
Definition base_media_base.h:163
BaseMedia::GetUsedSet
static const Tbase_set * GetUsedSet()
Return the used set.
Definition base_media_func.h:400
BaseMedia::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition base_media_func.h:188
BaseMedia::SetSetByShortname
static bool SetSetByShortname(uint32_t shortname)
Set the set to be used.
Definition base_media_func.h:287
BaseMedia::GetSetsList
static void GetSetsList(std::back_insert_iterator< std::string > &output_iterator)
Returns a list with the sets.
Definition base_media_func.h:306
BaseMedia::GetNumSets
static int GetNumSets()
Count the number of available graphics sets.
Definition base_media_func.h:357
BaseMedia::HasSet
static bool HasSet(const ContentInfo &ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
Definition base_media_func.h:346
BaseMedia::GetSet
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
Definition base_media_func.h:385
BaseMedia::GetIndexOfUsedSet
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
Definition base_media_func.h:369
BaseMedia::SetSetByName
static bool SetSetByName(const std::string &name)
Set the set to be used.
Definition base_media_func.h:267
BaseMedia::SetSet
static bool SetSet(const Tbase_set *set)
Set the set to be used.
Definition base_media_func.h:250
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
error_func.h
Error reporting related functions.
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:96
ini_type.h
Types related to reading/writing '*.ini' files.
ConvertHexToBytes
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
string_consumer.hpp
Parse strings.
string_func.h
Functions related to low-level strings.
BaseSet
Information about a single base set.
Definition base_media_base.h:50
BaseSet::valid_files
uint valid_files
Number of the files that could be found and are valid.
Definition base_media_base.h:71
BaseSet::LogError
void LogError(std::string_view full_filename, std::string_view detail, int level=0) const
Log error from reading basesets.
Definition base_media_func.h:29
BaseSet::GetMandatoryItem
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.
Definition base_media_func.h:43
BaseSet::GetFilenames
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
BaseSet::FillSetDetails
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.
Definition base_media_func.h:60
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:56
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition tcp_content_type.h:75
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition tcp_content_type.h:76
IniFile
Ini file that supports both loading and saving.
Definition ini_type.h:86
IniGroup
A group within an ini file.
Definition ini_type.h:34
IniGroup::GetItem
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:51
IniGroup::name
std::string name
name of group
Definition ini_type.h:37
IniGroup::items
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
IniItem
A single "line" in an ini file.
Definition ini_type.h:23
IniItem::value
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
IniLoadFile::GetGroup
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:118
IniLoadFile::LoadFromDisk
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:185
MD5File
Structure holding filename and MD5 information about a single file.
Definition base_media_base.h:25
MD5File::missing_warning
std::string missing_warning
warning when this file is missing
Definition base_media_base.h:36
MD5File::check_result
ChecksumResult check_result
cached result of md5 check
Definition base_media_base.h:37
MD5File::CR_MATCH
@ CR_MATCH
The file did exist and the md5 checksum did match.
Definition base_media_base.h:29
MD5File::CR_MISMATCH
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
Definition base_media_base.h:30
MD5File::CR_NO_FILE
@ CR_NO_FILE
The file did not exist.
Definition base_media_base.h:31
MD5File::CR_UNKNOWN
@ CR_UNKNOWN
The file has not been checked yet.
Definition base_media_base.h:28
MD5File::hash
MD5Hash hash
md5 sum of the file
Definition base_media_base.h:35
MD5File::filename
std::string filename
filename
Definition base_media_base.h:34
tcp_content_type.h
Basic types related to the content on the content server.