# music.cpp

Go to the documentation of this file.

    00001 /* $Id: music.cpp 19223 2010-02-23 23:26:37Z 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 #include "stdafx.h"
    00013
    00014 /* The type of set we're replacing */
    00015 #define SET_TYPE "music"
    00016 #include "base_media_func.h"
    00017
    00018 INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet)
    00019
    00020
    00021 static const char * const _music_file_names[] = {
    00022   "theme",
    00023   "old_0", "old_1", "old_2", "old_3", "old_4", "old_5", "old_6", "old_7", "old_8", "old_9",
    00024   "new_0", "new_1", "new_2", "new_3", "new_4", "new_5", "new_6", "new_7", "new_8", "new_9",
    00025   "ezy_0", "ezy_1", "ezy_2", "ezy_3", "ezy_4", "ezy_5", "ezy_6", "ezy_7", "ezy_8", "ezy_9",
    00026 };
    00027 assert_compile(lengthof(_music_file_names) == NUM_SONGS_AVAILABLE);
    00028
    00029 template <class T, size_t Tnum_files, Subdirectory Tsubdir>
    00030 /* static */ const char * const *BaseSet<T, Tnum_files, Tsubdir>::file_names = _music_file_names;
    00031
    00032 template <class Tbase_set>
    00033 /* static */ const char *BaseMedia<Tbase_set>::GetExtension()
    00034 {
    00035   return ".obm"; // OpenTTD Base Music
    00036 }
    00037
    00038 template <class Tbase_set>
    00039 /* static */ bool BaseMedia<Tbase_set>::DetermineBestSet()
    00040 {
    00041   if (BaseMedia<Tbase_set>::used_set != NULL) return true;
    00042
    00043   const Tbase_set *best = NULL;
    00044   for (const Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
    00045     if (c->GetNumMissing() != 0) continue;
    00046
    00047     if (best == NULL ||
    00048         (best->fallback && !c->fallback) ||
    00049         best->valid_files < c->valid_files ||
    00050         (best->valid_files == c->valid_files &&
    00051           (best->shortname == c->shortname && best->version < c->version))) {
    00052       best = c;
    00053     }
    00054   }
    00055
    00056   BaseMedia<Tbase_set>::used_set = best;
    00057   return BaseMedia<Tbase_set>::used_set != NULL;
    00058 }
    00059
    00060 bool MusicSet::FillSetDetails(IniFile *ini, const char *path)
    00061 {
    00062   bool ret = this->BaseSet<MusicSet, NUM_SONGS_AVAILABLE, GM_DIR>::FillSetDetails(ini, path);
    00063   if (ret) {
    00064     this->num_available = 0;
    00065     IniGroup *names = ini->GetGroup("names");
    00066     for (uint i = 0, j = 1; i < lengthof(this->song_name); i++) {
    00067       const char *filename = this->files[i].filename;
    00068       if (names == NULL || StrEmpty(filename)) {
    00069         this->song_name[i][0] = '\0';
    00070         continue;
    00071       }
    00072
    00073       IniItem *item = NULL;
    00074       /* As we possibly add a path to the filename and we compare
    00075        * on the filename with the path as in the .obm, we need to
    00076        * keep stripping path elements until we find a match. */
    00077       for (const char *p = filename; p != NULL; p = strchr(p, PATHSEPCHAR)) {
    00078         /* Remove possible double path separator characters from
    00079          * the beginning, so we don't start reading e.g. root. */
    00080         while (*p == PATHSEPCHAR) p++;
    00081
    00082         item = names->GetItem(p, false);
    00083         if (item != NULL && !StrEmpty(item->value)) break;
    00084       }
    00085
    00086       if (item == NULL || StrEmpty(item->value)) {
    00087         DEBUG(grf, 0, "Base music set song name missing: %s", filename);
    00088         return false;
    00089       }
    00090
    00091       strecpy(this->song_name[i], item->value, lastof(this->song_name[i]));
    00092       this->track_nr[i] = j++;
    00093       this->num_available++;
    00094     }
    00095   }
    00096   return true;
    00097 }