public static TickMark Deserialize(BinaryReader bs)
 {
     var tm = new TickMark();
     tm.Type = bs.ReadString2();
     tm.Frame = bs.ReadInt32();
     return tm;
 }
        }                                        // Frame?

        public static TickMark Deserialize(BinaryReader bs)
        {
            var tm = new TickMark();

            tm.Type  = bs.ReadString2();
            tm.Frame = bs.ReadInt32();
            return(tm);
        }
Example #3
0
        public static Replay Deserialize(BinaryReader br, out string log)
        {
            var logSb = new StringBuilder();

            var replay = new Replay();

            try
            {
                replay.Unknown1 = br.ReadInt32();
                replay.Unknown2 = br.ReadInt32();
                replay.Unknown3 = br.ReadInt32();
                replay.Unknown4 = br.ReadInt32();

                // This looks almost like an ArrayProperty, but without type and the unknown ints
                replay.Unknown5 = br.ReadString2();

                var s = br.BaseStream.Position;
                replay.Properties = new List <Property>();
                Property prop;
                do
                {
                    prop = Property.Deserialize(br);
                    replay.Properties.Add(prop);
                }while (prop.Name != "None");

                replay.LengthOfRemainingData = br.ReadInt32();
                replay.Unknown7    = br.ReadInt32();
                replay.LevelLength = br.ReadInt32();

                // looks like sfx data, not level data. shrug
                replay.Levels = new List <Level>();
                for (int i = 0; i < replay.LevelLength; i++)
                {
                    replay.Levels.Add(Level.Deserialize(br));
                }

                replay.KeyFrameLength = br.ReadInt32();
                replay.KeyFrames      = new List <KeyFrame>();
                for (int i = 0; i < replay.KeyFrameLength; i++)
                {
                    replay.KeyFrames.Add(KeyFrame.Deserialize(br));
                }

                replay.NetworkStreamLength = br.ReadInt32();
                replay.NetworkStream       = new List <byte>();
                for (int i = 0; i < replay.NetworkStreamLength; ++i)
                {
                    replay.NetworkStream.Add(br.ReadByte());
                }

                replay.DebugStringLength = br.ReadInt32();
                replay.DebugStrings      = new List <DebugString>();
                for (int i = 0; i < replay.DebugStringLength; i++)
                {
                    replay.DebugStrings.Add(DebugString.Deserialize(br));
                }

                replay.TickMarkLength = br.ReadInt32();
                replay.TickMarks      = new List <TickMark>();
                for (int i = 0; i < replay.TickMarkLength; i++)
                {
                    replay.TickMarks.Add(TickMark.Deserialize(br));
                }

                replay.PackagesLength = br.ReadInt32();
                replay.Packages       = new List <string>();
                for (int i = 0; i < replay.PackagesLength; i++)
                {
                    replay.Packages.Add(br.ReadString2());
                }

                replay.ObjectLength = br.ReadInt32();
                replay.Objects      = new string[replay.ObjectLength];
                for (int i = 0; i < replay.ObjectLength; i++)
                {
                    replay.Objects[i] = br.ReadString2();
                }

                replay.NamesLength = br.ReadInt32();
                replay.Names       = new string[replay.NamesLength];
                for (int i = 0; i < replay.NamesLength; i++)
                {
                    replay.Names[i] = br.ReadString2();
                }

                replay.ClassIndexLength = br.ReadInt32();
                replay.ClassIndexes     = new List <ClassIndex>();
                for (int i = 0; i < replay.ClassIndexLength; i++)
                {
                    replay.ClassIndexes.Add(ClassIndex.Deserialize(br));
                }

                replay.ClassNetCacheLength = br.ReadInt32();
                replay.ClassNetCaches      = new ClassNetCache[replay.ClassNetCacheLength];
                for (int i = 0; i < replay.ClassNetCacheLength; i++)
                {
                    replay.ClassNetCaches[i] = ClassNetCache.Deserialize(br);

                    int j = 0;
                    for (j = i - 1; j >= 0; --j)
                    {
                        if (replay.ClassNetCaches[i].ParentId == replay.ClassNetCaches[j].Id)
                        {
                            replay.ClassNetCaches[i].Parent = replay.ClassNetCaches[j];
                            replay.ClassNetCaches[j].Children.Add(replay.ClassNetCaches[i]);
                            break;
                        }
                    }
                    if (j < 0)
                    {
                        replay.ClassNetCaches[i].Root = true;
                    }
                }

                // 2016/02/10 patch replays have TAGame.PRI_TA classes with no parent.
                // Deserialization may have failed somehow, but for now manually fix it up.

                var priClassNetCache   = replay.ClassNetCaches.Where(cnc => replay.Objects[cnc.ObjectIndex] == "Engine.PlayerReplicationInfo").Single();
                var prixClassNetCache  = replay.ClassNetCaches.Where(cnc => replay.Objects[cnc.ObjectIndex] == "ProjectX.PRI_X").Single();
                var pritaClassNetCache = replay.ClassNetCaches.Where(cnc => replay.Objects[cnc.ObjectIndex] == "TAGame.PRI_TA").Single();
                if (prixClassNetCache.Parent == null)
                {
                    Console.WriteLine("Fudging the parent of ProjectX.PRI_X");
                    prixClassNetCache.Root   = false;
                    prixClassNetCache.Parent = priClassNetCache;
                    priClassNetCache.Children.Add(prixClassNetCache);
                }
                if (pritaClassNetCache.Parent == null)
                {
                    Console.WriteLine("Fudging the parent of TAGame.PRI_TA");
                    pritaClassNetCache.Root   = false;
                    pritaClassNetCache.Parent = prixClassNetCache;
                    prixClassNetCache.Children.Add(pritaClassNetCache);
                }

                var objectIndexToName = Enumerable.Range(0, replay.Objects.Length).ToDictionary(i => i, i => replay.Objects[i]);

                replay.Frames = ExtractFrames(replay.NetworkStream, replay.KeyFrames.Select(x => x.FilePosition), objectIndexToName, replay.ClassNetCaches, logSb);

#if DEBUG // Maybe change to write to a debug log
                foreach (var f in replay.Frames.Where(x => !x.Complete || x.ActorStates.Any(a => a.ForcedComplete)))
                {
                    logSb.AppendLine(f.ToDebugString(replay.Objects));
                }
#endif

                if (br.BaseStream.Position != br.BaseStream.Length)
                {
                    throw new Exception("Extra data somewhere!");
                }

                log = logSb.ToString();

                return(replay);
            }
            catch (Exception)
            {
#if DEBUG
                log = logSb.ToString();
                return(replay);
#else
                throw;
#endif
            }
        }
        public static Replay Deserialize(BinaryReader br)
        {
            var replay = DeserializeHeader(br);

            try
            {
                replay.Part2Length = br.ReadInt32();
                replay.Part2Crc    = br.ReadUInt32();

                replay.LevelLength = br.ReadInt32();
                // looks like sfx data, not level data. shrug
                replay.Levels = new List <Level>();
                for (int i = 0; i < replay.LevelLength; i++)
                {
                    replay.Levels.Add(Level.Deserialize(br));
                }

                replay.KeyFrameLength = br.ReadInt32();
                replay.KeyFrames      = new List <KeyFrame>();
                for (int i = 0; i < replay.KeyFrameLength; i++)
                {
                    replay.KeyFrames.Add(KeyFrame.Deserialize(br));
                }

                replay.NetworkStreamLength = br.ReadInt32();
                replay.NetworkStream       = new List <byte>();
                for (int i = 0; i < replay.NetworkStreamLength; ++i)
                {
                    replay.NetworkStream.Add(br.ReadByte());
                }

                replay.DebugStringLength = br.ReadInt32();
                replay.DebugStrings      = new List <DebugString>();
                for (int i = 0; i < replay.DebugStringLength; i++)
                {
                    replay.DebugStrings.Add(DebugString.Deserialize(br));
                }

                replay.TickMarkLength = br.ReadInt32();
                replay.TickMarks      = new List <TickMark>();
                for (int i = 0; i < replay.TickMarkLength; i++)
                {
                    replay.TickMarks.Add(TickMark.Deserialize(br));
                }

                replay.PackagesLength = br.ReadInt32();
                replay.Packages       = new List <string>();
                for (int i = 0; i < replay.PackagesLength; i++)
                {
                    replay.Packages.Add(br.ReadString2());
                }

                replay.ObjectLength = br.ReadInt32();
                replay.Objects      = new string[replay.ObjectLength];
                for (int i = 0; i < replay.ObjectLength; i++)
                {
                    replay.Objects[i] = br.ReadString2();
                }

                replay.NamesLength = br.ReadInt32();
                replay.Names       = new string[replay.NamesLength];
                for (int i = 0; i < replay.NamesLength; i++)
                {
                    replay.Names[i] = br.ReadString2();
                }

                replay.ClassIndexLength = br.ReadInt32();
                replay.ClassIndexes     = new List <ClassIndex>();
                for (int i = 0; i < replay.ClassIndexLength; i++)
                {
                    replay.ClassIndexes.Add(ClassIndex.Deserialize(br));
                }

                replay.ClassNetCacheLength = br.ReadInt32();
                replay.ClassNetCaches      = new ClassNetCache[replay.ClassNetCacheLength];
                for (int i = 0; i < replay.ClassNetCacheLength; i++)
                {
                    var classNetCache = ClassNetCache.Deserialize(br);
                    replay.ClassNetCaches[i] = classNetCache;

                    for (int j = i - 1; j >= 0; --j)
                    {
                        if (classNetCache.ParentId == replay.ClassNetCaches[j].Id)
                        {
                            classNetCache.Parent = replay.ClassNetCaches[j];
                            replay.ClassNetCaches[j].Children.Add(classNetCache);
                            break;
                        }
                    }

                    if (replay.ClassNetCaches[i].Parent == null)
                    {
                        replay.ClassNetCaches[i].Root = true;
                    }
                }

                replay.MergeDuplicateClasses();

                // 2016/02/10 patch replays have TAGame.PRI_TA classes with no parent.
                // Deserialization may have failed somehow, but for now manually fix it up.
                replay.FixClassParent("ProjectX.PRI_X", "Engine.PlayerReplicationInfo");
                replay.FixClassParent("TAGame.PRI_TA", "ProjectX.PRI_X");

                // A lot of replays have messed up class hierarchies, commonly giving
                // both Engine.TeamInfo, TAGame.CarComponent_TA, and others the same id.
                // Some ambiguities may have become more common since the 2016-06-20 patch,
                // but there have always been issues.
                //
                // For example, from E8B66F8A4561A2DAACC61FA9FBB710CD:
                //    Index 26(TAGame.CarComponent_TA) ParentId 21 Id 24
                //        Index 28(TAGame.CarComponent_Dodge_TA) ParentId 24 Id 25
                //        Index 188(TAGame.CarComponent_Jump_TA) ParentId 24 Id 24
                //            Index 190(TAGame.CarComponent_DoubleJump_TA) ParentId 24 Id 24
                //    Index 30(Engine.Info) ParentId 21 Id 21
                //        Index 31(Engine.ReplicationInfo) ParentId 21 Id 21
                //            Index 195(Engine.TeamInfo) ParentId 21 Id 24
                //                Index 214(TAGame.CarComponent_Boost_TA) ParentId 24 Id 31
                //                Index 237(TAGame.CarComponent_FlipCar_TA) ParentId 24 Id 26
                // Problems:
                //     TAGame.CarComponent_Jump_TA's parent id and id are both 24 (happens to work fine in this case)
                //     TAGame.CarComponent_DoubleJump_TA's parent id and id are both 24 (incorrectly picks CarComponent_Jump_TA as parent)
                //     Engine.TeamInfo's ID is 24, even though there are 3 other classes with that id
                //     TAGame.CarComponent_Boost_TA's parent is 24 (Incorrectly picks Engine.TeamInfo, since it's ambiguous)
                //     TAGame.CarComponent_FlipCar_TA's parent is 24 (Incorrectly picks Engine.TeamInfo, since it's ambiguous)
                //     Engine.ReplicationInfo and Engine.Info have the same parent id and id (no ill effects so far)
                //
                // Note: The heirarchy problems do not always cause parsing errors! But they can if you're unlucky.

                replay.FixClassParent("TAGame.CarComponent_Boost_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.CarComponent_FlipCar_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.CarComponent_Jump_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.CarComponent_Dodge_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.CarComponent_DoubleJump_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.GameEvent_TA", "Engine.Actor");
                replay.FixClassParent("TAGame.SpecialPickup_TA", "TAGame.CarComponent_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BallVelcro_TA", "TAGame.SpecialPickup_TA");
                replay.FixClassParent("TAGame.SpecialPickup_Targeted_TA", "TAGame.SpecialPickup_TA");
                replay.FixClassParent("TAGame.SpecialPickup_Spring_TA", "TAGame.SpecialPickup_Targeted_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BallLasso_TA", "TAGame.SpecialPickup_Spring_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BoostOverride_TA", "TAGame.SpecialPickup_Targeted_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BallCarSpring_TA", "TAGame.SpecialPickup_Spring_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BallFreeze_TA", "TAGame.SpecialPickup_Targeted_TA");
                replay.FixClassParent("TAGame.SpecialPickup_Swapper_TA", "TAGame.SpecialPickup_Targeted_TA");
                replay.FixClassParent("TAGame.SpecialPickup_GrapplingHook_TA", "TAGame.SpecialPickup_Targeted_TA");
                replay.FixClassParent("TAGame.SpecialPickup_BallGravity_TA", "TAGame.SpecialPickup_TA");
                replay.FixClassParent("TAGame.SpecialPickup_HitForce_TA", "TAGame.SpecialPickup_TA");
                replay.FixClassParent("TAGame.SpecialPickup_Tornado_TA", "TAGame.SpecialPickup_TA");

                // Havent had problems with these yet. They (among others) can be ambiguous,
                // but I havent found a replay yet where my parent choosing algorithm
                // (which picks the matching class that was most recently read) picks the wrong class.
                // Just a safeguard for now.
                replay.FixClassParent("Engine.TeamInfo", "Engine.ReplicationInfo");
                replay.FixClassParent("TAGame.Team_TA", "Engine.TeamInfo");

                replay.Frames = ExtractFrames(replay.MaxChannels(), replay.NetworkStream, replay.Objects, replay.ClassNetCaches, replay.EngineVersion, replay.LicenseeVersion, replay.NetVersion);

                if (br.BaseStream.Position != br.BaseStream.Length)
                {
                    throw new Exception("Extra data somewhere!");
                }

                return(replay);
            }
            catch (Exception e)
            {
#if DEBUG
                Console.Write(e.ToString());
                return(replay);
#else
                throw;
#endif
            }
        }