/// <summary> /// Load AssetBundle Error /// </summary> /// <param name="req">Req.</param> static void LoadError(CCar cloader, CRequest req) { downloadings.Remove(req.udKey); #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 1.3 <color=red>CResLoader.LoadError Request(assetName={0}, url={1},isShared={2} req.index={3},req.uris.count={4})</color>", req.assetName, req.url, req.isShared, req.index, req.uris == null ? 0 : req.uris.count); #endif if (req.uris != null && req.index < req.uris.count - 1 && UriGroup.CheckAndSetNextUriGroup(req)) // CUtils.SetRequestUri(req, req.index)) { QueueOrLoad(req); } else { if (!req.isShared) { currentLoaded++; } CallbackError(req); if (!req.isShared && !string.IsNullOrEmpty(req.assetBundleName) && _instance != null && _instance.OnAssetBundleErr != null) { _instance.OnAssetBundleErr(req); } BeginQueue(); CheckAllComplete(); } }
static void LoadError(CCar cloader, CRequest req) { string oldUdKey = req.udKey; downloadings.Remove(oldUdKey); req.index++; #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 2.<color=green>CResLoader.LoadError Request(assetName={0}, url={1},isShared={2})</color>", req.assetName, req.url, req.isShared); #endif if (req.index < req.uris.count && req.uris.SetNextUri(req))// CUtils.SetRequestUri(req, req.index)) { List <CRequest> callbacklist = null; if (requestCallBackList.TryGetValue(oldUdKey, out callbacklist)) { requestCallBackList.Remove(oldUdKey); } var udkey = req.udKey; if (callbacklist == null) { callbacklist = ListPool <CRequest> .Get();//new List<CRequest>(); } if (requestCallBackList.ContainsKey(udkey)) { //if 正在加载 var old = requestCallBackList[udkey]; CRequest chekReq = null; for (int i = 0; i < callbacklist.Count; i++) { chekReq = callbacklist[i]; if (!old.Contains(chekReq)) { old.Add(chekReq); } } } else { if (!callbacklist.Contains(req)) { callbacklist.Add(req); } requestCallBackList[udkey] = callbacklist; QueueOrLoad(req); //realyLoadingQueue.Enqueue(req); } } else { if (!req.isShared) { currentLoaded++; } CallbackError(req); if (!req.isShared && !string.IsNullOrEmpty(req.assetBundleName) && _instance != null && _instance.OnAssetBundleErr != null) { _instance.OnAssetBundleErr(req); } BeginQueue(); CheckAllComplete(); } }
static public int set_OnProcess(IntPtr l) { try { Hugula.Loader.CCar self = (Hugula.Loader.CCar)checkSelf(l); System.Action <Hugula.Loader.CCar, System.Single> v; int op = LuaDelegation.checkDelegate(l, 2, out v); if (op == 0) { self.OnProcess = v; } else if (op == 1) { self.OnProcess += v; } else if (op == 2) { self.OnProcess -= v; } pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int get_enabled(IntPtr l) { try { Hugula.Loader.CCar self = (Hugula.Loader.CCar)checkSelf(l); pushValue(l, true); pushValue(l, self.enabled); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int StopLoad(IntPtr l) { try { Hugula.Loader.CCar self = (Hugula.Loader.CCar)checkSelf(l); self.StopLoad(); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int constructor(IntPtr l) { try { Hugula.Loader.CCar o; o = new Hugula.Loader.CCar(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
/// <summary> /// 排队或者加载 /// </summary> /// <param name="req">Req.</param> static void QueueOrLoad(CRequest req) { if (req.isNormal) { realyLoadingQueue.Enqueue(req); } else { CCar car = GetCCar(); freeLoader.Add(car); car.BeginLoad(req); } }
static public int BeginLoad(IntPtr l) { try { Hugula.Loader.CCar self = (Hugula.Loader.CCar)checkSelf(l); Hugula.Loader.CRequest a1; checkType(l, 2, out a1); self.BeginLoad(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int set_enabled(IntPtr l) { try { Hugula.Loader.CCar self = (Hugula.Loader.CCar)checkSelf(l); System.Boolean v; checkType(l, 2, out v); self.enabled = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
/// <summary> /// 排队或者加载 /// </summary> /// <param name="req">Req.</param> static void QueueOrLoad(CRequest req) { #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 0.2 <color=#15A0A1>QueueOrLoad Request(assetName={0}, url={1},isNormal={2},isShared={3})</color>", req.assetName, req.url, req.isNormal, req.isShared); #endif if (req.isNormal) { realyLoadingQueue.Enqueue(req); } else { CCar car = GetCCar(); freeLoader.Add(car); car.BeginLoad(req); } }
/// <summary> /// assetbunlde load complete /// </summary> /// <param name="loader">loader.</param> /// <param name="req">Req.</param> static void LoadComplete(CCar loader, CRequest creq) { #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 1.3 <color=#15C1B2>CResLoader.LoadComplete Request(assetName={0}, hash={1},isShared={2})</color>", creq.assetName, creq.keyHashCode, creq.isShared); #endif downloadings.Remove(creq.udKey); CallbackAsyncList(creq); if (!creq.isShared || !creq.isNormal) //共享和非普通都不计数 { currentLoaded++; } if (!creq.isShared && creq.isAssetBundle && _instance != null && _instance.OnAssetBundleComplete != null) { _instance.OnAssetBundleComplete(creq); } }
static void OnProcess(CCar loader, float progress) { var loadingEvent = _instance.loadingEvent; loadingEvent.target = _instance; if (totalLoading <= 0) { loadingEvent.total = 1; } else { loadingEvent.total = totalLoading; } if (loadingEvent.current < currentLoaded) { loadingEvent.current = currentLoaded; } loadingEvent.progress = progress; if (_instance && _instance.OnProgress != null && totalLoading > 0) { _instance.OnProgress(loadingEvent); } }
/// <summary> /// 获取空闲的loader /// </summary> /// <returns></returns> internal static void CreateFreeLoader() { int dtCount = loaderPool.Count - maxLoading; //计算差值 if (dtCount < 0) { for (int i = dtCount; i <= -1; i++) { CCar cts = GetCCar(); //new CCar (); loaderPool.Add(cts); } } else if (dtCount > 0) { for (int i = dtCount; i >= 1; i--) { int rIndex = loaderPool.Count - 1; CCar cts = loaderPool[rIndex]; loaderPool.RemoveAt(rIndex); freeLoader.Add(cts); } } // Debug.LogFormat(" count = {0},dtCount {1} maxLoading {2}",loaderPool.Count,dtCount,maxLoading); }
// Update is called once per frame void Update() { //加载 for (int i = 0; i < loaderPool.Count; i++) { CCar load = loaderPool[i]; if (load.isFree && realyLoadingQueue.Count > 0) { var req = realyLoadingQueue.Dequeue(); if (!CheckLoadAssetBundle(req)) { downloadings[req.udKey] = true; load.BeginLoad(req); } } if (load.enabled) { load.Update(); } } for (int i = 0; i < freeLoader.Count;) { CCar load = freeLoader [i]; if (load.enabled) { load.Update(); } if (load.isFree) { #if HUGULA_LOADER_DEBUG if (load.req != null) { Debug.LogFormat(" 1. a <color=yellow>freeLoader is free Req(assetname={0},url={1}) frameCount{2}</color>", load.req.assetName, load.req.url, Time.frameCount); } else { Debug.LogFormat(" 1. a <color=yellow>freeLoader is free Req(assetname={0},url={1}) frameCount{2}</color>", load, load, Time.frameCount); } #endif freeLoader.RemoveAt(i); ReleaseCCar(load); //放回对象池 } else { i++; } } //ab for (int i = 0; i < loadingAssetBundleQueue.Count;) { var item = loadingAssetBundleQueue[i]; if (CacheManager.CheckDependenciesComplete(item)) //判断依赖项目是否加载完成 { CacheManager.SetRequestDataFromCache(item); //设置缓存数据。 loadingAssetQueue.Add(item); if (item.assetBundleRequest != null) { CacheManager.AddLock(item.keyHashCode); //异步需要锁定 } loadingAssetBundleQueue.RemoveAt(i); #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 4. <color=yellow>DependenciesComplete Req(assetname={0},url={1}) frameCount{2}</color>", item.assetName, item.url, Time.frameCount); #endif } else { i++; #if HUGULA_LOADER_DEBUG Debug.LogFormat(" -4. <color=yellow>CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2}</color>", item.assetName, item.url, Time.frameCount); #endif } } //asset for (int i = 0; i < loadingAssetQueue.Count; i++) { var item = loadingAssetQueue[i]; if (item.assetBundleRequest != null && item.assetBundleRequest.isDone) //如果加载完成 { if (item.assetBundleRequest is AssetBundleRequest) { item.data = ((AssetBundleRequest)item.assetBundleRequest).asset;//赋值 } else { item.data = item.assetBundleRequest; } #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 5. <color=yellow>set Req(assetname={0},url={1}).data asnyc Count{2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount); #endif loadedAssetQueue.Enqueue(item); loadingAssetQueue.RemoveAt(i); } else if (item.assetBundleRequest == null) //非异步 { loadedAssetQueue.Enqueue(item); loadingAssetQueue.RemoveAt(i); #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 5. <color=yellow>set Req(assetname={0},url={1}).data Count{2} frameCount{3}</color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount); #endif } else { i++; } } while (loadedAssetQueue.Count > 0) { LoadAssetComplate(loadedAssetQueue.Dequeue()); } //组进度条 for (int i = 0; i < allGroupRequestRecord.Count;) { var group = allGroupRequestRecord [i]; group.Progress(); if (group.Count > 0) { i++; } else { var act = group.onGroupComplate; GroupRequestRecordPool.Release(group); allGroupRequestRecord.RemoveAt(i); if (act != null) { act(group); } } } }
/// <summary> /// 获取空闲的loader /// </summary> /// <returns></returns> public static void CreateFreeLoader() { for (int i = loaderPool.Count; i < maxLoading; i++) { CCar cts = new CCar(); loaderPool.Add(cts); cts.OnComplete = LoadComplete; cts.OnError = LoadError; cts.OnProcess = OnProcess; } }
static void LoadComplete(CCar loader, CRequest creq) { //Debug.LogFormat(" <color=green>LoadComplete {0} {1}</color>", creq.key, creq.isShared); downloadings.Remove(creq.udKey); if (creq.isShared) { //添加引用计数 RemoveReferenceRecord(creq); //回调 if (_instance && _instance.OnSharedComplete != null) _instance.OnSharedComplete(creq); } else { CallbackAsyncList(creq); } }
static void LoadError(CCar cloader, CRequest req) { string oldUdKey = req.udKey; downloadings.Remove(oldUdKey); req.index++; if (req.index < req.maxTimes && CUtils.SetRequestUri(req, req.index)) { //req.priority = req.priority - 10; List<CRequest> callbacklist = null; if (requestCallBackList.TryGetValue(oldUdKey, out callbacklist)) { requestCallBackList.Remove(oldUdKey); } var udkey = req.udKey; if (callbacklist == null) callbacklist = new List<CRequest>(); if (requestCallBackList.ContainsKey(udkey)) { //if 正在加载 var old = requestCallBackList[udkey]; CRequest chekReq = null; for (int i = 0; i < callbacklist.Count; i++) { chekReq = callbacklist[i]; if (!old.Contains(chekReq)) old.Add(chekReq); } } else { if (!callbacklist.Contains(req)) callbacklist.Add(req); requestCallBackList[udkey] = callbacklist; realyLoadingQueue.Enqueue(req); } } else { CallbackError(req); BeginQueue(); CheckAllComplete(); } }
// Update is called once per frame void Update() { for (int i = 0; i < freeLoader.Count;) { CCar load = freeLoader[i]; if (load.enabled) { load.Update(); } if (load.isFree) { freeLoader.RemoveAt(i); ReleaseCCar(load); //放回对象池 } else { i++; } } //1 load assetbunlde for (int i = 0; i < loaderPool.Count; i++) { CCar load = loaderPool[i]; if (load.isFree && realyLoadingQueue.Count > 0) { var req = realyLoadingQueue.Dequeue(); if (CheckLoadedAssetBundle(req)) { CheckLoadAssetAsync(req); } else if (CheckLoadingAssetBundle(req)) { AddReqToQueue(req); } else { downloadings[req.udKey] = load; load.BeginLoad(req); } } if (load.enabled) { load.Update(); } } #if UNITY_EDITOR loadingAssetBundleQueueCount = loadingAssetBundleQueue.Count; loadingAssetQueueCount = loadingAssetQueue.Count; DebugInfo = ""; #endif //2 wait dependencies set asset for (int i = 0; i < loadingAssetBundleQueue.Count;) { var item = loadingAssetBundleQueue[i]; if (CacheManager.CheckDependenciesComplete(item)) //判断依赖项目是否加载完成 { if (CacheManager.SetRequestDataFromCache(item)) //设置缓存数据。 { loadingAssetQueue.Add(item); //加载资源 } else { loadedAssetQueue.Enqueue(item); #if HUGULA_LOADER_DEBUG Debug.LogFormat("<color=red> 2.2 SetRequestDataFromCache false Req(assetname={0},url={1}) frame={2}loadingAssetBundleQueue.Count={3} </color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count); #endif } // if (item.assetBundleRequest != null) // CacheManager.AddLock (item.keyHashCode);//异步需要锁定 loadingAssetBundleQueue.RemoveAt(i); #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 2.3 <color=#15C132>DependenciesComplete Req(assetname={0},url={1},async={2}) frameCount{3},loadingAssetBundleQueue.Count={4}</color>", item.assetName, item.url, item.async, Time.frameCount, loadingAssetBundleQueue.Count); #endif } else { i++; #if UNITY_EDITOR DebugInfo += string.Format(" -2.3CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2},loadingAssetBundleQueue.Count={3}</color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count); #endif #if HUGULA_LOADER_DEBUG Debug.LogFormat(" -2.3 <color=#15C132>CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2},loadingAssetBundleQueue.Count={3}</color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count); #endif } } //3 load asset for (int i = 0; i < loadingAssetQueue.Count; i++) { var item = loadingAssetQueue[i]; #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 3.2.-1 <color=#A9C115> Req(assetname={0},url={1}) loadingAssetQueue.Count={2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount); #endif if (item.assetBundleRequest != null && item.assetBundleRequest.isDone) //如果加载完成 { #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 3.2.0 <color=#A9C115>set Req(assetname={0},url={1}).data asnyc Count{2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount); #endif if (item.assetBundleRequest is AssetBundleRequest) { if (CacheManager.Typeof_ABAllAssets.Equals(item.assetType)) { item.data = ((AssetBundleRequest)item.assetBundleRequest).allAssets; //赋值 } else { item.data = ((AssetBundleRequest)item.assetBundleRequest).asset; //赋值 } } else { item.data = item.assetBundleRequest; } #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 3.2.1 <color=#A9C115>set Req(assetname={0},url={1} data{2}) end </color>", item.assetName, item.url, item.data); #endif loadedAssetQueue.Enqueue(item); loadingAssetQueue.RemoveAt(i); } else if (item.assetBundleRequest == null) //非异步 { loadedAssetQueue.Enqueue(item); loadingAssetQueue.RemoveAt(i); #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 3.2 <color=#A9C115>set Req(assetname={0},url={1}).data Async Count{2} frameCount{3}</color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount); #endif } else { i++; } } //4 complete asset while (loadedAssetQueue.Count > 0) { LoadAssetComplate(loadedAssetQueue.Dequeue()); } //group check for (int i = 0; i < allGroupRequestRecord.Count;) { var group = allGroupRequestRecord[i]; group.Progress(); if (group.Count > 0) { i++; } else { var act = group.onGroupComplate; GroupRequestRecord.Release(group); allGroupRequestRecord.RemoveAt(i); if (act != null) { act(group); } } } }
public void StopAll() { List <CRequest> removeReqs = ListPool <CRequest> .Get(); CRequest req = null; for (int i = 0; i < queue.Count;) { req = queue[i]; queue.RemoveAt(i); removeReqs.Add(req); } // step 2 remove loading assetbundle CCar load = null; foreach (var kv in downloadings) { load = kv.Value; removeReqs.Add(load.req); load.StopLoad(); } downloadings.Clear(); for (int i = 0; i < loadingAssetBundleQueue.Count;) { req = loadingAssetBundleQueue[i]; loadingAssetBundleQueue.RemoveAt(i); removeReqs.Add(req); } //step 3 remove loadingAssetQueue for (int i = 0; i < loadingAssetQueue.Count;) { req = loadingAssetQueue[i]; loadingAssetQueue.RemoveAt(i); removeReqs.Add(req); } //step 4 remove loadedAssetQueue while (loadedAssetQueue.Count > 0) { req = loadedAssetQueue.Dequeue(); removeReqs.Add(req); } //remove from requestCallBackList List <CRequest> callbacklist = null; foreach (var kv in requestCallBackList) { callbacklist = kv.Value; ListPool <CRequest> .Release(callbacklist); } foreach (var kv in assetCallBackList) { callbacklist = kv.Value; ListPool <CRequest> .Release(callbacklist); } for (int i = 0; i < removeReqs.Count; i++) { req = removeReqs[i]; PopGroup(req); } ListPool <CRequest> .Release(removeReqs); }
/// <summary> /// stop the req. /// </summary> /// <param name="req">Req.</param> /// <param name="group">Group.</param> public void StopReq(CRequest reqUrl) { string udKey = reqUrl.udKey; string udAssetKey = reqUrl.udAssetKey; //step 1 remove request list List <CRequest> removeReqs = ListPool <CRequest> .Get(); CRequest req; for (int i = 0; i < queue.Count;) { req = queue[i]; if (req.udKey.Equals(udKey)) { queue.RemoveAt(i); removeReqs.Add(req); } else { i++; } } // step 2 remove loading assetbundle CCar load = null; if (downloadings.TryGetValue(udKey, out load)) { removeReqs.Add(load.req); load.StopLoad(); downloadings.Remove(udKey); } for (int i = 0; i < loadingAssetBundleQueue.Count;) { req = loadingAssetBundleQueue[i]; if (req.udKey.Equals(udKey)) { loadingAssetBundleQueue.RemoveAt(i); removeReqs.Add(req); } else { i++; } } //step 3 remove loadingAssetQueue for (int i = 0; i < loadingAssetQueue.Count;) { req = loadingAssetQueue[i]; if (req.udKey.Equals(udKey)) { loadingAssetQueue.RemoveAt(i); removeReqs.Add(req); } else { i++; } } //step 4 remove loadedAssetQueue // while (loadedAssetQueue.Count > 0) // { // req = loadedAssetQueue.Dequeue(); // removeReqs.Add(req); // } //remove from requestCallBackList List <CRequest> callbacklist = null; if (requestCallBackList.TryGetValue(udKey, out callbacklist)) { requestCallBackList.Remove(udKey); ListPool <CRequest> .Release(callbacklist); } if (assetCallBackList.TryGetValue(udAssetKey, out callbacklist)) { assetCallBackList.Remove(udAssetKey); ListPool <CRequest> .Release(callbacklist); } for (int i = 0; i < removeReqs.Count; i++) { req = removeReqs[i]; PopGroup(req); } ListPool <CRequest> .Release(removeReqs); }
static void ReleaseCCar(CCar car) { freeLoaderPool.Release(car); }
static void m_CCarOnRelease(CCar re) { //re.isFree = true; }
static void m_CCarOnGet(CCar cts) { cts.OnComplete = LoadComplete; cts.OnError = LoadError; cts.OnProcess = OnProcess; }
static void OnProcess(CCar loader, float progress) { var loadingEvent = _instance.loadingEvent; loadingEvent.target = _instance; if (totalLoading <= 0) loadingEvent.total = 1; else loadingEvent.total = totalLoading; if (loadingEvent.current < currentLoaded) loadingEvent.current = currentLoaded; loadingEvent.progress = progress; if (_instance && _instance.OnProgress != null && totalLoading > 0) { _instance.OnProgress(_instance, loadingEvent); } }