Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#pragma once
2
 
3
 
4
namespace AutoUpdater {
5
 
6
	using namespace System;
7
	using namespace System::ComponentModel;
8
	using namespace System::Collections;
9
	using namespace System::Windows::Forms;
10
	using namespace System::Data;
11
	using namespace System::Drawing;
12
 
13
	/// <summary>
14
	/// Summary for Form1
15
	///
16
	/// WARNING: If you change the name of this class, you will need to change the
17
	///          'Resource File Name' property for the managed resource compiler tool
18
	///          associated with all .resx files this class depends on.  Otherwise,
19
	///          the designers will not be able to interact properly with localized
20
	///          resources associated with this form.
21
	/// </summary>
22
	public ref class Form1 : public System::Windows::Forms::Form
23
	{
24
	public:
25
		Form1(System::String ^sDownloadFile)
26
		{
27
			InitializeComponent();
28
 
29
			m_bRunSilent = false;
30
			m_bUpdated = false;
31
			this->Text = L"Plugin Manager Auto Updater V" + (float)VERSION;
32
 
33
			m_iFailed = 0;
34
			m_iFailed2 = 0;
35
 
36
			m_sFile = sDownloadFile;
37
			while ( m_sFile->EndsWith(" ") )
38
				m_sFile->Remove(m_sFile->Length - 1, 1);
39
 
40
			int pos = m_sFile->LastIndexOf("--");
41
			while ( pos != -1 ) {
42
				String ^checkSwitch = m_sFile->Substring(pos);
43
				if ( String::Compare(checkSwitch, "--silent", true) == 0 ) 
44
					m_bRunSilent = true;
45
				m_sFile = m_sFile->Substring(0, pos - 1);
46
				pos = m_sFile->LastIndexOf("--");
47
			}
48
 
49
			this->LabFile->Text = m_sFile;
50
			m_sSaveFile = System::IO::Path::GetTempPath() + m_sFile->Substring(m_sFile->LastIndexOf("/") + 1);
51
 
52
			backgroundWorker1->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &Form1::Background_Finished );
53
			backgroundWorker1->ProgressChanged += gcnew ProgressChangedEventHandler( this, &Form1::Background_Progress );
54
			backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &Form1::Background2_Finished );
55
		}
56
 
57
		bool IsUpdated() { return m_bUpdated; }
58
 
59
		bool CheckFile()
60
		{
61
			System::Net::WebRequest ^req = nullptr;
62
			System::Net::WebResponse ^res = nullptr;
63
			try {
64
				req = System::Net::WebRequest::Create(m_sFile);
65
				res = req->GetResponse();
66
 
67
				const int byteConversion = 1024;
68
				double bytes = Convert::ToDouble(res->ContentLength);
69
				if ( bytes >= System::Math::Pow(byteConversion, 3) )
70
				{
71
					this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / System::Math::Pow(byteConversion, 3), 2), " GB");
72
				}
73
				else if ( bytes >= System::Math::Pow(byteConversion, 2) )
74
				{
75
					this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / System::Math::Pow(byteConversion, 2), 2), " MB");
76
				}
77
				else if ( bytes >= byteConversion )
78
				{
79
					this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / byteConversion, 2), " KB");
80
				}
81
				else
82
				{
83
					this->LabSize->Text = System::String::Concat(bytes, " Bytes");
84
				}
85
			}
86
			catch (System::Net::WebException ^ex) {
87
				MessageBox::Show("Unable to find update file on server:\n" + m_sFile + "\n\n" + ex->ToString(), "Auto Updater", MessageBoxButtons::OK, MessageBoxIcon::Error);
88
				return false;
89
			}
90
 
91
			if ( res )
92
				res->Close();
93
 
94
			return true;
95
		}
96
 
97
		void Download()
98
		{
99
			System::Net::WebResponse ^res = nullptr;
100
			System::Net::WebRequest ^req = nullptr;
101
			try {
102
				req = System::Net::WebRequest::Create(m_sFile);
103
				res = req->GetResponse();
104
			}
105
			catch (System::Net::WebException ^) {
106
				m_iFailed = 1;
107
				return;
108
			}
109
 
110
			m_iFailed = 100;
111
			__int64 maxSize = res->ContentLength;
112
			__int64 curSize = 0;
113
 
114
			m_iFailed = 101;
115
 
116
			System::IO::FileStream ^writeStream = nullptr;
117
			try {
118
				 writeStream = gcnew System::IO::FileStream(m_sSaveFile, System::IO::FileMode::OpenOrCreate);
119
			}
120
			catch (System::IO::IOException ^e)
121
			{
122
				MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
123
				m_iFailed = 999;
124
			}
125
			catch (System::Exception ^e)
126
			{
127
				MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
128
				m_iFailed = 999;
129
			}
130
 
131
			do
132
			{
133
				m_iFailed = 103;
134
 
135
				if ( backgroundWorker1->CancellationPending )
136
					break;
137
 
138
				m_iFailed = 104;
139
 
140
 
141
				array<unsigned char> ^readBytes = gcnew array<unsigned char>(4096);
142
				int read = res->GetResponseStream()->Read(readBytes, 0, 4096);
143
 
144
				curSize += (__int64)read;
145
 
146
				int percent = (int)((curSize * 100) / maxSize);
147
				backgroundWorker1->ReportProgress(percent);
148
 
149
				if ( read <= 0 )
150
				{
151
					m_iFailed = 3;
152
					break;
153
				}
154
 
155
				writeStream->Write(readBytes, 0, read);
156
			}
157
			while(1);
158
 
159
			m_iFailed = 105;
160
 
161
			res->GetResponseStream()->Close();
162
			writeStream->Close();
163
 
164
 
165
			if ( backgroundWorker1->CancellationPending )
166
			{
167
				try {
168
					System::IO::File::Delete(m_sSaveFile);
169
				}
170
				catch (System::IO::IOException ^)
171
				{
172
				}
173
				catch (System::Exception ^)
174
				{
175
				}
176
 
177
				m_iFailed = 2;
178
			}
179
		}
