Ejemplo n.º 1
0
        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="data"></param>
        /// <param name="offset"></param>
        public void Read(byte[] data, ref int offset)
        {
            _cachedAssetInfos.Clear();
            _cachedAssetInfoMap.Clear();

            if (data == null)
            {
                return;
            }

            int dataLength = data.Length - offset;

            if (dataLength < 6)
            {
                return;
            }

            int storedDataLength = MemoryOperator.ReadInt(data, ref offset);

            if (storedDataLength < 6 || storedDataLength > dataLength)
            {
                return;
            }
            //
            int version = MemoryOperator.ReadShort(data, ref offset);

            if (version != Version)
            {
                return;
            }
            //信息数量
            int amount = MemoryOperator.ReadShort(data, ref offset);

            for (int i = 0; i < amount; i++)
            {
                NetAssetInfo info = new NetAssetInfo();
                info.Key       = MemoryOperator.ReadString(data, ref offset);
                info.AssetType = (NetAssetType)MemoryOperator.ReadShort(data, ref offset);
                if (info.AssetType == NetAssetType.Image)
                {
                    info.ImageWidth  = MemoryOperator.ReadShort(data, ref offset);
                    info.ImageHeight = MemoryOperator.ReadShort(data, ref offset);
                }
                //
                info.LastModifyTime = MemoryOperator.ReadDateTime(data, ref offset);
                //
                if (!_cachedAssetInfoMap.ContainsKey(info.Key))
                {
                    _cachedAssetInfoMap.Add(info.Key, info);
                    _cachedAssetInfos.Add(info);
                }
            }
            //排序
            _cachedAssetInfos.Sort();
        }
        private bool OnHandlerAdding(string eventType, Delegate handler)
        {
            bool result = true;

            if (!m_eventTable.ContainsKey(eventType))
            {
                m_eventTable.Add(eventType, null);
            }

            Delegate d = m_eventTable[eventType];

            if (d != null && d.GetType() != handler.GetType())
            {
                JW.Common.Log.LogE("There is no handler registered for event [" + eventType + "]!");
                result = false;
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 合并
        /// </summary>
        /// <param name="other"></param>
        public void MergeWithOther(ResPackConfig other)
        {
            if (_resourceMap == null || other == null)
            {
                return;
            }

            //先清理
            for (int i = 0; i < other.PackInfo.Count; ++i)
            {
                ResPackInfo resPackInfo = other.PackInfo[i];
                //
                for (int j = PackInfo.Count - 1; j >= 0; j--)
                {
                    if (PackInfo[j].Path == resPackInfo.Path)
                    {
                        PackInfo.RemoveAt(j);
                    }
                }
            }
            //添加
            for (int i = 0; i < other.PackInfo.Count; ++i)
            {
                ResPackInfo resPackInfo = other.PackInfo[i];
                PackInfo.Add(resPackInfo);

                List <ResInfo> resInfos = resPackInfo.Resources;
                for (int resIndex = 0; resIndex < resInfos.Count; ++resIndex)
                {
                    ResInfo res = resInfos[resIndex];
                    if (!_resourceMap.ContainsKey(res.Path))
                    {
                        _resourceMap.Add(res.Path, resPackInfo);
                    }
                    else
                    {
                        //Log.LogE("Pack Config {0} and {1}:{2}had same res : {3}", "Main", "Other", resPackInfo.Path, res.Path);
                        //替换
                        Log.LogD("Reload ResPackInfo:");
                        _resourceMap.Remove(res.Path);
                        _resourceMap.Add(res.Path, resPackInfo);
                    }
                }
            }

            //后处理下依赖
            for (int i = 0; i < PackInfo.Count; ++i)
            {
                ResPackInfo resPackInfo = PackInfo[i];
                if (resPackInfo != null)
                {
                    resPackInfo.ProcessDependency(this.PackInfo);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 移除资源对象
        /// </summary>
        /// <param name="resource">资源对象</param>
        /// <param name="immediately">是否立即删除</param>
        /// <returns>true:表示所有同路径资源都已移除完</returns>
        public bool Remove(ResObj resource, bool immediately)
        {
            if (resource == null)
            {
                return(true);
            }

            if (_resources.ContainsKey(resource.OriginPath))
            {
                if (immediately)
                {
                    resource.Unload();
                    _resources.Remove(resource.OriginPath);
                    ResObj.Recycle(resource);

                    return(true);
                }
                else
                {
                    if (resource.RefCnt > 0)
                    {
                        resource.RefCnt--;
                    }
                    else
                    {
                        JW.Common.Log.LogE("Unloading resource [{0}] while its reference count is zero.", resource.Path);
                    }

                    return(resource.RefCnt <= 0);
                }
            }
            else
            {
                resource.Unload();
                ResObj.Recycle(resource);
                return(true);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 注册游戏状态
 /// </summary>
 /// <param name="name"></param>
 /// <param name="state"></param>
 public void RegisteState(string name, IState state)
 {
     if (_registedState == null)
     {
         _registedState = new JWObjDictionary <string, IState>();
     }
     if (_registedState.ContainsKey(name))
     {
         JW.Common.Log.LogE("StateService:Already Registed:" + name);
         return;
     }
     if (state != null)
     {
         state.InitializeState();
     }
     _registedState.Add(name, state);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 批量并发加载bundle
        /// </summary>
        /// <param name="bundleList">bundle打包信息列表</param>
        /// <param name="complete">全部加载完成回调</param>
        /// <param name="progress">加载进度,0-1</param>
        /// <returns></returns>
        public IEnumerator BatchLoadAsync(JWObjList <BundlePackInfo> bundleList, Action complete, BundleBatchLoadingDelegate progress = null, BundleLoadedDelegate loaded = null, BundleLoadFailedDelegate failure = null)
        {
            if (bundleList == null || bundleList.Count == 0)
            {
                if (progress != null)
                {
                    progress(1f);
                }

                if (complete != null)
                {
                    complete();
                }

                yield break;
            }

            // 并发加载多个bundle
            JWObjList <string>         failedBundles = null;
            Dictionary <string, float> progressDict  = new Dictionary <string, float>(bundleList.Count); // 因为是并发,所以计算进度稍微复杂一些,需要每个bundle的进度收集计算
            float step = 1f / bundleList.Count;                                                          // 大致平分每个bundle的加载时间

            for (int i = 0; i < bundleList.Count; i++)
            {
                BundlePackInfo bundleInfo = bundleList[i];
                StartCoroutine(LoadAsync(bundleInfo, delegate(BundleRef bundleRef)
                {
                    // complete one
                    if (bundleRef != null)
                    {
                        // 已加载完成某一个bundle
                        if (loaded != null)
                        {
                            loaded(bundleRef);
                        }

                        if (progressDict.ContainsKey(bundleRef.Path))
                        {
                            progressDict[bundleRef.Path] = 1f;
                        }
                        else
                        {
                            progressDict.Add(bundleRef.Path, 1f);
                        }
                    }
                }, delegate(BundlePackInfo packInfo, string error)
                {
                    if (failedBundles == null)
                    {
                        failedBundles = new JWObjList <string>();
                    }

                    if (!failedBundles.Contains(packInfo.Path))
                    {
                        failedBundles.Add(packInfo.Path);
                    }

                    if (failure != null)
                    {
                        failure(packInfo, error);
                    }
                }, delegate(BundlePackInfo packInfo, float prog)
                {
                    // 加载进度回调,会有多次
                    if (progress != null)
                    {
                        // 记录每次每个bundle的进度
                        if (progressDict.ContainsKey(packInfo.Path))
                        {
                            progressDict[packInfo.Path] = prog;
                        }
                        else
                        {
                            progressDict.Add(packInfo.Path, prog);
                        }

                        // 计算总进度
                        float totalProgress = 0;
                        for (int j = 0; j < bundleList.Count; j++)
                        {
                            float eachProg = 0;
                            if (progressDict.TryGetValue(bundleList[j].Path, out eachProg))
                            {
                                totalProgress += eachProg * step;
                            }
                        }

                        progress(totalProgress);
                    }
                }));
            }

            // 检查是否全部完成
            while (true)
            {
                bool completed = true;
                for (int i = 0; i < bundleList.Count; i++)
                {
                    BundlePackInfo bundleInfo = bundleList[i];
                    if (_bundleDict.ContainsKey(bundleInfo.Path))
                    {
                        continue;
                    }

                    if (failedBundles != null && failedBundles.Contains(bundleInfo.Path))
                    {
                        continue;
                    }

                    completed = false;
                    yield return(null);

                    break;
                }

                if (completed)
                {
                    if (complete != null)
                    {
                        complete();
                    }

                    yield break;
                }
            }
        }