base_media_base.h

Go to the documentation of this file.
00001 /* $Id: base_media_base.h 19741 2010-04-30 21:01:21Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef BASE_MEDIA_BASE_H
00013 #define BASE_MEDIA_BASE_H
00014 
00015 #include "fileio_func.h"
00016 #include "core/smallmap_type.hpp"
00017 #include "gfx_type.h"
00018 
00019 /* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
00020 struct IniFile;
00021 struct ContentInfo;
00022 
00024 struct MD5File {
00026   enum ChecksumResult {
00027     CR_MATCH,    
00028     CR_MISMATCH, 
00029     CR_NO_FILE,  
00030   };
00031 
00032   const char *filename;        
00033   uint8 hash[16];              
00034   const char *missing_warning; 
00035 
00036   ChecksumResult CheckMD5(Subdirectory subdir) const;
00037 };
00038 
00045 template <class T, size_t Tnum_files, Subdirectory Tsubdir>
00046 struct BaseSet {
00047   typedef SmallMap<const char *, const char *> TranslatedStrings;
00048 
00050   static const size_t NUM_FILES = Tnum_files;
00051 
00053   static const Subdirectory SUBDIR = Tsubdir;
00054 
00056   static const char * const *file_names;
00057 
00058   const char *name;              
00059   TranslatedStrings description; 
00060   uint32 shortname;              
00061   uint32 version;                
00062   bool fallback;                 
00063 
00064   MD5File files[NUM_FILES];      
00065   uint found_files;              
00066   uint valid_files;              
00067 
00068   T *next;                       
00069 
00071   ~BaseSet()
00072   {
00073     free((void*)this->name);
00074 
00075     for (TranslatedStrings::iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
00076       free((void*)iter->first);
00077       free((void*)iter->second);
00078     }
00079 
00080     for (uint i = 0; i < NUM_FILES; i++) {
00081       free((void*)this->files[i].filename);
00082       free((void*)this->files[i].missing_warning);
00083     }
00084 
00085     delete this->next;
00086   }
00087 
00092   int GetNumMissing() const
00093   {
00094     return Tnum_files - this->found_files;
00095   }
00096 
00102   int GetNumInvalid() const
00103   {
00104     return Tnum_files - this->valid_files;
00105   }
00106 
00115   bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename = true);
00116 
00125   const char *GetDescription(const char *isocode = NULL) const
00126   {
00127     if (isocode != NULL) {
00128       /* First the full ISO code */
00129       for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
00130         if (strcmp(iter->first, isocode) == 0) return iter->second;
00131       }
00132       /* Then the first two characters */
00133       for (TranslatedStrings::const_iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
00134         if (strncmp(iter->first, isocode, 2) == 0) return iter->second;
00135       }
00136     }
00137     /* Then fall back */
00138     return this->description.Begin()->second;
00139   }
00140 };
00141 
00146 template <class Tbase_set>
00147 class BaseMedia : FileScanner {
00148 protected:
00149   static Tbase_set *available_sets; 
00150   static const Tbase_set *used_set; 
00151 
00152   /* virtual */ bool AddFile(const char *filename, size_t basepath_length);
00153 
00158   static const char *GetExtension();
00159 public:
00161   static const char *ini_set;
00162 
00168   static bool DetermineBestSet();
00169 
00171   static uint FindSets()
00172   {
00173     BaseMedia<Tbase_set> fs;
00174     /* GM_DIR == music set. Music sets don't support tars,
00175      * so there is no need to search for tars in that case. */
00176     return fs.Scan(GetExtension(), Tbase_set::SUBDIR, Tbase_set::SUBDIR != GM_DIR);
00177   }
00178 
00184   static bool SetSet(const char *name);
00185 
00192   static char *GetSetsList(char *p, const char *last);
00193 
00198   static int GetNumSets();
00199 
00204   static int GetIndexOfUsedSet();
00205 
00210   static const Tbase_set *GetSet(int index);
00215   static const Tbase_set *GetUsedSet();
00216 
00223   static bool HasSet(const ContentInfo *ci, bool md5sum);
00224 };
00225 
00226 
00228 enum GraphicsFileType {
00229   GFT_BASE,     
00230   GFT_LOGOS,    
00231   GFT_ARCTIC,   
00232   GFT_TROPICAL, 
00233   GFT_TOYLAND,  
00234   GFT_EXTRA,    
00235   MAX_GFT       
00236 };
00237 
00239 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, DATA_DIR> {
00240   PaletteType palette;       
00241 
00242   bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
00243 };
00244 
00246 class BaseGraphics : public BaseMedia<GraphicsSet> {
00247 public:
00251   static void DeterminePalette();
00252 };
00253 
00255 struct SoundsSet : BaseSet<SoundsSet, 1, DATA_DIR> {
00256 };
00257 
00259 class BaseSounds : public BaseMedia<SoundsSet> {
00260 public:
00261 };
00262 
00264 static const uint NUM_SONGS_CLASS     = 10;
00266 static const uint NUM_SONG_CLASSES    = 3;
00268 static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
00269 
00271 static const uint NUM_SONGS_PLAYLIST  = 32;
00272 
00274 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, GM_DIR> {
00276   char song_name[NUM_SONGS_AVAILABLE][32];
00277   byte track_nr[NUM_SONGS_AVAILABLE];
00278   byte num_available;
00279 
00280   bool FillSetDetails(struct IniFile *ini, const char *path, const char *full_filename);
00281 };
00282 
00284 class BaseMusic : public BaseMedia<MusicSet> {
00285 public:
00286 };
00287 
00288 #endif /* BASE_MEDIA_BASE_H */

Generated on Fri Apr 30 21:55:18 2010 for OpenTTD by  doxygen 1.6.1