Data Structures | Functions | Variables

# newgrf_config.cpp File Reference

Finding NewGRFs and configuring them. More...

`#include "stdafx.h"`
`#include "debug.h"`
`#include "3rdparty/md5/md5.h"`
`#include "newgrf.h"`
`#include "network/network_func.h"`
`#include "gfx_func.h"`
`#include "newgrf_text.h"`
`#include "window_func.h"`
`#include "progress.h"`
`#include "video/video_driver.hpp"`
`#include "strings_func.h"`
`#include "textfile_gui.h"`
`#include "fileio_func.h"`
`#include "fios.h"`

Go to the source code of this file.

## Data Structures
class | GRFFileScanner
| Helper for scanning for files with GRF as extension. More...
struct | UnknownGRF
| Structure for UnknownGRFs; this is a lightweight variant of GRFConfig. More...
## Functions
bool | UpdateNewGRFConfigPalette (int32 p1)
| Update the palettes of the graphics from the config file.
size_t | GRFGetSizeOfDataSection (FILE *f)
| Get the data section size of a GRF.
static bool | CalcGRFMD5Sum (GRFConfig *config, Subdirectory subdir)
| Calculate the MD5 sum for a GRF, and store it in the config.
bool | FillGRFDetails (GRFConfig *config, bool is_static, Subdirectory subdir)
| Find the GRFID of a given grf, and calculate its md5sum.
void | ClearGRFConfigList (GRFConfig **config)
| Clear a GRF Config list, freeing all nodes.
GRFConfig ** | CopyGRFConfigList (GRFConfig **dst, const GRFConfig *src, bool init_only)
| Copy a GRF Config list.
static void | RemoveDuplicatesFromGRFConfigList (GRFConfig *list)
| Removes duplicates from lists of GRFConfigs.
void | AppendStaticGRFConfigs (GRFConfig **dst)
| Appends the static GRFs to a list of GRFs.
void | AppendToGRFConfigList (GRFConfig **dst, GRFConfig *el)
| Appends an element to a list of GRFs.
void | ResetGRFConfig (bool defaults)
| Reset the current GRF Config to either blank or newgame settings.
GRFListCompatibility | IsGoodGRFConfigList (GRFConfig *grfconfig)
| Check if all GRFs in the GRF config from a savegame can be loaded.
static int CDECL | GRFSorter (GRFConfig *const *p1, GRFConfig *const *p2)
| Simple sorter for GRFS.
void | DoScanNewGRFFiles (void *callback)
| Really perform the scan for all NewGRFs.
void | ScanNewGRFFiles (NewGRFScanCallback *callback)
| Scan for all NewGRFs.
const GRFConfig * | FindGRFConfig (uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
| Find a NewGRF in the scanned list.
GRFTextWrapper * | FindUnknownGRFName (uint32 grfid, uint8 *md5sum, bool create)
| Finds the name of a NewGRF in the list of names for unknown GRFs.
GRFConfig * | GetGRFConfig (uint32 grfid, uint32 mask)
| Retrieve a NewGRF from the current config by its grfid.
char * | GRFBuildParamList (char *dst, const GRFConfig *c, const char *last)
| Build a string containing space separated parameter values, and terminate.
## Variables
GRFConfig * | _all_grfs
| First item in list of all scanned NewGRFs.
GRFConfig * | _grfconfig
| First item in list of current GRF set up.
GRFConfig * | _grfconfig_newgame
| First item in list of default GRF set up.
GRFConfig * | _grfconfig_static
| First item in list of static GRF set up.
static const uint32 | OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400)
| Base GRF ID for OpenTTD's base graphics GRFs.

## Detailed Description

Finding NewGRFs and configuring them.

Definition in file newgrf_config.cpp.

## Function Documentation

void AppendStaticGRFConfigs | ( | GRFConfig ** | dst | ) |

Appends the static GRFs to a list of GRFs.

Parameters:

  | dst | the head of the list to add to

Definition at line 461 of file newgrf_config.cpp.

