Exemplo n.º 1
0
        public bool ReadTXTHeader(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                return(false);
            }

            this.Folder = Path.GetDirectoryName(FilePath);

            foreach (string folder in CConfig.SongFolder)
            {
                if (this.Folder.Contains(folder))
                {
                    if (this.Folder.Length == folder.Length)
                    {
                        this.FolderName = "Songs";
                    }
                    else
                    {
                        this.FolderName = this.Folder.Substring(folder.Length + 1, this.Folder.Length - folder.Length - 1);

                        string str = this.FolderName;
                        try
                        {
                            str = this.FolderName.Substring(0, this.FolderName.IndexOf("\\"));
                        }
                        catch (Exception)
                        {
                        }
                        this.FolderName = str;
                    }
                }
            }

            this.FileName = Path.GetFileName(FilePath);

            EHeaderFlags HeaderFlags = new EHeaderFlags();
            StreamReader sr;

            try
            {
                sr = new StreamReader(FilePath, Encoding.Default, true);

                string line = sr.ReadLine();
                if (line.Length == 0)
                {
                    return(false);
                }

                int    pos        = -1;
                string Identifier = String.Empty;
                string Value      = String.Empty;

                while ((line.Length == 0) || (line[0].ToString().Equals("#")))
                {
                    pos = line.IndexOf(":");

                    if (pos > 0)
                    {
                        Identifier = line.Substring(1, pos - 1).Trim().ToUpper();
                        Value      = line.Substring(pos + 1, line.Length - pos - 1).Trim();

                        if (Value.Length > 0)
                        {
                            switch (Identifier)
                            {
                            case "ENCODING":
                                this.Encoding = CEncoding.GetEncoding(Value);
                                sr            = new StreamReader(FilePath, this.Encoding);
                                Identifier    = String.Empty;
                                line          = sr.ReadLine();

                                while ((line.Length == 0) || (line[0].ToString().Equals("#")) && (Identifier != "ENCODING"))
                                {
                                    pos = line.IndexOf(":");

                                    if (pos > 0)
                                    {
                                        Identifier = line.Substring(1, pos - 1).Trim().ToUpper();
                                        Value      = line.Substring(pos + 1, line.Length - pos - 1).Trim();
                                    }

                                    if (!sr.EndOfStream)
                                    {
                                        if (Identifier == "ENCODING")
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            line = sr.ReadLine();
                                        }
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                break;

                            case "TITLE":
                                if (Value != String.Empty)
                                {
                                    this.Title   = Value;
                                    HeaderFlags |= EHeaderFlags.Title;
                                }
                                break;

                            case "ARTIST":
                                if (Value != String.Empty)
                                {
                                    this.Artist  = Value;
                                    HeaderFlags |= EHeaderFlags.Artist;
                                }
                                break;

                            case "TITLE-ON-SORTING":
                                if (Value != String.Empty)
                                {
                                    this.TitleSorting = Value;
                                }
                                break;

                            case "ARTIST-ON-SORTING":
                                if (Value != String.Empty)
                                {
                                    this.ArtistSorting = Value;
                                }
                                break;

                            case "P1":
                                if (Value != String.Empty)
                                {
                                    this.DuetPart1 = Value;
                                }
                                break;

                            case "P2":
                                if (Value != String.Empty)
                                {
                                    this.DuetPart2 = Value;
                                }
                                break;

                            case "MP3":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.MP3FileName = Value;
                                    HeaderFlags     |= EHeaderFlags.MP3;
                                }
                                else
                                {
                                    CLog.LogError("Can't find audio file: " + Path.Combine(this.Folder, Value));
                                    return(false);
                                }
                                break;

                            case "BPM":
                                if (CHelper.TryParse(Value, out this.BPM))
                                {
                                    this.BPM    *= 4;
                                    HeaderFlags |= EHeaderFlags.BPM;
                                }
                                break;

                            case "EDITION":
                                if (Value.Length > 1)
                                {
                                    this.Edition.Add(Value);
                                }
                                break;

                            case "GENRE":
                                if (Value.Length > 1)
                                {
                                    this.Genre.Add(Value);
                                }
                                break;

                            case "YEAR":
                                int num = 0;
                                if (Value.Length == 4 && int.TryParse(Value, out num))
                                {
                                    this.Year = Value;
                                }
                                break;

                            case "LANGUAGE":
                                if (Value.Length > 1)
                                {
                                    this.Language.Add(Value);
                                }
                                break;

                            case "COMMENT":
                                if (Value.Length > 1)
                                {
                                    this.Comment.Add(Value);
                                }
                                break;

                            case "GAP":
                                if (CHelper.TryParse(Value, out this.Gap))
                                {
                                    this.Gap /= 1000f;
                                }
                                break;

                            case "COVER":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.CoverFileName = Value;
                                }
                                break;

                            case "BACKGROUND":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.BackgroundFileName = Value;
                                }
                                break;

                            case "VIDEO":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.VideoFileName = Value;
                                }
                                else
                                {
                                    CLog.LogError("Can't find video file: " + Path.Combine(this.Folder, Value));
                                }

                                break;

                            case "VIDEOGAP":
                                CHelper.TryParse(Value, out this.VideoGap);
                                break;

                            case "VIDEOASPECT":
                                CHelper.TryParse <EAspect>(Value, out this.VideoAspect, true);
                                break;

                            case "START":
                                CHelper.TryParse(Value, out this.Start);
                                break;

                            case "END":
                                if (CHelper.TryParse(Value, out this.Finish))
                                {
                                    this.Finish /= 1000f;
                                }
                                break;

                            case "PREVIEWSTART":
                                if (CHelper.TryParse(Value, out this.PreviewStart))
                                {
                                    if (this.PreviewStart < 0f)
                                    {
                                        this.PreviewStart = 0f;
                                    }
                                    else
                                    {
                                        HeaderFlags |= EHeaderFlags.PreviewStart;
                                    }
                                }
                                break;

                            case "MEDLEYSTARTBEAT":
                                if (int.TryParse(Value, out this.Medley.StartBeat))
                                {
                                    HeaderFlags |= EHeaderFlags.MedleyStartBeat;
                                }
                                break;

                            case "MEDLEYENDBEAT":
                                if (int.TryParse(Value, out this.Medley.EndBeat))
                                {
                                    HeaderFlags |= EHeaderFlags.MedleyEndBeat;
                                }
                                break;

                            case "CALCMEDLEY":
                                if (Value.ToUpper() == "OFF")
                                {
                                    this.CalculateMedley = false;
                                }
                                break;

                            case "RELATIVE":
                                if (Value.ToUpper() == "YES" && Value.ToUpper() != "NO")
                                {
                                    this.Relative = EOffOn.TR_CONFIG_ON;
                                }
                                break;

                            default:
                                ;
                                break;
                            }
                        }
                    }

                    if (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                    }
                    else
                    {
                        return(false);
                    }
                } //end of while

                if ((HeaderFlags & EHeaderFlags.Title) == 0)
                {
                    CLog.LogError("Title tag missing: " + FilePath);
                    return(false);
                }

                if ((HeaderFlags & EHeaderFlags.Artist) == 0)
                {
                    CLog.LogError("Artist tag missing: " + FilePath);
                    return(false);
                }

                if ((HeaderFlags & EHeaderFlags.MP3) == 0)
                {
                    CLog.LogError("MP3 tag missing: " + FilePath);
                    return(false);
                }

                if ((HeaderFlags & EHeaderFlags.BPM) == 0)
                {
                    CLog.LogError("BPM tag missing: " + FilePath);
                    return(false);
                }

                if (this.Relative == EOffOn.TR_CONFIG_ON)
                {
                    CLog.LogError("Relative songs are not supported by Vocaluxe (perhaps later)! (" + FilePath + ")");
                    return(false);
                }

                #region check medley tags
                if ((HeaderFlags & EHeaderFlags.MedleyStartBeat) != 0 && (HeaderFlags & EHeaderFlags.MedleyEndBeat) != 0)
                {
                    if (this.Medley.StartBeat > this.Medley.EndBeat)
                    {
                        CLog.LogError("MedleyStartBeat is bigger than MedleyEndBeat in file: " + FilePath);
                        HeaderFlags = HeaderFlags - EHeaderFlags.MedleyStartBeat - EHeaderFlags.MedleyEndBeat;
                    }
                }

                if ((HeaderFlags & EHeaderFlags.PreviewStart) == 0 || this.PreviewStart == 0f)
                {
                    //PreviewStart is not set or <=0
                    if ((HeaderFlags & EHeaderFlags.MedleyStartBeat) != 0)
                    {
                        //fallback to MedleyStart
                        this.PreviewStart = CGame.GetTimeFromBeats(this.Medley.StartBeat, this.BPM);
                    }
                    else
                    {
                        //else set to 0, it will be set in FindRefrainStart
                        this.PreviewStart = 0f;
                    }
                }

                if ((HeaderFlags & EHeaderFlags.MedleyStartBeat) != 0 && (HeaderFlags & EHeaderFlags.MedleyEndBeat) != 0)
                {
                    this.Medley.Source      = EMedleySource.Tag;
                    this.Medley.FadeInTime  = CSettings.DefaultMedleyFadeInTime;
                    this.Medley.FadeOutTime = CSettings.DefaultMedleyFadeOutTime;
                }
                #endregion check medley tags

                this.Encoding = sr.CurrentEncoding;
            }
            catch (Exception e)
            {
                CLog.LogError("Error reading txt header in file \"" + FilePath + "\": " + e.Message);
                return(false);
            }

            CheckFiles();

            //Before saving this tags to .txt: Check, if ArtistSorting and Artist are equal, then don't save this tag.
            if (this.ArtistSorting == String.Empty)
            {
                this.ArtistSorting = this.Artist;
            }

            if (this.TitleSorting == String.Empty)
            {
                this.TitleSorting = this.Title;
            }

            return(true);
        }
