OpenTTD
base_media_base.h
Go to the documentation of this file.
1 /* $Id: base_media_base.h 24487 2012-08-20 21:01:40Z yexo $ */
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 {
32  };
33 
34  const char *filename;
35  uint8 hash[16];
36  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 (TranslatedStrings::iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
78  free(iter->first);
79  free(iter->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 = NULL) const
120  {
121  if (isocode != NULL) {
122  /* First the full ISO code */
123  for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
124  if (strcmp(iter->first, isocode) == 0) return iter->second;
125  }
126  /* Then the first two characters */
127  for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
128  if (strncmp(iter->first, isocode, 2) == 0) return iter->second;
129  }
130  }
131  /* Then fall back */
132  return this->description.Begin()->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 != NULL) {
159  return textfile;
160  }
161  }
162  return NULL;
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  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
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 
229 template <class Tbase_set>
230 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
231 
241 };
242 
247 };
248 
250 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
253 
254  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
255 
256  static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
257 };
258 
260 class BaseGraphics : public BaseMedia<GraphicsSet> {
261 public:
262 };
263 
265 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
266 };
267 
269 class BaseSounds : public BaseMedia<SoundsSet> {
270 public:
271 };
272 
274 static const uint NUM_SONGS_CLASS = 10;
276 static const uint NUM_SONG_CLASSES = 3;
279 
281 static const uint NUM_SONGS_PLAYLIST = 32;
282 
284 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
286  char song_name[NUM_SONGS_AVAILABLE][32];
287  byte track_nr[NUM_SONGS_AVAILABLE];
288  byte num_available;
289 
290  bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
291 };
292 
294 class BaseMusic : public BaseMedia<MusicSet> {
295 public:
296 };
297 
298 #endif /* BASE_MEDIA_BASE_H */
Old subdirectory for the music.
Definition: fileio_type.h:116
uint32 shortname
Four letter short variant of the name.
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.
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:1428
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
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:73
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.
Extra sprites that were not part of the original sprites.
Landscape replacement sprites for tropical.
All data/functions related with replacing the base graphics.
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:399
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
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.
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.
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.
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Types related to textfiles.
Container for all important information about a piece of content.
Definition: tcp_content.h:58
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.