/// <summary> /// 直接添加信息 /// </summary> /// <param name="key"></param> public void AddCachedNetAssetInfoDirect(string key) { NetAssetInfo info = _cachedNetAssetInfoSet.GetCachedNetAssetInfo(key); if (info != null) { info.LastModifyTime = DateTime.Now; } else { int curCnt = _cachedNetAssetInfoSet.GetCachedNetAssetCnt(); //添加 挤掉旧的 if (curCnt >= C_CachedNetAssetMaxAmount) { string removeKey = _cachedNetAssetInfoSet.RemoveEarliestNetAssetInfo(); if (!string.IsNullOrEmpty(removeKey)) { string removePath = JW.Res.FileUtil.CombinePath(S_CachedNetAssetDirectory, removeKey + ".bytes"); if (JW.Res.FileUtil.IsFileExist(removePath)) { JW.Res.FileUtil.DeleteFile(removePath); } } } NetAssetInfo newInfo = new NetAssetInfo(); newInfo.Key = key; newInfo.LastModifyTime = DateTime.Now; _cachedNetAssetInfoSet.AddNetAssetInfo(key, newInfo); } _cachedNetAssetInfoSet.SortNetAssetInfo(); }
/// <summary> /// 添加信息记录 /// </summary> /// <param name="key"></param> /// <param name="info"></param> public void AddNetAssetInfo(string key, NetAssetInfo info) { if (_cachedAssetInfoMap.ContainsKey(key)) { return; } _cachedAssetInfoMap.Add(key, info); _cachedAssetInfos.Add(info); }
/// <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(); }
public void RemoveNetAssetInfoAndDelFileByDays(float days) { for (int i = _cachedAssetInfos.Count - 1; i >= 0; i--) { NetAssetInfo info = _cachedAssetInfos[i]; if ((DateTime.Now - info.LastModifyTime).TotalDays >= days) { //彻底清除 JW.Res.FileUtil.DeleteFile(info.GetCachedFilePath()); _cachedAssetInfoMap.Remove(info.Key); _cachedAssetInfos.RemoveAt(i); } } }
/// <summary> /// 移除最早的资产 /// </summary> /// <returns></returns> public string RemoveEarliestNetAssetInfo() { if (_cachedAssetInfos.Count <= 0) { return(null); } NetAssetInfo info = _cachedAssetInfos[0]; _cachedAssetInfos.RemoveAt(0); _cachedAssetInfoMap.Remove(info.Key); return(info.Key); }
/// <summary> /// 获取信息记录 /// </summary> /// <param name="key"></param> /// <returns></returns> public NetAssetInfo GetCachedNetAssetInfo(string key) { if (_cachedAssetInfoMap.ContainsKey(key)) { NetAssetInfo info = null; _cachedAssetInfoMap.TryGetValue(key, out info); return(info); } else { return(null); } }
/// <summary> /// 添加网络图片数据到缓存 /// </summary> /// <param name="url"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="data"></param> public void AddCachedNetImage(string url, int width, int height, byte[] data) { string key = JW.Res.FileUtil.GetMd5(url.ToLower()); NetAssetInfo info = _cachedNetAssetInfoSet.GetCachedNetAssetInfo(key); if (info != null) { info.LastModifyTime = DateTime.Now; } else { int curCnt = _cachedNetAssetInfoSet.GetCachedNetAssetCnt(); //添加 if (curCnt >= C_CachedNetAssetMaxAmount) { string removeKey = _cachedNetAssetInfoSet.RemoveEarliestNetAssetInfo(); if (!string.IsNullOrEmpty(removeKey)) { string removePath = JW.Res.FileUtil.CombinePath(S_CachedNetAssetDirectory, removeKey + ".bytes"); if (JW.Res.FileUtil.IsFileExist(removePath)) { JW.Res.FileUtil.DeleteFile(removePath); } } } // NetAssetInfo newInfo = new NetAssetInfo(); newInfo.Key = key; newInfo.AssetType = NetAssetType.Image; newInfo.ImageWidth = width; newInfo.ImageHeight = height; newInfo.LastModifyTime = DateTime.Now; _cachedNetAssetInfoSet.AddNetAssetInfo(key, newInfo); } _cachedNetAssetInfoSet.SortNetAssetInfo(); //保存数据 string filePath = JW.Res.FileUtil.CombinePath(S_CachedNetAssetDirectory, key + ".bytes"); if (JW.Res.FileUtil.IsFileExist(filePath)) { JW.Res.FileUtil.DeleteFile(filePath); } JW.Res.FileUtil.WriteFile(filePath, data); //记录保存 _isNeedSaveInfoSet = true; }
public int CompareTo(object obj) { NetAssetInfo cInfo = obj as NetAssetInfo; if (LastModifyTime > cInfo.LastModifyTime) { return(1); } else if (LastModifyTime == cInfo.LastModifyTime) { return(0); } else { return(-1); } }
//---------------------------------------Image 相关------------------------- /// 获取Cached 图片纹理 public Texture2D GetCachedNetImage(string url, float validDays, int width = 0, int height = 0) { string key = JW.Res.FileUtil.GetMd5(url.ToLower()); NetAssetInfo cachedInfo = _cachedNetAssetInfoSet.GetCachedNetAssetInfo(key); //验证是否过期 if (cachedInfo == null || (DateTime.Now - cachedInfo.LastModifyTime).TotalDays >= validDays) { return(null); } // string imageFilePath = JW.Res.FileUtil.CombinePath(S_CachedNetAssetDirectory, key + ".bytes"); if (!JW.Res.FileUtil.IsFileExist(imageFilePath)) { return(null); } // byte[] data = JW.Res.FileUtil.ReadFile(imageFilePath); if (data == null || data.Length <= 0) { return(null); } else { int w = width; int h = height; if (width == 0) { //使用记录的 w = cachedInfo.ImageWidth; h = cachedInfo.ImageHeight; } if (w == 0) { //Log.LogE("GetCachedNetImage No Set W And H"); w = 100; h = 100; } Texture2D texture2D = null; texture2D = new Texture2D(w, h, TextureFormat.ARGB32, false); texture2D.LoadImage(data); return(texture2D); } }
/// <summary> /// 写成配置 /// </summary> /// <param name="data"></param> /// <param name="offset"></param> public void Write(byte[] data, ref int offset) { int startOffset = offset; offset += 4; MemoryOperator.WriteShort((short)Version, data, ref offset); MemoryOperator.WriteShort((short)_cachedAssetInfos.Count, data, ref offset); // for (int i = 0; i < _cachedAssetInfos.Count; i++) { NetAssetInfo info = _cachedAssetInfos[i]; //Key MemoryOperator.WriteString(info.Key, data, ref offset); MemoryOperator.WriteShort((short)info.AssetType, data, ref offset); if (info.AssetType == NetAssetType.Image) { MemoryOperator.WriteShort((short)info.ImageWidth, data, ref offset); MemoryOperator.WriteShort((short)info.ImageHeight, data, ref offset); } //最后修改时间 MemoryOperator.WriteDateTime(ref info.LastModifyTime, data, ref offset); } MemoryOperator.WriteInt(offset - startOffset, data, ref startOffset); }
/// <summary> /// 准备一组网络文件 /// </summary> /// <param name="urls"></param> /// <param name="finishhandler"></param> public void PrepareNetAsset(LuaTable prepareUrls, System.Action <float> progresHandler) { if (prepareUrls == null) { Log.LogE("PrepareNetAsset Error Arg"); IsOver = true; return; } // List <string> urls = new List <string>(); for (int i = 0; i < prepareUrls.Length; i++) { //去重复 string cc = prepareUrls.Get <int, string>(i + 1); if (!urls.Contains(cc)) { urls.Add(cc); } } // if (urls.Count <= 0) { IsOver = true; Log.LogE("PrepareNetAsset Empty Urls"); if (progresHandler != null) { progresHandler(1.0f); } return; } // _finishedCnt = 0; _totalCnt = urls.Count; _httpSessionIds.Clear(); _handler = progresHandler; _failCnt = 0; IsOver = false; // for (int i = 0; i < urls.Count; i++) { string key = JW.Res.FileUtil.GetMd5(urls[i].ToLower()); string downloadPath = JW.Res.FileUtil.CombinePath(NetAssetService.S_CachedNetAssetDirectory, key + ".bytes"); //是否有了 NetAssetInfo info = NetAssetService.GetInstance().GetCachedNetAssetInfoByKey(key); string filePath = JW.Res.FileUtil.CombinePath(NetAssetService.S_CachedNetAssetDirectory, key + ".bytes"); if (info != null && (JW.Res.FileUtil.IsFileExist(filePath))) { _finishedCnt = _finishedCnt + 1; float pp = _finishedCnt / (float)_totalCnt; if (_handler != null) { _handler(pp); } //Over if (_finishedCnt >= _totalCnt) { if (_handler != null) { _handler(1.0f); } IsOver = true; } } else { //没有 就下载 uint sid = HttpService.GetInstance().RegisteFileHttpDownload(urls[i], downloadPath, this.OnHttpSessionCallBack); _httpSessionIds.Add(sid, key); } } }