# base_media_func.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "base_media_base.h"
    00013 #include "debug.h"
    00014 #include "ini_type.h"
    00015 #include "string_func.h"
    00016
    00017 template <class Tbase_set> /* static */ const char *BaseMedia<Tbase_set>::ini_set;
    00018 template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
    00019 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
    00020 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
    00021
    00026 #define fetch_metadata(name) \
    00027   item = metadata->GetItem(name, false); \
    00028   if (item == NULL || StrEmpty(item->value)) { \
    00029     DEBUG(grf, 0, "Base " SET_TYPE "set detail loading: %s field missing.", name); \
    00030     DEBUG(grf, 0, "  Is %s readable for the user running OpenTTD?", full_filename); \
    00031     return false; \
    00032   }
    00033
    00042 template <class T, size_t Tnum_files, Subdirectory Tsubdir>
    00043 bool BaseSet<T, Tnum_files, Tsubdir>::FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename)
    00044 {
    00045   memset(this, 0, sizeof(*this));
    00046
    00047   IniGroup *metadata = ini->GetGroup("metadata");
    00048   IniItem *item;
    00049
    00050   fetch_metadata("name");
    00051   this->name = strdup(item->value);
    00052
    00053   fetch_metadata("description");
    00054   this->description[strdup("")] = strdup(item->value);
    00055
    00056   /* Add the translations of the descriptions too. */
    00057   for (const IniItem *item = metadata->item; item != NULL; item = item->next) {
    00058     if (strncmp("description.", item->name, 12) != 0) continue;
    00059
    00060     this->description[strdup(item->name + 12)] = strdup(item->value);
    00061   }
    00062
    00063   fetch_metadata("shortname");
    00064   for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
    00065     this->shortname |= ((uint8)item->value[i]) << (i * 8);
    00066   }
    00067
    00068   fetch_metadata("version");
    00069   this->version = atoi(item->value);
    00070
    00071   item = metadata->GetItem("fallback", false);
    00072   this->fallback = (item != NULL && strcmp(item->value, "0") != 0 && strcmp(item->value, "false") != 0);
    00073
    00074   /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
    00075   IniGroup *files  = ini->GetGroup("files");
    00076   IniGroup *md5s   = ini->GetGroup("md5s");
    00077   IniGroup *origin = ini->GetGroup("origin");
    00078   for (uint i = 0; i < Tnum_files; i++) {
    00079     MD5File *file = &this->files[i];
    00080     /* Find the filename first. */
    00081     item = files->GetItem(BaseSet<T, Tnum_files, Tsubdir>::file_names[i], false);
    00082     if (item == NULL || (item->value == NULL && !allow_empty_filename)) {
    00083       DEBUG(grf, 0, "No " SET_TYPE " file for: %s (in %s)", BaseSet<T, Tnum_files, Tsubdir>::file_names[i], full_filename);
    00084       return false;
    00085     }
    00086
    00087     const char *filename = item->value;
    00088     if (filename == NULL) {
    00089       file->filename = NULL;
    00090       /* If we list no file, that file must be valid */
    00091       this->valid_files++;
    00092       this->found_files++;
    00093       continue;
    00094     }
    00095
    00096     file->filename = str_fmt("%s%s", path, filename);
    00097
    00098     /* Then find the MD5 checksum */
    00099     item = md5s->GetItem(filename, false);
    00100     if (item == NULL) {
    00101       DEBUG(grf, 0, "No MD5 checksum specified for: %s (in %s)", filename, full_filename);
    00102       return false;
    00103     }
    00104     char *c = item->value;
    00105     for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
    00106       uint j;
    00107       if ('0' <= *c && *c <= '9') {
    00108         j = *c - '0';
    00109       } else if ('a' <= *c && *c <= 'f') {
    00110         j = *c - 'a' + 10;
    00111       } else if ('A' <= *c && *c <= 'F') {
    00112         j = *c - 'A' + 10;
    00113       } else {
    00114         DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s (in %s)", filename, full_filename);
    00115         return false;
    00116       }
    00117       if (i % 2 == 0) {
    00118         file->hash[i / 2] = j << 4;
    00119       } else {
    00120         file->hash[i / 2] |= j;
    00121       }
    00122     }
    00123
    00124     /* Then find the warning message when the file's missing */
    00125     item = filename == NULL ? NULL : origin->GetItem(filename, false);
    00126     if (item == NULL) item = origin->GetItem("default", false);
    00127     if (item == NULL) {
    00128       DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
    00129       file->missing_warning = strdup("");
    00130     } else {
    00131       file->missing_warning = strdup(item->value);
    00132     }
    00133
    00134     switch (file->CheckMD5(Tsubdir)) {
    00135       case MD5File::CR_MATCH:
    00136         this->valid_files++;
    00137         /* FALL THROUGH */
    00138       case MD5File::CR_MISMATCH:
    00139         this->found_files++;
    00140         break;
    00141
    00142       case MD5File::CR_NO_FILE:
    00143         break;
    00144     }
    00145   }
    00146
    00147   return true;
    00148 }
    00149
    00150 template <class Tbase_set>
    00151 bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length)
    00152 {
    00153   bool ret = false;
    00154   DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename);
    00155
    00156   Tbase_set *set = new Tbase_set();
    00157   IniFile *ini = new IniFile();
    00158   ini->LoadFromDisk(filename);
    00159
    00160   char *path = strdup(filename + basepath_length);
    00161   char *psep = strrchr(path, PATHSEPCHAR);
    00162   if (psep != NULL) {
    00163     psep[1] = '\0';
    00164   } else {
    00165     *path = '\0';
    00166   }
    00167
    00168   if (set->FillSetDetails(ini, path, filename)) {
    00169     Tbase_set *duplicate = NULL;
    00170     for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
    00171       if (strcmp(c->name, set->name) == 0 || c->shortname == set->shortname) {
    00172         duplicate = c;
    00173         break;
    00174       }
    00175     }
    00176     if (duplicate != NULL) {
    00177       /* The more complete set takes precedence over the version number. */
    00178       if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
    00179           duplicate->valid_files > set->valid_files) {
    00180         DEBUG(grf, 1, "Not adding %s (%i) as base " SET_TYPE " set (duplicate)", set->name, set->version);
    00181         set->next = BaseMedia<Tbase_set>::duplicate_sets;
    00182         BaseMedia<Tbase_set>::duplicate_sets = set;
    00183       } else {
    00184         Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
    00185         while (*prev != duplicate) prev = &(*prev)->next;
    00186
    00187         *prev = set;
    00188         set->next = duplicate->next;
    00189
    00190         /* If the duplicate set is currently used (due to rescanning this can happen)
    00191          * update the currently used set to the new one. This will 'lie' about the
    00192          * version number until a new game is started which isn't a big problem */
    00193         if (BaseMedia<Tbase_set>::used_set == duplicate) BaseMedia<Tbase_set>::used_set = set;
    00194
    00195         DEBUG(grf, 1, "Removing %s (%i) as base " SET_TYPE " set (duplicate)", duplicate->name, duplicate->version);
    00196         duplicate->next = BaseMedia<Tbase_set>::duplicate_sets;
    00197         BaseMedia<Tbase_set>::duplicate_sets = duplicate;
    00198         ret = true;
    00199       }
    00200     } else {
    00201       Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
    00202       while (*last != NULL) last = &(*last)->next;
    00203
    00204       *last = set;
    00205       ret = true;
    00206     }
    00207     if (ret) {
    00208       DEBUG(grf, 1, "Adding %s (%i) as base " SET_TYPE " set", set->name, set->version);
    00209     }
    00210   } else {
    00211     delete set;
    00212   }
    00213   free(path);
    00214
    00215   delete ini;
    00216   return ret;
    00217 }
    00218
    00224 template <class Tbase_set>
    00225 /* static */ bool BaseMedia<Tbase_set>::SetSet(const char *name)
    00226 {
    00227   extern void CheckExternalFiles();
    00228
    00229   if (StrEmpty(name)) {
    00230     if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
    00231     CheckExternalFiles();
    00232     return true;
    00233   }
    00234
    00235   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
    00236     if (strcmp(name, s->name) == 0) {
    00237       BaseMedia<Tbase_set>::used_set = s;
    00238       CheckExternalFiles();
    00239       return true;
    00240     }
    00241   }
    00242   return false;
    00243 }
    00244
    00251 template <class Tbase_set>
    00252 /* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last)
    00253 {
    00254   p += seprintf(p, last, "List of " SET_TYPE " sets:\n");
    00255   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
    00256     p += seprintf(p, last, "%18s: %s", s->name, s->GetDescription());
    00257     int invalid = s->GetNumInvalid();
    00258     if (invalid != 0) {
    00259       int missing = s->GetNumMissing();
    00260       if (missing == 0) {
    00261         p += seprintf(p, last, " (%i corrupt file%s)\n", invalid, invalid == 1 ? "" : "s");
    00262       } else {
    00263         p += seprintf(p, last, " (unuseable: %i missing file%s)\n", missing, missing == 1 ? "" : "s");
    00264       }
    00265     } else {
    00266       p += seprintf(p, last, "\n");
    00267     }
    00268   }
    00269   p += seprintf(p, last, "\n");
    00270
    00271   return p;
    00272 }
    00273
    00274 #if defined(ENABLE_NETWORK)
    00275 #include "network/network_content.h"
    00276
    00277 template <class Tbase_set> bool HasBaseSet(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
    00278 {
    00279   for (; s != NULL; s = s->next) {
    00280     if (s->GetNumMissing() != 0) continue;
    00281
    00282     if (s->shortname != ci->unique_id) continue;
    00283     if (!md5sum) return true;
    00284
    00285     byte md5[16];
    00286     memset(md5, 0, sizeof(md5));
    00287     for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
    00288       for (uint j = 0; j < sizeof(md5); j++) {
    00289         md5[j] ^= s->files[i].hash[j];
    00290       }
    00291     }
    00292     if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return true;
    00293   }
    00294
    00295   return false;
    00296 }
    00297
    00298 template <class Tbase_set>
    00299 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
    00300 {
    00301   return HasBaseSet(ci, md5sum, BaseMedia<Tbase_set>::available_sets) ||
    00302       HasBaseSet(ci, md5sum, BaseMedia<Tbase_set>::duplicate_sets);
    00303 }
    00304
    00305 #else
    00306
    00307 template <class Tbase_set>
    00308 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
    00309 {
    00310   return false;
    00311 }
    00312
    00313 #endif /* ENABLE_NETWORK */
    00314
    00319 template <class Tbase_set>
    00320 /* static */ int BaseMedia<Tbase_set>::GetNumSets()
    00321 {
    00322   int n = 0;
    00323   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
    00324     if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
    00325     n++;
    00326   }
    00327   return n;
    00328 }
    00329
    00334 template <class Tbase_set>
    00335 /* static */ int BaseMedia<Tbase_set>::GetIndexOfUsedSet()
    00336 {
    00337   int n = 0;
    00338   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
    00339     if (s == BaseMedia<Tbase_set>::used_set) return n;
    00340     if (s->GetNumMissing() != 0) continue;
    00341     n++;
    00342   }
    00343   return -1;
    00344 }
    00345
    00350 template <class Tbase_set>
    00351 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
    00352 {
    00353   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
    00354     if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
    00355     if (index == 0) return s;
    00356     index--;
    00357   }
    00358   error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
    00359 }
    00360
    00365 template <class Tbase_set>
    00366 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
    00367 {
    00368   return BaseMedia<Tbase_set>::used_set;
    00369 }
    00370
    00376 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
    00377   template const char *repl_type::ini_set; \
    00378   template const char *repl_type::GetExtension(); \
    00379   template bool repl_type::AddFile(const char *filename, size_t pathlength); \
    00380   template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
    00381   template bool repl_type::SetSet(const char *name); \
    00382   template char *repl_type::GetSetsList(char *p, const char *last); \
    00383   template int repl_type::GetNumSets(); \
    00384   template int repl_type::GetIndexOfUsedSet(); \
    00385   template const set_type *repl_type::GetSet(int index); \
    00386   template const set_type *repl_type::GetUsedSet(); \
    00387   template bool repl_type::DetermineBestSet();
    00388