BiomePainter.Update.btnProgram_Click C# (CSharp) Method

btnProgram_Click() private method

private btnProgram_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void btnProgram_Click(object sender, EventArgs e)
        {
            if (readyToGo[Program])
            {
                Process.Start("http://mblaine.github.io/BiomePainter/downloads");
                return;
            }

            enableButtons(false);
            shouldBeEnabled[Program] = false;
            btnProgram.Text = "Checking for program updates.";
            btnProgram.Refresh();
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/mblaine/BiomePainter/tags");
                request.Method = "GET";
                request.Headers["Accept-Encoding"] = "gzip,deflate";
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                request.UserAgent = userAgent;
                request.Proxy = null;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader responseStream = new StreamReader(response.GetResponseStream());
                String json = responseStream.ReadToEnd();
                response.Close();
                responseStream.Close();

                int latestMajor = -1;
                int latestMinor = -1;
                int latestSubminor = -1;

                MatchCollection matches = new Regex("[\"']name[\"']:\\s*[\"'](\\d+)\\.(\\d+)\\.(\\d+)[\"']", RegexOptions.Multiline).Matches(json);
                foreach (Match m in matches)
                {
                    int tagMajor = Int32.Parse(m.Groups[1].Value);
                    int tagMinor = Int32.Parse(m.Groups[2].Value);
                    int tagSubminor = Int32.Parse(m.Groups[3].Value);

                    if (tagMajor > latestMajor)
                    {
                        latestMajor = tagMajor;
                        latestMinor = tagMinor;
                        latestSubminor = tagSubminor;
                    }
                    else if (tagMajor == latestMajor && tagMinor > latestMinor)
                    {
                        latestMinor = tagMinor;
                        latestSubminor = tagSubminor;
                    }
                    else if (tagMajor == latestMajor && tagMinor == latestMinor && tagSubminor > latestSubminor)
                    {
                        latestSubminor = tagSubminor;
                    }
                }

                if (latestMajor == -1)
                {
                    btnProgram.Text = "Unable to determine latest version. Click to open the list of downloads in your web browser.";
                    shouldBeEnabled[Program] = true;
                    readyToGo[Program] = true;
                    enableButtons(true);
                    picProgram.Image = Properties.Resources.x;
                    return;
                }

                Match match = new Regex("^(\\d+)\\.(\\d+)\\.(\\d+)").Match(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion);

                int major, minor, subminor;
                if (match.Groups.Count == 4 && Int32.TryParse(match.Groups[1].Value, out major) && Int32.TryParse(match.Groups[2].Value, out minor) && Int32.TryParse(match.Groups[3].Value, out subminor))
                {
                    if (latestMajor > major || (latestMajor == major && latestMinor > minor) || (latestMajor == major && latestMinor == minor && latestSubminor > subminor))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Biome Painter {0}.{1}", latestMajor, latestMinor);
                        if (latestSubminor != 0)
                            sb.AppendFormat(".{0}", latestSubminor);
                        sb.Append(" now available! Click to open the list of downloads in your web browser.");
                        btnProgram.Text = sb.ToString();
                        shouldBeEnabled[Program] = true;
                        readyToGo[Program] = true;
                        enableButtons(true);
                        picProgram.Image = Properties.Resources.bang;
                        return;
                    }
                    else
                    {
                        btnProgram.Text = "You are running the latest version of Biome Painter. No action is necessary.";
                        shouldBeEnabled[Program] = false;
                        readyToGo[Program] = false;
                        enableButtons(true);
                        picProgram.Image = Properties.Resources.check;
                        return;
                    }
                }
                else
                {
                    btnProgram.Text = "Unable to determine version number. Click to open the list of downloads in your web browser.";
                    shouldBeEnabled[Program] = true;
                    readyToGo[Program] = true;
                    enableButtons(true);
                    picProgram.Image = Properties.Resources.x;
                    return;
                }
            }
            catch (Exception)
            {
                btnProgram.Text = "Unable to determine latest version. Click to open the list of downloads in your web browser.";
                shouldBeEnabled[Program] = true;
                readyToGo[Program] = true;
                enableButtons(true);
                picProgram.Image = Properties.Resources.x;
                return;
            }
        }