/// <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(); }
/// <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); } } }
/// <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); }
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); }
/// <summary> /// 添加资源到缓存 /// </summary> /// <param name="path">资源路径</param> /// <returns>创建的资源对象</returns> public ResObj Add(string path) { ResObj resource; if (_resources.TryGetValue(path, out resource)) { if (resource.RefCnt == 0) { resource.UsingCache = true; } resource.RefCnt++; } else { resource = ResObj.Get(path); _resources.Add(path, resource); } return(resource); }
/// 下载更新文件 ToDO优化 IEnumerator DoDownloadDiffFileList() { if ((_diffFileList != null) && (_diffFileList.FileList.Count > 0)) { _diffTotalCnt = _diffFileList.FileList.Count; _diffDownloadedCnt = 0; string ifsDir = JW.Res.FileUtil.GetIFSExtractPath(); for (int i = 0; i < _diffFileList.FileList.Count; i++) { string url = _curSession.NetFileRootUrl + _diffFileList.FileList[i].FileName; string localPath = JW.Res.FileUtil.CombinePath(ifsDir, _diffFileList.FileList[i].FileName); uint sid = HttpService.GetInstance().RegisteFileHttpDownload(url, localPath, OnDiffFileDownloaded); if (_diffDownloadInfo == null) { _diffDownloadInfo = new JWObjDictionary <uint, string>(_diffFileList.FileList.Count); } else { _diffDownloadInfo.Clear(); } _diffDownloadInfo.Add(sid, _diffFileList.FileList[i].FileName); } // while (_diffDownloadInfo.Count > 0) { CallSessionHandler(IFSState.DownloadingDiff, (float)_diffDownloadedCnt / _diffTotalCnt); yield return(null); } // CallSessionHandler(IFSState.DownloadDiffSuccess, 1.0f); } else { CallSessionHandler(IFSState.DownloadDiffSuccess, 1.0f); } // _diffDownloadInfo = null; yield return(null); }
/// <summary> /// 同步加载bundle /// </summary> /// <param name="packInfo">bundle打包信息</param> public void LoadSync(BundlePackInfo packInfo) { if (packInfo == null) { JW.Common.Log.LogE("Sync loading bundle with null pack info."); return; } //是否已经加载 BundleRef br = null; if (_bundleDict.TryGetValue(packInfo.Path, out br)) { br.RefCnt++; if (_unloadingBundle.ContainsKey(packInfo.Path)) { _unloadingBundle.Remove(packInfo.Path); } return; } // 是否正在加载 if (_loadingBundle.Contains(packInfo.Path)) { JW.Common.Log.LogE("Bundle is loading when load sync. bundle:{0}", packInfo.Path); return; } // 依赖 for (int i = 0; packInfo.Dependencies != null && i < packInfo.Dependencies.Count; i++) { LoadSync(packInfo.Dependencies[i]); } //干活 AssetBundle ab = null; string path = GetBundleFullPath(packInfo, false); JW.Common.Log.LogD("Sync load bundle path:{0}", path); // if (packInfo.HasFlag(EBundleFlag.UnCompress) || packInfo.HasFlag(EBundleFlag.LZ4)) { ab = AssetBundle.LoadFromFile(path); } else { //LZMA 压缩的AssetBundle //LZMA压缩的 u2017可以直接通过 LoadFromFile 创建支持StreamingAssetPath 内存占用大 ab = AssetBundle.LoadFromFile(path); /* * byte[] bytes = null; * //优先外围目录 * if (FileUtil.IsExistInIFSExtraFolder(packInfo.Path)) * { * bytes = File.ReadAllBytes(path); * } * else * { * * //StreamingAsset目录的 #if UNITY_ANDROID && !UNITY_EDITOR * bytes = NativeHelper.Android_ReadFileInAssets(packInfo.Path); #else * bytes = File.ReadAllBytes(path); #endif * } * * if (null != bytes) * { * //注意 * //https://unity3d.com/cn/learn/tutorials/topics/best-practices/assetbundle-fundamentals?playlist=30089 * //The peak amount of memory consumed by this API will be at least twice the size of the AssetBundle: * ab = AssetBundle.LoadFromMemory(bytes); * }*/ } // 完成 if (ab != null) { br = new BundleRef(packInfo); br.Bundle = ab; _bundleDict.Add(packInfo.Path, br); } }