OpenTTD Source: src/base_media_base.h Source File

OpenTTD Source 15.0-beta1

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
25 struct MD5File {
27 enum ChecksumResult {
28 CR_UNKNOWN,
29 CR_MATCH,
30 CR_MISMATCH,
31 CR_NO_FILE,
32 };
33
34 std::string filename;
35 MD5Hash hash;
36 std::string missing_warning;
37 ChecksumResult check_result;
38
39 ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
40};
41
48template <class T, size_t Tnum_files, bool Tsearch_in_tars>
49 struct 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
68 MD5File files[NUM_FILES];
69 uint found_files;
70 uint valid_files;
71
72 T *next;
73
75 ~BaseSet()
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 this->name;
134
135 SetDParamStr(0, this->name);
136 SetDParam(1, this->GetNumInvalid());
137 return GetString(STR_BASESET_STATUS);
138 }
139
149 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
150 {
151 return file->CheckMD5(subdir, SIZE_MAX);
152 }
153
159 std::optional<std::string> GetTextfile(TextfileType type) const
160 {
161 for (const auto &file : this->files) {
162 auto textfile = ::GetTextfile(type, BASESET_DIR, file.filename);
163 if (textfile.has_value()) {
164 return textfile;
165 }
166 }
167 return std::nullopt;
168 }
169};
170
175template <class Tbase_set>
176 class BaseMedia : FileScanner {
177protected:
178 static Tbase_set *available_sets;
179 static Tbase_set *duplicate_sets;
180 static const Tbase_set *used_set;
181
182 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
183
188 static const char *GetExtension();
189public:
195 static bool DetermineBestSet();
196
198 static uint FindSets()
199 {
200 BaseMedia<Tbase_set> fs;
201 /* Searching in tars is only done in the old "data" directories basesets. */
202 uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? OLD_DATA_DIR : OLD_GM_DIR, Tbase_set::SEARCH_IN_TARS);
203 return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
204 }
205
206 static Tbase_set *GetAvailableSets();
207
208 static bool SetSet(const Tbase_set *set);
209 static bool SetSetByName(const std::string &name);
210 static bool SetSetByShortname(uint32_t shortname);
211 static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
212 static int GetNumSets();
213 static int GetIndexOfUsedSet();
214 static const Tbase_set *GetSet(int index);
215 static const Tbase_set *GetUsedSet();
216
223 static bool HasSet(const ContentInfo *ci, bool md5sum);
224};
225
226template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
227template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
228template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
229
237template <class Tbase_set>
238const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
239
241 enum GraphicsFileType {
242 GFT_BASE,
243 GFT_LOGOS,
244 GFT_ARCTIC,
245 GFT_TROPICAL,
246 GFT_TOYLAND,
247 GFT_EXTRA,
248 MAX_GFT,
249};
250
252 enum BlitterType {
253 BLT_8BPP,
254 BLT_32BPP,
255};
256
257struct GRFConfig;
258
260 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
261private:
262 mutable std::unique_ptr<GRFConfig> extra_cfg;
263public:
264 PaletteType palette;
265 BlitterType blitter;
266
267 GraphicsSet();
268 ~GraphicsSet();
269
270 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
271 GRFConfig *GetExtraConfig() const { return this->extra_cfg.get(); }
272 GRFConfig &GetOrCreateExtraConfig() const;
273 bool IsConfigurable() const;
274 void CopyCompatibleConfig(const GraphicsSet &src);
275
276 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
277};
278
280 class BaseGraphics : public BaseMedia<GraphicsSet> {
281public:
283 struct Ini {
284 std::string name;
285 uint32_t shortname;
286 uint32_t extra_version;
287 std::vector<uint32_t> extra_params;
288 };
289 static inline Ini ini_data;
290
291};
292
294 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
295};
296
298 class BaseSounds : public BaseMedia<SoundsSet> {
299public:
301 static inline std::string ini_set;
302
303};
304
306 static const uint NUM_SONGS_CLASS = 10;
308 static const uint NUM_SONG_CLASSES = 3;
310 static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
311
313 static const uint NUM_SONGS_PLAYLIST = 32;
314
315/* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
316std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum);
317std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum);
318
319 enum MusicTrackType {
320 MTT_STANDARDMIDI,
321 MTT_MPSMIDI,
322};
323
325 struct MusicSongInfo {
326 std::string songname;
327 uint8_t tracknr;
328 std::string filename;
329 MusicTrackType filetype;
330 int cat_index;
331 bool loop;
332 int override_start;
333 int override_end;
334};
335
337 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
339 MusicSongInfo songinfo[NUM_SONGS_AVAILABLE];
341 uint8_t num_available;
342
343 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
344};
345
347 class BaseMusic : public BaseMedia<MusicSet> {
348public:
350 static inline std::string ini_set;
351
352};
353
354#endif /* BASE_MEDIA_BASE_H */
GetMusicCatEntryName
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
TryGetBaseSetFile
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
Definition base_media_func.h:319
NUM_SONG_CLASSES
static const uint NUM_SONG_CLASSES
Number of classes for songs.
Definition base_media_base.h:308
NUM_SONGS_AVAILABLE
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
Definition base_media_base.h:310
NUM_SONGS_CLASS
static const uint NUM_SONGS_CLASS
Maximum number of songs in the 'class' playlists.
Definition base_media_base.h:306
MusicTrackType
MusicTrackType
Definition base_media_base.h:319
MTT_MPSMIDI
@ MTT_MPSMIDI
MPS GM driver MIDI format (contained in a CAT file)
Definition base_media_base.h:321
MTT_STANDARDMIDI
@ MTT_STANDARDMIDI
Standard MIDI file.
Definition base_media_base.h:320
GraphicsFileType
GraphicsFileType
Types of graphics in the base graphics set.
Definition base_media_base.h:241
GFT_TOYLAND
@ GFT_TOYLAND
Landscape replacement sprites for toyland.
Definition base_media_base.h:246
GFT_LOGOS
@ GFT_LOGOS
Logos, landscape icons and original terrain generator sprites.
Definition base_media_base.h:243
MAX_GFT
@ MAX_GFT
We are looking for this amount of GRFs.
Definition base_media_base.h:248
GFT_EXTRA
@ GFT_EXTRA
Extra sprites that were not part of the original sprites.
Definition base_media_base.h:247
GFT_ARCTIC
@ GFT_ARCTIC
Landscape replacement sprites for arctic.
Definition base_media_base.h:244
GFT_TROPICAL
@ GFT_TROPICAL
Landscape replacement sprites for tropical.
Definition base_media_base.h:245
GFT_BASE
@ GFT_BASE
Base sprites for all climates.
Definition base_media_base.h:242
NUM_SONGS_PLAYLIST
static const uint NUM_SONGS_PLAYLIST
Maximum number of songs in the (custom) playlist.
Definition base_media_base.h:313
BlitterType
BlitterType
Blitter type for base graphics sets.
Definition base_media_base.h:252
BLT_32BPP
@ BLT_32BPP
Base set has both 8 bpp and 32 bpp sprites.
Definition base_media_base.h:254
BLT_8BPP
@ BLT_8BPP
Base set has 8 bpp sprites only.
Definition base_media_base.h:253
GetMusicCatEntryData
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
BaseGraphics
All data/functions related with replacing the base graphics.
Definition base_media_base.h:280
BaseMedia
Base for all base media (graphics, sounds)
Definition base_media_base.h:176
BaseMedia::DetermineBestSet
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
Definition gfxinit.cpp:466
BaseMedia::GetUsedSet
static const Tbase_set * GetUsedSet()
Return the used set.
Definition base_media_func.h:394
BaseMedia::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition base_media_func.h:164
BaseMedia::duplicate_sets
static Tbase_set * duplicate_sets
All sets that aren't available, but needed for not downloading base sets when a newer version than th...
Definition base_media_base.h:179
BaseMedia::SetSetByShortname
static bool SetSetByShortname(uint32_t shortname)
Set the set to be used.
Definition base_media_func.h:278
BaseMedia::GetSetsList
static void GetSetsList(std::back_insert_iterator< std::string > &output_iterator)
Returns a list with the sets.
Definition base_media_func.h:297
BaseMedia::GetNumSets
static int GetNumSets()
Count the number of available graphics sets.
Definition base_media_func.h:348
BaseMedia::FindSets
static uint FindSets()
Do the scan for files.
Definition base_media_base.h:198
BaseMedia::used_set
static const Tbase_set * used_set
The currently used set.
Definition base_media_base.h:180
BaseMedia::GetSet
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
Definition base_media_func.h:379
BaseMedia::GetAvailableSets
static Tbase_set * GetAvailableSets()
Return the available sets.
Definition base_media_func.h:404
BaseMedia::GetIndexOfUsedSet
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
Definition base_media_func.h:363
BaseMedia::GetExtension
static const char * GetExtension()
Get the extension that is used to identify this set.
Definition gfxinit.cpp:490
BaseMedia::HasSet
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
Definition base_media_func.h:337
BaseMedia::available_sets
static Tbase_set * available_sets
All available sets.
Definition base_media_base.h:178
BaseMedia::SetSetByName
static bool SetSetByName(const std::string &name)
Set the set to be used.
Definition base_media_func.h:258
BaseMedia::SetSet
static bool SetSet(const Tbase_set *set)
Set the set to be used.
Definition base_media_func.h:241
BaseMusic
All data/functions related with replacing the base music.
Definition base_media_base.h:347
BaseMusic::ini_set
static std::string ini_set
The set as saved in the config file.
Definition base_media_base.h:350
BaseSounds
All data/functions related with replacing the base sounds.
Definition base_media_base.h:298
BaseSounds::ini_set
static std::string ini_set
The set as saved in the config file.
Definition base_media_base.h:301
FileScanner
Helper for scanning for files with a given name.
Definition fileio_func.h:37
FileScanner::Scan
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
fileio_func.h
Functions for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:115
OLD_DATA_DIR
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition fileio_type.h:122
OLD_GM_DIR
@ OLD_GM_DIR
Old subdirectory for the music.
Definition fileio_type.h:121
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:123
gfx_type.h
Types related to the graphics and/or input devices.
PaletteType
PaletteType
Palettes OpenTTD supports.
Definition gfx_type.h:304
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:333
SetDParamStr
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition strings.cpp:371
BaseGraphics::Ini
Values loaded from config file.
Definition base_media_base.h:283
BaseGraphics::Ini::extra_version
uint32_t extra_version
version of the extra GRF
Definition base_media_base.h:286
BaseGraphics::Ini::extra_params
std::vector< uint32_t > extra_params
parameters for the extra GRF
Definition base_media_base.h:287
BaseGraphics::Ini::shortname
uint32_t shortname
unique key for base set
Definition base_media_base.h:285
BaseSet
Information about a single base set.
Definition base_media_base.h:49
BaseSet::GetTextfile
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
Definition base_media_base.h:159
BaseSet::CheckMD5
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
Definition base_media_base.h:149
BaseSet::next
T * next
The next base set in this list.
Definition base_media_base.h:72
BaseSet::fallback
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
Definition base_media_base.h:66
BaseSet::SEARCH_IN_TARS
static const bool SEARCH_IN_TARS
Whether to search in the tars or not.
Definition base_media_base.h:56
BaseSet::GetNumInvalid
int GetNumInvalid() const
Get the number of invalid files.
Definition base_media_base.h:94
BaseSet::found_files
uint found_files
Number of the files that could be found.
Definition base_media_base.h:69
BaseSet::description
TranslatedStrings description
Description of the base set.
Definition base_media_base.h:63
BaseSet::~BaseSet
~BaseSet()
Free everything we allocated.
Definition base_media_base.h:75
BaseSet::GetNumMissing
int GetNumMissing() const
Get the number of missing files.
Definition base_media_base.h:84
BaseSet::FillSetDetails
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.
Definition base_media_func.h:42
BaseSet::GetDescription
const std::string & GetDescription(const std::string &isocode) const
Get the description for the given ISO code.
Definition base_media_base.h:110
BaseSet::version
uint32_t version
The version of this base set.
Definition base_media_base.h:65
BaseSet::file_names
static const char *const * file_names
Internal names of the files in this set.
Definition base_media_base.h:59
BaseSet::NUM_FILES
static const size_t NUM_FILES
Number of files in this set.
Definition base_media_base.h:53
BaseSet::name
std::string name
The name of the base set.
Definition base_media_base.h:61
BaseSet::GetListLabel
std::string GetListLabel() const
Get string to use when listing this set in the settings window.
Definition base_media_base.h:131
BaseSet::valid_files
uint valid_files
Number of the files that could be found and are valid.
Definition base_media_base.h:70
BaseSet::files
MD5File files[NUM_FILES]
All files part of this set.
Definition base_media_base.h:68
BaseSet::url
std::string url
URL for information about the base set.
Definition base_media_base.h:62
BaseSet::shortname
uint32_t shortname
Four letter short variant of the name.
Definition base_media_base.h:64
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:52
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:154
GraphicsSet
All data of a graphics set.
Definition base_media_base.h:260
GraphicsSet::extra_cfg
std::unique_ptr< GRFConfig > extra_cfg
Parameters for extra GRF.
Definition base_media_base.h:262
GraphicsSet::palette
PaletteType palette
Palette of this graphics set.
Definition base_media_base.h:264
GraphicsSet::CheckMD5
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
Definition gfxinit.cpp:415
GraphicsSet::blitter
BlitterType blitter
Blitter of this graphics set.
Definition base_media_base.h:265
GraphicsSet::GetOrCreateExtraConfig
GRFConfig & GetOrCreateExtraConfig() const
Return configuration for the extra GRF, or lazily create it.
Definition gfxinit.cpp:370
IniFile
Ini file that supports both loading and saving.
Definition ini_type.h:88
MD5File
Structure holding filename and MD5 information about a single file.
Definition base_media_base.h:25
MD5File::missing_warning
std::string missing_warning
warning when this file is missing
Definition base_media_base.h:36
MD5File::ChecksumResult
ChecksumResult
The result of a checksum check.
Definition base_media_base.h:27
MD5File::CR_MATCH
@ CR_MATCH
The file did exist and the md5 checksum did match.
Definition base_media_base.h:29
MD5File::CR_MISMATCH
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
Definition base_media_base.h:30
MD5File::CR_NO_FILE
@ CR_NO_FILE
The file did not exist.
Definition base_media_base.h:31
MD5File::CR_UNKNOWN
@ CR_UNKNOWN
The file has not been checked yet.
Definition base_media_base.h:28
MD5File::check_result
ChecksumResult check_result
cached result of md5 check
Definition base_media_base.h:37
MD5File::CheckMD5
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
Definition gfxinit.cpp:436
MD5File::hash
MD5Hash hash
md5 sum of the file
Definition base_media_base.h:35
MD5File::filename
std::string filename
filename
Definition base_media_base.h:34
MusicSet
All data of a music set.
Definition base_media_base.h:337
MusicSet::songinfo
MusicSongInfo songinfo[NUM_SONGS_AVAILABLE]
Data about individual songs in set.
Definition base_media_base.h:339
MusicSet::num_available
uint8_t num_available
Number of valid songs in set.
Definition base_media_base.h:341
MusicSongInfo
Metadata about a music track.
Definition base_media_base.h:325
MusicSongInfo::filetype
MusicTrackType filetype
decoder required for song file
Definition base_media_base.h:329
MusicSongInfo::songname
std::string songname
name of song displayed in UI
Definition base_media_base.h:326
MusicSongInfo::tracknr
uint8_t tracknr
track number of song displayed in UI
Definition base_media_base.h:327
MusicSongInfo::filename
std::string filename
file on disk containing song (when used in MusicSet class)
Definition base_media_base.h:328
MusicSongInfo::override_start
int override_start
MIDI ticks to skip over in beginning.
Definition base_media_base.h:332
MusicSongInfo::loop
bool loop
song should play in a tight loop if possible, never ending
Definition base_media_base.h:331
MusicSongInfo::cat_index
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
Definition base_media_base.h:330
MusicSongInfo::override_end
int override_end
MIDI tick to end the song at (0 if no override)
Definition base_media_base.h:333
SoundsSet
All data of a sounds set.
Definition base_media_base.h:294
textfile_gui.h
GUI functions related to textfiles.
textfile_type.h
Types related to textfiles.
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition textfile_type.h:14