예제 #1
0
 internal static void SetCurrent(string filename)
 {
     Current =
         BeatmapAvailable.Find(b => b.Filename.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
 }
예제 #2
0
        internal static void ProcessHeaders(Beatmap b)
        {
            b.TimingPoints.Clear();
            b.PreviewTime = -1;
            b.AudioLeadIn = 0;

            b.ObjectCount = 0;
            b.DrainLength = 0;

            b.countSlider    = 0;
            b.countNormal    = 0;
            b.countSpinner   = 0;
            b.BeatmapVersion = 4;

            b.VersionOffset = 0;

            b.StackLeniency = 0.7f;

            b.Countdown = Countdown.Normal;

            b.CustomColours = false;

            b.Mode = 0;

            b.DefaultSampleSet        = SampleSet.Normal;
            AudioEngine.CustomSamples = false;

            b.IncorrectChecksum = false;

            b.BeatmapPresent = File.Exists(b.Filename);
            if (!b.BeatmapPresent)
            {
                return;
            }

            b.BeatmapChecksum = CryptoHelper.GetMd5(b.Filename);

            StreamReader r = File.OpenText(b.Filename);

            b.DateModified = File.GetLastWriteTimeUtc(b.Filename);

            FileSection currentSection = FileSection.Unknown;

            int    firstTime   = -1;
            int    lastTime    = -1;
            string lastTimeStr = "";
            int    breakTime   = 0;

            string[] split;

            char[] sep = new[] { ',' };

            try
            {
                if (!r.EndOfStream)
                {
                    string headerInfo = r.ReadLine();
                    if (headerInfo.Contains("osu file format"))
                    {
                        b.BeatmapVersion          = Int32.Parse(headerInfo.Remove(0, headerInfo.LastIndexOf('v') + 1));
                        AudioEngine.CustomSamples = b.BeatmapVersion < 4;
                        b.VersionOffset           = b.BeatmapVersion < 5 && GameBase.Mode == Modes.Edit ? 24 : 0;
                    }
                }

                while (!r.EndOfStream)
                {
                    string line = r.ReadLine().Trim();

                    if (line.Length == 0)
                    {
                        continue;
                    }

                    string left  = "";
                    string right = "";

                    if (currentSection != FileSection.HitObjects)
                    {
                        int index = line.IndexOf(':');

                        if (index >= 0)
                        {
                            left  = line.Remove(index).Trim();
                            right = line.Remove(0, index + 1).Trim();
                        }
                        else if (line[0] == '[')
                        {
                            try
                            {
                                currentSection =
                                    (FileSection)Enum.Parse(typeof(FileSection), line.Trim(new[] { '[', ']' }));
                            }
                            catch
                            {
                            }
                            continue;
                        }
                    }

                    switch (currentSection)
                    {
                    case FileSection.TimingPoints:
                        split = line.Split(sep);
                        if (split.Length > 2)
                        {
                            b.TimingPoints.Add(
                                new TimingPoint(Double.Parse(split[0].Trim(), GameBase.nfi) + b.VersionOffset,
                                                Double.Parse(split[1].Trim(), GameBase.nfi),
                                                (TimeSignatures)Int32.Parse(split[2]),
                                                (SampleSet)Int32.Parse(split[3]),
                                                split.Length > 4 && split[4][0] == '1',
                                                split.Length > 5 ? Int32.Parse(split[5]) : b.SampleVolume));
                        }
                        else if (split.Length == 2)
                        {
                            b.TimingPoints.Add(
                                new TimingPoint(Double.Parse(split[0].Trim(), GameBase.nfi) + b.VersionOffset,
                                                Double.Parse(split[1].Trim(), GameBase.nfi),
                                                TimeSignatures.SimpleQuadruple,
                                                b.DefaultSampleSet,
                                                AudioEngine.CustomSamples,
                                                100));
                        }
                        break;

                    case FileSection.General:
                        switch (left)
                        {
                        case "SampleSet":
                            b.DefaultSampleSet           = (SampleSet)Enum.Parse(typeof(SampleSet), right);
                            AudioEngine.CurrentSampleSet = b.DefaultSampleSet;
                            break;

                        case "CustomSamples":
                            AudioEngine.CustomSamples = right[0] == '1';
                            break;

                        case "Countdown":
                            b.Countdown = (Countdown)Int32.Parse(right);
                            break;

                        case "AudioFilename":
                            if (right.Length > 0)
                            {
                                b.AudioFilename = string.Format("{0}\\{1}", b.ContainingFolder, right);
                            }
                            break;

                        case "AudioHash":
                            b.AudioExpectedMd5 = right;
                            break;

                        case "AudioLeadIn":
                            b.AudioLeadIn = Int32.Parse(right);
                            break;

                        case "PreviewTime":
                            b.PreviewTime = Int32.Parse(right);
                            break;

                        case "SampleVolume":
                            b.SampleVolume = Int32.Parse(right);
                            break;

                        case "StackLeniency":
                            b.StackLeniency = Math.Max(0, Math.Min(1, float.Parse(right, GameBase.nfi)));
                            break;

                        case "Mode":
                            b.Mode = Int32.Parse(right);
                            break;
                        }
                        break;

                    case FileSection.Metadata:
                        switch (left)
                        {
                        case "Artist":
                            b.Artist = right;
                            break;

                        case "Title":
                            b.Title = right;
                            break;

                        case "Creator":
                            b.Creator = right;
                            break;

                        case "Version":
                            b.Version = right;
                            break;
                        }
                        break;

                    case FileSection.Difficulty:
                        switch (left)
                        {
                        case "HPDrainRate":
                            b.DifficultyHpDrainRate = byte.Parse(right);
                            break;

                        case "CircleSize":
                            b.DifficultyCircleSize = byte.Parse(right);
                            break;

                        case "OverallDifficulty":
                            b.DifficultyOverall = byte.Parse(right);
                            break;

                        case "SliderMultiplier":
                            b.DifficultySliderMultiplier =
                                Math.Max(0.4, Math.Min(2.6, Double.Parse(right, GameBase.nfi)));
                            break;

                        case "SliderTickRate":
                            b.DifficultySliderTickRate =
                                Math.Max(0.5, Math.Min(8, Double.Parse(right, GameBase.nfi)));
                            break;
                        }
                        break;

                    case FileSection.Events:
                        if (line[0] == '2')
                        {
                            int rs;
                            split = line.Split(',');
                            if (Int32.TryParse(split[0], out rs) && rs == (int)EventTypes.Break)
                            {
                                breakTime += (Int32.Parse(split[2]) - Int32.Parse(split[1]));
                            }
                        }
                        break;

                    case FileSection.HitObjects:
                        split = line.Split(sep, 5);

                        if (firstTime == -1)
                        {
                            firstTime = Int32.Parse(split[2]);
                        }

                        /*
                         *     internal enum HitObjectType
                         *      {
                         *          Normal = 1,
                         *          Slider = 2,
                         *          NewCombo = 4,
                         *          NormalNewCombo = 5,
                         *          SliderNewCombo = 6,
                         *          Spinner = 8
                         *      } ;
                         *
                         * */

                        if (split[3][0] == '8' || split[3].Length > 1)
                        {
                            b.countSpinner++;
                        }
                        else if (split[3][0] == '1' || split[3][0] == '5')
                        {
                            b.countNormal++;
                        }
                        else if (split[3][0] == '2' || split[3][0] == '6')
                        {
                            b.countSlider++;
                        }

                        lastTimeStr = split[2];
                        b.ObjectCount++;
                        break;

                    case FileSection.Colours:
                        b.CustomColours = true;
                        break;
                    }
                }
                r.Close();
            }
            catch (Exception)
            {
                if (DialogResult.Yes ==
                    MessageBox.Show(
                        "Error loading beatmap file " + Path.GetFileName(b.Filename) + "\nDelete this file?", "error'd",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1))
                {
                    r.Close();
                    File.Delete(b.Filename);
                }
            }

            b.PopulateTitleStatics();

            if (lastTimeStr.Length > 0)
            {
                lastTime = Int32.Parse(lastTimeStr);
            }

            b.DrainLength = (lastTime - firstTime - breakTime) / 1000;
            b.TotalLength = lastTime;
        }
 public BeatmapDifficultyCalculator(Beatmap Beatmap)
 {
     // Avoid any possible data sharing and thus race conditions by working on a clone of the current beatmap.
     this.Beatmap = Beatmap.Clone();
 }
예제 #4
0
        internal static void ProcessFolder(string folder, BeatmapTreeLevel parent)
        {
            BeatmapTreeLevel tl = new BeatmapTreeLevel(folder, parent);

            parent.Children.Add(tl);

            string[] oggs = Directory.GetFiles(folder, "*.ogg");
            string[] mp3s = Directory.GetFiles(folder, "*.mp3");

            string audioFilename = "";

            if (oggs.Length > 0)
            {
                audioFilename = oggs[0];
            }
            else if (mp3s.Length > 0)
            {
                audioFilename = mp3s[0];
            }

            int osuCount = 0;

            foreach (string file in Directory.GetFiles(folder, "*.osu"))
            {
                if (parent == BeatmapTreeManager.Root)
                {
                    try
                    {
                        string baseDir  = Directory.GetCurrentDirectory();
                        string songsDir = baseDir + "\\songs\\";

                        string newFolder = songsDir + Path.GetFileNameWithoutExtension(file);
                        if (!Directory.Exists(newFolder))
                        {
                            Directory.CreateDirectory(newFolder);
                        }

                        foreach (string f in Directory.GetFiles(folder))
                        {
                            File.Move(f, newFolder + "\\" + Path.GetFileName(f));
                        }
                    }
                    catch
                    {
                    }
                    MessageBox.Show(
                        "You have extracted a beatmap the wrong way!  Make sure you place the osz/zip file you downloaded inside the Songs directory, WITHOUT extracting it yourself.\nI have fixed it for you this time, but this will CAUSE PROBLEMS if more than one beatmap are present!");
                    parent.Children.Remove(tl);
                    ProcessFolder(folder, parent);
                    return;
                }

                osuCount++;

                if (ReadingDatabase)
                {
                    Beatmap found = BeatmapAvailable.Find(bm => bm.Filename == file);
                    if (found != null)
                    {
                        if (found.DateModified == new FileInfo(file).LastWriteTimeUtc)
                        {
                            //Hasn't changed - skip over
                            found.DatabaseNotFound = false;
                            tl.Beatmaps.Add(found);
                            continue;
                        }
                        else
                        {
                            //Has changed - do a full reload.
                            BeatmapAvailable.Remove(found);
                        }
                    }
                }

                Beatmap b = new Beatmap();
                b.ContainingFolder = folder;
                b.AudioFilename    = audioFilename;
                b.AudioPresent     = true;
                b.BeatmapPresent   = true;
                b.Filename         = file;

                ProcessHeaders(b);

                if (b.AudioPresent || b.BeatmapPresent)
                {
                    BeatmapAvailable.Add(b);
                    tl.Beatmaps.Add(b);
                }
            }

            if (osuCount == 0 && audioFilename.Length > 0)
            {
                Beatmap b = new Beatmap();
                b.AudioFilename        = audioFilename;
                b.Filename             = string.Format("{0}\\{1}.osu", folder, Path.GetFileNameWithoutExtension(audioFilename));
                b.DisplayTitle         = Path.GetFileName(audioFilename);
                b.DisplayTitleNoArtist = Path.GetFileName(audioFilename);
                b.SortTitle            = Path.GetFileName(audioFilename);
                b.GroupTitle           = Path.GetFileName(audioFilename);
                b.AudioPresent         = true;
                b.ContainingFolder     = folder;
                BeatmapAvailable.Add(b);
                tl.Beatmaps.Add(b);

                TotalAudioOnly++;
            }

            if (osuCount > 0)
            {
                TotalUniqueFolders++;
            }

            foreach (string f in Directory.GetDirectories(folder))
            {
                ProcessFolder(f, tl);
            }
        }
예제 #5
0
        public static void DeleteBeatmap(Beatmap b, bool allDifficulties)
        {
            SongWatcher.EnableRaisingEvents = false;

            AudioEngine.FreeMusic();

            if (allDifficulties)
            {
                if (!b.BeatmapPresent || b.ContainingFolderAbsolute.EndsWith(BeatmapManager.SongsDirectory) || b.InOszContainer)
                {
                    try
                    {
                        if (b.InOszContainer)
                        {
                            AudioEngine.Stop();
                            AudioEngine.FreeMusic();
                            Osz2Factory.CloseMapPackage(b.Package);
                            RecycleBin.SendSilent(b.ContainingFolderAbsolute);
                            FolderFileCount--;
                        }
                        else if (!b.ContainingFolderAbsolute.EndsWith(BeatmapManager.SongsDirectory))
                        {
                            RecycleBin.SendSilent(b.ContainingFolderAbsolute);
                            FolderFileCount--;
                        }
                        else
                        {
                            b.DeleteFile(b.AudioFilename);
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    try
                    {
                        RecycleBin.SendSilent(b.ContainingFolderAbsolute);
                        FolderFileCount--;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                b.DeleteFile(Current.Filename);
            }

            List <Beatmap> matches = allDifficulties ? Beatmaps.FindAll(bm => bm.ContainingFolder == b.ContainingFolder) : new List <Beatmap>()
            {
                Current
            };

            foreach (Beatmap bm in matches)
            {
                Remove(bm);
                BeatmapManager.Root.Beatmaps.Remove(bm);
            }

            SongWatcher.EnableRaisingEvents = true;

            if (OnDeletedBeatmaps != null && matches.Count > 0)
            {
                OnDeletedBeatmaps(null, matches);
            }
        }