コード例 #1
0
ファイル: OpenChart.cs プロジェクト: bobwister/Unity-ITGHD
    public Dictionary<Difficulty, Song> readChart(string directory)
    {
        //read file

        var files = (string[]) Directory.GetFiles(directory);

        var stream = files.FirstOrDefault(c => (c.ToLower().Contains(".sm")) && !c.ToLower().Contains(".old")  && !c.ToLower().Contains(".dwi") && !c.Contains("._"));
        if(stream == null) stream = files.FirstOrDefault(c => (c.ToLower().Contains(".dwi"))   && !c.ToLower().Contains(".old") && !c.Contains("._"));
        if(stream == null) return null;
        StreamReader sr = new StreamReader(stream);
        songContent = sr.ReadToEnd();
        sr.Close();

        //split all line and put on a list
        var thesong = songContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        List<string> listLine = new List<string>();
        listLine.AddRange(thesong);
        List<int> indexNotes = new List<int>();

        //traitement des commentaires.
        /* Non : Impossible de savoir si single ou double :(
        for(int i=0; i < listLine.Count;i++){
            if(listLine.ElementAt(i).Contains("//")){
                listLine[i] = listLine.ElementAt(i).Split("//".ToCharArray())[0];
            }
        }*/

        for(int i=0; i < listLine.Count;i++){
            if(listLine.ElementAt(i).Contains("NOTES")){
                indexNotes.Add(i);
            }
        }

        Dictionary<Difficulty, Song>  outputSongs = new Dictionary<Difficulty, Song> ();

        try{
            //get generic information
            var thetitle = listLine.FirstOrDefault(c => c.Contains("TITLE")).Split(':');
            var title = thetitle[1].Replace(";","");
            var thesubtitle = listLine.FirstOrDefault(c => c.Contains("SUBTITLE")).Split(':');
            var subtitle = thesubtitle[1].Replace(";","");
            var theartist = listLine.FirstOrDefault(c => c.Contains("ARTIST")).Split(':');
            var artist = theartist[1].Replace(";","");
            var theoffset = listLine.FirstOrDefault(c => c.Contains("OFFSET")).Split(':');
            if(theoffset[1].Contains("//")) theoffset[1] = theoffset[1].Split('/')[0]; //Special Destination fix
            var offset = System.Convert.ToDouble(theoffset[1].Replace(";",""));
            var thesamplestart = listLine.FirstOrDefault(c => c.Contains("SAMPLESTART")).Split(':');
            var samplestart = System.Convert.ToDouble(thesamplestart[1].Replace(";",""));
            var thesamplelenght = listLine.FirstOrDefault(c => c.Contains("SAMPLELENGTH")).Split(':');
            var samplelenght = System.Convert.ToDouble(thesamplelenght[1].Replace(";",""));
            //"file://" +
            var theBanner = listLine.FirstOrDefault(c => c.Contains("BANNER")).Split(':');
            var bannerTemp = "";
            if(!String.IsNullOrEmpty(theBanner[1].Replace(";","")))
            {
                bannerTemp = files.FirstOrDefault(c => c.Contains(theBanner[1].Replace(";","")));
            }else{
                bannerTemp = files.FirstOrDefault(c => (c.ToLower().Contains("bn") || c.ToLower().Contains("banner")) && (c.ToLower().Contains(".png") || c.ToLower().Contains(".jpg") || c.ToLower().Contains(".bmp") || c.ToLower().Contains(".jpeg")));
            }
            var banner = "noBanner";
            if(!String.IsNullOrEmpty(bannerTemp)){
                banner = "file://" +  bannerTemp.Replace('\\', '/');
            }
            var theBpmListMesured = new Dictionary<double, double>();
            var theStopListMesured = new Dictionary<double, double>();

            var theBpmList = new Dictionary<double, double>();
            var theStopList = new Dictionary<double, double>();

            var theBpmMesureList = new List<double>();
            var theStopMesureList = new List<double>();

            //getting bpms with mesure
            string[] thebpm = listLine.FirstOrDefault(c => c.Contains("BPMS")).Split(':');
            string thebpmline = thebpm[1];
            string[] splitbpm = thebpmline.Split(',');
            /*double previousbps = 0;
            double previoustime = 0;
            double previousmesure = 0;
            List<double> timeToMesureBPM = new List<double>();*/
            foreach(string s in splitbpm){
                string[] mysplit = s.Replace(";","").Split('=');
                if(!theBpmListMesured.ContainsKey(System.Convert.ToDouble(mysplit[0]))) theBpmListMesured.Add(System.Convert.ToDouble(mysplit[0]), System.Convert.ToDouble(mysplit[1]));
                //theBpmList.Add(previousbps == 0 ? 0 : previoustime + (System.Convert.ToDouble(mysplit[0]) - previousmesure)/previousbps, System.Convert.ToDouble(mysplit[1]));
                /*previousbps = System.Convert.ToDouble(mysplit[1])/(double)60.0;
                previoustime = theBpmList.Last().Key;
                previousmesure = System.Convert.ToDouble(mysplit[0]);
                timeToMesureBPM.Add(System.Convert.ToDouble(mysplit[0]));
                Debug.Log ("bpm : " + theBpmList.Last().Key);*/
            }

            //getting stops mesured

            int stopbegin = listLine.IndexOf(listLine.FirstOrDefault(c => c.Contains("STOPS")));
            string thestop = "";
            bool stopTheRool = false;
            if(!listLine.ElementAt(stopbegin).Contains("STOPS:;")){
                for(int i=stopbegin; !stopTheRool; i++){
                    if( listLine.ElementAt(i).Contains("//")){
                        thestop += listLine.ElementAt(i).Split('/')[0];
                    }else{
                        thestop += listLine.ElementAt(i);
                    }

                    if(listLine.ElementAt(i).Contains(";")) stopTheRool = true;
                }
                thestop = thestop.Replace("/n", "");
                thestop = thestop.Replace(";", "");
            }

            if(thestop != ""){
                string[] thestopsplit = thestop.Split(':');
                string thestopline = thestopsplit[1];
                string[] splitstop = thestopline.Split(',');
                /*previousbps = theBpmList.First().Value;
                previoustime = 0;
                previousmesure = 0;*/
                foreach(string s in splitstop){

                    var mysplit = s.Split('=');

                    theStopListMesured.Add(System.Convert.ToDouble(mysplit[0]), System.Convert.ToDouble(mysplit[1]));
                    /*
                    var theMesure = timeToMesureBPM.IndexOf(timeToMesureBPM.FirstOrDefault(d => d == timeToMesureBPM.Where(c => c <= System.Convert.ToDouble(mysplit[0])).Max()));
                    previousbps = System.Convert.ToDouble(theBpmList.ElementAt(theMesure).Value)/(double)60.0;
                    previoustime = theBpmList.ElementAt(theMesure).Key;
                    previousmesure = timeToMesureBPM.ElementAt(theMesure);

                    theStopList.Add(previoustime + ((System.Convert.ToDouble(mysplit[0])) - previousmesure)/previousbps, System.Convert.ToDouble(mysplit[1]));

                    Debug.Log("real mesure : " + (System.Convert.ToDouble(mysplit[0])) + " analyse : " + theStopList.Last().Key + " : " + theStopList.Last().Value + " : " +
                        theMesure + " previous time : " + previoustime + " previous mesure : " +
                        previousmesure + " previous bps " + previousbps + " (" + previousbps*60.0 + ") bpm");*/
                }
            }

            //change bpm and stops mesured by time
            //Bug encore.
            double previousbps = 0;
            double stoptime = 0;
            double previoustime = 0;
            double previousmesure = 0;

            while(theStopListMesured.Count != 0 && theBpmListMesured.Count != 0){
                if((theStopListMesured.First().Key < theBpmListMesured.First().Key)){

                    theStopList.Add(previoustime + stoptime + (theStopListMesured.First().Key - previousmesure)/previousbps, theStopListMesured.First().Value);
                    theStopMesureList.Add(theStopListMesured.First().Key);

                    previoustime += (theStopListMesured.First().Key - previousmesure)/previousbps;
                    previousmesure = theStopListMesured.First().Key;
                    stoptime += theStopListMesured.First().Value;

                    theStopListMesured.Remove(theStopListMesured.First().Key);

                }else if((theStopListMesured.First().Key > theBpmListMesured.First().Key)){

                    theBpmList.Add(previousbps == 0 ? 0 : previoustime + stoptime + (theBpmListMesured.First().Key - previousmesure)/previousbps, theBpmListMesured.First().Value);
                    theBpmMesureList.Add(theBpmListMesured.First().Key);

                    previoustime += (previousbps == 0 ? 0 : (theBpmListMesured.First().Key - previousmesure)/previousbps);
                    previousbps = theBpmList.Last().Value/(double)60.0;
                    previousmesure = theBpmListMesured.First().Key;

                    theBpmListMesured.Remove(theBpmListMesured.First().Key);

                }else if(theStopListMesured.First().Key == theBpmListMesured.First().Key){

                    theStopList.Add(previousbps == 0 ? 0 : previoustime + stoptime + (theStopListMesured.First().Key - previousmesure)/previousbps, theStopListMesured.First().Value);
                    theStopMesureList.Add(theStopListMesured.First().Key);

                    theBpmList.Add(previousbps == 0 ? 0 : previoustime + stoptime + (theBpmListMesured.First().Key - previousmesure)/previousbps, theBpmListMesured.First().Value);
                    theBpmMesureList.Add(theBpmListMesured.First().Key);

                    previoustime += (previousbps == 0 ? 0 : (theBpmListMesured.First().Key - previousmesure)/previousbps);
                    previousbps = theBpmList.Last().Value/(double)60.0;
                    previousmesure = theBpmListMesured.First().Key;
                    stoptime += theStopListMesured.First().Value;

                    theStopListMesured.Remove(theStopListMesured.First().Key);
                    theBpmListMesured.Remove(theBpmListMesured.First().Key);
                }

            }

            while(theStopListMesured.Count != 0){

                    theStopList.Add(previoustime + stoptime + (theStopListMesured.First().Key - previousmesure)/previousbps, theStopListMesured.First().Value);
                    theStopMesureList.Add(theStopListMesured.First().Key);

                    previoustime += (theStopListMesured.First().Key - previousmesure)/previousbps;
                    previousmesure = theStopListMesured.First().Key;
                    stoptime += theStopListMesured.First().Value;

                    theStopListMesured.Remove(theStopListMesured.First().Key);

            }

            while(theBpmListMesured.Count != 0){

                    theBpmList.Add(previousbps == 0 ? 0 : previoustime + stoptime + (theBpmListMesured.First().Key - previousmesure)/previousbps, theBpmListMesured.First().Value);
                    theBpmMesureList.Add(theBpmListMesured.First().Key);

                    previoustime += (previousbps == 0 ? 0 : (theBpmListMesured.First().Key - previousmesure)/previousbps);
                    previousbps = theBpmList.Last().Value/(double)60.0;
                    previousmesure = theBpmListMesured.First().Key;

                    theBpmListMesured.Remove(theBpmListMesured.First().Key);

            }

            var thedisplayBPM = "";
            if(listLine.FirstOrDefault(c => c.Contains("DISPLAYBPM")) != null){
                try{
                    string[] thes = listLine.FirstOrDefault(c => c.Contains("DISPLAYBPM")).Split(':');
                    if(thes.Count() > 2){
                        thedisplayBPM = System.Convert.ToDouble(thes[1].Replace(";", "")).ToString("0") + " -> " + System.Convert.ToDouble(thes[2].Replace(";", "")).ToString("0");
                    }else{
                        thedisplayBPM = System.Convert.ToDouble(thes[1].Replace(";", "")).ToString("0");
                    }
                }catch{ //Special Gravity Blast fix
                    string[] thes = listLine.FirstOrDefault(c => c.Contains("DISPLAYBPM")).Split(':');
                    var theindex = 0;
                    for(int o=0; o<thes.Count(); o++){
                        if(thes[o].Contains("DISPLAYBPM")){
                            theindex = o + 1;
                        }
                    }
                    if((thes.Count() - theindex - 1) > 2){
                        thedisplayBPM = System.Convert.ToDouble(thes[theindex].Replace(";", "")).ToString("0") + " -> " + System.Convert.ToDouble(thes[theindex+1].Replace(";", "")).ToString("0");
                    }else{
                        thedisplayBPM = System.Convert.ToDouble(thes[theindex].Replace(";", "")).ToString("0");
                    }
                }
            }else{
                var themin = theBpmList.Min(c => c.Value);
                var themax = theBpmList.Max(c => c.Value);
                if(themin == themax){
                    thedisplayBPM = themax.ToString("0");
                }else{
                    thedisplayBPM = themin.ToString("0") + " -> " + themax.ToString("0");
                }
            }

            //debug
            /*foreach(var el in theStopList){
                Debug.Log(el.Key);
            }*/

            //For all difficulties
            foreach(int index in indexNotes){

                var theNewsong = new Song();

                //easy variable getted
                theNewsong.title = title;
                theNewsong.subtitle = subtitle;
                theNewsong.artist = artist;
                theNewsong.banner = banner;
                theNewsong.offset = offset;
                theNewsong.samplestart = samplestart;
                theNewsong.samplelenght = samplelenght;
                theNewsong.bpms = theBpmList;
                theNewsong.stops = theStopList;
                theNewsong.mesureBPMS = theBpmMesureList;
                theNewsong.mesureSTOPS = theStopMesureList;
                theNewsong.bpmToDisplay = thedisplayBPM;
                /*if(files.FirstOrDefault(c => c.Contains(".ogg")) == null){
                    foreach(var fil in files){
                        Debug.Log(fil);
                    }
                }*/
                theNewsong.song = "file://" + files.FirstOrDefault(c => c.Contains(".ogg") || c.Contains(".OGG")).Replace('\\', '/');

                //getting song information
                int beginInformation = index;
                string dl = "";
                var theinfo = listLine.ElementAt(beginInformation + 1).Replace(":","").Trim();
                if(theinfo.Contains("double")){
                    dl = "D";
                }else
                if(!theinfo.Contains("single") || theinfo.Contains("pump") || theinfo.Contains("ez2") ||
                        theinfo.Contains("para") || theinfo.Contains("ds3ddx") || theinfo.Contains("pnm") ||
                        theinfo.Contains("bm") || theinfo.Contains("maniax") || theinfo.Contains("techno")){
                    dl = "STOP";
                }
                theNewsong.stepartist = listLine.ElementAt(beginInformation + 2).Replace(":","").Trim();
                theNewsong.setDifficulty(dl + listLine.ElementAt(beginInformation + 3).Replace(":","").Trim());
                theNewsong.level = System.Convert.ToInt32(listLine.ElementAt(beginInformation + 4).Replace(":","").Trim());

                //getting stepchart
                int beginstepchart = beginInformation+6;
                while(listLine.ElementAt(beginstepchart).Contains("//") ||
                    String.IsNullOrEmpty(listLine.ElementAt(beginstepchart).Trim()) ||
                    listLine.ElementAt(beginstepchart) == ""){

                        if(listLine.ElementAt(beginstepchart).Contains("NOTES")) dl = "STOP";
                        beginstepchart++;
                }

                if(listLine.ElementAt(beginstepchart).Contains("NOTES")) dl = "STOP";
                //if(theNewsong.title == "The Last Kiss") Debug.Log(listLine.ElementAt(beginstepchart));

                if(dl != "STOP"){
                    var numberOfSteps = 0;
                    var numberOfMines = 0;
                    var numberOfRoll = 0;
                    var numberOfFreezes = 0;
                    var numberOfJump = 0;
                    var numberOfStepsWJ = 0;
                    theNewsong.stepchart.Add(new List<string>());
                    for(int i = beginstepchart; !listLine.ElementAt(i).Contains(";"); i++){
                        if(listLine.ElementAt(i).Contains(",")){
                            theNewsong.stepchart.Add(new List<string>());
                        }else if(!String.IsNullOrEmpty(listLine.ElementAt(i))){
                            theNewsong.stepchart.Last().Add(listLine.ElementAt(i));
                            numberOfSteps += listLine.ElementAt(i).Count(c => c == '1');
                            numberOfSteps += listLine.ElementAt(i).Count(c => c == '2');
                            numberOfSteps += listLine.ElementAt(i).Count(c => c == '4');
                            numberOfFreezes += listLine.ElementAt(i).Count(c => c == '2');
                            numberOfRoll += listLine.ElementAt(i).Count(c => c == '4');
                            numberOfMines += listLine.ElementAt(i).Count(c => c == 'M');
                            numberOfStepsWJ += listLine.ElementAt(i).Count(c => c == '1');
                            numberOfStepsWJ += listLine.ElementAt(i).Count(c => c == '2');
                            numberOfStepsWJ += listLine.ElementAt(i).Count(c => c == '4');

                            var countmesure = listLine.ElementAt(i).Count(c => c == '1') + listLine.ElementAt(i).Count(c => c == '2') + listLine.ElementAt(i).Count(c => c == '4');
                            if(countmesure == 2){
                                numberOfStepsWJ -= countmesure;
                                numberOfJump++;
                            }
                            if(countmesure >= 3){
                                numberOfStepsWJ -= countmesure;
                            }
                        }
                    }

                    theNewsong.numberOfSteps = numberOfSteps;
                    theNewsong.numberOfFreezes = numberOfFreezes;
                    theNewsong.numberOfRolls = numberOfRoll;
                    theNewsong.numberOfMines = numberOfMines;
                    theNewsong.numberOfJumps = numberOfJump;
                    theNewsong.numberOfStepsWithoutJumps = numberOfStepsWJ;
                    //A faire le mode double !
                    if(dl == "") fakeCreation(theNewsong);
                        //A mettre
                    //if(outputSongs.ContainsKey(theNewsong.difficulty))
                    theNewsong.sip = new SongInfoProfil(theNewsong.title, theNewsong.subtitle,
                        theNewsong.numberOfSteps, theNewsong.difficulty, theNewsong.level);
                    outputSongs.Add(theNewsong.difficulty, theNewsong);
                }

            }
        }catch(Exception e){
            Debug.Log(directory + " // " + e.Message + " //st : " + e.StackTrace);
        }

        return outputSongs;
    }