Represents a client able to connect to the Steam3 CDN and download games on the new content system.
        public static void SetCDNClient(CDNClient cdnClient)
        {
            CDNClient = cdnClient;

            ReloadFileList();

            string filesDir = Path.Combine(Application.Path, "files", ".support", "hashes");
            Directory.CreateDirectory(filesDir);
        }
        public DepotProcessor(SteamClient client, CallbackManager manager)
        {
            DepotLocks = new ConcurrentDictionary<uint, byte>();

            CDNClient = new CDNClient(client);

            FileDownloader.SetCDNClient(CDNClient);

            CDNServers = new List<string>
            {
                "cdn.level3.cs.steampowered.com",
                "cdn.akamai.cs.steampowered.com",
                "cdn.highwinds.cs.steampowered.com"
            };

            manager.Subscribe<SteamApps.CDNAuthTokenCallback>(OnCDNAuthTokenCallback);
            manager.Subscribe<SteamApps.DepotKeyCallback>(OnDepotKeyCallback);
        }
        public DepotProcessor(SteamClient client, CallbackManager manager)
        {
            DepotLocks = new ConcurrentDictionary<uint, byte>();

            CDNClient = new CDNClient(client);

            CDNServers = new List<string>
            {
                "content1.steampowered.com", // Limelight
                "content2.steampowered.com", // Level3
                "content3.steampowered.com", // Highwinds
                //"content4.steampowered.com", // EdgeCast, seems to be missing content
                "content5.steampowered.com", // CloudFront
                //"content6.steampowered.com", // Comcast, non optimal
                "content7.steampowered.com", // Akamai
                "content8.steampowered.com" // Akamai
            };

            manager.Register(new Callback<SteamApps.CDNAuthTokenCallback>(OnCDNAuthTokenCallback));
            manager.Register(new Callback<SteamApps.DepotKeyCallback>(OnDepotKeyCallback));
        }
        public DepotProcessor(SteamClient client)
        {
            UpdateScript = Path.Combine(Application.Path, "files", "update.sh");
            UpdateScriptLock = new SpinLock();
            DepotLocks = new Dictionary<uint, byte>();

            DepotThreadPool = new SmartThreadPool();
            DepotThreadPool.Concurrency = Settings.Current.FullRun == FullRunState.WithForcedDepots ? 15 : 5;
            DepotThreadPool.Name = "Depot Thread Pool";

            CDNClient = new CDNClient(client);

            FileDownloader.SetCDNClient(CDNClient);

            CDNServers = new List<string>
            {
                "cdn.level3.cs.steampowered.com",
                "cdn.akamai.cs.steampowered.com",
                "cdn.highwinds.cs.steampowered.com"
            };
        }
        private static void DownloadSteam3( List<DepotDownloadInfo> depots )
        {
            foreach (var depot in depots)
            {
                int depotId = depot.id;
                ulong depot_manifest = depot.manifestId;
                byte[] depotKey = depot.depotKey;
                string installDir = depot.installDir;

                Console.WriteLine("Downloading depot {0} - {1}", depot.id, depot.contentName);
                Console.Write("Finding content servers...");

                List<IPEndPoint> serverList = steam3.steamClient.GetServersOfType(EServerType.CS);

                List<CDNClient.ClientEndPoint> cdnServers = null;
                int counterDeferred = 0;

                for (int i = 0; ; i++)
                {
                    IPEndPoint endpoint = serverList[i % serverList.Count];

                    cdnServers = CDNClient.FetchServerList(new CDNClient.ClientEndPoint(endpoint.Address.ToString(), endpoint.Port), Config.CellID);

                    if (cdnServers == null) counterDeferred++;

                    if (cdnServers != null && cdnServers.Count((ep) => { return ep.Type == "CS"; }) > 0)
                        break;

                    if (((i + 1) % serverList.Count) == 0)
                    {
                        Console.WriteLine("Unable to find any Steam3 content servers");
                        return;
                    }
                }

                Console.WriteLine(" Done!");
                Console.Write("Downloading depot manifest...");

                List<CDNClient.ClientEndPoint> cdnEndpoints = cdnServers.Where((ep) => { return ep.Type == "CDN"; }).ToList();
                List<CDNClient.ClientEndPoint> csEndpoints = cdnServers.Where((ep) => { return ep.Type == "CS"; }).ToList();
                List<CDNClient> cdnClients = new List<CDNClient>();
                byte[] appTicket = steam3.AppTickets[(uint)depotId];

                foreach (var server in csEndpoints)
                {
                    CDNClient client;
                    if (appTicket == null)
                        client = new CDNClient(server, (uint)depotId, steam3.steamUser.SteamID);
                    else
                        client = new CDNClient(server, appTicket);

                    if (client.Connect())
                    {
                        cdnClients.Add(client);

                        if (cdnClients.Count >= NUM_STEAM3_CONNECTIONS)
                            break;
                    }
                }
                if (cdnClients.Count == 0)
                {
                    Console.WriteLine("\nCould not initialize connection with CDN.");
                    return;
                }

                DepotManifest depotManifest = cdnClients[0].DownloadDepotManifest( depotId, depot_manifest );

                if ( depotManifest == null )
                {
                    // TODO: check for 401s
                    for (int i = 1; i < cdnClients.Count && depotManifest == null; i++)
                    {
                        depotManifest = cdnClients[i].DownloadDepotManifest( depotId, depot_manifest );
                    }

                    if (depotManifest == null)
                    {
                        Console.WriteLine("\nUnable to download manifest {0} for depot {1}", depot_manifest, depotId);
                        return;
                    }
                }

                if (!depotManifest.DecryptFilenames(depotKey))
                {
                    Console.WriteLine("\nUnable to decrypt manifest for depot {0}", depotId);
                    return;
                }

                Console.WriteLine(" Done!");

                ulong complete_download_size = 0;
                ulong size_downloaded = 0;

                depotManifest.Files.Sort((x, y) => { return x.FileName.CompareTo(y.FileName); });

                if (Config.DownloadManifestOnly)
                {
                    StringBuilder manifestBuilder = new StringBuilder();
                    string txtManifest = Path.Combine(depot.installDir, string.Format("manifest_{0}.txt", depot.id));

                    foreach (var file in depotManifest.Files)
                    {
                        if (file.Flags.HasFlag(EDepotFileFlag.Directory))
                            continue;

                        manifestBuilder.Append(string.Format("{0}\n", file.FileName));
                    }

                    File.WriteAllText(txtManifest, manifestBuilder.ToString());
                    continue;
                }

                depotManifest.Files.RemoveAll((x) => !TestIsFileIncluded(x.FileName));

                foreach (var file in depotManifest.Files)
                {
                    complete_download_size += file.TotalSize;
                }

                foreach (var file in depotManifest.Files)
                {
                    string download_path = Path.Combine(installDir, file.FileName);

                    if (file.Flags.HasFlag(EDepotFileFlag.Directory))
                    {
                        if (!Directory.Exists(download_path))
                            Directory.CreateDirectory(download_path);
                        continue;
                    }

                    string dir_path = Path.GetDirectoryName(download_path);

                    if (!Directory.Exists(dir_path))
                        Directory.CreateDirectory(dir_path);

                    FileStream fs;
                    DepotManifest.ChunkData[] neededChunks;
                    FileInfo fi = new FileInfo(download_path);
                    if (!fi.Exists)
                    {
                        // create new file. need all chunks
                        fs = File.Create(download_path);
                        neededChunks = file.Chunks.ToArray();
                    }
                    else
                    {
                        // open existing
                        fs = File.Open(download_path, 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);
                            fs.Close();
                            continue;
                        }
                        else
                        {
                            size_downloaded += (file.TotalSize - (ulong)neededChunks.Select(x => (int)x.UncompressedLength).Sum());
                        }
                    }

                    Console.Write("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path);

                    foreach (var chunk in neededChunks)
                    {
                        string chunkID = EncodeHexString(chunk.ChunkID);

                        byte[] encrypted_chunk = cdnClients[0].DownloadDepotChunk(depotId, chunkID);

                        if (encrypted_chunk == null)
                        {
                            for (int i = 1; i < cdnClients.Count && encrypted_chunk == null; i++)
                            {
                                encrypted_chunk = cdnClients[i].DownloadDepotChunk(depotId, chunkID);
                            }

                            if (encrypted_chunk == null)
                            {
                                Console.WriteLine("Unable to download chunk id {0} for depot {1}", chunkID, depotId);
                                fs.Close();
                                return;
                            }
                        }

                        byte[] chunk_data = CDNClient.ProcessChunk(encrypted_chunk, depotKey);

                        fs.Seek((long)chunk.Offset, SeekOrigin.Begin);
                        fs.Write(chunk_data, 0, chunk_data.Length);

                        size_downloaded += chunk.UncompressedLength;

                        Console.CursorLeft = 0;
                        Console.Write("{0,6:#00.00}%", ((float)size_downloaded / (float)complete_download_size) * 100.0f);
                    }

                    fs.Close();

                    Console.WriteLine();
                }
            }
        }
        private static void DownloadManifest(ManifestJob request)
        {
            CDNClient cdnClient = new CDNClient(Steam.Instance.Client, request.DepotID, request.Ticket, request.DepotKey);
            List<CDNClient.Server> cdnServers;

            try
            {
                cdnServers = cdnClient.FetchServerList();

                if (cdnServers.Count == 0)
                {
                    throw new InvalidOperationException("No servers returned"); // Great programming!
                }
            }
            catch
            {
                Log.WriteError("Depot Processor", "Failed to get server list for depot {0}", request.DepotID);

                lock (ManifestJobs)
                {
                    ManifestJobs.Remove(request);
                }

                return;
            }

            DepotManifest depotManifest = null;

            foreach (var server in cdnServers)
            {
                try
                {
                    cdnClient.Connect(server);

                    depotManifest = cdnClient.DownloadManifest(request.ManifestID);

                    break;
                }
                catch { }
            }

            lock (ManifestJobs)
            {
                ManifestJobs.Remove(request);
            }

            if (depotManifest == null)
            {
                Log.WriteError("Depot Processor", "Failed to download depot manifest for depot {0} (parent {1}) (jobs still in queue: {2})", request.DepotID, request.ParentAppID, ManifestJobs.Count);

                if (SteamProxy.Instance.ImportantApps.Contains(request.ParentAppID))
                {
                    IRC.SendMain("Important manifest update: {0}{1}{2} {3}(parent {4}){5} -{6} manifest download failed from {7} servers", Colors.OLIVE, request.DepotName, Colors.NORMAL, Colors.DARK_GRAY, request.ParentAppID, Colors.NORMAL, Colors.RED, cdnServers.Count);
                }

                return;
            }

            if (SteamProxy.Instance.ImportantApps.Contains(request.ParentAppID))
            {
                IRC.SendMain("Important manifest update: {0}{1}{2} {3}(parent {4}){5} -{6} {7}", Colors.OLIVE, request.DepotName, Colors.NORMAL, Colors.DARK_GRAY, request.ParentAppID, Colors.NORMAL, Colors.DARK_BLUE, SteamDB.GetDepotURL(request.DepotID, "history"));
            }

            var sortedFiles = depotManifest.Files.OrderBy(f => f.FileName, StringComparer.OrdinalIgnoreCase);

            bool shouldHistorize = false;
            List<DepotFile> filesNew = new List<DepotFile>();
            List<DepotFile> filesOld = new List<DepotFile>();

            foreach (var file in sortedFiles)
            {
                System.Text.Encoding.UTF8.GetString(file.FileHash);

                filesNew.Add(new DepotFile
                {
                    Name = file.FileName.Replace('\\', '/'),
                    Size = file.TotalSize,
                    Chunks = file.Chunks.Count,
                    Flags = (int)file.Flags
                });
            }

            using (MySqlDataReader Reader = DbWorker.ExecuteReader("SELECT `Files` FROM `Depots` WHERE `DepotID` = @DepotID LIMIT 1", new MySqlParameter("DepotID", request.DepotID)))
            {
                if (Reader.Read())
                {
                    string files = Reader.GetString("Files");

                    if (!string.IsNullOrEmpty(files))
                    {
                        shouldHistorize = true;
                        filesOld = JsonConvert.DeserializeObject<List<DepotFile>>(files);
                    }
                }
            }

            DbWorker.ExecuteNonQuery("UPDATE `Depots` SET `Files` = @Files WHERE `DepotID` = @DepotID",
                                     new MySqlParameter("@DepotID", request.DepotID),
                                     new MySqlParameter("@Files", JsonConvert.SerializeObject(filesNew, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }))
            );

            if (shouldHistorize)
            {
                List<string> filesAdded = new List<string>();

                foreach (var file in filesNew)
                {
                    var oldFile = filesOld.Find(x => x.Name == file.Name);

                    if (oldFile == null)
                    {
                        // We want to historize modifications first, and only then deletions and additions
                        filesAdded.Add(file.Name);
                    }
                    else
                    {
                        if (oldFile.Size != file.Size)
                        {
                            MakeHistory(request, file.Name, "modified", oldFile.Size, file.Size);
                        }

                        filesOld.Remove(oldFile);
                    }
                }

                foreach (var file in filesOld)
                {
                    MakeHistory(request, file.Name, "removed");
                }

                foreach (string file in filesAdded)
                {
                    MakeHistory(request, file, "added");
                }
            }

            lock (ManifestJobs)
            {
                Log.WriteDebug("Depot Processor", "DepotID: Processed {0} (jobs still in queue: {1})", request.DepotID, ManifestJobs.Count);
            }

            // Our job is done here, now download important files if any
            if (Settings.Current.ImportantFiles.ContainsKey(request.DepotID))
            {
                // TODO: Horribly inefficient
                var importantFiles = sortedFiles.Where(x => Settings.Current.ImportantFiles[request.DepotID].Contains(x.FileName.Replace('\\', '/')));

                ThreadPool.QueueWorkItem(DownloadFiles, importantFiles, request.DepotID, cdnClient);
            }
        }
        private static void DownloadFiles(IEnumerable<DepotManifest.FileData> importantFiles, uint DepotID, CDNClient cdnClient)
        {
            foreach (var file in importantFiles)
            {
                Log.WriteDebug("Important Files", "Downloading {0} ({1} bytes, {2} chunks)", file.FileName, file.TotalSize, file.Chunks.Count);

                string downloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILES_DIRECTORY, string.Format("{0}_{1}", DepotID, file.FileName.Replace('\\', '/').Replace('/', '_')));

                using (FileStream fs = File.Open(downloadPath, FileMode.OpenOrCreate))
                {
                    fs.SetLength((long)file.TotalSize);

                    foreach (var chunk in file.Chunks)
                    {
                        try
                        {
                            var chunkData = cdnClient.DownloadDepotChunk(chunk);

                            fs.Seek((long)chunk.Offset, SeekOrigin.Begin);
                            fs.Write(chunkData.Data, 0, chunkData.Data.Length);

                            Log.WriteDebug("Important Files", "Downloaded {0}", file.FileName);

                            break;
                        }
                        catch (Exception e)
                        {
                            Log.WriteDebug("Important Files", "Fail {0}", e.Message);
                        }
                    }
                }
            }
        }
