base_media_func.h

Go to the documentation of this file.
00001 /* $Id: base_media_func.h 23886 2012-02-04 13:29:00Z michi_cc $ */
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, bool Tsearch_in_tars>
00043 bool BaseSet<T, Tnum_files, Tsearch_in_tars>::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, Tsearch_in_tars>::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, Tsearch_in_tars>::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 (T::CheckMD5(file, BASESET_DIR)) {
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, const char *tar_filename)
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, BASESET_DIR);
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 
00283 template <class Tbase_set> bool HasBaseSet(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
00284 {
00285   for (; s != NULL; s = s->next) {
00286     if (s->GetNumMissing() != 0) continue;
00287 
00288     if (s->shortname != ci->unique_id) continue;
00289     if (!md5sum) return true;
00290 
00291     byte md5[16];
00292     memset(md5, 0, sizeof(md5));
00293     for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
00294       for (uint j = 0; j < sizeof(md5); j++) {
00295         md5[j] ^= s->files[i].hash[j];
00296       }
00297     }
00298     if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return true;
00299   }
00300 
00301   return false;
00302 }
00303 
00304 template <class Tbase_set>
00305 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
00306 {
00307   return HasBaseSet(ci, md5sum, BaseMedia<Tbase_set>::available_sets) ||
00308       HasBaseSet(ci, md5sum, BaseMedia<Tbase_set>::duplicate_sets);
00309 }
00310 
00311 #else
00312 
00313 template <class Tbase_set>
00314 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
00315 {
00316   return false;
00317 }
00318 
00319 #endif /* ENABLE_NETWORK */
00320 
00325 template <class Tbase_set>
00326 /* static */ int BaseMedia<Tbase_set>::GetNumSets()
00327 {
00328   int n = 0;
00329   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
00330     if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
00331     n++;
00332   }
00333   return n;
00334 }
00335 
00340 template <class Tbase_set>
00341 /* static */ int BaseMedia<Tbase_set>::GetIndexOfUsedSet()
00342 {
00343   int n = 0;
00344   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
00345     if (s == BaseMedia<Tbase_set>::used_set) return n;
00346     if (s->GetNumMissing() != 0) continue;
00347     n++;
00348   }
00349   return -1;
00350 }
00351 
00356 template <class Tbase_set>
00357 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
00358 {
00359   for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
00360     if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
00361     if (index == 0) return s;
00362     index--;
00363   }
00364   error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
00365 }
00366 
00371 template <class Tbase_set>
00372 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
00373 {
00374   return BaseMedia<Tbase_set>::used_set;
00375 }
00376 
00382 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
00383   template const char *repl_type::ini_set; \
00384   template const char *repl_type::GetExtension(); \
00385   template bool repl_type::AddFile(const char *filename, size_t pathlength, const char *tar_filename); \
00386   template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
00387   template bool repl_type::SetSet(const char *name); \
00388   template char *repl_type::GetSetsList(char *p, const char *last); \
00389   template int repl_type::GetNumSets(); \
00390   template int repl_type::GetIndexOfUsedSet(); \
00391   template const set_type *repl_type::GetSet(int index); \
00392   template const set_type *repl_type::GetUsedSet(); \
00393   template bool repl_type::DetermineBestSet();
00394