forked from Tic-Tac-Toc/DevPro-PicsDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloader.cs
More file actions
146 lines (132 loc) · 5.22 KB
/
Downloader.cs
File metadata and controls
146 lines (132 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PicsDownloader
{
public class Downloader
{
private static string m_path = Path.Combine(Program.AppPath, "pics.zip");
private static bool m_downloaded = false;
public void Start()
{
Thread updateThread = new Thread(InternalDownload) { IsBackground = true };
updateThread.Start();
}
private void InternalDownload()
{
try
{
DownloadPics();
}
catch (Exception ex)
{
Program.Frm.SetText("Error when downloading.");
Thread.Sleep(2000);
}
}
public void DownloadPics()
{
if (!Directory.Exists(Path.Combine(Program.AppPath, "pics")))
{
Directory.CreateDirectory(Path.Combine(Program.AppPath, "pics"));
Directory.CreateDirectory(Path.Combine(Program.AppPath, "pics", "field"));
Directory.CreateDirectory(Path.Combine(Program.AppPath, "pics", "thumbnail"));
}
if (File.Exists("pics.zip"))
ExtractPics(0);
else
{
m_downloaded = false;
using (var m_client = new WebClient())
{
m_client.DownloadProgressChanged += M_client_DownloadProgressChanged;
m_client.DownloadFileCompleted += M_client_DownloadFileCompleted;
m_client.DownloadFileAsync(new Uri("https://github.com/SuperAndroid17/DevPro-Images/archive/master.zip"), m_path);
}
while (!m_downloaded)
Thread.Sleep(10);
}
}
public void ExtractPics(int attempt)
{
ZipFile zipfile = null;
try
{
zipfile = new ZipFile(File.OpenRead(m_path));
}
catch
{
if (attempt == 3)
{
if (MessageBox.Show("Error trying to extract pics, do you want to retry?", "Error Installing", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
ExtractPics(0);
return;
}
else
{
return;
}
}
Thread.Sleep(500);
ExtractPics(attempt + 1);
return;
}
for (int i = 0; i < zipfile.Count; i++)
{
ZipEntry entry = zipfile[i];
if (!entry.IsFile) continue;
double percent = (double)i / zipfile.Count;
int percentInt = (int)(percent * 100);
if (percentInt > 100) percentInt = 100;
if (percentInt < 0) percentInt = 0;
Program.Frm.SetText("Installing " + entry.Name);
Program.Frm.SetProgress(percentInt);
string filename = Path.Combine(Program.AppPath, entry.Name);
string directory = Path.GetDirectoryName(filename);
if (directory.Substring(directory.Length - 4).Trim() == "pics" || directory.Substring(directory.Length - 5).Trim() == "field" || directory.Substring(directory.Length - 9).Trim() == "thumbnail")
{
string[] args = filename.Split('/');
string path = Path.Combine(Program.AppPath);
foreach (string a in args)
{
if (a == "pics")
path = Path.Combine(path, "pics");
if (a == "field")
path = Path.Combine(path, "field");
if (a == "thumbnail")
path = Path.Combine(path, "thumbnail");
}
path = Path.Combine(path, args[args.Length - 1]);
byte[] buffer = new byte[4096];
Stream zipStream = zipfile.GetInputStream(entry);
using (FileStream streamWriter = new FileStream(path, FileMode.Create, FileAccess.Write))
StreamUtils.Copy(zipStream, streamWriter, buffer);
}
}
Program.Frm.SetProgress(100);
zipfile.Close();
File.Delete(Path.Combine(m_path));
MessageBox.Show("It's ok ! Your launcher is ready to play. Good game.");
Application.Exit();
}
private void M_client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
m_downloaded = true;
Program.Frm.SetProgress(0);
Program.Frm.SetText("Extracting pics...");
ExtractPics(0);
}
private void M_client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Program.Frm.SetProgress(e.ProgressPercentage);
}
}
}