This repository was archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdater.cs
More file actions
152 lines (144 loc) · 5.75 KB
/
Copy pathupdater.cs
File metadata and controls
152 lines (144 loc) · 5.75 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
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GFBattleTester
{
public partial class updater : Form
{
Form opener;
List<string> updatefilelist = new List<string>();
int downloadfilecount = 0;
int currentcount = 0;
bool isLatest = true;
public updater(Form parentForm)
{
InitializeComponent();
opener = parentForm;
}
//label1.Text = "다운로드 완료. 창을 닫아 프로그램을 재시작 해주세요.";
private void updater_Load(object senders, EventArgs e)
{
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
backgroundWorker1.RunWorkerAsync();
}
string getFileSizeMD5(string path)
{
var info = new FileInfo(path);
long size = info.Length;
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(size.ToString());
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
public string GetMD5HashFromFile(string filepath)
{
MD5 md5 = MD5.Create();
FileStream stream = File.OpenRead(filepath);
string hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLower();
stream.Close();
return hash;
}
private void updater_FormClosing(object sender, FormClosingEventArgs e)
{
opener.Close();
System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
}
void Downloadprogresschanged(object sender, DownloadProgressChangedEventArgs args)
{
if (args.TotalBytesToReceive > 0)
{
//progressBar1.Value = args.ProgressPercentage;
}
}
void Downloadcompleted(object sender, DownloadDataCompletedEventArgs args)
{
if (args.Error == null)
{
File.Delete(@updatefilelist[currentcount]);
File.WriteAllBytes(@updatefilelist[currentcount], args.Result);
currentcount++;
}
}
private void BackgroundWorker1_DoWork(object o, DoWorkEventArgs e)
{
WebClient updatercl = new WebClient();
byte[] updatelist = updatercl.DownloadData("https://dhlrunner.github.io/GFBT_update_data_files/update_files.txt");
string[] list = Encoding.UTF8.GetString(updatelist).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string a in list)
{
if (a != "")
{
string filepath = a.Split(',')[0];
string filehash = a.Split(',')[1];
string localhash = string.Empty;
if (File.Exists(filepath))
{
if (localhash != filehash)
{
updatefilelist.Add(filepath);
downloadfilecount++;
isLatest = false;
}
}
else
{
updatefilelist.Add(filepath);
downloadfilecount++;
isLatest = false;
}
}
}
if (!isLatest)
{
backgroundWorker2.RunWorkerAsync();
}
else
{
Close();
opener.Opacity = 100;
}
}
private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
for(int i=0; i<updatefilelist.Count; i++)
{
WebClient wc = new WebClient();
label1.Text = "다운로드 중... (" + (i + 1).ToString() + "/" + downloadfilecount.ToString() + ")";
label2.Text = updatefilelist[i];
byte[] data = wc.DownloadData(new Uri("https://dhlrunner.github.io/GFBT_update_data_files/" + updatefilelist[i]));
File.WriteAllBytes(updatefilelist[i], data);
/*using (WebClient wc = new WebClient())
{
progressBar1.Style = ProgressBarStyle.Blocks;
progressBar1.Value = 0;
wc.DownloadDataCompleted += Downloadcompleted;
wc.DownloadProgressChanged += Downloadprogresschanged;
wc.DownloadDataAsync(new Uri("https://dhlrunner.github.io/GFBT_update_data_files/" + updatefilelist[i]));
Uri a = new Uri("https://dhlrunner.github.io/GFBT_update_data_files/" + updatefilelist[i]);
}*/
}
label1.Text = "다운로드 완료. 창을 닫으면 프로그램이 재실행 됩니다.";
//progressBar1.Style = ProgressBarStyle.Continuous;
//progressBar1.Value = 100;
//opener.Close();
//Close();
}
}
}