示例#1
0
        /// <summary>
        ///     Loads a Replay file from a stream
        /// </summary>
        /// <param name="file">file to load</param>
        /// <returns></returns>
        public static ReplayFile FromStream(Stream file)
        {
            file.Position = 0;
            var binreader = new BinaryReader(file);
            var reader    = new ReplayReader(binreader);
            var replay    = new ReplayFile
            {
                GameMode     = (GameMode)reader.ReadNextByte(),
                OsuVersion   = reader.ReadNextInt(),
                BeatmapHash  = reader.ReadNextOsuString(),
                PlayerName   = reader.ReadNextOsuString(),
                ReplayHash   = reader.ReadNextOsuString(),
                Amount300    = reader.ReadNextShort(),
                Amount100    = reader.ReadNextShort(),
                Amount50     = reader.ReadNextShort(),
                AmountGeki   = reader.ReadNextShort(),
                AmountKatu   = reader.ReadNextShort(),
                AmountMiss   = reader.ReadNextShort(),
                TotalScore   = reader.ReadNextInt(),
                MaxCombo     = reader.ReadNextShort(),
                Perfect      = reader.ReadNextBool(),
                Mods         = (Mode)reader.ReadNextInt(),
                LifebarGraph = reader.ReadNextOsuString(),
                Timestamp    = reader.ReadNextLong(),
                ReplayLength = reader.ReadNextInt()
            };

            replay.CompressedReplayData = reader.ReadNextBytes(replay.ReplayLength);
            var decompressedData = SevenZipLZMAHelper.Decompress(replay.CompressedReplayData);

            replay.DecompressedReplayData = Encoding.UTF8.GetString(decompressedData);

            var moveData = new List <MoveData>();

            foreach (var replayData in replay.DecompressedReplayData.Split(','))
            {
                var contents = replayData.Split('|');
                if (contents.Length < 4)
                {
                    continue;
                }

                moveData.Add(new MoveData(long.Parse(contents[0]), float.Parse(contents[1]), float.Parse(contents[2]), int.Parse(contents[3])));
            }
            replay.ReplayData = moveData.AsReadOnly();

            replay.OnlineScoreId = reader.ReadNextLong();

            return(replay);
        }
示例#2
0
        /// <summary>
        ///     Create a replay from api entities.
        ///     You shouldn't mix up replays, users, beatmaps and scores, but.. you could.
        /// </summary>
        /// <param name="entityReplay">Replay entity</param>
        /// <param name="score">Score entity</param>
        /// <param name="beatmap">Beatmap entity</param>
        /// <returns></returns>
        public static ReplayFile CreateReplayFile(Replay entityReplay, Score score, Beatmap beatmap)
        {
            var playbytes = Convert.FromBase64String(entityReplay.Content);
            var replay    = new ReplayFile
            {
                Amount300            = (short)score.Count300,
                Amount100            = (short)score.Count100,
                Amount50             = (short)score.Count50,
                AmountGeki           = (short)score.Geki,
                AmountKatu           = (short)score.Katu,
                AmountMiss           = (short)score.Miss,
                BeatmapHash          = beatmap.FileMd5,
                GameMode             = beatmap.GameMode,
                TotalScore           = (int)score.TotalScore,
                ReplayLength         = playbytes.Length,
                CompressedReplayData = playbytes,
                ReplayHash           = "",
                MaxCombo             = (short)score.MaxCombo,
                LifebarGraph         = "",
                Mods          = score.Mods,
                Perfect       = score.Perfect,
                Timestamp     = score.Date.Value.Ticks,
                OsuVersion    = 0,
                PlayerName    = score.Username,
                OnlineScoreId = score.ScoreId // whatever it's both a long
            };

            var decompressedData = SevenZipLZMAHelper.Decompress(replay.CompressedReplayData);

            replay.DecompressedReplayData = Encoding.UTF8.GetString(decompressedData);

            var moveData = new List <MoveData>();

            foreach (var replayData in replay.DecompressedReplayData.Split(','))
            {
                var contents = replayData.Split('|');
                if (contents.Length < 4)
                {
                    continue;
                }

                moveData.Add(new MoveData(long.Parse(contents[0]), float.Parse(contents[1]), float.Parse(contents[2]), int.Parse(contents[3])));
            }
            replay.ReplayData = moveData.AsReadOnly();

            return(replay);
        }