Skip to content

Commit 807690d

Browse files
committed
ModAPI.Common: add StreamUtils.cs
1 parent d62374d commit 807690d

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

ModAPI.Common/DownloadClient.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
1+
using ModApi.Common;
2+
using ModAPI.Common.Update;
3+
using System;
24
using System.IO;
5+
using System.IO.Pipes;
36
using System.Net.Http;
4-
using ModAPI.Common.Update;
57

68
namespace ModAPI.Common
79
{
@@ -11,32 +13,7 @@ public class DownloadClient : IDisposable
1113
private HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
1214
private static readonly string httpUserAgent = "Spore-ModAPI-Launcher-Kit/" + UpdateManager.CurrentVersion.ToString();
1315

14-
public delegate void DownloadClientEventHandler(object source, int percentage);
15-
public event DownloadClientEventHandler DownloadProgressChanged = null;
16-
17-
private void copyStreamWithProgress(Stream inputStrem, Stream outputStream)
18-
{
19-
long streamLength = inputStrem.Length;
20-
byte[] buffer = new byte[4096];
21-
long totalBytesRead = 0;
22-
int bytesRead;
23-
int percentageDownloaded = 0;
24-
int percentage;
25-
26-
while ((bytesRead = inputStrem.Read(buffer, 0, buffer.Length)) > 0)
27-
{
28-
outputStream.Write(buffer, 0, bytesRead);
29-
totalBytesRead += bytesRead;
30-
31-
// only trigger event when percentage has changed
32-
percentage = (int)((double)totalBytesRead / (double)streamLength * 100.0);
33-
if (percentageDownloaded != percentage)
34-
{
35-
percentageDownloaded = percentage;
36-
DownloadProgressChanged?.Invoke(this, percentage);
37-
}
38-
}
39-
}
16+
public event StreamUtils.StreamProgressEventHandler DownloadProgressChanged = null;
4017

4118
public DownloadClient(string url)
4219
{
@@ -79,7 +56,7 @@ public void DownloadToFile(string file)
7956
using (var downloadStream = response.Content.ReadAsStreamAsync().Result)
8057
using (var fileStream = new FileStream(file, FileMode.Create))
8158
{
82-
copyStreamWithProgress(downloadStream, fileStream);
59+
StreamUtils.CopyStreamWithProgress(downloadStream, fileStream, this, DownloadProgressChanged);
8360
}
8461
}
8562

@@ -96,7 +73,7 @@ public MemoryStream DownloadToMemory()
9673
using (var downloadStream = response.Content.ReadAsStreamAsync().Result)
9774
{
9875
memoryStream = new MemoryStream((int)downloadStream.Length);
99-
copyStreamWithProgress(downloadStream, memoryStream);
76+
StreamUtils.CopyStreamWithProgress(downloadStream, memoryStream, this, DownloadProgressChanged);
10077
}
10178

10279
return memoryStream;

ModAPI.Common/ModApi.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="Permissions.cs" />
6868
<Compile Include="Properties\AssemblyInfo.cs" />
6969
<Compile Include="SporePath.cs" />
70+
<Compile Include="StreamUtils.cs" />
7071
<Compile Include="Types.cs" />
7172
<Compile Include="UI\AnimatedScrollViewer.cs" />
7273
<Compile Include="UI\Converters.cs" />

ModAPI.Common/StreamUtils.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.IO;
2+
3+
namespace ModApi.Common
4+
{
5+
public class StreamUtils
6+
{
7+
public delegate void StreamProgressEventHandler(object source, int percentage);
8+
9+
public static void CopyStreamWithProgress(Stream inputStrem, Stream outputStream, object eventSource, StreamProgressEventHandler eventHandler)
10+
{
11+
long streamLength = inputStrem.Length;
12+
byte[] buffer = new byte[4096];
13+
long totalBytesRead = 0;
14+
int bytesRead;
15+
int percentageDownloaded = 0;
16+
int percentage;
17+
18+
while ((bytesRead = inputStrem.Read(buffer, 0, buffer.Length)) > 0)
19+
{
20+
outputStream.Write(buffer, 0, bytesRead);
21+
totalBytesRead += bytesRead;
22+
23+
// only trigger event when percentage has changed
24+
percentage = (int)((double)totalBytesRead / (double)streamLength * 100.0);
25+
if (percentageDownloaded != percentage)
26+
{
27+
percentageDownloaded = percentage;
28+
eventHandler?.Invoke(eventSource, percentage);
29+
}
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)