OpenTTD Source  20241108-master-g80f628063a
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 
13 #include "base_media_base.h"
14 #include "debug.h"
15 #include "ini_type.h"
16 #include "string_func.h"
17 #include "error_func.h"
18 
19 extern void CheckExternalFiles();
20 
25 #define fetch_metadata(name) \
26  item = metadata->GetItem(name); \
27  if (item == nullptr || !item->value.has_value() || item->value->empty()) { \
28  Debug(grf, 0, "Base " SET_TYPE "set detail loading: {} field missing.", name); \
29  Debug(grf, 0, " Is {} readable for the user running OpenTTD?", full_filename); \
30  return false; \
31  }
32 
41 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
42 bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename)
43 {
44  const IniGroup *metadata = ini.GetGroup("metadata");
45  if (metadata == nullptr) {
46  Debug(grf, 0, "Base " SET_TYPE "set detail loading: metadata missing.");
47  Debug(grf, 0, " Is {} readable for the user running OpenTTD?", full_filename);
48  return false;
49  }
50  const IniItem *item;
51 
52  fetch_metadata("name");
53  this->name = *item->value;
54 
55  fetch_metadata("description");
56  this->description[std::string{}] = *item->value;
57 
58  item = metadata->GetItem("url");
59  if (item != nullptr) this->url = *item->value;
60 
61  /* Add the translations of the descriptions too. */
62  for (const IniItem &titem : metadata->items) {
63  if (titem.name.compare(0, 12, "description.") != 0) continue;
64 
65  this->description[titem.name.substr(12)] = titem.value.value_or("");
66  }
67 
68  fetch_metadata("shortname");
69  for (uint i = 0; (*item->value)[i] != '\0' && i < 4; i++) {
70  this->shortname |= ((uint8_t)(*item->value)[i]) << (i * 8);
71  }
72 
73  fetch_metadata("version");
74  this->version = atoi(item->value->c_str());
75 
76  item = metadata->GetItem("fallback");
77  this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false");
78 
79  /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
80  const IniGroup *files = ini.GetGroup("files");
81  const IniGroup *md5s = ini.GetGroup("md5s");
82  const IniGroup *origin = ini.GetGroup("origin");
83  for (uint i = 0; i < Tnum_files; i++) {
84  MD5File *file = &this->files[i];
85  /* Find the filename first. */
86  item = files != nullptr ? files->GetItem(BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i]) : nullptr;
87  if (item == nullptr || (!item->value.has_value() && !allow_empty_filename)) {
88  Debug(grf, 0, "No " SET_TYPE " file for: {} (in {})", BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], full_filename);
89  return false;
90  }
91 
92  if (!item->value.has_value()) {
93  file->filename.clear();
94  /* If we list no file, that file must be valid */
95  this->valid_files++;
96  this->found_files++;
97  continue;
98  }
99 
100  const std::string &filename = item->value.value();
101  file->filename = path + filename;
102 
103  /* Then find the MD5 checksum */
104  item = md5s != nullptr ? md5s->GetItem(filename) : nullptr;
105  if (item == nullptr || !item->value.has_value()) {
106  Debug(grf, 0, "No MD5 checksum specified for: {} (in {})", filename, full_filename);
107  return false;
108  }
109  const char *c = item->value->c_str();
110  for (size_t i = 0; i < file->hash.size() * 2; i++, c++) {
111  uint j;
112  if ('0' <= *c && *c <= '9') {
113  j = *c - '0';
114  } else if ('a' <= *c && *c <= 'f') {
115  j = *c - 'a' + 10;
116  } else if ('A' <= *c && *c <= 'F') {
117  j = *c - 'A' + 10;
118  } else {
119  Debug(grf, 0, "Malformed MD5 checksum specified for: {} (in {})", filename, full_filename);
120  return false;
121  }
122  if (i % 2 == 0) {
123  file->hash[i / 2] = j << 4;
124  } else {
125  file->hash[i / 2] |= j;
126  }
127  }
128 
129  /* Then find the warning message when the file's missing */
130  item = origin != nullptr ? origin->GetItem(filename) : nullptr;
131  if (item == nullptr) item = origin != nullptr ? origin->GetItem("default") : nullptr;
132  if (item == nullptr || !item->value.has_value()) {
133  Debug(grf, 1, "No origin warning message specified for: {}", filename);
134  file->missing_warning.clear();
135  } else {
136  file->missing_warning = item->value.value();
137  }
138 
139  file->check_result = T::CheckMD5(file, BASESET_DIR);
140  switch (file->check_result) {
141  case MD5File::CR_UNKNOWN:
142  break;
143 
144  case MD5File::CR_MATCH:
145  this->valid_files++;
146  this->found_files++;
147  break;
148 
150  Debug(grf, 1, "MD5 checksum mismatch for: {} (in {})", filename, full_filename);
151  this->found_files++;
152  break;
153 
154  case MD5File::CR_NO_FILE:
155  Debug(grf, 1, "The file {} specified in {} is missing", filename, full_filename);
156  break;
157  }
158  }
159 
160  return true;
161 }
162 
163 template <class Tbase_set>
164 bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &)
165 {
166  bool ret = false;
167  Debug(grf, 1, "Checking {} for base " SET_TYPE " set", filename);
168 
169  Tbase_set *set = new Tbase_set();
170  IniFile ini{};
171  std::string path{ filename, basepath_length };
172  ini.LoadFromDisk(path, BASESET_DIR);
173 
174  auto psep = path.rfind(PATHSEPCHAR);
175  if (psep != std::string::npos) {
176  path.erase(psep + 1);
177  } else {
178  path.clear();
179  }
180 
181  if (set->FillSetDetails(ini, path, filename)) {
182  Tbase_set *duplicate = nullptr;
183  for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
184  if (c->name == set->name || c->shortname == set->shortname) {
185  duplicate = c;
186  break;
187  }
188  }
189  if (duplicate != nullptr) {
190  /* The more complete set takes precedence over the version number. */
191  if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
192  duplicate->valid_files > set->valid_files) {
193  Debug(grf, 1, "Not adding {} ({}) as base " SET_TYPE " set (duplicate, {})", set->name, set->version,
194  duplicate->valid_files > set->valid_files ? "less valid files" : "lower version");
197  } else {
198  Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
199  while (*prev != duplicate) prev = &(*prev)->next;
200 
201  *prev = set;
202  set->next = duplicate->next;
203 
204  /* Keep baseset configuration, if compatible */
205  set->CopyCompatibleConfig(*duplicate);
206 
207  /* If the duplicate set is currently used (due to rescanning this can happen)
208  * update the currently used set to the new one. This will 'lie' about the
209  * version number until a new game is started which isn't a big problem */
211 
212  Debug(grf, 1, "Removing {} ({}) as base " SET_TYPE " set (duplicate, {})", duplicate->name, duplicate->version,
213  duplicate->valid_files < set->valid_files ? "less valid files" : "lower version");
216  ret = true;
217  }
218  } else {
219  Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
220  while (*last != nullptr) last = &(*last)->next;
221 
222  *last = set;
223  ret = true;
224  }
225  if (ret) {
226  Debug(grf, 1, "Adding {} ({}) as base " SET_TYPE " set", set->name, set->version);
227  }
228  } else {
229  delete set;
230  }
231 
232  return ret;
233 }
234 
240 template <class Tbase_set>
241 /* static */ bool BaseMedia<Tbase_set>::SetSet(const Tbase_set *set)
242 {
243  if (set == nullptr) {
244  if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
245  } else {
247  }
249  return true;
250 }
251 
257 template <class Tbase_set>
258 /* static */ bool BaseMedia<Tbase_set>::SetSetByName(const std::string &name)
259 {
260  if (name.empty()) {
261  return SetSet(nullptr);
262  }
263 
264  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
265  if (name == s->name) {
266  return SetSet(s);
267  }
268  }
269  return false;
270 }
271 
277 template <class Tbase_set>
278 /* static */ bool BaseMedia<Tbase_set>::SetSetByShortname(uint32_t shortname)
279 {
280  if (shortname == 0) {
281  return SetSet(nullptr);
282  }
283 
284  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
285  if (shortname == s->shortname) {
286  return SetSet(s);
287  }
288  }
289  return false;
290 }
291 
296 template <class Tbase_set>
297 /* static */ void BaseMedia<Tbase_set>::GetSetsList(std::back_insert_iterator<std::string> &output_iterator)
298 {
299  fmt::format_to(output_iterator, "List of " SET_TYPE " sets:\n");
300  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
301  fmt::format_to(output_iterator, "{:>18}: {}", s->name, s->GetDescription({}));
302  int invalid = s->GetNumInvalid();
303  if (invalid != 0) {
304  int missing = s->GetNumMissing();
305  if (missing == 0) {
306  fmt::format_to(output_iterator, " ({} corrupt file{})\n", invalid, invalid == 1 ? "" : "s");
307  } else {
308  fmt::format_to(output_iterator, " (unusable: {} missing file{})\n", missing, missing == 1 ? "" : "s");
309  }
310  } else {
311  fmt::format_to(output_iterator, "\n");
312  }
313  }
314  fmt::format_to(output_iterator, "\n");
315 }
316 
318 
319 template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
320 {
321  for (; s != nullptr; s = s->next) {
322  if (s->GetNumMissing() != 0) continue;
323 
324  if (s->shortname != ci->unique_id) continue;
325  if (!md5sum) return s->files[0].filename.c_str();
326 
327  MD5Hash md5;
328  for (const auto &file : s->files) {
329  md5 ^= file.hash;
330  }
331  if (md5 == ci->md5sum) return s->files[0].filename.c_str();
332  }
333  return nullptr;
334 }
335 
336 template <class Tbase_set>
337 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
338 {
339  return (TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::available_sets) != nullptr) ||
341 }
342 
347 template <class Tbase_set>
349 {
350  int n = 0;
351  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
352  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
353  n++;
354  }
355  return n;
356 }
357 
362 template <class Tbase_set>
364 {
365  int n = 0;
366  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
367  if (s == BaseMedia<Tbase_set>::used_set) return n;
368  if (s->GetNumMissing() != 0) continue;
369  n++;
370  }
371  return -1;
372 }
373 
378 template <class Tbase_set>
379 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
380 {
381  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
382  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
383  if (index == 0) return s;
384  index--;
385  }
386  FatalError("Base" SET_TYPE "::GetSet(): index {} out of range", index);
387 }
388 
393 template <class Tbase_set>
394 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
395 {
397 }
398 
403 template <class Tbase_set>
404 /* static */ Tbase_set *BaseMedia<Tbase_set>::GetAvailableSets()
405 {
407 }
408 
414 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
415  template const char *repl_type::GetExtension(); \
416  template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \
417  template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
418  template bool repl_type::SetSet(const set_type *set); \
419  template bool repl_type::SetSetByName(const std::string &name); \
420  template bool repl_type::SetSetByShortname(uint32_t shortname); \
421  template void repl_type::GetSetsList(std::back_insert_iterator<std::string> &output_iterator); \
422  template int repl_type::GetNumSets(); \
423  template int repl_type::GetIndexOfUsedSet(); \
424  template const set_type *repl_type::GetSet(int index); \
425  template const set_type *repl_type::GetUsedSet(); \
426  template bool repl_type::DetermineBestSet(); \
427  template set_type *repl_type::GetAvailableSets(); \
428  template const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const set_type *s);
429 
Generic functions for replacing base data (graphics, sounds).
#define fetch_metadata(name)
Try to read a single piece of metadata and return false if it doesn't exist.
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition: gfxinit.cpp:114
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
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 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 HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
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.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut 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:123
Types related to reading/writing '*.ini' files.
#define SET_TYPE
The type of set we're replacing.
Definition: music.cpp:15
Functions related to low-level strings.
Information about a single 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.
Definition: ini_type.h:88
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:52
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
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:187
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition: ini_load.cpp:119
Structure holding filename and MD5 information about a single file.
std::string missing_warning
warning when this file is missing
@ 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.
ChecksumResult check_result
cached result of md5 check
MD5Hash hash
md5 sum of the file
std::string filename
filename
Basic types related to the content on the content server.