/* 函数说明: 开始一个下载请求 */ public IEnumerator StartRequest(string fileName) { m_fileName = fileName; // 判断文件是否已存在 if (GetDownloadedFile(m_fileName, out m_filePath)) { m_status = Status.Finish; Close(); yield break; } // 创建本地下载目录 if (!ResUtils.MakeDirectory(m_filePath)) { m_status = Status.Failed; Close(); Debug.LogError(string.Format("HttpDownloader : MakeDirectory '{0}' Error !", m_filePath)); yield break; } string url = ResUpdater.Instance().GetDownloadUrl(m_fileName); if (string.IsNullOrEmpty(url)) { m_status = Status.Failed; Close(); Debug.LogError("HttpDownloader : download url is null !"); yield break; } m_status = Status.Downing; m_www = new WWW(url); yield return(m_www); // 如果错误, 则返回 if (!string.IsNullOrEmpty(m_www.error)) { m_status = Status.Failed; yield break; } // 写文件 WriteFile(m_filePath, m_www.bytes); }
public static bool GetDownloadedFile(string fileName, out string fullPath) { Debug.LogError("Start GetDownloadedFile " + fileName); if (string.IsNullOrEmpty(fileName)) { Debug.LogError("if (string.IsNullOrEmpty(fileName))"); fullPath = ""; return(false); } fullPath = ResUtils.GetDownloadPath(fileName); if (!File.Exists(fullPath)) { Debug.LogError("if (!File.Exists(fullPath)) fullPath :" + fullPath); return(false); } // 匹配文件md5 Dictionary <string, string> serverFilesMD5 = ResUpdater.Instance().serverFilesMD5; string fileMD5; if (serverFilesMD5.TryGetValue(fileName, out fileMD5)) { Debug.LogError("if (serverFilesMD5.TryGetValue(fileName, out fileMD5)) fileMD5 " + fileMD5); if (string.IsNullOrEmpty(fileMD5)) { return(false); } string localFileMD5 = ResUtils.GetFileMD5(fullPath); Debug.LogError(" if (string.IsNullOrEmpty(fileMD5)) fileMD5 " + fileMD5 + " localFileMD5 " + localFileMD5); return(string.Compare(fileMD5, localFileMD5, true) == 0); } Debug.LogError("Stop GetDownloadedFile"); return(false); }