CalculateCrc() public static method

public static CalculateCrc ( byte data, int startPosition, int length, UInt32 crc ) : UInt32
data byte
startPosition int
length int
crc System.UInt32
return System.UInt32
        public static bool ValidateCrc(string filePath, bool onlyPartOne)
        {
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                if (fs.Length <= 8)
                {
                    return(false);
                }

                var intBuffer = new byte[4];
                if (fs.Read(intBuffer, 0, 4) != 4)
                {
                    return(false);
                }
                var part1Length = BitConverter.ToInt32(intBuffer, 0);
                if (fs.Read(intBuffer, 0, 4) != 4)
                {
                    return(false);
                }
                var part1Crc   = BitConverter.ToUInt32(intBuffer, 0);
                var part1Bytes = new byte[part1Length];
                if (fs.Read(part1Bytes, 0, part1Length) != part1Length)
                {
                    return(false);
                }

                if (part1Crc != Crc32.CalculateCrc(part1Bytes, 0, part1Length, CRC_SEED))
                {
                    return(false);
                }

                if (!onlyPartOne)
                {
                    if (fs.Read(intBuffer, 0, 4) != 4)
                    {
                        return(false);
                    }
                    var part2Length = BitConverter.ToInt32(intBuffer, 0);
                    if (fs.Read(intBuffer, 0, 4) != 4)
                    {
                        return(false);
                    }
                    var part2Crc   = BitConverter.ToUInt32(intBuffer, 0);
                    var part2Bytes = new byte[part2Length];
                    if (fs.Read(part2Bytes, 0, part2Length) != part2Length)
                    {
                        return(false);
                    }

                    if (part2Crc != Crc32.CalculateCrc(part2Bytes, 0, part2Length, CRC_SEED))
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
        public void Serialize(Stream stream)
        {
            List <byte> part1Bytes = new List <byte>();

            part1Bytes.AddRange(BitConverter.GetBytes(EngineVersion));
            part1Bytes.AddRange(BitConverter.GetBytes(LicenseeVersion));
            part1Bytes.AddRange(TAGame_Replay_Soccar_TA.Serialize());

            part1Bytes.AddRange(Properties.Serialize());

            var bytes = part1Bytes.ToArray();

            stream.Write(BitConverter.GetBytes(part1Bytes.Count), 0, 4);
            var crc = Crc32.CalculateCrc(bytes, 0, bytes.Length, CRC_SEED);

            stream.Write(BitConverter.GetBytes(crc), 0, 4);
            stream.Write(bytes, 0, part1Bytes.Count);


            List <byte> part2Bytes = new List <byte>();

            part2Bytes.AddRange(BitConverter.GetBytes(Levels.Count));
            foreach (var level in Levels)
            {
                part2Bytes.AddRange(level.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(KeyFrames.Count));
            foreach (var keyFrame in KeyFrames)
            {
                part2Bytes.AddRange(keyFrame.Serialize());
            }

            var bw = new BitWriter(8 * 1024 * 1024); // 1MB is a decent starting point
            Dictionary <UInt32, ActorState> newActorsById = new Dictionary <uint, ActorState>();
            var maxChannels = MaxChannels();

            foreach (Frame f in Frames)
            {
                f.Serialize(maxChannels, ref newActorsById, EngineVersion, LicenseeVersion, bw);
            }
            var networkStreamBytes = bw.GetBytes();

            part2Bytes.AddRange(BitConverter.GetBytes(networkStreamBytes.Length));
            part2Bytes.AddRange(networkStreamBytes);


            part2Bytes.AddRange(BitConverter.GetBytes(DebugStrings.Count));
            foreach (var debugString in DebugStrings)
            {
                part2Bytes.AddRange(debugString.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(TickMarks.Count));
            foreach (var tickMark in TickMarks)
            {
                part2Bytes.AddRange(tickMark.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Packages.Count));
            foreach (var packages in Packages)
            {
                part2Bytes.AddRange(packages.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Objects.Length));
            foreach (var obj in Objects)
            {
                part2Bytes.AddRange(obj.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Names.Length));
            foreach (var name in Names)
            {
                part2Bytes.AddRange(name.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(ClassIndexes.Count));
            foreach (var classIndex in ClassIndexes)
            {
                part2Bytes.AddRange(classIndex.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(ClassNetCaches.Length));
            foreach (var classNetCache in ClassNetCaches)
            {
                part2Bytes.AddRange(classNetCache.Serialize());
            }

            bytes = part2Bytes.ToArray();
            stream.Write(BitConverter.GetBytes(part2Bytes.Count), 0, 4);
            crc = Crc32.CalculateCrc(bytes, 0, bytes.Length, CRC_SEED);
            stream.Write(BitConverter.GetBytes(crc), 0, 4);
            stream.Write(bytes, 0, part2Bytes.Count);
        }
        public void Serialize(Stream stream)
        {
            List<byte> part1Bytes = new List<byte>();

            part1Bytes.AddRange(BitConverter.GetBytes(EngineVersion));
            part1Bytes.AddRange(BitConverter.GetBytes(LicenseeVersion));
            
            if (EngineVersion >= 868 && LicenseeVersion >= 18)
            {
                part1Bytes.AddRange(BitConverter.GetBytes(NetVersion));
            }

            part1Bytes.AddRange(TAGame_Replay_Soccar_TA.Serialize());
            
            part1Bytes.AddRange(Properties.Serialize());

            var bytes = part1Bytes.ToArray();
            stream.Write(BitConverter.GetBytes(part1Bytes.Count), 0, 4);
            var crc = Crc32.CalculateCrc(bytes, 0, bytes.Length, CRC_SEED);
            stream.Write(BitConverter.GetBytes(crc), 0, 4);
            stream.Write(bytes, 0, part1Bytes.Count);


            List<byte> part2Bytes = new List<byte>();

            part2Bytes.AddRange(BitConverter.GetBytes(Levels.Count));
            foreach (var level in Levels)
            {
                part2Bytes.AddRange(level.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(KeyFrames.Count));
            foreach (var keyFrame in KeyFrames)
            {
                part2Bytes.AddRange(keyFrame.Serialize());
            }

            var bw = new BitWriter(8 * 1024 * 1024); // 1MB is a decent starting point
            var maxChannels = MaxChannels();
            foreach (Frame f in Frames)
            {
                f.Serialize(maxChannels, Objects, EngineVersion, LicenseeVersion, NetVersion, bw);
            }
            
            var networkStreamBytes = bw.GetBytes();
            var paddingSize = 512 - (networkStreamBytes.Length % 512); // Padding needed to match original replay files, but dont think it's necessary to produce good replay
            if ( paddingSize == 512 )
            {
                paddingSize = 0;
            }

            part2Bytes.AddRange(BitConverter.GetBytes(networkStreamBytes.Length + paddingSize));
            part2Bytes.AddRange(networkStreamBytes);

            for (int i = 0; i < paddingSize; ++i)
            {
                part2Bytes.Add(0);
            }

            part2Bytes.AddRange(BitConverter.GetBytes(DebugStrings.Count));
            foreach (var debugString in DebugStrings)
            {
                part2Bytes.AddRange(debugString.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(TickMarks.Count));
            foreach (var tickMark in TickMarks)
            {
                part2Bytes.AddRange(tickMark.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Packages.Count));
            foreach (var packages in Packages)
            {
                part2Bytes.AddRange(packages.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Objects.Length));
            foreach (var obj in Objects)
            {
                part2Bytes.AddRange(obj.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(Names.Length));
            foreach (var name in Names)
            {
                part2Bytes.AddRange(name.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(ClassIndexes.Count));
            foreach (var classIndex in ClassIndexes)
            {
                part2Bytes.AddRange(classIndex.Serialize());
            }

            part2Bytes.AddRange(BitConverter.GetBytes(ClassNetCaches.Length));
            foreach (var classNetCache in ClassNetCaches)
            {
                part2Bytes.AddRange(classNetCache.Serialize());
            }

            if (NetVersion >= 10)
            {
                part2Bytes.AddRange(BitConverter.GetBytes(Unknown));
            }

            bytes = part2Bytes.ToArray();
            stream.Write(BitConverter.GetBytes(part2Bytes.Count), 0, 4);
            crc = Crc32.CalculateCrc(bytes, 0, bytes.Length, CRC_SEED);
            stream.Write(BitConverter.GetBytes(crc), 0, 4);
            stream.Write(bytes, 0, part2Bytes.Count);
        }