//根据解析出来的map文件,比较本地文件的md5,如果不同,则需要重新下载 private int checkLocalFileMD5() { UpdateLog.DEBUG_LOG("检查本地文件md5+++"); int ret = CodeDefine.RET_SUCCESS; int total = _parsedMapDataList.Count; _checkedCount = 0; for (int i = 0; i < _parsedMapDataList.Count; i++) { MapFileData fileData = _parsedMapDataList[i]; if (fileData.Name.ToLower().Contains("localversion.xml") || fileData.Name.ToLower().Contains("resourceassetbundles")) { continue; } string localFile = (_storeDir + "/" + fileData.Dir + fileData.Name).Replace("\\", "/").Replace("//", "/"); string localFileMD5 = MD5.MD5File(localFile); if (localFileMD5.Equals("") || fileData.Md5.Equals(localFileMD5) == false) { MapFileDataListForDownload.Add(fileData); } _checkedCount++; } if (_checkedCount > 0) { UpdateLog.WARN_LOG("需要下载文件"); } UpdateLog.DEBUG_LOG("检查本地文件md5---"); return(ret); }
public ThreadPool(int maxThreadCount, ThreadPoolAction <T> action) { _threadQueue = new List <MyThread>(); _dataPool = new DataPool <T>(); _lockObj = new object(); _stop = false; _action = action; _threadManger = new ManualResetEvent(false); for (int i = 0; i < maxThreadCount; i++) { MyThread item = new MyThread(i); item.Name = "download thread: " + i; if (_highPriorityThread == null) { _highPriorityThread = item; _highPriorityThread.Start(new ThreadStart(HighPriorityThreadFunc)); } else { item.Start(new ThreadStart(ThreadFunc)); } _threadQueue.Add(item); } UpdateLog.WARN_LOG("Init ThreadPool"); }
/// <summary> /// 测试流程要用到的数据 /// </summary> /// <param name="imeiOrMacOrIdfa">imei、mac地址、idfa</param> /// <param name="ip">ip地址,基本没用了</param> public void SetImeiOrMacOrIdfa(string imeiOrMacOrIdfa) { UpdateLog.WARN_LOG("UpdateManager ---> SetImeiOrMacOrIdfa() "); if (this._initialized) { this.FlowInstance <Flow3RemoteXml>().SetExternalData(imeiOrMacOrIdfa, ""); } }
/// <summary> /// 中断更新流程 /// </summary> public void AbortFlows() { _abortFlows = true; if (CurrentFlow != null) { CurrentFlow.Abort(); UpdateLog.WARN_LOG("中断流程 " + CurrentFlow.FlowName()); } UpdateSystem.Download.BackDownload.AbortAll(null); }
/// <summary> /// 下载资源 /// </summary> /// <param name="absolutPath"></param> /// <param name="callback"></param> /// <param name="obj"></param> /// <returns></returns> public bool DownloadResource(string absolutPath, UpdateAction <string, bool, object> callback, object obj) { string fixPath = absolutPath; MapFileData data = null; if (!IsExistOrDownloaded(absolutPath, out data, out fixPath)) { if (data != null) { lock (BackDownload.GetLocker()) { //暂停中,则不接受请求,原样返回 if (IsPaused()) { UpdateLog.WARN_LOG("当前是暂停状态,直接返回请求:" + absolutPath); data.DownloadCallBack = null; data.ArgObj = null; callback(fixPath, false, obj); return(true); } else { data.DownloadCallBack = callback; data.ArgObj = obj; data.DataLevel = DataLevel.High; } } if (!data.Downloading) { BackDownload.AddDataToPool(data, DataLevel.High); } else { UpdateLog.WARN_LOG("resource is downloading: " + fixPath); } } else { //UpdateLog.WARN_LOG("no map data found: " + absolutPath); callback(absolutPath, false, obj); return(true); } } else { callback(fixPath, true, obj); } return(false); }
private int checkLocalFileMD5() { UpdateLog.DEBUG_LOG("检查本地文件md5+++"); int count = this._parsedMapDataList.Count; this._checkedCount = 0; for (int i = 0; i < this._parsedMapDataList.Count; i++) { this._checkedCount++; MapFileData item = this._parsedMapDataList[i]; string path = BaseFlow._storeDir + "/" + item.Dir + item.Name; if (item.Name.ToLower().IndexOf("localversion.xml") == -1 || item.Name.ToLower().IndexOf("resourceassetbundles") == -1) { string str2 = BaseFlow._appDir + "/" + item.Dir + item.Name; if (File.Exists(path) || File.Exists(str2)) { string str3 = MD5.MD5File(path); if (string.IsNullOrEmpty(str3)) { str3 = MD5.MD5File(str2); } if (!(!str3.Equals("") && item.Md5.Equals(str3))) { RepairList.Add(item); } } else { if (item.Name.ToLower().IndexOf("resourceassetbundles") == -1) { RepairList.Add(item); continue; //后台下载(是否后台下载) _backDownloadDict.Add(path, item); this.BackDownloadList.Add(item); } } } } if (this._checkedCount > 0) { UpdateLog.WARN_LOG("需要下载文件"); } MapFileDataListForDownload = RepairList; UpdateLog.DEBUG_LOG("检查本地文件md5---"); return(1); }
/// <summary> /// 下载函数 /// </summary> /// <param name="url">地址</param> /// <param name="outFile">输入路径</param> /// <param name="begin">起点</param> /// <param name="end">终点</param> /// <returns>小于0失败</returns> public int HttpDownload(string url, FileStream outFile, long begin = 0, long end = 0) { int num = CodeDefine.RET_INIT; _requestIns = null; try { DownloadUrl = url; _requestIns = createRequest(url); setRange(_requestIns, (int)begin, (int)end); requestData(_requestIns, outFile); num = CodeDefine.RET_SUCCESS; } catch (Exception exception) { UpdateLog.WARN_LOG("catch http instance exception :::" + exception.Message); if (!_isAbortOption) { UpdateLog.ERROR_LOG(_TAG + exception.Message + "\n" + exception.StackTrace); UpdateLog.EXCEPTION_LOG(exception); num = CodeDefine.RET_FAIL_EXCEPTION_DOWNLOAD; } UpdateLog.ERROR_LOG("HttpDownload: " + outFile.Name); } finally { if ((outFile != null) && outFile.CanWrite) { outFile.Close(); } _requestIns = null; if (_isAbortOption) { num = CodeDefine.RET_SKIP_BY_ABORT; } } _isAbortOption = false; if (MapFileData != null) { MapFileData.ErrorCode = num; } return(num); }
private void AddDataList(IEnumerable dataList, DataLevel level, bool append = false) { if (dataList != null && !append) { BackDownload.ClearData(level); if (_sceneResList.ContainsKey(level) && _sceneResList[level] != null) { _sceneResList[level].Clear(); } } Flow8ExCheckResource resource = InstanceFlow8Ex; string fixPath = ""; IEnumerator enumerator = dataList.GetEnumerator(); while (enumerator.MoveNext()) { //不存在的文件才加入到下载队列 if (!Exist(enumerator.Current as string, out fixPath)) { lock (BackDownload.GetLocker()) { MapFileData mapFileData = resource.GetMapFileDataByPath(fixPath); if (((mapFileData == null) || mapFileData.Downloading) || mapFileData.Downloaded) { UpdateLog.WARN_LOG("no map data: " + fixPath); } else { mapFileData.FullPath = fixPath; if (mapFileData.DataLevel > level) { mapFileData.DataLevel = level; } BackDownload.AddDataToPool(mapFileData, level); //添加到当前场景资源列表,用于统计大小,显示进度 AddToCurSceneResList(level, mapFileData); } } } } }
/// <summary> /// 下载场景资源 /// </summary> /// <param name="mapID">要下载的场景ID</param> /// <param name="current">是否当前场景</param> public void BackDownloadSceneData(int mapID, bool current = false, bool append = false) { string str = string.Format("{1}/{0}.txt", mapID, _sceneConfigPath); string path = _storePath + "/" + str; if (File.Exists(path)) { UpdateLog.WARN_LOG("download scene resource: " + str); string[] normalList = File.ReadAllLines(path); if (current) { BackDownloadCurSceneData(normalList, append); } else { BackDownloadNextSceneData(normalList, append); } } else { UpdateLog.WARN_LOG("找不到文件: " + path); } }
private int downloadMapData(MapFileData fileData, string saveFilePath) { int ret = CodeDefine.RET_FAIL; try { lock (m_locker) { if (!Directory.Exists(fileData.SaveDir + "/" + fileData.Dir)) { Directory.CreateDirectory(fileData.SaveDir + "/" + fileData.Dir); } } //计算下载点,在资源包中,每个文件都有4个32字节的数据头加上文件名、md5长度,要跳过 long begin = fileData.Begin + 32 * 4 + fileData.DirLen + fileData.NameLen + fileData.Md5Len; //http的AddRange方法是闭包的,所以减一。([from, to]) long end = fileData.End - 1; //每个文件有3次下载机会 int i = _RETRAY_TIMES; while (i > 0) { i--; if (fileData.Name.Contains("RemoteVersion.xml") || fileData.Name.ToLower().Contains("localversion.xml")) { ret = CodeDefine.RET_SUCCESS; return(ret); } if (Download.UseBackupCdn(fileData.ResUrl, Download.BackupCdn)) { //使用台湾备份cdn地址 for (int cdnIndex = 0; cdnIndex < Download.BackupCdn.Length; ++cdnIndex) { string backupUrl = fileData.ResUrl.Replace(Download.BackupCdn[0], Download.BackupCdn[cdnIndex]); using (FileStream outFile = new FileStream(saveFilePath, FileMode.Create)) { ret = httpDownload(backupUrl, outFile, begin, end); if (ret >= CodeDefine.RET_SUCCESS) { break; } if (ret == CodeDefine.RET_SKIP_BY_ABORT) { return(ret); } } } if (ret >= CodeDefine.RET_SUCCESS) { break; } } else { using (FileStream outFile = new FileStream(saveFilePath, FileMode.Create)) { //UpdateLog.INFO_LOG(_TAG + " download: " + saveFilePath); ret = httpDownload(fileData.ResUrl, outFile, begin, end); if (ret >= CodeDefine.RET_SUCCESS) { break; } if (ret == CodeDefine.RET_SKIP_BY_ABORT) { return(ret); } UpdateLog.WARN_LOG(_TAG + " try download i = " + i); Thread.Sleep(_SLEEP_TIME); } } } } catch (System.Exception ex) { if (saveFilePath.Contains("ClassesResources.xml")) { ret = CodeDefine.RET_SUCCESS; } UpdateLog.ERROR_LOG(_TAG + "ThreadCallBack(object state) download fail: file= " + saveFilePath + "\n error" + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return(ret); }
/// <summary> /// 设置预加载信息,用来显示进度 /// </summary> /// <param name="total"></param> /// <param name="loadedCount"></param> public void SetPreloadTotal(int total) { UpdateLog.WARN_LOG("start preload+++++++++++++"); _showPreloadRes = true; _totalPreloadRes = total; }
public int parseMapFile(string mapFile, string resUrl, string saveDir) { _saveDir = saveDir; UpdateLog.INFO_LOG(_TAG + "parseMapFile(string mapFile, string resUrl):" + mapFile + "," + resUrl); int ret = CodeDefine.RET_SUCCESS; List <MapFileData> mapFileDataList = _mapFileDataList; FileStream mapFileStream = null; try { mapFileStream = new FileStream(mapFile, FileMode.Open); long filePosition = 0; long mapFileSize = mapFileStream.Length; while (mapFileSize > 0 && filePosition != mapFileSize) { if (filePosition >= mapFileSize) { UpdateLog.ERROR_LOG("解析出错了"); return(ret); } MapFileData mapFileData = new MapFileData(); mapFileData.Begin = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); if (mapFileData.Begin == -1) { UpdateLog.WARN_LOG("解析map文件,出现异常,请检查: " + mapFile); return(ret); } mapFileData.End = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.DirLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.NameLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.Md5Len = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.FileSize = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.Dir = read(mapFileStream, mapFileData.DirLen, filePosition, out filePosition); mapFileData.Name = read(mapFileStream, mapFileData.NameLen, filePosition, out filePosition); mapFileData.Md5 = read(mapFileStream, mapFileData.Md5Len, filePosition, out filePosition); mapFileData.ResUrl = resUrl; mapFileData.SaveDir = _saveDir; UnityEngine.Debug.Log("mapFileData.Name :" + mapFileData.Name); mapFileDataList.Add(mapFileData); } } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_PARSE_MAP_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } finally { if (mapFileStream != null) { mapFileStream.Close(); } } return(ret); }
private void requestData(HttpWebRequest myRequest, FileStream outFile) { byte[] btContent = new byte[_BUFFER]; HttpWebResponse reponse = null; Stream myStream = null; try { using (reponse = myRequest.GetResponse() as HttpWebResponse) { using (myStream = reponse.GetResponseStream()) { int intSize = 0; int readLen = 0; intSize = myStream.Read(btContent, 0, _BUFFER); while (intSize > 0) { readLen += intSize; //已下载 DownloadedSize = readLen; lock (m_locker) { MutiDownloadedSize += intSize; //中断下载 if (_isAbortOption) { UpdateLog.WARN_LOG("abort download : " + Thread.CurrentThread.Name); break; } } outFile.Write(btContent, 0, intSize); if (!myStream.CanRead) { break; } intSize = myStream.Read(btContent, 0, _BUFFER); } } } } catch (System.Exception ex) { UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); if (reponse != null) { UpdateLog.ERROR_LOG("Http status: " + reponse.StatusCode); } } finally { if (reponse != null) { reponse.Close(); } if (myStream != null) { myStream.Close(); } if (outFile != null) { outFile.Flush(); } } btContent = null; }
/// <summary> /// 后台更新当前场景资源 /// </summary> /// <param name="normalList"></param> /// <param name="append"></param> internal void BackDownloadCurSceneData(string[] normalList, bool append = false) { UpdateLog.WARN_LOG("Add cur scene resources"); AddDataList(normalList, DataLevel.CurScene, append); }