Ejemplo n.º 1
0
        internal Replay(string replayPath)
        {
            Path         = replayPath;
            Size         = (int)new FileInfo(replayPath).Length;
            DateModified = File.GetLastWriteTime(replayPath);
            using (var stream = File.OpenRead(replayPath))
            {
                var rec = new BinaryReader(stream);
                try
                {
                    var frames = rec.ReadInt32();
                    var magic  = rec.ReadInt32();
                    if (magic != 0x83)
                    {
                        throw new BadFileException($"Unexpected magic ({magic}) in replay file: {replayPath}");
                    }
                    IsMulti = rec.ReadInt32() == 1;
                    var isFlagtag = rec.ReadInt32() == 1;
                    LevId         = rec.ReadInt32();
                    LevelFilename = rec.ReadNullTerminatedString(12);
                    rec.ReadInt32();
                    IsInternal = Level.IsInternalLevel(LevelFilename);
                    Player1    = new Player(rec, frames);
                    if (IsMulti)
                    {
                        frames = rec.ReadInt32();
                        rec.BaseStream.Seek(32, SeekOrigin.Current);
                        Player2 = new Player(rec, frames);
                    }
                }
                catch (EndOfStreamException)
                {
                    throw new BadFileException($"Corrupted replay file: {replayPath}");
                }
            }

            if (IsMulti)
            {
                if (!Player1.Finished)
                {
                    if (Player1.FakeFinish && Player2.IsLastEventApple)
                    {
                        Finished = true;
                        Time     = Player1.Time;
                    }
                    else if (Player2.FakeFinish && Player1.IsLastEventApple)
                    {
                        Finished = true;
                        Time     = Player2.Time;
                    }
                    else if (Player2.Finished)
                    {
                        Finished = true;
                        Time     = Player2.Time;
                    }
                    else //In this case, neither of players finished
                    {
                        Time = Math.Max(Player1.Time, Player2.Time);
                    }
                }
                else
                {
                    Time     = Player1.Time;
                    Finished = true;
                }
            }
            else
            {
                Finished = Player1.Finished;
                Time     = Player1.Time;
            }

            if (IsInternal)
            {
                LevelExists       = true;
                WrongLevelVersion = false;
                AcrossLevel       = false;
                _internalIndex    = int.Parse(LevelFilename.Substring(6, 2));
            }
            else
            {
                _internalIndex = -1;
                foreach (var levelFile in Global.GetLevelFiles())
                {
                    if (System.IO.Path.GetFileName(levelFile).CompareWith(LevelFilename))
                    {
                        LevelExists = true;
                        LevelPath   = levelFile;
                        var fileStream  = File.OpenRead(levelFile);
                        var levelStream = new BinaryReader(fileStream);
                        fileStream.Seek(3, SeekOrigin.Begin);
                        //Check also the version of the level
                        if (fileStream.Length > 0)
                        {
                            if (levelStream.ReadByte() == 49)
                            //If Level(3) = 49, it is Elma lev, otherwise (when 48) Across lev
                            {
                                AcrossLevel = false;
                                fileStream.Seek(7, SeekOrigin.Begin);
                            }
                            else
                            {
                                AcrossLevel = true;
                                fileStream.Seek(5, SeekOrigin.Begin);
                            }

                            if (levelStream.ReadInt32() != LevId)
                            {
                                WrongLevelVersion = true;
                                break;
                            }
                        }
                        else
                        {
                            LevelExists = false;
                        }

                        levelStream.Close();
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal Replay(string replayPath)
        {
            var rawData = File.ReadAllBytes(replayPath);

            Path         = replayPath;
            Size         = rawData.Length;
            DateModified = File.GetLastWriteTime(replayPath);
            var invalid = BitConverter.ToInt32(rawData, 4) != 0x83;

            if (invalid)
            {
                return;
            }
            LevId         = BitConverter.ToInt32(rawData, 16);
            LevelFilename = Utils.ReadNullTerminatedString(rawData, 20, 12);
            IsInternal    = Level.IsInternalLevel(LevelFilename);
            IsMulti       = rawData[8] == 1;
            Player1       = new Player(rawData, false);
            if (IsMulti)
            {
                Player2 = new Player(rawData, true);
                if (!Player1.Finished)
                {
                    if (Player1.FakeFinish && Player2.IsLastEventApple)
                    {
                        Finished = true;
                        Time     = Player1.Time;
                    }
                    else if (Player2.FakeFinish && Player1.IsLastEventApple)
                    {
                        Finished = true;
                        Time     = Player2.Time;
                    }
                    else if (Player2.Finished)
                    {
                        Finished = true;
                        Time     = Player2.Time;
                    }
                    else //In this case, neither of players finished
                    {
                        Time = Math.Max(Player1.Time, Player2.Time);
                    }
                }
                else
                {
                    Time     = Player1.Time;
                    Finished = true;
                }
            }
            else
            {
                Finished = Player1.Finished;
                Time     = Player1.Time;
            }

            if (IsInternal)
            {
                LevelExists       = true;
                WrongLevelVersion = false;
                AcrossLevel       = false;
                _internalIndex    = int.Parse(LevelFilename.Substring(6, 2));
            }
            else
            {
                _internalIndex = -1;
                foreach (var levelFile in Global.GetLevelFiles())
                {
                    if (System.IO.Path.GetFileName(levelFile).CompareWith(LevelFilename))
                    {
                        LevelExists = true;
                        LevelPath   = levelFile;
                        var fileStream  = new FileStream(levelFile, FileMode.Open, FileAccess.Read);
                        var levelStream = new BinaryReader(fileStream);
                        fileStream.Seek(3, SeekOrigin.Begin);
                        //Check also the version of the level
                        if (fileStream.Length > 0)
                        {
                            if (levelStream.ReadByte() == 49)
                            //If Level(3) = 49, it is Elma lev, otherwise (when 48) Across lev
                            {
                                AcrossLevel = false;
                                fileStream.Seek(7, SeekOrigin.Begin);
                            }
                            else
                            {
                                AcrossLevel = true;
                                fileStream.Seek(5, SeekOrigin.Begin);
                            }

                            if (levelStream.ReadInt32() != LevId)
                            {
                                WrongLevelVersion = true;
                                break;
                            }
                        }
                        else
                        {
                            LevelExists = false;
                        }

                        levelStream.Close();
                        break;
                    }
                }
            }
        }