OpenTTD Source 20260421-master-gc2fbc6fdeb
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NEWGRF_CONFIG_H
11#define NEWGRF_CONFIG_H
12
13#include "strings_type.h"
14#include "fileio_type.h"
15#include "textfile_type.h"
16#include "newgrf_text.h"
17#include "3rdparty/md5/md5.h"
18
30using GRFConfigFlags = EnumBitSet<GRFConfigFlag, uint8_t>;
31
40
42enum class GRFBug : uint8_t {
48};
49using GRFBugs = EnumBitSet<GRFBug, uint8_t>;
50
57
80
81
83struct GRFIdentifier {
84 uint32_t grfid;
85 MD5Hash md5sum;
86
87 GRFIdentifier() = default;
88 GRFIdentifier(const GRFIdentifier &other) = default;
89 GRFIdentifier(GRFIdentifier &&other) = default;
90 GRFIdentifier(uint32_t grfid, const MD5Hash &md5sum) : grfid(grfid), md5sum(md5sum) {}
91
92 GRFIdentifier& operator =(const GRFIdentifier &other) = default;
93
100 inline bool HasGrfIdentifier(uint32_t grfid, const MD5Hash *md5sum) const
101 {
102 if (this->grfid != grfid) return false;
103 if (md5sum == nullptr) return true;
104 return *md5sum == this->md5sum;
105 }
106};
107
109struct GRFError {
110 GRFError(StringID severity, uint32_t nfo_line, StringID message = {})
112
113 std::string custom_message{};
114 std::string data{};
117 uint32_t nfo_line;
118 std::array<uint32_t, 2> param_value{};
119};
120
122enum class GRFParameterType : uint8_t {
125};
126
133 explicit GRFParameterInfo(uint nr) : param_nr(nr) {}
134
137
138 uint32_t min_value = 0;
139 uint32_t max_value = UINT32_MAX;
140 uint32_t def_value = 0;
141
143
144 uint8_t param_nr;
145 uint8_t first_bit = 0;
146 uint8_t num_bit = 32;
147
148 bool complete_labels = false;
149
150 using ValueName = std::pair<uint32_t, GRFTextList>;
151 std::vector<ValueName> value_names;
152
153 void Finalize();
154};
155
157struct GRFConfig {
158 static constexpr uint8_t MAX_NUM_PARAMS = 0x80;
159
160 GRFConfig() = default;
161 GRFConfig(const std::string &filename) : filename(filename) {}
162 GRFConfig(const GRFConfig &config);
163
164 /* Remove the copy assignment, as the default implementation will not do the right thing. */
165 GRFConfig &operator=(GRFConfig &rhs) = delete;
166
168 MD5Hash original_md5sum{};
169 std::string filename{};
173 std::vector<GRFError> errors;
174
175 uint32_t version = 0;
176 uint32_t min_loadable_version = 0;
177 GRFConfigFlags flags = {};
179 GRFBugs grf_bugs = {};
180 uint8_t num_valid_params = MAX_NUM_PARAMS;
181 uint8_t palette = 0;
182 bool has_param_defaults = false;
183 std::vector<std::optional<GRFParameterInfo>> param_info;
184 std::vector<uint32_t> param;
185
186 bool IsCompatible(uint32_t old_version) const;
187 void SetParams(std::span<const uint32_t> pars);
188 void CopyParams(const GRFConfig &src);
189
190 uint32_t GetValue(const GRFParameterInfo &info) const;
191 void SetValue(const GRFParameterInfo &info, uint32_t value);
192
193 std::optional<std::string> GetTextfile(TextfileType type) const;
194 std::string GetName() const;
195 std::optional<std::string> GetDescription() const;
196 std::optional<std::string> GetURL() const;
197
199 void SetSuitablePalette();
201};
202
203using GRFConfigList = std::vector<std::unique_ptr<GRFConfig>>;
204
213
214extern GRFConfigList _all_grfs;
215extern GRFConfigList _grfconfig;
216extern GRFConfigList _grfconfig_newgame;
217extern GRFConfigList _grfconfig_static;
218extern uint _missing_extra_graphics;
219
223 virtual ~NewGRFScanCallback() = default;
225 virtual void OnNewGRFsScanned() = 0;
226};
227
229
230void ScanNewGRFFiles(NewGRFScanCallback *callback);
231const GRFConfig *FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum = nullptr, uint32_t desired_version = 0);
232GRFConfig *GetGRFConfig(uint32_t grfid, uint32_t mask = 0xFFFFFFFF);
233void CopyGRFConfigList(GRFConfigList &dst, const GRFConfigList &src, bool init_only);
234void AppendStaticGRFConfigs(GRFConfigList &dst);
235void AppendToGRFConfigList(GRFConfigList &dst, std::unique_ptr<GRFConfig> &&el);
236void ClearGRFConfigList(GRFConfigList &config);
237void ResetGRFConfig(bool defaults);
238GRFListCompatibility IsGoodGRFConfigList(GRFConfigList &grfconfig);
239bool FillGRFDetails(GRFConfig &config, bool is_static, Subdirectory subdir = Subdirectory::NewGrf);
240std::string GRFBuildParamList(const GRFConfig &c);
241
242/* In newgrf_gui.cpp */
243void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfigList &config);
244void OpenGRFParameterWindow(bool is_baseset, GRFConfig &c, bool editable);
245
246void UpdateNewGRFScanStatus(uint num, std::string &&name);
248
249#endif /* NEWGRF_CONFIG_H */
Enum-as-bit-set wrapper.
Types for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
@ NewGrf
Subdirectory for all NewGRFs.
Definition fileio_type.h:97
GRFConfigList _grfconfig
First item in list of current GRF set up.
GRFConfigList _grfconfig_static
First item in list of static GRF set up.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
GRFConfigList _grfconfig_newgame
First item in list of default GRF set up.
GRFConfigList _all_grfs
First item in list of all scanned NewGRFs.
GRFListCompatibility
Status of post-gameload GRF compatibility check.
@ AllGood
All GRF needed by game are present.
void CopyGRFConfigList(GRFConfigList &dst, const GRFConfigList &src, bool init_only)
Copy a GRF Config list.
GRFStatus
Status of GRF.
@ NotFound
GRF file was not found in the local cache.
@ Initialised
GRF file has been initialised.
@ Unknown
The status of this grf file is unknown.
@ Disabled
GRF file is disabled.
@ Activated
GRF file has been activated.
GRFConfig * GetGRFConfig(uint32_t grfid, uint32_t mask=0xFFFFFFFF)
Retrieve a NewGRF from the current config by its grfid.
GRFListCompatibility IsGoodGRFConfigList(GRFConfigList &grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
std::string GRFBuildParamList(const GRFConfig &c)
Build a string containing space separated parameter values.
GRFConfigFlag
GRF config bit flags.
@ Invalid
GRF is unusable with this version of OpenTTD.
@ Copy
The data is copied from a grf in _all_grfs.
@ Static
GRF file is used statically (can be used in any MP game).
@ Compatible
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches).
@ Reserved
GRF file passed GrfLoadingStage::Reserve stage.
@ System
GRF file is an openttd-internal system grf.
@ Unsafe
GRF file is unsafe for static usage.
@ InitOnly
GRF file is processed up to GrfLoadingStage::Init.
void UpdateNewGRFScanStatus(uint num, std::string &&name)
Update the NewGRF scan status.
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfigList &config)
Setup the NewGRF gui.
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
GRFBug
Encountered GRF bugs.
@ VehRefit
Articulated vehicles carry different cargoes resp. are differently refittable than specified in purch...
@ UnknownCbResult
A callback returned an unknown/invalid result.
@ VehCapacity
Capacity of vehicle changes when not refitting or arranging.
@ VehPoweredWagon
Powered wagon changed poweredness state when not inside a depot.
@ VehLength
Length of rail vehicle changes when not inside a depot.
bool FillGRFDetails(GRFConfig &config, bool is_static, Subdirectory subdir=Subdirectory::NewGrf)
Find the GRFID of a given grf, and calculate its md5sum.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum=nullptr, uint32_t desired_version=0)
Find a NewGRF in the scanned list.
void AppendStaticGRFConfigs(GRFConfigList &dst)
Appends the static GRFs to a list of GRFs.
GRFParameterType
The possible types of a newgrf parameter.
@ UintEnum
The parameter allows a range of numbers, each of which can have a special name.
@ Bool
The parameter is either 0 or 1.
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
FindGRFConfigMode
Method to find GRFs using FindGRFConfig.
@ Exact
Only find Grfs matching md5sum.
@ NewestValid
Find newest Grf, ignoring Grfs with GRFConfigFlag::Invalid set.
@ Newest
Find newest Grf.
@ Any
Use first found.
size_t GRFGetSizeOfDataSection(FileHandle &f)
Get the data section size of a GRF.
GRFPalette
Information that can/has to be stored about a GRF's palette.
@ GRFP_GRF_UNSET
The NewGRF provided no information.
@ GRFP_BLT_UNSET
The NewGRF provided no information or doesn't care about a 32 bpp blitter.
@ GRFP_USE_DOS
The palette state is set to use the DOS palette.
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
@ GRFP_USE_WINDOWS
The palette state is set to use the Windows palette.
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
@ GRFP_GRF_ANY
The NewGRF says any palette can be used.
@ GRFP_BLT_MASK
Bitmask to only get the blitter information.
@ GRFP_BLT_32BPP
The NewGRF prefers a 32 bpp blitter.
@ GRFP_BLT_OFFSET
The offset of the GRFP_BLT data.
@ GRFP_GRF_SIZE
The size of the GRFP_GRF data.
@ GRFP_USE_BIT
The bit used for storing the palette to use.
@ GRFP_USE_MASK
Bitmask to get only the use palette use states.
@ GRFP_BLT_SIZE
The size of the GRFP_BLT data.
@ GRFP_GRF_OFFSET
The offset of the GRFP_GRF data.
@ GRFP_GRF_MASK
Bitmask to get only the NewGRF supplied information.
void AppendToGRFConfigList(GRFConfigList &dst, std::unique_ptr< GRFConfig > &&el)
Appends an element to a list of GRFs.
void UpdateNewGRFConfigPalette()
Update the palettes of the graphics from the config file.
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
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.
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.
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
uint8_t palette
GRFPalette, bitset.
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08).
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
std::vector< GRFError > errors
NOSAVE: Error/Warning during GRF loading (Action 0x0B).
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
std::vector< uint32_t > param
GRF parameters.
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).
GRFBugs grf_bugs
NOSAVE: bugs in this GRF in this run,.
std::optional< std::string > GetURL() const
Get the grf url.
GRFStatus status
NOSAVE: GRFStatus, enum.
void SetValue(const GRFParameterInfo &info, uint32_t value)
Set the value of the given user-changeable parameter.
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.
GRFConfigFlags flags
NOSAVE: GCF_Flags, bitset.
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
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::string GetName() const
Get the name of this grf.
std::optional< std::string > GetDescription() const
Get the grf info.
uint32_t GetValue(const GRFParameterInfo &info) const
Get the value of the given user-changeable parameter.
StringID severity
Info / Warning / Error / Fatal.
std::array< uint32_t, 2 > param_value
Values of GRF parameters to show for message and custom_message.
uint32_t nfo_line
Line within NewGRF of error.
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.
bool HasGrfIdentifier(uint32_t grfid, const MD5Hash *md5sum) const
Does the identification match the provided values?
uint32_t grfid
GRF ID (defined by Action 0x08).
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF).
Information about one grf parameter.
bool complete_labels
True if all values have a label.
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.
GRFParameterType type
The type of this parameter.
std::vector< ValueName > value_names
Names for each value.
void Finalize()
Finalize Action 14 info after file scan is finished.
GRFParameterInfo(uint nr)
Create a new empty GRFParameterInfo object.
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
Ensure the destructor of the sub classes are called as well.
virtual void OnNewGRFsScanned()=0
Called whenever the NewGRF scan completed.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.