private static void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { AvatarData ad = e.UserState as AvatarData; if (e.Error != null) { DebugLog.WriteLine("CDNCache", "Unable to download avatar {0}.\n{1}", ad.fullURI, e.Error.ToString()); ad.callback(new AvatarDownloadDetails() { Success = false, Exception = e.Error }); } else { ad.callback(new AvatarDownloadDetails() { Success = true, Filename = ad.avatarFile }); } lock (QueuedDownloads) { QueuedDownloads.Remove(ad.steamID); } ad.client.Dispose(); }
public static void DownloadAvatar(SteamID steamId, byte[] avatarHash, Action <AvatarDownloadDetails> completionHandler) { string hashStr = BitConverter.ToString(avatarHash).Replace("-", "").ToLower(); string hashPrefix = hashStr.Substring(0, 2); // if an existing request exists, return false lock (QueuedDownloads) { if (QueuedDownloads.ContainsKey(steamId)) { completionHandler(new AvatarDownloadDetails() { Success = false }); return; } } string localPath = Path.Combine(Application.StartupPath, "cache"); string localFile = Path.Combine(localPath, hashStr + ".jpg"); if (File.Exists(localFile)) { FileInfo fi = new FileInfo(localFile); if (fi.Length == 0) { DebugLog.WriteLine("CDNCache", "Avatar {0} was truncated, redownloading.", hashStr); File.Delete(localFile); } else { DebugLog.WriteLine("CDNCache", "Avatar {0} found in the cache.", hashStr); completionHandler(new AvatarDownloadDetails() { Success = true, Filename = localFile }); return; } } DebugLog.WriteLine("CDNCache", "Downloading avatar {0}", hashStr); string downloadUri = string.Format(AvatarRoot + AvatarSmall, hashPrefix, hashStr); AvatarData ad = new AvatarData { avatarFile = localFile, callback = completionHandler, fullURI = downloadUri, steamID = steamId, client = SpawnWebClient() }; lock (QueuedDownloads) { QueuedDownloads.Add(steamId, ad); } ad.client.DownloadFileAsync(new Uri(downloadUri), localFile, ad); }
public static void DownloadAvatar(SteamID steamId, byte[] avatarHash, Action<AvatarDownloadDetails> completionHandler) { string hashStr = BitConverter.ToString(avatarHash).Replace("-", "").ToLower(); string hashPrefix = hashStr.Substring(0, 2); // if an existing request exists, return false lock (QueuedDownloads) { if (QueuedDownloads.ContainsKey(steamId)) { completionHandler(new AvatarDownloadDetails() { Success = false }); return; } } string localPath = Path.Combine(Application.StartupPath, "cache"); string localFile = Path.Combine(localPath, hashStr + ".jpg"); if (File.Exists(localFile)) { FileInfo fi = new FileInfo(localFile); if (fi.Length == 0) { DebugLog.WriteLine("CDNCache", "Avatar {0} was truncated, redownloading.", hashStr); File.Delete(localFile); } else { DebugLog.WriteLine("CDNCache", "Avatar {0} found in the cache.", hashStr); completionHandler(new AvatarDownloadDetails() { Success = true, Filename = localFile }); return; } } DebugLog.WriteLine("CDNCache", "Downloading avatar {0}", hashStr); string downloadUri = string.Format(AvatarRoot + AvatarSmall, hashPrefix, hashStr); AvatarData ad = new AvatarData { avatarFile = localFile, callback = completionHandler, fullURI = downloadUri, steamID = steamId, client = SpawnWebClient() }; lock (QueuedDownloads) { QueuedDownloads.Add(steamId, ad); } ad.client.DownloadFileAsync(new Uri(downloadUri), localFile, ad); }