base_media_func.h

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

Generated on Wed Feb 17 23:06:45 2010 for OpenTTD by  doxygen 1.6.1