Exemplo n.º 1
0
    //-------------------------------------------------------------------------
    public void addFile4Download(string remote_url_prefix, Dictionary <string, string> map_newfile)
    {
        foreach (var i in map_newfile)
        {
            DataFile data_file;
            data_file.datafile_local_path = AutoPatcherPath.combinePersistentDataPath(i.Key);
            data_file.datafile_remote_url = remote_url_prefix + i.Key;

            mQueDataFile.Enqueue(data_file);
        }

        TotalCount = mQueDataFile.Count;
    }
 //-------------------------------------------------------------------------
 void _deleteOldAsset()
 {
     foreach (var i in mMapNeedDeleteAsset)
     {
         string path = AutoPatcherPath.combinePersistentDataPath(i.Key);
         if (Directory.Exists(path))
         {
             Directory.Delete(path, true);
         }
         if (File.Exists(path))
         {
             File.Delete(path);
         }
     }
 }
    }                              // 完成自动更新

    //-------------------------------------------------------------------------
    public override void init()
    {
        EbLog.Note("ClientAutoPatcher.init()");

        StepGetRemoteVersionInfo = false;
        StepIsNeedBundlePatcher  = false;
        StepBundlePatcher        = false;
        StepIsNeedDataPatcher    = false;
        StepDataPatcher          = false;
        StepFinished             = false;

        AutoPatcherConfig      = new AutoPatcherConfig();
        VersionInfo            = new VersionInfo(AutoPatcherPath.combinePersistentDataPath("/DataFileList.txt"));
        mMapNeedDeleteAsset    = new Dictionary <string, string>();
        mMapSameAsset          = new Dictionary <string, string>();
        mQueueAlreadyLoadAsset = new Queue <string>();
    }
    //-------------------------------------------------------------------------
    public override void update(float elapsed_tm)
    {
        // 步骤:从自动更新服务器获取版本配置信息
        if (StepGetRemoteVersionInfo)
        {
            if (mWWWGetServerVersionCfg == null)
            {
                mWWWGetServerVersionCfg = new WWW(AutoPatcherConfig.RemoteVersionInfoUrl);

                if (OnAutoPatcherGetServerVersionCfg != null)
                {
                    OnAutoPatcherGetServerVersionCfg();
                }
            }
            else if (mWWWGetServerVersionCfg.isDone)
            {
                AutoPatcherResult r = AutoPatcherResult.Success;
                if (!string.IsNullOrEmpty(mWWWGetServerVersionCfg.error))
                {
                    r = AutoPatcherResult.Failed;
                }
                else
                {
                    VersionInfo.parseRemoteVersionInfo(mWWWGetServerVersionCfg.text);
                    StepIsNeedBundlePatcher = true;
                }

                if (OnAutoPatcherGetServerVersionCfgResult != null)
                {
                    OnAutoPatcherGetServerVersionCfgResult(r, mWWWGetServerVersionCfg.error);
                }

                StepGetRemoteVersionInfo = false;
                mWWWGetServerVersionCfg  = null;
            }
        }

        // 步骤:判定是否需要Bundle包更新
        if (StepIsNeedBundlePatcher)
        {
            bool is_need = false;
            if (!VersionInfo.remoteEqualsLocalBundleVersion())
            {
                is_need = true;
            }

            if (is_need)
            {
                if (OnAutoPatcherIsNeedBundlePatcher != null)
                {
                    OnAutoPatcherIsNeedBundlePatcher(is_need,
                                                     VersionInfo.LocalBundleVersion,
                                                     VersionInfo.RemoteVersionInfo.bundle_version);
                }
            }
            else
            {
                StepIsNeedDataPatcher = true;
            }

            StepIsNeedBundlePatcher = false;
        }

        // 步骤:Bundle包更新
        if (StepBundlePatcher)
        {
            Application.OpenURL(VersionInfo.RemoteVersionInfo.bundle_url);
            StepBundlePatcher = false;
        }

        // 步骤:判定是否需要数据包更新
        if (StepIsNeedDataPatcher)
        {
            bool is_need = false;
            if (!VersionInfo.remoteEqualsLocalDataVersion())
            {
                is_need = true;
            }

            if (is_need)
            {
                if (OnAutoPatcherIsNeedDataPatcher != null)
                {
                    OnAutoPatcherIsNeedDataPatcher(is_need,
                                                   VersionInfo.LocalVersionInfo.data_version,
                                                   VersionInfo.RemoteVersionInfo.data_version);
                }
            }
            else
            {
                StepFinished = true;
            }

            StepIsNeedDataPatcher = false;
        }

        // 步骤:从自动更新服务器获取数据清单信息
        if (StepGetRemoteDataFileList)
        {
            if (mUpdateDataWWW == null)
            {
                Caching.CleanCache();

                mUpdateDataWWW = new WWW(VersionInfo.RemoteVersionInfo.data_url);
                mRemoteRootDir = VersionInfo.RemoteVersionInfo.data_url.Substring(
                    0, VersionInfo.RemoteVersionInfo.data_url.LastIndexOf('/'));

                if (OnAutoPatcherGetRemoteDataFileList != null)
                {
                    OnAutoPatcherGetRemoteDataFileList();
                }
            }
            else if (mUpdateDataWWW.isDone)
            {
                AutoPatcherResult r = AutoPatcherResult.Success;
                if (string.IsNullOrEmpty(mUpdateDataWWW.error))
                {
                    mRemoteDataFileList = mUpdateDataWWW.bytes;
                    VersionInfo.parseRemoteDataFileList(mUpdateDataWWW.text);
                    var map_need_loadasset = _getNeedLoadAsset();
                    _deleteOldAsset();

                    if (map_need_loadasset.Count == 0)
                    {
                        VersionInfo.changeLocalDataVersionToRemote();

                        using (FileStream fs = new FileStream(AutoPatcherPath.combinePersistentDataPath("/DataFileList.txt"), FileMode.Create))
                        {
                            fs.Write(mRemoteDataFileList, 0, mRemoteDataFileList.Length);
                        }

                        StepFinished = true;
                    }
                    else
                    {
                        mAutoPatcherDataFileList = new AutoPatcherDataFileList();
                        mAutoPatcherDataFileList.addFile4Download(mRemoteRootDir, map_need_loadasset);

                        StepDataPatcher = true;
                    }
                }
                else
                {
                    r = AutoPatcherResult.Failed;
                }

                if (OnAutoPatcherGetRemoteDataFileListResult != null)
                {
                    OnAutoPatcherGetRemoteDataFileListResult(r, mUpdateDataWWW.error);
                }

                mUpdateDataWWW            = null;
                StepGetRemoteDataFileList = false;
            }
        }

        // 步骤:数据包更新
        if (StepDataPatcher)
        {
            mAutoPatcherDataFileList.update(elapsed_tm);

            if (mAutoPatcherDataFileList.Finished)
            {
                VersionInfo.changeLocalDataVersionToRemote();

                using (FileStream fs = new FileStream(AutoPatcherPath.combinePersistentDataPath("/DataFileList.txt"), FileMode.Create))
                {
                    fs.Write(mRemoteDataFileList, 0, mRemoteDataFileList.Length);
                }

                mRemoteDataFileList      = null;
                mAutoPatcherDataFileList = null;

                StepDataPatcher = false;
                StepFinished    = true;
            }
            else
            {
                float progress = mAutoPatcherDataFileList.CurrentLoadProgress;
                progress *= 100f;

                string info = string.Format("({0:00.0}%)正在下载数据文件{1}({2}/{3})",
                                            progress,
                                            mAutoPatcherDataFileList.CurrentDataFile,
                                            mAutoPatcherDataFileList.CurrentIndex,
                                            mAutoPatcherDataFileList.TotalCount);

                if (OnAutoPatcherDataPatcher != null)
                {
                    OnAutoPatcherDataPatcher(info);
                }
            }
        }

        // 自动更新结束
        if (StepFinished)
        {
            StepFinished = false;

            if (OnAutoPatcherFinished != null)
            {
                OnAutoPatcherFinished();
            }
        }
    }