180
 
181
		void RunUpdate()
182
		{
183
			System::Diagnostics::ProcessStartInfo ^info = gcnew System::Diagnostics::ProcessStartInfo(m_sSaveFile);
184
//			info->WorkingDirectory = dir;
185
			if ( m_bRunSilent ) {
186
				info->Arguments = "--silent";
187
			}
188
			info->UseShellExecute = false;
189
			info->WindowStyle = System::Diagnostics::ProcessWindowStyle::Normal;
190
			System::Diagnostics::Process ^process = System::Diagnostics::Process::Start(info);
191
			while ( !process->HasExited )
192
			{
193
				process->WaitForExit(1000);
194
			}
195
		}
196
 
197
	protected:
198
		bool	m_bUpdated;
199
		/// <summary>
200
		/// Clean up any resources being used.
201
		/// </summary>
202
		~Form1()
203
		{
204
			if (components)
205
			{
206
				delete components;
207
			}
208
		}
209
 
210
		void Background_Progress(System::Object ^Sender, ProgressChangedEventArgs ^E)
211
		{
212
			this->progressBar1->Value = E->ProgressPercentage;
213
		}
214
 
215
		void Background2_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E)
216
		{
217
			if ( System::IO::File::Exists(m_sSaveFile) )
218
			{
219
				try { System::IO::File::Delete(m_sSaveFile); }
220
				catch (System::IO::IOException ^) {}
221
				catch (System::Exception ^)	{}
222
			}
223
 
224
			m_bUpdated = true;
225
			this->Close();
226
		}
227
 
228
		void Background_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E)
