00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "debug.h"
00008 #include "gfxinit.h"
00009 #include "spritecache.h"
00010 #include "fileio_func.h"
00011 #include "fios.h"
00012 #include "newgrf.h"
00013 #include "md5.h"
00014 #include "variables.h"
00015 #include "fontcache.h"
00016 #include "gfx_func.h"
00017 #include "core/alloc_func.hpp"
00018 #include "core/bitmath_func.hpp"
00019 #include "core/smallvec_type.hpp"
00020 #include <string.h>
00021 #include "settings_type.h"
00022 #include "string_func.h"
00023 #include "ini_type.h"
00024
00025 #include "table/sprites.h"
00026 #include "table/palette_convert.h"
00027
00029 PaletteType _use_palette = PAL_AUTODETECT;
00031 bool _palette_remap_grf[MAX_FILE_SLOTS];
00033 const byte *_palette_remap = NULL;
00035 const byte *_palette_reverse_remap = NULL;
00036
00037 char _ini_graphics_set[32];
00038
00040 struct MD5File {
00041 const char *filename;
00042 uint8 hash[16];
00043 const char *missing_warning;
00044 };
00045
00047 enum GraphicsFileType {
00048 GFT_BASE,
00049 GFT_LOGOS,
00050 GFT_ARCTIC,
00051 GFT_TROPICAL,
00052 GFT_TOYLAND,
00053 GFT_EXTRA,
00054 MAX_GFT
00055 };
00056
00058 struct GraphicsSet {
00059 const char *name;
00060 const char *description;
00061 uint32 shortname;
00062 uint32 version;
00063 PaletteType palette;
00064
00065 MD5File files[MAX_GFT];
00066 uint found_grfs;
00067
00068 GraphicsSet *next;
00069
00071 ~GraphicsSet()
00072 {
00073 free((void*)this->name);
00074 free((void*)this->description);
00075 for (uint i = 0; i < MAX_GFT; i++) {
00076 free((void*)this->files[i].filename);
00077 free((void*)this->files[i].missing_warning);
00078 }
00079
00080 delete this->next;
00081 }
00082 };
00083
00085 static GraphicsSet *_available_graphics_sets = NULL;
00087 static const GraphicsSet *_used_graphics_set = NULL;
00088
00089 #include "table/files.h"
00090 #include "table/landscape_sprite.h"
00091
00092 static const SpriteID * const _landscape_spriteindexes[] = {
00093 _landscape_spriteindexes_1,
00094 _landscape_spriteindexes_2,
00095 _landscape_spriteindexes_3,
00096 };
00097
00098 static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
00099 {
00100 uint load_index_org = load_index;
00101 uint sprite_id = 0;
00102
00103 FioOpenFile(file_index, filename);
00104
00105 DEBUG(sprite, 2, "Reading grf-file '%s'", filename);
00106
00107 while (LoadNextSprite(load_index, file_index, sprite_id)) {
00108 load_index++;
00109 sprite_id++;
00110 if (load_index >= MAX_SPRITES) {
00111 usererror("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
00112 }
00113 }
00114 DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);
00115
00116 return load_index - load_index_org;
00117 }
00118
00119
00120 void LoadSpritesIndexed(int file_index, uint *sprite_id, const SpriteID *index_tbl)
00121 {
00122 uint start;
00123 while ((start = *index_tbl++) != END) {
00124 uint end = *index_tbl++;
00125
00126 do {
00127 bool b = LoadNextSprite(start, file_index, *sprite_id);
00128 assert(b);
00129 (*sprite_id)++;
00130 } while (++start <= end);
00131 }
00132 }
00133
00134 static void LoadGrfIndexed(const char* filename, const SpriteID* index_tbl, int file_index)
00135 {
00136 uint sprite_id = 0;
00137
00138 FioOpenFile(file_index, filename);
00139
00140 DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);
00141
00142 LoadSpritesIndexed(file_index, &sprite_id, index_tbl);
00143 }
00144
00145
00151 static bool FileMD5(const MD5File file)
00152 {
00153 size_t size;
00154 FILE *f = FioFOpenFile(file.filename, "rb", DATA_DIR, &size);
00155
00156 if (f != NULL) {
00157 Md5 checksum;
00158 uint8 buffer[1024];
00159 uint8 digest[16];
00160 size_t len;
00161
00162 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00163 size -= len;
00164 checksum.Append(buffer, len);
00165 }
00166
00167 FioFCloseFile(f);
00168
00169 checksum.Finish(digest);
00170 return memcmp(file.hash, digest, sizeof(file.hash)) == 0;
00171 } else {
00172 return false;
00173 }
00174 }
00175
00180 static bool DetermineGraphicsPack()
00181 {
00182 if (_used_graphics_set != NULL) return true;
00183
00184 const GraphicsSet *best = _available_graphics_sets;
00185 for (const GraphicsSet *c = _available_graphics_sets; c != NULL; c = c->next) {
00186 if (best->found_grfs < c->found_grfs ||
00187 (best->found_grfs == c->found_grfs && (
00188 (best->shortname == c->shortname && best->version < c->version) ||
00189 (best->palette != _use_palette && c->palette == _use_palette)))) {
00190 best = c;
00191 }
00192 }
00193
00194 _used_graphics_set = best;
00195 return _used_graphics_set != NULL;
00196 }
00197
00198 extern void UpdateNewGRFConfigPalette();
00199
00205 static void DeterminePalette()
00206 {
00207 assert(_used_graphics_set != NULL);
00208 if (_use_palette >= MAX_PAL) _use_palette = _used_graphics_set->palette;
00209
00210 switch (_use_palette) {
00211 case PAL_DOS:
00212 _palette_remap = _palmap_w2d;
00213 _palette_reverse_remap = _palmap_d2w;
00214 break;
00215
00216 case PAL_WINDOWS:
00217 _palette_remap = _palmap_d2w;
00218 _palette_reverse_remap = _palmap_w2d;
00219 break;
00220
00221 default:
00222 NOT_REACHED();
00223 }
00224
00225 UpdateNewGRFConfigPalette();
00226 }
00227
00233 void CheckExternalFiles()
00234 {
00235 DeterminePalette();
00236
00237 DEBUG(grf, 1, "Using the %s base graphics set with the %s palette", _used_graphics_set->name, _use_palette == PAL_DOS ? "DOS" : "Windows");
00238
00239 static const size_t ERROR_MESSAGE_LENGTH = 128;
00240 char error_msg[ERROR_MESSAGE_LENGTH * (MAX_GFT + 1)];
00241 error_msg[0] = '\0';
00242 char *add_pos = error_msg;
00243 const char *last = lastof(error_msg);
00244
00245 for (uint i = 0; i < lengthof(_used_graphics_set->files); i++) {
00246 if (!FileMD5(_used_graphics_set->files[i])) {
00247 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);
00248 }
00249 }
00250
00251 bool sound = false;
00252 for (uint i = 0; !sound && i < lengthof(_sound_sets); i++) {
00253 sound = FileMD5(_sound_sets[i]);
00254 }
00255
00256 if (!sound) {
00257 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");
00258 }
00259
00260 if (add_pos != error_msg) ShowInfoF(error_msg);
00261 }
00262
00263
00264 static void LoadSpriteTables()
00265 {
00266 memset(_palette_remap_grf, 0, sizeof(_palette_remap_grf));
00267 uint i = FIRST_GRF_SLOT;
00268
00269 _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
00270 LoadGrfFile(_used_graphics_set->files[GFT_BASE].filename, 0, i++);
00271
00272
00273
00274
00275
00276
00277
00278 _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
00279 LoadGrfFile(_used_graphics_set->files[GFT_LOGOS].filename, 4793, i++);
00280
00281
00282
00283
00284
00285
00286 if (_settings_game.game_creation.landscape != LT_TEMPERATE) {
00287 _palette_remap_grf[i] = (_use_palette != _used_graphics_set->palette);
00288 LoadGrfIndexed(
00289 _used_graphics_set->files[GFT_ARCTIC + _settings_game.game_creation.landscape - 1].filename,
00290 _landscape_spriteindexes[_settings_game.game_creation.landscape - 1],
00291 i++
00292 );
00293 }
00294
00295
00296 InitializeUnicodeGlyphMap();
00297
00298
00299
00300
00301
00302
00303 GRFConfig *top = _grfconfig;
00304 GRFConfig *master = CallocT<GRFConfig>(1);
00305 master->filename = strdup(_used_graphics_set->files[GFT_EXTRA].filename);
00306 FillGRFDetails(master, false);
00307 master->windows_paletted = (_used_graphics_set->palette == PAL_WINDOWS);
00308 ClrBit(master->flags, GCF_INIT_ONLY);
00309 master->next = top;
00310 _grfconfig = master;
00311
00312 LoadNewGRF(SPR_NEWGRFS_BASE, i);
00313
00314
00315 ClearGRFConfig(&master);
00316 _grfconfig = top;
00317 }
00318
00319
00320 void GfxLoadSprites()
00321 {
00322 DEBUG(sprite, 2, "Loading sprite set %d", _settings_game.game_creation.landscape);
00323
00324 GfxInitSpriteMem();
00325 LoadSpriteTables();
00326 GfxInitPalettes();
00327 }
00328
00333 #define fetch_metadata(name) \
00334 item = metadata->GetItem(name, false); \
00335 if (item == NULL || strlen(item->value) == 0) { \
00336 DEBUG(grf, 0, "Base graphics set detail loading: %s field missing", name); \
00337 return false; \
00338 }
00339
00341 static const char *_gft_names[MAX_GFT] = { "base", "logos", "arctic", "tropical", "toyland", "extra" };
00342
00350 static bool FillGraphicsSetDetails(GraphicsSet *graphics, IniFile *ini, const char *path)
00351 {
00352 memset(graphics, 0, sizeof(*graphics));
00353
00354 IniGroup *metadata = ini->GetGroup("metadata");
00355 IniItem *item;
00356
00357 fetch_metadata("name");
00358 graphics->name = strdup(item->value);
00359
00360 fetch_metadata("description");
00361 graphics->description = strdup(item->value);
00362
00363 fetch_metadata("shortname");
00364 for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
00365 graphics->shortname |= ((uint8)item->value[i]) << (32 - i * 8);
00366 }
00367
00368 fetch_metadata("version");
00369 graphics->version = atoi(item->value);
00370
00371 fetch_metadata("palette");
00372 graphics->palette = (*item->value == 'D' || *item->value == 'd') ? PAL_DOS : PAL_WINDOWS;
00373
00374
00375 IniGroup *files = ini->GetGroup("files");
00376 IniGroup *md5s = ini->GetGroup("md5s");
00377 IniGroup *origin = ini->GetGroup("origin");
00378 for (uint i = 0; i < MAX_GFT; i++) {
00379 MD5File *file = &graphics->files[i];
00380
00381 item = files->GetItem(_gft_names[i], false);
00382 if (item == NULL) {
00383 DEBUG(grf, 0, "No graphics file for: %s", _gft_names[i]);
00384 return false;
00385 }
00386
00387 const char *filename = item->value;
00388 file->filename = MallocT<char>(strlen(filename) + strlen(path) + 1);
00389 sprintf((char*)file->filename, "%s%s", path, filename);
00390
00391
00392 item = md5s->GetItem(filename, false);
00393 if (item == NULL) {
00394 DEBUG(grf, 0, "No MD5 checksum specified for: %s", filename);
00395 return false;
00396 }
00397 char *c = item->value;
00398 for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
00399 uint j;
00400 if ('0' <= *c && *c <= '9') {
00401 j = *c - '0';
00402 } else if ('a' <= *c && *c <= 'f') {
00403 j = *c - 'a' + 10;
00404 } else if ('A' <= *c && *c <= 'F') {
00405 j = *c - 'A' + 10;
00406 } else {
00407 DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s", filename);
00408 return false;
00409 }
00410 if (i % 2 == 0) {
00411 file->hash[i / 2] = j << 4;
00412 } else {
00413 file->hash[i / 2] |= j;
00414 }
00415 }
00416
00417
00418 item = origin->GetItem(filename, false);
00419 if (item == NULL) item = origin->GetItem("default", false);
00420 if (item == NULL) {
00421 DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
00422 file->missing_warning = strdup("");
00423 } else {
00424 file->missing_warning = strdup(item->value);
00425 }
00426
00427 if (FileMD5(*file)) graphics->found_grfs++;
00428 }
00429
00430 return true;
00431 }
00432
00434 class OBGFileScanner : FileScanner {
00435 public:
00436 bool AddFile(const char *filename, size_t basepath_length);
00437
00439 static uint DoScan()
00440 {
00441 OBGFileScanner fs;
00442 return fs.Scan(".obg", DATA_DIR);
00443 }
00444 };
00445
00452 bool OBGFileScanner::AddFile(const char *filename, size_t basepath_length)
00453 {
00454 bool ret = false;
00455 DEBUG(grf, 1, "Found %s as base graphics set", filename);
00456
00457 GraphicsSet *graphics = new GraphicsSet();;
00458 IniFile *ini = new IniFile();
00459 ini->LoadFromDisk(filename);
00460
00461 char *path = strdup(filename + basepath_length);
00462 char *psep = strrchr(path, PATHSEPCHAR);
00463 if (psep != NULL) {
00464 psep[1] = '\0';
00465 } else {
00466 *path = '\0';
00467 }
00468
00469 if (FillGraphicsSetDetails(graphics, ini, path)) {
00470 bool duplicate = false;
00471 for (const GraphicsSet *c = _available_graphics_sets; !duplicate && c != NULL; c = c->next) {
00472 duplicate = (strcmp(c->name, graphics->name) == 0) || (c->shortname == graphics->shortname && c->version == graphics->version);
00473 }
00474 if (duplicate) {
00475 delete graphics;
00476 } else {
00477 graphics->next = _available_graphics_sets;
00478 _available_graphics_sets = graphics;
00479 ret = true;
00480 }
00481 } else {
00482 delete graphics;
00483 }
00484 free(path);
00485
00486 delete ini;
00487 return ret;
00488 }
00489
00490
00491
00493 void FindGraphicsSets()
00494 {
00495 DEBUG(grf, 1, "Scanning for Graphics sets");
00496 OBGFileScanner::DoScan();
00497 }
00498
00504 bool SetGraphicsSet(const char *name)
00505 {
00506 if (StrEmpty(name)) {
00507 if (!DetermineGraphicsPack()) return false;
00508 CheckExternalFiles();
00509 return true;
00510 }
00511
00512 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00513 if (strcmp(name, g->name) == 0) {
00514 _used_graphics_set = g;
00515 CheckExternalFiles();
00516 return true;
00517 }
00518 }
00519 return false;
00520 }
00521
00528 char *GetGraphicsSetsList(char *p, const char *last)
00529 {
00530 p += seprintf(p, last, "List of graphics sets:\n");
00531 for (const GraphicsSet *g = _available_graphics_sets; g != NULL; g = g->next) {
00532 if (g->found_grfs <= 1) continue;
00533
00534 p += seprintf(p, last, "%18s: %s", g->name, g->description);
00535 int difference = MAX_GFT - g->found_grfs;
00536 if (difference != 0) {
00537 p += seprintf(p, last, " (missing %i file%s)\n", difference, difference == 1 ? "" : "s");
00538 } else {
00539 p += seprintf(p, last, "\n");
00540 }
00541 }
00542 p += seprintf(p, last, "\n");
00543
00544 return p;
00545 }