Пример #1
0
        public static AvailableUpdates CheckForUpdate(Game game)
        {
            AvailableUpdates result = new AvailableUpdates();
            GameUpdate availableUpdates;

            if (!string.IsNullOrEmpty(game.UpdateService))
            {
                try
                {
                    using (var responseStream = HttpRequestManager.Current.GetResponseStream(game.UpdateService))
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            availableUpdates = JsonConvert.DeserializeObject<GameUpdate>(reader.ReadToEnd());
                        }
                    }

                    var currentVersion = FileSystem.Current.GetFile(Resources.GamesManagerExecutable).AssemblyVersion;

                    //if the available version is specified and is newer than what we have already, then the framework needs an update
                    if (availableUpdates.Framework!=null && new Version(availableUpdates.Framework.Version) > currentVersion)
                    {
                        result.Framework = availableUpdates.Framework;
                    }

                    //if the available version isn't newer than what we have already, then don't bother updating.
                    if (availableUpdates.Latest!=null && new Version(availableUpdates.Latest.Version) > game.Version)
                    {
                        if (availableUpdates.UpdateOlderVersions != null)
                        {
                            //try to find a partial update for this version if possible
                            foreach (var olderVersion in availableUpdates.UpdateOlderVersions)
                            {
                                if (new Version(olderVersion.FromVersion) == game.Version)
                                {
                                    result.Game = new UpdateDownload
                                               {
                                                   Url = olderVersion.Url,
                                                   Version = availableUpdates.Latest.Version,
                                                   MD5 = olderVersion.MD5
                                               };
                                }
                            }
                        }

                        //otherwise resort to a full update
                        if (result.Game==null) result.Game = availableUpdates.Latest;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Current.Write(LogInfoLevel.Error, "Unable to read " + game.UpdateService + " - " + ex);
                }
            }
            return result;
        }