Exemple #1
0
        /// <summary>
        /// Downloads the depot manifest specified by the given manifest ID, and optionally decrypts the manifest's filenames if the depot decryption key has been provided.
        /// </summary>
        /// <param name="depotId">The id of the depot being accessed.</param>
        /// <param name="manifestId">The unique identifier of the manifest to be downloaded.</param>
        /// <param name="manifestRequestCode">The manifest request code for the manifest that is being downloaded.</param>
        /// <param name="server">The content server to connect to.</param>
        /// <param name="depotKey">
        /// The depot decryption key for the depot that will be downloaded.
        /// This is used for decrypting filenames (if needed) in depot manifests, and processing depot chunks.
        /// </param>
        /// <param name="proxyServer">Optional content server marked as UseAsProxy which transforms the request.</param>
        /// <returns>A <see cref="DepotManifest"/> instance that contains information about the files present within a depot.</returns>
        /// <exception cref="System.ArgumentNullException"><see ref="server"/> was null.</exception>
        /// <exception cref="HttpRequestException">An network error occurred when performing the request.</exception>
        /// <exception cref="SteamKitWebRequestException">A network error occurred when performing the request.</exception>
        public async Task <DepotManifest> DownloadManifestAsync(uint depotId, ulong manifestId, ulong manifestRequestCode, Server server, byte[]?depotKey = null, Server?proxyServer = null)
        {
            if (server == null)
            {
                throw new ArgumentNullException(nameof(server));
            }

            const uint MANIFEST_VERSION = 5;
            string     url;

            if (manifestRequestCode > 0)
            {
                url = $"depot/{depotId}/manifest/{manifestId}/{MANIFEST_VERSION}/{manifestRequestCode}";
            }
            else
            {
                url = $"depot/{depotId}/manifest/{manifestId}/{MANIFEST_VERSION}";
            }

            var manifestData = await DoRawCommandAsync(server, url, proxyServer).ConfigureAwait(false);

            manifestData = ZipUtil.Decompress(manifestData);

            var depotManifest = new DepotManifest(manifestData);

            if (depotKey != null)
            {
                // if we have the depot key, decrypt the manifest filenames
                depotManifest.DecryptFilenames(depotKey);
            }

            return(depotManifest);
        }
        private static void DownloadSteam3(List <DepotDownloadInfo3> 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();
                }
            }
        }
Exemple #3
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();
            }
        }