public void CancelInstall() { if (download == null) return; download.Cancel(); download = null; }
public void Install() { if (Status != MapStatus.DownloadAvailable || !Game.Settings.Game.AllowDownloading) return; Status = MapStatus.Downloading; var baseMapPath = new[] { Platform.SupportDir, "maps", Game.modData.Manifest.Mod.Id }.Aggregate(Path.Combine); // Create the map directory if it doesn't exist if (!Directory.Exists(baseMapPath)) Directory.CreateDirectory(baseMapPath); new Thread(() => { // Request the filename from the server // Run in a worker thread to avoid network delays var mapUrl = Game.Settings.Game.MapRepository + Uid; try { var request = WebRequest.Create(mapUrl); request.Method = "HEAD"; var res = request.GetResponse(); // Map not found if (res.Headers["Content-Disposition"] == null) { Status = MapStatus.DownloadError; return; } var mapPath = Path.Combine(baseMapPath, res.Headers["Content-Disposition"].Replace("attachment; filename = ", "")); Action<DownloadProgressChangedEventArgs> onDownloadProgress = i => { DownloadBytes = i.BytesReceived; DownloadPercentage = i.ProgressPercentage; }; Action<AsyncCompletedEventArgs, bool> onDownloadComplete = (i, cancelled) => { download = null; if (cancelled || i.Error != null) { Log.Write("debug", "Remote map download failed with error: {0}", i.Error != null ? i.Error.Message : "cancelled"); Log.Write("debug", "URL was: {0}", mapUrl); Status = MapStatus.DownloadError; return; } Log.Write("debug", "Downloaded map to '{0}'", mapPath); Game.RunAfterTick(() => { UpdateFromMap(new Map(mapPath), MapClassification.User); CacheRules(); }); }; download = new Download(mapUrl, mapPath, onDownloadProgress, onDownloadComplete); } catch (Exception e) { Console.WriteLine(e.Message); Status = MapStatus.DownloadError; } }).Start(); }
public void Install(Action onSuccess) { if (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; var modData = Game.ModData; new Thread(() => { // Request the filename from the server // Run in a worker thread to avoid network delays var mapUrl = Game.Settings.Game.MapRepository + 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 = modData.ModFiles.OpenPackage(mapFilename, mapInstallPackage); 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(); }
public void Install(Action onSuccess) { if (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; var modData = Game.ModData; new Thread(() => { // Request the filename from the server // Run in a worker thread to avoid network delays var mapUrl = Game.Settings.Game.MapRepository + 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 = modData.ModFiles.OpenPackage(mapFilename, mapInstallPackage); 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(); }
public void Install() { if (Status != MapStatus.DownloadAvailable || !Game.Settings.Game.AllowDownloading) { return; } Status = MapStatus.Downloading; var baseMapPath = new[] { Platform.SupportDir, "maps", Game.modData.Manifest.Mod.Id }.Aggregate(Path.Combine); // Create the map directory if it doesn't exist if (!Directory.Exists(baseMapPath)) { Directory.CreateDirectory(baseMapPath); } new Thread(() => { // Request the filename from the server // Run in a worker thread to avoid network delays var mapUrl = Game.Settings.Game.MapRepository + Uid; try { var request = WebRequest.Create(mapUrl); request.Method = "HEAD"; var res = request.GetResponse(); // Map not found if (res.Headers["Content-Disposition"] == null) { Status = MapStatus.DownloadError; return; } var mapPath = Path.Combine(baseMapPath, res.Headers["Content-Disposition"].Replace("attachment; filename = ", "")); Action <DownloadProgressChangedEventArgs> onDownloadProgress = i => { DownloadBytes = i.BytesReceived; DownloadPercentage = i.ProgressPercentage; }; Action <AsyncCompletedEventArgs, bool> onDownloadComplete = (i, cancelled) => { download = null; if (cancelled || i.Error != null) { Log.Write("debug", "Remote map download failed with error: {0}", i.Error != null ? i.Error.Message : "cancelled"); Log.Write("debug", "URL was: {0}", mapUrl); Status = MapStatus.DownloadError; return; } Log.Write("debug", "Downloaded map to '{0}'", mapPath); Game.RunAfterTick(() => { UpdateFromMap(new Map(mapPath), MapClassification.User); CacheRules(); }); }; download = new Download(mapUrl, mapPath, onDownloadProgress, onDownloadComplete); } catch (Exception e) { Console.WriteLine(e.Message); Status = MapStatus.DownloadError; } }).Start(); }
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); } foreach (var map in maps) { if (map.Value.Status != MapStatus.DownloadAvailable) { map.Value.UpdateRemoteSearch(MapStatus.Unavailable, null); } } } 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); }