OpenTTD
newgrf_config.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "3rdparty/md5/md5.h"
15 #include "newgrf.h"
16 #include "network/network_func.h"
17 #include "gfx_func.h"
18 #include "newgrf_text.h"
19 #include "window_func.h"
20 #include "progress.h"
21 #include "video/video_driver.hpp"
22 #include "strings_func.h"
23 #include "textfile_gui.h"
24 #include "thread.h"
25 #include "newgrf_config.h"
26 
27 #include "fileio_func.h"
28 #include "fios.h"
29 
30 #include "safeguards.h"
31 
34  text(nullptr)
35 {
36 }
37 
40 {
41  CleanUpGRFText(this->text);
42 }
43 
49 GRFConfig::GRFConfig(const char *filename) :
50  name(new GRFTextWrapper()),
51  info(new GRFTextWrapper()),
52  url(new GRFTextWrapper()),
53  num_valid_params(lengthof(param))
54 {
55  if (filename != nullptr) this->filename = stredup(filename);
56  this->name->AddRef();
57  this->info->AddRef();
58  this->url->AddRef();
59 }
60 
67  ident(config.ident),
68  name(config.name),
69  info(config.info),
70  url(config.url),
71  version(config.version),
73  flags(config.flags & ~(1 << GCF_COPY)),
74  status(config.status),
75  grf_bugs(config.grf_bugs),
76  num_params(config.num_params),
78  palette(config.palette),
80 {
81  MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
82  MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
83  if (config.filename != nullptr) this->filename = stredup(config.filename);
84  this->name->AddRef();
85  this->info->AddRef();
86  this->url->AddRef();
87  if (config.error != nullptr) this->error = new GRFError(*config.error);
88  for (uint i = 0; i < config.param_info.size(); i++) {
89  if (config.param_info[i] == nullptr) {
90  this->param_info.push_back(nullptr);
91  } else {
92  this->param_info.push_back(new GRFParameterInfo(*config.param_info[i]));
93  }
94  }
95 }
96 
99 {
100  /* GCF_COPY as in NOT stredupped/alloced the filename */
101  if (!HasBit(this->flags, GCF_COPY)) {
102  free(this->filename);
103  delete this->error;
104  }
105  this->name->Release();
106  this->info->Release();
107  this->url->Release();
108 
109  for (uint i = 0; i < this->param_info.size(); i++) delete this->param_info[i];
110 }
111 
117 {
118  this->num_params = src.num_params;
120  MemCpyT<uint32>(this->param, src.param, lengthof(this->param));
121 }
122 
128 const char *GRFConfig::GetName() const
129 {
130  const char *name = GetGRFStringFromGRFText(this->name->text);
131  return StrEmpty(name) ? this->filename : name;
132 }
133 
138 const char *GRFConfig::GetDescription() const
139 {
140  return GetGRFStringFromGRFText(this->info->text);
141 }
142 
147 const char *GRFConfig::GetURL() const
148 {
149  return GetGRFStringFromGRFText(this->url->text);
150 }
151 
154 {
155  this->num_params = 0;
156  MemSetT<uint32>(this->param, 0, lengthof(this->param));
157 
158  if (!this->has_param_defaults) return;
159 
160  for (uint i = 0; i < this->param_info.size(); i++) {
161  if (this->param_info[i] == nullptr) continue;
162  this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
163  }
164 }
165 
172 {
173  PaletteType pal;
174  switch (this->palette & GRFP_GRF_MASK) {
175  case GRFP_GRF_DOS: pal = PAL_DOS; break;
176  case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS; break;
177  default: pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
178  }
180 }
181 
186 {
187  for (GRFParameterInfo *info : this->param_info) {
188  if (info == nullptr) continue;
189  info->Finalize();
190  }
191 }
192 
198 
205  message(message),
206  severity(severity)
207 {
208 }
209 
217  data(error.data),
218  message(error.message),
219  severity(error.severity)
220 {
221  if (error.custom_message != nullptr) this->custom_message = stredup(error.custom_message);
222  if (error.data != nullptr) this->data = stredup(error.data);
223  memcpy(this->param_value, error.param_value, sizeof(this->param_value));
224 }
225 
226 GRFError::~GRFError()
227 {
228  free(this->custom_message);
229  free(this->data);
230 }
231 
237  name(nullptr),
238  desc(nullptr),
239  type(PTYPE_UINT_ENUM),
240  min_value(0),
241  max_value(UINT32_MAX),
242  def_value(0),
243  param_nr(nr),
244  first_bit(0),
245  num_bit(32),
246  complete_labels(false)
247 {}
248 
255  name(DuplicateGRFText(info.name)),
256  desc(DuplicateGRFText(info.desc)),
257  type(info.type),
258  min_value(info.min_value),
259  max_value(info.max_value),
260  def_value(info.def_value),
261  param_nr(info.param_nr),
262  first_bit(info.first_bit),
263  num_bit(info.num_bit),
265 {
266  for (uint i = 0; i < info.value_names.size(); i++) {
267  SmallPair<uint32, GRFText *> *data = info.value_names.data() + i;
268  this->value_names.Insert(data->first, DuplicateGRFText(data->second));
269  }
270 }
271 
274 {
275  CleanUpGRFText(this->name);
276  CleanUpGRFText(this->desc);
277  for (uint i = 0; i < this->value_names.size(); i++) {
278  SmallPair<uint32, GRFText *> *data = this->value_names.data() + i;
279  CleanUpGRFText(data->second);
280  }
281 }
282 
288 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
289 {
290  /* GB doesn't work correctly with nbits == 32, so handle that case here. */
291  if (this->num_bit == 32) return config->param[this->param_nr];
292  return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
293 }
294 
300 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
301 {
302  /* SB doesn't work correctly with nbits == 32, so handle that case here. */
303  if (this->num_bit == 32) {
304  config->param[this->param_nr] = value;
305  } else {
306  SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
307  }
308  config->num_params = max<uint>(config->num_params, this->param_nr + 1);
310 }
311 
316 {
317  this->complete_labels = true;
318  for (uint32 value = this->min_value; value <= this->max_value; value++) {
319  if (!this->value_names.Contains(value)) {
320  this->complete_labels = false;
321  break;
322  }
323  }
324 }
325 
333 {
334  for (GRFConfig *c = _grfconfig_newgame; c != nullptr; c = c->next) c->SetSuitablePalette();
335  for (GRFConfig *c = _grfconfig_static; c != nullptr; c = c->next) c->SetSuitablePalette();
336  for (GRFConfig *c = _all_grfs; c != nullptr; c = c->next) c->SetSuitablePalette();
337  return true;
338 }
339 
345 size_t GRFGetSizeOfDataSection(FILE *f)
346 {
347  extern const byte _grf_cont_v2_sig[];
348  static const uint header_len = 14;
349 
350  byte data[header_len];
351  if (fread(data, 1, header_len, f) == header_len) {
352  if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
353  /* Valid container version 2, get data section size. */
354  size_t offset = ((size_t)data[13] << 24) | ((size_t)data[12] << 16) | ((size_t)data[11] << 8) | (size_t)data[10];
355  if (offset >= 1 * 1024 * 1024 * 1024) {
356  DEBUG(grf, 0, "Unexpectedly large offset for NewGRF");
357  /* Having more than 1 GiB of data is very implausible. Mostly because then
358  * all pools in OpenTTD are flooded already. Or it's just Action C all over.
359  * In any case, the offsets to graphics will likely not work either. */
360  return SIZE_MAX;
361  }
362  return header_len + offset;
363  }
364  }
365 
366  return SIZE_MAX;
367 }
368 
375 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
376 {
377  FILE *f;
378  Md5 checksum;
379  uint8 buffer[1024];
380  size_t len, size;
381 
382  /* open the file */
383  f = FioFOpenFile(config->filename, "rb", subdir, &size);
384  if (f == nullptr) return false;
385 
386  long start = ftell(f);
387  size = min(size, GRFGetSizeOfDataSection(f));
388 
389  if (start < 0 || fseek(f, start, SEEK_SET) < 0) {
390  FioFCloseFile(f);
391  return false;
392  }
393 
394  /* calculate md5sum */
395  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
396  size -= len;
397  checksum.Append(buffer, len);
398  }
399  checksum.Finish(config->ident.md5sum);
400 
401  FioFCloseFile(f);
402 
403  return true;
404 }
405 
406 
414 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
415 {
416  if (!FioCheckFileExists(config->filename, subdir)) {
417  config->status = GCS_NOT_FOUND;
418  return false;
419  }
420 
421  /* Find and load the Action 8 information */
422  LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
423  config->SetSuitablePalette();
424  config->FinalizeParameterInfo();
425 
426  /* Skip if the grfid is 0 (not read) or if it is an internal GRF */
427  if (config->ident.grfid == 0 || HasBit(config->flags, GCF_SYSTEM)) return false;
428 
429  if (is_static) {
430  /* Perform a 'safety scan' for static GRFs */
431  LoadNewGRFFile(config, CONFIG_SLOT, GLS_SAFETYSCAN, subdir);
432 
433  /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
434  if (HasBit(config->flags, GCF_UNSAFE)) return false;
435  }
436 
437  return CalcGRFMD5Sum(config, subdir);
438 }
439 
440 
447 {
448  GRFConfig *c, *next;
449  for (c = *config; c != nullptr; c = next) {
450  next = c->next;
451  delete c;
452  }
453  *config = nullptr;
454 }
455 
456 
464 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
465 {
466  /* Clear destination as it will be overwritten */
467  ClearGRFConfigList(dst);
468  for (; src != nullptr; src = src->next) {
469  GRFConfig *c = new GRFConfig(*src);
470 
472  if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
473 
474  *dst = c;
475  dst = &c->next;
476  }
477 
478  return dst;
479 }
480 
495 {
496  GRFConfig *prev;
497  GRFConfig *cur;
498 
499  if (list == nullptr) return;
500 
501  for (prev = list, cur = list->next; cur != nullptr; prev = cur, cur = cur->next) {
502  if (cur->ident.grfid != list->ident.grfid) continue;
503 
504  prev->next = cur->next;
505  delete cur;
506  cur = prev; // Just go back one so it continues as normal later on
507  }
508 
510 }
511 
517 {
518  GRFConfig **tail = dst;
519  while (*tail != nullptr) tail = &(*tail)->next;
520 
521  CopyGRFConfigList(tail, _grfconfig_static, false);
523 }
524 
531 {
532  GRFConfig **tail = dst;
533  while (*tail != nullptr) tail = &(*tail)->next;
534  *tail = el;
535 
537 }
538 
539 
541 void ResetGRFConfig(bool defaults)
542 {
543  CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
544  AppendStaticGRFConfigs(&_grfconfig);
545 }
546 
547 
560 {
562 
563  for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) {
564  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
565  if (f == nullptr || HasBit(f->flags, GCF_INVALID)) {
566  char buf[256];
567 
568  /* If we have not found the exactly matching GRF try to find one with the
569  * same grfid, as it most likely is compatible */
570  f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version);
571  if (f != nullptr) {
572  md5sumToString(buf, lastof(buf), c->ident.md5sum);
573  DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
574  if (!HasBit(c->flags, GCF_COMPATIBLE)) {
575  /* Preserve original_md5sum after it has been assigned */
576  SetBit(c->flags, GCF_COMPATIBLE);
577  memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
578  }
579 
580  /* Non-found has precedence over compatibility load */
581  if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
582  goto compatible_grf;
583  }
584 
585  /* No compatible grf was found, mark it as disabled */
586  md5sumToString(buf, lastof(buf), c->ident.md5sum);
587  DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
588 
589  c->status = GCS_NOT_FOUND;
590  res = GLC_NOT_FOUND;
591  } else {
592 compatible_grf:
593  DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
594  /* The filename could be the filename as in the savegame. As we need
595  * to load the GRF here, we need the correct filename, so overwrite that
596  * in any case and set the name and info when it is not set already.
597  * When the GCF_COPY flag is set, it is certain that the filename is
598  * already a local one, so there is no need to replace it. */
599  if (!HasBit(c->flags, GCF_COPY)) {
600  free(c->filename);
601  c->filename = stredup(f->filename);
602  memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
603  c->name->Release();
604  c->name = f->name;
605  c->name->AddRef();
606  c->info->Release();
607  c->info = f->name;
608  c->info->AddRef();
609  c->error = nullptr;
610  c->version = f->version;
611  c->min_loadable_version = f->min_loadable_version;
612  c->num_valid_params = f->num_valid_params;
613  c->has_param_defaults = f->has_param_defaults;
614  for (uint i = 0; i < f->param_info.size(); i++) {
615  if (f->param_info[i] == nullptr) {
616  c->param_info.push_back(nullptr);
617  } else {
618  c->param_info.push_back(new GRFParameterInfo(*f->param_info[i]));
619  }
620  }
621  }
622  }
623  }
624 
625  return res;
626 }
627 
630  uint next_update;
631  uint num_scanned;
632 
633 public:
634  GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
635  {
636  }
637 
638  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
639 
641  static uint DoScan()
642  {
643  GRFFileScanner fs;
644  int ret = fs.Scan(".grf", NEWGRF_DIR);
645  /* The number scanned and the number returned may not be the same;
646  * duplicate NewGRFs and base sets are ignored in the return value. */
648  return ret;
649  }
650 };
651 
652 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
653 {
654  GRFConfig *c = new GRFConfig(filename + basepath_length);
655 
656  bool added = true;
657  if (FillGRFDetails(c, false)) {
658  if (_all_grfs == nullptr) {
659  _all_grfs = c;
660  } else {
661  /* Insert file into list at a position determined by its
662  * name, so the list is sorted as we go along */
663  GRFConfig **pd, *d;
664  bool stop = false;
665  for (pd = &_all_grfs; (d = *pd) != nullptr; pd = &d->next) {
666  if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
667  /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
668  * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
669  * just after the first with the same name. Avoids doubles in the list. */
670  if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
671  stop = true;
672  } else if (stop) {
673  break;
674  }
675  }
676  if (added) {
677  c->next = d;
678  *pd = c;
679  }
680  }
681  } else {
682  added = false;
683  }
684 
685  this->num_scanned++;
686  if (this->next_update <= _realtime_tick) {
689 
690  const char *name = nullptr;
691  if (c->name != nullptr) name = GetGRFStringFromGRFText(c->name->text);
692  if (name == nullptr) name = c->filename;
693  UpdateNewGRFScanStatus(this->num_scanned, name);
694 
697 
698  this->next_update = _realtime_tick + MODAL_PROGRESS_REDRAW_TIMEOUT;
699  }
700 
701  if (!added) {
702  /* File couldn't be opened, or is either not a NewGRF or is a
703  * 'system' NewGRF or it's already known, so forget about it. */
704  delete c;
705  }
706 
707  return added;
708 }
709 
716 static bool GRFSorter(GRFConfig * const &c1, GRFConfig * const &c2)
717 {
718  return strnatcmp(c1->GetName(), c2->GetName()) < 0;
719 }
720 
726 {
727  std::unique_lock<std::mutex> lock_work(_modal_progress_work_mutex);
728 
729  ClearGRFConfigList(&_all_grfs);
731 
732  DEBUG(grf, 1, "Scanning for NewGRFs");
733  uint num = GRFFileScanner::DoScan();
734 
735  DEBUG(grf, 1, "Scan complete, found %d files", num);
736  if (num != 0 && _all_grfs != nullptr) {
737  /* Sort the linked list using quicksort.
738  * For that we first have to make an array, then sort and
739  * then remake the linked list. */
740  std::vector<GRFConfig *> to_sort;
741 
742  uint i = 0;
743  for (GRFConfig *p = _all_grfs; p != nullptr; p = p->next, i++) {
744  to_sort.push_back(p);
745  }
746  /* Number of files is not necessarily right */
747  num = i;
748 
749  std::sort(to_sort.begin(), to_sort.end(), GRFSorter);
750 
751  for (i = 1; i < num; i++) {
752  to_sort[i - 1]->next = to_sort[i];
753  }
754  to_sort[num - 1]->next = nullptr;
755  _all_grfs = to_sort[0];
756 
758  }
759 
760  lock_work.unlock();
761  std::lock_guard<std::mutex> lock_paint(_modal_progress_paint_mutex);
762 
763  /* Yes... these are the NewGRF windows */
766  if (callback != nullptr) callback->OnNewGRFsScanned();
767 
769  SetModalProgress(false);
771 }
772 
778 {
779  /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
780  SetModalProgress(true);
781  /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
783 
784  if (!UseThreadedModelProgress() || !VideoDriver::GetInstance()->HasGUI() || !StartNewThread(nullptr, "ottd:newgrf-scan", &DoScanNewGRFFiles, (NewGRFScanCallback *)callback)) { // Without the seemingly superfluous cast, strange compiler errors ensue.
787  DoScanNewGRFFiles(callback);
790  } else {
791  UpdateNewGRFScanStatus(0, nullptr);
792  }
793 }
794 
803 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
804 {
805  assert((mode == FGCM_EXACT) != (md5sum == nullptr));
806  const GRFConfig *best = nullptr;
807  for (const GRFConfig *c = _all_grfs; c != nullptr; c = c->next) {
808  /* if md5sum is set, we look for an exact match and continue if not found */
809  if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
810  /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
811  if (md5sum != nullptr || mode == FGCM_ANY) return c;
812  /* Skip incompatible stuff, unless explicitly allowed */
813  if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
814  /* check version compatibility */
815  if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
816  /* remember the newest one as "the best" */
817  if (best == nullptr || c->version > best->version) best = c;
818  }
819 
820  return best;
821 }
822 
824 struct UnknownGRF : public GRFIdentifier {
827 };
828 
846 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
847 {
848  UnknownGRF *grf;
849  static UnknownGRF *unknown_grfs = nullptr;
850 
851  for (grf = unknown_grfs; grf != nullptr; grf = grf->next) {
852  if (grf->grfid == grfid) {
853  if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
854  }
855  }
856 
857  if (!create) return nullptr;
858 
859  grf = CallocT<UnknownGRF>(1);
860  grf->grfid = grfid;
861  grf->next = unknown_grfs;
862  grf->name = new GRFTextWrapper();
863  grf->name->AddRef();
864 
866  memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
867 
868  unknown_grfs = grf;
869  return grf->name;
870 }
871 
878 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
879 {
880  GRFConfig *c;
881 
882  for (c = _grfconfig; c != nullptr; c = c->next) {
883  if ((c->ident.grfid & mask) == (grfid & mask)) return c;
884  }
885 
886  return nullptr;
887 }
888 
889 
891 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
892 {
893  uint i;
894 
895  /* Return an empty string if there are no parameters */
896  if (c->num_params == 0) return strecpy(dst, "", last);
897 
898  for (i = 0; i < c->num_params; i++) {
899  if (i > 0) dst = strecpy(dst, " ", last);
900  dst += seprintf(dst, last, "%d", c->param[i]);
901  }
902  return dst;
903 }
904 
906 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
907 
914 {
915  return ::GetTextfile(type, NEWGRF_DIR, this->filename);
916 }
~GRFTextWrapper()
Cleanup a GRFTextWrapper object.
Functions related to OTTD&#39;s strings.
SmallMap< uint32, struct GRFText * > value_names
Names for each value.
void SetValue(struct GRFConfig *config, uint32 value)
Set the value of this user-changeable parameter in the given config.
Base of all video drivers.
uint8 newgrf_default_palette
default palette to use for NewGRFs without action 14 palette information
uint32 param_value[2]
Values of GRF parameters to show for message and custom_message.
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:50
The parameter allows a range of numbers, each of which can have a special name.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3199
GRFConfig * _grfconfig
First item in list of current GRF set up.
static int MemCmpT(const T *ptr1, const T *ptr2, size_t num=1)
Type-safe version of memcmp().
Definition: mem_func.hpp:65
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
size_t GRFGetSizeOfDataSection(FILE *f)
Get the data section size of a GRF.
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:334
GRF file is processed up to GLS_INIT.
Definition: newgrf_config.h:29
Saveload window; Window numbers:
Definition: window_type.h:139
GRFError(StringID severity, StringID message=0)
Construct a new GRFError.
All GRF needed by game are present.
Definition: newgrf_config.h:54
NewGRFs were just rescanned.
Definition: window_type.h:702
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Compatible (eg. the same ID, but different checksum) GRF found in at least one case.
Definition: newgrf_config.h:55
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
GRFParameterType type
The type of this parameter.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
Progress report of landscape generation; Window numbers:
Definition: window_type.h:458
const byte _grf_cont_v2_sig[8]
Signature of a container version 2 GRF.
Functions related to debugging.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
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:1375
UnknownGRF * next
The next unknown GRF.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
uint32 def_value
Default value of this parameter.
GRFStatus status
NOSAVE: GRFStatus, enum.
struct GRFText * text
The actual text.
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:427
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Use the Windows palette.
Definition: gfx_type.h:291
char * custom_message
Custom message (if present)
uint8 num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
GRFError * error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
Functions for Standard In/Out file operations.
void DoScanNewGRFFiles(NewGRFScanCallback *callback)
Really perform the scan for all NewGRFs.
byte param_nr
GRF parameter to store content in.
Use the DOS palette.
Definition: gfx_type.h:290
uint8 original_md5sum[16]
MD5 checksum of original file if only a &#39;compatible&#39; file was loaded.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
StringID severity
Info / Warning / Error / Fatal.
GRF file was not found in the local cache.
Definition: newgrf_config.h:38
bool UpdateNewGRFConfigPalette(int32 p1)
Update the palettes of the graphics from the config file.
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Slot used for the GRF scanning and such.
Definition: fios.h:95
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
static const uint MODAL_PROGRESS_REDRAW_TIMEOUT
Timeout between redraws.
Definition: progress.h:17
uint32 last_newgrf_count
the numbers of NewGRFs we found during the last scan
Basic data to distinguish a GRF.
Definition: newgrf_config.h:84
struct GRFConfig * next
NOSAVE: Next item in the linked list.
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.
GRFConfig * _grfconfig_static
First item in list of static GRF set up.
Helper for scanning for files with a given name.
Definition: fileio_func.h:72
std::mutex _modal_progress_work_mutex
Rights for the performing work.
Definition: progress.cpp:23
GRFConfig * _all_grfs
First item in list of all scanned NewGRFs.
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
Find the GRFID of a given grf, and calculate its md5sum.
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:3318
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:50
struct GRFText * desc
The description of this parameter.
Header of Action 04 "universal holder" structure and functions.
uint32 GetValue(struct GRFConfig *config) const
Get the value of this user-changeable parameter from the given config.
uint32 grf_bugs
NOSAVE: bugs in this GRF in this run,.
uint8 num_params
Number of used parameters.
GRFTextWrapper * FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
Finds the name of a NewGRF in the list of names for unknown GRFs.
Base of all threads.
static bool UseThreadedModelProgress()
Check if we can use a thread for modal progress.
Definition: progress.h:33
GRF file is an openttd-internal system grf.
Definition: newgrf_config.h:24
Information about GRF, used in the game and (part of it) in savegames.
void AddGRFTextToList(GRFText **list, GRFText *text_to_add)
Add a GRFText to a GRFText list.
Simple pair of data.
GRFConfig ** CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
Copy a GRF Config list.
bool has_param_defaults
NOSAVE: did this newgrf specify any defaults for it&#39;s parameters.
void CleanUpGRFText(GRFText *grftext)
Delete all items of a linked GRFText list.
Functions related to the gfx engine.
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
const char * GetDescription() const
Get the grf info.
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
char * GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
Build a string containing space separated parameter values, and terminate.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:465
byte num_bit
Number of bits to use for this parameter.
Functions related to modal progress.
The palette state is set to use the Windows palette.
Definition: newgrf_config.h:68
Definition of base types and functions in a cross-platform compatible way.
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:28
A number of safeguards to prevent using unsafe methods.
Scan for non-base sets.
Definition: fileio_func.h:101
Information about why GRF had problems during initialisation.
uint32 max_value
The maximal value of this parameter.
~GRFParameterInfo()
Cleanup all parameter info.
void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage, Subdirectory subdir)
Load a particular NewGRF.
Definition: newgrf.cpp:9333
static uint DoScan()
Do the scan for GRFs.
uint8 flags
NOSAVE: GCF_Flags, bitset.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
GRF is unusable with this version of OpenTTD.
Definition: newgrf_config.h:31
GRFTextWrapper()
Create a new GRFTextWrapper.
bool Insert(const T &key, const U &data)
Adds new item to this map.
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
uint next_update
The next (realtime tick) we do update the screen.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:16
StringID message
Default message.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Reference counted wrapper around a GRFText pointer.
void FinalizeParameterInfo()
Finalize Action 14 info after file scan is finished.
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:312
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:87
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1159
Functions to find and configure NewGRFs.
Subdirectory for all NewGRFs.
Definition: fileio_type.h:119
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:610
uint32 version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
The palette state is set to use the DOS palette.
Definition: newgrf_config.h:67
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
GRFTextWrapper * url
NOSAVE: URL belonging to this GRF.
~GRFConfig()
Cleanup a GRFConfig object.
GRFText * DuplicateGRFText(GRFText *orig)
Create a copy of this GRFText list.
The NewGRF says the Windows palette can be used.
Definition: newgrf_config.h:73
Callback for NewGRF scanning.
const char * GetURL() const
Get the grf url.
byte first_bit
First bit to use in the GRF parameter.
const char * GetGRFStringFromGRFText(const GRFText *text)
Get a C-string from a GRFText-list.
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
void Finalize()
Finalize Action 14 info after file scan is finished.
GRFConfig * GetGRFConfig(uint32 grfid, uint32 mask)
Retrieve a NewGRF from the current config by its grfid.
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
GUISettings gui
settings related to the GUI
Information about one grf parameter.
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:580
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Declarations for savegames operations.
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
GRFListCompatibility
Status of post-gameload GRF compatibility check.
Definition: newgrf_config.h:53
uint8 palette
GRFPalette, bitset.
#define UNKNOWN_GRF_NAME_PLACEHOLDER
For communication about GRFs over the network.
bool complete_labels
True if all values have a label.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
At least one GRF couldn&#39;t be found (higher priority than GLC_COMPATIBLE)
Definition: newgrf_config.h:56
GRF file is unsafe for static usage.
Definition: newgrf_config.h:25
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Find newest Grf.
Structure for UnknownGRFs; this is a lightweight variant of GRFConfig.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
char * filename
Filename - either with or without full path.
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:289
FindGRFConfigMode
Method to find GRFs using FindGRFConfig.
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
GRFTextWrapper * name
NOSAVE: GRF name (Action 0x08)
Network functions used by other parts of OpenTTD.
The NewGRF says the DOS palette can be used.
Definition: newgrf_config.h:72
uint32 min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
std::vector< GRFParameterInfo * > param_info
NOSAVE: extra information about the parameters.
uint32 min_value
The minimal value this parameter can have.
void SetModalProgress(bool state)
Set the modal progress state.
Definition: progress.cpp:32
const char * GetName() const
Get the name of this grf.
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:85
char * data
Additional data for message and custom_message.
struct GRFText * name
The name of this parameter.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
uint32 param[0x80]
GRF parameters.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig&#39;s of the servers in the game list as we did a rescan and might have found new N...
std::mutex _modal_progress_paint_mutex
Rights for the painting.
Definition: progress.cpp:25
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches) ...
Definition: newgrf_config.h:27
Bitmask to get only the NewGRF supplied information.
Definition: newgrf_config.h:75
Helper for scanning for files with GRF as extension.
The bit used for storing the palette to use.
Definition: newgrf_config.h:61
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Window functions not directly related to making/drawing windows.
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
Removes duplicates from lists of GRFConfigs.
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF) ...
Definition: newgrf_config.h:86
static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
Calculate the MD5 sum for a GRF, and store it in the config.
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Only find Grfs matching md5sum.
static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID
Base GRF ID for OpenTTD&#39;s base graphics GRFs.
GRFParameterInfo(uint nr)
Create a new empty GRFParameterInfo object.
static bool GRFSorter(GRFConfig *const &c1, GRFConfig *const &c2)
Simple sorter for GRFS.
Game options window; Window numbers:
Definition: window_type.h:608
uint num_scanned
The number of GRFs we have scanned.
GRFTextWrapper * name
Name of the GRF.
Find best compatible Grf wrt. desired_version.
virtual void OnNewGRFsScanned()=0
Called whenever the NewGRF scan completed.
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:3300
GRFConfig(const char *filename=nullptr)
Create a new GRFConfig.
Use first found.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1459
GUI functions related to textfiles.
GRFTextWrapper * info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Base for the NewGRF implementation.