OpenTTD

music.cpp
Go to the documentation of this file.
1 /* $Id: music.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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 #include "stdafx.h"
13
15 #define SET_TYPE "music"
16 #include "base_media_func.h"
17
18 #include "safeguards.h"
19
20 INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet)
21
22
23 static const char * const _music_file_names[] = {
24 "theme",
25 "old_0", "old_1", "old_2", "old_3", "old_4", "old_5", "old_6", "old_7", "old_8", "old_9",
26 "new_0", "new_1", "new_2", "new_3", "new_4", "new_5", "new_6", "new_7", "new_8", "new_9",
27 "ezy_0", "ezy_1", "ezy_2", "ezy_3", "ezy_4", "ezy_5", "ezy_6", "ezy_7", "ezy_8", "ezy_9",
28 };
30 assert_compile(lengthof(_music_file_names) == NUM_SONGS_AVAILABLE);
31
32 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
33 /* static */ const char * const *BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names = _music_file_names;
34
35 template <class Tbase_set>
36 /* static */ const char *BaseMedia<Tbase_set>::GetExtension()
37 {
38 return ".obm"; // OpenTTD Base Music
39 }
40
41 template <class Tbase_set>
42 /* static */ bool BaseMedia<Tbase_set>::DetermineBestSet()
43 {
44 if (BaseMedia<Tbase_set>::used_set != NULL) return true;
45
46 const Tbase_set *best = NULL;
47 for (const Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
48 if (c->GetNumMissing() != 0) continue;
49
50 if (best == NULL ||
51 (best->fallback && !c->fallback) ||
52 best->valid_files < c->valid_files ||
53 (best->valid_files == c->valid_files &&
54 (best->shortname == c->shortname && best->version < c->version))) {
55 best = c;
56 }
57 }
58
59 BaseMedia<Tbase_set>::used_set = best;
60 return BaseMedia<Tbase_set>::used_set != NULL;
61 }
62
63 bool MusicSet::FillSetDetails(IniFile *ini, const char *path, const char *full_filename)
64 {
65 bool ret = this->BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false>::FillSetDetails(ini, path, full_filename);
66 if (ret) {
67 this->num_available = 0;
68 IniGroup *names = ini->GetGroup("names");
69 for (uint i = 0, j = 1; i < lengthof(this->song_name); i++) {
70 const char *filename = this->files[i].filename;
71 if (names == NULL || StrEmpty(filename)) {
72 this->song_name[i][0] = '\0';
73 continue;
74 }
75
76 IniItem *item = NULL;
77 /* As we possibly add a path to the filename and we compare
78 * on the filename with the path as in the .obm, we need to
79 * keep stripping path elements until we find a match. */
80 for (const char *p = filename; p != NULL; p = strchr(p, PATHSEPCHAR)) {
81 /* Remove possible double path separator characters from
82 * the beginning, so we don't start reading e.g. root. */
83 while (*p == PATHSEPCHAR) p++;
84
85 item = names->GetItem(p, false);
86 if (item != NULL && !StrEmpty(item->value)) break;
87 }
88
89 if (item == NULL || StrEmpty(item->value)) {
90 DEBUG(grf, 0, "Base music set song name missing: %s", filename);
91 return false;
92 }
93
94 strecpy(this->song_name[i], item->value, lastof(this->song_name[i]));
95 this->track_nr[i] = j++;
96 this->num_available++;
97 }
98 }
99 return ret;
100 }
IniGroup
A group within an ini file.
Definition: ini_type.h:38
base_media_func.h
Generic function implementations for base data (graphics, sounds).
BaseMedia::GetExtension
static const char * GetExtension()
Get the extension that is used to identify this set.
Definition: gfxinit.cpp:456
NUM_SONGS_AVAILABLE
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
Definition: base_media_base.h:278
_music_file_names
static const char *const _music_file_names[]
Names corresponding to the music set&#39;s files.
Definition: music.cpp:23
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
IniLoadFile::GetGroup
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:156
BaseSet< MusicSet, NUM_SONGS_AVAILABLE, false >::files
MD5File files[NUM_FILES]
All files part of this set.
Definition: base_media_base.h:66
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
MusicSet
All data of a music set.
Definition: base_media_base.h:284
MD5File::filename
const char * filename
filename
Definition: base_media_base.h:34
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
assert_compile
assert_compile(lengthof(_music_file_names)==NUM_SONGS_AVAILABLE)
Make sure we aren&#39;t messing things up.
safeguards.h
A number of safeguards to prevent using unsafe methods.
IniItem::value
char * value
The value of this item.
Definition: ini_type.h:28
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
BaseMedia< MusicSet >
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:88
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
BaseSet::FillSetDetails
bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
Definition: base_media_func.h:46
INSTANTIATE_BASE_MEDIA_METHODS
#define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type)
Force instantiation of methods so we don&#39;t get linker errors.
Definition: base_media_func.h:400
IniGroup::GetItem
IniItem * GetItem(const char *name, bool create)
Get the item with the given name, and if it doesn&#39;t exist and create is true it creates a new item...
Definition: ini_load.cpp:105
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
MusicSet::song_name
char song_name[NUM_SONGS_AVAILABLE][32]
The name of the different songs.
Definition: base_media_base.h:286
BaseSet
Information about a single base set.
Definition: base_media_base.h:48
BaseMedia::DetermineBestSet
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
Definition: gfxinit.cpp:432