OpenTTD
base_media_func.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
15 #include "base_media_base.h"
16 #include "debug.h"
17 #include "ini_type.h"
18 #include "string_func.h"
19 
24 #define fetch_metadata(name) \
25  item = metadata->GetItem(name, false); \
26  if (item == NULL || StrEmpty(item->value)) { \
27  DEBUG(grf, 0, "Base " SET_TYPE "set detail loading: %s field missing.", name); \
28  DEBUG(grf, 0, " Is %s readable for the user running OpenTTD?", full_filename); \
29  return false; \
30  }
31 
40 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
41 bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename)
42 {
43  IniGroup *metadata = ini->GetGroup("metadata");
44  IniItem *item;
45 
46  fetch_metadata("name");
47  this->name = stredup(item->value);
48 
49  fetch_metadata("description");
50  this->description[stredup("")] = stredup(item->value);
51 
52  /* Add the translations of the descriptions too. */
53  for (const IniItem *item = metadata->item; item != NULL; item = item->next) {
54  if (strncmp("description.", item->name, 12) != 0) continue;
55 
56  this->description[stredup(item->name + 12)] = stredup(item->value);
57  }
58 
59  fetch_metadata("shortname");
60  for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
61  this->shortname |= ((uint8)item->value[i]) << (i * 8);
62  }
63 
64  fetch_metadata("version");
65  this->version = atoi(item->value);
66 
67  item = metadata->GetItem("fallback", false);
68  this->fallback = (item != NULL && strcmp(item->value, "0") != 0 && strcmp(item->value, "false") != 0);
69 
70  /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
71  IniGroup *files = ini->GetGroup("files");
72  IniGroup *md5s = ini->GetGroup("md5s");
73  IniGroup *origin = ini->GetGroup("origin");
74  for (uint i = 0; i < Tnum_files; i++) {
75  MD5File *file = &this->files[i];
76  /* Find the filename first. */
78  if (item == NULL || (item->value == NULL && !allow_empty_filename)) {
79  DEBUG(grf, 0, "No " SET_TYPE " file for: %s (in %s)", BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], full_filename);
80  return false;
81  }
82 
83  const char *filename = item->value;
84  if (filename == NULL) {
85  file->filename = NULL;
86  /* If we list no file, that file must be valid */
87  this->valid_files++;
88  this->found_files++;
89  continue;
90  }
91 
92  file->filename = str_fmt("%s%s", path, filename);
93 
94  /* Then find the MD5 checksum */
95  item = md5s->GetItem(filename, false);
96  if (item == NULL || item->value == NULL) {
97  DEBUG(grf, 0, "No MD5 checksum specified for: %s (in %s)", filename, full_filename);
98  return false;
99  }
100  char *c = item->value;
101  for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
102  uint j;
103  if ('0' <= *c && *c <= '9') {
104  j = *c - '0';
105  } else if ('a' <= *c && *c <= 'f') {
106  j = *c - 'a' + 10;
107  } else if ('A' <= *c && *c <= 'F') {
108  j = *c - 'A' + 10;
109  } else {
110  DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s (in %s)", filename, full_filename);
111  return false;
112  }
113  if (i % 2 == 0) {
114  file->hash[i / 2] = j << 4;
115  } else {
116  file->hash[i / 2] |= j;
117  }
118  }
119 
120  /* Then find the warning message when the file's missing */
121  item = origin->GetItem(filename, false);
122  if (item == NULL) item = origin->GetItem("default", false);
123  if (item == NULL) {
124  DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
125  file->missing_warning = stredup("");
126  } else {
127  file->missing_warning = stredup(item->value);
128  }
129 
130  file->check_result = T::CheckMD5(file, BASESET_DIR);
131  switch (file->check_result) {
132  case MD5File::CR_UNKNOWN:
133  break;
134 
135  case MD5File::CR_MATCH:
136  this->valid_files++;
137  this->found_files++;
138  break;
139 
141  DEBUG(grf, 1, "MD5 checksum mismatch for: %s (in %s)", filename, full_filename);
142  this->found_files++;
143  break;
144 
145  case MD5File::CR_NO_FILE:
146  DEBUG(grf, 1, "The file %s specified in %s is missing", filename, full_filename);
147  break;
148  }
149  }
150 
151  return true;
152 }
153 
154 template <class Tbase_set>
155 bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
156 {
157  bool ret = false;
158  DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename);
159 
160  Tbase_set *set = new Tbase_set();
161  IniFile *ini = new IniFile();
162  ini->LoadFromDisk(filename, BASESET_DIR);
163 
164  char *path = stredup(filename + basepath_length);
165  char *psep = strrchr(path, PATHSEPCHAR);
166  if (psep != NULL) {
167  psep[1] = '\0';
168  } else {
169  *path = '\0';
170  }
171 
172  if (set->FillSetDetails(ini, path, filename)) {
173  Tbase_set *duplicate = NULL;
174  for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
175  if (strcmp(c->name, set->name) == 0 || c->shortname == set->shortname) {
176  duplicate = c;
177  break;
178  }
179  }
180  if (duplicate != NULL) {
181  /* The more complete set takes precedence over the version number. */
182  if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
183  duplicate->valid_files > set->valid_files) {
184  DEBUG(grf, 1, "Not adding %s (%i) as base " SET_TYPE " set (duplicate, %s)", set->name, set->version,
185  duplicate->valid_files > set->valid_files ? "less valid files" : "lower version");
188  } else {
189  Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
190  while (*prev != duplicate) prev = &(*prev)->next;
191 
192  *prev = set;
193  set->next = duplicate->next;
194 
195  /* If the duplicate set is currently used (due to rescanning this can happen)
196  * update the currently used set to the new one. This will 'lie' about the
197  * version number until a new game is started which isn't a big problem */
199 
200  DEBUG(grf, 1, "Removing %s (%i) as base " SET_TYPE " set (duplicate, %s)", duplicate->name, duplicate->version,
201  duplicate->valid_files < set->valid_files ? "less valid files" : "lower version");
202  duplicate->next = BaseMedia<Tbase_set>::duplicate_sets;
204  ret = true;
205  }
206  } else {
207  Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
208  while (*last != NULL) last = &(*last)->next;
209 
210  *last = set;
211  ret = true;
212  }
213  if (ret) {
214  DEBUG(grf, 1, "Adding %s (%i) as base " SET_TYPE " set", set->name, set->version);
215  }
216  } else {
217  delete set;
218  }
219  free(path);
220 
221  delete ini;
222  return ret;
223 }
224 
230 template <class Tbase_set>
231 /* static */ bool BaseMedia<Tbase_set>::SetSet(const char *name)
232 {
233  extern void CheckExternalFiles();
234 
235  if (StrEmpty(name)) {
236  if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
238  return true;
239  }
240 
241  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
242  if (strcmp(name, s->name) == 0) {
245  return true;
246  }
247  }
248  return false;
249 }
250 
257 template <class Tbase_set>
258 /* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last)
259 {
260  p += seprintf(p, last, "List of " SET_TYPE " sets:\n");
261  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
262  p += seprintf(p, last, "%18s: %s", s->name, s->GetDescription());
263  int invalid = s->GetNumInvalid();
264  if (invalid != 0) {
265  int missing = s->GetNumMissing();
266  if (missing == 0) {
267  p += seprintf(p, last, " (%i corrupt file%s)\n", invalid, invalid == 1 ? "" : "s");
268  } else {
269  p += seprintf(p, last, " (unusable: %i missing file%s)\n", missing, missing == 1 ? "" : "s");
270  }
271  } else {
272  p += seprintf(p, last, "\n");
273  }
274  }
275  p += seprintf(p, last, "\n");
276 
277  return p;
278 }
279 
280 #if defined(ENABLE_NETWORK)
281 #include "network/network_content.h"
282 
283 template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
284 {
285  for (; s != NULL; s = s->next) {
286  if (s->GetNumMissing() != 0) continue;
287 
288  if (s->shortname != ci->unique_id) continue;
289  if (!md5sum) return s->files[0].filename;
290 
291  byte md5[16];
292  memset(md5, 0, sizeof(md5));
293  for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
294  for (uint j = 0; j < sizeof(md5); j++) {
295  md5[j] ^= s->files[i].hash[j];
296  }
297  }
298  if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return s->files[0].filename;
299  }
300  return NULL;
301 }
302 
303 template <class Tbase_set>
304 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
305 {
306  return (TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::available_sets) != NULL) ||
308 }
309 
310 #else
311 
312 template <class Tbase_set>
313 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
314 {
315  return NULL;
316 }
317 
318 template <class Tbase_set>
319 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
320 {
321  return false;
322 }
323 
324 #endif /* ENABLE_NETWORK */
325 
330 template <class Tbase_set>
332 {
333  int n = 0;
334  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
335  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
336  n++;
337  }
338  return n;
339 }
340 
345 template <class Tbase_set>
347 {
348  int n = 0;
349  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
350  if (s == BaseMedia<Tbase_set>::used_set) return n;
351  if (s->GetNumMissing() != 0) continue;
352  n++;
353  }
354  return -1;
355 }
356 
361 template <class Tbase_set>
362 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
363 {
364  for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
365  if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
366  if (index == 0) return s;
367  index--;
368  }
369  error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
370 }
371 
376 template <class Tbase_set>
377 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
378 {
380 }
381 
386 template <class Tbase_set>
387 /* static */ Tbase_set *BaseMedia<Tbase_set>::GetAvailableSets()
388 {
390 }
391 
397 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
398  template const char *repl_type::ini_set; \
399  template const char *repl_type::GetExtension(); \
400  template bool repl_type::AddFile(const char *filename, size_t pathlength, const char *tar_filename); \
401  template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
402  template bool repl_type::SetSet(const char *name); \
403  template char *repl_type::GetSetsList(char *p, const char *last); \
404  template int repl_type::GetNumSets(); \
405  template int repl_type::GetIndexOfUsedSet(); \
406  template const set_type *repl_type::GetSet(int index); \
407  template const set_type *repl_type::GetUsedSet(); \
408  template bool repl_type::DetermineBestSet(); \
409  template set_type *repl_type::GetAvailableSets(); \
410  template const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const set_type *s);
411 
A group within an ini file.
Definition: ini_type.h:38
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:151
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:77
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:409
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:41
#define SET_TYPE
The type of set we&#39;re replacing.
Definition: music.cpp:16
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition: gfxinit.cpp:121
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:156
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:118
IniItem * next
The next item in this group.
Definition: ini_type.h:26
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:25
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:212
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
char * value
The value of this item.
Definition: ini_type.h:28
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:78
Base for all base media (graphics, sounds)
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Add a file with the given filename.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
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:88
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
The file did exist and the md5 checksum did match.
char * name
The name of this item.
Definition: ini_type.h:27
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:105
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:114
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:58
static const Tbase_set * GetUsedSet()
Return the used set.
uint8 hash[16]
md5 sum of the file