00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "3rdparty/md5/md5.h"
00015 #include "newgrf.h"
00016 #include "gamelog.h"
00017 #include "network/network_func.h"
00018 #include "gfx_func.h"
00019
00020 #include "fileio_func.h"
00021 #include "fios.h"
00022
00023
00024 GRFConfig *_all_grfs;
00025 GRFConfig *_grfconfig;
00026 GRFConfig *_grfconfig_newgame;
00027 GRFConfig *_grfconfig_static;
00028
00029 GRFError::GRFError(StringID severity, StringID message) :
00030 message(message),
00031 severity(severity)
00032 {
00033 }
00034
00035 GRFError::~GRFError()
00036 {
00037 free(this->custom_message);
00038 free(this->data);
00039 }
00040
00049 void UpdateNewGRFConfigPalette()
00050 {
00051 for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00052 for (GRFConfig *c = _grfconfig_static; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00053 }
00054
00055
00056 static bool CalcGRFMD5Sum(GRFConfig *config)
00057 {
00058 FILE *f;
00059 Md5 checksum;
00060 uint8 buffer[1024];
00061 size_t len, size;
00062
00063
00064 f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
00065 if (f == NULL) return false;
00066
00067
00068 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00069 size -= len;
00070 checksum.Append(buffer, len);
00071 }
00072 checksum.Finish(config->md5sum);
00073
00074 FioFCloseFile(f);
00075
00076 return true;
00077 }
00078
00079
00080
00081 bool FillGRFDetails(GRFConfig *config, bool is_static)
00082 {
00083 if (!FioCheckFileExists(config->filename)) {
00084 config->status = GCS_NOT_FOUND;
00085 return false;
00086 }
00087
00088
00089 LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN);
00090
00091
00092 if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
00093
00094 if (is_static) {
00095
00096 LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
00097
00098
00099 if (HasBit(config->flags, GCF_UNSAFE)) return false;
00100 }
00101
00102 config->windows_paletted = (_use_palette == PAL_WINDOWS);
00103
00104 return CalcGRFMD5Sum(config);
00105 }
00106
00107
00108 void ClearGRFConfig(GRFConfig **config)
00109 {
00110
00111 if (!HasBit((*config)->flags, GCF_COPY)) {
00112 free((*config)->filename);
00113 free((*config)->name);
00114 free((*config)->info);
00115 delete (*config)->error;
00116 }
00117 free(*config);
00118 *config = NULL;
00119 }
00120
00121
00122
00123 void ClearGRFConfigList(GRFConfig **config)
00124 {
00125 GRFConfig *c, *next;
00126 for (c = *config; c != NULL; c = next) {
00127 next = c->next;
00128 ClearGRFConfig(&c);
00129 }
00130 *config = NULL;
00131 }
00132
00133
00139 GRFConfig *DuplicateGRFConfig(const GRFConfig *c)
00140 {
00141 GRFConfig *config = MallocT<GRFConfig>(1);
00142 *config = *c;
00143
00144 if (c->filename != NULL) config->filename = strdup(c->filename);
00145 if (c->name != NULL) config->name = strdup(c->name);
00146 if (c->info != NULL) config->info = strdup(c->info);
00147 if (c->error != NULL) {
00148 config->error = new GRFError(c->error->severity, c->error->message);
00149 config->error->num_params = c->error->num_params;
00150 memcpy(config->error->param_value, c->error->param_value, sizeof(config->error->param_value));
00151 if (c->error->data != NULL) config->error->data = strdup(c->error->data);
00152 if (c->error->custom_message != NULL) config->error->custom_message = strdup(c->error->custom_message);
00153 }
00154
00155 ClrBit(config->flags, GCF_COPY);
00156
00157 return config;
00158 }
00159
00165 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
00166 {
00167
00168 ClearGRFConfigList(dst);
00169 for (; src != NULL; src = src->next) {
00170 GRFConfig *c = DuplicateGRFConfig(src);
00171
00172 ClrBit(c->flags, GCF_INIT_ONLY);
00173 if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
00174
00175 *dst = c;
00176 dst = &c->next;
00177 }
00178
00179 return dst;
00180 }
00181
00195 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
00196 {
00197 GRFConfig *prev;
00198 GRFConfig *cur;
00199
00200 if (list == NULL) return;
00201
00202 for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
00203 if (cur->grfid != list->grfid) continue;
00204
00205 prev->next = cur->next;
00206 ClearGRFConfig(&cur);
00207 cur = prev;
00208 }
00209
00210 RemoveDuplicatesFromGRFConfigList(list->next);
00211 }
00212
00217 void AppendStaticGRFConfigs(GRFConfig **dst)
00218 {
00219 GRFConfig **tail = dst;
00220 while (*tail != NULL) tail = &(*tail)->next;
00221
00222 CopyGRFConfigList(tail, _grfconfig_static, false);
00223 RemoveDuplicatesFromGRFConfigList(*dst);
00224 }
00225
00229 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
00230 {
00231 GRFConfig **tail = dst;
00232 while (*tail != NULL) tail = &(*tail)->next;
00233 *tail = el;
00234
00235 RemoveDuplicatesFromGRFConfigList(*dst);
00236 }
00237
00238
00239
00240 void ResetGRFConfig(bool defaults)
00241 {
00242 CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
00243 AppendStaticGRFConfigs(&_grfconfig);
00244 }
00245
00246
00255 GRFListCompatibility IsGoodGRFConfigList()
00256 {
00257 GRFListCompatibility res = GLC_ALL_GOOD;
00258
00259 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00260 const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00261 if (f == NULL) {
00262 char buf[256];
00263
00264
00265
00266 f = FindGRFConfig(c->grfid);
00267 if (f != NULL) {
00268 md5sumToString(buf, lastof(buf), c->md5sum);
00269 DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
00270 SetBit(c->flags, GCF_COMPATIBLE);
00271
00272
00273 if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
00274 GamelogGRFCompatible(f);
00275 goto compatible_grf;
00276 }
00277
00278
00279 md5sumToString(buf, lastof(buf), c->md5sum);
00280 DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
00281
00282 GamelogGRFRemove(c->grfid);
00283
00284 c->status = GCS_NOT_FOUND;
00285 res = GLC_NOT_FOUND;
00286 } else {
00287 compatible_grf:
00288 DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
00289
00290
00291
00292
00293
00294 if (!HasBit(c->flags, GCF_COPY)) {
00295 free(c->filename);
00296 c->filename = strdup(f->filename);
00297 memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
00298 if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
00299 if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
00300 c->error = NULL;
00301 }
00302 }
00303 }
00304
00305 return res;
00306 }
00307
00309 class GRFFileScanner : FileScanner {
00310 public:
00311 bool AddFile(const char *filename, size_t basepath_length);
00312
00314 static uint DoScan()
00315 {
00316 GRFFileScanner fs;
00317 return fs.Scan(".grf", DATA_DIR);
00318 }
00319 };
00320
00321 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
00322 {
00323 GRFConfig *c = CallocT<GRFConfig>(1);
00324 c->filename = strdup(filename + basepath_length);
00325
00326 bool added = true;
00327 if (FillGRFDetails(c, false)) {
00328 if (_all_grfs == NULL) {
00329 _all_grfs = c;
00330 } else {
00331
00332
00333 GRFConfig **pd, *d;
00334 bool stop = false;
00335 for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
00336 if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
00337
00338
00339
00340 if (strcasecmp(c->name, d->name) <= 0) {
00341 stop = true;
00342 } else if (stop) {
00343 break;
00344 }
00345 }
00346 if (added) {
00347 c->next = d;
00348 *pd = c;
00349 }
00350 }
00351 } else {
00352 added = false;
00353 }
00354
00355 if (!added) {
00356
00357
00358 ClearGRFConfig(&c);
00359 }
00360
00361 return added;
00362 }
00363
00370 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
00371 {
00372 const GRFConfig *c1 = *p1;
00373 const GRFConfig *c2 = *p2;
00374
00375 return strcasecmp(c1->name != NULL ? c1->name : c1->filename,
00376 c2->name != NULL ? c2->name : c2->filename);
00377 }
00378
00379
00380 void ScanNewGRFFiles()
00381 {
00382 ClearGRFConfigList(&_all_grfs);
00383
00384 DEBUG(grf, 1, "Scanning for NewGRFs");
00385 uint num = GRFFileScanner::DoScan();
00386
00387 DEBUG(grf, 1, "Scan complete, found %d files", num);
00388 if (num == 0 || _all_grfs == NULL) return;
00389
00390
00391
00392
00393 GRFConfig **to_sort = MallocT<GRFConfig*>(num);
00394
00395 uint i = 0;
00396 for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
00397 to_sort[i] = p;
00398 }
00399
00400 num = i;
00401
00402 QSortT(to_sort, num, &GRFSorter);
00403
00404 for (i = 1; i < num; i++) {
00405 to_sort[i - 1]->next = to_sort[i];
00406 }
00407 to_sort[num - 1]->next = NULL;
00408 _all_grfs = to_sort[0];
00409
00410 free(to_sort);
00411
00412 #ifdef ENABLE_NETWORK
00413 NetworkAfterNewGRFScan();
00414 #endif
00415 }
00416
00417
00418
00419 const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
00420 {
00421 for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00422 if (c->grfid == grfid) {
00423 if (md5sum == NULL) return c;
00424
00425 if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
00426 }
00427 }
00428
00429 return NULL;
00430 }
00431
00432 #ifdef ENABLE_NETWORK
00433
00435 struct UnknownGRF : public GRFIdentifier {
00436 UnknownGRF *next;
00437 char name[NETWORK_GRF_NAME_LENGTH];
00438 };
00439
00457 char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
00458 {
00459 UnknownGRF *grf;
00460 static UnknownGRF *unknown_grfs = NULL;
00461
00462 for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
00463 if (grf->grfid == grfid) {
00464 if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
00465 }
00466 }
00467
00468 if (!create) return NULL;
00469
00470 grf = CallocT<UnknownGRF>(1);
00471 grf->grfid = grfid;
00472 grf->next = unknown_grfs;
00473 strecpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, lastof(grf->name));
00474 memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
00475
00476 unknown_grfs = grf;
00477 return grf->name;
00478 }
00479
00480 #endif
00481
00482
00483
00484 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
00485 {
00486 GRFConfig *c;
00487
00488 for (c = _grfconfig; c != NULL; c = c->next) {
00489 if ((c->grfid & mask) == (grfid & mask)) return c;
00490 }
00491
00492 return NULL;
00493 }
00494
00495
00496
00497 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
00498 {
00499 uint i;
00500
00501
00502 if (c->num_params == 0) return strecpy(dst, "", last);
00503
00504 for (i = 0; i < c->num_params; i++) {
00505 if (i > 0) dst = strecpy(dst, " ", last);
00506 dst += seprintf(dst, last, "%d", c->param[i]);
00507 }
00508 return dst;
00509 }
00510
00512 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
00513
00518 bool GRFConfig::IsOpenTTDBaseGRF() const
00519 {
00520 return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
00521 }