diff --git a/DepotDownloader/ContentDownloader.cs b/DepotDownloader/ContentDownloader.cs index 9042c39d..30c0c549 100644 --- a/DepotDownloader/ContentDownloader.cs +++ b/DepotDownloader/ContentDownloader.cs @@ -106,7 +106,7 @@ namespace DepotDownloader return false; } - static bool AccountHasAccess(uint depotId) + static async Task AccountHasAccess(uint depotId) { if (steam3 == null || steam3.steamUser.SteamID == null || (steam3.Licenses == null && steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser)) return false; @@ -121,7 +121,7 @@ namespace DepotDownloader licenseQuery = steam3.Licenses.Select(x => x.PackageID).Distinct(); } - steam3.RequestPackageInfo(licenseQuery); + await steam3.RequestPackageInfo(licenseQuery); foreach (var license in licenseQuery) { @@ -184,7 +184,7 @@ namespace DepotDownloader return uint.Parse(buildid.Value); } - static ulong GetSteam3DepotManifest(uint depotId, uint appId, string branch) + static async Task GetSteam3DepotManifest(uint depotId, uint appId, string branch) { var depots = GetSteam3AppSection(appId, EAppInfoSection.Depots); var depotChild = depots[depotId.ToString()]; @@ -206,9 +206,9 @@ namespace DepotDownloader return INVALID_MANIFEST_ID; } - steam3.RequestAppInfo(otherAppId); + await steam3.RequestAppInfo(otherAppId); - return GetSteam3DepotManifest(depotId, otherAppId, branch); + return await GetSteam3DepotManifest(depotId, otherAppId, branch); } var manifests = depotChild["manifests"]; @@ -236,7 +236,7 @@ namespace DepotDownloader if (encrypted_gid != KeyValue.Invalid) { // Submit the password to Steam now to get encryption keys - steam3.CheckAppBetaPassword(appId, Config.BetaPassword); + await steam3.CheckAppBetaPassword(appId, Config.BetaPassword); if (!steam3.AppBetaPasswords.TryGetValue(branch, out var appBetaPassword)) { @@ -326,7 +326,7 @@ namespace DepotDownloader public static async Task DownloadPubfileAsync(uint appId, ulong publishedFileId) { - var details = steam3.GetPublishedFileDetails(appId, publishedFileId); + var details = await steam3.GetPublishedFileDetails(appId, publishedFileId); if (!string.IsNullOrEmpty(details?.file_url)) { @@ -348,7 +348,7 @@ namespace DepotDownloader if (steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser) { - details = steam3.GetUGCDetails(ugcId); + details = await steam3.GetUGCDetails(ugcId); } else { @@ -410,16 +410,16 @@ namespace DepotDownloader Directory.CreateDirectory(Path.Combine(configPath, CONFIG_DIR)); DepotConfigStore.LoadFromFile(Path.Combine(configPath, CONFIG_DIR, "depot.config")); - steam3?.RequestAppInfo(appId); + await steam3?.RequestAppInfo(appId); - if (!AccountHasAccess(appId)) + if (!await AccountHasAccess(appId)) { - if (steam3.RequestFreeAppLicense(appId)) + if (await steam3.RequestFreeAppLicense(appId)) { Console.WriteLine("Obtained FreeOnDemand license for app {0}", appId); // Fetch app info again in case we didn't get it fully without a license. - steam3.RequestAppInfo(appId, true); + await steam3.RequestAppInfo(appId, true); } else { @@ -523,7 +523,7 @@ namespace DepotDownloader foreach (var (depotId, manifestId) in depotManifestIds) { - var info = GetDepotInfo(depotId, appId, manifestId, branch); + var info = await GetDepotInfo(depotId, appId, manifestId, branch); if (info != null) { infos.Add(info); @@ -541,12 +541,14 @@ namespace DepotDownloader } } - static DepotDownloadInfo GetDepotInfo(uint depotId, uint appId, ulong manifestId, string branch) + static async Task GetDepotInfo(uint depotId, uint appId, ulong manifestId, string branch) { if (steam3 != null && appId != INVALID_APP_ID) - steam3.RequestAppInfo(appId); + { + await steam3.RequestAppInfo(appId); + } - if (!AccountHasAccess(depotId)) + if (!await AccountHasAccess(depotId)) { Console.WriteLine("Depot {0} is not available from this account.", depotId); @@ -555,12 +557,12 @@ namespace DepotDownloader if (manifestId == INVALID_MANIFEST_ID) { - manifestId = GetSteam3DepotManifest(depotId, appId, branch); + manifestId = await GetSteam3DepotManifest(depotId, appId, branch); if (manifestId == INVALID_MANIFEST_ID && !string.Equals(branch, DEFAULT_BRANCH, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Warning: Depot {0} does not have branch named \"{1}\". Trying {2} branch.", depotId, branch, DEFAULT_BRANCH); branch = DEFAULT_BRANCH; - manifestId = GetSteam3DepotManifest(depotId, appId, branch); + manifestId = await GetSteam3DepotManifest(depotId, appId, branch); } if (manifestId == INVALID_MANIFEST_ID) @@ -570,7 +572,7 @@ namespace DepotDownloader } } - steam3.RequestDepotKey(depotId, appId); + await steam3.RequestDepotKey(depotId, appId); if (!steam3.DepotKeys.TryGetValue(depotId, out var depotKey)) { Console.WriteLine("No valid depot key for {0}, unable to download.", depotId); diff --git a/DepotDownloader/Steam3Session.cs b/DepotDownloader/Steam3Session.cs index 44b0ac69..83f0978d 100644 --- a/DepotDownloader/Steam3Session.cs +++ b/DepotDownloader/Steam3Session.cs @@ -127,36 +127,34 @@ namespace DepotDownloader return IsLoggedOn; } - public void RequestAppInfo(uint appId, bool bForce = false) + public async Task RequestAppInfo(uint appId, bool bForce = false) { if ((AppInfo.ContainsKey(appId) && !bForce) || bAborted) return; - var completed = false; - Action cbMethodTokens = appTokens => - { - completed = true; - if (appTokens.AppTokensDenied.Contains(appId)) - { - Console.WriteLine("Insufficient privileges to get access token for app {0}", appId); - } + var appTokens = await steamApps.PICSGetAccessTokens([appId], []); - foreach (var token_dict in appTokens.AppTokens) - { - this.AppTokens[token_dict.Key] = token_dict.Value; - } - }; + if (appTokens.AppTokensDenied.Contains(appId)) + { + Console.WriteLine("Insufficient privileges to get access token for app {0}", appId); + } - WaitUntilCallback(() => + foreach (var token_dict in appTokens.AppTokens) { - callbacks.Subscribe(steamApps.PICSGetAccessTokens(new List { appId }, new List()), cbMethodTokens); - }, () => { return completed; }); + this.AppTokens[token_dict.Key] = token_dict.Value; + } - completed = false; - Action cbMethod = appInfo => + var request = new SteamApps.PICSRequest(appId); + + if (AppTokens.TryGetValue(appId, out var token)) { - completed = !appInfo.ResponsePending; + request.AccessToken = token; + } + + var appInfoMultiple = await steamApps.PICSGetProductInfo([request], []); + foreach (var appInfo in appInfoMultiple.Results) + { foreach (var app_value in appInfo.Apps) { var app = app_value.Value; @@ -169,45 +167,17 @@ namespace DepotDownloader { AppInfo[app] = null; } - }; - - var request = new SteamApps.PICSRequest(appId); - if (AppTokens.TryGetValue(appId, out var token)) - { - request.AccessToken = token; } - - WaitUntilCallback(() => - { - callbacks.Subscribe(steamApps.PICSGetProductInfo(new List { request }, new List()), cbMethod); - }, () => { return completed; }); } - public void RequestPackageInfo(IEnumerable packageIds) + public async Task RequestPackageInfo(IEnumerable packageIds) { var packages = packageIds.ToList(); - packages.RemoveAll(pid => PackageInfo.ContainsKey(pid)); + packages.RemoveAll(PackageInfo.ContainsKey); if (packages.Count == 0 || bAborted) return; - var completed = false; - Action cbMethod = packageInfo => - { - completed = !packageInfo.ResponsePending; - - foreach (var package_value in packageInfo.Packages) - { - var package = package_value.Value; - PackageInfo[package.ID] = package; - } - - foreach (var package in packageInfo.UnknownPackages) - { - PackageInfo[package] = null; - } - }; - var packageRequests = new List(); foreach (var package in packages) @@ -222,55 +192,46 @@ namespace DepotDownloader packageRequests.Add(request); } - WaitUntilCallback(() => + var packageInfoMultiple = await steamApps.PICSGetProductInfo([], packageRequests); + + foreach (var packageInfo in packageInfoMultiple.Results) { - callbacks.Subscribe(steamApps.PICSGetProductInfo(new List(), packageRequests), cbMethod); - }, () => { return completed; }); + foreach (var package_value in packageInfo.Packages) + { + var package = package_value.Value; + PackageInfo[package.ID] = package; + } + + foreach (var package in packageInfo.UnknownPackages) + { + PackageInfo[package] = null; + } + } } - public bool RequestFreeAppLicense(uint appId) + public async Task RequestFreeAppLicense(uint appId) { - var success = false; - var completed = false; - Action cbMethod = resultInfo => - { - completed = true; - success = resultInfo.GrantedApps.Contains(appId); - }; + var resultInfo = await steamApps.RequestFreeLicense(appId); - WaitUntilCallback(() => - { - callbacks.Subscribe(steamApps.RequestFreeLicense(appId), cbMethod); - }, () => { return completed; }); - - return success; + return resultInfo.GrantedApps.Contains(appId); } - public void RequestDepotKey(uint depotId, uint appid = 0) + public async Task RequestDepotKey(uint depotId, uint appid = 0) { if (DepotKeys.ContainsKey(depotId) || bAborted) return; - var completed = false; + var depotKey = await steamApps.GetDepotDecryptionKey(depotId, appid); - Action cbMethod = depotKey => - { - completed = true; - Console.WriteLine("Got depot key for {0} result: {1}", depotKey.DepotID, depotKey.Result); + Console.WriteLine("Got depot key for {0} result: {1}", depotKey.DepotID, depotKey.Result); - if (depotKey.Result != EResult.OK) - { - Abort(); - return; - } - - DepotKeys[depotKey.DepotID] = depotKey.DepotKey; - }; - - WaitUntilCallback(() => + if (depotKey.Result != EResult.OK) { - callbacks.Subscribe(steamApps.GetDepotDecryptionKey(depotId, appid), cbMethod); - }, () => { return completed; }); + Abort(); + return; + } + + DepotKeys[depotKey.DepotID] = depotKey.DepotKey; } @@ -312,86 +273,49 @@ namespace DepotDownloader completion.TrySetResult(cdnAuth); } - public void CheckAppBetaPassword(uint appid, string password) + public async Task CheckAppBetaPassword(uint appid, string password) { - var completed = false; - Action cbMethod = appPassword => - { - completed = true; + var appPassword = await steamApps.CheckAppBetaPassword(appid, password); - Console.WriteLine("Retrieved {0} beta keys with result: {1}", appPassword.BetaPasswords.Count, appPassword.Result); + Console.WriteLine("Retrieved {0} beta keys with result: {1}", appPassword.BetaPasswords.Count, appPassword.Result); - foreach (var entry in appPassword.BetaPasswords) - { - AppBetaPasswords[entry.Key] = entry.Value; - } - }; - - WaitUntilCallback(() => + foreach (var entry in appPassword.BetaPasswords) { - callbacks.Subscribe(steamApps.CheckAppBetaPassword(appid, password), cbMethod); - }, () => { return completed; }); + AppBetaPasswords[entry.Key] = entry.Value; + } } - public PublishedFileDetails GetPublishedFileDetails(uint appId, PublishedFileID pubFile) + public async Task GetPublishedFileDetails(uint appId, PublishedFileID pubFile) { var pubFileRequest = new CPublishedFile_GetDetails_Request { appid = appId }; pubFileRequest.publishedfileids.Add(pubFile); - var completed = false; - PublishedFileDetails details = null; - - Action cbMethod = callback => - { - completed = true; - if (callback.Result == EResult.OK) - { - var response = callback.GetDeserializedResponse(); - details = response.publishedfiledetails.FirstOrDefault(); - } - else - { - throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving file details for pubfile {pubFile}."); - } - }; + var callback = await steamPublishedFile.SendMessage(api => api.GetDetails(pubFileRequest)); - WaitUntilCallback(() => + if (callback.Result == EResult.OK) { - callbacks.Subscribe(steamPublishedFile.SendMessage(api => api.GetDetails(pubFileRequest)), cbMethod); - }, () => { return completed; }); + var response = callback.GetDeserializedResponse(); + return response.publishedfiledetails.FirstOrDefault(); + } - return details; + throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving file details for pubfile {pubFile}."); } - public SteamCloud.UGCDetailsCallback GetUGCDetails(UGCHandle ugcHandle) + public async Task GetUGCDetails(UGCHandle ugcHandle) { - var completed = false; - SteamCloud.UGCDetailsCallback details = null; + var callback = await steamCloud.RequestUGCDetails(ugcHandle); - Action cbMethod = callback => + if (callback.Result == EResult.OK) { - completed = true; - if (callback.Result == EResult.OK) - { - details = callback; - } - else if (callback.Result == EResult.FileNotFound) - { - details = null; - } - else - { - throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving UGC details for {ugcHandle}."); - } - }; - - WaitUntilCallback(() => + return callback; + } + else if (callback.Result == EResult.FileNotFound) { - callbacks.Subscribe(steamCloud.RequestUGCDetails(ugcHandle), cbMethod); - }, () => { return completed; }); + return null; + } - return details; + throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving UGC details for {ugcHandle}."); } private void ResetConnectionFlags()