private static async Task <LinkedList <HttpFileInfo> > FindUpdatesInfo(String applicationDirectory, ManualResetEvent cancelEvent, GameSettingsControl gameSettings) { Downloader downloader = new Downloader(cancelEvent); String[] urls = gameSettings.DownloadMirrors; LinkedList <HttpFileInfo> list = new LinkedList <HttpFileInfo>(); Dictionary <String, LinkedListNode <HttpFileInfo> > dic = new Dictionary <String, LinkedListNode <HttpFileInfo> >(urls.Length); foreach (String url in urls) { try { HttpFileInfo fileInfo = await downloader.GetRemoteFileInfo(url); if (fileInfo == null) { continue; } Int32 separatorIndex = url.LastIndexOf('/'); String remoteFileName = url.Substring(separatorIndex + 1); fileInfo.TargetName = remoteFileName; fileInfo.TargetPath = Path.Combine(applicationDirectory, remoteFileName); LinkedListNode <HttpFileInfo> node; if (!dic.TryGetValue(fileInfo.TargetPath, out node) && File.Exists(fileInfo.TargetPath) && File.GetLastWriteTime(fileInfo.TargetPath) >= fileInfo.LastModified) { continue; } if (node != null) { if (node.Value.LastModified >= fileInfo.LastModified) { continue; } LinkedListNode <HttpFileInfo> newNode = list.AddBefore(node, fileInfo); list.Remove(node); dic[fileInfo.TargetPath] = newNode; } else { LinkedListNode <HttpFileInfo> newNode = list.AddLast(fileInfo); dic.Add(fileInfo.TargetPath, newNode); } } catch { // Do nothing } } return(list);; }
public async Task <HttpFileInfo> GetRemoteFileInfo(string url) { HttpFileInfo result = new HttpFileInfo(); if (_cancelEvent.WaitOne(0)) { return(result); } WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; using (WebResponse resp = await request.GetResponseAsync()) { if (_cancelEvent.WaitOne(0)) { return(result); } result.ReadFromResponse(url, resp); return(result); } }