internal void RemoveTask(ABInfo abInfo, BackGroundQueue bQueue) { loadingTasks.Remove(abInfo); bool isError = abInfo.state != ABInfoState.Success; var mainAbInfo = ManifestManager.GetABInfo(abInfo.abName); if (mainAbInfo != null) { mainAbInfo.state = abInfo.state; } #if !HUGULA_NO_LOG Debug.LogFormat("task complete abName={0},size={1},isError={2},loadingTasks.Count={3},bQueue.count={4}", abInfo.abName, abInfo.size, isError, loadingTasks.Count, bQueue.Count); #endif bQueue.Complete(abInfo, isError); if (!bQueue.IsError) { LoadingQueue(); } }
/// <summary> /// assetbundle is done /// </summary> /// <param name="req"></param> /// <param name="ab"></param> static protected void CallOnAssetBundleComplete(CRequest req, AssetBundle ab) { if (OnAssetBundleComplete != null) { OnAssetBundleComplete(req, ab); } #if HUGULA_PROFILER_DEBUG && !HUGULA_NO_LOG var now = System.DateTime.Now; var dt1 = now - req.beginQueueTime; var dt2 = now - req.beginLoadTime; var size = 0u; var abinfo = ManifestManager.GetABInfo(req.key); if (abinfo != null) { size = abinfo.size; } string tips = "AssetBundle"; if (req.isShared) { tips = "Shared AssetBundle"; } Debug.LogFormat("<color=#0a9a0a>{0} Request(asset={1},key={2},size={3}) alltime={4}s,loadtime={5}s,frame={6}</color>", tips, req.assetName, req.key, size, dt1.TotalSeconds, dt2.TotalSeconds, Time.frameCount); #endif }
private void _BeginDownload() { /* * Stream Compressed (LZMA) Mem: LZ4 compressed bundle size. Perf: reading from disk + LZMA decompression + LZ4 compression. * Chunk Compressed (LZ4) Mem: no extra memory is used. Perf: reading from disk. */ string url = CUtils.GetAndroidABLoadPath(cRequest.url); var abInfo = ManifestManager.GetABInfo(cRequest.key); if (abInfo != null && abInfo.size < ResourcesLoader.asyncSize) { #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("AssetBundle.LoadFromFile:" + url); #endif assetBundle = AssetBundle.LoadFromFile(url); } else { #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("AssetBundle.LoadFromFileAsync:" + url); #endif m_abRequest = AssetBundle.LoadFromFileAsync(url); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif if (m_abRequest != null) { m_abRequest.priority = cRequest.priority; } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(cRequest.key, "<color=#15A0A1>1.4 AssetBundleDownloadFromDiskOperation begin Request(url={0},assetname={1},dependencies.count={3})keyHashCode{2},asyn={4},frameCount{5}</color>", cRequest.url, cRequest.assetName, cRequest.keyHashCode, cRequest.dependencies == null ? 0 : cRequest.dependencies.Length, cRequest.async, Time.frameCount); #endif }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req, string[] deps) { // string[] deps = ManifestManager.fileManifest.GetDirectDependencies (req.key); if (deps.Length == 0) { return(null); } string abName = string.Empty; string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; for (int i = 0; i < deps.Length; i++) { depAbName = CUtils.GetBaseName(deps[i]); dep_url = ManifestManager.RemapVariantName(depAbName); //string gc alloc keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { sharedCD.count++; int count = sharedCD.count; //CountMananger.WillAdd(keyhash); //引用数量加1; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #endif if (count == 1) //相加后为1,可能在回收列表,需要对所有依赖项目引用+1 { ABDelayUnloadManager.CheckRemove(keyhash); } } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> will add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.vUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; string[] deps1 = ManifestManager.fileManifest.GetDirectDependencies(item.key); if (deps1.Length > 0) { item.dependencies = LoadDependencies(req, deps1); } LoadAssetBundleInternal(item); } } return(hashs); }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req) { string[] deps = ManifestManager.fileManifest.GetDirectDependencies(req.assetBundleName); if (deps.Length == 0) { return(null); } #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadDependencies ({0},{1},{2}) new int[deps.Length]", req.assetName, req.key, req.isShared)); #endif string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif for (int i = 0; i < deps.Length; i++) { depAbName = deps[i]; if (string.IsNullOrEmpty(depAbName)) // Dependency assetbundle name is empty unity bug? { #if UNITY_EDITOR Debug.LogWarningFormat("the request({0},{1}) Dependencies {2} is empty ", req.assetName, req.url, i); #endif hashs[i] = 0; continue; } dep_url = ManifestManager.RemapVariantName(depAbName);//string gc alloc dep_url = depAbName; keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { int count = 0; #if HUGULA_CACHE_DEBUG count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(req.key + "," + dep_url, " <color=#fcfcfc> will add (assetBundle={0},hash={1},count={2}) frameCount{3}</color>", dep_url, keyhash, count, UnityEngine.Time.frameCount); #else count = CountMananger.WillAdd(keyhash); //引用数量加1 #endif bool dependenciesRefer = count == 1; //the cache count == 0 if (dependenciesRefer) { ABDelayUnloadManager.AddDependenciesReferCount(sharedCD, dependenciesRefer); } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, " <color=#15A0A1> 1.1 Shared AssetBundle is Done Request(assetName={0}) Parent Req(assetName={1},key={2},isShared={3}) </color>", sharedCD.assetBundleKey, req.assetName, req.key, req.isShared); #endif } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(req.key + "," + dep_url, " <color=#fcfcfc> will add (assetBundle={0},hash={1},count={2}) frameCount{3}</color>", dep_url, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; item.dependencies = LoadDependencies(item); item.uris = req.uris; #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>1.1 Load Dependencies Req({1},keyHashCode{2}) AssetBundleInternal Parent Request(assetname={0}), dependencies.count={3},frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.dependencies == null ? 0 : item.dependencies.Length, Time.frameCount); #endif LoadAssetBundleInternal(item); } } return(hashs); }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req, CRequest parent) { string[] deps = ManifestManager.fileManifest.GetDirectDependencies(req.assetBundleName); if (deps.Length == 0) { return(null); } string abName = string.Empty; if (parent != null) { abName = CUtils.GetBaseName(parent.assetBundleName); } #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadDependencies ({0},{1},{2}) new int[deps.Length]", req.assetName, req.key, req.isShared)); #endif string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif for (int i = 0; i < deps.Length; i++) { depAbName = CUtils.GetBaseName(deps[i]); if (abName == depAbName) { #if UNITY_EDITOR Debug.LogErrorFormat("Dependencies({1}) Contains the parent({0}) ! ", req.assetBundleName, abName); #else Debug.LogWarningFormat("Dependencies({1}) Contains the parent({0}) ! ", req.assetBundleName, abName); #endif hashs[i] = 0; continue; } dep_url = ManifestManager.RemapVariantName(depAbName);//string gc alloc keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { sharedCD.count++; int count = sharedCD.count;//CountMananger.WillAdd(keyhash); //引用数量加1; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #endif if (count == 1)//相加后为1,可能在回收列表,需要对所有依赖项目引用+1 { ABDelayUnloadManager.CheckRemove(keyhash); } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, " <color=#15A0A1> 1.1 Shared AssetBundle is Done Request(assetName={0}) Parent Req(assetName={1},key={2},isShared={3}) </color>", sharedCD.assetBundleKey, req.assetName, req.key, req.isShared); #endif } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> will add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; item.dependencies = LoadDependencies(item, req); item.uris = req.uris; #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>1.1 Load Dependencies Req({1},keyHashCode{2}) AssetBundleInternal Parent Request(assetname={0}), dependencies.count={3},frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.dependencies == null ? 0 : item.dependencies.Length, Time.frameCount); #endif LoadAssetBundleInternal(item); } } return(hashs); }
/// <summary> /// load assetbundle /// </summary> /// <param name="req"></param> static protected void LoadAssetBundle(CRequest req) { //Variants string relativeUrl = ManifestManager.RemapVariantName(req.relativeUrl); req.relativeUrl = relativeUrl; #if UNITY_EDITOR if (ManifestManager.SimulateAssetBundleInEditor) { //load asset ResourcesLoadOperation operation1; var tp1 = req.assetType; if (req.assetOperation != null) { operation1 = req.assetOperation; } else if (CacheManager.Typeof_ABScene.Equals(tp1)) { operation1 = new AssetBundleLoadLevelSimulationOperation(); operation1.SetRequest(req); } else { operation1 = new AssetBundleLoadAssetOperationSimulation(); operation1.SetRequest(req); } bool isLoading1 = false; if (operation1 is AssetBundleLoadAssetOperation) { isLoading1 = AddAssetBundleLoadAssetOperationToCallBackList((AssetBundleLoadAssetOperation)operation1); } if (!isLoading1) { inProgressOperations.Add(operation1); loadingTasks.Add(req); } return; } #endif //remove delay unload assetbundle ABDelayUnloadManager.CheckRemove(req.keyHashCode); //check load asset from cache if (LoadAssetFromCache(req)) { DispatchReqAssetOperation(req, false); return; } totalCount++;//count ++ #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadAssetBundle ({0},{1},{2}) LoadDependencies and LoadAssetBundleInternal", req.assetName, req.key, req.isShared)); #endif AssetBundleDownloadOperation abDownloadOperation = null; if (downloadingBundles.TryGetValue(req.key, out abDownloadOperation)) //check is loading { } else if (CheckAssetBundleCanLoad(req)) //need load { //load dependencies and refrenece count if (ManifestManager.fileManifest != null) { req.dependencies = LoadDependencies(req, null); //load dependencies assetbundle } //load assetbundle abDownloadOperation = LoadAssetBundleInternal(req); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadAssetBundle ({0},{1},{2}) to inProgressOperations", req.assetName, req.key, req.isShared)); #endif //load asset ResourcesLoadOperation operation; var tp = req.assetType; if (req.assetOperation != null) { operation = req.assetOperation; } else if (CacheManager.Typeof_ABScene.Equals(tp)) { operation = new AssetBundleLoadLevelOperation(); operation.SetRequest(req); } else { operation = AssetBundleLoadAssetOperationFull.Get(); operation.SetRequest(req); } bool isLoading = false; //the same asset be one Operation if (operation is AssetBundleLoadAssetOperation) { isLoading = AddAssetBundleLoadAssetOperationToCallBackList((AssetBundleLoadAssetOperation)operation); } if (!isLoading) { if (abDownloadOperation != null) { abDownloadOperation.AddNext(operation);//wait for assetbunle complete } else { inProgressOperations.Add(operation);// the assetbundle is done } loadingTasks.Add(req); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>2.0 LoadAssetBundle Asset Request(url={0},assetname={1},dependencies.count={3})keyHashCode{2}, frameCount{4}</color>", req.url, req.assetName, req.keyHashCode, req.dependencies == null ? 0 : req.dependencies.Length, Time.frameCount); #endif }