OpenTTD Source 20250312-master-gcdcc6b491d
base_media_base.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 BASE_MEDIA_BASE_H
11#define BASE_MEDIA_BASE_H
12
13#include "fileio_func.h"
14#include "gfx_type.h"
15#include "textfile_type.h"
16#include "textfile_gui.h"
17#include "3rdparty/md5/md5.h"
18#include <unordered_map>
19
20/* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
21struct IniFile;
22struct ContentInfo;
23
25struct MD5File {
33
34 std::string filename;
35 MD5Hash hash;
36 std::string missing_warning;
38
39 ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
40};
41
48template <class T, size_t Tnum_files, bool Tsearch_in_tars>
49struct BaseSet {
50 typedef std::unordered_map<std::string, std::string> TranslatedStrings;
51
53 static const size_t NUM_FILES = Tnum_files;
54
56 static const bool SEARCH_IN_TARS = Tsearch_in_tars;
57
59 static const char * const *file_names;
60
61 std::string name;
62 std::string url;
63 TranslatedStrings description;
64 uint32_t shortname;
65 uint32_t version;
66 bool fallback;
67
71
72 T *next;
73
76 {
77 delete this->next;
78 }
79
84 int GetNumMissing() const
85 {
86 return Tnum_files - this->found_files;
87 }
88
94 int GetNumInvalid() const
95 {
96 return Tnum_files - this->valid_files;
97 }
98
99 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true);
100 void CopyCompatibleConfig([[maybe_unused]] const T &src) {}
101
110 const std::string &GetDescription(const std::string &isocode) const
111 {
112 if (!isocode.empty()) {
113 /* First the full ISO code */
114 auto desc = this->description.find(isocode);
115 if (desc != this->description.end()) return desc->second;
116
117 /* Then the first two characters */
118 desc = this->description.find(isocode.substr(0, 2));
119 if (desc != this->description.end()) return desc->second;
120 }
121 /* Then fall back */
122 return this->description.at(std::string{});
123 }
124
131 std::string GetListLabel() const
132 {
133 if (this->GetNumInvalid() == 0) return GetString(STR_JUST_RAW_STRING, this->name);
134
135 return GetString(STR_BASESET_STATUS, this->name, this->GetNumInvalid());
136 }
137
148 {
149 return file->CheckMD5(subdir, SIZE_MAX);
150 }
151
157 std::optional<std::string> GetTextfile(TextfileType type) const
158 {
159 for (const auto &file : this->files) {
160 auto textfile = ::GetTextfile(type, BASESET_DIR, file.filename);
161 if (textfile.has_value()) {
162 return textfile;
163 }
164 }
165 return std::nullopt;
166 }
167};
168
173template <class Tbase_set>
175protected:
176 static Tbase_set *available_sets;
177 static Tbase_set *duplicate_sets;
178 static const Tbase_set *used_set;
179
180 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
181
186 static const char *GetExtension();
187public:
193 static bool DetermineBestSet();
194
196 static uint FindSets()
197 {
199 /* Searching in tars is only done in the old "data" directories basesets. */
200 uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? OLD_DATA_DIR : OLD_GM_DIR, Tbase_set::SEARCH_IN_TARS);
201 return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
202 }
203
204 static Tbase_set *GetAvailableSets();
205
206 static bool SetSet(const Tbase_set *set);
207 static bool SetSetByName(const std::string &name);
208 static bool SetSetByShortname(uint32_t shortname);
209 static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
210 static int GetNumSets();
211 static int GetIndexOfUsedSet();
212 static const Tbase_set *GetSet(int index);
213 static const Tbase_set *GetUsedSet();
214
221 static bool HasSet(const ContentInfo *ci, bool md5sum);
222};
223
224template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
225template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
226template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
227
235template <class Tbase_set>
236const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
237
248
250enum BlitterType : uint8_t {
253};
254
255struct GRFConfig;
256
258struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
259private:
260 mutable std::unique_ptr<GRFConfig> extra_cfg;
261public:
264
265 GraphicsSet();
266 ~GraphicsSet();
267
268 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
269 GRFConfig *GetExtraConfig() const { return this->extra_cfg.get(); }
271 bool IsConfigurable() const;
272 void CopyCompatibleConfig(const GraphicsSet &src);
273
274 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
275};
276
278class BaseGraphics : public BaseMedia<GraphicsSet> {
279public:
281 struct Ini {
282 std::string name;
283 uint32_t shortname;
284 uint32_t extra_version;
285 std::vector<uint32_t> extra_params;
286 };
287 static inline Ini ini_data;
288
289};
290
292struct SoundsSet : BaseSet<SoundsSet, 1, true> {
293};
294
296class BaseSounds : public BaseMedia<SoundsSet> {
297public:
299 static inline std::string ini_set;
300
301};
302
304static const uint NUM_SONGS_CLASS = 10;
306static const uint NUM_SONG_CLASSES = 3;
309
311static const uint NUM_SONGS_PLAYLIST = 32;
312
313/* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
314std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum);
315std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum);
316
321
333
335struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
340
341 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
342};
343
345class BaseMusic : public BaseMedia<MusicSet> {
346public:
348 static inline std::string ini_set;
349
350};
351
352#endif /* BASE_MEDIA_BASE_H */
BlitterType
Blitter type for base graphics sets.
@ BLT_32BPP
Base set has both 8 bpp and 32 bpp sprites.
@ BLT_8BPP
Base set has 8 bpp sprites only.
std::optional< std::string > GetMusicCatEntryName(const std::string &filename, size_t entrynum)
Read the name of a music CAT file entry.
Definition music.cpp:28
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
static const uint NUM_SONG_CLASSES
Number of classes for songs.
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
GraphicsFileType
Types of graphics in the base graphics set.
@ GFT_TOYLAND
Landscape replacement sprites for toyland.
@ GFT_LOGOS
Logos, landscape icons and original terrain generator sprites.
@ MAX_GFT
We are looking for this amount of GRFs.
@ GFT_EXTRA
Extra sprites that were not part of the original sprites.
@ GFT_ARCTIC
Landscape replacement sprites for arctic.
@ GFT_TROPICAL
Landscape replacement sprites for tropical.
@ GFT_BASE
Base sprites for all climates.
static const uint NUM_SONGS_CLASS
Maximum number of songs in the 'class' playlists.
static const uint NUM_SONGS_PLAYLIST
Maximum number of songs in the (custom) playlist.
std::optional< std::vector< uint8_t > > GetMusicCatEntryData(const std::string &filename, size_t entrynum)
Read the full data of a music CAT file entry.
Definition music.cpp:52
MusicTrackType
@ MTT_MPSMIDI
MPS GM driver MIDI format (contained in a CAT file)
@ MTT_STANDARDMIDI
Standard MIDI file.
All data/functions related with replacing the base graphics.
Base for all base media (graphics, sounds)
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
Definition gfxinit.cpp:479
static const Tbase_set * GetUsedSet()
Return the used set.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
static Tbase_set * duplicate_sets
All sets that aren't available, but needed for not downloading base sets when a newer version than th...
static bool SetSetByShortname(uint32_t shortname)
Set the set to be used.
static void GetSetsList(std::back_insert_iterator< std::string > &output_iterator)
Returns a list with the sets.
static int GetNumSets()
Count the number of available graphics sets.
static uint FindSets()
Do the scan for files.
static const Tbase_set * used_set
The currently used set.
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
static Tbase_set * GetAvailableSets()
Return the available sets.
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
static const char * GetExtension()
Get the extension that is used to identify this set.
Definition gfxinit.cpp:503
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
static Tbase_set * available_sets
All available sets.
static bool SetSetByName(const std::string &name)
Set the set to be used.
static bool SetSet(const Tbase_set *set)
Set the set to be used.
All data/functions related with replacing the base music.
static std::string ini_set
The set as saved in the config file.
All data/functions related with replacing the base sounds.
static std::string ini_set
The set as saved in the config file.
Helper for scanning for files with a given name.
Definition fileio_func.h:37
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition fileio.cpp:1114
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ OLD_DATA_DIR
Old subdirectory for the data.
@ OLD_GM_DIR
Old subdirectory for the music.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Types related to the graphics and/or input devices.
PaletteType
Palettes OpenTTD supports.
Definition gfx_type.h:338
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
Values loaded from config file.
uint32_t extra_version
version of the extra GRF
std::vector< uint32_t > extra_params
parameters for the extra GRF
uint32_t shortname
unique key for base set
Information about a single base set.
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
T * next
The next base set in this list.
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
static const bool SEARCH_IN_TARS
Whether to search in the tars or not.
int GetNumInvalid() const
Get the number of invalid files.
uint found_files
Number of the files that could be found.
TranslatedStrings description
Description of the base set.
~BaseSet()
Free everything we allocated.
int GetNumMissing() const
Get the number of missing files.
bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
const std::string & GetDescription(const std::string &isocode) const
Get the description for the given ISO code.
uint32_t version
The version of this base set.
static const char *const * file_names
Internal names of the files in this set.
static const size_t NUM_FILES
Number of files in this set.
std::string name
The name of the base set.
std::string GetListLabel() const
Get string to use when listing this set in the settings window.
uint valid_files
Number of the files that could be found and are valid.
MD5File files[NUM_FILES]
All files part of this set.
std::string url
URL for information about the base set.
uint32_t shortname
Four letter short variant of the name.
Container for all important information about a piece of content.
Information about GRF, used in the game and (part of it) in savegames.
All data of a graphics set.
std::unique_ptr< GRFConfig > extra_cfg
Parameters for extra GRF.
PaletteType palette
Palette of this graphics set.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
Definition gfxinit.cpp:428
BlitterType blitter
Blitter of this graphics set.
GRFConfig & GetOrCreateExtraConfig() const
Return configuration for the extra GRF, or lazily create it.
Definition gfxinit.cpp:383
Ini file that supports both loading and saving.
Definition ini_type.h:88
Structure holding filename and MD5 information about a single file.
std::string missing_warning
warning when this file is missing
ChecksumResult check_result
cached result of md5 check
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
Definition gfxinit.cpp:449
ChecksumResult
The result of a checksum check.
@ CR_MATCH
The file did exist and the md5 checksum did match.
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
@ CR_NO_FILE
The file did not exist.
@ CR_UNKNOWN
The file has not been checked yet.
MD5Hash hash
md5 sum of the file
std::string filename
filename
All data of a music set.
MusicSongInfo songinfo[NUM_SONGS_AVAILABLE]
Data about individual songs in set.
uint8_t num_available
Number of valid songs in set.
Metadata about a music track.
MusicTrackType filetype
decoder required for song file
std::string songname
name of song displayed in UI
uint8_t tracknr
track number of song displayed in UI
std::string filename
file on disk containing song (when used in MusicSet class)
int override_start
MIDI ticks to skip over in beginning.
bool loop
song should play in a tight loop if possible, never ending
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
int override_end
MIDI tick to end the song at (0 if no override)
All data of a sounds set.
GUI functions related to textfiles.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.