OpenTTD
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 
19 /* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
20 struct IniFile;
21 struct ContentInfo;
22 
24 struct MD5File {
31  };
32 
33  const char *filename;
34  uint8 hash[16];
35  const char *missing_warning;
37 
38  ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
39 };
40 
47 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
48 struct BaseSet {
50 
52  static const size_t NUM_FILES = Tnum_files;
53 
55  static const bool SEARCH_IN_TARS = Tsearch_in_tars;
56 
58  static const char * const *file_names;
59 
60  const char *name;
61  TranslatedStrings description;
62  uint32 shortname;
63  uint32 version;
64  bool fallback;
65 
66  MD5File files[NUM_FILES];
67  uint found_files;
68  uint valid_files;
69 
70  T *next;
71 
74  {
75  free(this->name);
76 
77  for (auto &pair : this->description) {
78  free(pair.first);
79  free(pair.second);
80  }
81 
82  for (uint i = 0; i < NUM_FILES; i++) {
83  free(this->files[i].filename);
84  free(this->files[i].missing_warning);
85  }
86 
87  delete this->next;
88  }
89 
94  int GetNumMissing() const
95  {
96  return Tnum_files - this->found_files;
97  }
98 
104  int GetNumInvalid() const
105  {
106  return Tnum_files - this->valid_files;
107  }
108 
109  bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename = true);
110 
119  const char *GetDescription(const char *isocode = nullptr) const
120  {
121  if (isocode != nullptr) {
122  /* First the full ISO code */
123  for (const auto &pair : this->description) {
124  if (strcmp(pair.first, isocode) == 0) return pair.second;
125  }
126  /* Then the first two characters */
127  for (const auto &pair : this->description) {
128  if (strncmp(pair.first, isocode, 2) == 0) return pair.second;
129  }
130  }
131  /* Then fall back */
132  return this->description.front().second;
133  }
134 
145  {
146  return file->CheckMD5(subdir, SIZE_MAX);
147  }
148 
154  const char *GetTextfile(TextfileType type) const
155  {
156  for (uint i = 0; i < NUM_FILES; i++) {
157  const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename);
158  if (textfile != nullptr) {
159  return textfile;
160  }
161  }
162  return nullptr;
163  }
164 };
165 
170 template <class Tbase_set>
172 protected:
173  static Tbase_set *available_sets;
174  static Tbase_set *duplicate_sets;
175  static const Tbase_set *used_set;
176 
177  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
178 
183  static const char *GetExtension();
184 public:
186  static const char *ini_set;
187 
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 char *name);
207  static char *GetSetsList(char *p, const char *last);
208  static int GetNumSets();
209  static int GetIndexOfUsedSet();
210  static const Tbase_set *GetSet(int index);
211  static const Tbase_set *GetUsedSet();
212 
219  static bool HasSet(const ContentInfo *ci, bool md5sum);
220 };
221 
222 template <class Tbase_set> /* static */ const char *BaseMedia<Tbase_set>::ini_set;
223 template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
224 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
225 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
226 
234 template <class Tbase_set>
235 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
236 
246 };
247 
252 };
253 
255 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
258 
259  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
260 
261  static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
262 };
263 
265 class BaseGraphics : public BaseMedia<GraphicsSet> {
266 public:
267 };
268 
270 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
271 };
272 
274 class BaseSounds : public BaseMedia<SoundsSet> {
275 public:
276 };
277 
279 static const uint NUM_SONGS_CLASS = 10;
281 static const uint NUM_SONG_CLASSES = 3;
284 
286 static const uint NUM_SONGS_PLAYLIST = 32;
287 
288 /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
289 char *GetMusicCatEntryName(const char *filename, size_t entrynum);
290 byte *GetMusicCatEntryData(const char *filename, size_t entrynum, size_t &entrylen);
291 
295 };
296 
299  char songname[32];
300  byte tracknr;
301  const char *filename;
303  int cat_index;
304  bool loop;
307 };
308 
310 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
315 
316  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
317 };
318 
320 class BaseMusic : public BaseMedia<MusicSet> {
321 public:
322 };
323 
324 #endif /* BASE_MEDIA_BASE_H */
int override_end
MIDI tick to end the song at (0 if no override)
Metadata about a music track.
Standard MIDI file.
Old subdirectory for the music.
Definition: fileio_type.h:114
uint32 shortname
Four letter short variant of the name.
ChecksumResult check_result
cached result of md5 check
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
static const uint NUM_SONGS_PLAYLIST
Maximum number of songs in the (custom) playlist.
static const char *const * file_names
Internal names of the files in this set.
static const Tbase_set * used_set
The currently used set.
static uint FindSets()
Do the scan for files.
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
Structure holding filename and MD5 information about a single file.
int GetNumInvalid() const
Get the number of invalid files.
The file did not exist.
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:1373
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
int override_start
MIDI ticks to skip over in beginning.
Base set has both 8 bpp and 32 bpp sprites.
PaletteType palette
Palette of this graphics set.
Functions for Standard In/Out file operations.
TranslatedStrings description
Description of the base set.
All data/functions related with replacing the base sounds.
We are looking for this amount of GRFs.
Base set has 8 bpp sprites only.
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
int GetNumMissing() const
Get the number of missing files.
Base sprites for all climates.
The file did exist, just the md5 checksum did not match.
~BaseSet()
Free everything we allocated.
const char * missing_warning
warning when this file is missing
Helper for scanning for files with a given name.
Definition: fileio_func.h:70
Simple mapping class targeted for small sets of data.
Landscape replacement sprites for arctic.
All data of a music set.
const char * filename
filename
static const uint NUM_SONGS_CLASS
Maximum number of songs in the &#39;class&#39; playlists.
const char * name
The name of the base set.
MusicTrackType
Extra sprites that were not part of the original sprites.
MPS GM driver MIDI format (contained in a CAT file)
Landscape replacement sprites for tropical.
All data/functions related with replacing the base graphics.
const char * filename
file on disk containing song (when used in MusicSet class, this pointer is owned by MD5File object fo...
Landscape replacement sprites for toyland.
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
BlitterType blitter
Blitter of this graphics set.
All data/functions related with replacing the base music.
uint32 version
The version of this base set.
Base for all base media (graphics, sounds)
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
Definition: gfxinit.cpp:400
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
static const uint NUM_SONG_CLASSES
Number of classes for songs.
const char * GetDescription(const char *isocode=nullptr) const
Get the description for the given ISO code.
uint found_files
Number of the files that could be found.
GraphicsFileType
Types of graphics in the base graphics set.
Ini file that supports both loading and saving.
Definition: ini_type.h:86
byte tracknr
track number of song displayed in UI
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
uint valid_files
Number of the files that could be found and are valid.
The file did exist and the md5 checksum did match.
BlitterType
Blitter type for base graphics sets.
static Tbase_set * available_sets
All available sets.
All data of a sounds set.
All data of a graphics set.
bool loop
song should play in a tight loop if possible, never ending
ChecksumResult
The result of a checksum check.
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there&#39;s a base set matching some information.
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:288
Information about a single base set.
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
The file has not been checked yet.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
Logos, landscape icons and original terrain generator sprites.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
MusicTrackType filetype
decoder required for song file
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
byte num_available
Number of valid songs in set.
Types related to textfiles.
Container for all important information about a piece of content.
Definition: tcp_content.h:54
char * GetMusicCatEntryName(const char *filename, size_t entrynum)
Read the name of a music CAT file entry.
Definition: music.cpp:28
Types related to the graphics and/or input devices.
static Tbase_set * duplicate_sets
All sets that aren&#39;t available, but needed for not downloading base sets when a newer version than th...
uint8 hash[16]
md5 sum of the file
Old subdirectory for the data.
Definition: fileio_type.h:115
GUI functions related to textfiles.
static const char * ini_set
The set as saved in the config file.