public Replay LoadReplay()
        {
            Replay replay     = null;
            var    messageBox = new ReplayOptionBox(Options);

            if (messageBox.ShowDialog() == DialogResult.OK)
            {
                switch (messageBox.Result)
                {
                case ReplayFind.RECENT:
                    var files     = new DirectoryInfo(Path.Combine(Options.Settings["osudir"], "Data", "r")).GetFiles();
                    var replayDir = new DirectoryInfo(Path.Combine(Options.Settings["osudir"], "Replays"));
                    if (replayDir.Exists)
                    {
                        files.Concat(replayDir.GetFiles());
                    }
                    var replays = files.Where(f => f.Name.EndsWith("osr"))
                                  .OrderByDescending(f => f.LastWriteTime)
                                  .Take(10).Select(file => new Replay(file.FullName))
                                  .OrderByDescending(re => re.PlayTime)
                                  .Select(re => new ReplayListItem()
                    {
                        replay = re, beatmap = LoadBeatmap(re, false)
                    });
                    var replayListForm = new ListMessageBox();
                    replayListForm.SetContent(replays.ToList());
                    if (replayListForm.ShowDialog() == DialogResult.OK && replayListForm.GetResult() != null)
                    {
                        replay = replayListForm.GetResult().replay;
                    }
                    break;

                case ReplayFind.BEATMAP:
                    var beatmapForm = new BeatmapSearchBox();
                    beatmapForm.SetContent(Options.Database);
                    if (beatmapForm.ShowDialog() == DialogResult.OK)
                    {
                        Beatmap b = beatmapForm.Result.Load(Options.SongsFolder);
                        var     beatmapListForm = new ListMessageBox();
                        beatmapListForm.SetContent(Options.GetReplaysFromBeatmap(b.BeatmapHash).Select(r => new ReplayListItem {
                            replay = r, beatmap = b
                        }).ToList());
                        if (beatmapListForm.ShowDialog() == DialogResult.OK && beatmapListForm.GetResult() != null)
                        {
                            replay = beatmapListForm.GetResult().replay;
                        }
                    }
                    break;

                case ReplayFind.MANUAL:
                    using (OpenFileDialog fd = new OpenFileDialog())
                    {
                        fd.Title  = "Choose replay file";
                        fd.Filter = "osu! replay files (*.osr)|*.osr";
                        DialogResult result = fd.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            replay = new Replay(fd.FileName);
                        }
                    }
                    break;
                }
            }
            if (replay == null)
            {
                Program.ShowErrorDialog("Couldn't find replay");
            }
            return(replay);
        }
Esempio n. 2
0
        public async Task <Replay> LoadReplay()
        {
            Replay replay     = null;
            var    messageBox = new ReplayOptionBox
            {
                DataContext = new ReplayOptionBoxViewModel(Options)
            };

            bool skipLoadDialog = Options.WatchDogMode;

            if (skipLoadDialog || await messageBox.ShowDialog <bool>(App.Window))
            {
                var userResult = skipLoadDialog ? ReplayFind.WATCHDOG : messageBox.Result;
                switch (userResult)
                {
                case ReplayFind.RECENT:
                case ReplayFind.WATCHDOG:
                    var files             = LocalReplaysList;
                    var replaysEnumerable = files.Where(f => f.Name.EndsWith("osr"))
                                            .OrderByDescending(f => f.LastWriteTime)
                                            .Select(file => new Replay(file.FullName))
                                            .Where(re => re.GameMode == GameModes.osu)
                                            .Take(10);
                    if (userResult != ReplayFind.WATCHDOG)
                    {
                        replaysEnumerable = replaysEnumerable.OrderByDescending(re => re.PlayTime);
                    }

                    var replays = await Task.WhenAll(replaysEnumerable.Select(async re => new ReplayListItem()
                    {
                        Replay = re, Beatmap = await LoadBeatmap(re, false)
                    }));

                    var replayListForm = new ListMessageBox
                    {
                        DataContext = new ListMessageBoxViewModel
                        {
                            Items = replays.ToList(),
                        },
                    };
                    if (userResult == ReplayFind.WATCHDOG)
                    {
                        replay  = replays[0].Replay;
                        Beatmap = replays[0].Beatmap;
                    }
                    else if (await replayListForm.ShowDialog <bool>(App.Window))
                    {
                        replay  = replayListForm.Result.Replay;
                        Beatmap = replayListForm.Result.Beatmap;
                    }
                    break;

                case ReplayFind.BEATMAP:
                    var beatmapForm = new BeatmapSearchBox
                    {
                        DataContext = new BeatmapSearchBoxViewModel(Options)
                    };
                    if (await beatmapForm.ShowDialog <bool>(App.Window))
                    {
                        Beatmap b = beatmapForm.Result.Load(Options.SongsFolder);
                        var     beatmapListForm = new ListMessageBox
                        {
                            DataContext = new ListMessageBoxViewModel
                            {
                                Items = Options.GetReplaysFromBeatmap(b.BeatmapHash).Select(r => new ReplayListItem {
                                    Replay = r, Beatmap = b
                                }).ToList(),
                            }
                        };
                        if (await beatmapListForm.ShowDialog <bool>(App.Window))
                        {
                            replay  = beatmapListForm.Result.Replay;
                            Beatmap = beatmapListForm.Result.Beatmap;
                        }
                    }
                    break;

                case ReplayFind.MANUAL:
                    OpenFileDialog fd = new OpenFileDialog()
                    {
                        Title   = "Choose replay file",
                        Filters =
                        {
                            new FileDialogFilter
                            {
                                Name       = "osu! replay files (*.osr)",
                                Extensions ={ "osr"                  },
                            }
                        }
                    };
                    string[] result = await fd.ShowAsync(App.Window);

                    if (result != null && result.Length > 0)
                    {
                        replay = new Replay(result[0]);
                    }
                    break;
                }
            }
            return(replay);
        }