OpenTTD: src/base_media_base.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 {
28 enum ChecksumResult {
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
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 {
49 typedef SmallMap<const char *, const char *> TranslatedStrings;
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
73 ~BaseSet()
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
144 static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
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>
171 class BaseMedia : FileScanner {
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 {
198 BaseMedia<Tbase_set> fs;
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
233 enum GraphicsFileType {
234 GFT_BASE,
235 GFT_LOGOS,
236 GFT_ARCTIC,
237 GFT_TROPICAL,
238 GFT_TOYLAND,
239 GFT_EXTRA,
240 MAX_GFT,
241 };
242
244 enum BlitterType {
245 BLT_8BPP,
246 BLT_32BPP,
247 };
248
250 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
251 PaletteType palette;
252 BlitterType blitter;
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;
278 static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
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 */