예제 #1
0
    /// <summary>
    /// 启动更新下载,启动线程下载更新
    /// </summary>
    IEnumerator OnUpdateResource()
    {
        string message = string.Empty;

        if (!AppConst.UpdateMode)
        {
            message = "完成";
            OnResourceInited();
            //yield return new WaitForSeconds(0.1f);
            Invoke("OnUpdateFinsh", 0.1f);
            yield break;
        }
        string LocalServerList = Util.GetLocalServerList();
        string random          = DateTime.Now.ToString("yyyymmddhhmmss");
        string dataPath        = Util.DataPath; //数据目录
        WWW    www             = new WWW(AppConst.PublishServerListUrl + "?v=" + random);

        yield return(www);

        if (www.error != null)
        {
            OnResourceInited();
            Invoke("OnUpdateFailed", 0.1f);
            Invoke("OnUpdateFinsh", 0.1f);
            www.Dispose();
            www = null;
            yield break;
        }
        if (www.isDone)
        {
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }
            if (File.Exists(LocalServerList))
            {
                File.Delete(LocalServerList);
            }
            FileStream   fileStream   = null;
            StreamWriter streamWriter = null;
            fileStream = new FileStream(LocalServerList, FileMode.Create);
            string data = www.text;
            streamWriter = new StreamWriter(fileStream);
            streamWriter.Write(data);
            streamWriter.Flush();
            streamWriter.Close();
            fileStream.Close();
        }
        yield return(0);

        yield return(new WaitForEndOfFrame());

        string url = AppConst.PatchListUrl;

        string listUrl = url + Util.GetPlatformWebUrl() + AppConst.PatchList + "?v=" + random;

        Util.Log("Game", "LoadUpdate---->>>" + listUrl);
        yield return(new WaitForSeconds(0.1f));

        message = "正在验证资源....";
        AppFacade.Instance.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);
        www = new WWW(listUrl); yield return(www);

        if (www.error != null)
        {
            OnResourceInited();
            Invoke("OnUpdateFailed", 0.1f);
            Invoke("OnUpdateFinsh", 0.1f);
            www.Dispose();
            www = null;
            yield break;
        }
        if (!Directory.Exists(dataPath))
        {
            Directory.CreateDirectory(dataPath);
        }
        File.WriteAllBytes(dataPath + AppConst.PatchList, www.bytes);
        string         filesText     = www.text;
        FilesConfig    filelist      = JsonHelp.ReadFromJsonString <FilesConfig>(filesText);
        List <ResInfo> mShardInfos   = filelist.mShardInfos;
        string         versionFloder = filelist.VerRevision.GetTxt();

        AppConst.Version = versionFloder;
        versionFloder    = versionFloder.Replace('.', '_');
        string resUrl = filelist.PatchUrl + Util.GetPlatformWebUrl() + versionFloder + "/";

        if (filelist.AutoUpdate == false)
        {
            OnResourceInited();
            Invoke("OnUpdateFailed", 0.1f);
            Invoke("OnUpdateFinsh", 0.1f);
            www.Dispose();
            www = null;
            yield break;
        }
        for (int i = 0; i < mShardInfos.Count; i++)
        {
            string f         = mShardInfos[i].ResName;
            string localfile = (dataPath + f).Trim();
            string path      = Path.GetDirectoryName(localfile);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string fileUrl   = resUrl + f + "?v=" + random;
            bool   canUpdate = !File.Exists(localfile);
            if (!canUpdate)
            {
                string remoteMd5 = mShardInfos[i].md5;
                string localMd5  = Util.md5file(localfile);
                canUpdate = !remoteMd5.Equals(localMd5);
                if (canUpdate)
                {
                    File.Delete(localfile);
                }
            }
            if (canUpdate)
            {   //本地缺少文件
                message = "downloading>>" + fileUrl;
                AppFacade.Instance.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);
                BeginDownload(fileUrl, localfile);
                while (!(IsDownOK(localfile)))
                {
                    yield return(new WaitForEndOfFrame());
                }
            }
        }
        yield return(new WaitForEndOfFrame());

        OnResourceInited();
        message = "更新完成!!";
        Invoke("OnUpdateFinsh", 0.08f);
        www.Dispose();
        www = null;
        //AppFacade.Instance.SendMessageCommand(NotiConst.UPDATE_FINISHED, message);
    }