private void IndexDownloadWorker_DoWork(object sender, DoWorkEventArgs e) { ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Define.GetIndexUrl()); HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); Stream fileStream = new FileStream(Define.GetFullIndexPath(), FileMode.Create); long countSize = 0; long originSize = response.ContentLength / 100; byte[] bArr = new byte[10000]; int size = responseStream.Read(bArr, 0, bArr.Length); while (size > 0) { if (IndexDownloadWorker.CancellationPending) { e.Cancel = true; break; } fileStream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, bArr.Length); countSize = countSize + size; long result = countSize / originSize; IndexDownloadWorker.ReportProgress((int)result); } fileStream.Close(); responseStream.Close(); IndexDownloadWorker.ReportProgress(100); if (e.Cancel) { IndexDownloadWorker.ReportProgress(0); File.Delete(Define.GetFullIndexPath()); } }
private void btnFinish_Click(object sender, EventArgs e) { if (cacheVersionInfo == null) { return; } Define.GameVersion = cacheVersionInfo.version; Define.IndexName = cacheVersionInfo.indexName; Define.VersionUpdateTime = cacheVersionInfo.updateTime; Define.ChangeServerTo(chosenServer); if (!Define.VersionDic.ContainsKey(cacheVersionInfo.version)) { Define.VersionDic.Add(cacheVersionInfo.version, cacheVersionInfo.indexName); } FileManager.SaveVersionFile(Define.CurrentServer); Define.IndexDic.Clear(); Close(); }
//覆盖保存urls.json文件。正常情况下新文件中url的数量总是会大于等于旧文件,因此覆盖不会产生数据丢失。 //注意:但在切换区服时会清除缓存中的url数据,因此需先使用该函数进行保存,再进行切换,否则会产生数据丢失。 public static void SaveVersionFile(SERVER_TYPE currentServer) { Dictionary <int, string> newDic = Define.VersionDic.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); StringBuilder sb = new StringBuilder(); sb.Append("[\n\t\""); foreach (var url in newDic) { sb.Append(Define.GetSourceUrlByServer() + url.Key + "/production/" + Define.GetUnityVersion(url.Key) + "/Android/" + url.Value + "\""); if (url.Key != newDic.Last().Key) { sb.Append(",\n\t\""); } else { sb.Append("\n]"); } } string str = sb.ToString(); StreamWriter sw; if (Define.CurrentServer == SERVER_TYPE.JP) { sw = new StreamWriter(Define.LocalPath + @"\urls.json"); sw.Write(str); sw.Close(); } else if (Define.CurrentServer == SERVER_TYPE.CNT) { sw = new StreamWriter(Define.LocalPath + @"\urls_cn.json"); sw.Write(str); sw.Close(); } }
private void AssetDowloadWorker_DoWork(object sender, DoWorkEventArgs e) { List <string> nameList; nameList = (List <string>)e.Argument; if (!Directory.Exists(Define.LocalPath + Define.AssetPath + "/" + Define.GameVersion)) { Directory.CreateDirectory(Define.LocalPath + Define.AssetPath + "/" + Define.GameVersion); } int total = nameList.Count; int progress = 0; long currntSize = 0; long totalSize = 0; assetProgress = total > 1 ? "已完成:0/" + total : ""; foreach (var name in nameList) { totalSize += long.Parse(Define.IndexDic[name].size); } totalSize = totalSize / 100; foreach (var name in nameList) { if (e.Cancel) { return; } string fullPathName = Define.LocalPath + Define.AssetPath + "/" + Define.GameVersion + "/" + name; string url = Define.GetAssetUrl(name); ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); Stream stream = new FileStream(fullPathName, FileMode.Create); byte[] bArr = new byte[10000]; int size = responseStream.Read(bArr, 0, bArr.Length); while (size > 0) { if (AssetDowloadWorker.CancellationPending) { e.Cancel = true; break; } stream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, bArr.Length); currntSize = currntSize + size; long result = currntSize / totalSize; AssetDowloadWorker.ReportProgress((int)result); } stream.Dispose(); responseStream.Dispose(); if (e.Cancel) { File.Delete(fullPathName); // 下载到一半的文件无法使用,需作删除处理 assetProgress = ""; AssetDowloadWorker.ReportProgress(0); } else { progress = progress + 1; assetProgress = total > 1 ? "已完成:" + progress + "/" + total : ""; } } }