Ejemplo n.º 1
0
        public void Open(string pathIn)
        {
            _inputPath = pathIn;
            _inputDir = Path.GetDirectoryName(_inputPath) ?? _inputPath;
            if (_inputDir == "") _inputDir = ".";
            if (_inputDir == pathIn && CUEProcessorPlugins.ripper != null)
            {
                ICDRipper ripper = Activator.CreateInstance(CUEProcessorPlugins.ripper) as ICDRipper;
                try
                {
                    ripper.Open(pathIn[0]);
                    if (ripper.TOC.AudioTracks > 0)
                    {
                        OpenCD(ripper);
                        int driveOffset;
                        if (!AccurateRipVerify.FindDriveReadOffset(_ripper.ARName, out driveOffset))
                            throw new Exception("Failed to find drive read offset for drive" + _ripper.ARName);
                        _ripper.DriveOffset = driveOffset;
                        //LookupAlbumInfo();
                        return;
                    }
                }
                catch
                {
                    ripper.Dispose();
                    _ripper = null;
                    throw;
                }
            }

            TextReader sr;

            if (Directory.Exists(pathIn))
                throw new Exception("is a directory");
            //{
            //    if (cueDir + Path.DirectorySeparatorChar != pathIn && cueDir != pathIn)
            //        throw new Exception("Input directory must end on path separator character.");
            //    string cueSheet = null;
            //    string[] audioExts = new string[] { "*.wav", "*.flac", "*.wv", "*.ape", "*.m4a", "*.tta" };
            //    for (i = 0; i < audioExts.Length && cueSheet == null; i++)
            //        cueSheet = CUESheet.CreateDummyCUESheet(pathIn, audioExts[i]);
            //    if (_config.udc1Extension != null && cueSheet == null)
            //        cueSheet = CUESheet.CreateDummyCUESheet(pathIn, "*." + _config.udc1Extension);
            //    if (cueSheet == null)
            //        throw new Exception("Input directory doesn't contain supported audio files.");
            //    sr = new StringReader(cueSheet);

            //    List<CUEToolsSourceFile> logFiles = new List<CUEToolsSourceFile>();
            //    foreach (string logPath in Directory.GetFiles(pathIn, "*.log"))
            //        logFiles.Add(new CUEToolsSourceFile(logPath, new StreamReader(logPath, CUESheet.Encoding)));
            //    CUEToolsSourceFile selectedLogFile = ChooseFile(logFiles, null, false);
            //    _eacLog = selectedLogFile != null ? selectedLogFile.contents : null;
            //} 
            else if (CUEProcessorPlugins.arcp_fmt.Contains(Path.GetExtension(pathIn).ToLower().Trim('.')))
            {
                _archive = null;
                foreach (Type type in CUEProcessorPlugins.arcp)
                {
                    CompressionProviderClass archclass = Attribute.GetCustomAttribute(type, typeof(CompressionProviderClass)) as CompressionProviderClass;
                    if (archclass.Extension == Path.GetExtension(pathIn).ToLower().Trim('.'))
                    {
                        _archive = Activator.CreateInstance(type, pathIn) as ICompressionProvider;
                        break;
                    }
                }
                if (_archive == null)
                    throw new Exception("archive type not supported.");
                _isArchive = true;
                _archiveContents = new List<string>();
                _archive.PasswordRequired += new EventHandler<CompressionPasswordRequiredEventArgs>(unzip_PasswordRequired);
                _archive.ExtractionProgress += new EventHandler<CompressionExtractionProgressEventArgs>(unzip_ExtractionProgress);
                foreach (string f in _archive.Contents)
                    _archiveContents.Add(f);

                _logFiles = new List<CUEToolsSourceFile>();
                List<CUEToolsSourceFile> cueFiles = new List<CUEToolsSourceFile>();
                foreach (string s in _archiveContents)
                {
                    if (Path.GetExtension(s).ToLower() == ".cue" || Path.GetExtension(s).ToLower() == ".log")
                    {
                        Stream archiveStream = OpenArchive(s, false);
                        CUEToolsSourceFile sourceFile = new CUEToolsSourceFile(s, new StreamReader(archiveStream, CUESheet.Encoding));
                        archiveStream.Close();
                        if (Path.GetExtension(s).ToLower() == ".cue")
                            cueFiles.Add(sourceFile);
                        else
                            _logFiles.Add(sourceFile);
                    }
                }
                CUEToolsSourceFile selectedCUEFile = ChooseFile(cueFiles, null, true);
                if (selectedCUEFile == null || selectedCUEFile.contents == "")
                    throw new Exception("Input archive doesn't contain a usable cue sheet.");
                _defaultLog = Path.GetFileNameWithoutExtension(selectedCUEFile.path);
                _archiveCUEpath = Path.GetDirectoryName(selectedCUEFile.path);
                string cueText = selectedCUEFile.contents;
                if (_config.autoCorrectFilenames)
                {
                    string extension;
                    cueText = CorrectAudioFilenames(_config, _archiveCUEpath, cueText, false, _archiveContents, out extension);
                }
                sr = new StringReader(cueText);
                if (_logFiles.Count == 1)
                    _eacLog = _logFiles[0].contents;
            }
            else if (Path.GetExtension(pathIn).ToLower() == ".cue")
            {
                if (_config.autoCorrectFilenames)
                    sr = new StringReader(CorrectAudioFilenames(_config, pathIn, false));
                else
                    sr = new StreamReader(pathIn, CUESheet.Encoding);

                _logFiles = new List<CUEToolsSourceFile>();
                _defaultLog = Path.GetFileNameWithoutExtension(pathIn);
                foreach (string logPath in Directory.GetFiles(_inputDir, "*.log"))
                    try { _logFiles.Add(new CUEToolsSourceFile(logPath, new StreamReader(logPath, CUESheet.Encoding))); }
                    catch { }
            }
            else if (Path.GetExtension(pathIn).ToLower() == ".m3u")
            {
                string cueSheet = CUESheet.CreateDummyCUESheet(_config, pathIn);
                sr = new StringReader(cueSheet);
                _logFiles = new List<CUEToolsSourceFile>();
                _defaultLog = Path.GetFileNameWithoutExtension(pathIn);
                foreach (string logPath in Directory.GetFiles(_inputDir, "*.log"))
                    try { _logFiles.Add(new CUEToolsSourceFile(logPath, new StreamReader(logPath, CUESheet.Encoding))); }
                    catch { }
            }
            else
            {
                string extension = Path.GetExtension(pathIn).ToLower();
                sr = null;
                CUEToolsFormat fmt;
                if (!extension.StartsWith(".") || !_config.formats.TryGetValue(extension.Substring(1), out fmt) || !fmt.allowLossless)
                    throw new Exception("Unknown input format.");
                if (fmt.allowEmbed)
                {
                    string cuesheetTag = null;
                    TagLib.File fileInfo;
                    GetSampleLength(pathIn, out fileInfo);
                    NameValueCollection tags = Tagging.Analyze(fileInfo);
                    cuesheetTag = tags.Get("CUESHEET");
                    _accurateRipId = tags.Get("ACCURATERIPID");
                    _eacLog = tags.Get("LOG");
                    if (_eacLog == null) _eacLog = tags.Get("LOGFILE");
                    if (_eacLog == null) _eacLog = tags.Get("EACLOG");
                    if (cuesheetTag != null)
                    {
                        sr = new StringReader(cuesheetTag);
                        _hasEmbeddedCUESheet = true;
                    }
                }
                if (!_hasEmbeddedCUESheet)
                {
                    string cueSheet = CUESheet.CreateDummyCUESheet(_config, pathIn);
                    if (cueSheet == null)
                        throw new Exception("Input file doesn't seem to contain a cue sheet or be part of an album.");
                    sr = new StringReader(cueSheet);
                    _logFiles = new List<CUEToolsSourceFile>();
                    foreach (string logPath in Directory.GetFiles(_inputDir, "*.log"))
                        try { _logFiles.Add(new CUEToolsSourceFile(logPath, new StreamReader(logPath, CUESheet.Encoding))); }
                        catch { }
                }
            }

            OpenCUE(sr);
        }
