cdriso: look for data files if other one is selected
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 7798774..9ca4172 100644 (file)
@@ -16,7 +16,7 @@
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 #include "psxcommon.h"
@@ -28,6 +28,7 @@
 #ifdef _WIN32
 #include <process.h>
 #include <windows.h>
+#define strcasecmp _stricmp
 #else
 #include <pthread.h>
 #include <sys/time.h>
@@ -46,6 +47,8 @@ static boolean subChanMixed = FALSE;
 static boolean subChanRaw = FALSE;
 static boolean subChanMissing = FALSE;
 
+static boolean multifile = FALSE;
+
 static unsigned char cdbuffer[CD_FRAMESIZE_RAW];
 static unsigned char subbuffer[SUB_FRAMESIZE];
 
@@ -71,6 +74,8 @@ static unsigned int cdda_file_offset;
  * XXX: there could be multiple pregaps but PSX dumps only have one? */
 static unsigned int pregapOffset;
 
+#define cddaCurPos cdda_cur_sector
+
 // compressed image stuff
 static struct {
        unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
@@ -93,10 +98,8 @@ long CALLBACK CDR__getStatus(struct CdrStat *stat);
 
 static void DecodeRawSubData(void);
 
-extern void *hCDRDriver;
-
 struct trackinfo {
-       enum {DATA, CDDA} type;
+       enum {DATA=1, CDDA} type;
        char start[3];          // MSF-format
        char length[3];         // MSF-format
        FILE *handle;           // for multi-track images CDDA
@@ -324,6 +327,14 @@ static int parsetoc(const char *isofile) {
                                return -1;
                        }
                }
+               // check if it's really a TOC named as a .cue
+               fgets(linebuf, sizeof(linebuf), fi);
+               token = strtok(tmp, " ");
+               if (strncmp(token, "CD", 2) != 0 && strcmp(token, "CATALOG") != 0) {
+                       fclose(fi);
+                       return -1;
+               }
+               fseek(fi, 0, SEEK_SET);
        }
 
        memset(&ti, 0, sizeof(ti));
