TModLoader v1.4.4.9
TModLoader source code documentation
Loading...
Searching...
No Matches

◆ HandleResponse()

DownloadFile Terraria.ModLoader.UI.DownloadManager.DownloadFile.HandleResponse ( WebResponse response,
CancellationToken token )
inlineprivate

Definition at line 83 of file DownloadFile.cs.

84 {
85 long contentLength = response.ContentLength;
86 if (contentLength < 0)
87 {
88 string txt = "Could not get a proper content length for DownloadFile[" + DisplayText + "]";
89 Logging.tML.Error((object)txt);
90 throw new Exception(txt);
91 }
92 string _downloadPath = $"{new FileInfo(FilePath).Directory.FullName}{Path.DirectorySeparatorChar}{DateTime.Now.Ticks}{".tmp"}";
93 _fileStream = new FileStream(_downloadPath, FileMode.Create);
94 Stream responseStream = response.GetResponseStream();
95 int currentIndex = 0;
96 byte[] buf = new byte[1048576];
97 try
98 {
99 int r;
100 while ((r = responseStream.Read(buf, 0, buf.Length)) > 0)
101 {
102 token.ThrowIfCancellationRequested();
103 _fileStream.Write(buf, 0, r);
104 currentIndex += r;
105 this.OnUpdateProgress?.Invoke((float)((double)currentIndex / (double)contentLength), currentIndex, response.ContentLength);
106 }
107 }
109 {
111 Logging.tML.Info((object)("DownloadFile[" + DisplayText + "] operation was cancelled"), (Exception)e2);
112 }
113 catch (Exception e)
114 {
116 Logging.tML.Info((object)"Unknown error", e);
117 }
118 if (!_aborted)
119 {
120 _fileStream?.Close();
121 PreCopy();
122 File.Copy(_downloadPath, FilePath, overwrite: true);
123 File.Delete(_downloadPath);
124 this.OnComplete?.Invoke();
125 }
126 return this;
127 }

References Terraria.ModLoader.UI.DownloadManager.DownloadFile._aborted, Terraria.ModLoader.UI.DownloadManager.DownloadFile._fileStream, Terraria.ModLoader.UI.DownloadManager.DownloadFile.AbortDownload(), Terraria.ModLoader.UI.DownloadManager.DownloadFile.DisplayText, Terraria.ModLoader.UI.DownloadManager.DownloadFile.FilePath, Terraria.ModLoader.UI.DownloadManager.DownloadFile.PreCopy(), and Terraria.ModLoader.Logging.tML.

Referenced by Terraria.ModLoader.UI.DownloadManager.DownloadFile.Download().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: