public RemoteAssetDownloadInfo DownloadAsset(RemoteAssetInfo asset)
        {
            if (asset == null)
            {
                return(null);
            }
            RemoteAssetDownloadInfo downloadInfo = new RemoteAssetDownloadInfo();

            downloadInfo.Asset = asset;
            ProcessFile(downloadInfo);
            return(downloadInfo);
        }
 private void ProcessNextStartupFile_OnStatusUpdate(RemoteAssetDownloadInfo remoteAssetDownloadInfo)
 {
     if (remoteAssetDownloadInfo.IsCompleted)
     {
         ProcessNextStartupFile(remoteAssetDownloadInfo.Asset);
         remoteAssetDownloadInfo = null;
     }
     else if (remoteAssetDownloadInfo.HasError)
     {
         ProcessNextGameReadyFile(remoteAssetDownloadInfo.Asset, true);
         remoteAssetDownloadInfo = null;
     }
 }
        private void UpdateCurrentProgress(UnityWebRequest www, RemoteAssetDownloadInfo downloadInfo)
        {
            //if (currentGameReadyFilesQueue != null && currentGameReadyFilesQueue.Contains(downloadInfo.Asset))
            //{
            //    interimGameReadySizeDownloaded = (www == null) ? 0 : Convert.ToInt64(www.downloadedBytes);

            //    if (ShowGameReadyAssetsProgress)
            //    {
            //        currentProgress = string.Format(ResourceManager.GetValue("Homesreen.Loading.DownloadingAssets"), CurrentGameAssetDownloadProgress);
            //    }
            //}
            //else if (currentStartupFilesQueue != null && currentStartupFilesQueue.Contains(downloadInfo.Asset))
            //{
            //    currentProgress = string.Format(ResourceManager.GetValue("Homesreen.Loading.DownloadingDatabase"), downloadInfo.Progress.ToString());
            //}
            //else
            //{
            //    currentProgress = string.Empty;
            //}
        }
        private void ProcessFile(RemoteAssetDownloadInfo downloadInfo)
        {
            GetContentDownloadUrlRequest request = new GetContentDownloadUrlRequest()
            {
                Key = downloadInfo.Asset.File.RelativeName
            };

            var   timeSinceLastRequest = (DateTime.Now - LastRequest).TotalMilliseconds;
            float delay = 0f;

            if (timeSinceLastRequest < 1000)
            {
                delay = 0.6f;
            }

            UnityEngine.Debug.Log("GetContentDownloadUrl DELAY " + timeSinceLastRequest + " ms.");
            DOVirtual.DelayedCall(delay, () =>
                                  PlayFab.PlayFabClientAPI.GetContentDownloadUrl(request, (GetContentDownloadUrlResult r) => GetContentDownloadUrl_Completed(r, downloadInfo), (PlayFabError e) => GetContentDownloadUrl_Completed(null, downloadInfo)));

            LastRequest = DateTime.Now;
        }
        private void OnFileDownloaded(UnityWebRequest www, RemoteAssetDownloadInfo downloadInfo)
        {
            if (www.isNetworkError || www.isHttpError)
            {
                downloadInfo.Progress = 0;
                downloadInfo.HasError = true;
                downloadInfo.RaiseStatusEvent();
                Debug.Log(downloadInfo.Asset.File.FileName + " FILE DOWNLOAD ERROR!");
                www = null;
            }
            else
            {
                if (www.isDone)
                {
                    byte[] results = www.downloadHandler.data;
                    if (File.Exists(downloadInfo.Asset.File.FileName))
                    {
                        File.Delete(downloadInfo.Asset.File.FileName);
                    }
                    if (!Directory.Exists(Path.GetDirectoryName(downloadInfo.Asset.File.FileName)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(downloadInfo.Asset.File.FileName));
                    }

                    File.WriteAllBytes(downloadInfo.Asset.File.FileName, results);
                    if (downloadInfo.Asset.Version > 1)
                    {
                        IncrementFileVersion(downloadInfo.Asset.File.LocalRelativeName, downloadInfo.Asset.Version);
                    }
                    downloadInfo.IsCompleted = true;
                    downloadInfo.Progress    = 100;

                    if (downloadInfo.Asset.File.FileName.ToLower().EndsWith(".zip"))
                    {
                        //ZipArchive zip = ZipFile.OpenRead(downloadInfo.Asset.File.FileName);


                        FileInfo fileInfo = new FileInfo(downloadInfo.Asset.File.FileName);



                        using (ZipArchive archive = ZipFile.OpenRead(downloadInfo.Asset.File.FileName))
                        {
                            string currentFileName = Path.GetFileName(downloadInfo.Asset.File.FileName);
                            string folderName      = downloadInfo.Asset.File.FileName.Replace(@"\", @"/").Replace(currentFileName, "");


                            foreach (var entry in archive.Entries)
                            {
                                using (var entryStream = entry.Open())
                                {
                                    string finalFileName = folderName + entry.Name;
                                    //try
                                    //{
                                    if (File.Exists(finalFileName))
                                    {
                                        File.Delete(finalFileName);
                                    }
                                    if (!File.Exists(finalFileName))
                                    {
                                        using (FileStream outputFileStream = new FileStream(finalFileName, FileMode.Create))
                                        {
                                            entryStream.CopyTo(outputFileStream);
                                        }
                                    }
                                    //}
                                    //catch {}
                                }
                            }
                        }
                    }

                    downloadInfo.RaiseStatusEvent();
                    downloadInfo.Asset.RequireDownload = false;
                    www = null;
                    totalGameReadySizeDownloaded  += downloadInfo.Asset.File.Size;
                    interimGameReadySizeDownloaded = 0;



                    UpdateCurrentProgress(null, downloadInfo);
                }
                else
                {
                    //long totalLenght = Convert.ToInt64((www.downloadProgress * 100f) * www.downloadedBytes);
                    downloadInfo.Progress = Convert.ToInt32(www.downloadProgress * 100);
                    downloadInfo.RaiseStatusEvent();
                    UpdateCurrentProgress(www, downloadInfo);
                }
                Debug.Log(downloadInfo.Asset.File.FileName + " " + downloadInfo.Progress.ToString() + "%");
            }
        }
 private void ProcessFileDownload(RemoteAssetDownloadInfo downloadInfo, string url)
 {
     UnityWebRequestHandler.Instance.GetBodyFromHttpWithProgress(url, null, (UnityWebRequest r) => OnFileDownloaded(r, downloadInfo));
 }
 private void GetContentDownloadUrl_Completed(GetContentDownloadUrlResult result, RemoteAssetDownloadInfo downloadInfo)
 {
     if (result != null)
     {
         string urlToDownloadFile = result.URL;
         ProcessFileDownload(downloadInfo, urlToDownloadFile);
     }
     else
     {
         downloadInfo.Progress = 0;
         downloadInfo.HasError = true;
         downloadInfo.RaiseStatusEvent();
     }
 }