(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / newgrf_config.cpp
blobb970e0a80bf8b4c90eb2130accc396e8b0b7205f
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "3rdparty/md5/md5.h"
15 #include "newgrf.h"
16 #include "network/network_func.h"
17 #include "gfx_func.h"
18 #include "newgrf_text.h"
19 #include "window_func.h"
20 #include "progress.h"
21 #include "video/video_driver.hpp"
22 #include "strings_func.h"
23 #include "textfile_gui.h"
25 #include "fileio_func.h"
26 #include "fios.h"
28 #include "safeguards.h"
30 /** Create a new GRFTextWrapper. */
31 GRFTextWrapper::GRFTextWrapper() :
32 text(NULL)
36 /** Cleanup a GRFTextWrapper object. */
37 GRFTextWrapper::~GRFTextWrapper()
39 CleanUpGRFText(this->text);
42 /**
43 * Create a new GRFConfig.
44 * @param filename Set the filename of this GRFConfig to filename. The argument
45 * is copied so the original string isn't needed after the constructor.
47 GRFConfig::GRFConfig(const char *filename) :
48 name(new GRFTextWrapper()),
49 info(new GRFTextWrapper()),
50 url(new GRFTextWrapper()),
51 num_valid_params(lengthof(param))
53 if (filename != NULL) this->filename = stredup(filename);
54 this->name->AddRef();
55 this->info->AddRef();
56 this->url->AddRef();
59 /**
60 * Create a new GRFConfig that is a deep copy of an existing config.
61 * @param config The GRFConfig object to make a copy of.
63 GRFConfig::GRFConfig(const GRFConfig &config) :
64 ZeroedMemoryAllocator(),
65 ident(config.ident),
66 name(config.name),
67 info(config.info),
68 url(config.url),
69 version(config.version),
70 min_loadable_version(config.min_loadable_version),
71 flags(config.flags & ~(1 << GCF_COPY)),
72 status(config.status),
73 grf_bugs(config.grf_bugs),
74 num_params(config.num_params),
75 num_valid_params(config.num_valid_params),
76 palette(config.palette),
77 has_param_defaults(config.has_param_defaults)
79 MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
80 MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
81 if (config.filename != NULL) this->filename = stredup(config.filename);
82 this->name->AddRef();
83 this->info->AddRef();
84 this->url->AddRef();
85 if (config.error != NULL) this->error = new GRFError(*config.error);
86 for (uint i = 0; i < config.param_info.Length(); i++) {
87 if (config.param_info[i] == NULL) {
88 *this->param_info.Append() = NULL;
89 } else {
90 *this->param_info.Append() = new GRFParameterInfo(*config.param_info[i]);
95 /** Cleanup a GRFConfig object. */
96 GRFConfig::~GRFConfig()
98 /* GCF_COPY as in NOT stredupped/alloced the filename */
99 if (!HasBit(this->flags, GCF_COPY)) {
100 free(this->filename);
101 delete this->error;
103 this->name->Release();
104 this->info->Release();
105 this->url->Release();
107 for (uint i = 0; i < this->param_info.Length(); i++) delete this->param_info[i];
111 * Copy the parameter information from the \a src config.
112 * @param src Source config.
114 void GRFConfig::CopyParams(const GRFConfig &src)
116 this->num_params = src.num_params;
117 this->num_valid_params = src.num_valid_params;
118 MemCpyT<uint32>(this->param, src.param, lengthof(this->param));
122 * Get the name of this grf. In case the name isn't known
123 * the filename is returned.
124 * @return The name of filename of this grf.
126 const char *GRFConfig::GetName() const
128 const char *name = GetGRFStringFromGRFText(this->name->text);
129 return StrEmpty(name) ? this->filename : name;
133 * Get the grf info.
134 * @return A string with a description of this grf.
136 const char *GRFConfig::GetDescription() const
138 return GetGRFStringFromGRFText(this->info->text);
142 * Get the grf url.
143 * @return A string with an url of this grf.
145 const char *GRFConfig::GetURL() const
147 return GetGRFStringFromGRFText(this->url->text);
150 /** Set the default value for all parameters as specified by action14. */
151 void GRFConfig::SetParameterDefaults()
153 this->num_params = 0;
154 MemSetT<uint32>(this->param, 0, lengthof(this->param));
156 if (!this->has_param_defaults) return;
158 for (uint i = 0; i < this->param_info.Length(); i++) {
159 if (this->param_info[i] == NULL) continue;
160 this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
165 * Set the palette of this GRFConfig to something suitable.
166 * That is either the setting coming from the NewGRF or
167 * the globally used palette.
169 void GRFConfig::SetSuitablePalette()
171 PaletteType pal;
172 switch (this->palette & GRFP_GRF_MASK) {
173 case GRFP_GRF_DOS: pal = PAL_DOS; break;
174 case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS; break;
175 default: pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
177 SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
181 * Finalize Action 14 info after file scan is finished.
183 void GRFConfig::FinalizeParameterInfo()
185 for (GRFParameterInfo **info = this->param_info.Begin(); info != this->param_info.End(); ++info) {
186 if (*info == NULL) continue;
187 (*info)->Finalize();
191 GRFConfig *_all_grfs;
192 GRFConfig *_grfconfig;
193 GRFConfig *_grfconfig_newgame;
194 GRFConfig *_grfconfig_static;
197 * Construct a new GRFError.
198 * @param severity The severity of this error.
199 * @param message The actual error-string.
201 GRFError::GRFError(StringID severity, StringID message) :
202 message(message),
203 severity(severity)
208 * Create a new GRFError that is a deep copy of an existing error message.
209 * @param error The GRFError object to make a copy of.
211 GRFError::GRFError(const GRFError &error) :
212 ZeroedMemoryAllocator(),
213 custom_message(error.custom_message),
214 data(error.data),
215 message(error.message),
216 severity(error.severity)
218 if (error.custom_message != NULL) this->custom_message = stredup(error.custom_message);
219 if (error.data != NULL) this->data = stredup(error.data);
220 memcpy(this->param_value, error.param_value, sizeof(this->param_value));
223 GRFError::~GRFError()
225 free(this->custom_message);
226 free(this->data);
230 * Create a new empty GRFParameterInfo object.
231 * @param nr The newgrf parameter that is changed.
233 GRFParameterInfo::GRFParameterInfo(uint nr) :
234 name(NULL),
235 desc(NULL),
236 type(PTYPE_UINT_ENUM),
237 min_value(0),
238 max_value(UINT32_MAX),
239 def_value(0),
240 param_nr(nr),
241 first_bit(0),
242 num_bit(32),
243 complete_labels(false)
247 * Create a new GRFParameterInfo object that is a deep copy of an existing
248 * parameter info object.
249 * @param info The GRFParameterInfo object to make a copy of.
251 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
252 name(DuplicateGRFText(info.name)),
253 desc(DuplicateGRFText(info.desc)),
254 type(info.type),
255 min_value(info.min_value),
256 max_value(info.max_value),
257 def_value(info.def_value),
258 param_nr(info.param_nr),
259 first_bit(info.first_bit),
260 num_bit(info.num_bit),
261 complete_labels(info.complete_labels)
263 for (uint i = 0; i < info.value_names.Length(); i++) {
264 SmallPair<uint32, GRFText *> *data = info.value_names.Get(i);
265 this->value_names.Insert(data->first, DuplicateGRFText(data->second));
269 /** Cleanup all parameter info. */
270 GRFParameterInfo::~GRFParameterInfo()
272 CleanUpGRFText(this->name);
273 CleanUpGRFText(this->desc);
274 for (uint i = 0; i < this->value_names.Length(); i++) {
275 SmallPair<uint32, GRFText *> *data = this->value_names.Get(i);
276 CleanUpGRFText(data->second);
281 * Get the value of this user-changeable parameter from the given config.
282 * @param config The GRFConfig to get the value from.
283 * @return The value of this parameter.
285 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
287 /* GB doesn't work correctly with nbits == 32, so handle that case here. */
288 if (this->num_bit == 32) return config->param[this->param_nr];
289 return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
293 * Set the value of this user-changeable parameter in the given config.
294 * @param config The GRFConfig to set the value in.
295 * @param value The new value.
297 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
299 /* SB doesn't work correctly with nbits == 32, so handle that case here. */
300 if (this->num_bit == 32) {
301 config->param[this->param_nr] = value;
302 } else {
303 SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
305 config->num_params = max<uint>(config->num_params, this->param_nr + 1);
306 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
310 * Finalize Action 14 info after file scan is finished.
312 void GRFParameterInfo::Finalize()
314 this->complete_labels = true;
315 for (uint32 value = this->min_value; value <= this->max_value; value++) {
316 if (!this->value_names.Contains(value)) {
317 this->complete_labels = false;
318 break;
324 * Update the palettes of the graphics from the config file.
325 * Called when changing the default palette in advanced settings.
326 * @param p1 Unused.
327 * @return Always true.
329 bool UpdateNewGRFConfigPalette(int32 p1)
331 for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
332 for (GRFConfig *c = _grfconfig_static; c != NULL; c = c->next) c->SetSuitablePalette();
333 for (GRFConfig *c = _all_grfs; c != NULL; c = c->next) c->SetSuitablePalette();
334 return true;
338 * Get the data section size of a GRF.
339 * @param f GRF.
340 * @return Size of the data section or SIZE_MAX if the file has no separate data section.
342 size_t GRFGetSizeOfDataSection(FILE *f)
344 extern const byte _grf_cont_v2_sig[];
345 static const uint header_len = 14;
347 byte data[header_len];
348 if (fread(data, 1, header_len, f) == header_len) {
349 if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
350 /* Valid container version 2, get data section size. */
351 size_t offset = ((size_t)data[13] << 24) | ((size_t)data[12] << 16) | ((size_t)data[11] << 8) | (size_t)data[10];
352 if (offset >= 1 * 1024 * 1024 * 1024) {
353 DEBUG(grf, 0, "Unexpectedly large offset for NewGRF");
354 /* Having more than 1 GiB of data is very implausible. Mostly because then
355 * all pools in OpenTTD are flooded already. Or it's just Action C all over.
356 * In any case, the offsets to graphics will likely not work either. */
357 return SIZE_MAX;
359 return header_len + offset;
363 return SIZE_MAX;
367 * Calculate the MD5 sum for a GRF, and store it in the config.
368 * @param config GRF to compute.
369 * @param subdir The subdirectory to look in.
370 * @return MD5 sum was successfully computed
372 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
374 FILE *f;
375 Md5 checksum;
376 uint8 buffer[1024];
377 size_t len, size;
379 /* open the file */
380 f = FioFOpenFile(config->filename, "rb", subdir, &size);
381 if (f == NULL) return false;
383 long start = ftell(f);
384 size = min(size, GRFGetSizeOfDataSection(f));
386 if (start < 0 || fseek(f, start, SEEK_SET) < 0) {
387 FioFCloseFile(f);
388 return false;
391 /* calculate md5sum */
392 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
393 size -= len;
394 checksum.Append(buffer, len);
396 checksum.Finish(config->ident.md5sum);
398 FioFCloseFile(f);
400 return true;
405 * Find the GRFID of a given grf, and calculate its md5sum.
406 * @param config grf to fill.
407 * @param is_static grf is static.
408 * @param subdir the subdirectory to search in.
409 * @return Operation was successfully completed.
411 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
413 if (!FioCheckFileExists(config->filename, subdir)) {
414 config->status = GCS_NOT_FOUND;
415 return false;
418 /* Find and load the Action 8 information */
419 LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
420 config->SetSuitablePalette();
421 config->FinalizeParameterInfo();
423 /* Skip if the grfid is 0 (not read) or if it is an internal GRF */
424 if (config->ident.grfid == 0 || HasBit(config->flags, GCF_SYSTEM)) return false;
426 if (is_static) {
427 /* Perform a 'safety scan' for static GRFs */
428 LoadNewGRFFile(config, CONFIG_SLOT, GLS_SAFETYSCAN, subdir);
430 /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
431 if (HasBit(config->flags, GCF_UNSAFE)) return false;
434 return CalcGRFMD5Sum(config, subdir);
439 * Clear a GRF Config list, freeing all nodes.
440 * @param config Start of the list.
441 * @post \a config is set to \c NULL.
443 void ClearGRFConfigList(GRFConfig **config)
445 GRFConfig *c, *next;
446 for (c = *config; c != NULL; c = next) {
447 next = c->next;
448 delete c;
450 *config = NULL;
455 * Copy a GRF Config list
456 * @param dst pointer to destination list
457 * @param src pointer to source list values
458 * @param init_only the copied GRF will be processed up to GLS_INIT
459 * @return pointer to the last value added to the destination list
461 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
463 /* Clear destination as it will be overwritten */
464 ClearGRFConfigList(dst);
465 for (; src != NULL; src = src->next) {
466 GRFConfig *c = new GRFConfig(*src);
468 ClrBit(c->flags, GCF_INIT_ONLY);
469 if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
471 *dst = c;
472 dst = &c->next;
475 return dst;
479 * Removes duplicates from lists of GRFConfigs. These duplicates
480 * are introduced when the _grfconfig_static GRFs are appended
481 * to the _grfconfig on a newgame or savegame. As the parameters
482 * of the static GRFs could be different that the parameters of
483 * the ones used non-statically. This can result in desyncs in
484 * multiplayers, so the duplicate static GRFs have to be removed.
486 * This function _assumes_ that all static GRFs are placed after
487 * the non-static GRFs.
489 * @param list the list to remove the duplicates from
491 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
493 GRFConfig *prev;
494 GRFConfig *cur;
496 if (list == NULL) return;
498 for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
499 if (cur->ident.grfid != list->ident.grfid) continue;
501 prev->next = cur->next;
502 delete cur;
503 cur = prev; // Just go back one so it continues as normal later on
506 RemoveDuplicatesFromGRFConfigList(list->next);
510 * Appends the static GRFs to a list of GRFs
511 * @param dst the head of the list to add to
513 void AppendStaticGRFConfigs(GRFConfig **dst)
515 GRFConfig **tail = dst;
516 while (*tail != NULL) tail = &(*tail)->next;
518 CopyGRFConfigList(tail, _grfconfig_static, false);
519 RemoveDuplicatesFromGRFConfigList(*dst);
523 * Appends an element to a list of GRFs
524 * @param dst the head of the list to add to
525 * @param el the new tail to be
527 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
529 GRFConfig **tail = dst;
530 while (*tail != NULL) tail = &(*tail)->next;
531 *tail = el;
533 RemoveDuplicatesFromGRFConfigList(*dst);
537 /** Reset the current GRF Config to either blank or newgame settings. */
538 void ResetGRFConfig(bool defaults)
540 CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
541 AppendStaticGRFConfigs(&_grfconfig);
546 * Check if all GRFs in the GRF config from a savegame can be loaded.
547 * @param grfconfig GrfConfig to check
548 * @return will return any of the following 3 values:<br>
549 * <ul>
550 * <li> GLC_ALL_GOOD: No problems occurred, all GRF files were found and loaded
551 * <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
552 * compatible GRF with the same grfid was found and used instead
553 * <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
554 * </ul>
556 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
558 GRFListCompatibility res = GLC_ALL_GOOD;
560 for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
561 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
562 if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
563 char buf[256];
565 /* If we have not found the exactly matching GRF try to find one with the
566 * same grfid, as it most likely is compatible */
567 f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
568 if (f != NULL) {
569 md5sumToString(buf, lastof(buf), c->ident.md5sum);
570 DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
571 if (!HasBit(c->flags, GCF_COMPATIBLE)) {
572 /* Preserve original_md5sum after it has been assigned */
573 SetBit(c->flags, GCF_COMPATIBLE);
574 memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
577 /* Non-found has precedence over compatibility load */
578 if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
579 goto compatible_grf;
582 /* No compatible grf was found, mark it as disabled */
583 md5sumToString(buf, lastof(buf), c->ident.md5sum);
584 DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
586 c->status = GCS_NOT_FOUND;
587 res = GLC_NOT_FOUND;
588 } else {
589 compatible_grf:
590 DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
591 /* The filename could be the filename as in the savegame. As we need
592 * to load the GRF here, we need the correct filename, so overwrite that
593 * in any case and set the name and info when it is not set already.
594 * When the GCF_COPY flag is set, it is certain that the filename is
595 * already a local one, so there is no need to replace it. */
596 if (!HasBit(c->flags, GCF_COPY)) {
597 free(c->filename);
598 c->filename = stredup(f->filename);
599 memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
600 c->name->Release();
601 c->name = f->name;
602 c->name->AddRef();
603 c->info->Release();
604 c->info = f->name;
605 c->info->AddRef();
606 c->error = NULL;
607 c->version = f->version;
608 c->min_loadable_version = f->min_loadable_version;
609 c->num_valid_params = f->num_valid_params;
610 c->has_param_defaults = f->has_param_defaults;
611 for (uint i = 0; i < f->param_info.Length(); i++) {
612 if (f->param_info[i] == NULL) {
613 *c->param_info.Append() = NULL;
614 } else {
615 *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
622 return res;
625 /** Helper for scanning for files with GRF as extension */
626 class GRFFileScanner : FileScanner {
627 uint next_update; ///< The next (realtime tick) we do update the screen.
628 uint num_scanned; ///< The number of GRFs we have scanned.
630 public:
631 GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
635 /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
637 /** Do the scan for GRFs. */
638 static uint DoScan()
640 GRFFileScanner fs;
641 int ret = fs.Scan(".grf", NEWGRF_DIR);
642 /* The number scanned and the number returned may not be the same;
643 * duplicate NewGRFs and base sets are ignored in the return value. */
644 _settings_client.gui.last_newgrf_count = fs.num_scanned;
645 return ret;
649 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
651 GRFConfig *c = new GRFConfig(filename + basepath_length);
653 bool added = true;
654 if (FillGRFDetails(c, false)) {
655 if (_all_grfs == NULL) {
656 _all_grfs = c;
657 } else {
658 /* Insert file into list at a position determined by its
659 * name, so the list is sorted as we go along */
660 GRFConfig **pd, *d;
661 bool stop = false;
662 for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
663 if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
664 /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
665 * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
666 * just after the first with the same name. Avoids doubles in the list. */
667 if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
668 stop = true;
669 } else if (stop) {
670 break;
673 if (added) {
674 c->next = d;
675 *pd = c;
678 } else {
679 added = false;
682 this->num_scanned++;
683 if (this->next_update <= _realtime_tick) {
684 _modal_progress_work_mutex->EndCritical();
685 _modal_progress_paint_mutex->BeginCritical();
687 const char *name = NULL;
688 if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
689 if (name == NULL) name = c->filename;
690 UpdateNewGRFScanStatus(this->num_scanned, name);
692 _modal_progress_work_mutex->BeginCritical();
693 _modal_progress_paint_mutex->EndCritical();
695 this->next_update = _realtime_tick + 200;
698 if (!added) {
699 /* File couldn't be opened, or is either not a NewGRF or is a
700 * 'system' NewGRF or it's already known, so forget about it. */
701 delete c;
704 return added;
708 * Simple sorter for GRFS
709 * @param p1 the first GRFConfig *
710 * @param p2 the second GRFConfig *
711 * @return the same strcmp would return for the name of the NewGRF.
713 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
715 const GRFConfig *c1 = *p1;
716 const GRFConfig *c2 = *p2;
718 return strnatcmp(c1->GetName(), c2->GetName());
722 * Really perform the scan for all NewGRFs.
723 * @param callback The callback to call after the scanning is complete.
725 void DoScanNewGRFFiles(void *callback)
727 _modal_progress_work_mutex->BeginCritical();
729 ClearGRFConfigList(&_all_grfs);
730 TarScanner::DoScan(TarScanner::NEWGRF);
732 DEBUG(grf, 1, "Scanning for NewGRFs");
733 uint num = GRFFileScanner::DoScan();
735 DEBUG(grf, 1, "Scan complete, found %d files", num);
736 if (num != 0 && _all_grfs != NULL) {
737 /* Sort the linked list using quicksort.
738 * For that we first have to make an array, then sort and
739 * then remake the linked list. */
740 GRFConfig **to_sort = MallocT<GRFConfig*>(num);
742 uint i = 0;
743 for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
744 to_sort[i] = p;
746 /* Number of files is not necessarily right */
747 num = i;
749 QSortT(to_sort, num, &GRFSorter);
751 for (i = 1; i < num; i++) {
752 to_sort[i - 1]->next = to_sort[i];
754 to_sort[num - 1]->next = NULL;
755 _all_grfs = to_sort[0];
757 free(to_sort);
759 #ifdef ENABLE_NETWORK
760 NetworkAfterNewGRFScan();
761 #endif
764 _modal_progress_work_mutex->EndCritical();
765 _modal_progress_paint_mutex->BeginCritical();
767 /* Yes... these are the NewGRF windows */
768 InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
769 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
770 if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
772 DeleteWindowByClass(WC_MODAL_PROGRESS);
773 SetModalProgress(false);
774 MarkWholeScreenDirty();
775 _modal_progress_paint_mutex->EndCritical();
779 * Scan for all NewGRFs.
780 * @param callback The callback to call after the scanning is complete.
782 void ScanNewGRFFiles(NewGRFScanCallback *callback)
784 /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
785 SetModalProgress(true);
786 /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
787 MarkWholeScreenDirty();
789 if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL, "ottd:newgrf-scan")) {
790 _modal_progress_work_mutex->EndCritical();
791 _modal_progress_paint_mutex->EndCritical();
792 DoScanNewGRFFiles(callback);
793 _modal_progress_paint_mutex->BeginCritical();
794 _modal_progress_work_mutex->BeginCritical();
795 } else {
796 UpdateNewGRFScanStatus(0, NULL);
801 * Find a NewGRF in the scanned list.
802 * @param grfid GRFID to look for,
803 * @param mode Restrictions for matching grfs
804 * @param md5sum Expected MD5 sum
805 * @param desired_version Requested version
806 * @return The matching grf, if it exists in #_all_grfs, else \c NULL.
808 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
810 assert((mode == FGCM_EXACT) != (md5sum == NULL));
811 const GRFConfig *best = NULL;
812 for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
813 /* if md5sum is set, we look for an exact match and continue if not found */
814 if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
815 /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
816 if (md5sum != NULL || mode == FGCM_ANY) return c;
817 /* Skip incompatible stuff, unless explicitly allowed */
818 if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
819 /* check version compatibility */
820 if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
821 /* remember the newest one as "the best" */
822 if (best == NULL || c->version > best->version) best = c;
825 return best;
828 #ifdef ENABLE_NETWORK
830 /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
831 struct UnknownGRF : public GRFIdentifier {
832 UnknownGRF *next; ///< The next unknown GRF.
833 GRFTextWrapper *name; ///< Name of the GRF.
837 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
838 * unknown GRF is a GRF where the .grf is not found during scanning.
840 * The names are resolved via UDP calls to servers that should know the name,
841 * though the replies may not come. This leaves "<Unknown>" as name, though
842 * that shouldn't matter _very_ much as they need GRF crawler or so to look
843 * up the GRF anyway and that works better with the GRF ID.
845 * @param grfid the GRF ID part of the 'unique' GRF identifier
846 * @param md5sum the MD5 checksum part of the 'unique' GRF identifier
847 * @param create whether to create a new GRFConfig if the GRFConfig did not
848 * exist in the fake list of GRFConfigs.
849 * @return The GRFTextWrapper of the name of the GRFConfig with the given GRF ID
850 * and MD5 checksum or NULL when it does not exist and create is false.
851 * This value must NEVER be freed by the caller.
853 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
855 UnknownGRF *grf;
856 static UnknownGRF *unknown_grfs = NULL;
858 for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
859 if (grf->grfid == grfid) {
860 if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
864 if (!create) return NULL;
866 grf = CallocT<UnknownGRF>(1);
867 grf->grfid = grfid;
868 grf->next = unknown_grfs;
869 grf->name = new GRFTextWrapper();
870 grf->name->AddRef();
872 AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
873 memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
875 unknown_grfs = grf;
876 return grf->name;
879 #endif /* ENABLE_NETWORK */
883 * Retrieve a NewGRF from the current config by its grfid.
884 * @param grfid grf to look for.
885 * @param mask GRFID mask to allow for partial matching.
886 * @return The grf config, if it exists, else \c NULL.
888 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
890 GRFConfig *c;
892 for (c = _grfconfig; c != NULL; c = c->next) {
893 if ((c->ident.grfid & mask) == (grfid & mask)) return c;
896 return NULL;
900 /** Build a string containing space separated parameter values, and terminate */
901 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
903 uint i;
905 /* Return an empty string if there are no parameters */
906 if (c->num_params == 0) return strecpy(dst, "", last);
908 for (i = 0; i < c->num_params; i++) {
909 if (i > 0) dst = strecpy(dst, " ", last);
910 dst += seprintf(dst, last, "%d", c->param[i]);
912 return dst;
915 /** Base GRF ID for OpenTTD's base graphics GRFs. */
916 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
919 * Search a textfile file next to this NewGRF.
920 * @param type The type of the textfile to search for.
921 * @return The filename for the textfile, \c NULL otherwise.
923 const char *GRFConfig::GetTextfile(TextfileType type) const
925 return ::GetTextfile(type, NEWGRF_DIR, this->filename);