Example #1
0
        public override BundleLoader Create(BundleManager manager, BundleInfo bundleInfo, BundleLoader[] dependencies)
        {
            Uri loadBaseUri = this.BaseUri;

            if (this.useCache && BundleUtil.ExistsInCache(bundleInfo))
            {
                loadBaseUri = this.BaseUri;
                return(new WWWBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache));
            }

            if (BundleUtil.ExistsInStorableDirectory(bundleInfo))
            {
                /* Path: Application.persistentDataPath + "/bundles/" + bundleInfo.Filename  */
                loadBaseUri = new Uri(BundleUtil.GetStorableDirectory());
            }
#if !UNITY_WEBGL || UNITY_EDITOR
            else if (BundleUtil.ExistsInReadOnlyDirectory(bundleInfo))
            {
                /* Path: Application.streamingAssetsPath + "/bundles/" + bundleInfo.Filename */
                loadBaseUri = new Uri(BundleUtil.GetReadOnlyDirectory());
            }
#endif
            if (bundleInfo.IsEncrypted)
            {
                if (this.decryptor != null && bundleInfo.Encoding.Equals(decryptor.AlgorithmName))
                {
                    return(new CryptographBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, decryptor));
                }

                throw new NotSupportedException(string.Format("Not support the encryption algorithm '{0}'.", bundleInfo.Encoding));
            }

            return(new WWWBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache));
        }
Example #2
0
        public override BundleLoader Create(BundleManager manager, BundleInfo bundleInfo)
        {
            Uri loadBaseUri = this.BaseUri;

            if (this.useCache && BundleUtil.ExistsInCache(bundleInfo))
            {
                //Load assets from the cache of Unity3d.
                loadBaseUri = this.BaseUri;
                return(new UnityWebRequestBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager, this.useCache));
            }

            if (BundleUtil.ExistsInStorableDirectory(bundleInfo))
            {
                //Load assets from the "Application.persistentDataPath/bundles" folder.
                /* Path: Application.persistentDataPath + "/bundles/" + bundleInfo.Filename  */
                loadBaseUri = new Uri(BundleUtil.GetStorableDirectory());
            }

#if !UNITY_WEBGL || UNITY_EDITOR
            else if (BundleUtil.ExistsInReadOnlyDirectory(bundleInfo))
            {
                //Load assets from the "Application.streamingAssetsPath/bundles" folder.
                /* Path: Application.streamingAssetsPath + "/bundles/" + bundleInfo.Filename */
                loadBaseUri = new Uri(BundleUtil.GetReadOnlyDirectory());
            }
#endif

            if (bundleInfo.IsEncrypted)
            {
                if (this.decryptor != null && bundleInfo.Encoding.Equals(decryptor.AlgorithmName))
                {
                    return(new CryptographBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager, decryptor));
                }

                throw new NotSupportedException(string.Format("Not support the encryption algorithm '{0}'.", bundleInfo.Encoding));
            }

            //Loads assets from an Internet address if it does not exist in the local directory.
            if (this.IsRemoteUri(loadBaseUri))
            {
                return(new UnityWebRequestBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager, this.useCache));
            }
            else
            {
                return(new FileAsyncBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager));
            }
        }
        public override BundleLoader Create(BundleManager manager, BundleInfo bundleInfo)
        {
            Uri loadBaseUri = this.BaseUri;

            if (BundleUtil.ExistsInStorableDirectory(bundleInfo))
            {
                /* Path: Application.persistentDataPath + "/bundles/" + bundleInfo.Filename  */
                loadBaseUri = new Uri(BundleUtil.GetStorableDirectory());
            }
#if !UNITY_WEBGL || UNITY_EDITOR
            else if (BundleUtil.ExistsInReadOnlyDirectory(bundleInfo))
            {
                /* Path: Application.streamingAssetsPath + "/bundles/" + bundleInfo.Filename */
                loadBaseUri = new Uri(BundleUtil.GetReadOnlyDirectory());
            }
#endif

            if (bundleInfo.IsEncrypted)
            {
                if (this.decryptor != null && bundleInfo.Encoding.Equals(decryptor.AlgorithmName))
                {
                    return(new CryptographBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager, decryptor));
                }

                throw new NotSupportedException(string.Format("Not support the encryption algorithm '{0}'.", bundleInfo.Encoding));
            }

#if UNITY_ANDROID && !UNITY_5_4_OR_NEWER
            if (loadBaseUri != null && loadBaseUri.Scheme.Equals("jar", StringComparison.OrdinalIgnoreCase))
            {
                return(new WWWBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager, false));
            }
