public static bool ExportReplay(Score score, bool userTriggered = true)
        {
            string filename;

            if (userTriggered)
            {
                string replaysFolder = Path.Combine(OsuMain.UserPath, @"Replays");
                Directory.CreateDirectory(replaysFolder);

                filename = string.Format(@"{0} - {1} ({2:yyyy-MM-dd}) {3}.osr", score.PlayerName, BeatmapManager.Current.DisplayTitle, score.Date.ToUniversalTime(), score.PlayMode);
                filename = Path.Combine(replaysFolder, GeneralHelper.WindowsFilenameStrip(filename));
            }
            else
            {
                filename = score.ReplayFilename;
            }

            bool success = true;

            if (score.ReplayCompressed == null)
            {
                if (score.Replay == null || score.Replay.Count == 0)
                {
                    success = false;
                }
                else
                {
                    score.ReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(score.ReplayString));
                }
            }

            if (success)
            {
                try
                {
                    using (Stream stream = File.Open(filename, FileMode.Create))
                        score.WriteToStream(new SerializationWriter(stream));

                    if (!userTriggered && score.Frames.Count > 0)
                    {
                        using (Stream stream = File.Open(score.GhostFilename, FileMode.Create))
                            using (SerializationWriter sw = new SerializationWriter(stream))
                            {
                                sw.Write(General.VERSION);
                                sw.Write(score.Frames);
                            }
                    }
                }
                catch (IOException)
                {
                    success = false;
                }
            }

            if (userTriggered)
            {
                if (success)
                {
                    NotificationManager.ShowMessage(string.Format(LocalisationManager.GetString(OsuString.ScoreManager_SavedReplayToFile), filename), Color.BlueViolet, 6000, delegate
                    {
                        GameBase.OpenFolderToFile(filename);
                    });
                }
                else
                {
                    NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_ReplayFailed));
                }
            }

            return(success);
        }
        internal static int InsertScore(Score score, bool checkOnly, bool saveReplay = true)
        {
            InitializeScoreManager();

            if (score == null)
            {
                return(-1);
            }

            int   higherCount       = 0;
            int   count             = 0;
            Score lowestHigherScore = null;
            Score lowestLowerScore  = null;

            List <Score> songList = FindScores(score.FileChecksum, Player.Mode);

            if (songList == null)
            {
                songList = new List <Score>();
                LocalScores.Add(score.FileChecksum, songList);
                HasUnsavedChanges = true;
            }

            foreach (Score s in songList)
            {
                count++;
                if (s.TotalScore >= score.TotalScore)
                {
                    higherCount++;
                    if (lowestHigherScore == null || s.TotalScore < lowestHigherScore.TotalScore)
                    {
                        lowestHigherScore = s;
                    }
                    if (higherCount >= SCORES_PER_SONG)
                    {
                        return(-1);
                    }
                }
                else if (lowestLowerScore == null || s.TotalScore < lowestLowerScore.TotalScore)
                {
                    lowestLowerScore = s;
                }

                if (s.LocalScoreChecksum == score.LocalScoreChecksum)
                {
                    return(count);
                }
            }

            if (!checkOnly)
            {
                //The above list is a copy (from FindAll) so we want to get the actual list to add to.
                List <Score> actualList = LocalScores[score.FileChecksum];
                HasUnsavedChanges = true;

                if (lowestLowerScore != null && count >= SCORES_PER_SONG)
                {
                    lowestLowerScore.PurgeReplay();
                    actualList.Remove(lowestLowerScore);
                }

                if (saveReplay)
                {
                    ExportReplay(score, false);
                }

                int index = actualList.BinarySearch(score);
                if (index < 0)
                {
                    index = ~index;
                }

                actualList.Insert(index, score);
            }

            return(higherCount + 1);
        }
        public static Score ReadReplayFromFile(string filename, bool handlePickup)
        {
            //Make sure the score manager is already initialized.
            InitializeScoreManager();

            Score inScore = null;

            bool success = false;

            try
            {
                using (Stream stream = File.Open(filename, FileMode.Open))
                {
                    SerializationReader sr = new SerializationReader(stream);
                    inScore = ScoreFactory.Create((PlayModes)sr.ReadByte(), null);
                    inScore.ReadHeaderFromStream(sr);
                    if (ModManager.CheckActive(inScore.EnabledMods, Mods.Target))
                    {
                        inScore = new ScoreTarget(inScore);
                    }
                    inScore.ReadFromStream(sr);
                    if (inScore.Date < DateTime.UtcNow + new TimeSpan(365 * 5, 0, 0, 0))
                    {
                        success = true;
                    }
                }
            }
            catch { }


            if (!success)
            {
                try
                {
                    using (Stream stream = File.Open(filename, FileMode.Open))
                        inScore = (Score)DynamicDeserializer.Deserialize(stream);
                }
                catch
                {
                    NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_ReplayCorrupt));
                    return(null);
                }
            }

            if (inScore == null)
            {
                NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_ReplayCorrupt));
                return(null);
            }

            if (inScore.Date.Year > 2050 || inScore.Date == DateTime.MinValue)
            {
                string[] split = filename.Split('-');
                if (split.Length > 1)
                {
                    long outfiletime = 0;
                    if (long.TryParse(split[1].Replace(@".osr", string.Empty), out outfiletime))
                    {
                        inScore.Date = DateTime.FromFileTimeUtc(outfiletime);
                    }
                }
            }

            if (inScore.Date.Year > 2050)
            {
                //this score is TOTALLY f****d.
                File.Delete(filename);
                NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_ReplayCorrupt));
                return(null);
            }

            if (BeatmapManager.GetBeatmapByChecksum(inScore.FileChecksum) == null)
            {
                //attempt pickup.
                if (handlePickup)
                {
                    OsuDirect.HandlePickup(inScore.FileChecksum, null,
                                           delegate
                    {
                        NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_DontHaveBeatmap));
                    });
                }
                return(null);
            }

            InsertScore(inScore, false);

            return(inScore);
        }