| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 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. |
| 13 | * |
| 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: cdrom_image.cpp,v 1.24 2009-03-19 20:45:42 c2woody Exp $ */ |
| 20 | |
| 21 | #include <cctype> |
| 22 | #include <cmath> |
| 23 | #include <cstdio> |
| 24 | #include <fstream> |
| 25 | #include <iostream> |
| 26 | #include <limits> |
| 27 | #include <limits.h> //GCC 2.95 |
| 28 | #include <sstream> |
| 29 | #include <vector> |
| 30 | #include <sys/stat.h> |
| 31 | #include "cdrom.h" |
| 32 | #include "drives.h" |
| 33 | #include "support.h" |
| 34 | #include "setup.h" |
| 35 | |
| 36 | #if !defined(WIN32) |
| 37 | #include <libgen.h> |
| 38 | #else |
| 39 | #include <string.h> |
| 40 | #endif |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | #define MAX_LINE_LENGTH 512 |
| 45 | #define MAX_FILENAME_LENGTH 256 |
| 46 | |
| 47 | CDROM_Interface_Image::BinaryFile::BinaryFile(const char *filename, bool &error) |
| 48 | { |
| 49 | file = new ifstream(filename, ios::in | ios::binary); |
| 50 | error = (file == NULL) || (file->fail()); |
| 51 | } |
| 52 | |
| 53 | CDROM_Interface_Image::BinaryFile::~BinaryFile() |
| 54 | { |
| 55 | delete file; |
| 56 | file = NULL; |
| 57 | } |
| 58 | |
| 59 | bool CDROM_Interface_Image::BinaryFile::read(Bit8u *buffer, int seek, int count) |
| 60 | { |
| 61 | file->seekg(seek, ios::beg); |
| 62 | file->read((char*)buffer, count); |
| 63 | return !(file->fail()); |
| 64 | } |
| 65 | |
| 66 | int CDROM_Interface_Image::BinaryFile::getLength() |
| 67 | { |
| 68 | file->seekg(0, ios::end); |
| 69 | int length = (int)file->tellg(); |
| 70 | if (file->fail()) return -1; |
| 71 | return length; |
| 72 | } |
| 73 | |
| 74 | #if defined(C_SDL_SOUND) |
| 75 | CDROM_Interface_Image::AudioFile::AudioFile(const char *filename, bool &error) |
| 76 | { |
| 77 | Sound_AudioInfo desired = {AUDIO_S16, 2, 44100}; |
| 78 | sample = Sound_NewSampleFromFile(filename, &desired, RAW_SECTOR_SIZE); |
| 79 | lastCount = RAW_SECTOR_SIZE; |
| 80 | lastSeek = 0; |
| 81 | error = (sample == NULL); |
| 82 | } |
| 83 | |
| 84 | CDROM_Interface_Image::AudioFile::~AudioFile() |
| 85 | { |
| 86 | Sound_FreeSample(sample); |
| 87 | } |
| 88 | |
| 89 | bool CDROM_Interface_Image::AudioFile::read(Bit8u *buffer, int seek, int count) |
| 90 | { |
| 91 | if (lastCount != count) { |
| 92 | int success = Sound_SetBufferSize(sample, count); |
| 93 | if (!success) return false; |
| 94 | } |
| 95 | if (lastSeek != (seek - count)) { |
| 96 | int success = Sound_Seek(sample, (int)((double)(seek) / 176.4f)); |
| 97 | if (!success) return false; |
| 98 | } |
| 99 | lastSeek = seek; |
| 100 | int bytes = Sound_Decode(sample); |
| 101 | if (bytes < count) { |
| 102 | memcpy(buffer, sample->buffer, bytes); |
| 103 | memset(buffer + bytes, 0, count - bytes); |
| 104 | } else { |
| 105 | memcpy(buffer, sample->buffer, count); |
| 106 | } |
| 107 | |
| 108 | return !(sample->flags & SOUND_SAMPLEFLAG_ERROR); |
| 109 | } |
| 110 | |
| 111 | int CDROM_Interface_Image::AudioFile::getLength() |
| 112 | { |
| 113 | int time = 1; |
| 114 | int shift = 0; |
| 115 | if (!(sample->flags & SOUND_SAMPLEFLAG_CANSEEK)) return -1; |
| 116 | |
| 117 | while (true) { |
| 118 | int success = Sound_Seek(sample, (unsigned int)(shift + time)); |
| 119 | if (!success) { |
| 120 | if (time == 1) return lround((double)shift * 176.4f); |
| 121 | shift += time >> 1; |
| 122 | time = 1; |
| 123 | } else { |
| 124 | if (time > ((numeric_limits<int>::max() - shift) / 2)) return -1; |
| 125 | time = time << 1; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | // initialize static members |
| 132 | int CDROM_Interface_Image::refCount = 0; |
| 133 | CDROM_Interface_Image* CDROM_Interface_Image::images[26] = {}; |
| 134 | CDROM_Interface_Image::imagePlayer CDROM_Interface_Image::player = { |
| 135 | NULL, NULL, NULL, {0}, 0, 0, 0, false, false }; |
| 136 | |
| 137 | |
| 138 | CDROM_Interface_Image::CDROM_Interface_Image(Bit8u subUnit) |
| 139 | :subUnit(subUnit) |
| 140 | { |
| 141 | images[subUnit] = this; |
| 142 | if (refCount == 0) { |
| 143 | player.mutex = SDL_CreateMutex(); |
| 144 | if (!player.channel) { |
| 145 | player.channel = MIXER_AddChannel(&CDAudioCallBack, 44100, "CDAUDIO"); |
| 146 | } |
| 147 | player.channel->Enable(true); |
| 148 | } |
| 149 | refCount++; |
| 150 | } |
| 151 | |
| 152 | CDROM_Interface_Image::~CDROM_Interface_Image() |
| 153 | { |
| 154 | refCount--; |
| 155 | if (player.cd == this) player.cd = NULL; |
| 156 | ClearTracks(); |
| 157 | if (refCount == 0) { |
| 158 | SDL_DestroyMutex(player.mutex); |
| 159 | player.channel->Enable(false); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void CDROM_Interface_Image::InitNewMedia() |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | bool CDROM_Interface_Image::SetDevice(char* path, int forceCD) |
| 168 | { |
| 169 | if (LoadCueSheet(path)) return true; |
| 170 | if (LoadIsoFile(path)) return true; |
| 171 | |
| 172 | // print error message on dosbox console |
| 173 | char buf[MAX_LINE_LENGTH]; |
| 174 | snprintf(buf, MAX_LINE_LENGTH, "Could not load image file: %s\n", path); |
| 175 | Bit16u size = (Bit16u)strlen(buf); |
| 176 | DOS_WriteFile(STDOUT, (Bit8u*)buf, &size); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool CDROM_Interface_Image::GetUPC(unsigned char& attr, char* upc) |
| 181 | { |
| 182 | attr = 0; |
| 183 | strcpy(upc, this->mcn.c_str()); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | bool CDROM_Interface_Image::GetAudioTracks(int& stTrack, int& end, TMSF& leadOut) |
| 188 | { |
| 189 | stTrack = 1; |
| 190 | end = (int)(tracks.size() - 1); |
| 191 | FRAMES_TO_MSF(tracks[tracks.size() - 1].start + 150, &leadOut.min, &leadOut.sec, &leadOut.fr); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | bool CDROM_Interface_Image::GetAudioTrackInfo(int track, TMSF& start, unsigned char& attr) |
| 196 | { |
| 197 | if (track < 1 || track > (int)tracks.size()) return false; |
| 198 | FRAMES_TO_MSF(tracks[track - 1].start + 150, &start.min, &start.sec, &start.fr); |
| 199 | attr = tracks[track - 1].attr; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | bool CDROM_Interface_Image::GetAudioSub(unsigned char& attr, unsigned char& track, unsigned char& index, TMSF& relPos, TMSF& absPos) |
| 204 | { |
| 205 | int cur_track = GetTrack(player.currFrame); |
| 206 | if (cur_track < 1) return false; |
| 207 | track = (unsigned char)cur_track; |
| 208 | attr = tracks[track - 1].attr; |
| 209 | index = 1; |
| 210 | FRAMES_TO_MSF(player.currFrame + 150, &absPos.min, &absPos.sec, &absPos.fr); |
| 211 | FRAMES_TO_MSF(player.currFrame - tracks[track - 1].start + 150, &relPos.min, &relPos.sec, &relPos.fr); |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool CDROM_Interface_Image::GetAudioStatus(bool& playing, bool& pause) |
| 216 | { |
| 217 | playing = player.isPlaying; |
| 218 | pause = player.isPaused; |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | bool CDROM_Interface_Image::GetMediaTrayStatus(bool& mediaPresent, bool& mediaChanged, bool& trayOpen) |
| 223 | { |
| 224 | mediaPresent = true; |
| 225 | mediaChanged = false; |
| 226 | trayOpen = false; |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool CDROM_Interface_Image::PlayAudioSector(unsigned long start,unsigned long len) |
| 231 | { |
| 232 | // We might want to do some more checks. E.g valid start and length |
| 233 | SDL_mutexP(player.mutex); |
| 234 | player.cd = this; |
| 235 | player.currFrame = start; |
| 236 | player.targetFrame = start + len; |
| 237 | int track = GetTrack(start) - 1; |
| 238 | if(track >= 0 && tracks[track].attr == 0x40) { |
| 239 | LOG(LOG_MISC,LOG_WARN)("Game tries to play the data track. Not doing this"); |
| 240 | player.isPlaying = false; |
| 241 | //Unclear wether return false should be here. |
| 242 | //specs say that this function returns at once and games should check the status wether the audio is actually playing |
| 243 | //Real drives either fail or succeed as well |
| 244 | } else player.isPlaying = true; |
| 245 | player.isPaused = false; |
| 246 | SDL_mutexV(player.mutex); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | bool CDROM_Interface_Image::PauseAudio(bool resume) |
| 251 | { |
| 252 | if (!player.isPlaying) return false; |
| 253 | player.isPaused = !resume; |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | bool CDROM_Interface_Image::StopAudio(void) |
| 258 | { |
| 259 | player.isPlaying = false; |
| 260 | player.isPaused = false; |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | bool CDROM_Interface_Image::ReadSectors(PhysPt buffer, bool raw, unsigned long sector, unsigned long num) |
| 265 | { |
| 266 | int sectorSize = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; |
| 267 | Bitu buflen = num * sectorSize; |
| 268 | Bit8u* buf = new Bit8u[buflen]; |
| 269 | |
| 270 | bool success = true; //Gobliiins reads 0 sectors |
| 271 | for(unsigned long i = 0; i < num; i++) { |
| 272 | success = ReadSector(&buf[i * sectorSize], raw, sector + i); |
| 273 | if (!success) break; |
| 274 | } |
| 275 | |
| 276 | MEM_BlockWrite(buffer, buf, buflen); |
| 277 | delete[] buf; |
| 278 | |
| 279 | return success; |
| 280 | } |
| 281 | |
| 282 | bool CDROM_Interface_Image::LoadUnloadMedia(bool unload) |
| 283 | { |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | int CDROM_Interface_Image::GetTrack(int sector) |
| 288 | { |
| 289 | vector<Track>::iterator i = tracks.begin(); |
| 290 | vector<Track>::iterator end = tracks.end() - 1; |
| 291 | |
| 292 | while(i != end) { |
| 293 | Track &curr = *i; |
| 294 | Track &next = *(i + 1); |
| 295 | if (curr.start <= sector && sector < next.start) return curr.number; |
| 296 | i++; |
| 297 | } |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | bool CDROM_Interface_Image::ReadSector(Bit8u *buffer, bool raw, unsigned long sector) |
| 302 | { |
| 303 | int track = GetTrack(sector) - 1; |
| 304 | if (track < 0) return false; |
| 305 | |
| 306 | int seek = tracks[track].skip + (sector - tracks[track].start) * tracks[track].sectorSize; |
| 307 | int length = (raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE); |
| 308 | if (tracks[track].sectorSize != RAW_SECTOR_SIZE && raw) return false; |
| 309 | if (tracks[track].sectorSize == RAW_SECTOR_SIZE && !tracks[track].mode2 && !raw) seek += 16; |
| 310 | if (tracks[track].mode2 && !raw) seek += 24; |
| 311 | |
| 312 | return tracks[track].file->read(buffer, seek, length); |
| 313 | } |
| 314 | |
| 315 | void CDROM_Interface_Image::CDAudioCallBack(Bitu len) |
| 316 | { |
| 317 | len *= 4; // 16 bit, stereo |
| 318 | if (!len) return; |
| 319 | if (!player.isPlaying || player.isPaused) { |
| 320 | player.channel->AddSilence(); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | SDL_mutexP(player.mutex); |
| 325 | while (player.bufLen < (Bits)len) { |
| 326 | bool success; |
| 327 | if (player.targetFrame > player.currFrame) |
| 328 | success = player.cd->ReadSector(&player.buffer[player.bufLen], true, player.currFrame); |
| 329 | else success = false; |
| 330 | |
| 331 | if (success) { |
| 332 | player.currFrame++; |
| 333 | player.bufLen += RAW_SECTOR_SIZE; |
| 334 | } else { |
| 335 | memset(&player.buffer[player.bufLen], 0, len - player.bufLen); |
| 336 | player.bufLen = len; |
| 337 | player.isPlaying = false; |
| 338 | } |
| 339 | } |
| 340 | SDL_mutexV(player.mutex); |
| 341 | #if defined(WORDS_BIGENDIAN) |
| 342 | player.channel->AddSamples_s16_nonnative(len/4,(Bit16s *)player.buffer); |
| 343 | #else |
| 344 | player.channel->AddSamples_s16(len/4,(Bit16s *)player.buffer); |
| 345 | #endif |
| 346 | memmove(player.buffer, &player.buffer[len], player.bufLen - len); |
| 347 | player.bufLen -= len; |
| 348 | } |
| 349 | |
| 350 | bool CDROM_Interface_Image::LoadIsoFile(char* filename) |
| 351 | { |
| 352 | tracks.clear(); |
| 353 | |
| 354 | // data track |
| 355 | Track track = {0, 0, 0, 0, 0, 0, false, NULL}; |
| 356 | bool error; |
| 357 | track.file = new BinaryFile(filename, error); |
| 358 | if (error) { |
| 359 | delete track.file; |
| 360 | track.file = NULL; |
| 361 | return false; |
| 362 | } |
| 363 | track.number = 1; |
| 364 | track.attr = 0x40;//data |
| 365 | |
| 366 | // try to detect iso type |
| 367 | if (CanReadPVD(track.file, COOKED_SECTOR_SIZE, false)) { |
| 368 | track.sectorSize = COOKED_SECTOR_SIZE; |
| 369 | track.mode2 = false; |
| 370 | } else if (CanReadPVD(track.file, RAW_SECTOR_SIZE, false)) { |
| 371 | track.sectorSize = RAW_SECTOR_SIZE; |
| 372 | track.mode2 = false; |
| 373 | } else if (CanReadPVD(track.file, 2336, true)) { |
| 374 | track.sectorSize = 2336; |
| 375 | track.mode2 = true; |
| 376 | } else if (CanReadPVD(track.file, RAW_SECTOR_SIZE, true)) { |
| 377 | track.sectorSize = RAW_SECTOR_SIZE; |
| 378 | track.mode2 = true; |
| 379 | } else return false; |
| 380 | |
| 381 | track.length = track.file->getLength() / track.sectorSize; |
| 382 | tracks.push_back(track); |
| 383 | |
| 384 | // leadout track |
| 385 | track.number = 2; |
| 386 | track.attr = 0; |
| 387 | track.start = track.length; |
| 388 | track.length = 0; |
| 389 | track.file = NULL; |
| 390 | tracks.push_back(track); |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool CDROM_Interface_Image::CanReadPVD(TrackFile *file, int sectorSize, bool mode2) |
| 396 | { |
| 397 | Bit8u pvd[COOKED_SECTOR_SIZE]; |
| 398 | int seek = 16 * sectorSize; // first vd is located at sector 16 |
| 399 | if (sectorSize == RAW_SECTOR_SIZE && !mode2) seek += 16; |
| 400 | if (mode2) seek += 24; |
| 401 | file->read(pvd, seek, COOKED_SECTOR_SIZE); |
| 402 | // pvd[0] = descriptor type, pvd[1..5] = standard identifier, pvd[6] = iso version |
| 403 | return (pvd[0] == 1 && !strncmp((char*)(&pvd[1]), "CD001", 5) && pvd[6] == 1); |
| 404 | } |
| 405 | |
| 406 | #if defined(WIN32) |
| 407 | static string dirname(char * file) { |
| 408 | char * sep = strrchr(file, '\\'); |
| 409 | if (sep == NULL) |
| 410 | sep = strrchr(file, '/'); |
| 411 | if (sep == NULL) |
| 412 | return ""; |
| 413 | else { |
| 414 | int len = (int)(sep - file); |
| 415 | char tmp[MAX_FILENAME_LENGTH]; |
| 416 | safe_strncpy(tmp, file, len+1); |
| 417 | return tmp; |
| 418 | } |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | bool CDROM_Interface_Image::LoadCueSheet(char *cuefile) |
| 423 | { |
| 424 | Track track = {0, 0, 0, 0, 0, 0, false, NULL}; |
| 425 | tracks.clear(); |
| 426 | int shift = 0; |
| 427 | int currPregap = 0; |
| 428 | int totalPregap = 0; |
| 429 | int prestart = 0; |
| 430 | bool success; |
| 431 | bool canAddTrack = false; |
| 432 | char tmp[MAX_FILENAME_LENGTH]; // dirname can change its argument |
| 433 | safe_strncpy(tmp, cuefile, MAX_FILENAME_LENGTH); |
| 434 | string pathname(dirname(tmp)); |
| 435 | ifstream in; |
| 436 | in.open(cuefile, ios::in); |
| 437 | if (in.fail()) return false; |
| 438 | |
| 439 | while(!in.eof()) { |
| 440 | // get next line |
| 441 | char buf[MAX_LINE_LENGTH]; |
| 442 | in.getline(buf, MAX_LINE_LENGTH); |
| 443 | if (in.fail() && !in.eof()) return false; // probably a binary file |
| 444 | istringstream line(buf); |
| 445 | |
| 446 | string command; |
| 447 | GetCueKeyword(command, line); |
| 448 | |
| 449 | if (command == "TRACK") { |
| 450 | if (canAddTrack) success = AddTrack(track, shift, prestart, totalPregap, currPregap); |
| 451 | else success = true; |
| 452 | |
| 453 | track.start = 0; |
| 454 | track.skip = 0; |
| 455 | currPregap = 0; |
| 456 | prestart = 0; |
| 457 | |
| 458 | line >> track.number; |
| 459 | string type; |
| 460 | GetCueKeyword(type, line); |
| 461 | |
| 462 | if (type == "AUDIO") { |
| 463 | track.sectorSize = RAW_SECTOR_SIZE; |
| 464 | track.attr = 0; |
| 465 | track.mode2 = false; |
| 466 | } else if (type == "MODE1/2048") { |
| 467 | track.sectorSize = COOKED_SECTOR_SIZE; |
| 468 | track.attr = 0x40; |
| 469 | track.mode2 = false; |
| 470 | } else if (type == "MODE1/2352") { |
| 471 | track.sectorSize = RAW_SECTOR_SIZE; |
| 472 | track.attr = 0x40; |
| 473 | track.mode2 = false; |
| 474 | } else if (type == "MODE2/2336") { |
| 475 | track.sectorSize = 2336; |
| 476 | track.attr = 0x40; |
| 477 | track.mode2 = true; |
| 478 | } else if (type == "MODE2/2352") { |
| 479 | track.sectorSize = RAW_SECTOR_SIZE; |
| 480 | track.attr = 0x40; |
| 481 | track.mode2 = true; |
| 482 | } else success = false; |
| 483 | |
| 484 | canAddTrack = true; |
| 485 | } |
| 486 | else if (command == "INDEX") { |
| 487 | int index; |
| 488 | line >> index; |
| 489 | int frame; |
| 490 | success = GetCueFrame(frame, line); |
| 491 | |
| 492 | if (index == 1) track.start = frame; |
| 493 | else if (index == 0) prestart = frame; |
| 494 | // ignore other indices |
| 495 | } |
| 496 | else if (command == "FILE") { |
| 497 | if (canAddTrack) success = AddTrack(track, shift, prestart, totalPregap, currPregap); |
| 498 | else success = true; |
| 499 | canAddTrack = false; |
| 500 | |
| 501 | string filename; |
| 502 | GetCueString(filename, line); |
| 503 | GetRealFileName(filename, pathname); |
| 504 | string type; |
| 505 | GetCueKeyword(type, line); |
| 506 | |
| 507 | track.file = NULL; |
| 508 | bool error = true; |
| 509 | if (type == "BINARY") { |
| 510 | track.file = new BinaryFile(filename.c_str(), error); |
| 511 | } |
| 512 | #if defined(C_SDL_SOUND) |
| 513 | //The next if has been surpassed by the else, but leaving it in as not |
| 514 | //to break existing cue sheets that depend on this.(mine with OGG tracks specifying MP3 as type) |
| 515 | else if (type == "WAVE" || type == "AIFF" || type == "MP3") { |
| 516 | track.file = new AudioFile(filename.c_str(), error); |
| 517 | } else { |
| 518 | const Sound_DecoderInfo **i; |
| 519 | for (i = Sound_AvailableDecoders(); *i != NULL; i++) { |
| 520 | if (*(*i)->extensions == type) { |
| 521 | track.file = new AudioFile(filename.c_str(), error); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | #endif |
| 527 | if (error) { |
| 528 | delete track.file; |
| 529 | track.file = NULL; |
| 530 | success = false; |
| 531 | } |
| 532 | } |
| 533 | else if (command == "PREGAP") success = GetCueFrame(currPregap, line); |
| 534 | else if (command == "CATALOG") success = GetCueString(mcn, line); |
| 535 | // ignored commands |
| 536 | else if (command == "CDTEXTFILE" || command == "FLAGS" || command == "ISRC" |
| 537 | || command == "PERFORMER" || command == "POSTGAP" || command == "REM" |
| 538 | || command == "SONGWRITER" || command == "TITLE" || command == "") success = true; |
| 539 | // failure |
| 540 | else success = false; |
| 541 | |
| 542 | if (!success) return false; |
| 543 | } |
| 544 | // add last track |
| 545 | if (!AddTrack(track, shift, prestart, totalPregap, currPregap)) return false; |
| 546 | |
| 547 | // add leadout track |
| 548 | track.number++; |
| 549 | track.attr = 0;//sync with load iso |
| 550 | track.start = 0; |
| 551 | track.length = 0; |
| 552 | track.file = NULL; |
| 553 | if(!AddTrack(track, shift, 0, totalPregap, 0)) return false; |
| 554 | |
| 555 | return true; |
| 556 | } |
| 557 | |
| 558 | bool CDROM_Interface_Image::AddTrack(Track &curr, int &shift, int prestart, int &totalPregap, int currPregap) |
| 559 | { |
| 560 | // frames between index 0(prestart) and 1(curr.start) must be skipped |
| 561 | int skip; |
| 562 | if (prestart > 0) { |
| 563 | if (prestart > curr.start) return false; |
| 564 | skip = curr.start - prestart; |
| 565 | } else skip = 0; |
| 566 | |
| 567 | // first track (track number must be 1) |
| 568 | if (tracks.empty()) { |
| 569 | if (curr.number != 1) return false; |
| 570 | curr.skip = skip * curr.sectorSize; |
| 571 | curr.start += currPregap; |
| 572 | totalPregap = currPregap; |
| 573 | tracks.push_back(curr); |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | Track &prev = *(tracks.end() - 1); |
| 578 | |
| 579 | // current track consumes data from the same file as the previous |
| 580 | if (prev.file == curr.file) { |
| 581 | curr.start += shift; |
| 582 | prev.length = curr.start + totalPregap - prev.start - skip; |
| 583 | curr.skip += prev.skip + prev.length * prev.sectorSize + skip * curr.sectorSize; |
| 584 | totalPregap += currPregap; |
| 585 | curr.start += totalPregap; |
| 586 | // current track uses a different file as the previous track |
| 587 | } else { |
| 588 | int tmp = prev.file->getLength() - prev.skip; |
| 589 | prev.length = tmp / prev.sectorSize; |
| 590 | if (tmp % prev.sectorSize != 0) prev.length++; // padding |
| 591 | |
| 592 | curr.start += prev.start + prev.length + currPregap; |
| 593 | curr.skip = skip * curr.sectorSize; |
| 594 | shift += prev.start + prev.length; |
| 595 | totalPregap = currPregap; |
| 596 | } |
| 597 | |
| 598 | // error checks |
| 599 | if (curr.number <= 1) return false; |
| 600 | if (prev.number + 1 != curr.number) return false; |
| 601 | if (curr.start < prev.start + prev.length) return false; |
| 602 | if (curr.length < 0) return false; |
| 603 | |
| 604 | tracks.push_back(curr); |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | bool CDROM_Interface_Image::HasDataTrack(void) |
| 609 | { |
| 610 | //Data track has attribute 0x40 |
| 611 | for(track_it it = tracks.begin(); it != tracks.end(); it++) { |
| 612 | if ((*it).attr == 0x40) return true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | bool CDROM_Interface_Image::GetRealFileName(string &filename, string &pathname) |
| 619 | { |
| 620 | // check if file exists |
| 621 | struct stat test; |
| 622 | if (stat(filename.c_str(), &test) == 0) return true; |
| 623 | |
| 624 | // check if file with path relative to cue file exists |
| 625 | string tmpstr(pathname + "/" + filename); |
| 626 | if (stat(tmpstr.c_str(), &test) == 0) { |
| 627 | filename = tmpstr; |
| 628 | return true; |
| 629 | } |
| 630 | // finally check if file is in a dosbox local drive |
| 631 | char fullname[CROSS_LEN]; |
| 632 | char tmp[CROSS_LEN]; |
| 633 | safe_strncpy(tmp, filename.c_str(), CROSS_LEN); |
| 634 | Bit8u drive; |
| 635 | if (!DOS_MakeName(tmp, fullname, &drive)) return false; |
| 636 | |
| 637 | localDrive *ldp = dynamic_cast<localDrive*>(Drives[drive]); |
| 638 | if (ldp) { |
| 639 | ldp->GetSystemFilename(tmp, fullname); |
| 640 | if (stat(tmp, &test) == 0) { |
| 641 | filename = tmp; |
| 642 | return true; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | bool CDROM_Interface_Image::GetCueKeyword(string &keyword, istream &in) |
| 650 | { |
| 651 | in >> keyword; |
| 652 | for(Bitu i = 0; i < keyword.size(); i++) keyword[i] = toupper(keyword[i]); |
| 653 | |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | bool CDROM_Interface_Image::GetCueFrame(int &frames, istream &in) |
| 658 | { |
| 659 | string msf; |
| 660 | in >> msf; |
| 661 | int min, sec, fr; |
| 662 | bool success = sscanf(msf.c_str(), "%d:%d:%d", &min, &sec, &fr) == 3; |
| 663 | frames = MSF_TO_FRAMES(min, sec, fr); |
| 664 | |
| 665 | return success; |
| 666 | } |
| 667 | |
| 668 | bool CDROM_Interface_Image::GetCueString(string &str, istream &in) |
| 669 | { |
| 670 | int pos = (int)in.tellg(); |
| 671 | in >> str; |
| 672 | if (str[0] == '\"') { |
| 673 | if (str[str.size() - 1] == '\"') { |
| 674 | str.assign(str, 1, str.size() - 2); |
| 675 | } else { |
| 676 | in.seekg(pos, ios::beg); |
| 677 | char buffer[MAX_FILENAME_LENGTH]; |
| 678 | in.getline(buffer, MAX_FILENAME_LENGTH, '\"'); // skip |
| 679 | in.getline(buffer, MAX_FILENAME_LENGTH, '\"'); |
| 680 | str = buffer; |
| 681 | } |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | void CDROM_Interface_Image::ClearTracks() |
| 687 | { |
| 688 | vector<Track>::iterator i = tracks.begin(); |
| 689 | vector<Track>::iterator end = tracks.end(); |
| 690 | |
| 691 | TrackFile* last = NULL; |
| 692 | while(i != end) { |
| 693 | Track &curr = *i; |
| 694 | if (curr.file != last) { |
| 695 | delete curr.file; |
| 696 | last = curr.file; |
| 697 | } |
| 698 | i++; |
| 699 | } |
| 700 | tracks.clear(); |
| 701 | } |
| 702 | |
| 703 | void CDROM_Image_Destroy(Section*) { |
| 704 | #if defined(C_SDL_SOUND) |
| 705 | Sound_Quit(); |
| 706 | #endif |
| 707 | } |
| 708 | |
| 709 | void CDROM_Image_Init(Section* section) { |
| 710 | #if defined(C_SDL_SOUND) |
| 711 | Sound_Init(); |
| 712 | section->AddDestroyFunction(CDROM_Image_Destroy, false); |
| 713 | #endif |
| 714 | } |