Example #8
0
        private static void DownloadSteam3( int depotId, ulong depot_manifest, byte[] depotKey, string installDir )
        {
            Console.Write("Finding content servers...");

            List<IPEndPoint> serverList = steam3.steamClient.GetServersOfType(EServerType.CS);

            List<CDNClient.ClientEndPoint> cdnServers = null;
            int tries = 0, counterDeferred = 0;

            for(int i = 0; ; i++ )
            {
                IPEndPoint endpoint = serverList[i % serverList.Count];

                cdnServers = CDNClient.FetchServerList(new CDNClient.ClientEndPoint(endpoint.Address.ToString(), endpoint.Port), Config.CellID);

                if (cdnServers == null) counterDeferred++;

                if (cdnServers != null && cdnServers.Count((ep) => { return ep.Type == "CS"; }) > 0)
                    break;

                if (((i+1) % serverList.Count) == 0)
                {
                    if (++tries > MAX_CONNECT_RETRIES)
                    {
                        Console.WriteLine("\nGiving up finding Steam3 content server.");
                        return;
                    }

                    Console.Write("\nSearching for content servers... (deferred: {0})", counterDeferred);
                    counterDeferred = 0;
                    Thread.Sleep(1000);
                }
            }

            if (cdnServers == null || cdnServers.Count == 0)
            {
                Console.WriteLine("Unable to find any Steam3 content servers");
                return;
            }

            Console.WriteLine(" Done!");
            Console.Write("Downloading depot manifest...");

            List<CDNClient.ClientEndPoint> cdnEndpoints = cdnServers.Where((ep) => { return ep.Type == "CDN"; }).ToList();
            List<CDNClient.ClientEndPoint> csEndpoints = cdnServers.Where((ep) => { return ep.Type == "CS"; }).ToList();
            CDNClient cdnClient = null;

            foreach (var server in csEndpoints)
            {
                CDNClient client = new CDNClient(server, steam3.AppTickets[(uint)depotId]);

                if (client.Connect())
                {
                    cdnClient = client;
                    break;
                }
            }

            if (cdnClient == null)
            {
                Console.WriteLine("\nCould not initialize connection with CDN.");
                return;
            }

            DepotManifest depotManifest = cdnClient.DownloadDepotManifest( depotId, depot_manifest );

            if ( depotManifest == null )
            {
                Console.WriteLine( "\nUnable to download manifest {0} for depot {1}", depot_manifest, depotId );
                return;
            }

            if (!depotManifest.DecryptFilenames(depotKey))
            {
                Console.WriteLine("\nUnable to decrypt manifest for depot {0}", depotId);
                return;
            }

            Console.WriteLine(" Done!");

            ulong complete_download_size = 0;
            ulong size_downloaded = 0;

            depotManifest.Files.RemoveAll((x) => !TestIsFileIncluded(x.FileName));
            depotManifest.Files.Sort((x, y) => { return x.FileName.CompareTo(y.FileName); });
 
            foreach (var file in depotManifest.Files)
            {
                complete_download_size += file.TotalSize;
            }

            foreach (var file in depotManifest.Files)
            {
                string download_path = Path.Combine(installDir, file.FileName);

                if (file.TotalSize == 0) // directory
                {
                    if (!Directory.Exists(download_path))
                        Directory.CreateDirectory(download_path);
                    continue;
                }

                string dir_path = Path.GetDirectoryName(download_path);

                if (!Directory.Exists(dir_path))
                    Directory.CreateDirectory(dir_path);

                // TODO: non-checksum validation
                FileInfo fi = new FileInfo(download_path);
                if (fi.Exists && (ulong)fi.Length == file.TotalSize)
                {
                    size_downloaded += file.TotalSize;
                    Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path);
                    continue;
                }

                Console.Write("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path);

                FileStream fs = File.Create(download_path);
                fs.SetLength((long)file.TotalSize);

                foreach (var chunk in file.Chunks)
                {
                    string chunkID = EncodeHexString(chunk.ChunkID);

                    byte[] encrypted_chunk = cdnClient.DownloadDepotChunk(depotId, chunkID);
                    byte[] chunk_data = CDNClient.ProcessChunk(encrypted_chunk, depotKey);

                    fs.Seek((long)chunk.Offset, SeekOrigin.Begin);
                    fs.Write(chunk_data, 0, chunk_data.Length);

                    size_downloaded += chunk.UncompressedLength;

                    Console.CursorLeft = 0;
                    Console.Write("{0,6:#00.00}%", ((float)size_downloaded / (float)complete_download_size) * 100.0f);
                }

                Console.WriteLine();
            }
        }
        public static void SetCDNClient(CDNClient cdnClient)
        {
            CDNClient = cdnClient;

            try
            {
                string filesDir = Path.Combine(Application.Path, FILES_DIRECTORY, ".support", "chunks");
                Directory.CreateDirectory(filesDir);
            }
            catch (Exception ex)
            {
                Log.WriteError("FileDownloader", "Unable to create files directory: {0}", ex.Message);
            }

            ReloadFileList();
        }
 public CdnClient(SteamKit2.CDNClient cdnClient, CdnServerWrapper serverWrapper)
 {
     InternalCdnClient = cdnClient;
     ServerWrapper     = serverWrapper;
 }
        private static void DownloadManifest(ManifestJob request)
        {
            var cdnClient = new CDNClient(Steam.Instance.Client, request.DepotID, request.Ticket, request.DepotKey);
            List<CDNClient.Server> cdnServers = null;

            for (var i = 0; i <= 5; i++)
            {
                try
                {
                    cdnServers = cdnClient.FetchServerList();

                    if (cdnServers.Count > 0)
                    {
                        break;
                    }
                }
                catch { }
            }

            if (cdnServers == null || cdnServers.Count == 0)
            {
                Log.WriteError("Depot Processor", "Failed to get server list for depot {0}", request.DepotID);

                if (SteamProxy.Instance.ImportantApps.Contains(request.ParentAppID))
                {
                    IRC.SendMain("Important manifest update: {0}{1}{2} {3}(parent {4}){5} -{6} failed to fetch server list", Colors.OLIVE, request.DepotName, Colors.NORMAL, Colors.DARK_GRAY, request.ParentAppID, Colors.NORMAL, Colors.RED);
                }

                lock (ManifestJobs)
                {
                    ManifestJobs.Remove(request);
                }

                return;
            }

            DepotManifest depotManifest = null;

            foreach (var server in cdnServers)
            {
                try
                {
                    cdnClient.Connect(server);

                    depotManifest = cdnClient.DownloadManifest(request.ManifestID);

                    break;
                }
                catch { }
            }

            lock (ManifestJobs)
            {
                ManifestJobs.Remove(request);
            }

            if (depotManifest == null)
            {
                Log.WriteError("Depot Processor", "Failed to download depot manifest for depot {0} (parent {1}) (jobs still in queue: {2})", request.DepotID, request.ParentAppID, ManifestJobs.Count);

                if (SteamProxy.Instance.ImportantApps.Contains(request.ParentAppID))
                {
                    IRC.SendMain("Important manifest update: {0}{1}{2} {3}(parent {4}){5} -{6} failed to download depot manifest from {7} servers", Colors.OLIVE, request.DepotName, Colors.NORMAL, Colors.DARK_GRAY, request.ParentAppID, Colors.NORMAL, Colors.RED, cdnServers.Count);
                }

                return;
            }

            if (SteamProxy.Instance.ImportantApps.Contains(request.ParentAppID))
            {
                IRC.SendMain("Important manifest update: {0}{1}{2} {3}(parent {4}){5} -{6} {7}", Colors.OLIVE, request.DepotName, Colors.NORMAL, Colors.DARK_GRAY, request.ParentAppID, Colors.NORMAL, Colors.DARK_BLUE, SteamDB.GetDepotURL(request.DepotID, "history"));
            }

            var sortedFiles = depotManifest.Files.OrderBy(f => f.FileName, StringComparer.OrdinalIgnoreCase);

            bool shouldHistorize = false;
            var filesNew = new List<DepotFile>();
            var filesOld = new List<DepotFile>();

            foreach (var file in sortedFiles)
            {
                System.Text.Encoding.UTF8.GetString(file.FileHash);

                var depotFile = new DepotFile
                {
                    Name = file.FileName.Replace('\\', '/'),
                    Size = file.TotalSize,
                    Chunks = file.Chunks.Count,
                    Flags = (int)file.Flags
                };

                // TODO: Ideally we would check if filehash is not empty
                if (!file.Flags.HasFlag(EDepotFileFlag.Directory))
                {
                    depotFile.Hash = string.Concat(Array.ConvertAll(file.FileHash, x => x.ToString("X2")));
                }

                filesNew.Add(depotFile);
            }

            using (MySqlDataReader Reader = DbWorker.ExecuteReader("SELECT `Files` FROM `Depots` WHERE `DepotID` = @DepotID LIMIT 1", new MySqlParameter("DepotID", request.DepotID)))
            {
                if (Reader.Read())
                {
                    string files = Reader.GetString("Files");

                    if (!string.IsNullOrEmpty(files))
                    {
                        shouldHistorize = true;
                        filesOld = JsonConvert.DeserializeObject<List<DepotFile>>(files);
                    }
                }
            }

            DbWorker.ExecuteNonQuery("UPDATE `Depots` SET `Files` = @Files WHERE `DepotID` = @DepotID",
                                     new MySqlParameter("@DepotID", request.DepotID),
                                     new MySqlParameter("@Files", JsonConvert.SerializeObject(filesNew, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }))
            );

            if (shouldHistorize)
            {
                var filesAdded = new List<string>();

                foreach (var file in filesNew)
                {
                    var oldFile = filesOld.Find(x => x.Name == file.Name);

                    if (oldFile == null)
                    {
                        // We want to historize modifications first, and only then deletions and additions
                        filesAdded.Add(file.Name);
                    }
                    else
                    {
                        if (oldFile.Size != file.Size)
                        {
                            MakeHistory(request, file.Name, "modified", oldFile.Size, file.Size);
                        }
                        else if (file.Hash != null && oldFile.Hash != null && !file.Hash.Equals(oldFile.Hash))
                        {
                            MakeHistory(request, file.Name, "modified", oldFile.Size, file.Size);
                        }

                        filesOld.Remove(oldFile);
                    }
                }

                foreach (var file in filesOld)
                {
                    MakeHistory(request, file.Name, "removed");
                }

                foreach (string file in filesAdded)
                {
                    MakeHistory(request, file, "added");
                }
            }

            lock (ManifestJobs)
            {
                Log.WriteDebug("Depot Processor", "DepotID: Processed {0} (jobs still in queue: {1})", request.DepotID, ManifestJobs.Count);
            }
        }
        private static List<CDNClient> CollectCDNClientsForDepot(DepotDownloadInfo depot)
        {
            var cdnClients = new List<CDNClient>();
            CDNClient initialClient = new CDNClient(steam3.steamClient, steam3.AppTickets[depot.id]);
            var cdnServers = initialClient.FetchServerList(cellId: (uint)Config.CellID);

            // Grab up to the first eight server in the allegedly best-to-worst order from Steam
            var limit = cdnServers.Take(Config.MaxServers);
            int tries = 0;
            foreach (var s in limit)
            {
                CDNClient c;

                if (tries == 0)
                {
                    c = initialClient;
                }
                else
                {
                    c = new CDNClient(steam3.steamClient, steam3.AppTickets[depot.id]);
                }

                try
                {
                    c.Connect(s);
                    cdnClients.Add(c);
                }
                catch
                {
                    Console.WriteLine("\nFailed to connect to content server {0}. Remaining content servers for depot: {1}.", s, Config.MaxServers - tries - 1);
                }

                tries++;
            }

            if (cdnClients.Count == 0)
            {
                Console.WriteLine("\nUnable to find any content servers for depot {0} - {1}", depot.id, depot.contentName);
            }

            return cdnClients;
        }
        private static BlockingCollection<CDNClient> CollectCDNClientsForDepot(DepotDownloadInfo depot)
        {
            Console.WriteLine("Finding content servers...");

            var cdnClients = new BlockingCollection<CDNClient>();
            CDNClient initialClient = new CDNClient( steam3.steamClient, steam3.AppTickets[depot.id] );
            List<CDNClient.Server> cdnServers = null;

            while(true)
            {
                try
                {
                    cdnServers = initialClient.FetchServerList( cellId: (uint)Config.CellID );
                    if (cdnServers != null) break;
                }
                catch (WebException)
                {
                    Console.WriteLine("\nFailed to retrieve content server list.");
                    Thread.Sleep(500);
                }
            }

            if (cdnServers == null)
            {
                Console.WriteLine("\nUnable to query any content servers for depot {0} - {1}", depot.id, depot.contentName);
                return cdnClients;
            }

            var weightedCdnServers = cdnServers.Select(x =>
            {
                int penalty = 0;
                ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(x.Host, out penalty);

                return Tuple.Create(x, penalty);
            }).OrderBy(x => x.Item2).ThenBy(x => x.Item1.WeightedLoad);

            // Grab up to the first eight server in the allegedly best-to-worst order from Steam
            Parallel.ForEach(weightedCdnServers, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (serverTuple, parallelLoop) =>
            {
                var server = serverTuple.Item1;
                CDNClient c = new CDNClient( steam3.steamClient, steam3.AppTickets[ depot.id ] );

                try
                {
                    for (int i = 0; i < server.NumEntries; i++)
                    {
                        c.Connect(server);

                        string cdnAuthToken = null;
                        if (server.Type == "CDN")
                        {
                            steam3.RequestCDNAuthToken(depot.id, server.Host);
                            cdnAuthToken = steam3.CDNAuthTokens[Tuple.Create(depot.id, server.Host)].Token;
                        }

                        c.AuthenticateDepot(depot.id, depot.depotKey, cdnAuthToken);
                        cdnClients.Add(c);
                    }

                    if (cdnClients.Count >= Config.MaxServers) parallelLoop.Stop();
                }
                catch (Exception ex)
                {
                    int penalty = 0;
                    ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(server.Host, out penalty);
                    ConfigStore.TheConfig.ContentServerPenalty[server.Host] = penalty + 1;

                    Console.WriteLine("\nFailed to connect to content server {0}: {1}", server, ex.Message);
                }
            });

            if (cdnClients.Count == 0)
            {
                Console.WriteLine("\nUnable to find any content servers for depot {0} - {1}", depot.id, depot.contentName);
            }

            Config.MaxDownloads = Math.Min(Config.MaxDownloads, cdnClients.Count);

            return cdnClients;
        }
        public static void SetCDNClient(CDNClient cdnClient)
        {
            CDNClient = cdnClient;

            ReloadFileList();
        }