Blame | Last modification | View Log | RSS feed
#pragma oncenamespace AutoUpdater {using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;/// <summary>/// Summary for Form1////// WARNING: If you change the name of this class, you will need to change the/// 'Resource File Name' property for the managed resource compiler tool/// associated with all .resx files this class depends on. Otherwise,/// the designers will not be able to interact properly with localized/// resources associated with this form./// </summary>public ref class Form1 : public System::Windows::Forms::Form{public:Form1(System::String ^sDownloadFile){InitializeComponent();m_bRunSilent = false;m_bUpdated = false;this->Text = L"Plugin Manager Auto Updater V" + (float)VERSION;m_iFailed = 0;m_iFailed2 = 0;m_sFile = sDownloadFile;while ( m_sFile->EndsWith(" ") )m_sFile->Remove(m_sFile->Length - 1, 1);int pos = m_sFile->LastIndexOf("--");while ( pos != -1 ) {String ^checkSwitch = m_sFile->Substring(pos);if ( String::Compare(checkSwitch, "--silent", true) == 0 )m_bRunSilent = true;m_sFile = m_sFile->Substring(0, pos - 1);pos = m_sFile->LastIndexOf("--");}this->LabFile->Text = m_sFile;m_sSaveFile = System::IO::Path::GetTempPath() + m_sFile->Substring(m_sFile->LastIndexOf("/") + 1);backgroundWorker1->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &Form1::Background_Finished );backgroundWorker1->ProgressChanged += gcnew ProgressChangedEventHandler( this, &Form1::Background_Progress );backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &Form1::Background2_Finished );}bool IsUpdated() { return m_bUpdated; }bool CheckFile(){System::Net::WebRequest ^req = nullptr;System::Net::WebResponse ^res = nullptr;try {req = System::Net::WebRequest::Create(m_sFile);res = req->GetResponse();const int byteConversion = 1024;double bytes = Convert::ToDouble(res->ContentLength);if ( bytes >= System::Math::Pow(byteConversion, 3) ){this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / System::Math::Pow(byteConversion, 3), 2), " GB");}else if ( bytes >= System::Math::Pow(byteConversion, 2) ){this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / System::Math::Pow(byteConversion, 2), 2), " MB");}else if ( bytes >= byteConversion ){this->LabSize->Text = System::String::Concat(System::Math::Round(bytes / byteConversion, 2), " KB");}else{this->LabSize->Text = System::String::Concat(bytes, " Bytes");}}catch (System::Net::WebException ^ex) {MessageBox::Show("Unable to find update file on server:\n" + m_sFile + "\n\n" + ex->ToString(), "Auto Updater", MessageBoxButtons::OK, MessageBoxIcon::Error);return false;}if ( res )res->Close();return true;}void Download(){System::Net::WebResponse ^res = nullptr;System::Net::WebRequest ^req = nullptr;try {req = System::Net::WebRequest::Create(m_sFile);res = req->GetResponse();}catch (System::Net::WebException ^) {m_iFailed = 1;return;}m_iFailed = 100;__int64 maxSize = res->ContentLength;__int64 curSize = 0;m_iFailed = 101;System::IO::FileStream ^writeStream = nullptr;try {writeStream = gcnew System::IO::FileStream(m_sSaveFile, System::IO::FileMode::OpenOrCreate);}catch (System::IO::IOException ^e){MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);m_iFailed = 999;}catch (System::Exception ^e){MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);m_iFailed = 999;}do{m_iFailed = 103;if ( backgroundWorker1->CancellationPending )break;m_iFailed = 104;array<unsigned char> ^readBytes = gcnew array<unsigned char>(4096);int read = res->GetResponseStream()->Read(readBytes, 0, 4096);curSize += (__int64)read;int percent = (int)((curSize * 100) / maxSize);backgroundWorker1->ReportProgress(percent);if ( read <= 0 ){m_iFailed = 3;break;}writeStream->Write(readBytes, 0, read);}while(1);m_iFailed = 105;res->GetResponseStream()->Close();writeStream->Close();if ( backgroundWorker1->CancellationPending ){try {System::IO::File::Delete(m_sSaveFile);}catch (System::IO::IOException ^){}catch (System::Exception ^){}m_iFailed = 2;}}void RunUpdate(){System::Diagnostics::ProcessStartInfo ^info = gcnew System::Diagnostics::ProcessStartInfo(m_sSaveFile);// info->WorkingDirectory = dir;if ( m_bRunSilent ) {info->Arguments = "--silent";}info->UseShellExecute = false;info->WindowStyle = System::Diagnostics::ProcessWindowStyle::Normal;System::Diagnostics::Process ^process = System::Diagnostics::Process::Start(info);while ( !process->HasExited ){process->WaitForExit(1000);}}protected:bool m_bUpdated;/// <summary>/// Clean up any resources being used./// </summary>~Form1(){if (components){delete components;}}void Background_Progress(System::Object ^Sender, ProgressChangedEventArgs ^E){this->progressBar1->Value = E->ProgressPercentage;}void Background2_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E){if ( System::IO::File::Exists(m_sSaveFile) ){try { System::IO::File::Delete(m_sSaveFile); }catch (System::IO::IOException ^) {}catch (System::Exception ^) {}}m_bUpdated = true;this->Close();}void Background_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E){if ( m_iFailed == 1 ){if ( MessageBox::Show("Unable to download file:\n" + m_sFile, "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry ){m_iFailed = 0;backgroundWorker1->RunWorkerAsync();}elsethis->Close();}else if ( m_iFailed == 2 ){if ( MessageBox::Show("Downloading Update has been canceled", "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry ){m_iFailed = 0;backgroundWorker1->RunWorkerAsync();}elsethis->Close();}else if ( m_iFailed == 3 ){if ( MessageBox::Show("Nothing was downloaded??", "Auto-Updater", MessageBoxButtons::RetryCancel, MessageBoxIcon::Error) == System::Windows::Forms::DialogResult::Retry ){m_iFailed = 0;backgroundWorker1->RunWorkerAsync();}elsethis->Close();}else if ( System::IO::File::Exists(m_sSaveFile) ){this->LabTitle->Text = "Updating...";this->LabFile->Text = m_sSaveFile;backgroundWorker2->RunWorkerAsync();}else{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 ){m_iFailed = 0;backgroundWorker1->RunWorkerAsync();}elsethis->Close();}}private: System::Windows::Forms::Label^ LabTitle;protected:System::String ^m_sFile;System::String ^m_sSaveFile;int m_iFailed;int m_iFailed2;bool m_bRunSilent;protected:private: System::Windows::Forms::Label^ LabFile;private: System::Windows::Forms::ProgressBar^ progressBar1;private: System::Windows::Forms::Label^ label1;private: System::Windows::Forms::Panel^ panel1;private: System::Windows::Forms::Label^ LabSize;private: System::ComponentModel::BackgroundWorker^ backgroundWorker1;private: System::ComponentModel::BackgroundWorker^ backgroundWorker2;private:/// <summary>/// Required designer variable./// </summary>System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>void InitializeComponent(void){System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));this->LabTitle = (gcnew System::Windows::Forms::Label());this->LabFile = (gcnew System::Windows::Forms::Label());this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());this->label1 = (gcnew System::Windows::Forms::Label());this->panel1 = (gcnew System::Windows::Forms::Panel());this->LabSize = (gcnew System::Windows::Forms::Label());this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());this->backgroundWorker2 = (gcnew System::ComponentModel::BackgroundWorker());this->panel1->SuspendLayout();this->SuspendLayout();//// LabTitle//this->LabTitle->Dock = System::Windows::Forms::DockStyle::Top;this->LabTitle->Font = (gcnew System::Drawing::Font(L"Arial", 24, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(0)));this->LabTitle->Location = System::Drawing::Point(0, 0);this->LabTitle->Name = L"LabTitle";this->LabTitle->Size = System::Drawing::Size(424, 57);this->LabTitle->TabIndex = 0;this->LabTitle->Text = L"Downloading";this->LabTitle->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;//// LabFile//this->LabFile->Dock = System::Windows::Forms::DockStyle::Top;this->LabFile->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(0)));this->LabFile->Location = System::Drawing::Point(0, 57);this->LabFile->Name = L"LabFile";this->LabFile->Size = System::Drawing::Size(424, 30);this->LabFile->TabIndex = 1;this->LabFile->Text = L"label2";this->LabFile->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;//// progressBar1//this->progressBar1->Dock = System::Windows::Forms::DockStyle::Bottom;this->progressBar1->Location = System::Drawing::Point(0, 141);this->progressBar1->Name = L"progressBar1";this->progressBar1->Size = System::Drawing::Size(424, 31);this->progressBar1->TabIndex = 2;//// label1//this->label1->Dock = System::Windows::Forms::DockStyle::Left;this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(0)));this->label1->Location = System::Drawing::Point(0, 0);this->label1->Name = L"label1";this->label1->Size = System::Drawing::Size(126, 54);this->label1->TabIndex = 3;this->label1->Text = L"File Size:";this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;//// panel1//this->panel1->Controls->Add(this->LabSize);this->panel1->Controls->Add(this->label1);this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;this->panel1->Location = System::Drawing::Point(0, 87);this->panel1->Name = L"panel1";this->panel1->Size = System::Drawing::Size(424, 54);this->panel1->TabIndex = 4;//// LabSize//this->LabSize->Dock = System::Windows::Forms::DockStyle::Fill;this->LabSize->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(0)));this->LabSize->Location = System::Drawing::Point(126, 0);this->LabSize->Name = L"LabSize";this->LabSize->Size = System::Drawing::Size(298, 54);this->LabSize->TabIndex = 4;this->LabSize->Text = L"Unknown";this->LabSize->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;//// backgroundWorker1//this->backgroundWorker1->WorkerReportsProgress = true;this->backgroundWorker1->WorkerSupportsCancellation = true;this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);//// backgroundWorker2//this->backgroundWorker2->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker2_DoWork);//// Form1//this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;this->ClientSize = System::Drawing::Size(424, 172);this->Controls->Add(this->panel1);this->Controls->Add(this->progressBar1);this->Controls->Add(this->LabFile);this->Controls->Add(this->LabTitle);this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));this->Name = L"Form1";this->Text = L"Plugin Manager Auto Updater V1.00";this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);this->panel1->ResumeLayout(false);this->ResumeLayout(false);}#pragma endregionprivate: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {if ( !this->CheckFile() )this->Close();elsebackgroundWorker1->RunWorkerAsync();}private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {this->Download();}private: System::Void backgroundWorker2_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {this->RunUpdate();}};}