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/>.
10 /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */
14 #include "3rdparty/md5/md5.h"
16 #include "network/network_func.h"
18 #include "newgrf_text.h"
19 #include "window_func.h"
21 #include "video/video_driver.hpp"
22 #include "strings_func.h"
25 #include "fileio_func.h"
30 * Create a new GRFConfig.
31 * @param filename Set the filename of this GRFConfig to filename. The argument
32 * is copied so the original string isn't needed after the constructor.
34 GRFConfig::GRFConfig(const char *filename
) :
38 num_valid_params(lengthof(param
))
40 if (filename
!= NULL
) this->filename
= xstrdup(filename
);
44 * Create a new GRFConfig that is a deep copy of an existing config.
45 * @param config The GRFConfig object to make a copy of.
47 GRFConfig::GRFConfig(const GRFConfig
&config
) :
48 ZeroedMemoryAllocator(),
53 version(config
.version
),
54 min_loadable_version(config
.min_loadable_version
),
55 flags(config
.flags
& ~(1 << GCF_COPY
)),
56 status(config
.status
),
57 grf_bugs(config
.grf_bugs
),
58 num_params(config
.num_params
),
59 num_valid_params(config
.num_valid_params
),
60 palette(config
.palette
),
61 has_param_defaults(config
.has_param_defaults
)
63 MemCpyT
<uint8
>(this->original_md5sum
, config
.original_md5sum
, lengthof(this->original_md5sum
));
64 MemCpyT
<uint32
>(this->param
, config
.param
, lengthof(this->param
));
65 if (config
.filename
!= NULL
) this->filename
= xstrdup(config
.filename
);
66 if (config
.error
!= NULL
) this->error
= new GRFError(*config
.error
);
67 for (uint i
= 0; i
< config
.param_info
.Length(); i
++) {
68 if (config
.param_info
[i
] == NULL
) {
69 *this->param_info
.Append() = NULL
;
71 *this->param_info
.Append() = new GRFParameterInfo(*config
.param_info
[i
]);
76 /** Cleanup a GRFConfig object. */
77 GRFConfig::~GRFConfig()
79 /* GCF_COPY as in NOT strdupped/alloced the filename */
80 if (!HasBit(this->flags
, GCF_COPY
)) {
85 for (uint i
= 0; i
< this->param_info
.Length(); i
++) delete this->param_info
[i
];
89 * Copy the parameter information from the \a src config.
90 * @param src Source config.
92 void GRFConfig::CopyParams(const GRFConfig
&src
)
94 this->num_params
= src
.num_params
;
95 this->num_valid_params
= src
.num_valid_params
;
96 MemCpyT
<uint32
>(this->param
, src
.param
, lengthof(this->param
));
100 * Get the name of this grf. In case the name isn't known
101 * the filename is returned.
102 * @return The name of filename of this grf.
104 const char *GRFConfig::GetName() const
106 const char *name
= this->name
->get_string();
107 return StrEmpty(name
) ? this->filename
: name
;
112 * @return A string with a description of this grf.
114 const char *GRFConfig::GetDescription() const
116 return this->info
->get_string();
121 * @return A string with an url of this grf.
123 const char *GRFConfig::GetURL() const
125 return this->url
->get_string();
128 /** Set the default value for all parameters as specified by action14. */
129 void GRFConfig::SetParameterDefaults()
131 this->num_params
= 0;
132 MemSetT
<uint32
>(this->param
, 0, lengthof(this->param
));
134 if (!this->has_param_defaults
) return;
136 for (uint i
= 0; i
< this->param_info
.Length(); i
++) {
137 if (this->param_info
[i
] == NULL
) continue;
138 this->param_info
[i
]->SetValue(this, this->param_info
[i
]->def_value
);
143 * Set the palette of this GRFConfig to something suitable.
144 * That is either the setting coming from the NewGRF or
145 * the globally used palette.
147 void GRFConfig::SetSuitablePalette()
150 switch (this->palette
& GRFP_GRF_MASK
) {
151 case GRFP_GRF_DOS
: pal
= PAL_DOS
; break;
152 case GRFP_GRF_WINDOWS
: pal
= PAL_WINDOWS
; break;
153 default: pal
= _settings_client
.gui
.newgrf_default_palette
== 1 ? PAL_WINDOWS
: PAL_DOS
; break;
155 SB(this->palette
, GRFP_USE_BIT
, 1, pal
== PAL_WINDOWS
? GRFP_USE_WINDOWS
: GRFP_USE_DOS
);
159 * Finalize Action 14 info after file scan is finished.
161 void GRFConfig::FinalizeParameterInfo()
163 for (GRFParameterInfo
**info
= this->param_info
.Begin(); info
!= this->param_info
.End(); ++info
) {
164 if (*info
== NULL
) continue;
169 GRFConfig
*_all_grfs
;
170 GRFConfig
*_grfconfig
;
171 GRFConfig
*_grfconfig_newgame
;
172 GRFConfig
*_grfconfig_static
;
175 * Construct a new GRFError.
176 * @param severity The severity of this error.
177 * @param message The actual error-string.
179 GRFError::GRFError(StringID severity
, StringID message
) :
186 * Create a new GRFError that is a deep copy of an existing error message.
187 * @param error The GRFError object to make a copy of.
189 GRFError::GRFError(const GRFError
&error
) :
190 ZeroedMemoryAllocator(),
191 custom_message(error
.custom_message
),
193 message(error
.message
),
194 severity(error
.severity
)
196 if (error
.custom_message
!= NULL
) this->custom_message
= xstrdup(error
.custom_message
);
197 if (error
.data
!= NULL
) this->data
= xstrdup(error
.data
);
198 memcpy(this->param_value
, error
.param_value
, sizeof(this->param_value
));
201 GRFError::~GRFError()
203 free(this->custom_message
);
208 * Create a new empty GRFParameterInfo object.
209 * @param nr The newgrf parameter that is changed.
211 GRFParameterInfo::GRFParameterInfo(uint nr
) :
214 type(PTYPE_UINT_ENUM
),
216 max_value(UINT32_MAX
),
221 complete_labels(false)
225 * Create a new GRFParameterInfo object that is a deep copy of an existing
226 * parameter info object.
227 * @param info The GRFParameterInfo object to make a copy of.
229 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo
&info
) :
233 min_value(info
.min_value
),
234 max_value(info
.max_value
),
235 def_value(info
.def_value
),
236 param_nr(info
.param_nr
),
237 first_bit(info
.first_bit
),
238 num_bit(info
.num_bit
),
239 value_names(info
.value_names
),
240 complete_labels(info
.complete_labels
)
244 /** Cleanup all parameter info. */
245 GRFParameterInfo::~GRFParameterInfo()
250 * Get the value of this user-changeable parameter from the given config.
251 * @param config The GRFConfig to get the value from.
252 * @return The value of this parameter.
254 uint32
GRFParameterInfo::GetValue(struct GRFConfig
*config
) const
256 /* GB doesn't work correctly with nbits == 32, so handle that case here. */
257 if (this->num_bit
== 32) return config
->param
[this->param_nr
];
258 return GB(config
->param
[this->param_nr
], this->first_bit
, this->num_bit
);
262 * Set the value of this user-changeable parameter in the given config.
263 * @param config The GRFConfig to set the value in.
264 * @param value The new value.
266 void GRFParameterInfo::SetValue(struct GRFConfig
*config
, uint32 value
)
268 /* SB doesn't work correctly with nbits == 32, so handle that case here. */
269 if (this->num_bit
== 32) {
270 config
->param
[this->param_nr
] = value
;
272 SB(config
->param
[this->param_nr
], this->first_bit
, this->num_bit
, value
);
274 config
->num_params
= max
<uint
>(config
->num_params
, this->param_nr
+ 1);
275 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_NEWGRF_STATE
);
279 * Finalize Action 14 info after file scan is finished.
281 void GRFParameterInfo::Finalize()
283 this->complete_labels
= true;
284 for (uint32 value
= this->min_value
; value
<= this->max_value
; value
++) {
285 if (this->value_names
.count(value
) == 0) {
286 this->complete_labels
= false;
293 * Update the palettes of the graphics from the config file.
294 * Called when changing the default palette in advanced settings.
296 * @return Always true.
298 bool UpdateNewGRFConfigPalette(int32 p1
)
300 for (GRFConfig
*c
= _grfconfig_newgame
; c
!= NULL
; c
= c
->next
) c
->SetSuitablePalette();
301 for (GRFConfig
*c
= _grfconfig_static
; c
!= NULL
; c
= c
->next
) c
->SetSuitablePalette();
302 for (GRFConfig
*c
= _all_grfs
; c
!= NULL
; c
= c
->next
) c
->SetSuitablePalette();
307 * Get the data section size of a GRF.
309 * @return Size of the data section or SIZE_MAX if the file has no separate data section.
311 size_t GRFGetSizeOfDataSection(FILE *f
)
313 extern const byte _grf_cont_v2_sig
[];
314 static const uint header_len
= 14;
316 byte data
[header_len
];
317 if (fread(data
, 1, header_len
, f
) == header_len
) {
318 if (data
[0] == 0 && data
[1] == 0 && MemCmpT(data
+ 2, _grf_cont_v2_sig
, 8) == 0) {
319 /* Valid container version 2, get data section size. */
320 size_t offset
= ((size_t)data
[13] << 24) | ((size_t)data
[12] << 16) | ((size_t)data
[11] << 8) | (size_t)data
[10];
321 if (offset
>= 1 * 1024 * 1024 * 1024) {
322 DEBUG(grf
, 0, "Unexpectedly large offset for NewGRF");
323 /* Having more than 1 GiB of data is very implausible. Mostly because then
324 * all pools in OpenTTD are flooded already. Or it's just Action C all over.
325 * In any case, the offsets to graphics will likely not work either. */
328 return header_len
+ offset
;
336 * Calculate the MD5 sum for a GRF, and store it in the config.
337 * @param config GRF to compute.
338 * @param subdir The subdirectory to look in.
339 * @return MD5 sum was successfully computed
341 static bool CalcGRFMD5Sum(GRFConfig
*config
, Subdirectory subdir
)
349 f
= FioFOpenFile(config
->filename
, "rb", subdir
, &size
);
350 if (f
== NULL
) return false;
352 long start
= ftell(f
);
353 size
= min(size
, GRFGetSizeOfDataSection(f
));
355 if (start
< 0 || fseek(f
, start
, SEEK_SET
) < 0) {
360 /* calculate md5sum */
361 while ((len
= fread(buffer
, 1, (size
> sizeof(buffer
)) ? sizeof(buffer
) : size
, f
)) != 0 && size
!= 0) {
363 checksum
.Append(buffer
, len
);
365 checksum
.Finish(config
->ident
.md5sum
);
374 * Find the GRFID of a given grf, and calculate its md5sum.
375 * @param config grf to fill.
376 * @param is_static grf is static.
377 * @param subdir the subdirectory to search in.
378 * @return Operation was successfully completed.
380 bool FillGRFDetails(GRFConfig
*config
, bool is_static
, Subdirectory subdir
)
382 if (!FioCheckFileExists(config
->filename
, subdir
)) {
383 config
->status
= GCS_NOT_FOUND
;
387 /* Find and load the Action 8 information */
388 LoadNewGRFFile(config
, CONFIG_SLOT
, GLS_FILESCAN
, subdir
);
389 config
->SetSuitablePalette();
390 config
->FinalizeParameterInfo();
392 /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
393 if (config
->ident
.grfid
== 0 || config
->ident
.grfid
== 0xFFFFFFFF || config
->IsOpenTTDBaseGRF()) return false;
396 /* Perform a 'safety scan' for static GRFs */
397 LoadNewGRFFile(config
, CONFIG_SLOT
, GLS_SAFETYSCAN
, subdir
);
399 /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
400 if (HasBit(config
->flags
, GCF_UNSAFE
)) return false;
403 return CalcGRFMD5Sum(config
, subdir
);
408 * Clear a GRF Config list, freeing all nodes.
409 * @param config Start of the list.
410 * @post \a config is set to \c NULL.
412 void ClearGRFConfigList(GRFConfig
**config
)
415 for (c
= *config
; c
!= NULL
; c
= next
) {
424 * Copy a GRF Config list
425 * @param dst pointer to destination list
426 * @param src pointer to source list values
427 * @param init_only the copied GRF will be processed up to GLS_INIT
428 * @return pointer to the last value added to the destination list
430 GRFConfig
**CopyGRFConfigList(GRFConfig
**dst
, const GRFConfig
*src
, bool init_only
)
432 /* Clear destination as it will be overwritten */
433 ClearGRFConfigList(dst
);
434 for (; src
!= NULL
; src
= src
->next
) {
435 GRFConfig
*c
= new GRFConfig(*src
);
437 ClrBit(c
->flags
, GCF_INIT_ONLY
);
438 if (init_only
) SetBit(c
->flags
, GCF_INIT_ONLY
);
448 * Removes duplicates from lists of GRFConfigs. These duplicates
449 * are introduced when the _grfconfig_static GRFs are appended
450 * to the _grfconfig on a newgame or savegame. As the parameters
451 * of the static GRFs could be different that the parameters of
452 * the ones used non-statically. This can result in desyncs in
453 * multiplayers, so the duplicate static GRFs have to be removed.
455 * This function _assumes_ that all static GRFs are placed after
456 * the non-static GRFs.
458 * @param list the list to remove the duplicates from
460 static void RemoveDuplicatesFromGRFConfigList(GRFConfig
*list
)
465 if (list
== NULL
) return;
467 for (prev
= list
, cur
= list
->next
; cur
!= NULL
; prev
= cur
, cur
= cur
->next
) {
468 if (cur
->ident
.grfid
!= list
->ident
.grfid
) continue;
470 prev
->next
= cur
->next
;
472 cur
= prev
; // Just go back one so it continues as normal later on
475 RemoveDuplicatesFromGRFConfigList(list
->next
);
479 * Appends the static GRFs to a list of GRFs
480 * @param dst the head of the list to add to
482 void AppendStaticGRFConfigs(GRFConfig
**dst
)
484 GRFConfig
**tail
= dst
;
485 while (*tail
!= NULL
) tail
= &(*tail
)->next
;
487 CopyGRFConfigList(tail
, _grfconfig_static
, false);
488 RemoveDuplicatesFromGRFConfigList(*dst
);
492 * Appends an element to a list of GRFs
493 * @param dst the head of the list to add to
494 * @param el the new tail to be
496 void AppendToGRFConfigList(GRFConfig
**dst
, GRFConfig
*el
)
498 GRFConfig
**tail
= dst
;
499 while (*tail
!= NULL
) tail
= &(*tail
)->next
;
502 RemoveDuplicatesFromGRFConfigList(*dst
);
506 /** Reset the current GRF Config to either blank or newgame settings. */
507 void ResetGRFConfig(bool defaults
)
509 CopyGRFConfigList(&_grfconfig
, _grfconfig_newgame
, !defaults
);
510 AppendStaticGRFConfigs(&_grfconfig
);
515 * Check if all GRFs in the GRF config from a savegame can be loaded.
516 * @param grfconfig GrfConfig to check
517 * @return will return any of the following 3 values:<br>
519 * <li> GLC_ALL_GOOD: No problems occurred, all GRF files were found and loaded
520 * <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
521 * compatible GRF with the same grfid was found and used instead
522 * <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
525 GRFListCompatibility
IsGoodGRFConfigList(GRFConfig
*grfconfig
)
527 GRFListCompatibility res
= GLC_ALL_GOOD
;
529 for (GRFConfig
*c
= grfconfig
; c
!= NULL
; c
= c
->next
) {
530 const GRFConfig
*f
= FindGRFConfig(c
->ident
.grfid
, FGCM_EXACT
, c
->ident
.md5sum
);
531 if (f
== NULL
|| HasBit(f
->flags
, GCF_INVALID
)) {
533 md5sumToString (buf
, c
->ident
.md5sum
);
535 /* If we have not found the exactly matching GRF try to find one with the
536 * same grfid, as it most likely is compatible */
537 f
= FindGRFConfig(c
->ident
.grfid
, FGCM_COMPATIBLE
, NULL
, c
->version
);
539 /* No compatible grf was found, mark it as disabled */
540 DEBUG(grf
, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c
->ident
.grfid
), c
->filename
, buf
);
542 c
->status
= GCS_NOT_FOUND
;
547 DEBUG(grf
, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c
->ident
.grfid
), c
->filename
, buf
);
548 if (!HasBit(c
->flags
, GCF_COMPATIBLE
)) {
549 /* Preserve original_md5sum after it has been assigned */
550 SetBit(c
->flags
, GCF_COMPATIBLE
);
551 memcpy(c
->original_md5sum
, c
->ident
.md5sum
, sizeof(c
->original_md5sum
));
554 /* Non-found has precedence over compatibility load */
555 if (res
!= GLC_NOT_FOUND
) res
= GLC_COMPATIBLE
;
558 DEBUG(grf
, 1, "Loading GRF %08X from %s", BSWAP32(f
->ident
.grfid
), f
->filename
);
559 /* The filename could be the filename as in the savegame. As we need
560 * to load the GRF here, we need the correct filename, so overwrite that
561 * in any case and set the name and info when it is not set already.
562 * When the GCF_COPY flag is set, it is certain that the filename is
563 * already a local one, so there is no need to replace it. */
564 if (!HasBit(c
->flags
, GCF_COPY
)) {
566 c
->filename
= xstrdup(f
->filename
);
567 memcpy(c
->ident
.md5sum
, f
->ident
.md5sum
, sizeof(c
->ident
.md5sum
));
571 c
->version
= f
->version
;
572 c
->min_loadable_version
= f
->min_loadable_version
;
573 c
->num_valid_params
= f
->num_valid_params
;
574 c
->has_param_defaults
= f
->has_param_defaults
;
575 for (uint i
= 0; i
< f
->param_info
.Length(); i
++) {
576 if (f
->param_info
[i
] == NULL
) {
577 *c
->param_info
.Append() = NULL
;
579 *c
->param_info
.Append() = new GRFParameterInfo(*f
->param_info
[i
]);
588 /** Helper for scanning for files with GRF as extension */
589 class GRFFileScanner
: FileScanner
{
590 uint next_update
; ///< The next (realtime tick) we do update the screen.
591 uint num_scanned
; ///< The number of GRFs we have scanned.
594 GRFFileScanner() : next_update(_realtime_tick
), num_scanned(0)
598 /* virtual */ bool AddFile(const char *filename
, size_t basepath_length
, const char *tar_filename
);
600 /** Do the scan for GRFs. */
604 int ret
= fs
.Scan(".grf", NEWGRF_DIR
);
605 /* The number scanned and the number returned may not be the same;
606 * duplicate NewGRFs and base sets are ignored in the return value. */
607 _settings_client
.gui
.last_newgrf_count
= fs
.num_scanned
;
612 bool GRFFileScanner::AddFile(const char *filename
, size_t basepath_length
, const char *tar_filename
)
614 GRFConfig
*c
= new GRFConfig(filename
+ basepath_length
);
617 if (FillGRFDetails(c
, false)) {
618 if (_all_grfs
== NULL
) {
621 /* Insert file into list at a position determined by its
622 * name, so the list is sorted as we go along */
625 for (pd
= &_all_grfs
; (d
= *pd
) != NULL
; pd
= &d
->next
) {
626 if (c
->ident
.matches (d
->ident
)) added
= false;
627 /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
628 * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
629 * just after the first with the same name. Avoids doubles in the list. */
630 if (strcasecmp(c
->GetName(), d
->GetName()) <= 0) {
646 if (this->next_update
<= _realtime_tick
) {
647 _modal_progress_work_mutex
->EndCritical();
648 _modal_progress_paint_mutex
->BeginCritical();
650 const char *name
= c
->name
->get_string();
651 if (name
== NULL
) name
= c
->filename
;
652 UpdateNewGRFScanStatus(this->num_scanned
, name
);
654 _modal_progress_work_mutex
->BeginCritical();
655 _modal_progress_paint_mutex
->EndCritical();
657 this->next_update
= _realtime_tick
+ 200;
661 /* File couldn't be opened, or is either not a NewGRF or is a
662 * 'system' NewGRF or it's already known, so forget about it. */
670 * Simple sorter for GRFS
671 * @param p1 the first GRFConfig *
672 * @param p2 the second GRFConfig *
673 * @return the same strcmp would return for the name of the NewGRF.
675 static int CDECL
GRFSorter(GRFConfig
* const *p1
, GRFConfig
* const *p2
)
677 const GRFConfig
*c1
= *p1
;
678 const GRFConfig
*c2
= *p2
;
680 return strnatcmp(c1
->GetName(), c2
->GetName());
684 * Really perform the scan for all NewGRFs.
685 * @param callback The callback to call after the scanning is complete.
687 void DoScanNewGRFFiles(void *callback
)
689 _modal_progress_work_mutex
->BeginCritical();
691 ClearGRFConfigList(&_all_grfs
);
692 TarScanner::DoScan(TarScanner::NEWGRF
);
694 DEBUG(grf
, 1, "Scanning for NewGRFs");
695 uint num
= GRFFileScanner::DoScan();
697 DEBUG(grf
, 1, "Scan complete, found %d files", num
);
698 if (num
!= 0 && _all_grfs
!= NULL
) {
699 /* Sort the linked list using quicksort.
700 * For that we first have to make an array, then sort and
701 * then remake the linked list. */
702 GRFConfig
**to_sort
= xmalloct
<GRFConfig
*>(num
);
705 for (GRFConfig
*p
= _all_grfs
; p
!= NULL
; p
= p
->next
, i
++) {
708 /* Number of files is not necessarily right */
711 QSortT(to_sort
, num
, &GRFSorter
);
713 for (i
= 1; i
< num
; i
++) {
714 to_sort
[i
- 1]->next
= to_sort
[i
];
716 to_sort
[num
- 1]->next
= NULL
;
717 _all_grfs
= to_sort
[0];
721 #ifdef ENABLE_NETWORK
722 NetworkAfterNewGRFScan();
726 _modal_progress_work_mutex
->EndCritical();
727 _modal_progress_paint_mutex
->BeginCritical();
729 /* Yes... these are the NewGRF windows */
730 InvalidateWindowClassesData(WC_SAVELOAD
, 0, true);
731 InvalidateWindowData(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_NEWGRF_STATE
, GOID_NEWGRF_RESCANNED
, true);
732 if (callback
!= NULL
) ((NewGRFScanCallback
*)callback
)->OnNewGRFsScanned();
734 DeleteWindowByClass(WC_MODAL_PROGRESS
);
735 SetModalProgress(false);
736 MarkWholeScreenDirty();
737 _modal_progress_paint_mutex
->EndCritical();
741 * Scan for all NewGRFs.
742 * @param callback The callback to call after the scanning is complete.
744 void ScanNewGRFFiles(NewGRFScanCallback
*callback
)
746 /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
747 SetModalProgress(true);
748 /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
749 MarkWholeScreenDirty();
751 if (!VideoDriver::GetActiveDriver()->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles
, callback
, NULL
, "ottd:newgrf-scan")) {
752 _modal_progress_work_mutex
->EndCritical();
753 _modal_progress_paint_mutex
->EndCritical();
754 DoScanNewGRFFiles(callback
);
755 _modal_progress_paint_mutex
->BeginCritical();
756 _modal_progress_work_mutex
->BeginCritical();
758 UpdateNewGRFScanStatus(0, NULL
);
763 * Find a NewGRF in the scanned list.
764 * @param grfid GRFID to look for,
765 * @param mode Restrictions for matching grfs
766 * @param md5sum Expected MD5 sum
767 * @param desired_version Requested version
768 * @return The matching grf, if it exists in #_all_grfs, else \c NULL.
770 const GRFConfig
*FindGRFConfig(uint32 grfid
, FindGRFConfigMode mode
, const uint8
*md5sum
, uint32 desired_version
)
772 assert((mode
== FGCM_EXACT
) != (md5sum
== NULL
));
773 const GRFConfig
*best
= NULL
;
774 for (const GRFConfig
*c
= _all_grfs
; c
!= NULL
; c
= c
->next
) {
775 /* if md5sum is set, we look for an exact match and continue if not found */
776 if (!c
->ident
.matches (grfid
, md5sum
)) continue;
777 /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
778 if (md5sum
!= NULL
|| mode
== FGCM_ANY
) return c
;
779 /* Skip incompatible stuff, unless explicitly allowed */
780 if (mode
!= FGCM_NEWEST
&& HasBit(c
->flags
, GCF_INVALID
)) continue;
781 /* check version compatibility */
782 if (mode
== FGCM_COMPATIBLE
&& (c
->version
< desired_version
|| c
->min_loadable_version
> desired_version
)) continue;
783 /* remember the newest one as "the best" */
784 if (best
== NULL
|| c
->version
> best
->version
) best
= c
;
790 #ifdef ENABLE_NETWORK
792 /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
793 struct UnknownGRF
: public GRFIdentifier
{
794 UnknownGRF
*next
; ///< The next unknown GRF.
795 ttd_shared_ptr
<GRFTextMap
> name
; ///< Name of the GRF.
799 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
800 * unknown GRF is a GRF where the .grf is not found during scanning.
802 * The names are resolved via UDP calls to servers that should know the name,
803 * though the replies may not come. This leaves "<Unknown>" as name, though
804 * that shouldn't matter _very_ much as they need GRF crawler or so to look
805 * up the GRF anyway and that works better with the GRF ID.
807 * @param ident the GRF identifier
808 * @param create whether to create a new GRFConfig if the GRFConfig did not
809 * exist in the fake list of GRFConfigs.
810 * If true, a GRFConfig will be returned unconditionally, and
811 * it will be created if it does not exist yet.
812 * If false, a GRFConfig will be returned only if it already
813 * existed and is still unknown; otherwise, NULL will be returned.
814 * @return The shared GRFTextMap of the name of the GRFConfig with the given GRF ID
815 * and MD5 checksum or NULL when it does not exist and create is false.
816 * This value must NEVER be freed by the caller.
818 ttd_shared_ptr
<GRFTextMap
> *FindUnknownGRFName (const GRFIdentifier
&ident
, bool create
)
821 static UnknownGRF
*unknown_grfs
= NULL
;
823 for (grf
= unknown_grfs
; grf
!= NULL
; grf
= grf
->next
) {
824 if (grf
->matches (ident
)) {
825 return (create
|| strcmp (grf
->name
->get_string(), UNKNOWN_GRF_NAME_PLACEHOLDER
) == 0) ? &grf
->name
: NULL
;
829 if (!create
) return NULL
;
831 grf
= new UnknownGRF
;
832 grf
->grfid
= ident
.grfid
;
833 memcpy (grf
->md5sum
, ident
.md5sum
, sizeof(grf
->md5sum
));
834 grf
->next
= unknown_grfs
;
836 GRFTextMap
*map
= new GRFTextMap
;
837 map
->add_default (UNKNOWN_GRF_NAME_PLACEHOLDER
);
838 grf
->name
.reset (map
);
844 #endif /* ENABLE_NETWORK */
848 * Retrieve a NewGRF from the current config by its grfid.
849 * @param grfid grf to look for.
850 * @param mask GRFID mask to allow for partial matching.
851 * @return The grf config, if it exists, else \c NULL.
853 GRFConfig
*GetGRFConfig(uint32 grfid
, uint32 mask
)
857 for (c
= _grfconfig
; c
!= NULL
; c
= c
->next
) {
858 if ((c
->ident
.grfid
& mask
) == (grfid
& mask
)) return c
;
865 /** Build a string containing space separated parameter values, and terminate */
866 void GRFBuildParamList (stringb
*dst
, const GRFConfig
*c
)
868 /* Return an empty string if there are no parameters */
869 if (c
->num_params
== 0) {
874 dst
->fmt ("%d", c
->param
[0]);
876 for (uint i
= 1; i
< c
->num_params
; i
++) {
877 dst
->append_fmt (" %d", c
->param
[i
]);
881 /** Base GRF ID for OpenTTD's base graphics GRFs. */
882 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID
= BSWAP32(0xFF4F5400);
885 * Checks whether this GRF is a OpenTTD base graphic GRF.
886 * @return true if and only if it is a base GRF.
888 bool GRFConfig::IsOpenTTDBaseGRF() const
890 return (this->ident
.grfid
& 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID
;
894 * Search a textfile file next to this NewGRF.
895 * @param type The type of the textfile to search for.
896 * @return A description for the textfile.
898 TextfileDesc
GRFConfig::GetTextfile (TextfileType type
) const
900 return TextfileDesc (type
, NEWGRF_DIR
, this->filename
);