private async void UnityBackgroundDownloadStart(IntPtr www, string full_path) { BackgroundDownloadConfig config = new BackgroundDownloadConfig(); config.url = www.uri; config.filePath = full_path; var dl = new BackgroundDownloadEditor(www, config); try { _dwnloads.Remove(full_path); } catch (Exception) { } _dwnloads.Add(full_path, www); if (!full_path.Contains(Application.persistentDataPath)) { full_path = System.IO.Path.Combine(Application.persistentDataPath, full_path); } _status = BackgroundDownloadStatus.Downloading; await www.SendWebRequest(); // _status = www.isDone ? BackgroundDownloadStatus.Done : www.isHttpError? BackgroundDownloadStatus.Failed : BackgroundDownloadStatus.Downloading; // _dwnloads.Remove(full_path); /* * BinaryWriter writer = new BinaryWriter(File.OpenWrite(full_path)); * writer.Write(www.downloadHandler.data, 0, www.downloadHandler.data.Length); * writer.Flush(); * writer.Close(); */ }
internal BackgroundDownloadEditor(BackgroundDownloadConfig config) : base(config) { var destDir = Path.GetDirectoryName(Path.Combine(Application.persistentDataPath, config.filePath)); try { if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } } catch (Exception E) { } IntPtr request = UnityBackgroundDownloadCreateRequest(config.url.AbsoluteUri); request.downloadHandler = new DownloadHandlerFile(Path.Combine(Application.persistentDataPath, config.filePath)); if (config.requestHeaders != null) { foreach (var header in config.requestHeaders) { if (header.Value != null) { foreach (var val in header.Value) { UnityBackgroundDownloadAddRequestHeader(request, header.Key, val); } } } } _backend = request; UnityBackgroundDownloadStart(request, config.filePath); }
internal static Dictionary <string, BackgroundDownload> LoadDownloads() { var downloads = new Dictionary <string, BackgroundDownload>(); int numDownloads = UnityBackgroundDownloadGetCount(); if (numDownloads > 0) { IntPtr[] loadedDownloads = new IntPtr[numDownloads]; UnityBackgroundDownloadGetAll(loadedDownloads); byte[] buffer = new byte[2048]; for (int i = 0; i < numDownloads; ++i) { IntPtr backend = loadedDownloads[i]; BackgroundDownloadConfig config = new BackgroundDownloadConfig(); int length = UnityBackgroundDownloadGetUrl(backend, buffer); config.url = new Uri(MarshalObjCString(buffer, length)); length = UnityBackgroundDownloadGetFilePath(backend, buffer); config.filePath = MarshalObjCString(buffer, length); var dl = new BackgroundDownloadiOS(backend, config); downloads[config.filePath] = dl; } } return(downloads); }
/// <summary> /// Start download from given URL. Create BackgroundDownloadConfig using given arguments. /// </summary> /// <param name="url">URL to download from.</param> /// <param name="filePath">A relative path to save to; will be saved under Application.persistentDataPath.</param> /// <returns>An instance for monitoring the progress.</returns> /// <exception cref="ArgumentException">Thrown if there already is a download with given destination file.</exception> public static BackgroundDownload Start(Uri url, String filePath) { var config = new BackgroundDownloadConfig(); config.url = url; config.filePath = filePath; return(Start(config)); }
internal BackgroundDownloadAndroid(BackgroundDownloadConfig config) : base(config) { SetupBackendStatics(); string filePath = Path.Combine(Application.persistentDataPath, config.filePath); if (File.Exists(filePath)) { File.Delete(filePath); } else { var dir = Path.GetDirectoryName(filePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } string fileUri = "file://" + filePath; bool allowMetered = false; bool allowRoaming = false; switch (_config.policy) { case BackgroundDownloadPolicy.AllowMetered: allowMetered = true; break; case BackgroundDownloadPolicy.AlwaysAllow: allowMetered = true; allowRoaming = true; break; default: break; } _download = _backgroundDownloadClass.CallStatic <AndroidJavaObject>("create", config.url.AbsoluteUri, fileUri); _download.Call("setAllowMetered", allowMetered); _download.Call("setAllowRoaming", allowRoaming); if (config.requestHeaders != null) { foreach (var header in config.requestHeaders) { if (header.Value != null) { foreach (var val in header.Value) { _download.Call("addRequestHeader", header.Key, val); } } } } var activity = _playerClass.GetStatic <AndroidJavaObject>("currentActivity"); _id = _download.Call <long>("start", activity); }
public static BackgroundDownload Start(BackgroundDownloadConfig config) { LoadDownloads(); if (_downloads.ContainsKey(config.filePath)) { throw new ArgumentException("Download of this file is already present"); } var download = new BackgroundDownloadimpl(config); _downloads.Add(config.filePath, download); SaveDownloads(); return(download); }
internal BackgroundDownloadUWP(BackgroundDownloadConfig config) : base(config) { #if ENABLE_WINMD_SUPPORT CreateBackgroundDownloadGroup(); string filePath = Path.Combine(Application.persistentDataPath, config.filePath); string directory = Path.GetDirectoryName(filePath).Replace('/', '\\'); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } _cancelSource = new CancellationTokenSource(); StartDownload(filePath, directory); #endif }
internal static Dictionary <string, BackgroundDownload> LoadDownloads() { Dictionary <string, BackgroundDownload> downloads = new Dictionary <string, BackgroundDownload>(); foreach (var item in _dwnloads) { IntPtr backend = item.Value; BackgroundDownloadConfig config = new BackgroundDownloadConfig(); config.url = backend.uri; config.filePath = item.Key; var dl = new BackgroundDownloadEditor(backend, config); downloads[config.filePath] = dl; } return(downloads); }
protected BackgroundDownload(BackgroundDownloadConfig config) { _config = config; switch (_config.policy) { case BackgroundDownloadPolicy.UnrestrictedOnly: case BackgroundDownloadPolicy.AllowMetered: case BackgroundDownloadPolicy.AlwaysAllow: break; case BackgroundDownloadPolicy.Default: default: _config.policy = BackgroundDownloadPolicy.AllowMetered; break; } }
public BackgroundDownloadEditor(BackgroundDownloadConfig config) : base(config) { _status = BackgroundDownloadStatus.Failed; _error = "Not implemented for Unity Editor"; }
BackgroundDownloadEditor(IntPtr backend, BackgroundDownloadConfig config) : base(config) { _backend = backend; }
public BackgroundDownloadEditor(BackgroundDownloadConfig config) : base(config) { _tokenSource = new CancellationTokenSource(); StartDownloading(); }