static Log() { try { //IsDebugBuild = Debug.isDebugBuild; IsUnityEditor = Application.isEditor; } catch (Exception e) { Log.LogConsole_MultiThread("Log Static Constructor Failed!"); Log.LogConsole_MultiThread(e.Message + " , " + e.StackTrace); } }
static Log() { #if !KENGINE_DLL // isDebugBuild先预存起来,因为它是一个get_属性, 在非Unity主线程里不能用,导致多线程网络打印log时报错 try { //IsDebugBuild = Debug.isDebugBuild; IsUnityEditor = Application.isEditor; } catch (Exception e) { Log.LogConsole_MultiThread("Log Static Constructor Failed!"); Log.LogConsole_MultiThread(e.Message + " , " + e.StackTrace); } #endif }
private void ThreadableResumeDownload(string url, Action <int, int> stepCallback, Action errorCallback, Action successCallback) { System.IO.FileStream downloadFileStream; //打开上次下载的文件或新建文件 long lStartPos = 0; if (_useContinue && System.IO.File.Exists(TmpDownloadPath)) { downloadFileStream = System.IO.File.OpenWrite(TmpDownloadPath); lStartPos = downloadFileStream.Length; downloadFileStream.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针 if (_isLog) { Log.LogConsole_MultiThread("Resume.... from {0}", lStartPos); } } else { downloadFileStream = new System.IO.FileStream(TmpDownloadPath, System.IO.FileMode.OpenOrCreate); lStartPos = 0; } System.Net.HttpWebRequest request = null; //打开网络连接 try { request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); if (lStartPos > 0) { request.AddRange((int)lStartPos); //设置Range值 } if (_isLog) { Log.LogConsole_MultiThread("Getting Response : {0}", url); } //向服务器请求,获得服务器回应数据流 using (var response = request.GetResponse()) // TODO: Async Timeout { TotalSize = (int)response.ContentLength; if (_isLog) { Log.LogConsole_MultiThread("Getted Response : {0}", url); } if (IsFinished) { throw new Exception(string.Format("Get Response ok, but is finished , maybe timeout! : {0}", url)); } else { var totalSize = TotalSize; using (var ns = response.GetResponseStream()) { if (_isLog) { Log.LogConsole_MultiThread("Start Stream: {0}", url); } int downSize = (int)lStartPos; int chunkSize = 10240; byte[] nbytes = new byte[chunkSize]; int nReadSize = (int)lStartPos; while ((nReadSize = ns.Read(nbytes, 0, chunkSize)) > 0) { if (IsFinished) { throw new Exception("When Reading Web stream but Downloder Finished!"); } downloadFileStream.Write(nbytes, 0, nReadSize); downSize += nReadSize; //if(_isLog) Log.Info($"Progress11111111 nReadSize:{nReadSize} ,downloadSize:{downSize} ,totalSize:{totalSize}"); stepCallback(totalSize, downSize); } stepCallback(totalSize, totalSize); request.Abort(); downloadFileStream.Close(); } } } if (_isLog) { Log.LogConsole_MultiThread("下载完成: {0}", url); } if (File.Exists(SaveFullPath)) { File.Delete(SaveFullPath); } File.Move(TmpDownloadPath, SaveFullPath); } catch (Exception ex) { if (_isLog) { Log.LogConsole_MultiThread("下载过程中出现错误:" + ex.ToString()); } downloadFileStream.Close(); if (request != null) { request.Abort(); } try { if (File.Exists(TmpDownloadPath)) { File.Delete(TmpDownloadPath); // delete temporary file } } catch (Exception e) { if (_isLog) { Log.LogConsole_MultiThread("删除临时下载文件出错:" + e.Message); } } errorCallback(); } successCallback(); }