/// <summary>
        /// Load the assetbundle from path via UnityWebRequest
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private IEnumerator DownloadAssetBundle(FileDownloadVO fileDownloadVO)
        {
            UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(fileDownloadVO.path, (uint)fileDownloadVO.version, 0);

            uwr.SendWebRequest();

            while (!uwr.isDone)
            {
                yield return(null);
            }

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                ReportError(fileDownloadVO.path);
                yield break;
            }
            else
            {
                AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);

                // Check for onComplete callback
                fileDownloadVO.onComplete?.Invoke(bundle);

                // Notify system
                SendNotification(FileDownloadNote.FILE_DOWNLOADED, bundle);
            }

            yield return(new WaitForFixedUpdate());

            ProcessNextItemInQueue();
        }
        protected void ProcessNextItemInQueue()
        {
            // Get next item in queue
            FileDownloadVO fileDownloadVO = fileDownloadSystemProxy.GetNextItemInQueue();

            // Null check for end of queue
            if (fileDownloadVO == null)
            {
                fileDownloadSystemProxy.RemoveLoadingGameObject();
                fileDownloadSystemProxy.IsQueueProcessing = false;
                SendNotification(FileDownloadNote.QUEUE_FINISHED);
                return;
            }

            // Null check for invalid file path
            if (string.IsNullOrEmpty(fileDownloadVO.path))
            {
                ReportError(fileDownloadVO.path);
                return;
            }

            if (!string.IsNullOrEmpty(fileDownloadVO.loadingTemplate))
            {
                // Show loading screen
                fileDownloadSystemProxy.CreateLoadingGameObject(fileDownloadVO.loadingTemplate);
            }

            // Determine the file type and pass to appropriate Coroutine
            IEnumerator coroutine = null;

            switch (fileDownloadVO.fileType)
            {
            case FileType.ASSET_BUNDLE:
                coroutine = DownloadAssetBundle(fileDownloadVO);
                break;

            case FileType.PNG:
            case FileType.JPG:
                coroutine = DownloadTexture(fileDownloadVO);
                break;

            case FileType.MOV:
            case FileType.MP4:
                coroutine = DownloadVideoClip(fileDownloadVO);
                break;
            }

            if (coroutine == null)
            {
                ReportError(fileDownloadVO.path);
                return;
            }

            // Request starting the coroutine
            SendNotification(CoreNote.REQUEST_START_COROUTINE, new RequestStartCoroutineVO
            {
                coroutine = coroutine
            });
        }
Example #3
0
        /// <summary>
        /// Retrieve the next item from the queue
        /// This automatically removes the item from the queue
        /// </summary>
        /// <returns></returns>
        internal FileDownloadVO GetNextItemInQueue()
        {
            if (FileDownloadQueueVO.queue.Count <= 0)
            {
                return(null);
            }
            FileDownloadVO fileDownloadVO = FileDownloadQueueVO.queue.First();

            FileDownloadQueueVO.queue.RemoveAt(0);
            return(fileDownloadVO);
        }
        /// <summary>
        /// Start coroutine to load assetbundle
        /// </summary>
        /// <param name="notification"></param>
        public override void Execute(INotification notification)
        {
            // Get the FileDownloadSystemProxy
            fileDownloadSystemProxy = Facade.RetrieveProxy(FileDownloadProxy.NAME) as FileDownloadProxy;

            // Get the bundle VO
            FileDownloadVO fileDownloadVO = notification.Body as FileDownloadVO;

            // Add the VO to queue
            fileDownloadSystemProxy.AddItemToQueue(fileDownloadVO);

            // Request process queue if auto AND is not already processing
            if (fileDownloadVO.autoProcessQueue && !fileDownloadSystemProxy.IsQueueProcessing)
            {
                SendNotification(FileDownloadNote.REQUEST_PROCESS_QUEUE);
            }
        }
        /// <summary>
        /// Load the texture from path via UnityWebRequest
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private IEnumerator DownloadVideoClip(FileDownloadVO fileDownloadVO, bool forceCached = false)
        {
            // Attempt to load cached version
            string fileName       = Path.GetFileName(fileDownloadVO.path);
            string cachedFilePath = Path.Combine(UnityEngine.Application.persistentDataPath, fileName);
            string loadFromPath   = fileDownloadVO.path;
            bool   cacheExists    = File.Exists(cachedFilePath);
            int    cacheAge       = -1;

            if (cacheExists)
            {
                cacheAge = System.DateTime.Now.Subtract(File.GetLastWriteTime(cachedFilePath)).Hours;

                // Check the age of file
                if (cacheAge <= 24 || forceCached)
                {
                    loadFromPath = cachedFilePath;

                    // Check for onComplete callback
                    fileDownloadVO.onComplete?.Invoke(cachedFilePath);

                    // Notify the application
                    SendNotification(FileDownloadNote.FILE_DOWNLOADED, cachedFilePath);

                    yield return(new WaitForFixedUpdate());

                    ProcessNextItemInQueue();

                    yield break;
                }
            }


            UnityWebRequest uwr = UnityWebRequest.Get(loadFromPath);

            uwr.SendWebRequest();
            ProcessNextItemInQueue();

            while (!uwr.isDone)
            {
                yield return(null);
            }

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                if (loadFromPath == fileDownloadVO.path && cacheExists && cacheAge > 24)
                {
                    // Request starting the coroutine with forced cache
                    SendNotification(CoreNote.REQUEST_START_COROUTINE, new RequestStartCoroutineVO
                    {
                        coroutine = DownloadVideoClip(fileDownloadVO, true)
                    });
                    yield break;
                }

                ReportError(fileDownloadVO.path);
                yield break;
            }
            else
            {
                // Store in cache
                if (!cacheExists || cacheAge > 24 && loadFromPath == fileDownloadVO.path)
                {
                    File.WriteAllBytes(cachedFilePath, uwr.downloadHandler.data);
                }

                // Check for onComplete callback
                fileDownloadVO.onComplete?.Invoke(cachedFilePath);

                // Notify the application
                SendNotification(FileDownloadNote.FILE_DOWNLOADED, cachedFilePath);
            }
            yield return(new WaitForFixedUpdate());
            // ProcessNextItemInQueue();
        }
Example #6
0
 /// <summary>
 /// Add a new file download request to the queue
 /// </summary>
 /// <param name="fileDownloadVO"></param>
 internal void AddItemToQueue(FileDownloadVO fileDownloadVO)
 {
     FileDownloadQueueVO.queue.Add(fileDownloadVO);
 }