00001
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 {
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("%s", 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
00267
00268
00269
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
00276
00277
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
00289 InitializeUnicodeGlyphMap();
00290
00291
00292
00293
00294
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
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
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
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
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
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 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, "Checking %s for 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 GraphicsSet *duplicate = NULL;
00464 for (GraphicsSet *c = _available_graphics_sets; c != NULL; c = c->next) {
00465 if (strcmp(c->name, graphics->name) == 0 || c->shortname == graphics->shortname) {
00466 duplicate = c;
00467 break;
00468 }
00469 }
00470 if (duplicate != NULL) {
00471
00472 if ((duplicate->found_grfs == graphics->found_grfs && duplicate->version >= graphics->version) ||
00473 duplicate->found_grfs > graphics->found_grfs) {
00474 DEBUG(grf, 1, "Not adding %s (%i) as base graphics set (duplicate)", graphics->name, graphics->version);
00475 delete graphics;
00476 } else {
00477 GraphicsSet **prev = &_available_graphics_sets;
00478 while (*prev != duplicate) prev = &(*prev)->next;
00479
00480 *prev = graphics;
00481 graphics->next = duplicate->next;
00482
00483 duplicate->next = NULL;
00484
00485 DEBUG(grf, 1, "Removing %s (%i) as base graphics set (duplicate)", duplicate->name, duplicate->version);
00486 delete duplicate;
00487 ret = true;
00488 }
00489 } else {
00490 GraphicsSet **last = &_available_graphics_sets;
00491 while (*last != NULL) last = &(*last)->next;
00492
00493 *last = graphics;
00494 ret = true;
00495 }
00496 if (ret) {
00497 DEBUG(grf, 1, "Adding %s (%i) as base graphics set", graphics->name, graphics->version);
00498 }
00499 } else {
00500 delete graphics;
00501 }
00502 free(path);
00503
00504 delete ini;
00505 return ret;
00506 }
00507
00508
00509
00511 void FindGraphicsSets()
00512 {
00513 DEBUG(grf, 1, "Scanning for Graphics sets");
00514 OBGFileScanner::DoScan();
00515 }
00516
00522 bool SetGraphicsSet(const char *name)
00523 {
00524 if (StrEmpty(name)) {
00525 if (!DetermineGraphicsPack()) return false;
00526 CheckExternalFiles();
00527 return true;
00528 }
00529
00530 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00531 if (strcmp(name, g->name) == 0) {
00532 _used_graphics_set = g;
00533 CheckExternalFiles();
00534 return true;
00535 }
00536 }
00537 return false;
00538 }
00539
00546 char *GetGraphicsSetsList(char *p, const char *last)
00547 {
00548 p += seprintf(p, last, "List of graphics sets:\n");
00549 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00550 if (g->found_grfs <= 1) continue;
00551
00552 p += seprintf(p, last, "%18s: %s", g->name, g->description);
00553 int difference = MAX_GFT - g->found_grfs;
00554 if (difference != 0) {
00555 p += seprintf(p, last, " (missing %i file%s)\n", difference, difference == 1 ? "" : "s");
00556 } else {
00557 p += seprintf(p, last, "\n");
00558 }
00559 }
00560 p += seprintf(p, last, "\n");
00561
00562 return p;
00563 }
00564
00565 #if defined(ENABLE_NETWORK)
00566 #include "network/network_content.h"
00567
00574 bool HasGraphicsSet(const ContentInfo *ci, bool md5sum)
00575 {
00576 assert(ci->type == CONTENT_TYPE_BASE_GRAPHICS);
00577 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00578 if (g->found_grfs <= 1) continue;
00579
00580 if (g->shortname != ci->unique_id) continue;
00581 if (!md5sum) return true;
00582
00583 byte md5[16];
00584 memset(md5, 0, sizeof(md5));
00585 for (uint i = 0; i < MAX_GFT; i++) {
00586 for (uint j = 0; j < sizeof(md5); j++) {
00587 md5[j] ^= g->files[i].hash[j];
00588 }
00589 }
00590 if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return true;
00591 }
00592
00593 return false;
00594 }
00595
00596 #endif
00597
00601 int GetNumGraphicsSets()
00602 {
00603 int n = 0;
00604 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00605 if (g != _used_graphics_set && g->found_grfs <= 1) continue;
00606 n++;
00607 }
00608 return n;
00609 }
00610
00614 int GetIndexOfCurrentGraphicsSet()
00615 {
00616 int n = 0;
00617 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00618 if (g == _used_graphics_set) return n;
00619 if (g->found_grfs <= 1) continue;
00620 n++;
00621 }
00622 return -1;
00623 }
00624
00628 const char *GetGraphicsSetName(int index)
00629 {
00630 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00631 if (g != _used_graphics_set && g->found_grfs <= 1) continue;
00632 if (index == 0) return g->name;
00633 index--;
00634 }
00635 error("GetGraphicsSetName: index %d out of range", index);
00636 }