static int Dispose(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         LuaFramework.ThreadManager obj = (LuaFramework.ThreadManager)ToLua.CheckObject(L, 1, typeof(LuaFramework.ThreadManager));
         obj.Dispose();
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Exemple #2
0
        IEnumerator DownloadErrorFiles()
        {
            if (packDownloadErrorCount > 3)
            {
                ThreadManager.Dispose();
                yield break;
            }
            string dataPath = Util.DataPath;  //数据目录
            string random   = DateTime.Now.ToString("yyyymmddhhmmss");

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }
            foreach (var key in downloadErrorFiles)
            {
                string fileUrl = string.Format("{0}/{1}?v={2}", Config.User_Config.web_url, key, random);
                Debugger.Log(string.Format("[1]Download in background>>{0}", fileUrl));
                var www = new WWW(fileUrl);
                yield return(www);

                if (www.error != null)
                {
                    packDownloadErrorCount++;
                    Debugger.LogError(string.Format("[1]Background download error:>>{0}\nfile url: {1}", www.error, fileUrl));
                    StartCoroutine(DownloadErrorFiles());
                    yield break;
                }
                if (www.isDone)
                {
                    try
                    {
                        Debug.Assert(key.CustomEndsWith(".ab"));
                        BeginDownloadExtract(dataPath, key, www.bytes);
                        www.Dispose();
                        www = null;
                    }
                    catch (Exception e)
                    {
                        packDownloadErrorCount++;
                        Debug.LogError(string.Format("[1]Background downloading error: {0}\nFile url: {1}", e.Message, fileUrl));
                        www.Dispose();
                        www = null;
                        StartCoroutine(DownloadErrorFiles());
                        yield break;
                    }
                    yield return(Yielders.GetWaitForSeconds(2f));
                }
            }

            yield return(Yielders.GetWaitForSeconds(5f));

            if (downloadErrorFiles.Count > 0)
            {
                Debug.LogError(string.Format("[1]Background downloading error: {0}", LanguageTips.UPDATE_MD5_ERROR));
                packDownloadErrorCount++;
                StartCoroutine(DownloadErrorFiles());
            }
            else
            {
                ThreadManager.Dispose();
            }
        }
