# base_media_func.h

Go to the documentation of this file.

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