/// <summary> /// 读取配置初始化 包列表信息 /// </summary> /// <param name="data"></param> /// <param name="offset"></param> public void Read(byte[] data, ref int offset, bool procDependency = false) { int count = MemoryOperator.ReadShort(data, ref offset); int resCount = 0; for (int i = 0; i < count; i++) { ResPackInfo packInfo = CreatePackInfo(data, ref offset); if (packInfo != null) { packInfo.Read(data, ref offset); if (procDependency) { packInfo.ProcessDependency(PackInfo); } PackInfo.Add(packInfo); resCount += packInfo.Resources.Count; } } if (_resourceMap == null) { _resourceMap = new JWObjDictionary <string, ResPackInfo>(resCount, StringComparer.OrdinalIgnoreCase); } for (int i = 0; i < PackInfo.Count; ++i) { AddToMap(PackInfo[i]); } }
/// <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="ResObj">资源实例</param> private void LoadResource(ResObj resource) { //找配置 ResPackInfo packInfo = GetResPackInfo(resource); if (packInfo != null) { if (packInfo.GetPackType() == (byte)ResPackType.ResPackTypeBundle) { BundlePackInfo pi = packInfo as BundlePackInfo; //跟随Resource if (!pi.Outside && pi.IsNoBundle()) { // load from Resources resource.Load(); } else { // load from bundle // load bundle BundleService.GetInstance().LoadSync(pi); // load asset from bundle AssetBundle bundle = BundleService.GetInstance().GetBundle(packInfo.Path); if (bundle != null) { if (string.IsNullOrEmpty(resource.Ext)) { resource.Ext = pi.GetResExt(resource.Path); } resource.Load(bundle); } else { JW.Common.Log.LogE("Loading bundle failed for path:{0}, bundle path:{1}", resource.Path, packInfo.Path); } } } else { //二进制类型 直接根据文件路径 获取 resource.Load(packInfo.Path, packInfo.IsOutside(resource.Path)); } } else { //不从属于任何资源包, 从Resources目录下读取 Editor模式 或者保护为未打包的 resource.Load(); } }
/// <summary> /// 添加资源映射 /// </summary> /// <param name="packInfo"></param> void AddToMap(ResPackInfo packInfo) { for (int i = 0; i < packInfo.Resources.Count; i++) { ResInfo r = packInfo.Resources[i]; try { _resourceMap.Add(r.Path, packInfo); } catch (Exception e) { JW.Common.Log.LogE("Add {0} to map error:{1}", r.Path, e.Message); } } }
/// <summary> /// ͨ获取资源对应的包信息 /// </summary> /// <param name="resourcePath">资源路径</param> /// <returns></returns> public ResPackInfo GetPackInfoForResource(string resourcePath) { if (string.IsNullOrEmpty(resourcePath)) { return(null); } ResPackInfo packInfo = null; if (_resourceMap != null) { _resourceMap.TryGetValue(resourcePath, out packInfo); } return(packInfo); }
/// <summary> /// 添加包信息 /// </summary> /// <param name="packInfo"></param> public void AddPackInfo(ResPackInfo packInfo) { if (packInfo == null) { return; } if (_resourceMap == null) { _resourceMap = new JWObjDictionary <string, ResPackInfo>(StringComparer.OrdinalIgnoreCase); } packInfo.ProcessDependency(PackInfo); PackInfo.Add(packInfo); AddToMap(packInfo); }
//判断 public bool IsBinaryResourceExsitInPkg(string Path) { string noExtPath = FileUtil.EraseExtension(Path); ResPackInfo packInfo = GetResPackInfo(noExtPath); if (packInfo != null) { if (packInfo.GetPackType() == (byte)ResPackType.ResPackTypeBinary) { string realPath = FileUtil.CombinePath(packInfo.Path, Path); if (FileUtil.IsExistInIFSExtraFolder(realPath) || FileUtil.IsFileExistInStreamingAssets(realPath)) { return(true); } } } return(false); }
public void ReloadDataBytes(byte[] bbs) { if (bbs == null || bbs.Length == 0) { JW.Common.Log.LogE("ResPackConfig ReloadDataBytes Error No Bytes"); return; } PackInfo.Clear(); _resourceMap.Clear(); int offset = 0; Read(bbs, ref offset); //后处理下依赖 for (int i = 0; i < PackInfo.Count; ++i) { ResPackInfo resPackInfo = PackInfo[i]; if (resPackInfo != null) { resPackInfo.ProcessDependency(this.PackInfo); } } }
//根据二进制初始化 public ResPackConfig(byte[] bbs, bool isSub = false) { if (bbs == null || bbs.Length == 0) { JW.Common.Log.LogE("Init ResPackConfig Error No Bytes"); return; } int offset = 0; Read(bbs, ref offset); if (!isSub) { //后处理下依赖 for (int i = 0; i < PackInfo.Count; ++i) { ResPackInfo resPackInfo = PackInfo[i]; if (resPackInfo != null) { resPackInfo.ProcessDependency(this.PackInfo); } } } }
/// <summary> /// 异步加载资源 /// </summary> /// <param name="path">资源路径</param> /// <returns></returns> public ResObjRequest GetResourceAsync(string path) { if (string.IsNullOrEmpty(path)) { return(null); } // request ResObjRequest rr = new ResObjRequest(); // complete loading Action <ResObj> complete = (ResObj r) => { // try repaire //if (r.Content is GameObject) //{ // _repaireMachine.Repaire(r.Content as GameObject); //} rr.isDone = true; rr.resource = r; }; // loading progress Action <float> progress = (float prog) => { rr.progress = prog; }; // cache ResObj resource = _resCache.Add(path); if (resource.Content != null) { //cache包含直接返回 complete(resource); return(rr); } // real loading ResPackInfo packInfo = GetResPackInfo(resource); if (packInfo != null) { //打包的资源AssetBundle if (packInfo.GetPackType() == (byte)ResPackType.ResPackTypeBundle) { //AssetBundle 信息 BundlePackInfo pi = packInfo as BundlePackInfo; //补充扩展名 if (string.IsNullOrEmpty(resource.Ext)) { resource.Ext = pi.GetResExt(resource.Path); } // AssetBundle 缓存 AssetBundle bundle = BundleService.GetInstance().GetBundle(packInfo.Path); if (bundle == null) { //不在外且跟随Resource出档了 if (!pi.Outside && pi.IsNoBundle()) { // in Resources StartCoroutine(resource.LoadAsync(complete, progress)); } else { // load bundle first StartCoroutine(BundleService.GetInstance().LoadAsync(pi, delegate(BundleRef bundleRef) { if (bundleRef != null && bundleRef.Bundle != null) { bundle = bundleRef.Bundle; //从asset bundle 加载 StartCoroutine(resource.LoadAsync(bundle, complete, progress)); } else { JW.Common.Log.LogE("Async loading bundle failed, resource:{0}, bundle:{1}", path, packInfo.Path); complete(null); _resCache.Remove(resource, true); } }, delegate(BundlePackInfo failedPackInfo, string error) { JW.Common.Log.LogE("Load bundle failed, error:{0}", error); complete(null); _resCache.Remove(resource, true); })); } } else { // bundle is exists // 增加引用计数 BundleService.GetInstance().LoadSync(pi); // 加载 StartCoroutine(resource.LoadAsync(bundle, complete, progress)); } } else { //二进制文件包异步获取 StartCoroutine(resource.LoadAsync(packInfo.Path, packInfo.IsOutside(resource.Path), complete, progress)); } } else { // 非IFS 打包资源,从Resources目录异步读取 //JW.Common.Log.LogD("Load From Resource Async:"+ path); StartCoroutine(resource.LoadAsync(complete, progress)); } return(rr); }