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/>.
10 /** @file newgrf_config.h Functions to find and configure NewGRFs. */
12 #ifndef NEWGRF_CONFIG_H
13 #define NEWGRF_CONFIG_H
17 #include "core/pointer.h"
19 #include "strings_type.h"
20 #include "core/alloc_type.hpp"
21 #include "core/smallmap_type.hpp"
22 #include "fileio_type.h"
24 #include "newgrf_text.h"
26 /** GRF config bit flags */
28 GCF_SYSTEM
, ///< GRF file is an openttd-internal system grf
29 GCF_UNSAFE
, ///< GRF file is unsafe for static usage
30 GCF_STATIC
, ///< GRF file is used statically (can be used in any MP game)
31 GCF_COMPATIBLE
, ///< GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches)
32 GCF_COPY
, ///< The data is copied from a grf in _all_grfs
33 GCF_INIT_ONLY
, ///< GRF file is processed up to GLS_INIT
34 GCF_RESERVED
, ///< GRF file passed GLS_RESERVE stage
35 GCF_INVALID
, ///< GRF is unusable with this version of OpenTTD
40 GCS_UNKNOWN
, ///< The status of this grf file is unknown
41 GCS_DISABLED
, ///< GRF file is disabled
42 GCS_NOT_FOUND
, ///< GRF file was not found in the local cache
43 GCS_INITIALISED
, ///< GRF file has been initialised
44 GCS_ACTIVATED
, ///< GRF file has been activated
47 /** Encountered GRF bugs */
49 GBUG_VEH_LENGTH
, ///< Length of rail vehicle changes when not inside a depot
50 GBUG_VEH_REFIT
, ///< Articulated vehicles carry different cargoes resp. are differently refittable than specified in purchase list
51 GBUG_VEH_POWERED_WAGON
, ///< Powered wagon changed poweredness state when not inside a depot
52 GBUG_UNKNOWN_CB_RESULT
, ///< A callback returned an unknown/invalid result
53 GBUG_VEH_CAPACITY
, ///< Capacity of vehicle changes when not refitting or arranging
56 /** Status of post-gameload GRF compatibility check */
57 enum GRFListCompatibility
{
58 GLC_ALL_GOOD
, ///< All GRF needed by game are present
59 GLC_COMPATIBLE
, ///< Compatible (eg. the same ID, but different checksum) GRF found in at least one case
60 GLC_NOT_FOUND
, ///< At least one GRF couldn't be found (higher priority than GLC_COMPATIBLE)
63 /** Information that can/has to be stored about a GRF's palette. */
65 GRFP_USE_BIT
= 0, ///< The bit used for storing the palette to use.
66 GRFP_GRF_OFFSET
= 2, ///< The offset of the GRFP_GRF data.
67 GRFP_GRF_SIZE
= 2, ///< The size of the GRFP_GRF data.
68 GRFP_BLT_OFFSET
= 4, ///< The offset of the GRFP_BLT data.
69 GRFP_BLT_SIZE
= 1, ///< The size of the GRFP_BLT data.
71 GRFP_USE_DOS
= 0x0, ///< The palette state is set to use the DOS palette.
72 GRFP_USE_WINDOWS
= 0x1, ///< The palette state is set to use the Windows palette.
73 GRFP_USE_MASK
= 0x1, ///< Bitmask to get only the use palette use states.
75 GRFP_GRF_UNSET
= 0x0 << GRFP_GRF_OFFSET
, ///< The NewGRF provided no information.
76 GRFP_GRF_DOS
= 0x1 << GRFP_GRF_OFFSET
, ///< The NewGRF says the DOS palette can be used.
77 GRFP_GRF_WINDOWS
= 0x2 << GRFP_GRF_OFFSET
, ///< The NewGRF says the Windows palette can be used.
78 GRFP_GRF_ANY
= GRFP_GRF_DOS
| GRFP_GRF_WINDOWS
, ///< The NewGRF says any palette can be used.
79 GRFP_GRF_MASK
= GRFP_GRF_ANY
, ///< Bitmask to get only the NewGRF supplied information.
81 GRFP_BLT_UNSET
= 0x0 << GRFP_BLT_OFFSET
, ///< The NewGRF provided no information or doesn't care about a 32 bpp blitter.
82 GRFP_BLT_32BPP
= 0x1 << GRFP_BLT_OFFSET
, ///< The NewGRF prefers a 32 bpp blitter.
83 GRFP_BLT_MASK
= GRFP_BLT_32BPP
, ///< Bitmask to only get the blitter information.
87 /** Basic data to distinguish a GRF. Used in the server list window */
88 struct GRFIdentifier
{
89 uint32 grfid
; ///< GRF ID (defined by Action 0x08)
90 uint8 md5sum
[16]; ///< MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
93 * Does the identification match the provided values?
94 * @param grfid Expected grfid.
95 * @param md5sum Expected md5sum, may be \c NULL (in which case, do not check it).
96 * @return the object has the provided grfid and md5sum.
98 inline bool matches (uint32 grfid
, const uint8
*md5sum
) const
100 if (this->grfid
!= grfid
) return false;
101 if (md5sum
== NULL
) return true;
102 return memcmp(md5sum
, this->md5sum
, sizeof(this->md5sum
)) == 0;
106 * Does this identifier match another one?
107 * @param other The identifier to match against.
108 * @return Whether this identifier is the same as the provided one.
110 inline bool matches (const GRFIdentifier
&other
) const
112 return this->matches (other
.grfid
, other
.md5sum
);
116 /** Information about why GRF had problems during initialisation */
117 struct GRFError
: ZeroedMemoryAllocator
{
118 GRFError(StringID severity
, StringID message
= 0);
119 GRFError(const GRFError
&error
);
122 char *custom_message
; ///< Custom message (if present)
123 char *data
; ///< Additional data for message and custom_message
124 StringID message
; ///< Default message
125 StringID severity
; ///< Info / Warning / Error / Fatal
126 uint32 param_value
[2]; ///< Values of GRF parameters to show for message and custom_message
129 /** The possible types of a newgrf parameter. */
130 enum GRFParameterType
{
131 PTYPE_UINT_ENUM
, ///< The parameter allows a range of numbers, each of which can have a special name
132 PTYPE_BOOL
, ///< The parameter is either 0 or 1
133 PTYPE_END
, ///< Invalid parameter type
136 /** Information about one grf parameter. */
137 struct GRFParameterInfo
{
138 GRFParameterInfo(uint nr
);
139 GRFParameterInfo(GRFParameterInfo
&info
);
141 GRFTextMap name
; ///< The name of this parameter
142 GRFTextMap desc
; ///< The description of this parameter
143 GRFParameterType type
; ///< The type of this parameter
144 uint32 min_value
; ///< The minimal value this parameter can have
145 uint32 max_value
; ///< The maximal value of this parameter
146 uint32 def_value
; ///< Default value of this parameter
147 byte param_nr
; ///< GRF parameter to store content in
148 byte first_bit
; ///< First bit to use in the GRF parameter
149 byte num_bit
; ///< Number of bits to use for this parameter
150 std::map
<uint32
, GRFTextMap
> value_names
; ///< Names for each value.
151 bool complete_labels
; ///< True if all values have a label.
153 uint32
GetValue(struct GRFConfig
*config
) const;
154 void SetValue(struct GRFConfig
*config
, uint32 value
);
158 /** Information about GRF, used in the game and (part of it) in savegames */
159 struct GRFConfig
: ZeroedMemoryAllocator
{
160 GRFConfig(const char *filename
= NULL
);
161 GRFConfig(const GRFConfig
&config
);
164 GRFIdentifier ident
; ///< grfid and md5sum to uniquely identify newgrfs
165 uint8 original_md5sum
[16]; ///< MD5 checksum of original file if only a 'compatible' file was loaded
166 char *filename
; ///< Filename - either with or without full path
167 ttd_shared_ptr
<GRFTextMap
> name
; ///< NOSAVE: GRF name (Action 0x08)
168 ttd_shared_ptr
<GRFTextMap
> info
; ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
169 ttd_shared_ptr
<GRFTextMap
> url
; ///< NOSAVE: URL belonging to this GRF.
170 GRFError
*error
; ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B)
172 uint32 version
; ///< NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown
173 uint32 min_loadable_version
; ///< NOSAVE: Minimum compatible version a NewGRF can define
174 uint8 flags
; ///< NOSAVE: GCF_Flags, bitset
175 GRFStatus status
; ///< NOSAVE: GRFStatus, enum
176 uint32 grf_bugs
; ///< NOSAVE: bugs in this GRF in this run, @see enum GRFBugs
177 uint32 param
[0x80]; ///< GRF parameters
178 uint8 num_params
; ///< Number of used parameters
179 uint8 num_valid_params
; ///< NOSAVE: Number of valid parameters (action 0x14)
180 uint8 palette
; ///< GRFPalette, bitset
181 SmallVector
<GRFParameterInfo
*, 4> param_info
; ///< NOSAVE: extra information about the parameters
182 bool has_param_defaults
; ///< NOSAVE: did this newgrf specify any defaults for it's parameters
184 struct GRFConfig
*next
; ///< NOSAVE: Next item in the linked list
186 void CopyParams(const GRFConfig
&src
);
188 TextfileDesc
GetTextfile (TextfileType type
) const;
189 const char *GetName() const;
190 const char *GetDescription() const;
191 const char *GetURL() const;
193 void SetParameterDefaults();
194 void SetSuitablePalette();
195 void FinalizeParameterInfo();
198 /** Method to find GRFs using FindGRFConfig */
199 enum FindGRFConfigMode
{
200 FGCM_EXACT
, ///< Only find Grfs matching md5sum
201 FGCM_COMPATIBLE
, ///< Find best compatible Grf wrt. desired_version
202 FGCM_NEWEST
, ///< Find newest Grf
203 FGCM_NEWEST_VALID
,///< Find newest Grf, ignoring Grfs with GCF_INVALID set
204 FGCM_ANY
, ///< Use first found
207 extern GRFConfig
*_all_grfs
; ///< First item in list of all scanned NewGRFs
208 extern GRFConfig
*_grfconfig
; ///< First item in list of current GRF set up
209 extern GRFConfig
*_grfconfig_newgame
; ///< First item in list of default GRF set up
210 extern GRFConfig
*_grfconfig_static
; ///< First item in list of static GRF set up
211 extern uint _missing_extra_graphics
; ///< Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
213 /** Callback for NewGRF scanning. */
214 struct NewGRFScanCallback
{
215 /** Make sure the right destructor gets called. */
216 virtual ~NewGRFScanCallback() {}
217 /** Called whenever the NewGRF scan completed. */
218 virtual void OnNewGRFsScanned() = 0;
221 size_t GRFGetSizeOfDataSection(FILE *f
);
223 void ScanNewGRFFiles(NewGRFScanCallback
*callback
);
224 const GRFConfig
*FindGRFConfig(uint32 grfid
, FindGRFConfigMode mode
, const uint8
*md5sum
= NULL
, uint32 desired_version
= 0);
225 GRFConfig
*GetGRFConfig(uint32 grfid
, uint32 mask
= 0xFFFFFFFF);
226 GRFConfig
**CopyGRFConfigList(GRFConfig
**dst
, const GRFConfig
*src
, bool init_only
);
227 void AppendStaticGRFConfigs(GRFConfig
**dst
);
228 void AppendToGRFConfigList(GRFConfig
**dst
, GRFConfig
*el
);
229 void ClearGRFConfigList(GRFConfig
**config
);
230 void ResetGRFConfig(bool defaults
);
231 GRFListCompatibility
IsGoodGRFConfigList(GRFConfig
*grfconfig
);
232 bool FillGRFDetails(GRFConfig
*config
, bool is_static
, Subdirectory subdir
= NEWGRF_DIR
);
233 void GRFBuildParamList (stringb
*dst
, const GRFConfig
*c
);
235 /* In newgrf_gui.cpp */
236 void ShowNewGRFSettings(bool editable
, bool show_params
, bool exec_changes
, GRFConfig
**config
);
238 #ifdef ENABLE_NETWORK
239 /** For communication about GRFs over the network */
240 #define UNKNOWN_GRF_NAME_PLACEHOLDER "<Unknown>"
241 ttd_shared_ptr
<GRFTextMap
> *FindUnknownGRFName (const GRFIdentifier
&ident
, bool create
);
242 #endif /* ENABLE_NETWORK */
244 void UpdateNewGRFScanStatus(uint num
, const char *name
);
245 bool UpdateNewGRFConfigPalette(int32 p1
= 0);
247 #endif /* NEWGRF_CONFIG_H */