OpenTTD Source: src/newgrf_config.cpp Source File

OpenTTD Source 1.11.0-RC1

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 "strings_func.h"
21 #include "textfile_gui.h"
22 #include "thread.h"
23 #include "newgrf_config.h"
24 #include "newgrf_text.h"
25
26 #include "fileio_func.h"
27 #include "fios.h"
28
29 #include "safeguards.h"
30
31
37 GRFConfig::GRFConfig(const char *filename) :
38 num_valid_params(lengthof(param))
39 {
40 if (filename != nullptr) this->filename = stredup(filename);
41 }
42
47 GRFConfig::GRFConfig(const GRFConfig &config) :
48 ZeroedMemoryAllocator(),
49 ident(config.ident),
50 name(config.name),
51 info(config.info),
52 url(config.url),
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)
62 {
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 != nullptr) this->filename = stredup(config.filename);
66 if (config.error != nullptr) this->error = new GRFError(*config.error);
67 for (uint i = 0; i < config.param_info.size(); i++) {
68 if (config.param_info[i] == nullptr) {
69 this->param_info.push_back(nullptr);
70 } else {
71 this->param_info.push_back(new GRFParameterInfo(*config.param_info[i]));
72 }
73 }
74 }
75
77 GRFConfig::~GRFConfig()
78 {
79 /* GCF_COPY as in NOT stredupped/alloced the filename */
80 if (!HasBit(this->flags, GCF_COPY)) {
81 free(this->filename);
82 delete this->error;
83 }
84
85 for (uint i = 0; i < this->param_info.size(); i++) delete this->param_info[i];
86 }
87
92 void GRFConfig::CopyParams(const GRFConfig &src)
93 {
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));
97 }
98
104 const char *GRFConfig::GetName() const
105 {
106 const char *name = GetGRFStringFromGRFText(this->name);
107 return StrEmpty(name) ? this->filename : name;
108 }
109
114 const char *GRFConfig::GetDescription() const
115 {
116 return GetGRFStringFromGRFText(this->info);
117 }
118
123 const char *GRFConfig::GetURL() const
124 {
125 return GetGRFStringFromGRFText(this->url);
126 }
127
129 void GRFConfig::SetParameterDefaults()
130 {
131 this->num_params = 0;
132 MemSetT<uint32>(this->param, 0, lengthof(this->param));
133
134 if (!this->has_param_defaults) return;
135
136 for (uint i = 0; i < this->param_info.size(); i++) {
137 if (this->param_info[i] == nullptr) continue;
138 this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
139 }
140 }
141
147 void GRFConfig::SetSuitablePalette()
148 {
149 PaletteType pal;
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;
154 }
155 SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
156 }
157
161 void GRFConfig::FinalizeParameterInfo()
162 {
163 for (GRFParameterInfo *info : this->param_info) {
164 if (info == nullptr) continue;
165 info->Finalize();
166 }
167 }
168
169 GRFConfig *_all_grfs;
170 GRFConfig *_grfconfig;
171 GRFConfig *_grfconfig_newgame;
172 GRFConfig *_grfconfig_static;
173 uint _missing_extra_graphics = 0;
174
180 GRFError::GRFError(StringID severity, StringID message) :
181 message(message),
182 severity(severity),
183 param_value()
184 {
185 }
186
191 GRFError::GRFError(const GRFError &error) :
192 custom_message(error.custom_message),
193 data(error.data),
194 message(error.message),
195 severity(error.severity)
196 {
197 memcpy(this->param_value, error.param_value, sizeof(this->param_value));
198 }
199
204 GRFParameterInfo::GRFParameterInfo(uint nr) :
205 name(),
206 desc(),
207 type(PTYPE_UINT_ENUM),
208 min_value(0),
209 max_value(UINT32_MAX),
210 def_value(0),
211 param_nr(nr),
212 first_bit(0),
213 num_bit(32),
214 value_names(),
215 complete_labels(false)
216 {}
217
223 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
224 name(info.name),
225 desc(info.desc),
226 type(info.type),
227 min_value(info.min_value),
228 max_value(info.max_value),
229 def_value(info.def_value),
230 param_nr(info.param_nr),
231 first_bit(info.first_bit),
232 num_bit(info.num_bit),
233 value_names(info.value_names),
234 complete_labels(info.complete_labels)
235 {
236 }
237
243 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
244 {
245 /* GB doesn't work correctly with nbits == 32, so handle that case here. */
246 if (this->num_bit == 32) return config->param[this->param_nr];
247 return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
248 }
249
255 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
256 {
257 /* SB doesn't work correctly with nbits == 32, so handle that case here. */
258 if (this->num_bit == 32) {
259 config->param[this->param_nr] = value;
260 } else {
261 SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
262 }
263 config->num_params = std::max<uint>(config->num_params, this->param_nr + 1);
264 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
265 }
266
270 void GRFParameterInfo::Finalize()
271 {
272 this->complete_labels = true;
273 for (uint32 value = this->min_value; value <= this->max_value; value++) {
274 if (!this->value_names.Contains(value)) {
275 this->complete_labels = false;
276 break;
277 }
278 }
279 }
280
287 bool UpdateNewGRFConfigPalette(int32 p1)
288 {
289 for (GRFConfig *c = _grfconfig_newgame; c != nullptr; c = c->next) c->SetSuitablePalette();
290 for (GRFConfig *c = _grfconfig_static; c != nullptr; c = c->next) c->SetSuitablePalette();
291 for (GRFConfig *c = _all_grfs; c != nullptr; c = c->next) c->SetSuitablePalette();
292 return true;
293 }
294
300 size_t GRFGetSizeOfDataSection(FILE *f)
301 {
302 extern const byte _grf_cont_v2_sig[];
303 static const uint header_len = 14;
304
305 byte data[header_len];
306 if (fread(data, 1, header_len, f) == header_len) {
307 if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
308 /* Valid container version 2, get data section size. */
309 size_t offset = ((size_t)data[13] << 24) | ((size_t)data[12] << 16) | ((size_t)data[11] << 8) | (size_t)data[10];
310 if (offset >= 1 * 1024 * 1024 * 1024) {
311 DEBUG(grf, 0, "Unexpectedly large offset for NewGRF");
312 /* Having more than 1 GiB of data is very implausible. Mostly because then
313 * all pools in OpenTTD are flooded already. Or it's just Action C all over.
314 * In any case, the offsets to graphics will likely not work either. */
315 return SIZE_MAX;
316 }
317 return header_len + offset;
318 }
319 }
320
321 return SIZE_MAX;
322 }
323
330 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
331 {
332 FILE *f;
333 Md5 checksum;
334 uint8 buffer[1024];
335 size_t len, size;
336
337 /* open the file */
338 f = FioFOpenFile(config->filename, "rb", subdir, &size);
339 if (f == nullptr) return false;
340
341 long start = ftell(f);
342 size = std::min(size, GRFGetSizeOfDataSection(f));
343
344 if (start < 0 || fseek(f, start, SEEK_SET) < 0) {
345 FioFCloseFile(f);
346 return false;
347 }
348
349 /* calculate md5sum */
350 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
351 size -= len;
352 checksum.Append(buffer, len);
353 }
354 checksum.Finish(config->ident.md5sum);
355
356 FioFCloseFile(f);
357
358 return true;
359 }
360
361
369 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
370 {
371 if (!FioCheckFileExists(config->filename, subdir)) {
372 config->status = GCS_NOT_FOUND;
373 return false;
374 }
375
376 /* Find and load the Action 8 information */
377 LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
378 config->SetSuitablePalette();
379 config->FinalizeParameterInfo();
380
381 /* Skip if the grfid is 0 (not read) or if it is an internal GRF */
382 if (config->ident.grfid == 0 || HasBit(config->flags, GCF_SYSTEM)) return false;
383
384 if (is_static) {
385 /* Perform a 'safety scan' for static GRFs */
386 LoadNewGRFFile(config, CONFIG_SLOT, GLS_SAFETYSCAN, subdir);
387
388 /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
389 if (HasBit(config->flags, GCF_UNSAFE)) return false;
390 }
391
392 return CalcGRFMD5Sum(config, subdir);
393 }
394
395
401 void ClearGRFConfigList(GRFConfig **config)
402 {
403 GRFConfig *c, *next;
404 for (c = *config; c != nullptr; c = next) {
405 next = c->next;
406 delete c;
407 }
408 *config = nullptr;
409 }
410
411
419 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
420 {
421 /* Clear destination as it will be overwritten */
422 ClearGRFConfigList(dst);
423 for (; src != nullptr; src = src->next) {
424 GRFConfig *c = new GRFConfig(*src);
425
426 ClrBit(c->flags, GCF_INIT_ONLY);
427 if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
428
429 *dst = c;
430 dst = &c->next;
431 }
432
433 return dst;
434 }
435
449 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
450 {
451 GRFConfig *prev;
452 GRFConfig *cur;
453
454 if (list == nullptr) return;
455
456 for (prev = list, cur = list->next; cur != nullptr; prev = cur, cur = cur->next) {
457 if (cur->ident.grfid != list->ident.grfid) continue;
458
459 prev->next = cur->next;
460 delete cur;
461 cur = prev; // Just go back one so it continues as normal later on
462 }
463
464 RemoveDuplicatesFromGRFConfigList(list->next);
465 }
466
471 void AppendStaticGRFConfigs(GRFConfig **dst)
472 {
473 GRFConfig **tail = dst;
474 while (*tail != nullptr) tail = &(*tail)->next;
475
476 CopyGRFConfigList(tail, _grfconfig_static, false);
477 RemoveDuplicatesFromGRFConfigList(*dst);
478 }
479
485 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
486 {
487 GRFConfig **tail = dst;
488 while (*tail != nullptr) tail = &(*tail)->next;
489 *tail = el;
490
491 RemoveDuplicatesFromGRFConfigList(*dst);
492 }
493
494
496 void ResetGRFConfig(bool defaults)
497 {
498 CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
499 AppendStaticGRFConfigs(&_grfconfig);
500 }
501
502
514 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
515 {
516 GRFListCompatibility res = GLC_ALL_GOOD;
517
518 for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) {
519 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
520 if (f == nullptr || HasBit(f->flags, GCF_INVALID)) {
521 char buf[256];
522
523 /* If we have not found the exactly matching GRF try to find one with the
524 * same grfid, as it most likely is compatible */
525 f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version);
526 if (f != nullptr) {
527 md5sumToString(buf, lastof(buf), c->ident.md5sum);
528 DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
529 if (!HasBit(c->flags, GCF_COMPATIBLE)) {
530 /* Preserve original_md5sum after it has been assigned */
531 SetBit(c->flags, GCF_COMPATIBLE);
532 memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
533 }
534
535 /* Non-found has precedence over compatibility load */
536 if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
537 goto compatible_grf;
538 }
539
540 /* No compatible grf was found, mark it as disabled */
541 md5sumToString(buf, lastof(buf), c->ident.md5sum);
542 DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
543
544 c->status = GCS_NOT_FOUND;
545 res = GLC_NOT_FOUND;
546 } else {
547 compatible_grf:
548 DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
549 /* The filename could be the filename as in the savegame. As we need
550 * to load the GRF here, we need the correct filename, so overwrite that
551 * in any case and set the name and info when it is not set already.
552 * When the GCF_COPY flag is set, it is certain that the filename is
553 * already a local one, so there is no need to replace it. */
554 if (!HasBit(c->flags, GCF_COPY)) {
555 free(c->filename);
556 c->filename = stredup(f->filename);
557 memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
558 c->name = f->name;
559 c->info = f->name;
560 c->error = nullptr;
561 c->version = f->version;
562 c->min_loadable_version = f->min_loadable_version;
563 c->num_valid_params = f->num_valid_params;
564 c->has_param_defaults = f->has_param_defaults;
565 for (uint i = 0; i < f->param_info.size(); i++) {
566 if (f->param_info[i] == nullptr) {
567 c->param_info.push_back(nullptr);
568 } else {
569 c->param_info.push_back(new GRFParameterInfo(*f->param_info[i]));
570 }
571 }
572 }
573 }
574 }
575
576 return res;
577 }
578
580 class GRFFileScanner : FileScanner {
581 std::chrono::steady_clock::time_point next_update;
582 uint num_scanned;
583
584 public:
585 GRFFileScanner() : num_scanned(0)
586 {
587 this->next_update = std::chrono::steady_clock::now();
588 }
589
590 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
591
593 static uint DoScan()
594 {
595 GRFFileScanner fs;
596 int ret = fs.Scan(".grf", NEWGRF_DIR);
597 /* The number scanned and the number returned may not be the same;
598 * duplicate NewGRFs and base sets are ignored in the return value. */
599 _settings_client.gui.last_newgrf_count = fs.num_scanned;
600 return ret;
601 }
602 };
603
604 bool GRFFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
605 {
606 /* Abort if the user stopped the game during a scan. */
607 if (_exit_game) return false;
608
609 GRFConfig *c = new GRFConfig(filename.c_str() + basepath_length);
610
611 bool added = true;
612 if (FillGRFDetails(c, false)) {
613 if (_all_grfs == nullptr) {
614 _all_grfs = c;
615 } else {
616 /* Insert file into list at a position determined by its
617 * name, so the list is sorted as we go along */
618 GRFConfig **pd, *d;
619 bool stop = false;
620 for (pd = &_all_grfs; (d = *pd) != nullptr; pd = &d->next) {
621 if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
622 /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
623 * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
624 * just after the first with the same name. Avoids doubles in the list. */
625 if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
626 stop = true;
627 } else if (stop) {
628 break;
629 }
630 }
631 if (added) {
632 c->next = d;
633 *pd = c;
634 }
635 }
636 } else {
637 added = false;
638 }
639
640 this->num_scanned++;
641
642 const char *name = nullptr;
643 if (c->name != nullptr) name = GetGRFStringFromGRFText(c->name);
644 if (name == nullptr) name = c->filename;
645 UpdateNewGRFScanStatus(this->num_scanned, name);
646 VideoDriver::GetInstance()->GameLoopPause();
647
648 if (!added) {
649 /* File couldn't be opened, or is either not a NewGRF or is a
650 * 'system' NewGRF or it's already known, so forget about it. */
651 delete c;
652 }
653
654 return added;
655 }
656
663 static bool GRFSorter(GRFConfig * const &c1, GRFConfig * const &c2)
664 {
665 return strnatcmp(c1->GetName(), c2->GetName()) < 0;
666 }
667
672 void DoScanNewGRFFiles(NewGRFScanCallback *callback)
673 {
674 ClearGRFConfigList(&_all_grfs);
675 TarScanner::DoScan(TarScanner::NEWGRF);
676
677 DEBUG(grf, 1, "Scanning for NewGRFs");
678 uint num = GRFFileScanner::DoScan();
679
680 DEBUG(grf, 1, "Scan complete, found %d files", num);
681 if (num != 0 && _all_grfs != nullptr) {
682 /* Sort the linked list using quicksort.
683 * For that we first have to make an array, then sort and
684 * then remake the linked list. */
685 std::vector<GRFConfig *> to_sort;
686
687 uint i = 0;
688 for (GRFConfig *p = _all_grfs; p != nullptr; p = p->next, i++) {
689 to_sort.push_back(p);
690 }
691 /* Number of files is not necessarily right */
692 num = i;
693
694 std::sort(to_sort.begin(), to_sort.end(), GRFSorter);
695
696 for (i = 1; i < num; i++) {
697 to_sort[i - 1]->next = to_sort[i];
698 }
699 to_sort[num - 1]->next = nullptr;
700 _all_grfs = to_sort[0];
701
702 NetworkAfterNewGRFScan();
703 }
704
705 /* Yes... these are the NewGRF windows */
706 InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
707 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
708 if (!_exit_game && callback != nullptr) callback->OnNewGRFsScanned();
709
710 DeleteWindowByClass(WC_MODAL_PROGRESS);
711 SetModalProgress(false);
712 MarkWholeScreenDirty();
713 }
714
719 void ScanNewGRFFiles(NewGRFScanCallback *callback)
720 {
721 /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
722 SetModalProgress(true);
723 /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
724 MarkWholeScreenDirty();
725
726 DoScanNewGRFFiles(callback);
727 }
728
737 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
738 {
739 assert((mode == FGCM_EXACT) != (md5sum == nullptr));
740 const GRFConfig *best = nullptr;
741 for (const GRFConfig *c = _all_grfs; c != nullptr; c = c->next) {
742 /* if md5sum is set, we look for an exact match and continue if not found */
743 if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
744 /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
745 if (md5sum != nullptr || mode == FGCM_ANY) return c;
746 /* Skip incompatible stuff, unless explicitly allowed */
747 if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
748 /* check version compatibility */
749 if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
750 /* remember the newest one as "the best" */
751 if (best == nullptr || c->version > best->version) best = c;
752 }
753
754 return best;
755 }
756
758 struct UnknownGRF : public GRFIdentifier {
759 GRFTextWrapper name;
760
761 UnknownGRF() = default;
762 UnknownGRF(const UnknownGRF &other) = default;
763 UnknownGRF(UnknownGRF &&other) = default;
764 UnknownGRF(uint32 grfid, const uint8 *_md5sum) : GRFIdentifier(grfid, _md5sum), name(new GRFTextList) {}
765 };
766
784 GRFTextWrapper FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
785 {
786 static std::vector<UnknownGRF> unknown_grfs;
787
788 for (const auto &grf : unknown_grfs) {
789 if (grf.grfid == grfid) {
790 if (memcmp(md5sum, grf.md5sum, sizeof(grf.md5sum)) == 0) return grf.name;
791 }
792 }
793
794 if (!create) return nullptr;
795
796 unknown_grfs.emplace_back(grfid, md5sum);
797 UnknownGRF &grf = unknown_grfs.back();
798
799 AddGRFTextToList(grf.name, UNKNOWN_GRF_NAME_PLACEHOLDER);
800
801 return grf.name;
802 }
803
810 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
811 {
812 GRFConfig *c;
813
814 for (c = _grfconfig; c != nullptr; c = c->next) {
815 if ((c->ident.grfid & mask) == (grfid & mask)) return c;
816 }
817
818 return nullptr;
819 }
820
821
823 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
824 {
825 uint i;
826
827 /* Return an empty string if there are no parameters */
828 if (c->num_params == 0) return strecpy(dst, "", last);
829
830 for (i = 0; i < c->num_params; i++) {
831 if (i > 0) dst = strecpy(dst, " ", last);
832 dst += seprintf(dst, last, "%d", c->param[i]);
833 }
834 return dst;
835 }
836
838 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
839
845 const char *GRFConfig::GetTextfile(TextfileType type) const
846 {
847 return ::GetTextfile(type, NEWGRF_DIR, this->filename);
848 }
GRFConfig::version
uint32 version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
Definition: newgrf_config.h:165
GRFConfig::SetSuitablePalette
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
Definition: newgrf_config.cpp:147
WC_SAVELOAD
@ WC_SAVELOAD
Saveload window; Window numbers:
Definition: window_type.h:137
GRFConfig::num_valid_params
uint8 num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
Definition: newgrf_config.h:172
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:3321
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:161
PAL_DOS
@ PAL_DOS
Use the DOS palette.
Definition: gfx_type.h:294
GRFConfig::error
GRFError * error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
Definition: newgrf_config.h:163
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3220
GRFTextList
std::vector< GRFText > GRFTextList
A GRF text with a list of translations.
Definition: newgrf_text.h:31
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:737
GLC_COMPATIBLE
@ GLC_COMPATIBLE
Compatible (eg. the same ID, but different checksum) GRF found in at least one case.
Definition: newgrf_config.h:54
UnknownGRF::name
GRFTextWrapper name
Name of the GRF.
Definition: newgrf_config.cpp:759
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:401
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:569
GRFConfig::num_params
uint8 num_params
Number of used parameters.
Definition: newgrf_config.h:171
DoScanNewGRFFiles
void DoScanNewGRFFiles(NewGRFScanCallback *callback)
Really perform the scan for all NewGRFs.
Definition: newgrf_config.cpp:672
_all_grfs
GRFConfig * _all_grfs
First item in list of all scanned NewGRFs.
Definition: newgrf_config.cpp:169
GRFSorter
static bool GRFSorter(GRFConfig *const &c1, GRFConfig *const &c2)
Simple sorter for GRFS.
Definition: newgrf_config.cpp:663
_grf_cont_v2_sig
const byte _grf_cont_v2_sig[8]
Signature of a container version 2 GRF.
PaletteType
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:293
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:1367
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
GRFParameterInfo::SetValue
void SetValue(struct GRFConfig *config, uint32 value)
Set the value of this user-changeable parameter in the given config.
Definition: newgrf_config.cpp:255
FGCM_COMPATIBLE
@ FGCM_COMPATIBLE
Find best compatible Grf wrt. desired_version.
Definition: newgrf_config.h:194
GRFParameterInfo::param_nr
byte param_nr
GRF parameter to store content in.
Definition: newgrf_config.h:140
fileio_func.h
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 newgrf_default_palette
default palette to use for NewGRFs without action 14 palette information
Definition: settings_type.h:180
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:157
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
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:168
OPENTTD_GRAPHICS_BASE_GRF_ID
static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID
Base GRF ID for OpenTTD's base graphics GRFs.
Definition: newgrf_config.cpp:838
DeleteWindowByClass
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1178
GRFP_USE_BIT
@ GRFP_USE_BIT
The bit used for storing the palette to use.
Definition: newgrf_config.h:60
newgrf_config.h
fios.h
GRFIdentifier::md5sum
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Definition: newgrf_config.h:85
WN_GAME_OPTIONS_NEWGRF_STATE
@ WN_GAME_OPTIONS_NEWGRF_STATE
NewGRF settings.
Definition: window_type.h:17
textfile_gui.h
GRFIdentifier::grfid
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:84
GRFParameterInfo::GetValue
uint32 GetValue(struct GRFConfig *config) const
Get the value of this user-changeable parameter from the given config.
Definition: newgrf_config.cpp:243
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:142
UpdateNewGRFScanStatus
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
Definition: newgrf_gui.cpp:2268
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
UpdateNewGRFConfigPalette
bool UpdateNewGRFConfigPalette(int32 p1)
Update the palettes of the graphics from the config file.
Definition: newgrf_config.cpp:287
GRFBuildParamList
char * GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
Build a string containing space separated parameter values, and terminate.
Definition: newgrf_config.cpp:823
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition: newgrf_config.h:197
WC_MODAL_PROGRESS
@ WC_MODAL_PROGRESS
Progress report of landscape generation; Window numbers:
Definition: window_type.h:456
GRFIdentifier
Basic data to distinguish a GRF.
Definition: newgrf_config.h:83
GRFP_GRF_DOS
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
Definition: newgrf_config.h:71
IsGoodGRFConfigList
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Definition: newgrf_config.cpp:514
UnknownGRF
Structure for UnknownGRFs; this is a lightweight variant of GRFConfig.
Definition: newgrf_config.cpp:758
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:152
GRFError::GRFError
GRFError(StringID severity, StringID message=0)
Construct a new GRFError.
Definition: newgrf_config.cpp:180
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
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CONFIG_SLOT
@ CONFIG_SLOT
Slot used for the GRF scanning and such.
Definition: fios.h:93
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:406
GRFConfig::SetParameterDefaults
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
Definition: newgrf_config.cpp:129
GRFConfig::flags
uint8 flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:167
GRFP_USE_WINDOWS
@ GRFP_USE_WINDOWS
The palette state is set to use the Windows palette.
Definition: newgrf_config.h:67
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
_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:173
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:419
GetGRFStringFromGRFText
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Definition: newgrf_text.cpp:621
GRFFileScanner
Helper for scanning for files with GRF as extension.
Definition: newgrf_config.cpp:580
UNKNOWN_GRF_NAME_PLACEHOLDER
#define UNKNOWN_GRF_NAME_PLACEHOLDER
For communication about GRFs over the network.
Definition: newgrf_config.h:232
GRFConfig::GetTextfile
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
Definition: newgrf_config.cpp:845
FindGRFConfigMode
FindGRFConfigMode
Method to find GRFs using FindGRFConfig.
Definition: newgrf_config.h:192
GRFParameterInfo
Information about one grf parameter.
Definition: newgrf_config.h:131
safeguards.h
GRFGetSizeOfDataSection
size_t GRFGetSizeOfDataSection(FILE *f)
Get the data section size of a GRF.
Definition: newgrf_config.cpp:300
GRFConfig::has_param_defaults
bool has_param_defaults
NOSAVE: did this newgrf specify any defaults for it's parameters.
Definition: newgrf_config.h:175
TarScanner::NEWGRF
@ NEWGRF
Scan for non-base sets.
Definition: fileio_func.h:88
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
ScanNewGRFFiles
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
Definition: newgrf_config.cpp:719
newgrf_text.h
GRFConfig::~GRFConfig
~GRFConfig()
Cleanup a GRFConfig object.
Definition: newgrf_config.cpp:77
LoadNewGRFFile
void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage, Subdirectory subdir)
Load a particular NewGRF.
Definition: newgrf.cpp:9347
GRFConfig::GetURL
const char * GetURL() const
Get the grf url.
Definition: newgrf_config.cpp:123
GRFP_GRF_MASK
@ GRFP_GRF_MASK
Bitmask to get only the NewGRF supplied information.
Definition: newgrf_config.h:74
stdafx.h
GRFConfig::CopyParams
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
Definition: newgrf_config.cpp:92
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:380
GRFFileScanner::DoScan
static uint DoScan()
Do the scan for GRFs.
Definition: newgrf_config.cpp:593
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:187
_grfconfig_static
GRFConfig * _grfconfig_static
First item in list of static GRF set up.
Definition: newgrf_config.cpp:172
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:330
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:604
VideoDriver::GameLoopPause
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
Definition: video_driver.cpp:63
GRFError
Information about why GRF had problems during initialisation.
Definition: newgrf_config.h:112
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
GRFParameterInfo::Finalize
void Finalize()
Finalize Action 14 info after file scan is finished.
Definition: newgrf_config.cpp:270
GRFParameterInfo::max_value
uint32 max_value
The maximal value of this parameter.
Definition: newgrf_config.h:138
WC_GAME_OPTIONS
@ WC_GAME_OPTIONS
Game options window; Window numbers:
Definition: window_type.h:606
GRFP_GRF_WINDOWS
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
Definition: newgrf_config.h:72
strings_func.h
GetTextfile
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:385
FioCheckFileExists
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:266
GRFParameterInfo::value_names
SmallMap< uint32, GRFTextList > value_names
Names for each value.
Definition: newgrf_config.h:143
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition: newgrf_config.h:160
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
Definition: newgrf_config.cpp:471
GRFConfig::min_loadable_version
uint32 min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
Definition: newgrf_config.h:166
GRFConfig::GetDescription
const char * GetDescription() const
Get the grf info.
Definition: newgrf_config.cpp:114
GRFConfig::original_md5sum
uint8 original_md5sum[16]
MD5 checksum of original file if only a 'compatible' file was loaded.
Definition: newgrf_config.h:158
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:3339
GOID_NEWGRF_RESCANNED
@ GOID_NEWGRF_RESCANNED
NewGRFs were just rescanned.
Definition: window_type.h:706
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:177
newgrf.h
FGCM_NEWEST
@ FGCM_NEWEST
Find newest Grf.
Definition: newgrf_config.h:195
NewGRFScanCallback::OnNewGRFsScanned
virtual void OnNewGRFsScanned()=0
Called whenever the NewGRF scan completed.
progress.h
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
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:162
GRFConfig::GRFConfig
GRFConfig(const char *filename=nullptr)
Create a new GRFConfig.
Definition: newgrf_config.cpp:37
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
GRFConfig::palette
uint8 palette
GRFPalette, bitset.
Definition: newgrf_config.h:173
GRFParameterInfo::complete_labels
bool complete_labels
True if all values have a label.
Definition: newgrf_config.h:144
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:131
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:170
window_func.h
NewGRFScanCallback
Callback for NewGRF scanning.
Definition: newgrf_config.h:207
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1597
GRFConfig::FinalizeParameterInfo
void FinalizeParameterInfo()
Finalize Action 14 info after file scan is finished.
Definition: newgrf_config.cpp:161
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:59
RemoveDuplicatesFromGRFConfigList
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
Removes duplicates from lists of GRFConfigs.
Definition: newgrf_config.cpp:449
strnatcmp
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:625
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:164
GUISettings::last_newgrf_count
uint32 last_newgrf_count
the numbers of NewGRFs we found during the last scan
Definition: settings_type.h:153
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:125
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
md5sumToString
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:460
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition: newgrf_config.cpp:496
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:193
GRFTextWrapper
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
Definition: newgrf_text.h:33
GRFParameterInfo::GRFParameterInfo
GRFParameterInfo(uint nr)
Create a new empty GRFParameterInfo object.
Definition: newgrf_config.cpp:204
GLC_ALL_GOOD
@ GLC_ALL_GOOD
All GRF needed by game are present.
Definition: newgrf_config.h:53
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:159
FindUnknownGRFName
GRFTextWrapper FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
Finds the name of a NewGRF in the list of names for unknown GRFs.
Definition: newgrf_config.cpp:784
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
SetModalProgress
void SetModalProgress(bool state)
Set the modal progress state.
Definition: progress.cpp:22
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
MemCmpT
static int MemCmpT(const T *ptr1, const T *ptr2, size_t num=1)
Type-safe version of memcmp().
Definition: mem_func.hpp:63
PAL_WINDOWS
@ PAL_WINDOWS
Use the Windows palette.
Definition: gfx_type.h:295
GRFFileScanner::next_update
std::chrono::steady_clock::time_point next_update
The next moment we do update the screen.
Definition: newgrf_config.cpp:581
thread.h
GRFError::param_value
uint32 param_value[2]
Values of GRF parameters to show for message and custom_message.
Definition: newgrf_config.h:120
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:369
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
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:485
GetGRFConfig
GRFConfig * GetGRFConfig(uint32 grfid, uint32 mask)
Retrieve a NewGRF from the current config by its grfid.
Definition: newgrf_config.cpp:810
AddGRFTextToList
static void AddGRFTextToList(GRFTextList &list, byte langid, const std::string &text_to_add)
Add a new text to a GRFText list.
Definition: newgrf_text.cpp:494
GRFConfig::param_info
std::vector< GRFParameterInfo * > param_info
NOSAVE: extra information about the parameters.
Definition: newgrf_config.h:174
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:577
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:288
debug.h
GRFConfig::param
uint32 param[0x80]
GRF parameters.
Definition: newgrf_config.h:170
GRFParameterInfo::min_value
uint32 min_value
The minimal value this parameter can have.
Definition: newgrf_config.h:137
_grfconfig_newgame
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Definition: newgrf_config.cpp:171
GRFConfig::GetName
const char * GetName() const
Get the name of this grf.
Definition: newgrf_config.cpp:104
GRFFileScanner::num_scanned
uint num_scanned
The number of GRFs we have scanned.
Definition: newgrf_config.cpp:582
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79