OpenTTD
base_media_base.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef BASE_MEDIA_BASE_H
13 #define BASE_MEDIA_BASE_H
14 
15 #include "fileio_func.h"
16 #include "core/smallmap_type.hpp"
17 #include "gfx_type.h"
18 #include "textfile_type.h"
19 #include "textfile_gui.h"
20 
21 /* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
22 struct IniFile;
23 struct ContentInfo;
24 
26 struct MD5File {
33  };
34 
35  const char *filename;
36  uint8 hash[16];
37  const char *missing_warning;
39 
40  ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
41 };
42 
49 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
50 struct BaseSet {
52 
54  static const size_t NUM_FILES = Tnum_files;
55 
57  static const bool SEARCH_IN_TARS = Tsearch_in_tars;
58 
60  static const char * const *file_names;
61 
62  const char *name;
63  TranslatedStrings description;
64  uint32 shortname;
65  uint32 version;
66  bool fallback;
67 
68  MD5File files[NUM_FILES];
69  uint found_files;
70  uint valid_files;
71 
72  T *next;
73 
76  {
77  free(this->name);
78 
79  for (TranslatedStrings::iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
80  free(iter->first);
81  free(iter->second);
82  }
83 
84  for (uint i = 0; i < NUM_FILES; i++) {
85  free(this->files[i].filename);
86  free(this->files[i].missing_warning);
87  }
88 
89  delete this->next;
90  }
91 
96  int GetNumMissing() const
97  {
98  return Tnum_files - this->found_files;
99  }
100 
106  int GetNumInvalid() const
107  {
108  return Tnum_files - this->valid_files;
109  }
110 
111  bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename = true);
112 
121  const char *GetDescription(const char *isocode = NULL) const
122  {
123  if (isocode != NULL) {
124  /* First the full ISO code */
125  for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
126  if (strcmp(iter->first, isocode) == 0) return iter->second;
127  }
128  /* Then the first two characters */
129  for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
130  if (strncmp(iter->first, isocode, 2) == 0) return iter->second;
131  }
132  }
133  /* Then fall back */
134  return this->description.Begin()->second;
135  }
136 
147  {
148  return file->CheckMD5(subdir, SIZE_MAX);
149  }
150 
156  const char *GetTextfile(TextfileType type) const
157  {
158  for (uint i = 0; i < NUM_FILES; i++) {
159  const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename);
160  if (textfile != NULL) {
161  return textfile;
162  }
163  }
164  return NULL;
165  }
166 };
167 
172 template <class Tbase_set>
174 protected:
175  static Tbase_set *available_sets;
176  static Tbase_set *duplicate_sets;
177  static const Tbase_set *used_set;
178 
179  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
180 
185  static const char *GetExtension();
186 public:
188  static const char *ini_set;
189 
195  static bool DetermineBestSet();
196 
198  static uint FindSets()
199  {
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 char *name);
209  static char *GetSetsList(char *p, const char *last);
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 
224 template <class Tbase_set> /* static */ const char *BaseMedia<Tbase_set>::ini_set;
225 template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
226 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
227 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
228 
236 template <class Tbase_set>
237 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
238 
248 };
249 
254 };
255 
257 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
260 
261  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
262 
263  static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
264 };
265 
267 class BaseGraphics : public BaseMedia<GraphicsSet> {
268 public:
269 };
270 
272 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
273 };
274 
276 class BaseSounds : public BaseMedia<SoundsSet> {
277 public:
278 };
279 
281 static const uint NUM_SONGS_CLASS = 10;
283 static const uint NUM_SONG_CLASSES = 3;
286 
288 static const uint NUM_SONGS_PLAYLIST = 32;
289 
290 /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
291 char *GetMusicCatEntryName(const char *filename, size_t entrynum);
292 byte *GetMusicCatEntryData(const char *filename, size_t entrynum, size_t &entrylen);
293 
297 };
298 
301  char songname[32];
302  byte tracknr;
303  const char *filename;
305  int cat_index;
306  bool loop;
309 };
310 
312 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
317 
318  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
319 };
320 
322 class BaseMusic : public BaseMedia<MusicSet> {
323 public:
324 };
325 
326 #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:116
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:110
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:57
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:1419
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.
const T * Begin() const
Get the pointer to the first item (const)
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:118
int GetNumMissing() const
Get the number of missing files.
const T * End() const
Get the pointer behind the last valid item (const)
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:74
Simple mapping class targeted for small sets of data.
Landscape replacement sprites for arctic.
All data of a music set.
const char * filename
filename
Simple pair of data.
const char * GetDescription(const char *isocode=NULL) const
Get the description for the given ISO code.
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:16
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:402
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.
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:88
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:289
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:114
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:58
char * GetMusicCatEntryName(const char *filename, size_t entrynum)
Read the name of a music CAT file entry.
Definition: music.cpp:30
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:117
GUI functions related to textfiles.
static const char * ini_set
The set as saved in the config file.