diff --git a/DepotDownloader/ContentDownloader.cs b/DepotDownloader/ContentDownloader.cs index 0015ee95..f072a63e 100644 --- a/DepotDownloader/ContentDownloader.cs +++ b/DepotDownloader/ContentDownloader.cs @@ -583,8 +583,30 @@ namespace DepotDownloader if ( lastManifestId != INVALID_MANIFEST_ID ) { var oldManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", lastManifestId ) ); - if ( File.Exists( oldManifestFileName ) ) - oldProtoManifest = ProtoManifest.LoadFromFile( oldManifestFileName ); + + if (File.Exists(oldManifestFileName)) + { + byte[] expectedChecksum, currentChecksum; + + try + { + expectedChecksum = File.ReadAllBytes(oldManifestFileName + ".sha"); + } + catch (IOException) + { + expectedChecksum = null; + } + + oldProtoManifest = ProtoManifest.LoadFromFile(oldManifestFileName, out currentChecksum); + + if (expectedChecksum == null || !expectedChecksum.SequenceEqual(currentChecksum)) + { + // We only have to show this warning if the old manifest ID was different + if (lastManifestId != depot.manifestId) + Console.WriteLine("Manifest {0} on disk did not match the expected checksum.", lastManifestId); + oldProtoManifest = null; + } + } } if ( lastManifestId == depot.manifestId && oldProtoManifest != null ) @@ -597,7 +619,24 @@ namespace DepotDownloader var newManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", depot.manifestId ) ); if ( newManifestFileName != null ) { - newProtoManifest = ProtoManifest.LoadFromFile( newManifestFileName ); + byte[] expectedChecksum, currentChecksum; + + try + { + expectedChecksum = File.ReadAllBytes(newManifestFileName + ".sha"); + } + catch (IOException) + { + expectedChecksum = null; + } + + newProtoManifest = ProtoManifest.LoadFromFile(newManifestFileName, out currentChecksum); + + if (expectedChecksum == null || !expectedChecksum.SequenceEqual(currentChecksum)) + { + Console.WriteLine("Manifest {0} on disk did not match the expected checksum.", depot.manifestId); + newProtoManifest = null; + } } if ( newProtoManifest != null ) @@ -656,8 +695,11 @@ namespace DepotDownloader return; } + byte[] checksum; + newProtoManifest = new ProtoManifest( depotManifest, depot.manifestId ); - newProtoManifest.SaveToFile( newManifestFileName ); + newProtoManifest.SaveToFile( newManifestFileName, out checksum ); + File.WriteAllBytes( newManifestFileName + ".sha", checksum ); Console.WriteLine( " Done!" ); } diff --git a/DepotDownloader/ProtoManifest.cs b/DepotDownloader/ProtoManifest.cs index 6a80da47..7c1c086e 100644 --- a/DepotDownloader/ProtoManifest.cs +++ b/DepotDownloader/ProtoManifest.cs @@ -117,21 +117,42 @@ namespace DepotDownloader [ProtoMember(2)] public ulong ID { get; private set; } - public static ProtoManifest LoadFromFile(string filename) + public static ProtoManifest LoadFromFile(string filename, out byte[] checksum) { if (!File.Exists(filename)) + { + checksum = null; return null; + } + + using (MemoryStream ms = new MemoryStream()) + { + using (FileStream fs = File.Open(filename, FileMode.Open)) + using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress)) + ds.CopyTo(ms); + + checksum = Util.SHAHash(ms.ToArray()); - using (FileStream fs = File.Open(filename, FileMode.Open)) - using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress)) - return ProtoBuf.Serializer.Deserialize(ds); + ms.Seek(0, SeekOrigin.Begin); + return ProtoBuf.Serializer.Deserialize(ms); + } } - public void SaveToFile(string filename) + public void SaveToFile(string filename, out byte[] checksum) { - using (FileStream fs = File.Open(filename, FileMode.Create)) - using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress)) - ProtoBuf.Serializer.Serialize(ds, this); + + using (MemoryStream ms = new MemoryStream()) + { + ProtoBuf.Serializer.Serialize(ms, this); + + checksum = Util.SHAHash(ms.ToArray()); + + ms.Seek(0, SeekOrigin.Begin); + + using (FileStream fs = File.Open(filename, FileMode.Create)) + using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress)) + ms.CopyTo(ds); + } } } }