Exemple #3
0
        private IEnumerator UpdatePackResources()
        {
            if (beginPackDownload)
            {
                yield break;
            }
            extractFiles.Clear();
            downloadCheckDatas.Clear();
            downloadErrorFiles.Clear();
            totalExtractCount = 0;
            beginPackDownload = true;
            curDownloadIndex  = 0;
            string dataPath = Util.DataPath;  //数据目录
            string random   = DateTime.Now.ToString("yyyymmddhhmmss");

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            int curOrder = 0;

            foreach (var key in needDownloadPackFiles.Keys)
            {
                string[] keyValue  = needDownloadPackFiles[key].Split('|');
                var      order     = Convert.ToInt32(keyValue[4].Split(':')[1]);
                string   f         = keyValue[0];
                string   localfile = (dataPath + f).Trim();
                string   path      = Path.GetDirectoryName(localfile);
                string   md5       = keyValue[1].Trim();
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var tempMd5 = string.Format("{0}_md5", f);
                if (File.Exists(localfile) && PlayerPrefs.GetString(tempMd5) == md5)
                {
                    curDownloadIndex++;
                    if (progressChanged != null && !pauseDownloading)
                    {
                        progressChanged((float)curDownloadIndex / (float)needDownloadPackFiles.Count);
                    }
                    continue;
                }
                while ((Application.internetReachability != NetworkReachability.ReachableViaLocalAreaNetwork && !allowCarrierDataDownload) || pauseDownloading)
                {
                    yield return(Yielders.EndOfFrame);
                }
                if (curOrder != order)
                {
                    if (!unlimited)
                    {
                        yield return(Yielders.GetWaitForSeconds(1f));
                    }
                    curOrder = order;
                }
                string fileUrl = string.Format("{0}/{1}?v={2}", Config.User_Config.web_url, keyValue[0], random);
                Debugger.Log(string.Format("[0]Download in background>>{0}", fileUrl));

                packDownloadTimeout = 0;
                timeout             = false;
                var www = new WWW(fileUrl);
                while (!www.isDone)
                {
                    if (packDownloadTimeout >= 20 && www.progress == 0 || packDownloadTimeout >= 300)
                    {
                        timeout = true;
                        break;
                    }
                    packDownloadTimeout += Time.deltaTime;
                    yield return(Yielders.EndOfFrame);
                }

                if (www.error != null || timeout)
                {
                    packDownloadErrorCount++;
                    if (timeout)
                    {
                        Debugger.LogError(string.Format("[0]Background download error:>> timeout\nfile url: {0}", fileUrl));
                    }
                    else
                    {
                        Debugger.LogError(string.Format("[0]Background download error:>>{0}\nfile url: {1}", www.error, fileUrl));
                    }
                    if (packDownloadErrorCount > 3)
                    {
                        packDownloadJumpedCount++;
                        continue;
                    }
                    else
                    {
                        beginPackDownload = false;
                        StartCoroutine(UpdatePackResources());
                        yield break;
                    }
                }
                if (www.isDone)
                {
                    try
                    {
                        if (keyValue[0].EndsWith(".ab"))
                        {
                            if (!downloadCheckDatas.ContainsKey(keyValue[0]))
                            {
                                downloadCheckDatas.Add(keyValue[0], md5);
                            }
                            else if (downloadCheckDatas[keyValue[0]] != md5)
                            {
                                downloadCheckDatas[keyValue[0]] = md5;
                            }
                            BeginDownloadExtract(dataPath, keyValue[0], www.bytes);
                        }
                        else
                        {
                            var realMd5 = MD5.ComputeHashString(www.bytes);
                            if (realMd5 != md5)
                            {
                                packDownloadJumpedCount++;
                                curDownloadIndex++;
                                Debug.LogError(string.Format("[0]Background downloading md5 error!\nfile url: {0}", fileUrl));
                                www.Dispose();
                                www = null;
                                continue;
                            }
                            if (File.Exists(localfile))
                            {
                                File.Delete(localfile);
                            }
                            File.WriteAllBytes(localfile, www.bytes);
                            // 记录文件下载状态: 1-下载完毕
                            PlayerPrefs.SetInt(f, 1);
                            PlayerPrefs.DeleteKey(tempMd5);
                            PlayerPrefs.SetString(tempMd5, realMd5);
                            PlayerPrefs.Save();
                            Util.CallMethod("Game", "OnOnePackFileDownload", keyValue[0]);
                        }
                        www.Dispose();
                        www = null;
                        curDownloadIndex++;
                        var value   = (float)curDownloadIndex / (float)needDownloadPackFiles.Count;
                        var percent = (int)(value * 100);
                        if (percent == 30)
                        {
                            Util.CallMethod("Network", "OnPackageDownloadComplete", 30);
                        }
                        else if (percent == 60)
                        {
                            Util.CallMethod("Network", "OnPackageDownloadComplete", 60);
                        }
                        if (progressChanged != null && !pauseDownloading)
                        {
                            progressChanged(value);
                        }
                    }
                    catch (Exception e)
                    {
                        packDownloadJumpedCount++;
                        curDownloadIndex++;
                        Debug.LogError(string.Format("[0]Background downloading error: {0}\nFile url: {1}", e.Message, fileUrl));
                        www.Dispose();
                        www = null;
                        continue;
                    }
                    if (!unlimited)
                    {
                        yield return(Yielders.GetWaitForSeconds(1f));
                    }
                }
            }
            if (downloadErrorFiles.Count > 0)
            {
                Debug.LogError(string.Format("[0]Background downloading error: {0}", LanguageTips.UPDATE_MD5_ERROR));
                StartCoroutine(DownloadErrorFiles());
            }
            while (extractFiles.Count > 0)
            {
                backgroundExtracting = true;
                if (progressChanged != null)
                {
                    progressChanged(GetExtractingPercent());
                }
                yield return(Yielders.GetWaitForSeconds(0.5f));
            }
            backgroundExtracting          = false;
            finishedBackgroundDownloading = true;
            if (downloadErrorFiles.Count == 0)
            {
                ThreadManager.Dispose();
            }
            if (progressChanged != null && !pauseDownloading)
            {
                progressChanged(1);
            }
            Util.CallMethod("Network", "OnPackageDownloadComplete", 100);
        }