Пример #1
0
        // Possibly updates to a new version of Ace. This method is blocking.
        // Returns true if an update was applied, false otherwise.
        static bool Update()
        {
            try {
                string    json = Encoding.UTF8.GetString(RequestURL("https://api.github.com/repos/ace-lol/ace/releases").ToArray());
                JsonArray data = SimpleJson.DeserializeObject <JsonArray>(json);
                if (data.Count < 1)
                {
                    return(false);
                }

                JsonObject latest  = (JsonObject)data[0];
                string     release = (string)latest["tag_name"];
                if (release == null)
                {
                    return(false);
                }

                SemVersion newVer;
                // If the semver isn't valid or if we are already on the newest version.
                if (!SemVersion.TryParse(release, out newVer) || newVer <= GetBundleVersion())
                {
                    return(false);
                }

                JsonArray assets = (JsonArray)latest["assets"];
                if (assets == null)
                {
                    return(false);
                }
                if (assets.Count < 1)
                {
                    return(false);
                }

                string[][] updateGroups = new string[][] {
                    new string[] { "bundle.js", "bundle.js" },
                    new string[] { "inject.js", "inject.js" },
                    new string[] { "payload_win.dll", "payload.dll" }
                };

                foreach (string[] group in updateGroups)
                {
                    JsonObject asset = (JsonObject)assets.Find(x => ((string)((JsonObject)x)["name"]) == group[0]);
                    if (asset == null)
                    {
                        continue;
                    }

                    string path = Path.Combine(dataDir, group[1]);

                    MemoryStream newData = RequestURL(((string)asset["browser_download_url"]));
                    if (newData == null)
                    {
                        return(false);
                    }

                    File.WriteAllBytes(path, newData.ToArray());
                }

                return(true);
            } catch (Exception ex) {
                Console.WriteLine("error: " + ex);
                return(false);
            }
        }
Пример #2
0
        static bool Update()
        {
            try
            {
                string[][] updaters = new string[][] {
                    //          { "GitHub Repo Name", "GitHub Asset Name", "File Path", "Local Version" }
                    new string[] { "ace-windows", "Ace.exe", currentExe, LauncherVersion },
                    new string[] { "ace", "bundle.js", bundleJsPath, File.Exists(bundleJsPath) ? GetBundleVersion(File.ReadAllText(bundleJsPath)) : "0.0.0" },
                };

                foreach (string[] updater in updaters)
                {
                    // Delete old file if it exists.
                    if (File.Exists(updater[2] + ".old"))
                    {
                        File.Delete(updater[2] + ".old");
                    }

                    string    json = Encoding.UTF8.GetString(RequestURL($"https://api.github.com/repos/zombiewizzard/{updater[0]}/releases").ToArray());
                    JsonArray data = SimpleJson.DeserializeObject <JsonArray>(json);
                    if (data.Count < 1)
                    {
                        continue;
                    }

                    JsonObject latest  = (JsonObject)data[0];
                    string     release = (string)latest["tag_name"];
                    if (release == null)
                    {
                        continue;
                    }

                    SemVersion newVer;
                    // If the semver isn't vaalid or if we are already on the newsest version
                    if (!SemVersion.TryParse(release, out newVer) || newVer <= updater[3])
                    {
                        continue;
                    }

                    DialogResult dialogResult = MessageBox.Show(
                        "Ace has detected an update is available, would you like to install it now?",
                        "Update downloaded",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question
                        );

                    // If user doesn't want to install update don't check for any more updates.
                    if (dialogResult == DialogResult.No)
                    {
                        return(false);
                    }

                    JsonArray assets = (JsonArray)latest["assets"];
                    if (assets == null)
                    {
                        continue;
                    }
                    if (assets.Count < 1)
                    {
                        continue;
                    }

                    JsonObject asset = (JsonObject)assets.Find(x => ((string)((JsonObject)x)["name"]) == updater[1]);
                    if (asset == null)
                    {
                        continue;
                    }

                    MemoryStream newData = RequestURL((string)asset["browser_download_url"]);
                    if (newData == null)
                    {
                        continue;
                    }

                    File.Move(updater[2], $"{updater[2]}.old");
                    using (FileStream fileWriter = File.OpenWrite(updater[2])) newData.CopyTo(fileWriter);

                    return(true);
                }

                // No updates could be found.
                return(false);
            } catch (Exception ex) {
                Console.WriteLine("Error: " + ex);
                return(false);
            }
        }