OpenTTD Source: src/newgrf_config.cpp Source File

OpenTTD Source 14.0-beta1

newgrf_config.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "3rdparty/md5/md5.h"
13 #include "newgrf.h"
14 #include "network/network_func.h"
15 #include "gfx_func.h"
16 #include "newgrf_text.h"
17 #include "window_func.h"
18 #include "progress.h"
19 #include "video/video_driver.hpp"
20 #include "string_func.h"
21 #include "strings_func.h"
22 #include "textfile_gui.h"
23 #include "thread.h"
24 #include "newgrf_config.h"
25 #include "newgrf_text.h"
26
27 #include "fileio_func.h"
28 #include "fios.h"
29
30 #include "safeguards.h"
31
32
37 GRFConfig::GRFConfig(const std::string &filename) :
38 filename(filename), num_valid_params(ClampTo<uint8_t>(GRFConfig::param.size()))
39 {
40 }
41
46 GRFConfig::GRFConfig(const GRFConfig &config) :
47 ZeroedMemoryAllocator(),
48 ident(config.ident),
49 original_md5sum(config.original_md5sum),
50 filename(config.filename),
51 name(config.name),
52 info(config.info),
53 url(config.url),
54 error(config.error),
55 version(config.version),
56 min_loadable_version(config.min_loadable_version),
57 flags(config.flags & ~(1 << GCF_COPY)),
58 status(config.status),
59 grf_bugs(config.grf_bugs),
60 param(config.param),
61 num_params(config.num_params),
62 num_valid_params(config.num_valid_params),
63 palette(config.palette),
64 param_info(config.param_info),
65 has_param_defaults(config.has_param_defaults)
66 {
67 }
68
69 void GRFConfig::SetParams(const std::vector<uint32_t> &pars)
70 {
71 this->num_params = static_cast<uint8_t>(std::min(this->param.size(), pars.size()));
72 std::copy(pars.begin(), pars.begin() + this->num_params, this->param.begin());
73 }
74
78 bool GRFConfig::IsCompatible(uint32_t old_version) const
79 {
80 return this->min_loadable_version <= old_version && old_version <= this->version;
81 }
82
87 void GRFConfig::CopyParams(const GRFConfig &src)
88 {
89 this->num_params = src.num_params;
90 this->param = src.param;
91 }
92
98 const char *GRFConfig::GetName() const
99 {
100 const char *name = GetGRFStringFromGRFText(this->name);
101 return StrEmpty(name) ? this->filename.c_str() : name;
102 }
103
108 const char *GRFConfig::GetDescription() const
109 {
110 return GetGRFStringFromGRFText(this->info);
111 }
112
117 const char *GRFConfig::GetURL() const
118 {
119 return GetGRFStringFromGRFText(this->url);
120 }
121
123 void GRFConfig::SetParameterDefaults()
124 {
125 this->num_params = 0;
126 this->param = {};
127
128 if (!this->has_param_defaults) return;
129
130 for (uint i = 0; i < this->param_info.size(); i++) {
131 if (!this->param_info[i]) continue;
132 this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
133 }
134 }
135
141 void GRFConfig::SetSuitablePalette()
142 {
143 PaletteType pal;
144 switch (this->palette & GRFP_GRF_MASK) {
145 case GRFP_GRF_DOS: pal = PAL_DOS; break;
146 case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS; break;
147 default: pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
148 }
149 SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
150 }
151
155 void GRFConfig::FinalizeParameterInfo()
156 {
157 for (auto &info : this->param_info) {
158 if (!info.has_value()) continue;
159 info->Finalize();
160 }
161 }
162
163 GRFConfig *_all_grfs;
164 GRFConfig *_grfconfig;
165 GRFConfig *_grfconfig_newgame;
166 GRFConfig *_grfconfig_static;
167 uint _missing_extra_graphics = 0;
168
174 GRFError::GRFError(StringID severity, StringID message) : message(message), severity(severity)
175 {
176 }
177
182 GRFParameterInfo::GRFParameterInfo(uint nr) :
183 name(),
184 desc(),
185 type(PTYPE_UINT_ENUM),
186 min_value(0),
187 max_value(UINT32_MAX),
188 def_value(0),
189 param_nr(nr),
190 first_bit(0),
191 num_bit(32),
192 value_names(),
193 complete_labels(false)
194 {}
195
201 uint32_t GRFParameterInfo::GetValue(struct GRFConfig *config) const
202 {
203 /* GB doesn't work correctly with nbits == 32, so handle that case here. */
204 if (this->num_bit == 32) return config->param[this->param_nr];
205 return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
206 }
207
213 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32_t value)
214 {
215 /* SB doesn't work correctly with nbits == 32, so handle that case here. */
216 if (this->num_bit == 32) {
217 config->param[this->param_nr] = value;
218 } else {
219 SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
220 }
221 config->num_params = std::max<uint>(config->num_params, this->param_nr + 1);
222 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
223 }
224
228 void GRFParameterInfo::Finalize()
229 {
230 this->complete_labels = true;
231 for (uint32_t value = this->min_value; value <= this->max_value; value++) {
232 if (this->value_names.count(value) == 0) {
233 this->complete_labels = false;
234 break;
235 }
236 }
237 }
238
243 void UpdateNewGRFConfigPalette(int32_t)
244 {
245 for (GRFConfig *c = _grfconfig_newgame; c != nullptr; c = c->next) c->SetSuitablePalette();
246 for (GRFConfig *c = _grfconfig_static; c != nullptr; c = c->next) c->SetSuitablePalette();
247 for (GRFConfig *c = _all_grfs; c != nullptr; c = c->next) c->SetSuitablePalette();
248 }
249
255 size_t GRFGetSizeOfDataSection(FILE *f)
256 {
257 extern const byte _grf_cont_v2_sig[];
258 static const uint header_len = 14;
259
260 byte data[header_len];
261 if (fread(data, 1, header_len, f) == header_len) {
262 if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
263 /* Valid container version 2, get data section size. */
264 size_t offset = ((size_t)data[13] << 24) | ((size_t)data[12] << 16) | ((size_t)data[11] << 8) | (size_t)data[10];
265 if (offset >= 1 * 1024 * 1024 * 1024) {
266 Debug(grf, 0, "Unexpectedly large offset for NewGRF");
267 /* Having more than 1 GiB of data is very implausible. Mostly because then
268 * all pools in OpenTTD are flooded already. Or it's just Action C all over.
269 * In any case, the offsets to graphics will likely not work either. */
270 return SIZE_MAX;
271 }
272 return header_len + offset;
273 }
274 }
275
276 return SIZE_MAX;
277 }
278
285 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
286 {
287 FILE *f;
288 Md5 checksum;
289 uint8_t buffer[1024];
290 size_t len, size;
291
292 /* open the file */
293 f = FioFOpenFile(config->filename, "rb", subdir, &size);
294 if (f == nullptr) return false;
295
296 long start = ftell(f);
297 size = std::min(size, GRFGetSizeOfDataSection(f));
298
299 if (start < 0 || fseek(f, start, SEEK_SET) < 0) {
300 FioFCloseFile(f);
301 return false;
302 }
303
304 /* calculate md5sum */
305 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
306 size -= len;
307 checksum.Append(buffer, len);
308 }
309 checksum.Finish(config->ident.md5sum);
310
311 FioFCloseFile(f);
312
313 return true;
314 }
315
316
324 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
325 {
326 if (!FioCheckFileExists(config->filename, subdir)) {
327 config->status = GCS_NOT_FOUND;
328 return false;
329 }
330
331 /* Find and load the Action 8 information */
332 LoadNewGRFFile(config, GLS_FILESCAN, subdir, true);
333 config->SetSuitablePalette();
334 config->FinalizeParameterInfo();
335
336 /* Skip if the grfid is 0 (not read) or if it is an internal GRF */
337 if (config->ident.grfid == 0 || HasBit(config->flags, GCF_SYSTEM)) return false;
338
339 if (is_static) {
340 /* Perform a 'safety scan' for static GRFs */
341 LoadNewGRFFile(config, GLS_SAFETYSCAN, subdir, true);
342
343 /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
344 if (HasBit(config->flags, GCF_UNSAFE)) return false;
345 }
346
347 return CalcGRFMD5Sum(config, subdir);
348 }
349
350
356 void ClearGRFConfigList(GRFConfig **config)
357 {
358 GRFConfig *c, *next;
359 for (c = *config; c != nullptr; c = next) {
360 next = c->next;
361 delete c;
362 }
363 *config = nullptr;
364 }
365
366
374 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
375 {
376 /* Clear destination as it will be overwritten */
377 ClearGRFConfigList(dst);
378 for (; src != nullptr; src = src->next) {
379 GRFConfig *c = new GRFConfig(*src);
380
381 ClrBit(c->flags, GCF_INIT_ONLY);
382 if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
383
384 *dst = c;
385 dst = &c->next;
386 }
387
388 return dst;
389 }
390
404 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
405 {
406 GRFConfig *prev;
407 GRFConfig *cur;
408
409 if (list == nullptr) return;
410
411 for (prev = list, cur = list->next; cur != nullptr; prev = cur, cur = cur->next) {
412 if (cur->ident.grfid != list->ident.grfid) continue;
413
414 prev->next = cur->next;
415 delete cur;
416 cur = prev; // Just go back one so it continues as normal later on
417 }
418
419 RemoveDuplicatesFromGRFConfigList(list->next);
420 }
421
426 void AppendStaticGRFConfigs(GRFConfig **dst)
427 {
428 GRFConfig **tail = dst;
429 while (*tail != nullptr) tail = &(*tail)->next;
430
431 CopyGRFConfigList(tail, _grfconfig_static, false);
432 RemoveDuplicatesFromGRFConfigList(*dst);
433 }
434
440 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
441 {
442 GRFConfig **tail = dst;
443 while (*tail != nullptr) tail = &(*tail)->next;
444 *tail = el;
445
446 RemoveDuplicatesFromGRFConfigList(*dst);
447 }
448
449
451 void ResetGRFConfig(bool defaults)
452 {
453 CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
454 AppendStaticGRFConfigs(&_grfconfig);
455 }
456
457
469 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
470 {
471 GRFListCompatibility res = GLC_ALL_GOOD;
472
473 for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) {
474 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
475 if (f == nullptr || HasBit(f->flags, GCF_INVALID)) {
476 /* If we have not found the exactly matching GRF try to find one with the
477 * same grfid, as it most likely is compatible */
478 f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version);
479 if (f != nullptr) {
480 Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum));
481 if (!HasBit(c->flags, GCF_COMPATIBLE)) {
482 /* Preserve original_md5sum after it has been assigned */
483 SetBit(c->flags, GCF_COMPATIBLE);
484 c->original_md5sum = c->ident.md5sum;
485 }
486
487 /* Non-found has precedence over compatibility load */
488 if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
489 goto compatible_grf;
490 }
491
492 /* No compatible grf was found, mark it as disabled */
493 Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum));
494
495 c->status = GCS_NOT_FOUND;
496 res = GLC_NOT_FOUND;
497 } else {
498 compatible_grf:
499 Debug(grf, 1, "Loading GRF {:08X} from {}", BSWAP32(f->ident.grfid), f->filename);
500 /* The filename could be the filename as in the savegame. As we need
501 * to load the GRF here, we need the correct filename, so overwrite that
502 * in any case and set the name and info when it is not set already.
503 * When the GCF_COPY flag is set, it is certain that the filename is
504 * already a local one, so there is no need to replace it. */
505 if (!HasBit(c->flags, GCF_COPY)) {
506 c->filename = f->filename;
507 c->ident.md5sum = f->ident.md5sum;
508 c->name = f->name;
509 c->info = f->name;
510 c->error.reset();
511 c->version = f->version;
512 c->min_loadable_version = f->min_loadable_version;
513 c->num_valid_params = f->num_valid_params;
514 c->param_info = f->param_info;
515 c->has_param_defaults = f->has_param_defaults;
516 }
517 }
518 }
519
520 return res;
521 }
522
523
525 int _skip_all_newgrf_scanning = 0;
526
528 class GRFFileScanner : FileScanner {
529 std::chrono::steady_clock::time_point next_update;
530 uint num_scanned;
531
532 public:
533 GRFFileScanner() : num_scanned(0)
534 {
535 this->next_update = std::chrono::steady_clock::now();
536 }
537
538 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
539
541 static uint DoScan()
542 {
543 if (_skip_all_newgrf_scanning > 0) {
544 if (_skip_all_newgrf_scanning == 1) _skip_all_newgrf_scanning = 0;
545 return 0;
546 }
547
548 GRFFileScanner fs;
549 int ret = fs.Scan(".grf", NEWGRF_DIR);
550 /* The number scanned and the number returned may not be the same;
551 * duplicate NewGRFs and base sets are ignored in the return value. */
552 _settings_client.gui.last_newgrf_count = fs.num_scanned;
553 return ret;
554 }
555 };
556
557 bool GRFFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &)
558 {
559 /* Abort if the user stopped the game during a scan. */
560 if (_exit_game) return false;
561
562 GRFConfig *c = new GRFConfig(filename.c_str() + basepath_length);
563
564 bool added = true;
565 if (FillGRFDetails(c, false)) {
566 if (_all_grfs == nullptr) {
567 _all_grfs = c;
568 } else {
569 /* Insert file into list at a position determined by its
570 * name, so the list is sorted as we go along */
571 GRFConfig **pd, *d;
572 bool stop = false;
573 for (pd = &_all_grfs; (d = *pd) != nullptr; pd = &d->next) {
574 if (c->ident.grfid == d->ident.grfid && c->ident.md5sum == d->ident.md5sum) added = false;
575 /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
576 * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
577 * just after the first with the same name. Avoids doubles in the list. */
578 if (StrCompareIgnoreCase(c->GetName(), d->GetName()) <= 0) {
579 stop = true;
580 } else if (stop) {
581 break;
582 }
583 }
584 if (added) {
585 c->next = d;
586 *pd = c;
587 }
588 }
589 } else {
590 added = false;
591 }
592
593 this->num_scanned++;
594
595 const char *name = nullptr;
596 if (c->name != nullptr) name = GetGRFStringFromGRFText(c->name);
597 if (name == nullptr) name = c->filename.c_str();
598 UpdateNewGRFScanStatus(this->num_scanned, name);
599 VideoDriver::GetInstance()->GameLoopPause();
600
601 if (!added) {
602 /* File couldn't be opened, or is either not a NewGRF or is a
603 * 'system' NewGRF or it's already known, so forget about it. */
604 delete c;
605 }
606
607 return added;
608 }
609
616 static bool GRFSorter(GRFConfig * const &c1, GRFConfig * const &c2)
617 {
618 return StrNaturalCompare(c1->GetName(), c2->GetName()) < 0;
619 }
620
625 void DoScanNewGRFFiles(NewGRFScanCallback *callback)
626 {
627 ClearGRFConfigList(&_all_grfs);
628 TarScanner::DoScan(TarScanner::NEWGRF);
629
630 Debug(grf, 1, "Scanning for NewGRFs");
631 uint num = GRFFileScanner::DoScan();
632
633 Debug(grf, 1, "Scan complete, found {} files", num);
634 if (num != 0 && _all_grfs != nullptr) {
635 /* Sort the linked list using quicksort.
636 * For that we first have to make an array, then sort and
637 * then remake the linked list. */
638 std::vector<GRFConfig *> to_sort;
639
640 uint i = 0;
641 for (GRFConfig *p = _all_grfs; p != nullptr; p = p->next, i++) {
642 to_sort.push_back(p);
643 }
644 /* Number of files is not necessarily right */
645 num = i;
646
647 std::sort(to_sort.begin(), to_sort.end(), GRFSorter);
648
649 for (i = 1; i < num; i++) {
650 to_sort[i - 1]->next = to_sort[i];
651 }
652 to_sort[num - 1]->next = nullptr;
653 _all_grfs = to_sort[0];
654
655 NetworkAfterNewGRFScan();
656 }
657
658 /* Yes... these are the NewGRF windows */
659 InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
660 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
661 if (!_exit_game && callback != nullptr) callback->OnNewGRFsScanned();
662
663 CloseWindowByClass(WC_MODAL_PROGRESS);
664 SetModalProgress(false);
665 MarkWholeScreenDirty();
666 }
667
672 void ScanNewGRFFiles(NewGRFScanCallback *callback)
673 {
674 /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
675 SetModalProgress(true);
676 /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
677 MarkWholeScreenDirty();
678
679 DoScanNewGRFFiles(callback);
680 }
681
690 const GRFConfig *FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
691 {
692 assert((mode == FGCM_EXACT) != (md5sum == nullptr));
693 const GRFConfig *best = nullptr;
694 for (const GRFConfig *c = _all_grfs; c != nullptr; c = c->next) {
695 /* if md5sum is set, we look for an exact match and continue if not found */
696 if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
697 /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
698 if (md5sum != nullptr || mode == FGCM_ANY) return c;
699 /* Skip incompatible stuff, unless explicitly allowed */
700 if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
701 /* check version compatibility */
702 if (mode == FGCM_COMPATIBLE && !c->IsCompatible(desired_version)) continue;
703 /* remember the newest one as "the best" */
704 if (best == nullptr || c->version > best->version) best = c;
705 }
706
707 return best;
708 }
709
716 GRFConfig *GetGRFConfig(uint32_t grfid, uint32_t mask)
717 {
718 GRFConfig *c;
719
720 for (c = _grfconfig; c != nullptr; c = c->next) {
721 if ((c->ident.grfid & mask) == (grfid & mask)) return c;
722 }
723
724 return nullptr;
725 }
726
727
729 std::string GRFBuildParamList(const GRFConfig *c)
730 {
731 std::string result;
732 for (uint i = 0; i < c->num_params; i++) {
733 if (!result.empty()) result += ' ';
734 result += std::to_string(c->param[i]);
735 }
736 return result;
737 }
738
744 std::optional<std::string> GRFConfig::GetTextfile(TextfileType type) const
745 {
746 return ::GetTextfile(type, NEWGRF_DIR, this->filename);
747 }
GRFConfig::SetSuitablePalette
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
Definition: newgrf_config.cpp:141
WC_SAVELOAD
@ WC_SAVELOAD
Saveload window; Window numbers:
Definition: window_type.h:144
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3200
CloseWindowByClass
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition: window.cpp:1153
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:158
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const byte > data)
Format a byte array into a continuous hex string.
Definition: string.cpp:88
PAL_DOS
@ PAL_DOS
Use the DOS palette.
Definition: gfx_type.h:300
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3082
GRFConfig::num_valid_params
uint8_t num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
Definition: newgrf_config.h:169
GLC_COMPATIBLE
@ GLC_COMPATIBLE
Compatible (eg. the same ID, but different checksum) GRF found in at least one case.
Definition: newgrf_config.h:54
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:356
GRFParameterInfo::GetValue
uint32_t GetValue(struct GRFConfig *config) const
Get the value of this user-changeable parameter from the given config.
Definition: newgrf_config.cpp:201
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:427
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
DoScanNewGRFFiles
void DoScanNewGRFFiles(NewGRFScanCallback *callback)
Really perform the scan for all NewGRFs.
Definition: newgrf_config.cpp:625
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
_all_grfs
GRFConfig * _all_grfs
First item in list of all scanned NewGRFs.
Definition: newgrf_config.cpp:163
_skip_all_newgrf_scanning
int _skip_all_newgrf_scanning
Set this flag to prevent any NewGRF scanning from being done.
Definition: newgrf_config.cpp:525
GRFSorter
static bool GRFSorter(GRFConfig *const &c1, GRFConfig *const &c2)
Simple sorter for GRFS.
Definition: newgrf_config.cpp:616
PaletteType
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:299
FileScanner::Scan
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1210
GRFConfig::filename
std::string filename
Filename - either with or without full path.
Definition: newgrf_config.h:156
FGCM_COMPATIBLE
@ FGCM_COMPATIBLE
Find best compatible Grf wrt. desired_version.
Definition: newgrf_config.h:193
GRFParameterInfo::param_nr
byte param_nr
GRF parameter to store content in.
Definition: newgrf_config.h:135
GRFConfig::GRFConfig
GRFConfig(const std::string &filename=std::string{})
Create a new GRFConfig.
Definition: newgrf_config.cpp:37
fileio_func.h
StrNaturalCompare
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:585
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
GUISettings::newgrf_default_palette
uint8_t newgrf_default_palette
default palette to use for NewGRFs without action 14 palette information
Definition: settings_type.h:221
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:154
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
GCF_COPY
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:27
GRFConfig::status
GRFStatus status
NOSAVE: GRFStatus, enum.
Definition: newgrf_config.h:165
GetGRFConfig
GRFConfig * GetGRFConfig(uint32_t grfid, uint32_t mask)
Retrieve a NewGRF from the current config by its grfid.
Definition: newgrf_config.cpp:716
GRFP_USE_BIT
@ GRFP_USE_BIT
The bit used for storing the palette to use.
Definition: newgrf_config.h:60
GRFConfig::min_loadable_version
uint32_t min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
Definition: newgrf_config.h:163
newgrf_config.h
fios.h
StrEmpty
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:56
WN_GAME_OPTIONS_NEWGRF_STATE
@ WN_GAME_OPTIONS_NEWGRF_STATE
NewGRF settings.
Definition: window_type.h:25
textfile_gui.h
GRFParameterInfo::min_value
uint32_t min_value
The minimal value this parameter can have.
Definition: newgrf_config.h:132
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
GCF_COMPATIBLE
@ GCF_COMPATIBLE
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches)
Definition: newgrf_config.h:26
GRFParameterInfo::num_bit
byte num_bit
Number of bits to use for this parameter.
Definition: newgrf_config.h:137
UpdateNewGRFScanStatus
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
Definition: newgrf_gui.cpp:2289
GCF_INVALID
@ GCF_INVALID
GRF is unusable with this version of OpenTTD.
Definition: newgrf_config.h:30
GCF_INIT_ONLY
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
Definition: newgrf_config.h:28
gfx_func.h
GRFConfig::GetTextfile
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
Definition: newgrf_config.cpp:744
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition: newgrf_config.h:196
WC_MODAL_PROGRESS
@ WC_MODAL_PROGRESS
Progress report of landscape generation; Window numbers:
Definition: window_type.h:463
GRFP_GRF_DOS
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
Definition: newgrf_config.h:71
GRFParameterInfo::SetValue
void SetValue(struct GRFConfig *config, uint32_t value)
Set the value of this user-changeable parameter in the given config.
Definition: newgrf_config.cpp:213
LoadNewGRFFile
void LoadNewGRFFile(GRFConfig *config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition: newgrf.cpp:9651
GRFConfig::version
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
Definition: newgrf_config.h:162
GRFParameterInfo::value_names
std::map< uint32_t, GRFTextList > value_names
Names for each value.
Definition: newgrf_config.h:138
IsGoodGRFConfigList
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Definition: newgrf_config.cpp:469
BSWAP32
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:345
GCF_UNSAFE
@ GCF_UNSAFE
GRF file is unsafe for static usage.
Definition: newgrf_config.h:24
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:147
GRFError::GRFError
GRFError(StringID severity, StringID message=0)
Construct a new GRFError.
Definition: newgrf_config.cpp:174
GCF_SYSTEM
@ GCF_SYSTEM
GRF file is an openttd-internal system grf.
Definition: newgrf_config.h:23
GRFP_USE_DOS
@ GRFP_USE_DOS
The palette state is set to use the DOS palette.
Definition: newgrf_config.h:66
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:263
GRFConfig::SetParameterDefaults
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
Definition: newgrf_config.cpp:123
GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:862
GRFP_USE_WINDOWS
@ GRFP_USE_WINDOWS
The palette state is set to use the Windows palette.
Definition: newgrf_config.h:67
GRFIdentifier::md5sum
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Definition: newgrf_config.h:85
_missing_extra_graphics
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
Definition: newgrf_config.cpp:167
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:85
CopyGRFConfigList
GRFConfig ** CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
Copy a GRF Config list.
Definition: newgrf_config.cpp:374
GetGRFStringFromGRFText
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Definition: newgrf_text.cpp:606
GRFFileScanner
Helper for scanning for files with GRF as extension.
Definition: newgrf_config.cpp:528
FindGRFConfigMode
FindGRFConfigMode
Method to find GRFs using FindGRFConfig.
Definition: newgrf_config.h:191
safeguards.h
GRFGetSizeOfDataSection
size_t GRFGetSizeOfDataSection(FILE *f)
Get the data section size of a GRF.
Definition: newgrf_config.cpp:255
MemCmpT
int MemCmpT(const T *ptr1, const T *ptr2, size_t num=1)
Type-safe version of memcmp().
Definition: mem_func.hpp:63
GRFConfig::has_param_defaults
bool has_param_defaults
NOSAVE: did this newgrf specify any defaults for it's parameters.
Definition: newgrf_config.h:172
TarScanner::NEWGRF
@ NEWGRF
Scan for non-base sets.
Definition: fileio_func.h:66
GRFConfig::IsCompatible
bool IsCompatible(uint32_t old_version) const
Return whether this NewGRF can replace an older version of the same NewGRF.
Definition: newgrf_config.cpp:78
ScanNewGRFFiles
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
Definition: newgrf_config.cpp:672
newgrf_text.h
GRFConfig::GetURL
const char * GetURL() const
Get the grf url.
Definition: newgrf_config.cpp:117
GRFP_GRF_MASK
@ GRFP_GRF_MASK
Bitmask to get only the NewGRF supplied information.
Definition: newgrf_config.h:74
stdafx.h
ClampTo
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
Definition: math_func.hpp:167
GRFConfig::CopyParams
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
Definition: newgrf_config.cpp:87
GRFFileScanner::DoScan
static uint DoScan()
Do the scan for GRFs.
Definition: newgrf_config.cpp:541
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition: fileio_type.h:117
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:200
GRFConfig::palette
uint8_t palette
GRFPalette, bitset.
Definition: newgrf_config.h:170
_grfconfig_static
GRFConfig * _grfconfig_static
First item in list of static GRF set up.
Definition: newgrf_config.cpp:166
GRFConfig::param_info
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
Definition: newgrf_config.h:171
CalcGRFMD5Sum
static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
Calculate the MD5 sum for a GRF, and store it in the config.
Definition: newgrf_config.cpp:285
GRFFileScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition: newgrf_config.cpp:557
VideoDriver::GameLoopPause
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
Definition: video_driver.cpp:66
string_func.h
GRFParameterInfo::Finalize
void Finalize()
Finalize Action 14 info after file scan is finished.
Definition: newgrf_config.cpp:228
GRFParameterInfo::max_value
uint32_t max_value
The maximal value of this parameter.
Definition: newgrf_config.h:133
UpdateNewGRFConfigPalette
void UpdateNewGRFConfigPalette(int32_t)
Update the palettes of the graphics from the config file.
Definition: newgrf_config.cpp:243
WC_GAME_OPTIONS
@ WC_GAME_OPTIONS
Game options window; Window numbers:
Definition: window_type.h:619
GRFConfig::flags
uint8_t flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:164
GRFP_GRF_WINDOWS
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
Definition: newgrf_config.h:72
strings_func.h
FioCheckFileExists
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:126
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition: newgrf_config.h:157
GRFConfig::param
std::array< uint32_t, 0x80 > param
GRF parameters.
Definition: newgrf_config.h:167
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
Definition: newgrf_config.cpp:426
GRFBuildParamList
std::string GRFBuildParamList(const GRFConfig *c)
Build a string containing space separated parameter values, and terminate.
Definition: newgrf_config.cpp:729
GRFConfig::GetDescription
const char * GetDescription() const
Get the grf info.
Definition: newgrf_config.cpp:108
video_driver.hpp
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3217
GOID_NEWGRF_RESCANNED
@ GOID_NEWGRF_RESCANNED
NewGRFs were just rescanned.
Definition: window_type.h:725
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:174
newgrf.h
FGCM_NEWEST
@ FGCM_NEWEST
Find newest Grf.
Definition: newgrf_config.h:194
NewGRFScanCallback::OnNewGRFsScanned
virtual void OnNewGRFsScanned()=0
Called whenever the NewGRF scan completed.
progress.h
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
GRFConfig::url
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
Definition: newgrf_config.h:159
GUISettings::last_newgrf_count
uint32_t last_newgrf_count
the numbers of NewGRFs we found during the last scan
Definition: settings_type.h:192
GRFParameterInfo::complete_labels
bool complete_labels
True if all values have a label.
Definition: newgrf_config.h:139
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:164
window_func.h
NewGRFScanCallback
Callback for NewGRF scanning.
Definition: newgrf_config.h:206
_grf_cont_v2_sig
const byte _grf_cont_v2_sig[8]
Signature of a container version 2 GRF.
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1552
GRFIdentifier::grfid
uint32_t grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:84
GRFConfig::num_params
uint8_t num_params
Number of used parameters.
Definition: newgrf_config.h:168
GRFConfig::FinalizeParameterInfo
void FinalizeParameterInfo()
Finalize Action 14 info after file scan is finished.
Definition: newgrf_config.cpp:155
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:37
RemoveDuplicatesFromGRFConfigList
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
Removes duplicates from lists of GRFConfigs.
Definition: newgrf_config.cpp:404
NetworkAfterNewGRFScan
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig's of the servers in the game list as we did a rescan and might have found new N...
Definition: network_gamelist.cpp:118
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
PTYPE_UINT_ENUM
@ PTYPE_UINT_ENUM
The parameter allows a range of numbers, each of which can have a special name.
Definition: newgrf_config.h:121
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition: newgrf_config.cpp:451
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:192
GRFParameterInfo::GRFParameterInfo
GRFParameterInfo(uint nr)
Create a new empty GRFParameterInfo object.
Definition: newgrf_config.cpp:182
GLC_ALL_GOOD
@ GLC_ALL_GOOD
All GRF needed by game are present.
Definition: newgrf_config.h:53
SetModalProgress
void SetModalProgress(bool state)
Set the modal progress state.
Definition: progress.cpp:22
PAL_WINDOWS
@ PAL_WINDOWS
Use the Windows palette.
Definition: gfx_type.h:301
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
GRFFileScanner::next_update
std::chrono::steady_clock::time_point next_update
The next moment we do update the screen.
Definition: newgrf_config.cpp:529
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:690
thread.h
FillGRFDetails
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
Find the GRFID of a given grf, and calculate its md5sum.
Definition: newgrf_config.cpp:324
GLC_NOT_FOUND
@ GLC_NOT_FOUND
At least one GRF couldn't be found (higher priority than GLC_COMPATIBLE)
Definition: newgrf_config.h:55
GRFListCompatibility
GRFListCompatibility
Status of post-gameload GRF compatibility check.
Definition: newgrf_config.h:52
network_func.h
AppendToGRFConfigList
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
Definition: newgrf_config.cpp:440
StrCompareIgnoreCase
int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition: string.cpp:353
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:636
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:148
debug.h
_grfconfig_newgame
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Definition: newgrf_config.cpp:165
GRFConfig::GetName
const char * GetName() const
Get the name of this grf.
Definition: newgrf_config.cpp:98
GRFFileScanner::num_scanned
uint num_scanned
The number of GRFs we have scanned.
Definition: newgrf_config.cpp:530
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103