private void OnDonwloadResFinish(WWW www, AssetbundleJsonData json) { bool success = string.IsNullOrEmpty(www.error); byte[] data = www.bytes; if (success) { if (json.resName.Contains("version.dat")) { tip = " 忽略version.dat "; Debug.LogError(tip); } else { SafeWriteFileInPersistent(json.resName, data); } download++; if (download == updateFiles.Count) { StartCoroutine(DoLastJob()); } } else { UIMessageBoxHelper.onShow0("error: 801"); } //else //{ // downFileCount--; //} }
public static void SafeWriteFileInPersistent(string fileName, byte[] content) { #if UNITY_ANDROID && !UNITY_EDITOR long sizeB = AndroidHelper.GetSystemSize(); if (sizeB == 0) { sizeB = AndroidHelper.GetSdCardSize(); } int contB = (content.Length) + 10; //Debug.LogError("磁盘大小 " + (sizeB >> 20)); if (sizeB < contB) { UIMessageBoxHelper.Show("磁盘空间不足,清理磁盘后再尝试!", () => { Application.Quit(); throw new System.Exception("磁盘空间不足"); }); } else { FileUtils.WriteFileInPersistent(fileName, content, false); } #else FileUtils.WriteFileInPersistent(fileName, content, false); #endif }
void ShowTip(float size, OnDo dofunc) { string msg = null; switch (Application.internetReachability) { //4g or 3g case NetworkReachability.ReachableViaCarrierDataNetwork: msg = string.Format(" 本次更新约{0:F2}MB,你当前处于运营商网络,是否开始更新?", size); break; //wifi case NetworkReachability.ReachableViaLocalAreaNetwork: msg = string.Format(" 本次更新约{0:F2}MB,你当前处于wifi网络,是否开始更新?", size); break; default: break; } if (string.IsNullOrEmpty(updateTip)) { UIMessageBoxHelper.Show(msg, () => { dofunc(); }, () => { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif }); } else { UIMessageBoxHelper.ShowUpdate(updateTip, msg, () => { timeStart = Time.time; dofunc(); }, () => { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif }); } }
public IEnumerator DownloadFile(string url) { string u = FileUtils.url + "/" + url; WWW www = new WWW(u + "?time=" + System.DateTime.Now.Ticks + "&r=" + Random.Range(int.MinValue, int.MaxValue)); string tip = "下载文件 " + u + " " + www.progress; #if UNITY_EDITOR Debug.Log(tip); #endif float timeStart = Time.time; yield return(www); GameUpdater.networkSpeed = www.bytesDownloaded / (Time.time - timeStart); if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); if (FileUtils.IsAllCDNCantDownload()) { string msg = string.Format("网络错误(error : 801)\r\n{0}", www.responseHeaders.ContainsKey("STATUS") ? www.responseHeaders["STATUS"] : www.error); UIMessageBoxHelper.Show(msg, () => { UnityEngine.SceneManagement.SceneManager.LoadScene("Launcher"); } ); yield return(new WaitForSeconds(4f)); } else { FileUtils.ChangeCDNState(); //u = FileUtils.url + "/" + url; //www = new WWW(u + "?time=" + System.DateTime.Now.Ticks + "&r=" + Random.Range(int.MinValue, int.MaxValue)); //yield return www; //tip = "下载文件 " + u + " " + www.progress; //Debug.Log(tip); MultiWWW.instance.StartCoroutine(DownloadFile(url)); } } if (www.isDone && string.IsNullOrEmpty(www.error)) { GameUpdater.networkAvgSpeed += www.bytes.Length; if (callback != null) { callback(www, data); } if (finishCallback != null) { finishCallback(); } } }
void CheckNetStatus() { if (Application.internetReachability == NetworkReachability.NotReachable) { string msg = string.Format("网络断开,请重试..."); UIMessageBoxHelper.Show( msg, () => { }, () => { ReStart(); }); } }
public void DownloadResVersionList() { string versionURL = string.Format("{0}/{1}", ResSetting.GetChannelRes(), "version.dat"); if (Application.internetReachability == NetworkReachability.NotReachable) { string msg = string.Format("无法链接到网络"); UIMessageBoxHelper.Show( msg, () => { ReStart(); }); } else { StartCoroutine(DownloadFile(versionURL, OnLoadVersionList)); } }
// public IEnumerator DownloadAPKFile(string url, OnDownloadFinish callback) // { // CheckIsWifi(); // CheckNetStatus(); // string u = FileUtils.url + "/" + url; // WWW www = new WWW(u + "?time=" + System.DateTime.Now.Ticks + "&r=" + Random.Range(int.MinValue, int.MaxValue)); // tip = "下载文件 " + u + " " + www.progress; //#if UNITY_EDITOR // Debug.Log(tip); //#endif // float timeStart = Time.time; // yield return www; // networkSpeed = www.bytesDownloaded / (Time.time - timeStart); // UILoadingHelper.SetProgress(www.progress * apkSize / (resSize + apkSize) + resSize / (apkSize + resSize)); // if (!string.IsNullOrEmpty(www.error)) // { // Debug.LogError(www.error); // if (FileUtils.IsAllCDNCantDownload()) // { // string msg = string.Format("网络错误(error : 801)\r\n{0}", www.responseHeaders.ContainsKey("STATUS") ? www.responseHeaders["STATUS"] : www.error); // UIMessageBoxHelper.Show(msg, // () => // { // ReStart(); // } // ); // yield return new WaitForSeconds(4f); // } // else // { // FileUtils.ChangeCDNState(); // //u = FileUtils.url + "/" + url; // //www = new WWW(u + "?time=" + System.DateTime.Now.Ticks + "&r=" + Random.Range(int.MinValue, int.MaxValue)); // //yield return www; // //tip = "下载文件 " + u + " " + www.progress; // //Debug.Log(tip); // StartCoroutine(DownloadAPKFile(url, callback)); // } // } // if (www.isDone && string.IsNullOrEmpty(www.error)) // { // networkAvgSpeed += www.bytes.Length; // callback(www); // } // } void CheckIsWifi() { if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork) { if (ignore4g == false) { string msg = "你当前处于运营商网络,下载会产生额外流量,是否继续更新?"; UIMessageBoxHelper.Show(msg, () => { ignore4g = true; }, () => { ReStart(); }); } } }
public IEnumerator DownloadFile(string url, OnDownloadFinish callback) { CheckNetStatus(); string u = FileUtils.url + "/" + url; WWW www = new WWW(u + "?time=" + System.DateTime.Now.Ticks + "&r=" + Random.Range(int.MinValue, int.MaxValue)); tip = "下载文件 " + u + " " + www.progress; #if UNITY_EDITOR Debug.Log(tip); #endif float timeStart = Time.time; yield return(www); networkSpeed = www.bytesDownloaded / (Time.time - timeStart); if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); if (FileUtils.IsAllCDNCantDownload()) { string msg = string.Format("网络错误(error : 801)\r\n{0}", www.responseHeaders.ContainsKey("STATUS") ? www.responseHeaders["STATUS"] : www.error); UIMessageBoxHelper.Show(msg, () => { ReStart(); } ); yield return(new WaitForSeconds(4f)); } else { FileUtils.ChangeCDNState(); StartCoroutine(DownloadFile(url, callback)); Debug.Log(tip); } } if (www.isDone && string.IsNullOrEmpty(www.error)) { callback(www); } }
void OnLoadVersionList(WWW www) { bool b = string.IsNullOrEmpty(www.error); byte[] data = www.bytes; if (b) { Debug.Log("version.dat --> " + www.text); VersionData vesion_data = null; try { vesion_data = JsonMapper.ToObject <VersionData>(www.text); } catch (System.Exception e) { Debug.LogError(e.ToString()); UIMessageBoxHelper.Show("CDN版本号配置错误,请联系官方", () => { ReStart(); }); } Version cdn_release = new Version(vesion_data.relase); Version cdn_beta = new Version(vesion_data.beta); if (currentResVersion > cdn_beta)//最高版本,程序版本 { newVersion = currentResVersion; } else if (currentResVersion > cdn_release) //beta 先行版本 { newVersion = cdn_beta; } else // 外网版本 { newVersion = cdn_release; } } SuperBoBo.EventManager.Instance.FireEvent(UpdateEvent.Updater_DownloadVersionListDone, null); }
void CheckPersistDir() { if (File.Exists(Application.persistentDataPath)) { Debug.LogError("缓存文件夹和文件同名"); File.Delete(Application.persistentDataPath); } if (!Directory.Exists(Application.persistentDataPath)) { Debug.LogError("缓存文件夹不存在"); Directory.CreateDirectory(Application.persistentDataPath); } if (File.Exists(Application.persistentDataPath)) { UIMessageBoxHelper.Show("文件夹创建失败,请关闭安全软件或者重启", () => { Application.Quit(); }); } Debug.Log(Application.persistentDataPath); }
public static string TestCDN() { if (_isDev) { return("http://116.236.180.14"); } #if UNITY_EDITOR string content = FileUtils.ReadStringFromStreaming("url.dat", false); if (!string.IsNullOrEmpty(content)) { urls = content.Replace("\r\n", "\n").Split('\n'); foreach (var k in urls) { if (!urlDatas.ContainsKey(k)) { URLData data = new URLData(); data.pingOK = true; data.isUsed = false; data.downloadOK = true; data.url = k; urlDatas.Add(k, data); } } } #else if (!_isReadUrl) { _isReadUrl = true; string content = FileUtils.ReadStringFromStreaming("url.dat", false); if (!string.IsNullOrEmpty(content)) { urls = content.Replace("\r\n", "\n").Split('\n'); foreach (var k in urls) { if (!urlDatas.ContainsKey(k)) { URLData data = new URLData(); data.pingOK = true; data.isUsed = false; data.downloadOK = true; data.url = k; urlDatas.Add(k, data); } } } } #endif if (urls == null || urls.Length == 0) { return(null); } //Common.ShowLoading(); //#if UNITY_EDITOR // foreach (var k in urls) // { // isCDNOK(k); // } //#else // if (!_isTestedCDN) // { // _isTestedCDN = true; // foreach (var k in urls) // { // isCDNOK(k); // } // } //#endif bool isAnyPingOk = true; //foreach (var k in urlDatas) //{ // if (k.Value.pingOK) // { // isAnyPingOk = true; // break; // } //} if (!isAnyPingOk) { //Common.HideLoading(); UIMessageBoxHelper.Show("网络断开" + "(error : 701)", () => { SceneManager.LoadScene("Launcher"); } ); throw new System.Exception("网络断开(error : 701)"); } else { //Common.HideLoading(); URLData data = null; foreach (var k in urlDatas) { if (!k.Value.isUsed && k.Value.downloadOK) { data = k.Value; //Debug.LogError("CDN地址无效,切换新IP:" + data.url); break; } } if (data == null) { return(urls[0]); } return(data.url); } }
public IEnumerator UpdateRes(List <AssetbundleJsonData> updateFiles) { download = 0; tip = " 更新资源中 文件个数 " + updateFiles.Count; Debug.LogError(tip); this.updateFiles = updateFiles; UILoadingHelper.Start(1, null); int taskNum = 10; MultiWWW.instance.Init(20); while (MultiWWW.instance.currentDownload < updateFiles.Count) { if ((downFileCount - MultiWWW.instance.currentDownload) <= (taskNum / 2) && downFileCount < updateFiles.Count) { for (int i = 0; i < taskNum; i++) { if (downFileCount < updateFiles.Count) { string urlFile = string.Format("{0}/{1}/{2}", ResSetting.GetChannelRes(), newVersion, updateFiles[downFileCount].resName); MultiWWW.instance.AddJob(urlFile, OnDonwloadResFinish, updateFiles[downFileCount]); downFileCount++; } } } if (condition) { UIMessageBoxHelper.Cancel(); if (MultiWWW.instance.current != null) { tip = "下载文件 " + MultiWWW.instance.current.url; } yield return(MultiWWW.instance.StartDownload()); } if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork) { condition = true; } if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork) { if (ignore4g == false) { condition = false; string msg = "你当前处于运营商网络,下载会产生额外流量,是否继续更新?"; UIMessageBoxHelper.Show(msg, () => { ignore4g = true; condition = true; }, () => { condition = false; ReStart(); }); } } if (Application.internetReachability == NetworkReachability.NotReachable) { condition = false; string msg = string.Format("网络断开,尝试重连中..."); UIMessageBoxHelper.Show( msg, () => { }, () => { ReStart(); }); } if (condition == false) { yield return(new WaitForSeconds(2f)); } UILoadingHelper.SetProgress((float)MultiWWW.instance.currentDownload / (float)updateFiles.Count * (resSize / (resSize))); } UILoadingHelper.SetProgress(resSize / (resSize)); }