public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            // Set the security protocol, mainly for Windows 7 users.
            ServicePointManager.SecurityProtocol = (ServicePointManager.SecurityProtocol & SecurityProtocolType.Ssl3) | (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12);

            // Initialize variable for the current PP version.
            var forceCheckUpdate = false;

            // Get the current version of the application.
            var result = Version.TryParse(FileVersionInfo.GetVersionInfo(Path.Combine(Environment.CurrentDirectory, "PaisleyPark.exe")).FileVersion, out Version CurrentVersion);

            if (!result)
            {
                MessageBox.Show(
                    "There was an error when trying to read the current version of Paisley Park, you will be prompted to download the latest version.",
                    "Paisley Park Updater",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
                // Force to check the update.
                forceCheckUpdate = true;
            }

            // Create request for Github REST API for the latest release of Paisley Park.
            if (WebRequest.Create("https://api.github.com/repos/LeonBlade/PaisleyPark/releases/latest") is HttpWebRequest request)
            {
                request.Method    = "GET";
                request.UserAgent = "PaisleyPark";
                request.ServicePoint.Expect100Continue = false;

                try
                {
                    using (var r = new StreamReader(request.GetResponse().GetResponseStream()))
                    {
                        // Get the JSON as a JObject to get the properties dynamically.
                        json = JsonConvert.DeserializeObject <JObject>(r.ReadToEnd());
                        // Get tag name and remove the v in front.
                        var tag_name = json["tag_name"].Value <string>().Substring(1);
                        // Form release version from this string.
                        var releaseVersion = new Version(tag_name);
                        // Check if the release is newer.
                        if (releaseVersion > CurrentVersion || forceCheckUpdate)
                        {
                            // Create HTML out of the markdown in body.
                            var html = Markdown.ToHtml(json["body"].Value <string>());
                            // Set the update string
                            UpdateString = $"Paisley Park {releaseVersion.VersionString()} is now available, you have {CurrentVersion.VersionString()}. Would you like to download it now?";
                            // Set HTML in the window.
                            HTML = "<style>body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;ul{margin:0;padding:0;list-style-position:inside;}</style>" + html;
                        }
                        else
                        {
                            // MessageBox.Show("You're up to date!", "Paisley Park Updater", MessageBoxButton.OK, MessageBoxImage.Information);
                            Application.Current.Shutdown();
                        }
                    }
                }
                catch (Exception)
                {
                    var response = MessageBox.Show(
                        "Failed to fetch the latest version! Would you like to visit the page manually to check for the latest release manually?",
                        "Paisley Park Updater",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Error
                        );
                    if (response == MessageBoxResult.Yes)
                    {
                        // Visit the latest releases page on GitHub to download the latest Paisley Park.
                        Process.Start("https://github.com/LeonBlade/PaisleyPark/releases/latest");
                    }
                }
            }
        }
Example #2
0
        public bool Initialize()
        {
            // Get the current version of the application.
            var result = Version.TryParse(FileVersionInfo.GetVersionInfo(Path.Combine(Environment.CurrentDirectory, $"{App.ToolBin}.exe")).FileVersion, out Version CurrentVersion);

            if (!result)
            {
                MessageBox.Show(
                    $"There was an error when trying to read the current version of {App.ToolName}, you will be prompted to download the latest version.",
                    App.UpdaterName,
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
                // Force to check the update.
                ForceCheckUpdate = true;
            }

            // Create request for Github REST API for the latest release of tool.
            if (WebRequest.Create($"https://api.github.com/repos/{App.GithubRepo}/releases/latest") is HttpWebRequest request)
            {
                request.Method    = "GET";
                request.UserAgent = App.ToolName;
                request.ServicePoint.Expect100Continue = false;

                try
                {
                    using (var r = new StreamReader(request.GetResponse().GetResponseStream()))
                    {
                        // Get the JSON as a JObject to get the properties dynamically.
                        json = JsonConvert.DeserializeObject <JObject>(r.ReadToEnd());

                        // Get tag name and remove the v in front.
                        var tag_name = json["tag_name"].Value <string>();
                        // Form release version from this string.
                        var releaseVersion = new Version(tag_name);

                        // Check if the release is newer.
                        if (releaseVersion > CurrentVersion || ForceCheckUpdate)
                        {
                            // Create HTML out of the markdown in body.
                            var html = Markdown.ToHtml(json["body"].Value <string>());
                            // Set the update string
                            StatusLabel = $"{App.ToolName} {releaseVersion.VersionString()} is now available, you have {CurrentVersion.VersionString()}. Would you like to download it now?";
                            // Set HTML in the window.
                            HTML = "<style>body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;margin:8px 10px;padding:0;font-size:12px;}ul{margin:0;padding:0;list-style-position:inside;}</style>" + html;
                        }
                        else
                        {
                            // Alerts that you're up to date.
                            if (AlertUpToDate)
                            {
                                MessageBox.Show("You're up to date!", App.UpdaterName, MessageBoxButton.OK, MessageBoxImage.Information);
                            }

                            // Do not show.
                            return(false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    var response = MessageBox.Show(
                        "Failed to fetch the latest version! Would you like to visit the page manually to check for the latest release manually?",
                        App.UpdaterName,
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Error
                        );
                    if (response == MessageBoxResult.Yes)
                    {
                        // Visit the latest releases page on GitHub to download the latest version.
                        Process.Start($"https://github.com/{App.GithubRepo}/releases/latest");

                        // Do not show.
                        return(false);
                    }
                }
            }

            return(true);
        }