# gfxinit.cpp

Go to the documentation of this file.

    00001 /* $Id: gfxinit.cpp 15476 2009-02-14 01:42:12Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "debug.h"
    00007 #include "spritecache.h"
    00008 #include "fileio_func.h"
    00009 #include "fios.h"
    00010 #include "newgrf.h"
    00011 #include "md5.h"
    00012 #include "fontcache.h"
    00013 #include "gfx_func.h"
    00014 #include "settings_type.h"
    00015 #include "string_func.h"
    00016 #include "ini_type.h"
    00017
    00018 #include "table/sprites.h"
    00019 #include "table/palette_convert.h"
    00020
    00022 PaletteType _use_palette = PAL_AUTODETECT;
    00024 bool _palette_remap_grf[MAX_FILE_SLOTS];
    00026 const byte *_palette_remap = NULL;
    00028 const byte *_palette_reverse_remap = NULL;
    00029
    00030 char *_ini_graphics_set;
    00031
    00033 struct MD5File {
    00034   const char *filename;
    00035   uint8 hash[16];
    00036   const char *missing_warning;
    00037 };
    00038
    00040 enum GraphicsFileType {
    00041   GFT_BASE,
    00042   GFT_LOGOS,
    00043   GFT_ARCTIC,
    00044   GFT_TROPICAL,
    00045   GFT_TOYLAND,
    00046   GFT_EXTRA,
    00047   MAX_GFT
    00048 };
    00049
    00051 struct GraphicsSet {
    00052   const char *name;
    00053   const char *description;
    00054   uint32 shortname;
    00055   uint32 version;
    00056   PaletteType palette;
    00057
    00058   MD5File files[MAX_GFT];
    00059   uint found_grfs;
    00060
    00061   GraphicsSet *next;
    00062
    00064   ~GraphicsSet()
    00065   {
    00066     free((void*)this->name);
    00067     free((void*)this->description);
    00068     for (uint i = 0; i < MAX_GFT; i++) {
    00069       free((void*)this->files[i].filename);
    00070       free((void*)this->files[i].missing_warning);
    00071     }
    00072
    00073     delete this->next;
    00074   }
    00075 };
    00076
    00078 static GraphicsSet *_available_graphics_sets = NULL;
    00080 static const GraphicsSet *_used_graphics_set = NULL;
    00081
    00082 #include "table/files.h"
    00083 #include "table/landscape_sprite.h"
    00084
    00085 static const SpriteID * const _landscape_spriteindexes[] = {
    00086   _landscape_spriteindexes_1,
    00087   _landscape_spriteindexes_2,
    00088   _landscape_spriteindexes_3,
    00089 };
    00090
    00091 static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
    00092 {
    00093   uint load_index_org = load_index;
    00094   uint sprite_id = 0;
    00095
    00096   FioOpenFile(file_index, filename);
    00097
    00098   DEBUG(sprite, 2, "Reading grf-file '%s'", filename);
    00099
    00100   while (LoadNextSprite(load_index, file_index, sprite_id)) {
    00101     load_index++;
    00102     sprite_id++;
    00103     if (load_index >= MAX_SPRITES) {
    00104       usererror("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
    00105     }
    00106   }
    00107   DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);
    00108
    00109   return load_index - load_index_org;
    00110 }
    00111
    00112
    00113 void LoadSpritesIndexed(int file_index, uint *sprite_id, const SpriteID *index_tbl)
    00114 {
    00115   uint start;
    00116   while ((start = *index_tbl++) != END) {
    00117     uint end = *index_tbl++;
    00118
    00119     do {
    00120       bool b = LoadNextSprite(start, file_index, *sprite_id);
    00121       assert(b);
    00122       (*sprite_id)++;
    00123     } while (++start <= end);
    00124   }
    00125 }
    00126
    00127 static void LoadGrfIndexed(const char *filename, const SpriteID *index_tbl, int file_index)
    00128 {
    00129   uint sprite_id = 0;
    00130
    00131   FioOpenFile(file_index, filename);
    00132
    00133   DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);
    00134
    00135   LoadSpritesIndexed(file_index, &sprite_id, index_tbl);
    00136 }
    00137
    00138
    00144 static bool FileMD5(const MD5File file)
    00145 {
    00146   size_t size;
    00147   FILE *f = FioFOpenFile(file.filename, "rb", DATA_DIR, &size);
    00148
    00149   if (f != NULL) {
    00150     Md5 checksum;
    00151     uint8 buffer[1024];
    00152     uint8 digest[16];
    00153     size_t len;
    00154
    00155     while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
    00156       size -= len;
    00157       checksum.Append(buffer, len);
    00158     }
    00159
    00160     FioFCloseFile(f);
    00161
    00162     checksum.Finish(digest);
    00163     return memcmp(file.hash, digest, sizeof(file.hash)) == 0;
    00164   } else { // file not found
    00165     return false;
    00166   }
    00167 }
    00168
    00173 static bool DetermineGraphicsPack()
    00174 {
    00175   if (_used_graphics_set != NULL) return true;
    00176
    00177   const GraphicsSet *best = _available_graphics_sets;
    00178   for (const GraphicsSet *c = _available_graphics_sets; c != NULL; c = c->next) {
    00179     if (best->found_grfs < c->found_grfs ||
    00180         (best->found_grfs == c->found_grfs && (
    00181           (best->shortname == c->shortname && best->version < c->version) ||
    00182           (best->palette != _use_palette && c->palette == _use_palette)))) {
    00183       best = c;
    00184     }
    00185   }
    00186
    00187   _used_graphics_set = best;
    00188   return _used_graphics_set != NULL;
    00189 }
    00190
    00191 extern void UpdateNewGRFConfigPalette();
    00192
    00198 static void DeterminePalette()
    00199 {
    00200   assert(_used_graphics_set != NULL);
    00201   if (_use_palette >= MAX_PAL) _use_palette = _used_graphics_set->palette;
    00202
    00203   switch (_use_palette) {
    00204     case PAL_DOS:
    00205       _palette_remap = _palmap_w2d;
    00206       _palette_reverse_remap = _palmap_d2w;
    00207       break;
    00208
    00209     case PAL_WINDOWS:
    00210       _palette_remap = _palmap_d2w;
    00211       _palette_reverse_remap = _palmap_w2d;
    00212       break;
    00213
    00214     default:
    00215       NOT_REACHED();
    00216   }
    00217
    00218   UpdateNewGRFConfigPalette();
    00219 }
    00220
    00226 void CheckExternalFiles()
    00227 {
    00228   DeterminePalette();
    00229
    00230   DEBUG(grf, 1, "Using the %s base graphics set with the %s palette", _used_graphics_set->name, _use_palette == PAL_DOS ? "DOS" : "Windows");
    00231
    00232   static const size_t ERROR_MESSAGE_LENGTH = 128;
    00233   char error_msg[ERROR_MESSAGE_LENGTH * (MAX_GFT + 1)];
    00234   error_msg[0] = '\0';
    00235   char *add_pos = error_msg;
    00236   const char *last = lastof(error_msg);
    00237
    00238   for (uint i = 0; i < lengthof(_used_graphics_set->files); i++) {
    00239     if (!FileMD5(_used_graphics_set->files[i])) {
    00240       add_pos += seprintf(add_pos, last, "Your '%s' file is corrupted or missing! %s\n", _used_graphics_set->files[i].filename, _used_graphics_set->files[i].missing_warning);
    00241     }
    00242   }
    00243
    00244   bool sound = false;
    00245   for (uint i = 0; !sound && i < lengthof(_sound_sets); i++) {
    00246     sound = FileMD5(_sound_sets[i]);
    00247   }
    00248
    00249   if (!sound) {
    00250     add_pos += seprintf(add_pos, last, "Your 'sample.cat' file is corrupted or missing! You can find 'sample.cat' on your Transport Tycoon Deluxe CD-ROM.\n");
    00251   }
    00252
    00253   if (add_pos != error_msg) ShowInfoF(error_msg);
    00254 }
    00255
    00256
    00257 static void LoadSpriteTables()
    00258 {
    00259   memset(_palette_remap_grf, 0, sizeof(_palette_remap_grf));
    00260   uint i = FIRST_GRF_SLOT;
    00261
    00262   _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
    00263   LoadGrfFile(_used_graphics_set->files[GFT_BASE].filename, 0, i++);
    00264
    00265   /*
    00266    * The second basic file always starts at the given location and does
    00267    * contain a different amount of sprites depending on the "type"; DOS
    00268    * has a few sprites less. However, we do not care about those missing
    00269    * sprites as they are not shown anyway (logos in intro game).
    00270    */
    00271   _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
    00272   LoadGrfFile(_used_graphics_set->files[GFT_LOGOS].filename, 4793, i++);
    00273
    00274   /*
    00275    * Load additional sprites for climates other than temperate.
    00276    * This overwrites some of the temperate sprites, such as foundations
    00277    * and the ground sprites.
    00278    */
    00279   if (_settings_game.game_creation.landscape != LT_TEMPERATE) {
    00280     _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
    00281     LoadGrfIndexed(
    00282       _used_graphics_set->files[GFT_ARCTIC + _settings_game.game_creation.landscape - 1].filename,
    00283       _landscape_spriteindexes[_settings_game.game_creation.landscape - 1],
    00284       i++
    00285     );
    00286   }
    00287
    00288   /* Initialize the unicode to sprite mapping table */
    00289   InitializeUnicodeGlyphMap();
    00290
    00291   /*
    00292    * Load the base NewGRF with OTTD required graphics as first NewGRF.
    00293    * However, we do not want it to show up in the list of used NewGRFs,
    00294    * so we have to manually add it, and then remove it later.
    00295    */
    00296   GRFConfig *top = _grfconfig;
    00297   GRFConfig *master = CallocT<GRFConfig>(1);
    00298   master->filename = strdup(_used_graphics_set->files[GFT_EXTRA].filename);
    00299   FillGRFDetails(master, false);
    00300   master->windows_paletted = (_used_graphics_set->palette == PAL_WINDOWS);
    00301   ClrBit(master->flags, GCF_INIT_ONLY);
    00302   master->next = top;
    00303   _grfconfig = master;
    00304
    00305   LoadNewGRF(SPR_NEWGRFS_BASE, i);
    00306
    00307   /* Free and remove the top element. */
    00308   ClearGRFConfig(&master);
    00309   _grfconfig = top;
    00310 }
    00311
    00312
    00313 void GfxLoadSprites()
    00314 {
    00315   DEBUG(sprite, 2, "Loading sprite set %d", _settings_game.game_creation.landscape);
    00316
    00317   GfxInitSpriteMem();
    00318   LoadSpriteTables();
    00319   GfxInitPalettes();
    00320 }
    00321
    00326 #define fetch_metadata(name) \
    00327   item = metadata->GetItem(name, false); \
    00328   if (item == NULL || strlen(item->value) == 0) { \
    00329     DEBUG(grf, 0, "Base graphics set detail loading: %s field missing", name); \
    00330     return false; \
    00331   }
    00332
    00334 static const char *_gft_names[MAX_GFT] = { "base", "logos", "arctic", "tropical", "toyland", "extra" };
    00335
    00343 static bool FillGraphicsSetDetails(GraphicsSet *graphics, IniFile *ini, const char *path)
    00344 {
    00345   memset(graphics, 0, sizeof(*graphics));
    00346
    00347   IniGroup *metadata = ini->GetGroup("metadata");
    00348   IniItem *item;
    00349
    00350   fetch_metadata("name");
    00351   graphics->name = strdup(item->value);
    00352
    00353   fetch_metadata("description");
    00354   graphics->description = strdup(item->value);
    00355
    00356   fetch_metadata("shortname");
    00357   for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
    00358     graphics->shortname |= ((uint8)item->value[i]) << (i * 8);
    00359   }
    00360
    00361   fetch_metadata("version");
    00362   graphics->version = atoi(item->value);
    00363
    00364   fetch_metadata("palette");
    00365   graphics->palette = (*item->value == 'D' || *item->value == 'd') ? PAL_DOS : PAL_WINDOWS;
    00366
    00367   /* For each of the graphics file types we want to find the file, MD5 checksums and warning messages. */
    00368   IniGroup *files  = ini->GetGroup("files");
    00369   IniGroup *md5s   = ini->GetGroup("md5s");
    00370   IniGroup *origin = ini->GetGroup("origin");
    00371   for (uint i = 0; i < MAX_GFT; i++) {
    00372     MD5File *file = &graphics->files[i];
    00373     /* Find the filename first. */
    00374     item = files->GetItem(_gft_names[i], false);
    00375     if (item == NULL) {
    00376       DEBUG(grf, 0, "No graphics file for: %s", _gft_names[i]);
    00377       return false;
    00378     }
    00379
    00380     const char *filename = item->value;
    00381     file->filename = MallocT<char>(strlen(filename) + strlen(path) + 1);
    00382     sprintf((char*)file->filename, "%s%s", path, filename);
    00383
    00384     /* Then find the MD5 checksum */
    00385     item = md5s->GetItem(filename, false);
    00386     if (item == NULL) {
    00387       DEBUG(grf, 0, "No MD5 checksum specified for: %s", filename);
    00388       return false;
    00389     }
    00390     char *c = item->value;
    00391     for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
    00392       uint j;
    00393       if ('0' <= *c && *c <= '9') {
    00394         j = *c - '0';
    00395       } else if ('a' <= *c && *c <= 'f') {
    00396         j = *c - 'a' + 10;
    00397       } else if ('A' <= *c && *c <= 'F') {
    00398         j = *c - 'A' + 10;
    00399       } else {
    00400         DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s", filename);
    00401         return false;
    00402       }
    00403       if (i % 2 == 0) {
    00404         file->hash[i / 2] = j << 4;
    00405       } else {
    00406         file->hash[i / 2] |= j;
    00407       }
    00408     }
    00409
    00410     /* Then find the warning message when the file's missing */
    00411     item = origin->GetItem(filename, false);
    00412     if (item == NULL) item = origin->GetItem("default", false);
    00413     if (item == NULL) {
    00414       DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
    00415       file->missing_warning = strdup("");
    00416     } else {
    00417       file->missing_warning = strdup(item->value);
    00418     }
    00419
    00420     if (FileMD5(*file)) graphics->found_grfs++;
    00421   }
    00422
    00423   return true;
    00424 }
    00425
    00427 class OBGFileScanner : FileScanner {
    00428 public:
    00429   /* virtual */ bool AddFile(const char *filename, size_t basepath_length);
    00430
    00432   static uint DoScan()
    00433   {
    00434     OBGFileScanner fs;
    00435     return fs.Scan(".obg", DATA_DIR);
    00436   }
    00437 };
    00438
    00445 bool OBGFileScanner::AddFile(const char *filename, size_t basepath_length)
    00446 {
    00447   bool ret = false;
    00448   DEBUG(grf, 1, "Found %s as base graphics set", filename);
    00449
    00450   GraphicsSet *graphics = new GraphicsSet();;
    00451   IniFile *ini = new IniFile();
    00452   ini->LoadFromDisk(filename);
    00453
    00454   char *path = strdup(filename + basepath_length);
    00455   char *psep = strrchr(path, PATHSEPCHAR);
    00456   if (psep != NULL) {
    00457     psep[1] = '\0';
    00458   } else {
    00459     *path = '\0';
    00460   }
    00461
    00462   if (FillGraphicsSetDetails(graphics, ini, path)) {
    00463     bool duplicate = false;
    00464     for (const GraphicsSet *c = _available_graphics_sets; !duplicate && c != NULL; c = c->next) {
    00465       duplicate = (strcmp(c->name, graphics->name) == 0 || c->shortname == graphics->shortname) && c->version == graphics->version;
    00466     }
    00467     if (duplicate) {
    00468       delete graphics;
    00469     } else {
    00470       graphics->next = _available_graphics_sets;
    00471       _available_graphics_sets = graphics;
    00472       ret = true;
    00473     }
    00474   } else {
    00475     delete graphics;
    00476   }
    00477   free(path);
    00478
    00479   delete ini;
    00480   return ret;
    00481 }
    00482
    00483
    00484
    00486 void FindGraphicsSets()
    00487 {
    00488   DEBUG(grf, 1, "Scanning for Graphics sets");
    00489   OBGFileScanner::DoScan();
    00490 }
    00491
    00497 bool SetGraphicsSet(const char *name)
    00498 {
    00499   if (StrEmpty(name)) {
    00500     if (!DetermineGraphicsPack()) return false;
    00501     CheckExternalFiles();
    00502     return true;
    00503   }
    00504
    00505   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00506     if (strcmp(name, g->name) == 0) {
    00507       _used_graphics_set = g;
    00508       CheckExternalFiles();
    00509       return true;
    00510     }
    00511   }
    00512   return false;
    00513 }
    00514
    00521 char *GetGraphicsSetsList(char *p, const char *last)
    00522 {
    00523   p += seprintf(p, last, "List of graphics sets:\n");
    00524   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00525     if (g->found_grfs <= 1) continue;
    00526
    00527     p += seprintf(p, last, "%18s: %s", g->name, g->description);
    00528     int difference = MAX_GFT - g->found_grfs;
    00529     if (difference != 0) {
    00530       p += seprintf(p, last, " (missing %i file%s)\n", difference, difference == 1 ? "" : "s");
    00531     } else {
    00532       p += seprintf(p, last, "\n");
    00533     }
    00534   }
    00535   p += seprintf(p, last, "\n");
    00536
    00537   return p;
    00538 }
    00539
    00540 #if defined(ENABLE_NETWORK)
    00541 #include "network/network_content.h"
    00542
    00549 bool HasGraphicsSet(const ContentInfo *ci, bool md5sum)
    00550 {
    00551   assert(ci->type == CONTENT_TYPE_BASE_GRAPHICS);
    00552   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00553     if (g->found_grfs <= 1) continue;
    00554
    00555     if (g->shortname != ci->unique_id) continue;
    00556     if (!md5sum) return true;
    00557
    00558     byte md5[16];
    00559     memset(md5, 0, sizeof(md5));
    00560     for (uint i = 0; i < MAX_GFT; i++) {
    00561       for (uint j = 0; j < sizeof(md5); j++) {
    00562         md5[j] ^= g->files[i].hash[j];
    00563       }
    00564     }
    00565     if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return true;
    00566   }
    00567
    00568   return false;
    00569 }
    00570
    00571 #endif /* ENABLE_NETWORK */
    00572
    00576 int GetNumGraphicsSets()
    00577 {
    00578   int n = 0;
    00579   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00580     if (g != _used_graphics_set && g->found_grfs <= 1) continue;
    00581     n++;
    00582   }
    00583   return n;
    00584 }
    00585
    00589 int GetIndexOfCurrentGraphicsSet()
    00590 {
    00591   int n = 0;
    00592   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00593     if (g == _used_graphics_set) return n;
    00594     if (g->found_grfs <= 1) continue;
    00595     n++;
    00596   }
    00597   return -1;
    00598 }
    00599
    00603 const char *GetGraphicsSetName(int index)
    00604 {
    00605   for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
    00606     if (g != _used_graphics_set && g->found_grfs <= 1) continue;
    00607     if (index == 0) return g->name;
    00608     index--;
    00609   }
    00610   error("GetGraphicsSetName: index %d out of range", index);
    00611 }