Beispiel #1
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.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);
                }

                // 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();
            }
        }