References CopyGRFConfigList(), GRFConfig::next, and RemoveDuplicatesFromGRFConfigList().

Referenced by ResetGRFConfig().

void AppendToGRFConfigList | ( | GRFConfig ** | dst,
|  | GRFConfig * | el |
| ) |  |  |

Appends an element to a list of GRFs.

Parameters:

  | dst | the head of the list to add to
  | el | the new tail to be

Definition at line 475 of file newgrf_config.cpp.

References GRFConfig::next, and RemoveDuplicatesFromGRFConfigList().

static bool CalcGRFMD5Sum | ( | GRFConfig * | config,
|  | Subdirectory | subdir |
| ) |  |  | `[static]`

Calculate the MD5 sum for a GRF, and store it in the config.

Parameters:

  | config | GRF to compute.
  | subdir | The subdirectory to look in.

Returns:
  MD5 sum was successfully computed

Definition at line 325 of file newgrf_config.cpp.

References GRFConfig::filename, FioFCloseFile(), FioFOpenFile(), GRFGetSizeOfDataSection(), GRFConfig::ident, GRFIdentifier::md5sum, and min().

Referenced by FillGRFDetails().

void ClearGRFConfigList | ( | GRFConfig ** | config | ) |

Clear a GRF Config list, freeing all nodes.

Parameters:

  | config | Start of the list.

Postcondition:
  config is set to `NULL`.

Definition at line 391 of file newgrf_config.cpp.

References GRFConfig::next.

Referenced by LoadCheckData::Clear(), CopyGRFConfigList(), DoLoad(), DoScanNewGRFFiles(), NetworkGameListHandleDelayedInsert(), NetworkGameListRemoveItem(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), and SaveOrLoad().

GRFConfig** CopyGRFConfigList | ( | GRFConfig ** | dst,
|  | const GRFConfig * | src,
|  | bool | init_only |
| ) |  |  |

Copy a GRF Config list.

Parameters:

  | dst | pointer to destination list
  | src | pointer to source list values
  | init_only | the copied GRF will be processed up to GLS_INIT

Returns:
  pointer to the last value added to the destination list

Definition at line 409 of file newgrf_config.cpp.

References ClearGRFConfigList(), ClrBit(), GRFConfig::flags, GCF_INIT_ONLY, GRFConfig::next, and SetBit().

Referenced by AppendStaticGRFConfigs(), NewGRFConfirmationCallback(), and ResetGRFConfig().

void DoScanNewGRFFiles | ( | void * | callback | ) |

Really perform the scan for all NewGRFs.

Parameters:

  | callback | The callback to call after the scanning is complete.

Definition at line 673 of file newgrf_config.cpp.

References _modal_progress_paint_mutex, _modal_progress_work_mutex, ThreadMutex::BeginCritical(), ClearGRFConfigList(), DEBUG, DeleteWindowByClass(), GRFFileScanner::DoScan(), TarScanner::DoScan(), ThreadMutex::EndCritical(), free(), GOID_NEWGRF_RESCANNED, GRFSorter(), InvalidateWindowClassesData(), InvalidateWindowData(), MarkWholeScreenDirty(), NetworkAfterNewGRFScan(), TarScanner::NEWGRF, GRFConfig::next, QSortT(), SetModalProgress(), WC_GAME_OPTIONS, WC_MODAL_PROGRESS, WC_SAVELOAD, and WN_GAME_OPTIONS_NEWGRF_STATE.

Referenced by ScanNewGRFFiles().

bool FillGRFDetails | ( | GRFConfig * | config,
|  | bool | is_static,
|  | Subdirectory | subdir |
| ) |  |  |

Find the GRFID of a given grf, and calculate its md5sum.

Parameters:

  | config | grf to fill.
  | is_static | grf is static.
  | subdir | the subdirectory to search in.

Returns:
  Operation was successfully completed.

Definition at line 360 of file newgrf_config.cpp.

