OpenTTD Source  20241108-master-g80f628063a
newgrf_config.h
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 #ifndef NEWGRF_CONFIG_H
11 #define NEWGRF_CONFIG_H
12 
13 #include "strings_type.h"
14 #include "core/alloc_type.hpp"
15 #include "fileio_type.h"
16 #include "textfile_type.h"
17 #include "newgrf_text.h"
18 #include "3rdparty/md5/md5.h"
19 
21 enum GCF_Flags {
30 };
31 
33 enum GRFStatus {
39 };
40 
42 enum GRFBugs {
48 };
49 
55 };
56 
58 enum GRFPalette {
64 
65  GRFP_USE_DOS = 0x0,
67  GRFP_USE_MASK = 0x1,
68 
74 
78 };
79 
80 
82 struct GRFIdentifier {
83  uint32_t grfid;
84  MD5Hash md5sum;
85 
86  GRFIdentifier() = default;
87  GRFIdentifier(const GRFIdentifier &other) = default;
88  GRFIdentifier(GRFIdentifier &&other) = default;
89  GRFIdentifier(uint32_t grfid, const MD5Hash &md5sum) : grfid(grfid), md5sum(md5sum) {}
90 
91  GRFIdentifier& operator =(const GRFIdentifier &other) = default;
92 
99  inline bool HasGrfIdentifier(uint32_t grfid, const MD5Hash *md5sum) const
100  {
101  if (this->grfid != grfid) return false;
102  if (md5sum == nullptr) return true;
103  return *md5sum == this->md5sum;
104  }
105 };
106 
108 struct GRFError {
110 
111  std::string custom_message{};
112  std::string data{};
115  std::array<uint32_t, 2> param_value{};
116 };
117 
123 };
124 
127  GRFParameterInfo(uint nr);
131  uint32_t min_value;
132  uint32_t max_value;
133  uint32_t def_value;
134  uint8_t param_nr;
135  uint8_t first_bit;
136  uint8_t num_bit;
137  std::map<uint32_t, GRFTextList> value_names;
139 
140  uint32_t GetValue(struct GRFConfig *config) const;
141  void SetValue(struct GRFConfig *config, uint32_t value);
142  void Finalize();
143 };
144 
147  GRFConfig(const std::string &filename = std::string{});
148  GRFConfig(const GRFConfig &config);
149 
150  /* Remove the copy assignment, as the default implementation will not do the right thing. */
151  GRFConfig &operator=(GRFConfig &rhs) = delete;
152 
154  MD5Hash original_md5sum;
155  std::string filename;
159  std::optional<GRFError> error;
160 
161  uint32_t version;
163  uint8_t flags;
165  uint32_t grf_bugs;
166  std::array<uint32_t, 0x80> param;
167  uint8_t num_params;
169  uint8_t palette;
170  std::vector<std::optional<GRFParameterInfo>> param_info;
172 
173  struct GRFConfig *next;
174 
175  bool IsCompatible(uint32_t old_version) const;
176  void SetParams(const std::vector<uint32_t> &pars);
177  void CopyParams(const GRFConfig &src);
178 
179  std::optional<std::string> GetTextfile(TextfileType type) const;
180  const char *GetName() const;
181  const char *GetDescription() const;
182  const char *GetURL() const;
183 
184  void SetParameterDefaults();
185  void SetSuitablePalette();
186  void FinalizeParameterInfo();
187 };
188 
196 };
197 
198 extern GRFConfig *_all_grfs;
199 extern GRFConfig *_grfconfig;
202 extern uint _missing_extra_graphics;
203 
207  virtual ~NewGRFScanCallback() = default;
209  virtual void OnNewGRFsScanned() = 0;
210 };
211 
213 
214 void ScanNewGRFFiles(NewGRFScanCallback *callback);
215 const GRFConfig *FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum = nullptr, uint32_t desired_version = 0);
216 GRFConfig *GetGRFConfig(uint32_t grfid, uint32_t mask = 0xFFFFFFFF);
217 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only);
219 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
220 void ClearGRFConfigList(GRFConfig **config);
221 void ResetGRFConfig(bool defaults);
223 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir = NEWGRF_DIR);
224 std::string GRFBuildParamList(const GRFConfig *c);
225 
226 /* In newgrf_gui.cpp */
227 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config);
228 void OpenGRFParameterWindow(bool is_baseset, GRFConfig *c, bool editable);
229 
230 void UpdateNewGRFScanStatus(uint num, const char *name);
231 void UpdateNewGRFConfigPalette(int32_t new_value = 0);
232 
233 #endif /* NEWGRF_CONFIG_H */
Helper types related to the allocation of memory.
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:86
Types for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition: fileio_type.h:124
GRFListCompatibility
Status of post-gameload GRF compatibility check.
Definition: newgrf_config.h:51
@ GLC_COMPATIBLE
Compatible (eg. the same ID, but different checksum) GRF found in at least one case.
Definition: newgrf_config.h:53
@ GLC_ALL_GOOD
All GRF needed by game are present.
Definition: newgrf_config.h:52
@ GLC_NOT_FOUND
At least one GRF couldn't be found (higher priority than GLC_COMPATIBLE)
Definition: newgrf_config.h:54
void UpdateNewGRFConfigPalette(int32_t new_value=0)
Update the palettes of the graphics from the config file.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum=nullptr, uint32_t desired_version=0)
Find a NewGRF in the scanned list.
GRFConfig * _all_grfs
First item in list of all scanned NewGRFs.
GRFPalette
Information that can/has to be stored about a GRF's palette.
Definition: newgrf_config.h:58
@ GRFP_GRF_UNSET
The NewGRF provided no information.
Definition: newgrf_config.h:69
@ GRFP_BLT_UNSET
The NewGRF provided no information or doesn't care about a 32 bpp blitter.
Definition: newgrf_config.h:75
@ GRFP_USE_DOS
The palette state is set to use the DOS palette.
Definition: newgrf_config.h:65
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
Definition: newgrf_config.h:71
@ GRFP_USE_WINDOWS
The palette state is set to use the Windows palette.
Definition: newgrf_config.h:66
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
Definition: newgrf_config.h:70
@ GRFP_GRF_ANY
The NewGRF says any palette can be used.
Definition: newgrf_config.h:72
@ GRFP_BLT_MASK
Bitmask to only get the blitter information.
Definition: newgrf_config.h:77
@ GRFP_BLT_32BPP
The NewGRF prefers a 32 bpp blitter.
Definition: newgrf_config.h:76
@ GRFP_BLT_OFFSET
The offset of the GRFP_BLT data.
Definition: newgrf_config.h:62
@ GRFP_GRF_SIZE
The size of the GRFP_GRF data.
Definition: newgrf_config.h:61
@ GRFP_USE_BIT
The bit used for storing the palette to use.
Definition: newgrf_config.h:59
@ GRFP_USE_MASK
Bitmask to get only the use palette use states.
Definition: newgrf_config.h:67
@ GRFP_BLT_SIZE
The size of the GRFP_BLT data.
Definition: newgrf_config.h:63
@ GRFP_GRF_OFFSET
The offset of the GRFP_GRF data.
Definition: newgrf_config.h:60
@ GRFP_GRF_MASK
Bitmask to get only the NewGRF supplied information.
Definition: newgrf_config.h:73
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
GCF_Flags
GRF config bit flags.
Definition: newgrf_config.h:21
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
Definition: newgrf_config.h:27
@ GCF_INVALID
GRF is unusable with this version of OpenTTD.
Definition: newgrf_config.h:29
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:24
@ GCF_UNSAFE
GRF file is unsafe for static usage.
Definition: newgrf_config.h:23
@ GCF_COMPATIBLE
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches)
Definition: newgrf_config.h:25
@ GCF_RESERVED
GRF file passed GLS_RESERVE stage.
Definition: newgrf_config.h:28
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:26
@ GCF_SYSTEM
GRF file is an openttd-internal system grf.
Definition: newgrf_config.h:22
GRFParameterType
The possible types of a newgrf parameter.
@ PTYPE_UINT_ENUM
The parameter allows a range of numbers, each of which can have a special name.
@ PTYPE_BOOL
The parameter is either 0 or 1.
@ PTYPE_END
Invalid parameter type.
std::string GRFBuildParamList(const GRFConfig *c)
Build a string containing space separated parameter values, and terminate.
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
GRFStatus
Status of GRF.
Definition: newgrf_config.h:33
@ GCS_INITIALISED
GRF file has been initialised.
Definition: newgrf_config.h:37
@ GCS_DISABLED
GRF file is disabled.
Definition: newgrf_config.h:35
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:36
@ GCS_UNKNOWN
The status of this grf file is unknown.
Definition: newgrf_config.h:34
@ GCS_ACTIVATED
GRF file has been activated.
Definition: newgrf_config.h:38
GRFBugs
Encountered GRF bugs.
Definition: newgrf_config.h:42
@ GBUG_VEH_POWERED_WAGON
Powered wagon changed poweredness state when not inside a depot.
Definition: newgrf_config.h:45
@ GBUG_VEH_REFIT
Articulated vehicles carry different cargoes resp. are differently refittable than specified in purch...
Definition: newgrf_config.h:44
@ GBUG_VEH_LENGTH
Length of rail vehicle changes when not inside a depot.
Definition: newgrf_config.h:43
@ GBUG_UNKNOWN_CB_RESULT
A callback returned an unknown/invalid result.
Definition: newgrf_config.h:46
@ GBUG_VEH_CAPACITY
Capacity of vehicle changes when not refitting or arranging.
Definition: newgrf_config.h:47
GRFConfig * _grfconfig
First item in list of current GRF set up.
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
GRFConfig * _grfconfig_static
First item in list of static GRF set up.
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir=NEWGRF_DIR)
Find the GRFID of a given grf, and calculate its md5sum.
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
FindGRFConfigMode
Method to find GRFs using FindGRFConfig.
@ FGCM_NEWEST
Find newest Grf.
@ FGCM_ANY
Use first found.
@ FGCM_NEWEST_VALID
Find newest Grf, ignoring Grfs with GCF_INVALID set.
@ FGCM_EXACT
Only find Grfs matching md5sum.
@ FGCM_COMPATIBLE
Find best compatible Grf wrt. desired_version.
GRFConfig * GetGRFConfig(uint32_t grfid, uint32_t mask=0xFFFFFFFF)
Retrieve a NewGRF from the current config by its grfid.
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
GRFConfig ** CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
Copy a GRF Config list.
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
Setup the NewGRF gui.
size_t GRFGetSizeOfDataSection(FileHandle &f)
Get the data section size of a GRF.
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Header of Action 04 "universal holder" structure and functions.
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
std::vector< GRFText > GRFTextList
A GRF text with a list of translations.
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Information about GRF, used in the game and (part of it) in savegames.
MD5Hash original_md5sum
MD5 checksum of original file if only a 'compatible' file was loaded.
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
const char * GetURL() const
Get the grf url.
GRFConfig(const std::string &filename=std::string{})
Create a new GRFConfig.
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
uint8_t palette
GRFPalette, bitset.
uint8_t flags
NOSAVE: GCF_Flags, bitset.
const char * GetDescription() const
Get the grf info.
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
uint32_t grf_bugs
NOSAVE: bugs in this GRF in this run,.
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
void FinalizeParameterInfo()
Finalize Action 14 info after file scan is finished.
bool has_param_defaults
NOSAVE: did this newgrf specify any defaults for it's parameters.
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
uint8_t num_params
Number of used parameters.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
GRFStatus status
NOSAVE: GRFStatus, enum.
std::optional< GRFError > error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
bool IsCompatible(uint32_t old_version) const
Return whether this NewGRF can replace an older version of the same NewGRF.
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
uint8_t num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
std::string filename
Filename - either with or without full path.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
const char * GetName() const
Get the name of this grf.
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
uint32_t min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
std::array< uint32_t, 0x80 > param
GRF parameters.
Information about why GRF had problems during initialisation.
GRFError(StringID severity, StringID message=0)
Construct a new GRFError.
StringID severity
Info / Warning / Error / Fatal.
std::array< uint32_t, 2 > param_value
Values of GRF parameters to show for message and custom_message.
StringID message
Default message.
std::string custom_message
Custom message (if present)
std::string data
Additional data for message and custom_message.
Basic data to distinguish a GRF.
Definition: newgrf_config.h:82
bool HasGrfIdentifier(uint32_t grfid, const MD5Hash *md5sum) const
Does the identification match the provided values?
Definition: newgrf_config.h:99
uint32_t grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:83
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Definition: newgrf_config.h:84
Information about one grf parameter.
bool complete_labels
True if all values have a label.
std::map< uint32_t, GRFTextList > value_names
Names for each value.
uint8_t param_nr
GRF parameter to store content in.
uint32_t min_value
The minimal value this parameter can have.
uint8_t num_bit
Number of bits to use for this parameter.
uint8_t first_bit
First bit to use in the GRF parameter.
uint32_t max_value
The maximal value of this parameter.
uint32_t GetValue(struct GRFConfig *config) const
Get the value of this user-changeable parameter from the given config.
GRFParameterType type
The type of this parameter.
void Finalize()
Finalize Action 14 info after file scan is finished.
GRFParameterInfo(uint nr)
Create a new empty GRFParameterInfo object.
void SetValue(struct GRFConfig *config, uint32_t value)
Set the value of this user-changeable parameter in the given config.
uint32_t def_value
Default value of this parameter.
GRFTextList name
The name of this parameter.
GRFTextList desc
The description of this parameter.
Callback for NewGRF scanning.
virtual ~NewGRFScanCallback()=default
Make sure the right destructor gets called.
virtual void OnNewGRFsScanned()=0
Called whenever the NewGRF scan completed.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14