229
		{
230
			if ( m_iFailed == 1 )
231
			{
232
				if ( MessageBox::Show("Unable to download file:\n" + m_sFile, "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry )
233
				{
234
					m_iFailed = 0;
235
					backgroundWorker1->RunWorkerAsync();
236
				}
237
				else
238
					this->Close();
239
			}
240
			else if ( m_iFailed == 2 )
241
			{
242
				if ( MessageBox::Show("Downloading Update has been canceled", "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry )
243
				{
244
					m_iFailed = 0;
245
					backgroundWorker1->RunWorkerAsync();
246
				}
247
				else
248
					this->Close();
249
			}
250
			else if ( m_iFailed == 3 )
251
			{
252
				if ( MessageBox::Show("Nothing was downloaded??", "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry )
253
				{
254
					m_iFailed = 0;
255
					backgroundWorker1->RunWorkerAsync();
256
				}
257
				else
258
					this->Close();
259
			}
260
 
261
			else if ( System::IO::File::Exists(m_sSaveFile) )
262
			{
263
				this->LabTitle->Text = "Updating...";
264
				this->LabFile->Text = m_sSaveFile;
265
				backgroundWorker2->RunWorkerAsync();
266
			}
267
			else
268
			{
269
				if ( MessageBox::Show("Unable to update the plugin manager:\nFailed Message = " + m_iFailed + "\nWrite File = " + m_sSaveFile + "<", "Auto Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry )
270
				{
271
					m_iFailed = 0;
272
					backgroundWorker1->RunWorkerAsync();
273
				}
274
				else
275
					this->Close();
276
			}
277
		}
278
 
279
 
280
	private: System::Windows::Forms::Label^  LabTitle;
281
	protected: 
282
		System::String ^m_sFile;
283
		System::String ^m_sSaveFile;
284
 
285
		int	m_iFailed;
286
		int m_iFailed2;
287
 
288
		bool m_bRunSilent;
289
 
290
	protected: 
291
	private: System::Windows::Forms::Label^  LabFile;
292
	private: System::Windows::Forms::ProgressBar^  progressBar1;
293
	private: System::Windows::Forms::Label^  label1;
294
	private: System::Windows::Forms::Panel^  panel1;
295
	private: System::Windows::Forms::Label^  LabSize;
296
	private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;
297
private: System::ComponentModel::BackgroundWorker^  backgroundWorker2;
298
 
299
 
300
	private:
301
		/// <summary>
302
		/// Required designer variable.
303
		/// </summary>
304
		System::ComponentModel::Container ^components;
305
 
306
#pragma region Windows Form Designer generated code
307
		/// <summary>
308
		/// Required method for Designer support - do not modify
309
		/// the contents of this method with the code editor.
310
		/// </summary>
311
		void InitializeComponent(void)
312
		{
313
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
314
			this->LabTitle = (gcnew System::Windows::Forms::Label());
315
			this->LabFile = (gcnew System::Windows::Forms::Label());
316
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
317
			this->label1 = (gcnew System::Windows::Forms::Label());
318
			this->panel1 = (gcnew System::Windows::Forms::Panel());
319
			this->LabSize = (gcnew System::Windows::Forms::Label());
320
			this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
321
			this->backgroundWorker2 = (gcnew System::ComponentModel::BackgroundWorker());
322
			this->panel1->SuspendLayout();
323
			this->SuspendLayout();
324
			// 
325
			// LabTitle
326
			// 
327
			this->LabTitle->Dock = System::Windows::Forms::DockStyle::Top;
328
			this->LabTitle->Font = (gcnew System::Drawing::Font(L"Arial", 24, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
329
				static_cast<System::Byte>(0)));
330
			this->LabTitle->Location = System::Drawing::Point(0, 0);
331
			this->LabTitle->Name = L"LabTitle";
332
			this->LabTitle->Size = System::Drawing::Size(424, 57);
333
			this->LabTitle->TabIndex = 0;
334
			this->LabTitle->Text = L"Downloading";
335
			this->LabTitle->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
336
			// 
337
			// LabFile
338
			// 
339
			this->LabFile->Dock = System::Windows::Forms::DockStyle::Top;
340
			this->LabFile->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point, 
341
				static_cast<System::Byte>(0)));
342
			this->LabFile->Location = System::Drawing::Point(0, 57);
343
			this->LabFile->Name = L"LabFile";
344
			this->LabFile->Size = System::Drawing::Size(424, 30);
345
			this->LabFile->TabIndex = 1;
346
			this->LabFile->Text = L"label2";
347
			this->LabFile->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
348
			// 
349
			// progressBar1
350
			// 
351
			this->progressBar1->Dock = System::Windows::Forms::DockStyle::Bottom;
352
			this->progressBar1->Location = System::Drawing::Point(0, 141);
353
			this->progressBar1->Name = L"progressBar1";
354
			this->progressBar1->Size = System::Drawing::Size(424, 31);
355
			this->progressBar1->TabIndex = 2;
356
			// 
357
			// label1
358
			// 
359
			this->label1->Dock = System::Windows::Forms::DockStyle::Left;
360
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
361
				static_cast<System::Byte>(0)));
362
			this->label1->Location = System::Drawing::Point(0, 0);
363
			this->label1->Name = L"label1";
364
			this->label1->Size = System::Drawing::Size(126, 54);
365
			this->label1->TabIndex = 3;
366
			this->label1->Text = L"File Size:";
367
			this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
368
			// 
369
			// panel1
370
			// 
371
			this->panel1->Controls->Add(this->LabSize);
372
			this->panel1->Controls->Add(this->label1);
373
			this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
374
			this->panel1->Location = System::Drawing::Point(0, 87);
375
			this->panel1->Name = L"panel1";
376
			this->panel1->Size = System::Drawing::Size(424, 54);
377
			this->panel1->TabIndex = 4;
378
			// 
379
			// LabSize
380
			// 
381
			this->LabSize->Dock = System::Windows::Forms::DockStyle::Fill;
382
			this->LabSize->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point, 
383
				static_cast<System::Byte>(0)));
384
			this->LabSize->Location = System::Drawing::Point(126, 0);
385
			this->LabSize->Name = L"LabSize";
386
			this->LabSize->Size = System::Drawing::Size(298, 54);
387
			this->LabSize->TabIndex = 4;
388
			this->LabSize->Text = L"Unknown";
389
			this->LabSize->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
390
			// 
391
			// backgroundWorker1
392
			// 
393
			this->backgroundWorker1->WorkerReportsProgress = true;
394
			this->backgroundWorker1->WorkerSupportsCancellation = true;
395
			this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
396
			// 
397
			// backgroundWorker2
398
			// 
399
			this->backgroundWorker2->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker2_DoWork);
400
			// 
401
			// Form1
402
			// 
403
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
404
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
405
			this->ClientSize = System::Drawing::Size(424, 172);
406
			this->Controls->Add(this->panel1);
407
			this->Controls->Add(this->progressBar1);
408
			this->Controls->Add(this->LabFile);
409
			this->Controls->Add(this->LabTitle);
410
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
411
			this->Name = L"Form1";
412
			this->Text = L"Plugin Manager Auto Updater V1.00";
413
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
414
			this->panel1->ResumeLayout(false);
415
			this->ResumeLayout(false);
416
 
417
		}
418
#pragma endregion
419
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
420
			if ( !this->CheckFile() )
421
				this->Close();
422
			else
423
				backgroundWorker1->RunWorkerAsync();
424
		 }
425
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
426
			 this->Download();
427
		 }
428
private: System::Void backgroundWorker2_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
429
			 this->RunUpdate();
430
		 }
431
};
432
}
433