OpenTTD Source: src/base_media_base.h Source File

OpenTTD Source 1.11.0-RC1

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 "core/smallmap_type.hpp"
15 #include "gfx_type.h"
16 #include "textfile_type.h"
17 #include "textfile_gui.h"
18 #include <unordered_map>
19
20 /* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
21 struct IniFile;
22 struct ContentInfo;
23
25 struct MD5File {
27 enum ChecksumResult {
28 CR_UNKNOWN,
29 CR_MATCH,
30 CR_MISMATCH,
31 CR_NO_FILE,
32 };
33
34 const char *filename;
35 uint8 hash[16];
36 const char *missing_warning;
37 ChecksumResult check_result;
38
39 ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
40 };
41
48 template <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 TranslatedStrings description;
63 uint32 shortname;
64 uint32 version;
65 bool fallback;
66
67 MD5File files[NUM_FILES];
68 uint found_files;
69 uint valid_files;
70
71 T *next;
72
74 ~BaseSet()
75 {
76 for (uint i = 0; i < NUM_FILES; i++) {
77 free(this->files[i].filename);
78 free(this->files[i].missing_warning);
79 }
80
81 delete this->next;
82 }
83
88 int GetNumMissing() const
89 {
90 return Tnum_files - this->found_files;
91 }
92
98 int GetNumInvalid() const
99 {
100 return Tnum_files - this->valid_files;
101 }
102
103 bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename = true);
104
113 const char *GetDescription(const std::string &isocode) const
114 {
115 if (!isocode.empty()) {
116 /* First the full ISO code */
117 auto desc = this->description.find(isocode);
118 if (desc != this->description.end()) return desc->second.c_str();
119
120 /* Then the first two characters */
121 desc = this->description.find(isocode.substr(0, 2));
122 if (desc != this->description.end()) return desc->second.c_str();
123 }
124 /* Then fall back */
125 return this->description.at(std::string{}).c_str();
126 }
127
137 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
138 {
139 return file->CheckMD5(subdir, SIZE_MAX);
140 }
141
147 const char *GetTextfile(TextfileType type) const
148 {
149 for (uint i = 0; i < NUM_FILES; i++) {
150 const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename);
151 if (textfile != nullptr) {
152 return textfile;
153 }
154 }
155 return nullptr;
156 }
157 };
158
163 template <class Tbase_set>
164 class BaseMedia : FileScanner {
165 protected:
166 static Tbase_set *available_sets;
167 static Tbase_set *duplicate_sets;
168 static const Tbase_set *used_set;
169
170 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
171
176 static const char *GetExtension();
177 public:
179 static std::string ini_set;
180
186 static bool DetermineBestSet();
187
189 static uint FindSets()
190 {
191 BaseMedia<Tbase_set> fs;
192 /* Searching in tars is only done in the old "data" directories basesets. */
193 uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? OLD_DATA_DIR : OLD_GM_DIR, Tbase_set::SEARCH_IN_TARS);
194 return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
195 }
196
197 static Tbase_set *GetAvailableSets();
198
199 static bool SetSet(const std::string &name);
200 static char *GetSetsList(char *p, const char *last);
201 static int GetNumSets();
202 static int GetIndexOfUsedSet();
203 static const Tbase_set *GetSet(int index);
204 static const Tbase_set *GetUsedSet();
205
212 static bool HasSet(const ContentInfo *ci, bool md5sum);
213 };
214
215 template <class Tbase_set> /* static */ std::string BaseMedia<Tbase_set>::ini_set;
216 template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
217 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
218 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
219
227 template <class Tbase_set>
228 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
229
231 enum GraphicsFileType {
232 GFT_BASE,
233 GFT_LOGOS,
234 GFT_ARCTIC,
235 GFT_TROPICAL,
236 GFT_TOYLAND,
237 GFT_EXTRA,
238 MAX_GFT,
239 };
240
242 enum BlitterType {
243 BLT_8BPP,
244 BLT_32BPP,
245 };
246
248 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
249 PaletteType palette;
250 BlitterType blitter;
251
252 bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
253
254 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
255 };
256
258 class BaseGraphics : public BaseMedia<GraphicsSet> {
259 public:
260 };
261
263 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
264 };
265
267 class BaseSounds : public BaseMedia<SoundsSet> {
268 public:
269 };
270
272 static const uint NUM_SONGS_CLASS = 10;
274 static const uint NUM_SONG_CLASSES = 3;
276 static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
277
279 static const uint NUM_SONGS_PLAYLIST = 32;
280
281 /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
282 char *GetMusicCatEntryName(const char *filename, size_t entrynum);
283 byte *GetMusicCatEntryData(const char *filename, size_t entrynum, size_t &entrylen);
284
285 enum MusicTrackType {
286 MTT_STANDARDMIDI,
287 MTT_MPSMIDI,
288 };
289
291 struct MusicSongInfo {
292 char songname[32];
293 byte tracknr;
294 const char *filename;
295 MusicTrackType filetype;
296 int cat_index;
297 bool loop;
298 int override_start;
299 int override_end;
300 };
301
303 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
305 MusicSongInfo songinfo[NUM_SONGS_AVAILABLE];
307 byte num_available;
308
309 bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
310 };
311
313 class BaseMusic : public BaseMedia<MusicSet> {
314 public:
315 };
316
317 #endif /* BASE_MEDIA_BASE_H */
BaseMedia::GetAvailableSets
static Tbase_set * GetAvailableSets()
Return the available sets.
Definition: base_media_func.h:367
BaseSet::GetTextfile
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
Definition: base_media_base.h:147
BaseMedia::DetermineBestSet
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
Definition: gfxinit.cpp:426
MusicSongInfo::tracknr
byte tracknr
track number of song displayed in UI
Definition: base_media_base.h:293
SoundsSet
All data of a sounds set.
Definition: base_media_base.h:263
BaseMedia::GetIndexOfUsedSet
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
Definition: base_media_func.h:326
BaseSet::next
T * next
The next base set in this list.
Definition: base_media_base.h:71
GFT_EXTRA
@ GFT_EXTRA
Extra sprites that were not part of the original sprites.
Definition: base_media_base.h:237
BaseSet::GetDescription
const char * GetDescription(const std::string &isocode) const
Get the description for the given ISO code.
Definition: base_media_base.h:113
BLT_8BPP
@ BLT_8BPP
Base set has 8 bpp sprites only.
Definition: base_media_base.h:243
MD5File::CR_MISMATCH
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
Definition: base_media_base.h:30
MD5File::ChecksumResult
ChecksumResult
The result of a checksum check.
Definition: base_media_base.h:27
BaseSet::shortname
uint32 shortname
Four letter short variant of the name.
Definition: base_media_base.h:63
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
BaseSet::name
std::string name
The name of the base set.
Definition: base_media_base.h:61
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:279
MusicTrackType
MusicTrackType
Definition: base_media_base.h:285
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:276
MusicSongInfo::override_start
int override_start
MIDI ticks to skip over in beginning.
Definition: base_media_base.h:298
GFT_BASE
@ GFT_BASE
Base sprites for all climates.
Definition: base_media_base.h:232
BaseSet::NUM_FILES
static const size_t NUM_FILES
Number of files in this set.
Definition: base_media_base.h:53
PaletteType
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:293
BaseSet::description
TranslatedStrings description
Description of the base set.
Definition: base_media_base.h:62
FileScanner::Scan
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1367
MusicSongInfo::loop
bool loop
song should play in a tight loop if possible, never ending
Definition: base_media_base.h:297
fileio_func.h
BaseSet::found_files
uint found_files
Number of the files that could be found.
Definition: base_media_base.h:68
BaseSet::version
uint32 version
The version of this base set.
Definition: base_media_base.h:64
smallmap_type.hpp
BaseMedia::GetSetsList
static char * GetSetsList(char *p, const char *last)
Returns a list with the sets.
Definition: base_media_func.h:255
MD5File::check_result
ChecksumResult check_result
cached result of md5 check
Definition: base_media_base.h:37
BLT_32BPP
@ BLT_32BPP
Base set has both 8 bpp and 32 bpp sprites.
Definition: base_media_base.h:244
BaseGraphics
All data/functions related with replacing the base graphics.
Definition: base_media_base.h:258
OLD_GM_DIR
@ OLD_GM_DIR
Old subdirectory for the music.
Definition: fileio_type.h:114
MusicSongInfo::override_end
int override_end
MIDI tick to end the song at (0 if no override)
Definition: base_media_base.h:299
textfile_gui.h
BlitterType
BlitterType
Blitter type for base graphics sets.
Definition: base_media_base.h:242
BaseMedia::ini_set
static std::string ini_set
The set as saved in the config file.
Definition: base_media_base.h:179
MD5File::CR_MATCH
@ CR_MATCH
The file did exist and the md5 checksum did match.
Definition: base_media_base.h:29
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:65
textfile_type.h
MD5File::missing_warning
const char * missing_warning
warning when this file is missing
Definition: base_media_base.h:36
MD5File::hash
uint8 hash[16]
md5 sum of the file
Definition: base_media_base.h:35
BaseMedia::available_sets
static Tbase_set * available_sets
All available sets.
Definition: base_media_base.h:166
BaseSet::file_names
static const char *const * file_names
Internal names of the files in this set.
Definition: base_media_base.h:59
MusicSongInfo::songname
char songname[32]
name of song displayed in UI
Definition: base_media_base.h:292
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content.h:54
BaseMedia
Base for all base media (graphics, sounds)
Definition: base_media_base.h:164
MusicSongInfo
Metadata about a music track.
Definition: base_media_base.h:291
MTT_STANDARDMIDI
@ MTT_STANDARDMIDI
Standard MIDI file.
Definition: base_media_base.h:286
MTT_MPSMIDI
@ MTT_MPSMIDI
MPS GM driver MIDI format (contained in a CAT file)
Definition: base_media_base.h:287
MD5File::CheckMD5
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
Definition: gfxinit.cpp:393
MusicSet
All data of a music set.
Definition: base_media_base.h:303
MusicSet::songinfo
MusicSongInfo songinfo[NUM_SONGS_AVAILABLE]
Data about individual songs in set.
Definition: base_media_base.h:305
GraphicsSet::blitter
BlitterType blitter
Blitter of this graphics set.
Definition: base_media_base.h:250
BaseSet::valid_files
uint valid_files
Number of the files that could be found and are valid.
Definition: base_media_base.h:69
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:153
NUM_SONGS_CLASS
static const uint NUM_SONGS_CLASS
Maximum number of songs in the 'class' playlists.
Definition: base_media_base.h:272
BaseSet::GetNumMissing
int GetNumMissing() const
Get the number of missing files.
Definition: base_media_base.h:88
BaseMedia::used_set
static const Tbase_set * used_set
The currently used set.
Definition: base_media_base.h:168
GFT_TOYLAND
@ GFT_TOYLAND
Landscape replacement sprites for toyland.
Definition: base_media_base.h:236
GFT_LOGOS
@ GFT_LOGOS
Logos, landscape icons and original terrain generator sprites.
Definition: base_media_base.h:233
BaseMedia::GetNumSets
static int GetNumSets()
Count the number of available graphics sets.
Definition: base_media_func.h:311
GFT_TROPICAL
@ GFT_TROPICAL
Landscape replacement sprites for tropical.
Definition: base_media_base.h:235
GFT_ARCTIC
@ GFT_ARCTIC
Landscape replacement sprites for arctic.
Definition: base_media_base.h:234
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:88
GetMusicCatEntryData
byte * GetMusicCatEntryData(const char *filename, size_t entrynum, size_t &entrylen)
Read the full data of a music CAT file entry.
Definition: music.cpp:55
MD5File
Structure holding filename and MD5 information about a single file.
Definition: base_media_base.h:25
NUM_SONGS_PLAYLIST
static const uint NUM_SONGS_PLAYLIST
Maximum number of songs in the (custom) playlist.
Definition: base_media_base.h:279
GraphicsSet::CheckMD5
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
Definition: gfxinit.cpp:370
NUM_SONG_CLASSES
static const uint NUM_SONG_CLASSES
Number of classes for songs.
Definition: base_media_base.h:274
GetMusicCatEntryName
char * GetMusicCatEntryName(const char *filename, size_t entrynum)
Read the name of a music CAT file entry.
Definition: music.cpp:28
GraphicsSet::palette
PaletteType palette
Palette of this graphics set.
Definition: base_media_base.h:249
BaseMusic
All data/functions related with replacing the base music.
Definition: base_media_base.h:313
BaseMedia::FindSets
static uint FindSets()
Do the scan for files.
Definition: base_media_base.h:189
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:300
BaseSet
Information about a single base set.
Definition: base_media_base.h:49
MD5File::CR_NO_FILE
@ CR_NO_FILE
The file did not exist.
Definition: base_media_base.h:31
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
BaseSet::~BaseSet
~BaseSet()
Free everything we allocated.
Definition: base_media_base.h:74
MD5File::CR_UNKNOWN
@ CR_UNKNOWN
The file has not been checked yet.
Definition: base_media_base.h:28
GraphicsFileType
GraphicsFileType
Types of graphics in the base graphics set.
Definition: base_media_base.h:231
MD5File::filename
const char * filename
filename
Definition: base_media_base.h:34
MusicSongInfo::filename
const char * filename
file on disk containing song (when used in MusicSet class, this pointer is owned by MD5File object fo...
Definition: base_media_base.h:294
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:342
BaseSet::SEARCH_IN_TARS
static const bool SEARCH_IN_TARS
Whether to search in the tars or not.
Definition: base_media_base.h:56
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:59
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
BaseMedia::GetUsedSet
static const Tbase_set * GetUsedSet()
Return the used set.
Definition: base_media_func.h:357
MusicSet::num_available
byte num_available
Number of valid songs in set.
Definition: base_media_base.h:307
BaseSounds
All data/functions related with replacing the base sounds.
Definition: base_media_base.h:267
OLD_DATA_DIR
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition: fileio_type.h:115
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:167
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
gfx_type.h
BaseSet::GetNumInvalid
int GetNumInvalid() const
Get the number of invalid files.
Definition: base_media_base.h:98
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:137
BaseMedia::GetExtension
static const char * GetExtension()
Get the extension that is used to identify this set.
Definition: gfxinit.cpp:450
BaseSet::FillSetDetails
bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
Definition: base_media_func.h:39
GraphicsSet
All data of a graphics set.
Definition: base_media_base.h:248
MusicSongInfo::cat_index
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
Definition: base_media_base.h:296
MAX_GFT
@ MAX_GFT
We are looking for this amount of GRFs.
Definition: base_media_base.h:238
BaseSet::files
MD5File files[NUM_FILES]
All files part of this set.
Definition: base_media_base.h:67
MusicSongInfo::filetype
MusicTrackType filetype
decoder required for song file
Definition: base_media_base.h:295
BaseMedia::SetSet
static bool SetSet(const std::string &name)
Set the set to be used.
Definition: base_media_func.h:228