diff --git a/DepotDownloader/ContentDownloader.cs b/DepotDownloader/ContentDownloader.cs index b3273831..39bec8f8 100644 --- a/DepotDownloader/ContentDownloader.cs +++ b/DepotDownloader/ContentDownloader.cs @@ -559,13 +559,13 @@ namespace DepotDownloader Directory.CreateDirectory(fileStagingPath); } else - { + { // Some manifests don't explicitly include all necessary directories Directory.CreateDirectory(Path.GetDirectoryName(fileFinalPath)); Directory.CreateDirectory(Path.GetDirectoryName(fileStagingPath)); - complete_download_size += file.TotalSize; - } + complete_download_size += file.TotalSize; + } }); var rand = new Random(); @@ -576,7 +576,7 @@ namespace DepotDownloader { var clientIndex = rand.Next(0, cdnClients.Count); - string download_path = Path.Combine(depot.installDir, file.FileName); + string fileFinalPath = Path.Combine(depot.installDir, file.FileName); string fileStagingPath = Path.Combine(stagingDir, file.FileName); // This may still exist if the previous run exited before cleanup @@ -587,11 +587,12 @@ namespace DepotDownloader FileStream fs = null; List neededChunks; - FileInfo fi = new FileInfo(download_path); + FileInfo fi = new FileInfo(fileFinalPath); if (!fi.Exists) { // create new file. need all chunks - fs = File.Create(download_path); + fs = File.Create(fileFinalPath); + fs.SetLength((long)file.TotalSize); neededChunks = new List(file.Chunks); } else @@ -607,72 +608,63 @@ namespace DepotDownloader { neededChunks = new List(); - // we have a version of this file. it may or may not match what we want + if (oldManifestFile.FileHash != file.FileHash) + { + // we have a version of this file, but it doesn't fully match what we want - var matchingChunks = new List(); + var matchingChunks = new List(); - foreach (var chunk in file.Chunks) - { - var oldChunk = oldManifestFile.Chunks.FirstOrDefault(c => c.ChunkID.SequenceEqual(chunk.ChunkID)); - if (oldChunk != null) + foreach (var chunk in file.Chunks) { - matchingChunks.Add(new ChunkMatch(oldChunk, chunk)); - } - else - { - neededChunks.Add(chunk); + var oldChunk = oldManifestFile.Chunks.FirstOrDefault(c => c.ChunkID.SequenceEqual(chunk.ChunkID)); + if (oldChunk != null) + { + matchingChunks.Add(new ChunkMatch(oldChunk, chunk)); + } + else + { + neededChunks.Add(chunk); + } } - } - // can't use FileHash here. It doesn't seem to actually change... - if (matchingChunks.Count != file.Chunks.Count) - { - if (File.Exists(download_path)) - { - File.Move(download_path, fileStagingPath); + File.Move(fileFinalPath, fileStagingPath); - fs = File.Open(download_path, FileMode.Create); - fs.SetLength((long)file.TotalSize); + fs = File.Open(fileFinalPath, FileMode.Create); + fs.SetLength((long)file.TotalSize); - using (var fsOld = File.Open(fileStagingPath, FileMode.Open)) + using (var fsOld = File.Open(fileStagingPath, FileMode.Open)) + { + foreach (var match in matchingChunks) { - foreach (var match in matchingChunks) - { - fs.Seek((long)match.NewChunk.Offset, SeekOrigin.Begin); - fsOld.Seek((long)match.OldChunk.Offset, SeekOrigin.Begin); - - byte[] tmp = new byte[match.OldChunk.UncompressedLength]; - fsOld.Read(tmp, 0, tmp.Length); - fs.Write(tmp, 0, tmp.Length); - } - } + fs.Seek((long)match.NewChunk.Offset, SeekOrigin.Begin); + fsOld.Seek((long)match.OldChunk.Offset, SeekOrigin.Begin); - File.Delete(fileStagingPath); - } - else - { - fs = File.Open(download_path, FileMode.Create); - fs.SetLength((long)file.TotalSize); - neededChunks = new List(file.Chunks); + byte[] tmp = new byte[match.OldChunk.UncompressedLength]; + fsOld.Read(tmp, 0, tmp.Length); + fs.Write(tmp, 0, tmp.Length); + } } + + File.Delete(fileStagingPath); } } else { - fs = File.Open(download_path, FileMode.Open); + // No old manifest or file not in old manifest. We must validate. + + fs = File.Open(fileFinalPath, FileMode.Open); if ((ulong)fi.Length != file.TotalSize) { fs.SetLength((long)file.TotalSize); } - // find which chunks we need, in order so that we aren't seeking every which way neededChunks = Util.ValidateSteam3FileChecksums(fs, file.Chunks.OrderBy(x => x.Offset).ToArray()); } if (neededChunks.Count() == 0) { size_downloaded += file.TotalSize; - Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path); + Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, fileFinalPath); if (fs != null) fs.Close(); return; @@ -725,7 +717,7 @@ namespace DepotDownloader fs.Close(); - Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path); + Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, fileFinalPath); }); newProtoManifest.SaveToFile(Path.Combine(configDir, string.Format("{0}.bin", depot.id))); diff --git a/DepotDownloader/Program.cs b/DepotDownloader/Program.cs index 94d7e404..d5dd92f1 100644 --- a/DepotDownloader/Program.cs +++ b/DepotDownloader/Program.cs @@ -92,7 +92,7 @@ namespace DepotDownloader if (username != null && password == null) { - Console.Write("Enter account password: "); + Console.Write("Enter account password for \"{0}\": ", username); password = Util.ReadPassword(); Console.WriteLine(); }