1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "MUSImporter.h"
23 #include "Interface.h"
26 #include "GameData.h" // For ResourceHolder
28 static char musicsubfolder
[6] = "music";
30 MUSImporter::MUSImporter()
34 str
= new FileStream();
37 lastSound
= 0xffffffff;
39 PathJoin(path
, core
->GamePath
, musicsubfolder
, NULL
);
40 manager
.AddSource(path
, "Music", PLUGIN_RESOURCE_DIRECTORY
);
43 MUSImporter::~MUSImporter()
49 /** Initializes the PlayList Manager */
50 bool MUSImporter::Init()
55 /** Loads a PlayList for playing */
56 bool MUSImporter::OpenPlaylist(const char* name
)
58 if (PLName
[0] != '\0') {
59 int len
= ( int ) strlen( PLName
);
60 if (strnicmp( name
, PLName
, len
) == 0)
66 core
->GetAudioDrv()->ResetMusics();
73 PathJoin(path
, core
->GamePath
, musicsubfolder
, name
, NULL
);
74 printMessage("MUSImporter", "", WHITE
);
75 printf( "Loading %s...", path
);
76 if (!str
->Open( path
, true )) {
77 printStatus("NOT FOUND", LIGHT_RED
);
80 printStatus("FOUND", LIGHT_GREEN
);
81 int c
= str
->ReadLine( PLName
, 32 );
83 if (( PLName
[c
- 1] == ' ' ) || ( PLName
[c
- 1] == '\t' ))
90 str
->ReadLine( counts
, 5 );
91 int count
= atoi( counts
);
94 int len
= str
->ReadLine( line
, 64 );
99 if (( line
[i
] != ' ' ) && ( line
[i
] != '\t' ))
100 pls
.PLFile
[p
++] = line
[i
++];
103 if (( line
[i
] == ' ' ) || ( line
[i
] == '\t' ))
113 if (line
[i
] != '@' && ( i
< len
)) {
115 if (( line
[i
] != ' ' ) && ( line
[i
] != '\t' ))
116 pls
.PLTag
[p
++] = line
[i
++];
123 if (( line
[i
] == ' ' ) || ( line
[i
] == '\t' ))
130 strcpy( pls
.PLLoop
, pls
.PLTag
);
133 if (( line
[i
] != ' ' ) && ( line
[i
] != '\t' ))
134 pls
.PLLoop
[p
++] = line
[i
++];
141 if (( line
[i
] == ' ' ) || ( line
[i
] == '\t' ))
152 if (( line
[i
] != ' ' ) && ( line
[i
] != '\t' ))
156 if (( line
[i
] == ' ' ) || ( line
[i
] == '\t' ))
165 if (( line
[i
] != ' ' ) && ( line
[i
] != '\t' ))
166 pls
.PLEnd
[p
++] = line
[i
++];
171 playlist
.push_back( pls
);
176 /** Start the PlayList Music Execution */
177 void MUSImporter::Start()
181 if (playlist
.size() == 0)
183 if (playlist
[PLpos
].PLLoop
[0] != 0) {
184 for (unsigned int i
= 0; i
< playlist
.size(); i
++) {
185 if (stricmp( playlist
[i
].PLFile
, playlist
[PLpos
].PLLoop
) == 0) {
192 if ((unsigned int) PLnext
>= playlist
.size())
196 core
->GetAudioDrv()->Play();
197 lastSound
= playlist
[PLpos
].soundID
;
201 /** Ends the Current PlayList Execution */
202 void MUSImporter::End()
205 if (playlist
.size() == 0)
207 if (playlist
[PLpos
].PLEnd
[0] != 0) {
208 if (stricmp( playlist
[PLpos
].PLEnd
, "end" ) != 0)
209 PlayMusic( playlist
[PLpos
].PLEnd
);
215 void MUSImporter::HardEnd()
217 core
->GetAudioDrv()->Stop();
222 /** Switches the current PlayList while playing the current one, return nonzero on error */
223 int MUSImporter::SwitchPlayList(const char* name
, bool Hard
)
225 //don't do anything if the requested song is already playing
226 //this fixed PST's infinite song start
228 int len
= ( int ) strlen( PLName
);
229 if (strnicmp( name
, PLName
, len
) == 0)
237 if (OpenPlaylist( name
)) {
243 /** Plays the Next Entry */
244 void MUSImporter::PlayNext()
252 if (playlist
[PLpos
].PLLoop
[0] != 0) {
253 for (unsigned int i
= 0; i
< playlist
.size(); i
++) {
254 if (stricmp( playlist
[i
].PLFile
, playlist
[PLpos
].PLLoop
) == 0) {
260 if (stricmp( playlist
[PLnext
].PLEnd
, "end" ) == 0)
264 if ((unsigned int) PLnext
>= playlist
.size() ) {
270 core
->GetAudioDrv()->Stop();
274 void MUSImporter::PlayMusic(int pos
)
276 PlayMusic( playlist
[pos
].PLFile
);
279 void MUSImporter::PlayMusic(char* name
)
281 char FName
[_MAX_PATH
];
282 if (strnicmp( name
, "mx9000", 6 ) == 0) { //iwd2
283 PathJoin(FName
, "mx9000", name
, NULL
);
284 } else if (strnicmp( name
, "mx0000", 6 ) == 0) { //iwd
285 PathJoin(FName
, "mx0000", name
, NULL
);
286 } else if (strnicmp( name
, "SPC", 3 ) != 0) { //bg2
287 char File
[_MAX_PATH
];
288 snprintf(File
, _MAX_PATH
, "%s%s", PLName
, name
);
289 PathJoin(FName
, PLName
, File
, NULL
);
291 strncpy(FName
, name
, _MAX_PATH
);
294 ResourceHolder
<SoundMgr
> sound(FName
, manager
);
296 int soundID
= core
->GetAudioDrv()->CreateStream( sound
);
298 core
->GetAudioDrv()->Stop();
301 core
->GetAudioDrv()->Stop();
303 printf( "Playing: %s\n", FName
);
306 bool MUSImporter::CurrentPlayList(const char* name
) {
307 int len
= ( int ) strlen( PLName
);
308 if (strnicmp( name
, PLName
, len
) == 0) return true;
313 #include "plugindef.h"
315 GEMRB_PLUGIN(0x2DCB9E8, "MUS File Importer")
316 PLUGIN_CLASS(IE_MUS_CLASS_ID
, MUSImporter
)