Exemplo n.º 2
0
        public bool ReadTXTHeader(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                return(false);
            }

            this.Folder = Path.GetDirectoryName(FilePath);

            foreach (string folder in CConfig.SongFolder)
            {
                if (this.Folder.Contains(folder))
                {
                    if (this.Folder.Length == folder.Length)
                    {
                        this.FolderName = "Songs";
                    }
                    else
                    {
                        this.FolderName = this.Folder.Substring(folder.Length + 1, this.Folder.Length - folder.Length - 1);

                        string str = this.FolderName;
                        try
                        {
                            str = this.FolderName.Substring(0, this.FolderName.IndexOf("\\"));
                        }
                        catch (Exception)
                        {
                        }
                        this.FolderName = str;
                    }
                }
            }

            this.FileName = Path.GetFileName(FilePath);

            EHeaderFlags HeaderFlags = new EHeaderFlags();
            StreamReader sr;

            try
            {
                sr = new StreamReader(FilePath, Encoding.Default, true);

                string line = sr.ReadLine();
                if (line.Length == 0)
                {
                    return(false);
                }

                int    pos        = -1;
                string Identifier = String.Empty;
                string Value      = String.Empty;

                while ((line.Length == 0) || (line[0].ToString().Equals("#")))
                {
                    pos = line.IndexOf(":");

                    if (pos > 0)
                    {
                        Identifier = line.Substring(1, pos - 1).Trim().ToUpper();
                        Value      = line.Substring(pos + 1, line.Length - pos - 1).Trim();

                        if (Value.Length > 0)
                        {
                            switch (Identifier)
                            {
                            case "ENCODING":
                                this.Encoding = CEncoding.GetEncoding(Value);
                                sr            = new StreamReader(FilePath, this.Encoding);
                                Identifier    = String.Empty;
                                line          = sr.ReadLine();

                                while ((line.Length == 0) || (line[0].ToString().Equals("#")) && (Identifier != "ENCODING"))
                                {
                                    pos = line.IndexOf(":");

                                    if (pos > 0)
                                    {
                                        Identifier = line.Substring(1, pos - 1).Trim().ToUpper();
                                        Value      = line.Substring(pos + 1, line.Length - pos - 1).Trim();
                                    }

                                    if (!sr.EndOfStream)
                                    {
                                        if (Identifier == "ENCODING")
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            line = sr.ReadLine();
                                        }
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                break;

                            case "TITLE":
                                if (Value != String.Empty)
                                {
                                    this.Title   = Value;
                                    HeaderFlags |= EHeaderFlags.Title;
                                }
                                break;

                            case "ARTIST":
                                if (Value != String.Empty)
                                {
                                    this.Artist  = Value;
                                    HeaderFlags |= EHeaderFlags.Artist;
                                }
                                break;

                            case "MP3":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.MP3FileName = Value;
                                    HeaderFlags     |= EHeaderFlags.MP3;
                                }
                                else
                                {
                                    CLog.LogError("Can't find audio file: " + Path.Combine(this.Folder, Value));
                                    return(false);
                                }
                                break;

                            case "BPM":
                                if (CHelper.TryParse(Value, out this.BPM))
                                {
                                    this.BPM    *= 4;
                                    HeaderFlags |= EHeaderFlags.BPM;
                                }
                                break;

                            case "EDITION":
                                if (Value.Length > 1)
                                {
                                    this.Edition.Add(Value);
                                }
                                break;

                            case "GENRE":
                                if (Value.Length > 1)
                                {
                                    this.Genre.Add(Value);
                                }
                                break;

                            case "YEAR":
                                int num = 0;
                                if (Value.Length == 4 && int.TryParse(Value, out num))
                                {
                                    this.Year = Value;
                                }
                                break;

                            case "LANGUAGE":
                                if (Value.Length > 1)
                                {
                                    this.Language.Add(Value);
                                }
                                break;

                            case "COMMENT":
                                if (Value.Length > 1)
                                {
                                    this.Comment.Add(Value);
                                }
                                break;

                            case "GAP":
                                if (CHelper.TryParse(Value, out this.Gap))
                                {
                                    this.Gap /= 1000f;
                                }
                                break;

                            case "COVER":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.CoverFileName = Value;
                                }
                                break;

                            case "BACKGROUND":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.BackgroundFileName = Value;
                                }
                                break;

                            case "VIDEO":
                                if (File.Exists(Path.Combine(this.Folder, Value)))
                                {
                                    this.VideoFileName = Value;
                                }
                                else
                                {
                                    CLog.LogError("Can't find video file: " + Path.Combine(this.Folder, Value));
                                }

                                break;

                            case "VIDEOGAP":
                                CHelper.TryParse(Value, out this.VideoGap);
                                break;

                            case "VIDEOASPECT":
                                CHelper.TryParse <EAspect>(Value, out this.VideoAspect, true);
                                break;

                            case "START":
                                CHelper.TryParse(Value, out this.Start);
                                break;

                            case "END":
                                if (CHelper.TryParse(Value, out this.Finish))
                                {
                                    this.Finish /= 1000f;
                                }
                                break;

                            default:
                                ;
                                break;
                            }
                        }
                    }

                    if (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                    }
                    else
                    {
                        return(false);
                    }
                } //end of while

                if (HeaderFlags != (EHeaderFlags.Title | EHeaderFlags.Artist | EHeaderFlags.MP3 | EHeaderFlags.BPM))
                {
                    return(false);
                }

                this.Encoding = sr.CurrentEncoding;
            }
            catch (Exception)
            {
                return(false);
            }

            CheckFiles();

            return(true);
        }