Ejemplo n.º 2
0
        public string ExecuteScript(CUEToolsScript script)
        {
            if (!script.builtin)
                return ExecuteScript(script.code);

            switch (script.name)
            {
                case "default":
                    return Go();
                case "only if found":
                    return ArVerify.ExceptionStatus != WebExceptionStatus.Success ? WriteReport() : Go();
                case "repair":
                    {
                        UseCUEToolsDB("CUETools " + CUEToolsVersion, null, true, CTDBMetadataSearch.None);
                        Action = CUEAction.Verify;
                        if (CTDB.DBStatus != null)
                            return CTDB.DBStatus;
                        bool useAR = isUsingAccurateRip;
                        isUsingAccurateRip = true;
                        Go();
                        isUsingAccurateRip = useAR;
                        List<CUEToolsSourceFile> choices = new List<CUEToolsSourceFile>();
                        foreach (DBEntry entry in CTDB.Entries)
                            if (!entry.hasErrors || entry.canRecover)
                            {
                                StringBuilder sb = new StringBuilder();
                                if (entry.hasErrors)
                                {
                                    sb.AppendFormat("Affected positions:\n");
                                    for (int sec = 0; sec < entry.repair.AffectedSectorArray.Length; sec++)
                                        if (entry.repair.AffectedSectorArray[sec])
                                            sb.AppendFormat("{0}\n", CDImageLayout.TimeToString((uint)sec));
                                }
                                CUEToolsSourceFile choice = new CUEToolsSourceFile(entry.Status, new StringReader(sb.ToString()));
                                choice.data = entry;
                                choices.Add(choice);
                            }
                        CUEToolsSourceFile selectedEntry = ChooseFile(choices, null, true);
                        if (selectedEntry == null)
                            return CTDB.Status;
                        CTDB.SelectedEntry = (DBEntry)selectedEntry.data;
                        if (!CTDB.SelectedEntry.hasErrors)
                            return CTDB.Status;
                        isUsingCUEToolsDBFix = true;
                        Action = CUEAction.Encode;
                        return Go();
                    }
                case "fix offset":
                    {
                        if (ArVerify.ExceptionStatus != WebExceptionStatus.Success)
                            return WriteReport();

                        WriteOffset = 0;
                        Action = CUEAction.Verify;
                        string status = Go();

                        uint tracksMatch;
                        int bestOffset;
                        FindBestOffset(Config.fixOffsetMinimumConfidence, !Config.fixOffsetToNearest, out tracksMatch, out bestOffset);
                        if (tracksMatch * 100 >= Config.fixOffsetMinimumTracksPercent * TrackCount)
                        {
                            WriteOffset = bestOffset;
                            Action = CUEAction.Encode;
                            status = Go();
                        }
                        return status;
                    }

                case "encode if verified":
                    {
                        if (ArVerify.ExceptionStatus != WebExceptionStatus.Success)
                            return WriteReport();

                        Action = CUEAction.Verify;
                        string status = Go();

                        uint tracksMatch;
                        int bestOffset;
                        FindBestOffset(Config.encodeWhenConfidence, false, out tracksMatch, out bestOffset);
                        if (tracksMatch * 100 >= Config.encodeWhenPercent * TrackCount && (!_config.encodeWhenZeroOffset || bestOffset == 0))
                        {
                            Action = CUEAction.Encode;
                            status = Go();
                        }
                        return status;
                    }
            }

            return "internal error";
        }