OpenTTD
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 
22 #define fetch_metadata(name) \
23  item = metadata->GetItem(name, false); \
24  if (item == nullptr || StrEmpty(item->value)) { \
25  DEBUG(grf, 0, "Base " SET_TYPE "set detail loading: %s field missing.", name); \
26  DEBUG(grf, 0, " Is %s readable for the user running OpenTTD?", full_filename); \
27  return false; \
28  }
29 
38 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
39 bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename)
40 {
41  IniGroup *metadata = ini->GetGroup("metadata");
42  IniItem *item;
43 
44  fetch_metadata("name");
45  this->name = stredup(item->value);
46 
47  fetch_metadata("description");
48  this->description[stredup("")] = stredup(item->value);
49 
50  /* Add the translations of the descriptions too. */
51  for (const IniItem *item = metadata->item; item != nullptr; item = item->next) {
52  if (strncmp("description.", item->name, 12) != 0) continue;
53 
54  this->description[stredup(item->name + 12)] = stredup(item->value);
55  }
56 
57  fetch_metadata("shortname");
58  for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
59  this->shortname |= ((uint8)item->value[i]) << (i * 8);
60  }
61 
62  fetch_metadata("version");
63  this->version = atoi(item->value);
64 
65  item = metadata->GetItem("fallback", false);
66  this->fallback = (item != nullptr && strcmp(item->value, "0") != 0 && strcmp(item->value, "false") != 0);
67 
68  /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
69  IniGroup *files = ini->GetGroup("files");
70  IniGroup *md5s = ini->GetGroup("md5s");
71  IniGroup *origin = ini->GetGroup("origin");
72  for (uint i = 0; i < Tnum_files; i++) {
73  MD5File *file = &this->files[i];
74  /* Find the filename first. */
76  if (item == nullptr || (item->value == nullptr && !allow_empty_filename)) {
77  DEBUG(grf, 0, "No " SET_TYPE " file for: %s (in %s)", BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], full_filename);
78  return false;
79  }
80 
81  const char *filename = item->value;
82  if (filename == nullptr) {
83  file->filename = nullptr;
84  /* If we list no file, that file must be valid */
85  this->valid_files++;
86  this->found_files++;
87  continue;
88  }
89 
90  file->filename = str_fmt("%s%s", path, filename);
91 
92  /* Then find the MD5 checksum */
93  item = md5s->GetItem(filename, false);
94  if (item == nullptr || item->value == nullptr) {
95  DEBUG(grf, 0, "No MD5 checksum specified for: %s (in %s)", filename, full_filename);
96  return false;
97  }
98  char *c = item->value;
99  for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
100  uint j;
101  if ('0' <= *c && *c <= '9') {
102  j = *c - '0';
103  } else if ('a' <= *c && *c <= 'f') {
104  j = *c - 'a' + 10;
105  } else if ('A' <= *c && *c <= 'F') {
106  j = *c - 'A' + 10;
107  } else {
108  DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s (in %s)", filename, full_filename);
109  return false;
110  }
111  if (i % 2 == 0) {
112  file->hash[i / 2] = j << 4;
113  } else {
114  file->hash[i / 2] |= j;
115  }
116  }
117 
118  /* Then find the warning message when the file's missing */
119  item = origin->GetItem(filename, false);
120  if (item == nullptr) item = origin->GetItem("default", false);
121  if (item == nullptr) {
122  DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
123  file->missing_warning = stredup("");
124  } else {
125  file->missing_warning = stredup(item->value);
126  }
127 
128  file->check_result = T::CheckMD5(file, BASESET_DIR);
129  switch (file->check_result) {
130  case MD5File::CR_UNKNOWN:
131  break;
132 
133  case MD5File::CR_MATCH:
134  this->valid_files++;
135  this->found_files++;
136  break;
137 
139  DEBUG(grf, 1, "MD5 checksum mismatch for: %s (in %s)", filename, full_filename);
140  this->found_files++;
141  break;
142 
143  case MD5File::CR_NO_FILE:
144  DEBUG(grf, 1, "The file %s specified in %s is missing", filename, full_filename);
145  break;
146  }
147  }
148 
149  return true;
150 }
151 
152 template <class Tbase_set>
153 bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
154 {
155  bool ret = false;
156  DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename);
157 
158  Tbase_set *set = new Tbase_set();
159  IniFile *ini = new IniFile();
160  char *path = stredup(filename + basepath_length);
161  ini->LoadFromDisk(path, BASESET_DIR);
162 
163  char *psep = strrchr(path, PATHSEPCHAR);
164  if (psep != nullptr) {
165  psep[1] = '\0';
166  } else {
167  *path = '\0';
168  }
169 
170  if (set->FillSetDetails(ini, path, filename)) {
171  Tbase_set *duplicate = nullptr;
172  for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
173  if (strcmp(c->name, set->name) == 0 || c->shortname == set->shortname) {
174  duplicate = c;
175  break;
176  }
177  }
178  if (duplicate != nullptr) {
179  /* The more complete set takes precedence over the version number. */
180  if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
181  duplicate->valid_files > set->valid_files) {
182  DEBUG(grf, 1, "Not adding %s (%i) as base " SET_TYPE " set (duplicate, %s)", set->name, set->version,
183  duplicate->valid_files > set->valid_files ? "less valid files" : "lower version");
186  } else {
187  Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
188  while (*prev != duplicate) prev = &(*prev)->next;
189 
190  *prev = set;
191  set->next = duplicate->next;
192 
193  /* If the duplicate set is currently used (due to rescanning this can happen)
194  * update the currently used set to the new one. This will 'lie' about the
195  * version number until a new game is started which isn't a big problem */
197 
198  DEBUG(grf, 1, "Removing %s (%i) as base " SET_TYPE " set (duplicate, %s)", duplicate->name, duplicate->version,
199  duplicate->valid_files < set->valid_files ? "less valid files" : "lower version");
200  duplicate->next = BaseMedia<Tbase_set>::duplicate_sets;
202  ret = true;
203  }
204  } else {
205  Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
206  while (*last != nullptr) last = &(*last)->next;
207 
208  *last = set;
209  ret = true;
210  }
211  if (ret) {
212  DEBUG(grf, 1, "Adding %s (%i) as base " SET_TYPE " set", set->name, set->version);
213  }
214  } else {
215  delete set;
216  }
217  free(path);
218 
219  delete ini;
220  return ret;
221 }
222 
228 template <class Tbase_set>
229 /* static */ bool BaseMedia<Tbase_set>::SetSet(const char *name)
230 {
231  extern void CheckExternalFiles();
232 
233  if (StrEmpty(name)) {
234  if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
236  return true;
237  }
238 
239  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
240  if (strcmp(name, s->name) == 0) {
243  return true;
244  }
245  }
246  return false;
247 }
248 
255 template <class Tbase_set>
256 /* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last)
257 {
258  p += seprintf(p, last, "List of " SET_TYPE " sets:\n");
259  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
260  p += seprintf(p, last, "%18s: %s", s->name, s->GetDescription());
261  int invalid = s->GetNumInvalid();
262  if (invalid != 0) {
263  int missing = s->GetNumMissing();
264  if (missing == 0) {
265  p += seprintf(p, last, " (%i corrupt file%s)\n", invalid, invalid == 1 ? "" : "s");
266  } else {
267  p += seprintf(p, last, " (unusable: %i missing file%s)\n", missing, missing == 1 ? "" : "s");
268  }
269  } else {
270  p += seprintf(p, last, "\n");
271  }
272  }
273  p += seprintf(p, last, "\n");
274 
275  return p;
276 }
277 
278 #include "network/network_content.h"
279 
280 template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
281 {
282  for (; s != nullptr; s = s->next) {
283  if (s->GetNumMissing() != 0) continue;
284 
285  if (s->shortname != ci->unique_id) continue;
286  if (!md5sum) return s->files[0].filename;
287 
288  byte md5[16];
289  memset(md5, 0, sizeof(md5));
290  for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
291  for (uint j = 0; j < sizeof(md5); j++) {
292  md5[j] ^= s->files[i].hash[j];
293  }
294  }
295  if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return s->files[0].filename;
296  }
297  return nullptr;
298 }
299 
300 template <class Tbase_set>
301 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
302 {
303  return (TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::available_sets) != nullptr) ||
305 }
306 
311 template <class Tbase_set>
313 {
314  int n = 0;
315  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
316  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
317  n++;
318  }
319  return n;
320 }
321 
326 template <class Tbase_set>
328 {
329  int n = 0;
330  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
331  if (s == BaseMedia<Tbase_set>::used_set) return n;
332  if (s->GetNumMissing() != 0) continue;
333  n++;
334  }
335  return -1;
336 }
337 
342 template <class Tbase_set>
343 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
344 {
345  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
346  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
347  if (index == 0) return s;
348  index--;
349  }
350  error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
351 }
352 
357 template <class Tbase_set>
358 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
359 {
361 }
362 
367 template <class Tbase_set>
368 /* static */ Tbase_set *BaseMedia<Tbase_set>::GetAvailableSets()
369 {
371 }
372 
378 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
379  template const char *repl_type::ini_set; \
380  template const char *repl_type::GetExtension(); \
381  template bool repl_type::AddFile(const char *filename, size_t pathlength, const char *tar_filename); \
382  template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
383  template bool repl_type::SetSet(const char *name); \
384  template char *repl_type::GetSetsList(char *p, const char *last); \
385  template int repl_type::GetNumSets(); \
386  template int repl_type::GetIndexOfUsedSet(); \
387  template const set_type *repl_type::GetSet(int index); \
388  template const set_type *repl_type::GetUsedSet(); \
389  template bool repl_type::DetermineBestSet(); \
390  template set_type *repl_type::GetAvailableSets(); \
391  template const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const set_type *s);
392 
A group within an ini file.
Definition: ini_type.h:36
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:149
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:73
ChecksumResult check_result
cached result of md5 check
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
Structure holding filename and MD5 information about a single file.
Functions related to debugging.
The file did not exist.
Generic functions for replacing base data (graphics, sounds).
IniItem * item
the first item in the group
Definition: ini_type.h:39
#define SET_TYPE
The type of set we&#39;re replacing.
Definition: music.cpp:14
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition: gfxinit.cpp:119
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:154
static bool SetSet(const char *name)
Set the set to be used.
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
IniItem * next
The next item in this group.
Definition: ini_type.h:24
static int GetNumSets()
Count the number of available graphics sets.
The file did exist, just the md5 checksum did not match.
A single "line" in an ini file.
Definition: ini_type.h:23
const char * missing_warning
warning when this file is missing
Functions related to low-level strings.
const char * filename
filename
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:210
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
char * value
The value of this item.
Definition: ini_type.h:26
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
Part of the network protocol handling content distribution.
#define fetch_metadata(name)
Try to read a single piece of metadata and return false if it doesn&#39;t exist.
Types related to reading/writing &#39;*.ini&#39; files.
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:74
Base for all base media (graphics, sounds)
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
static Tbase_set * GetAvailableSets()
Return the available sets.
Ini file that supports both loading and saving.
Definition: ini_type.h:86
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
The file did exist and the md5 checksum did match.
char * name
The name of this item.
Definition: ini_type.h:25
bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
static char * GetSetsList(char *p, const char *last)
Returns a list with the sets.
IniItem * GetItem(const char *name, bool create)
Get the item with the given name, and if it doesn&#39;t exist and create is true it creates a new item...
Definition: ini_load.cpp:103
Information about a single base set.
The file has not been checked yet.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there&#39;s a base set matching some information.
Container for all important information about a piece of content.
Definition: tcp_content.h:54
static const Tbase_set * GetUsedSet()
Return the used set.
uint8 hash[16]
md5 sum of the file