base_media_func.h

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

Generated on Mon Aug 30 19:36:53 2010 for OpenTTD by  doxygen 1.6.1