# base_media_func.h

Go to the documentation of this file.

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