Exemple #1
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;
                    }
                }
            }
        }