References CalcGRFMD5Sum(), CONFIG_SLOT, GRFConfig::filename, FioCheckFileExists(), GRFConfig::flags, GCF_UNSAFE, GRFIdentifier::grfid, HasBit(), GRFConfig::ident, GRFConfig::IsOpenTTDBaseGRF(), LoadNewGRFFile(), GRFConfig::SetSuitablePalette(), and GRFConfig::status.

Referenced by GRFFileScanner::AddFile(), GRFLoadConfig(), and LoadSpriteTables().

const GRFConfig* FindGRFConfig | ( | uint32 | grfid,
|  | FindGRFConfigMode | mode,
|  | const uint8 * | md5sum,
|  | uint32 | desired_version |
| ) |  |  |

Find a NewGRF in the scanned list.

Parameters:

  | grfid | GRFID to look for,
  | mode | Restrictions for matching grfs
  | md5sum | Expected MD5 sum
  | desired_version | Requested version

Returns:
  The matching grf, if it exists in _all_grfs, else `NULL`.

Definition at line 756 of file newgrf_config.cpp.

References FGCM_ANY, FGCM_COMPATIBLE, FGCM_EXACT, FGCM_NEWEST, GCF_INVALID, HasBit(), GRFConfig::next, and GRFConfig::version.

Referenced by GamelogPrint(), ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(), HasGRFConfig(), IsGoodGRFConfigList(), NetworkAfterNewGRFScan(), NewGRFWindow::OnInvalidateData(), PrintGrfInfo(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), and ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS().

GRFTextWrapper* FindUnknownGRFName | ( | uint32 | grfid,
|  | uint8 * | md5sum,
|  | bool | create |
| ) |  |  |

Finds the name of a NewGRF in the list of names for unknown GRFs.

An unknown GRF is a GRF where the .grf is not found during scanning.

The names are resolved via UDP calls to servers that should know the name, though the replies may not come. This leaves "<Unknown>" as name, though that shouldn't matter _very_ much as they need GRF crawler or so to look up the GRF anyway and that works better with the GRF ID.

Parameters:

  | grfid | the GRF ID part of the 'unique' GRF identifier
  | md5sum | the MD5 checksum part of the 'unique' GRF identifier
  | create | whether to create a new GRFConfig if the GRFConfig did not exist in the fake list of GRFConfigs.

Returns:
  The GRFTextWrapper of the name of the GRFConfig with the given GRF ID and MD5 checksum or NULL when it does not exist and create is false. This value must NEVER be freed by the caller.

Definition at line 801 of file newgrf_config.cpp.

References AddGRFTextToList(), GRFIdentifier::grfid, GRFIdentifier::md5sum, UnknownGRF::name, UnknownGRF::next, GRFTextWrapper::text, and UNKNOWN_GRF_NAME_PLACEHOLDER.

Referenced by ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(), NetworkAfterNewGRFScan(), and ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS().

GRFConfig* GetGRFConfig | ( | uint32 | grfid,
|  | uint32 | mask |
| ) |  |  |

Retrieve a NewGRF from the current config by its grfid.

Parameters:

  | grfid | grf to look for.
  | mask | GRFID mask to allow for partial matching.

Returns:
  The grf config, if it exists, else `NULL`.

Definition at line 836 of file newgrf_config.cpp.

References GRFIdentifier::grfid, GRFConfig::ident, and GRFConfig::next.

Referenced by ErrorUnknownCallbackResult(), ParamSet(), ShowNewGrfVehicleError(), TranslateGRFStrings(), and VehicleLengthChanged().

size_t GRFGetSizeOfDataSection | ( | FILE * | f | ) |

Get the data section size of a GRF.

Parameters:

  | f | GRF.

Returns:
  Size of the data section or SIZE_MAX if the file has no separate data section.

Definition at line 302 of file newgrf_config.cpp.

References _grf_cont_v2_sig, and MemCmpT().

Referenced by CalcGRFMD5Sum(), and GraphicsSet::CheckMD5().

static int CDECL GRFSorter | ( | GRFConfig *const * | p1,
|  | GRFConfig *const * | p2 |
| ) |  |  | `[static]`

Simple sorter for GRFS.

