Esempio n. 1
0
        // Used to check for and download non-canary releases (including documentation updates)
        public static async Task CheckUpdate(bool Overwrite, string releaseTag = "", bool manual = false,
                                             string openFile = null, bool checkDocumentation     = false,
                                             bool Automatic  = false)
        {
            // If canary is active, disable it
            if (File.Exists(AppPath + "\\Canary\\Active"))
            {
                File.Delete(AppPath + "\\Canary\\Active");
            }

            string repoOwner = mainRepo.Split('/')[0];
            string repoName  = mainRepo.Split('/')[1];

            try
            {
                // check to see if the user is online, and that github is up and running.
                Console.Write("Checking connection to server... ");
                try
                {
                    using (Ping s = new Ping())
                    {
                        IPStatus status = s.Send("www.github.com").Status;
                        Console.WriteLine(status);
                        if (status != IPStatus.Success)
                        {
                            Console.WriteLine("Failed to connect");
                            if (manual)
                            {
                                MessageBox.Show("Unable to connect to GitHub. The website may be down.");
                            }

                            return;
                        }
                    }
                }
                catch
                {
                    throw new HttpRequestException();
                }
                // Initiate the github client.

                // get Release
                IReadOnlyList <Release> AllReleases = await github.Repository.Release.GetAll(repoOwner, repoName);

                IReadOnlyList <Release> releases = null;
                Release release       = null;
                bool    documentation = false;
                if (AllReleases.Count == 0)
                {
                    goto UpdateDL;
                }

                // Remove all pre-release versions from the list (Prerelease versions are exclusively documentation updates)
                releases = AllReleases.Where(r => !r.Prerelease).ToList();
                if (releases[0].TagName != releaseTag)
                {
                    release = releases[0];
                    goto UpdateDL;
                }

                if (checkDocumentation)
                {
                    // Figure out what documentation version you're on
                    string docVer = "";
                    try
                    {
                        docVer = File.ReadAllLines(AppPath + "\\InternalDocumentation\\version.txt")[0];
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        MessageBox.Show(
                            "ERROR: Documentation Version could not be found. Downloading the latest documentation release.");
                        await ForceDownloadDocumentation();

                        try
                        {
                            docVer = File.ReadAllLines(AppPath + "\\InternalDocumentation\\version.txt")[0];
                            // Documentation has already been updated, no need to check again.
                            checkDocumentation = false;
                        }
                        catch (Exception e2)
                        {
                            Console.WriteLine(e2.Message);
                            MessageBox.Show(
                                "ERROR: Documentation Version still could not be found. Please report this on Discord or Github.\n" +
                                e2.Message);
                        }
                    }

                    // Don't need to check for update unless the latest release is a prerelease (documentation is included in full releases)
                    if (AllReleases[0].Prerelease)
                    {
                        // This track is shared by canary updates. Ensure that a documentation release is found.
                        foreach (Release r in AllReleases)
                        {
                            if (r.TagName == releaseTag || r.TagName == docVer)
                            {
                                // Documentation is already up-to-date if the latest release (either of documentation or the full program) has been downloaded
                                release = null;
                                break;
                            }

                            if (r.Prerelease && r.Name.ToLower().Contains("documentation"))
                            {
                                release       = r;
                                documentation = true;
                                break;
                            }
                        }
                    }
                }

UpdateDL:
                // If there are no releases available, download will fail.
                if (release == null || release.Assets.Count == 0)
                {
                    if (manual)
                    {
                        MessageBox.Show("No updates found.");
                    }

                    return;
                }

                // Show warnings as applicable to those using the automatic updater
                if (Automatic)
                {
                    if (release.Body.Contains("WARNING: "))
                    {
                        if (release.Body.StartsWith("WARNING: "))
                        {
                            DialogResult dr = MessageBox.Show(
                                release.Body.Substring(0, release.Body.IndexOf("\n") - 1) +
                                "\n\nWould you like to continue updating?", "Automatic Update Warning",
                                MessageBoxButtons.YesNo);
                            if (dr != DialogResult.Yes)
                            {
                                return;
                            }
                        }
                        else
                        {
                            DialogResult dr = MessageBox.Show(
                                release.Body.Substring(release.Body.IndexOf("WARNING: ")) +
                                "\n\nWould you like to continue updating?", "Automatic Update Warning",
                                MessageBoxButtons.YesNo);
                            if (dr != DialogResult.Yes)
                            {
                                return;
                            }
                        }
                    }

                    if (GetOpenWindowsCount() > 1 &&
                        MessageBox.Show(
                            "Update to " + release.Name +
                            " was found. Would you like to download now?\n\nAll current windows will be closed and changes will be lost!",
                            "Updater", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    {
                        return;
                    }
                }
                else // Allow the user to choose whether or not to download a release if not using the automatic updater
                {
                    if (MessageBox.Show(
                            release.Name + " is available!\n\nThis release:\n\n" + release.Body + "\n\nUpdate now?" +
                            (manual ? "\n\nThe program will be closed, and changes will not be saved!" : ""), "Update",
                            MessageBoxButtons.YesNo) != DialogResult.Yes)
                    {
                        return;
                    }
                }

                await DownloadRelease(release, Overwrite, Automatic, manual, documentation, openFile);
            }
            catch (HttpRequestException)
            {
                if (manual)
                {
                    MessageBox.Show("Unable to connect to the internet.");
                }
            }
            catch (Exception e)
            {
                if (manual)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
Esempio n. 2
0
        public static async Task ShowCanaryChangelog()
        {
            string changelog = "";
            string newSha;
            string oldSha;
            string newBranch;
            string oldBranch;
            string newRepo;
            string oldRepo;
            string Filename   = AppPath + "\\Canary\\Old";
            bool   showErrors = true;

            try
            {
                newSha = File.ReadAllLines(AppPath + "\\Canary\\New")[2];
                oldSha = File.ReadAllLines(AppPath + "\\Canary\\Old")[2];
                try
                {
                    newBranch = File.ReadAllLines(AppPath + "\\Canary\\New")[3];
                    oldBranch = File.ReadAllLines(AppPath + "\\Canary\\Old")[3];
                }
                catch
                {
                    // Assume that this is updating from an old version before branch data was tracked.
                    newBranch = "";
                    oldBranch = "";
                }

                try
                {
                    newRepo = File.ReadAllLines(AppPath + "\\Canary\\New")[4];
                    oldRepo = File.ReadAllLines(AppPath + "\\Canary\\Old")[4];
                }
                catch
                {
                    // Assume that this is updating from an old version before repo data was tracked.
                    newRepo = oldRepo = "";
                }
            }
            catch
            {
                try
                {
                    newSha = File.ReadAllLines(AppPath + "\\Canary\\New")[2];
                    oldSha = "";
                    try
                    {
                        newBranch = File.ReadAllLines(AppPath + "\\Canary\\New")[3];
                        oldBranch = newBranch;
                    }
                    catch
                    {
                        // Assume that this is updating from an old version before branch data was tracked.
                        newBranch = "";
                        oldBranch = "";
                    }

                    try
                    {
                        newRepo = File.ReadAllLines(AppPath + "\\Canary\\New")[4];
                        oldRepo = newRepo;
                    }
                    catch
                    {
                        // Assume that this is updating from an old version before repo data was tracked.
                        newRepo = "";
                        oldRepo = "";
                    }

                    showErrors = false;
                }
                catch
                {
                    MessageBox.Show(
                        "Canary changelog could not be shown. Make sure to never disturb the \"Canary\" folder in the installation folder.");
                    return;
                }
            }

            if (newSha == oldSha)
            {
                MessageBox.Show("Welcome to BrawlCrate Canary! You were already on the latest commit.");
                if (File.Exists(Filename))
                {
                    File.Delete(Filename);
                }

                return;
            }

            if (newRepo != oldRepo)
            {
                MessageBox.Show("Welcome to BrawlCrate Canary! You are now tracking the " + newBranch +
                                " branch of the " + newRepo + " repository instead of the " + oldBranch +
                                " branch of the " + oldRepo +
                                " repository. Canary changelog is not supported when switching repositories, so please check online to see differences.");
                if (File.Exists(Filename))
                {
                    File.Delete(Filename);
                }

                return;
            }

            if (newBranch != oldBranch)
            {
                MessageBox.Show("Welcome to BrawlCrate Canary! You are now tracking the " + newBranch +
                                " branch instead of the " + oldBranch +
                                " branch. Canary changelog is not supported when switching branches, so please check the Discord for what's been changed.");
                if (File.Exists(Filename))
                {
                    File.Delete(Filename);
                }

                return;
            }

            // check to see if the user is online, and that github is up and running.
            Console.WriteLine("Checking connection to server.");
            using (Ping s = new Ping())
            {
                Console.WriteLine(s.Send("www.github.com").Status);
            }

            char[]   slashes  = { '\\', '/' };
            string[] repoData = currentRepo.Split(slashes);

            try
            {
                Credentials  cr     = new Credentials(Encoding.Default.GetString(_rawData));
                GitHubClient github = new GitHubClient(new ProductHeaderValue("BrawlCrate"))
                {
                    Credentials = cr
                };
                try
                {
                    await github.Repository.Branch.Get(repoData[0], repoData[1], currentBranch);
                }
                catch
                {
                    repoData      = mainRepo.Split(slashes);
                    currentBranch = mainBranch;
                    currentRepo   = mainRepo;
                }

                ApiOptions options = new ApiOptions
                {
                    PageSize  = 120,
                    PageCount = 1
                };
                List <GitHubCommit> commits =
                    (await github.Repository.Commit.GetAll(repoData[0], repoData[1], options)).ToList();
                int  i;
                bool foundCurrentCommit = false;
                for (i = 0; i < commits.Count;)
                {
                    GitHubCommit c = commits[i];
                    if (!foundCurrentCommit && c.Sha != newSha)
                    {
                        commits.Remove(c);
                        continue;
                    }

                    foundCurrentCommit = true;
                    if (c.Sha == oldSha || i > 99)
                    {
                        break;
                    }

                    i++;
                }

                if (i == 0)
                {
                    MessageBox.Show("No changes were found.");
                }

                for (int j = i - 1; j >= 0; j--)
                {
                    if (j >= commits.Count)
                    {
                        continue;
                    }

                    if (j == 99 && showErrors)
                    {
                        changelog += "\n\nMax commits reached. Showing last 100.";
                    }

                    GitHubCommit c = new GitHubCommit();
                    try
                    {
                        c = commits[j];
                    }
                    catch
                    {
                        continue;
                    }

                    changelog += "\n\n========================================================\n\n";
                    try
                    {
                        string s = "#" + c.Sha.Substring(0, 7) + "@" + currentRepo + '\\' + currentBranch + " by " +
                                   c.Author.Login + "\n";
                        changelog += s;
                    }
                    catch
                    {
                        changelog += "#" + c.Sha.Substring(0, 7) + "@" + currentRepo + '\\' + currentBranch + "\n";
                    }

                    changelog += c.Commit.Message;
                }

                changelog += "\n\n========================================================";
                if (!string.IsNullOrEmpty(oldSha))
                {
                    MessageBox.Show("Canary successfully updated from #" + oldSha.Substring(0, 7) + " to #" +
                                    newSha.Substring(0,
                                                     7)); // For some reason, without this, the changelog window never shows.
                }
                else
                {
                    MessageBox.Show(
                        "The last 100 Canary commits will be shown. For a more in-depth view of changes, visit https://github.com/soopercool101/BrawlCrateNext/commits/master");
                }

                CanaryChangelogViewer logWindow = new CanaryChangelogViewer(newSha.Substring(0, 7), changelog);
                logWindow.ShowDialog();
                DirectoryInfo CanaryDir = Directory.CreateDirectory(AppPath + "\\Canary");
                CanaryDir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                if (File.Exists(Filename))
                {
                    File.Delete(Filename);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Canary Changelog could not be shown:\n" + e.Message);
            }
        }