示例#1
0
        private static void AsyncCheckVersion(object foo)
        {
            Exception error  = null;
            bool      manual = (bool)foo;

            if (UrlManager.UpdateMain != null)
            {
                error = null;
                try { CheckSite(UrlManager.UpdateMain, manual); }                 //official https
                catch (Exception ex1)
                {
                    error = ex1;
                    if (UrlManager.UpdateMirror != null)
                    {
                        error = null;
                        try { CheckSite(UrlManager.UpdateMirror, manual); }                             //http mirror
                        catch (Exception ex2) { error = ex2; }
                    }
                }
            }
            else if (UrlManager.UpdateMirror != null)             //only mirror configured
            {
                error = null;
                try { CheckSite(UrlManager.UpdateMirror, manual); }                 //http mirror
                catch (Exception ex3) { error = ex3; }
            }

            if (error != null && manual)
            {
                NewVersion?.Invoke(null, null, error);
            }
        }
示例#2
0
        private async void RunInternal()
        {
            string manifestContent = await DownloadManifestContent();

            if (manifestContent == null)
            {
                return;
            }

            JObject manifest = ParseManifestContent(manifestContent);

            if (manifest == null)
            {
                return;
            }

            if (DetermineVersionAndArchiveName(manifest, out Version version, out string archiveName) == false)
            {
                return;
            }

            if (version > App.Version)
            {
                await dispatcher.BeginInvoke((Action) delegate
                {
                    var downloadUrl = new Uri(DownloadBaseUrl + archiveName, UriKind.Absolute);
                    NewVersion?.Invoke(this, new NewVersionEventArgs(version, downloadUrl));
                });
            }
        }
示例#3
0
        private static void CheckSite(string site, bool manual)
        {
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.Headers.Add("User-Agent: .Net WebClient");
                string json = wc.DownloadString(site);

                List <OnlineVersion> versions = Tools.JSONParser.FromJson <List <OnlineVersion> >(json);

                bool build = Settings.GetObject("Auto Update Build", false) || manual;
                bool pre   = Settings.GetObject("Auto Update Pre", false);

                Version       current = typeof(GitHub).Assembly.GetName().Version;
                OnlineVersion found   = null;
                foreach (OnlineVersion ov in versions)   //version start with higher first
                {
                    if (!ov.IsValid)                     //skip invalid
                    {
                        continue;
                    }
                    if (!pre && ov.IsPreRelease)                     //skip pre-release
                    {
                        continue;
                    }
                    if (!build && ov.Version.Build != 0)                     //skip build version (only update to minor number change)
                    {
                        continue;
                    }
                    if (found != null && ov.Version < found.Version)                     //already found a better match
                    {
                        continue;
                    }

                    if (ov.Version > current)
                    {
                        found = ov;
                    }
                }

                if (found != null)
                {
                    NewVersion?.Invoke(current, found, null);
                }
                else if (manual)                 //notify "no new version"
                {
                    NewVersion?.Invoke(current, null, null);
                }
            }
        }
示例#4
0
        private static void CheckSite(string site)
        {
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.Headers.Add("User-Agent: .Net WebClient");
                string json = wc.DownloadString(site);

                string url        = null;
                string versionstr = null;
                string name       = null;

                foreach (Match m in Regex.Matches(json, @"""browser_download_url"":""([^""]+)"""))
                {
                    if (url == null)
                    {
                        url = m.Groups[1].Value;
                    }
                }
                foreach (Match m in Regex.Matches(json, @"""tag_name"":""v([^""]+)"""))
                {
                    if (versionstr == null)
                    {
                        versionstr = m.Groups[1].Value;
                    }
                }
                foreach (Match m in Regex.Matches(json, @"""name"":""([^""]+)"""))
                {
                    if (name == null)
                    {
                        name = m.Groups[1].Value;
                    }
                }

                Version current = typeof(GitHub).Assembly.GetName().Version;
                Version latest  = new Version(versionstr);

                if (current < latest)
                {
                    bool minor = Settings.GetObject("Auto Update", false);
                    bool build = Settings.GetObject("Auto Update Build", false);

                    if ((current.Major != latest.Major) || (current.Minor != latest.Minor && minor) || (current.Build != latest.Build && build))
                    {
                        NewVersion?.Invoke(current, latest, name, url);
                    }
                }
            }
        }
示例#5
0
 public void CommandNewVersion(string version)
 {
     Version = version;
     NewVersion?.Invoke(this, null);
 }
示例#6
0
        private static async void AsyncCheckVersion(int retry = 0)
        {
            var http = new HttpClient
            {
                BaseAddress = new Uri(versionURI)
            };

            var json          = JsonSerializer.Create();
            var cachedVersion = GetCachedVersion(out var notify);
            var req           = string.Empty;

            try
            {
                req = await http.GetStringAsync(string.Empty);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("[Updater] Failed to get current version. Caused by");
                Console.WriteLine(e.ToString());
                if (retry < netRetryCount)
                {
                    AsyncCheckVersion(retry + 1);
                    return;
                }
            }

            VersionUpdate update = new VersionUpdate();

            try
            {
                using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(req)))
                {
                    update = json.Deserialize <VersionUpdate>(jsonReader);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to read current version info.");
                return;
            }


            var currentVer   = Version.Parse(update.Version);
            var cacheValidTo = DateTime.UtcNow + update.CacheDuration;

            if (currentVer == null || cachedVersion >= currentVer)
            {
                currentVer = cachedVersion;
            }

            if (currentVer > LauncherVersion)
            {
                var args = new VersionUpdateEventArgs(update);
                NewVersion?.Invoke(null, args);

                if (notify)
                {
                    NotifyNewVersion?.Invoke(null, args);
                    notify = false;
                }
            }

            WriteVersionCache(notify, currentVer, cacheValidTo);
        }