Parameters:

  | p1 | the first GRFConfig *
  | p2 | the second GRFConfig *

Returns:
  the same strcmp would return for the name of the NewGRF.

Definition at line 661 of file newgrf_config.cpp.

References GRFConfig::GetName().

Referenced by DoScanNewGRFFiles().

GRFListCompatibility IsGoodGRFConfigList | ( | GRFConfig * | grfconfig | ) |

Check if all GRFs in the GRF config from a savegame can be loaded.

Parameters:

  | grfconfig | GrfConfig to check

Returns:
  will return any of the following 3 values:
  - GLC_ALL_GOOD: No problems occurred, all GRF files were found and loaded
  - GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a compatible GRF with the same grfid was found and used instead
  - GLC_NOT_FOUND: For one or more GRF's no match was found at all

Definition at line 504 of file newgrf_config.cpp.

References BSWAP32(), DEBUG, FGCM_COMPATIBLE, FGCM_EXACT, GRFConfig::filename, FindGRFConfig(), GRFConfig::flags, free(), GCF_COMPATIBLE, GCF_COPY, GCF_INVALID, GLC_NOT_FOUND, GRFIdentifier::grfid, GRFConfig::has_param_defaults, HasBit(), GRFConfig::ident, lastof, SmallVector< T, S >::Length(), GRFIdentifier::md5sum, md5sumToString(), GRFConfig::min_loadable_version, GRFConfig::name, GRFConfig::next, GRFConfig::num_valid_params, GRFConfig::param_info, SetBit(), and GRFConfig::version.

Referenced by AfterLoadGame(), and DoLoad().

static void RemoveDuplicatesFromGRFConfigList | ( | GRFConfig * | list | ) | `[static]`

Removes duplicates from lists of GRFConfigs.

These duplicates are introduced when the _grfconfig_static GRFs are appended to the _grfconfig on a newgame or savegame. As the parameters of the static GRFs could be different that the parameters of the ones used non-statically. This can result in desyncs in multiplayers, so the duplicate static GRFs have to be removed.

This function _assumes_ that all static GRFs are placed after the non-static GRFs.

Parameters:

  | list | the list to remove the duplicates from

Definition at line 439 of file newgrf_config.cpp.

References GRFIdentifier::grfid, GRFConfig::ident, and GRFConfig::next.

Referenced by AppendStaticGRFConfigs(), and AppendToGRFConfigList().

void ResetGRFConfig | ( | bool | defaults | ) |

Reset the current GRF Config to either blank or newgame settings.

Definition at line 486 of file newgrf_config.cpp.

References AppendStaticGRFConfigs(), and CopyGRFConfigList().

Referenced by LoadIntroGame(), and AfterNewGRFScan::OnNewGRFsScanned().

void ScanNewGRFFiles | ( | NewGRFScanCallback * | callback | ) |

Scan for all NewGRFs.

Parameters:

  | callback | The callback to call after the scanning is complete.

Definition at line 730 of file newgrf_config.cpp.

References _modal_progress_paint_mutex, _modal_progress_work_mutex, _video_driver, ThreadMutex::BeginCritical(), DoScanNewGRFFiles(), ThreadMutex::EndCritical(), VideoDriver::HasGUI(), MarkWholeScreenDirty(), ThreadObject::New(), SetModalProgress(), and UpdateNewGRFScanStatus().

Referenced by NetworkContentDownloadStatusWindow::~NetworkContentDownloadStatusWindow().

bool UpdateNewGRFConfigPalette | ( | int32 | p1 | ) |

Update the palettes of the graphics from the config file.

Called when changing the default palette in advanced settings.

Parameters:

  | p1 | Unused.

Returns:
  Always true.

Definition at line 289 of file newgrf_config.cpp.

References GRFConfig::next.

Referenced by AfterNewGRFScan::OnNewGRFsScanned().

## Variable Documentation

const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400)`[static]`

Base GRF ID for OpenTTD's base graphics GRFs.

Definition at line 864 of file newgrf_config.cpp.

Referenced by GRFConfig::IsOpenTTDBaseGRF().