예제 #1
0
        private static async Task DownloadHelper(
            DownloadableFile downloadableFile,
            int retry = 1,
            System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null
            )
        {
            if (downloadableFile.Url == null)
            {
                return;
            }

            //uncomment the following line to force re-download every time.
            //File.Delete(downloadableFile.LocalFile);
            if (!File.Exists(downloadableFile.LocalFile) || new FileInfo(downloadableFile.LocalFile).Length == 0)
            {
                try
                {
                    //Download the file
                    Trace.WriteLine("downloading file from:" + downloadableFile.Url + " to: " + downloadableFile.LocalFile);
                    System.Net.WebClient downloadClient = new System.Net.WebClient();

                    if (onDownloadProgressChanged != null)
                    {
                        downloadClient.DownloadProgressChanged += onDownloadProgressChanged;
                    }

                    FileInfo fi = new FileInfo(downloadableFile.LocalFile);
                    if (!fi.Directory.Exists)
                    {
                        fi.Directory.Create();
                    }
                    await downloadClient.DownloadFileTaskAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile);
                }
                catch (Exception e)
                {
                    if (File.Exists(downloadableFile.LocalFile))
                    {
                        //The downloaded file may be corrupted, should delete it
                        File.Delete(downloadableFile.LocalFile);
                    }

                    if (retry > 0)
                    {
                        await DownloadHelper(downloadableFile, retry - 1);
                    }
                    else
                    {
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE
                        UnityEngine.Debug.Log(e.StackTrace);
#else
                        Trace.WriteLine(e);
#endif
                        throw;
                    }
                }
            }
        }
예제 #2
0
        private static async Task DownloadHelper(
            DownloadableFile downloadableFile,
            int retry = 1,
            System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null
            )
        {
            if (downloadableFile.Url == null)
            {
                return;
            }

            //uncomment the following line to force re-download every time.
            //File.Delete(downloadableFile.LocalFile);
            if (!downloadableFile.IsLocalFileValid)
            {
                try
                {
                    //Download the file
                    Trace.WriteLine("downloading file from:" + downloadableFile.Url + " to: " + downloadableFile.LocalFile);
                    System.Net.WebClient downloadClient = new System.Net.WebClient();

                    if (onDownloadProgressChanged != null)
                    {
                        downloadClient.DownloadProgressChanged += onDownloadProgressChanged;
                    }

                    FileInfo fi = new FileInfo(downloadableFile.LocalFile);
                    if (!fi.Directory.Exists)
                    {
                        fi.Directory.Create();
                    }
                    await downloadClient.DownloadFileTaskAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile);
                }
                catch (Exception e)
                {
                    if (!downloadableFile.IsLocalFileValid)
                    {
                        //The downloaded file may be corrupted, should delete it
                        File.Delete(downloadableFile.LocalFile);
                    }

                    if (retry > 0)
                    {
                        await DownloadHelper(downloadableFile, retry - 1);
                    }
                    else
                    {
                        Trace.WriteLine(e);
                        throw;
                    }
                }
            }
        }
예제 #3
0
        private static async Task DownloadHelperMultiple(
            DownloadableFile[] downloadableFiles,
            int retry = 1,
            System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null)
        {
            if (downloadableFiles == null || downloadableFiles.Length == 0)
            {
                return;
            }
            else if (downloadableFiles.Length == 1)
            {
                await DownloadHelper(downloadableFiles[0], retry, onDownloadProgressChanged);
            }
            else
            {
                DownloadableFile   currentFile    = downloadableFiles[0];
                DownloadableFile[] remainingFiles = new DownloadableFile[downloadableFiles.Length - 1];
                Array.Copy(downloadableFiles, 1, remainingFiles, 0, remainingFiles.Length);
                await DownloadHelper(currentFile, retry, onDownloadProgressChanged);

                await DownloadHelperMultiple(remainingFiles, retry, onDownloadProgressChanged);
            }
        }
예제 #4
0
 /// <summary>
 /// Add a file to download
 /// </summary>
 /// <param name="downloadableFile">The file to be downloaded</param>
 public void AddFile(DownloadableFile downloadableFile)
 {
     _files.Add(downloadableFile);
 }