Await steam jobs instead of WaitUntilCallback

pull/554/head
Pavel Djundik 1 year ago
parent e7d637ace6
commit 9f64bd0439

@ -106,7 +106,7 @@ namespace DepotDownloader
return false; return false;
} }
static bool AccountHasAccess(uint depotId) static async Task<bool> AccountHasAccess(uint depotId)
{ {
if (steam3 == null || steam3.steamUser.SteamID == null || (steam3.Licenses == null && steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser)) if (steam3 == null || steam3.steamUser.SteamID == null || (steam3.Licenses == null && steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser))
return false; return false;
@ -121,7 +121,7 @@ namespace DepotDownloader
licenseQuery = steam3.Licenses.Select(x => x.PackageID).Distinct(); licenseQuery = steam3.Licenses.Select(x => x.PackageID).Distinct();
} }
steam3.RequestPackageInfo(licenseQuery); await steam3.RequestPackageInfo(licenseQuery);
foreach (var license in licenseQuery) foreach (var license in licenseQuery)
{ {
@ -184,7 +184,7 @@ namespace DepotDownloader
return uint.Parse(buildid.Value); return uint.Parse(buildid.Value);
} }
static ulong GetSteam3DepotManifest(uint depotId, uint appId, string branch) static async Task<ulong> GetSteam3DepotManifest(uint depotId, uint appId, string branch)
{ {
var depots = GetSteam3AppSection(appId, EAppInfoSection.Depots); var depots = GetSteam3AppSection(appId, EAppInfoSection.Depots);
var depotChild = depots[depotId.ToString()]; var depotChild = depots[depotId.ToString()];
@ -206,9 +206,9 @@ namespace DepotDownloader
return INVALID_MANIFEST_ID; 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"]; var manifests = depotChild["manifests"];
@ -236,7 +236,7 @@ namespace DepotDownloader
if (encrypted_gid != KeyValue.Invalid) if (encrypted_gid != KeyValue.Invalid)
{ {
// Submit the password to Steam now to get encryption keys // 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)) if (!steam3.AppBetaPasswords.TryGetValue(branch, out var appBetaPassword))
{ {
@ -326,7 +326,7 @@ namespace DepotDownloader
public static async Task DownloadPubfileAsync(uint appId, ulong publishedFileId) 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)) if (!string.IsNullOrEmpty(details?.file_url))
{ {
@ -348,7 +348,7 @@ namespace DepotDownloader
if (steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser) if (steam3.steamUser.SteamID.AccountType != EAccountType.AnonUser)
{ {
details = steam3.GetUGCDetails(ugcId); details = await steam3.GetUGCDetails(ugcId);
} }
else else
{ {
@ -410,16 +410,16 @@ namespace DepotDownloader
Directory.CreateDirectory(Path.Combine(configPath, CONFIG_DIR)); Directory.CreateDirectory(Path.Combine(configPath, CONFIG_DIR));
DepotConfigStore.LoadFromFile(Path.Combine(configPath, CONFIG_DIR, "depot.config")); 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); Console.WriteLine("Obtained FreeOnDemand license for app {0}", appId);
// Fetch app info again in case we didn't get it fully without a license. // 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 else
{ {
@ -523,7 +523,7 @@ namespace DepotDownloader
foreach (var (depotId, manifestId) in depotManifestIds) foreach (var (depotId, manifestId) in depotManifestIds)
{ {
var info = GetDepotInfo(depotId, appId, manifestId, branch); var info = await GetDepotInfo(depotId, appId, manifestId, branch);
if (info != null) if (info != null)
{ {
infos.Add(info); infos.Add(info);
@ -541,12 +541,14 @@ namespace DepotDownloader
} }
} }
static DepotDownloadInfo GetDepotInfo(uint depotId, uint appId, ulong manifestId, string branch) static async Task<DepotDownloadInfo> GetDepotInfo(uint depotId, uint appId, ulong manifestId, string branch)
{ {
if (steam3 != null && appId != INVALID_APP_ID) 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); Console.WriteLine("Depot {0} is not available from this account.", depotId);
@ -555,12 +557,12 @@ namespace DepotDownloader
if (manifestId == INVALID_MANIFEST_ID) 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)) 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); Console.WriteLine("Warning: Depot {0} does not have branch named \"{1}\". Trying {2} branch.", depotId, branch, DEFAULT_BRANCH);
branch = DEFAULT_BRANCH; branch = DEFAULT_BRANCH;
manifestId = GetSteam3DepotManifest(depotId, appId, branch); manifestId = await GetSteam3DepotManifest(depotId, appId, branch);
} }
if (manifestId == INVALID_MANIFEST_ID) 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)) if (!steam3.DepotKeys.TryGetValue(depotId, out var depotKey))
{ {
Console.WriteLine("No valid depot key for {0}, unable to download.", depotId); Console.WriteLine("No valid depot key for {0}, unable to download.", depotId);

@ -127,15 +127,13 @@ namespace DepotDownloader
return IsLoggedOn; 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) if ((AppInfo.ContainsKey(appId) && !bForce) || bAborted)
return; return;
var completed = false; var appTokens = await steamApps.PICSGetAccessTokens([appId], []);
Action<SteamApps.PICSTokensCallback> cbMethodTokens = appTokens =>
{
completed = true;
if (appTokens.AppTokensDenied.Contains(appId)) if (appTokens.AppTokensDenied.Contains(appId))
{ {
Console.WriteLine("Insufficient privileges to get access token for app {0}", appId); Console.WriteLine("Insufficient privileges to get access token for app {0}", appId);
@ -145,18 +143,18 @@ namespace DepotDownloader
{ {
this.AppTokens[token_dict.Key] = token_dict.Value; this.AppTokens[token_dict.Key] = token_dict.Value;
} }
};
WaitUntilCallback(() => var request = new SteamApps.PICSRequest(appId);
{
callbacks.Subscribe(steamApps.PICSGetAccessTokens(new List<uint> { appId }, new List<uint>()), cbMethodTokens);
}, () => { return completed; });
completed = false; if (AppTokens.TryGetValue(appId, out var token))
Action<SteamApps.PICSProductInfoCallback> cbMethod = appInfo =>
{ {
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) foreach (var app_value in appInfo.Apps)
{ {
var app = app_value.Value; var app = app_value.Value;
@ -169,45 +167,17 @@ namespace DepotDownloader
{ {
AppInfo[app] = null; 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<SteamApps.PICSRequest> { request }, new List<SteamApps.PICSRequest>()), cbMethod);
}, () => { return completed; });
} }
public void RequestPackageInfo(IEnumerable<uint> packageIds) public async Task RequestPackageInfo(IEnumerable<uint> packageIds)
{ {
var packages = packageIds.ToList(); var packages = packageIds.ToList();
packages.RemoveAll(pid => PackageInfo.ContainsKey(pid)); packages.RemoveAll(PackageInfo.ContainsKey);
if (packages.Count == 0 || bAborted) if (packages.Count == 0 || bAborted)
return; return;
var completed = false;
Action<SteamApps.PICSProductInfoCallback> 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<SteamApps.PICSRequest>(); var packageRequests = new List<SteamApps.PICSRequest>();
foreach (var package in packages) foreach (var package in packages)
@ -222,40 +192,37 @@ namespace DepotDownloader
packageRequests.Add(request); packageRequests.Add(request);
} }
WaitUntilCallback(() => var packageInfoMultiple = await steamApps.PICSGetProductInfo([], packageRequests);
foreach (var packageInfo in packageInfoMultiple.Results)
{
foreach (var package_value in packageInfo.Packages)
{ {
callbacks.Subscribe(steamApps.PICSGetProductInfo(new List<SteamApps.PICSRequest>(), packageRequests), cbMethod); var package = package_value.Value;
}, () => { return completed; }); PackageInfo[package.ID] = package;
} }
public bool RequestFreeAppLicense(uint appId) foreach (var package in packageInfo.UnknownPackages)
{
var success = false;
var completed = false;
Action<SteamApps.FreeLicenseCallback> cbMethod = resultInfo =>
{ {
completed = true; PackageInfo[package] = null;
success = resultInfo.GrantedApps.Contains(appId); }
}; }
}
WaitUntilCallback(() => public async Task<bool> RequestFreeAppLicense(uint appId)
{ {
callbacks.Subscribe(steamApps.RequestFreeLicense(appId), cbMethod); var resultInfo = await steamApps.RequestFreeLicense(appId);
}, () => { 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) if (DepotKeys.ContainsKey(depotId) || bAborted)
return; return;
var completed = false; var depotKey = await steamApps.GetDepotDecryptionKey(depotId, appid);
Action<SteamApps.DepotKeyCallback> 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) if (depotKey.Result != EResult.OK)
@ -265,12 +232,6 @@ namespace DepotDownloader
} }
DepotKeys[depotKey.DepotID] = depotKey.DepotKey; DepotKeys[depotKey.DepotID] = depotKey.DepotKey;
};
WaitUntilCallback(() =>
{
callbacks.Subscribe(steamApps.GetDepotDecryptionKey(depotId, appid), cbMethod);
}, () => { return completed; });
} }
@ -312,12 +273,9 @@ namespace DepotDownloader
completion.TrySetResult(cdnAuth); completion.TrySetResult(cdnAuth);
} }
public void CheckAppBetaPassword(uint appid, string password) public async Task CheckAppBetaPassword(uint appid, string password)
{
var completed = false;
Action<SteamApps.CheckAppBetaPasswordCallback> 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);
@ -325,73 +283,39 @@ namespace DepotDownloader
{ {
AppBetaPasswords[entry.Key] = entry.Value; AppBetaPasswords[entry.Key] = entry.Value;
} }
};
WaitUntilCallback(() =>
{
callbacks.Subscribe(steamApps.CheckAppBetaPassword(appid, password), cbMethod);
}, () => { return completed; });
} }
public PublishedFileDetails GetPublishedFileDetails(uint appId, PublishedFileID pubFile) public async Task<PublishedFileDetails> GetPublishedFileDetails(uint appId, PublishedFileID pubFile)
{ {
var pubFileRequest = new CPublishedFile_GetDetails_Request { appid = appId }; var pubFileRequest = new CPublishedFile_GetDetails_Request { appid = appId };
pubFileRequest.publishedfileids.Add(pubFile); pubFileRequest.publishedfileids.Add(pubFile);
var completed = false; var callback = await steamPublishedFile.SendMessage(api => api.GetDetails(pubFileRequest));
PublishedFileDetails details = null;
Action<SteamUnifiedMessages.ServiceMethodResponse> cbMethod = callback =>
{
completed = true;
if (callback.Result == EResult.OK) if (callback.Result == EResult.OK)
{ {
var response = callback.GetDeserializedResponse<CPublishedFile_GetDetails_Response>(); var response = callback.GetDeserializedResponse<CPublishedFile_GetDetails_Response>();
details = response.publishedfiledetails.FirstOrDefault(); return response.publishedfiledetails.FirstOrDefault();
} }
else
{
throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving file details for pubfile {pubFile}.");
}
};
WaitUntilCallback(() => throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving file details for pubfile {pubFile}.");
{
callbacks.Subscribe(steamPublishedFile.SendMessage(api => api.GetDetails(pubFileRequest)), cbMethod);
}, () => { return completed; });
return details;
} }
public SteamCloud.UGCDetailsCallback GetUGCDetails(UGCHandle ugcHandle) public async Task<SteamCloud.UGCDetailsCallback> GetUGCDetails(UGCHandle ugcHandle)
{ {
var completed = false; var callback = await steamCloud.RequestUGCDetails(ugcHandle);
SteamCloud.UGCDetailsCallback details = null;
Action<SteamCloud.UGCDetailsCallback> cbMethod = callback =>
{
completed = true;
if (callback.Result == EResult.OK) if (callback.Result == EResult.OK)
{ {
details = callback; return callback;
} }
else if (callback.Result == EResult.FileNotFound) else if (callback.Result == EResult.FileNotFound)
{ {
details = null; return null;
} }
else
{
throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving UGC details for {ugcHandle}.");
}
};
WaitUntilCallback(() =>
{
callbacks.Subscribe(steamCloud.RequestUGCDetails(ugcHandle), cbMethod);
}, () => { return completed; });
return details; throw new Exception($"EResult {(int)callback.Result} ({callback.Result}) while retrieving UGC details for {ugcHandle}.");
} }
private void ResetConnectionFlags() private void ResetConnectionFlags()

Loading…
Cancel
Save