@@ -467,10 +478,12 @@ static int parsecue(const char *isofile) {
 
        // build a path for files referenced in .cue
        strncpy(filepath, cuename, sizeof(filepath));
-       tmp = strrchr(filepath, '/') + 1;
-       if (tmp == NULL)
-               tmp = strrchr(filepath, '\\') + 1;
+       tmp = strrchr(filepath, '/');
        if (tmp == NULL)
+               tmp = strrchr(filepath, '\\');
+       if (tmp != NULL)
+               tmp++;
+       else
                tmp = filepath;
        *tmp = 0;
        filepath[sizeof(filepath) - 1] = 0;
@@ -558,8 +571,10 @@ static int parsecue(const char *isofile) {
                        }
 
                        // update global offset if this is not first file in this .cue
-                       if (numtracks + 1 > 1)
+                       if (numtracks + 1 > 1) {
+                               multifile = 1;
                                sector_offs += file_len;
+                       }
 
                        file_len = 0;
                        if (ti[numtracks + 1].handle == NULL) {
@@ -1215,14 +1230,12 @@ static long CALLBACK ISOopen(void) {
        subChanRaw = FALSE;
        pregapOffset = 0;
        cdrIsoMultidiskCount = 1;
+       multifile = 0;
 
        CDR_getBuffer = ISOgetBuffer;
        cdimg_read_func = cdread_normal;
 
-       if (parsecue(GetIsoFile()) == 0) {
-               SysPrintf("[+cue]");
-       }
-       else if (parsetoc(GetIsoFile()) == 0) {
+       if (parsetoc(GetIsoFile()) == 0) {
                SysPrintf("[+toc]");
        }
        else if (parseccd(GetIsoFile()) == 0) {
@@ -1231,6 +1244,9 @@ static long CALLBACK ISOopen(void) {
        else if (parsemds(GetIsoFile()) == 0) {
                SysPrintf("[+mds]");
        }
+       else if (parsecue(GetIsoFile()) == 0) {
+               SysPrintf("[+cue]");
+       }
        if (handlepbp(GetIsoFile()) == 0) {
                SysPrintf("[pbp]");
                CDR_getBuffer = ISOgetBuffer_compr;
@@ -1249,8 +1265,33 @@ static long CALLBACK ISOopen(void) {
                SysPrintf("[+sbi]");
        }
 
-       // guess whether it is mode1/2048
        fseek(cdHandle, 0, SEEK_END);
+
+       // maybe user selected metadata file instead of main .bin ..
+       if (ftell(cdHandle) < 2352 * 0x10) {
+               static const char *exts[] = { ".bin", ".BIN", ".img", ".IMG" };
+               char tmp[MAXPATHLEN], *p;
+               FILE *tmpf;
+               size_t i;
+
+               strncpy(tmp, GetIsoFile(), sizeof(tmp));
+               tmp[MAXPATHLEN - 1] = '\0';
+               if (strlen(tmp) >= 4) {
+                       p = tmp + strlen(tmp) - 4;
+                       for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) {
+                               strcpy(p, exts[i]);
+                               tmpf = fopen(tmp, "rb");
+                               if (tmpf != NULL) {
+                                       fclose(cdHandle);
+                                       cdHandle = tmpf;
+                                       fseek(cdHandle, 0, SEEK_END);
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       // guess whether it is mode1/2048
        if (ftell(cdHandle) % 2048 == 0) {
                unsigned int modeTest = 0;
                fseek(cdHandle, 0, SEEK_SET);
@@ -1308,8 +1349,12 @@ static long CALLBACK ISOclose(void) {
                }
        }
        numtracks = 0;
+       ti[1].type = 0;
        UnloadSBI();
 
+       memset(cdbuffer, 0, sizeof(cdbuffer));
+       CDR_getBuffer = ISOgetBuffer;
+
        return 0;
 }
 
@@ -1349,7 +1394,6 @@ static long CALLBACK ISOgetTN(unsigned char *buffer) {
 //  byte 2 - minute
 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
        if (track == 0) {
-               // CD length according pcsxr-svn (done a bit different here)
                unsigned int sect;
                unsigned char time[3];
                sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
@@ -1393,6 +1437,7 @@ static void DecodeRawSubData(void) {
 // uses bcd format
 static long CALLBACK ISOreadTrack(unsigned char *time) {
        int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
+       long ret;
 
        if (cdHandle == NULL) {
                return -1;
@@ -1407,7 +1452,9 @@ static long CALLBACK ISOreadTrack(unsigned char *time) {
                }
        }
 
-       cdimg_read_func(cdHandle, 0, cdbuffer, sector);
+       ret = cdimg_read_func(cdHandle, 0, cdbuffer, sector);
+       if (ret < 0)
+               return -1;
 
        if (subHandle != NULL) {
                fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
@@ -1466,20 +1513,74 @@ static unsigned char* CALLBACK ISOgetBufferSub(void) {
 }
 
 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
-       int sec;
-
+       u32 sect;
+       
        CDR__getStatus(stat);
-
+       
        if (playing) {
                stat->Type = 0x02;
                stat->Status |= 0x80;
        }
        else {
-               stat->Type = 0x01;
+               // BIOS - boot ID (CD type)
+               stat->Type = ti[1].type;
+       }
+       
+       // relative -> absolute time
+       sect = cddaCurPos;
+       sec2msf(sect, (char *)stat->Time);
+       
+       return 0;
+}
+
+// read CDDA sector into buffer
+long CALLBACK ISOreadCDDA(unsigned char m, unsigned char s, unsigned char f, unsigned char *buffer) {
+       unsigned char msf[3] = {m, s, f};
+       unsigned int file, track, track_start = 0;
+       int ret;
+
+       cddaCurPos = msf2sec((char *)msf);
+
+       // find current track index
+       for (track = numtracks; ; track--) {
+               track_start = msf2sec(ti[track].start);
+               if (track_start <= cddaCurPos)
+                       break;
+               if (track == 1)
+                       break;
        }
 
-       sec = cdda_cur_sector;
-       sec2msf(sec, (char *)stat->Time);
+       // data tracks play silent
+       if (ti[track].type != CDDA) {
+               memset(buffer, 0, CD_FRAMESIZE_RAW);
+               return 0;
+       }
+
+       file = 1;
+       if (multifile) {
+               // find the file that contains this track
+               for (file = track; file > 1; file--)
+                       if (ti[file].handle != NULL)
+                               break;
+       }
+
+       ret = cdimg_read_func(ti[file].handle, ti[track].start_offset,
+               buffer, cddaCurPos - track_start);
+       if (ret != CD_FRAMESIZE_RAW) {
+               memset(buffer, 0, CD_FRAMESIZE_RAW);
+               return -1;
+       }
+
+       if (cddaBigEndian) {
+               int i;
+               unsigned char tmp;
+
+               for (i = 0; i < CD_FRAMESIZE_RAW / 2; i++) {
+                       tmp = buffer[i * 2];
+                       buffer[i * 2] = buffer[i * 2 + 1];
+                       buffer[i * 2 + 1] = tmp;
+               }
+       }
 
        return 0;
 }
@@ -1497,6 +1598,7 @@ void cdrIsoInit(void) {
        CDR_stop = ISOstop;
        CDR_getBufferSub = ISOgetBufferSub;
        CDR_getStatus = ISOgetStatus;
+       CDR_readCDDA = ISOreadCDDA;
 
        CDR_getDriveLetter = CDR__getDriveLetter;
        CDR_configure = CDR__configure;