#endif

            return(new FileAsyncBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, manager));
        }
        protected override IEnumerator DoLoadAssetBundle(IProgressPromise <float, AssetBundle> promise)
        {
            if (this.BundleInfo.IsEncrypted)
            {
                promise.UpdateProgress(0f);
                promise.SetException(new NotSupportedException(string.Format("The data of the AssetBundle named '{0}' is encrypted,use the CryptographBundleLoader to load,please.", this.BundleInfo.Name)));
                yield break;
            }

            AssetBundle assetBundle;
            string      path      = this.GetAbsoluteUri();
            long        totalSize = this.BundleInfo.FileSize;

            if (this.IsRemoteUri())
            {
                string fullname = BundleUtil.GetStorableDirectory() + this.BundleInfo.Filename;
                using (UnityWebRequest www = new UnityWebRequest(path))
                {
                    www.downloadHandler = new DownloadHandlerBuffer();
#if UNITY_2018_1_OR_NEWER
                    www.SendWebRequest();
#else
                    www.Send();
#endif
                    while (!www.isDone)
                    {
                        //if (www.downloadProgress >= 0)
                        //    promise.UpdateProgress(www.downloadProgress);
                        if (www.downloadedBytes >= 0 && totalSize > 0)
                        {
                            promise.UpdateProgress((float)www.downloadedBytes / totalSize);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }

                    var data = www.downloadHandler.data;

                    try
                    {
                        FileInfo info = new FileInfo(fullname);
                        if (info.Exists)
                        {
                            info.Delete();
                        }

                        if (!info.Directory.Exists)
                        {
                            info.Directory.Create();
                        }

                        File.WriteAllBytes(info.FullName, data);
                    }
                    catch (Exception e)
                    {
                        if (log.IsWarnEnabled)
                        {
                            log.WarnFormat("Save AssetBundle '{0}' to the directory '{1}' failed.Reason:{2}", this.BundleInfo.FullName, fullname, e);
                        }
                    }
                }

                AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(fullname);
                while (!request.isDone)
                {
                    promise.UpdateProgress(request.progress);
                    yield return(null);
                }

                assetBundle = request.assetBundle;
                if (assetBundle == null)
                {
                    promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                    yield break;
                }

                promise.UpdateProgress(1f);
                promise.SetResult(assetBundle);
                yield break;
            }

#if UNITY_ANDROID && !UNITY_2017_1_OR_NEWER
            if (this.Uri.Scheme.Equals("jar", StringComparison.OrdinalIgnoreCase))
            {
                using (WWW www = useCache ? WWW.LoadFromCacheOrDownload(path, this.BundleInfo.Hash) : new WWW(path))
                {
                    while (!www.isDone)
                    {
                        if (www.progress >= 0)
                        {
                            promise.UpdateProgress(www.progress);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }

                    assetBundle = www.assetBundle;
                }
            }
            else
#endif
            {
#if UNITY_2018_1_OR_NEWER
                using (UnityWebRequest www = useCache ? UnityWebRequestAssetBundle.GetAssetBundle(path, this.BundleInfo.Hash, 0) : UnityWebRequestAssetBundle.GetAssetBundle(path))
                {
                    www.SendWebRequest();
#else
                using (UnityWebRequest www = useCache ? UnityWebRequest.GetAssetBundle(path, this.BundleInfo.Hash, 0) : UnityWebRequest.GetAssetBundle(path))
                {
                    www.Send();
#endif
                    while (!www.isDone)
                    {
                        //if (www.downloadProgress >= 0)
                        //    promise.UpdateProgress(www.downloadProgress);
                        if (www.downloadedBytes >= 0 && totalSize > 0)
                        {
                            promise.UpdateProgress((float)www.downloadedBytes / totalSize);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }

                    DownloadHandlerAssetBundle handler = (DownloadHandlerAssetBundle)www.downloadHandler;
                    assetBundle = handler.assetBundle;
                }
            }

            if (assetBundle == null)
            {
                promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                yield break;
            }

            promise.UpdateProgress(1f);
            promise.SetResult(assetBundle);
        }
    }
Example #5
0
        protected override IEnumerator DoLoadAssetBundle(IProgressPromise <float, AssetBundle> promise)
        {
            if (this.BundleInfo.IsEncrypted)
            {
                promise.UpdateProgress(0f);
                promise.SetException(new NotSupportedException(string.Format("The data of the AssetBundle named '{0}' is encrypted,use the CryptographBundleLoader to load,please.", this.BundleInfo.Name)));
                yield break;
            }

            string path = this.GetAbsolutePath();

#if UNITY_ANDROID && !UNITY_5_4_OR_NEWER
            if (this.Uri.Scheme.Equals("jar", StringComparison.OrdinalIgnoreCase))
            {
                promise.UpdateProgress(0f);
                promise.SetException(new NotSupportedException(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.It is not supported before the Unity3d 5.4.0 version.", this.BundleInfo.Name, path)));
                yield break;
            }
#endif
            float weight    = 0;
            long  totalSize = this.BundleInfo.FileSize;
            if (this.IsRemoteUri())
            {
                weight = WEIGHT;
                string fullname = BundleUtil.GetStorableDirectory() + this.BundleInfo.Filename;
                using (UnityWebRequest www = new UnityWebRequest(path))
                {
                    www.downloadHandler = new DownloadFileHandler(fullname);
#if UNITY_2018_1_OR_NEWER
                    www.SendWebRequest();
#else
                    www.Send();
#endif
                    while (!www.isDone)
                    {
                        if (www.downloadedBytes >= 0 && totalSize > 0)
                        {
                            promise.UpdateProgress(weight * (float)www.downloadedBytes / totalSize);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }
                    path = fullname;
                }
            }

            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
            while (!request.isDone)
            {
                promise.UpdateProgress(weight + (1 - weight) * request.progress);
                yield return(null);
            }

            var assetBundle = request.assetBundle;
            if (assetBundle == null)
            {
                promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                yield break;
            }

            promise.UpdateProgress(1f);
            promise.SetResult(assetBundle);
        }
Example #6
0
        protected override IEnumerator DoLoadAssetBundle(IProgressPromise <float, AssetBundle> promise)
        {
            if (this.BundleInfo.IsEncrypted)
            {
                promise.UpdateProgress(0f);
                promise.SetException(new NotSupportedException(string.Format("The data of the AssetBundle named '{0}' is encrypted,use the CryptographBundleLoader to load,please.", this.BundleInfo.Name)));
                yield break;
            }

            string path = this.GetAbsoluteUri();

            using (WWW www = useCache ? WWW.LoadFromCacheOrDownload(path, this.BundleInfo.Hash) : new WWW(path))
            {
                while (!www.isDone)
                {
                    if (www.progress >= 0)
                    {
                        promise.UpdateProgress(www.progress);
                    }
                    yield return(null);
                }

                if (!string.IsNullOrEmpty(www.error))
                {
                    promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                    yield break;
                }

                var assetBundle = www.assetBundle;
                if (assetBundle == null)
                {
                    promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                    yield break;
                }

                if (!useCache && this.IsRemoteUri())
                {
                    string fullname = BundleUtil.GetStorableDirectory() + this.BundleInfo.Filename;
                    try
                    {
                        FileInfo info = new FileInfo(fullname);
                        if (info.Exists)
                        {
                            info.Delete();
                        }

                        if (!info.Directory.Exists)
                        {
                            info.Directory.Create();
                        }

                        File.WriteAllBytes(info.FullName, www.bytes);
                    }
                    catch (Exception e)
                    {
                        if (log.IsWarnEnabled)
                        {
                            log.WarnFormat("Save AssetBundle '{0}' to the directory '{1}' failed.Reason:{2}", this.BundleInfo.FullName, fullname, e);
                        }
                    }
                }
                promise.UpdateProgress(1f);
                promise.SetResult(assetBundle);
            }
        }
        protected override IEnumerator DoLoadAssetBundle(IProgressPromise <float, AssetBundle> promise)
        {
            if (!this.decryptor.AlgorithmName.Equals(this.BundleInfo.Encoding))
            {
                promise.UpdateProgress(0f);
                promise.SetException(new Exception(string.Format("The encryption algorithm '{0}' and decryption algorithm '{1}' does not match when decrypts Assetbundle {2}. ", this.BundleInfo.Encoding, this.decryptor.AlgorithmName, this.BundleInfo.Name)));
                yield break;
            }

            byte[] chiperData = null;
            string path       = this.GetAbsoluteUri();

#if UNITY_2017_1_OR_NEWER
            using (UnityWebRequest www = new UnityWebRequest(path))
            {
                www.downloadHandler = new DownloadHandlerBuffer();
#if UNITY_2018_1_OR_NEWER
                www.SendWebRequest();
#else
                www.Send();
#endif
                while (!www.isDone)
                {
                    if (www.downloadProgress >= 0)
                    {
                        promise.UpdateProgress(www.downloadProgress * WEIGHT);
                    }
                    yield return(null);
                }

                if (!string.IsNullOrEmpty(www.error))
                {
                    promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                    yield break;
                }

                chiperData = www.downloadHandler.data;
            }
#elif UNITY_5_4_OR_NEWER
            if (this.Uri.Scheme.Equals("jar", StringComparison.OrdinalIgnoreCase))
            {
                using (WWW www = new WWW(path))
                {
                    while (!www.isDone)
                    {
                        if (www.progress >= 0)
                        {
                            promise.UpdateProgress(www.progress * WEIGHT);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }

                    chiperData = www.bytes;
                }
            }
            else
            {
                using (UnityWebRequest www = new UnityWebRequest(path))
                {
                    www.downloadHandler = new DownloadHandlerBuffer();
                    www.Send();
                    while (!www.isDone)
                    {
                        if (www.downloadProgress >= 0)
                        {
                            promise.UpdateProgress(www.downloadProgress * WEIGHT);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }

                    chiperData = www.downloadHandler.data;
                }
            }
#else
            using (WWW www = new WWW(path))
            {
                while (!www.isDone)
                {
                    if (www.progress >= 0)
                    {
                        promise.UpdateProgress(www.progress * WEIGHT);
                    }
                    yield return(null);
                }

                if (!string.IsNullOrEmpty(www.error))
                {
                    promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                    yield break;
                }

                chiperData = www.bytes;
            }
#endif

            if (this.IsRemoteUri())
            {
                string fullname = BundleUtil.GetStorableDirectory() + this.BundleInfo.Filename;
                try
                {
                    FileInfo info = new FileInfo(fullname);
                    if (info.Exists)
                    {
                        info.Delete();
                    }

                    if (!info.Directory.Exists)
                    {
                        info.Directory.Create();
                    }

                    File.WriteAllBytes(info.FullName, chiperData);
                }
                catch (Exception e)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Save AssetBundle '{0}' to the directory '{1}' failed.Reason:{2}", this.BundleInfo.FullName, fullname, e);
                    }
                }
            }

            byte[] textData = null;
            try
            {
                textData = this.decryptor.Decrypt(chiperData);
            }
            catch (Exception e)
            {
                promise.SetException(new Exception(string.Format("Failed to decrypt the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, e)));
                yield break;
            }

            AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(textData);
            while (!request.isDone)
            {
                promise.UpdateProgress(WEIGHT + (1 - WEIGHT) * request.progress);
                yield return(null);
            }

            var assetBundle = request.assetBundle;
            if (assetBundle == null)
            {
                promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                yield break;
            }

            promise.UpdateProgress(1f);
            promise.SetResult(assetBundle);
        }
        protected virtual IEnumerator DoLoadAssetBundleFromStream(IProgressPromise <float, AssetBundle> promise)
        {
            if (!this.decryptor.AlgorithmName.Equals(this.BundleInfo.Encoding))
            {
                promise.UpdateProgress(0f);
                promise.SetException(new Exception(string.Format("The encryption algorithm '{0}' and decryption algorithm '{1}' does not match when decrypts Assetbundle {2}. ", this.BundleInfo.Encoding, this.decryptor.AlgorithmName, this.BundleInfo.Name)));
                yield break;
            }

            string path      = this.GetAbsoluteUri();
            long   totalSize = this.BundleInfo.FileSize;
            float  weight    = WEIGHT;

            if (this.IsRemoteUri())
            {
                string fullname = BundleUtil.GetStorableDirectory() + this.BundleInfo.Filename;
                using (UnityWebRequest www = new UnityWebRequest(path))
                {
                    www.downloadHandler = new DownloadFileHandler(fullname);
                    www.SendWebRequest();
                    while (!www.isDone)
                    {
                        if (www.downloadedBytes >= 0 && totalSize > 0)
                        {
                            promise.UpdateProgress(weight * (float)www.downloadedBytes / totalSize);
                        }
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, www.error)));
                        yield break;
                    }
                    path = fullname;
                }
            }
            else
            {
                weight = 0f;
                path   = this.GetAbsolutePath();
            }

            AssetBundleCreateRequest request = null;
            Stream stream = null;

            try
            {
                stream  = FileUtil.OpenRead(path);
                request = AssetBundle.LoadFromStreamAsync(decryptor.Decrypt(stream));
            }
            catch (Exception e)
            {
                if (stream != null)
                {
                    stream.Close();
                }

                promise.SetException(new Exception(string.Format("Failed to decrypt the AssetBundle '{0}' at the address '{1}'.Error:{2}", this.BundleInfo.Name, path, e)));
                yield break;
            }

            while (!request.isDone)
            {
                promise.UpdateProgress(weight + (1 - weight) * request.progress);
                yield return(null);
            }

            var assetBundle = request.assetBundle;

            if (assetBundle == null)
            {
                promise.SetException(new Exception(string.Format("Failed to load the AssetBundle '{0}' at the address '{1}'.", this.BundleInfo.Name, path)));
                yield break;
            }
            promise.UpdateProgress(1f);
            promise.SetResult(assetBundle);
        }