public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable <string> uids, Action <MapPreview> mapDetailsReceived = null, Action queryFailed = null) { var maps = uids.Distinct() .Select(uid => previews[uid]) .Where(p => p.Status == MapStatus.Unavailable) .ToDictionary(p => p.Uid, p => p); if (!maps.Any()) { return; } foreach (var p in maps.Values) { p.UpdateRemoteSearch(MapStatus.Searching, null); } var url = repositoryUrl + "hash/" + string.Join(",", maps.Keys) + "/yaml"; Action <DownloadDataCompletedEventArgs> onInfoComplete = i => { if (i.Error != null) { Log.Write("debug", "Remote map query failed with error: {0}", Download.FormatErrorMessage(i.Error)); Log.Write("debug", "URL was: {0}", url); foreach (var p in maps.Values) { p.UpdateRemoteSearch(MapStatus.Unavailable, null); } if (queryFailed != null) { queryFailed(); } return; } var data = Encoding.UTF8.GetString(i.Result); try { var yaml = MiniYaml.FromString(data); foreach (var kv in yaml) { maps[kv.Key].UpdateRemoteSearch(MapStatus.DownloadAvailable, kv.Value, mapDetailsReceived); } } catch (Exception e) { Log.Write("debug", "Can't parse remote map search data:\n{0}", data); Log.Write("debug", "Exception: {0}", e); if (queryFailed != null) { queryFailed(); } } }; new Download(url, _ => { }, onInfoComplete); }
public void Install(string mapRepositoryUrl, Action onSuccess) { if ((Status != MapStatus.DownloadError && Status != MapStatus.DownloadAvailable) || !Game.Settings.Game.AllowDownloading) { return; } innerData.Status = MapStatus.Downloading; var installLocation = cache.MapLocations.FirstOrDefault(p => p.Value == MapClassification.User); if (installLocation.Key == null || !(installLocation.Key is IReadWritePackage)) { Log.Write("debug", "Map install directory not found"); innerData.Status = MapStatus.DownloadError; return; } var mapInstallPackage = installLocation.Key as IReadWritePackage; new Thread(() => { // Request the filename from the server // Run in a worker thread to avoid network delays var mapUrl = mapRepositoryUrl + Uid; var mapFilename = string.Empty; try { var request = WebRequest.Create(mapUrl); request.Method = "HEAD"; using (var res = request.GetResponse()) { // Map not found if (res.Headers["Content-Disposition"] == null) { innerData.Status = MapStatus.DownloadError; return; } mapFilename = res.Headers["Content-Disposition"].Replace("attachment; filename = ", ""); } Action <DownloadProgressChangedEventArgs> onDownloadProgress = i => { DownloadBytes = i.BytesReceived; DownloadPercentage = i.ProgressPercentage; }; Action <DownloadDataCompletedEventArgs> onDownloadComplete = i => { download = null; if (i.Error != null) { Log.Write("debug", "Remote map download failed with error: {0}", Download.FormatErrorMessage(i.Error)); Log.Write("debug", "URL was: {0}", mapUrl); innerData.Status = MapStatus.DownloadError; return; } mapInstallPackage.Update(mapFilename, i.Result); Log.Write("debug", "Downloaded map to '{0}'", mapFilename); Game.RunAfterTick(() => { var package = mapInstallPackage.OpenPackage(mapFilename, modData.ModFiles); if (package == null) { innerData.Status = MapStatus.DownloadError; } else { UpdateFromMap(package, mapInstallPackage, MapClassification.User, null, GridType); onSuccess(); } }); }; download = new Download(mapUrl, onDownloadProgress, onDownloadComplete); } catch (Exception e) { Console.WriteLine(e.Message); innerData.Status = MapStatus.DownloadError; } }).Start(); }