public IAppUpdaterStrategy Resolve(AppUpdaterContext context)
        {
            Checks.ArgumentNotNull(context, "context");

            DebugLogger.Log("Resolving best strategy for updating...");

            if (context.App.IsInstalled())
            {
                int installedVersionId = context.App.GetInstalledVersionId();
                int latestVersionId    = context.App.GetLatestVersionId();

                if (installedVersionId == latestVersionId)
                {
                    DebugLogger.Log("Installed version is the same as the latest version. Using empty strategy.");

                    return(new AppUpdaterEmptyStrategy());
                }

                if (installedVersionId < latestVersionId)
                {
                    DebugLogger.Log("Installed version is older than the latest version. Checking whether cost of updating with diff is lower than cost of updating with content...");

                    if (context.Configuration.CheckConsistencyBeforeDiffUpdate)
                    {
                        DebugLogger.Log("Checking consitency before allowing diff update...");

                        var commandFactory = new AppUpdaterCommandFactory();

                        var checkVersionIntegrity = commandFactory.CreateCheckVersionIntegrityCommand(
                            installedVersionId, context);

                        checkVersionIntegrity.Prepare(context.StatusMonitor);
                        checkVersionIntegrity.Execute(CancellationToken.Empty);

                        if (checkVersionIntegrity.Results.Files.All(
                                fileIntegrity => fileIntegrity.Status == FileIntegrityStatus.Ok))
                        {
                            DebugLogger.Log("Version is consistent. Diff update is allowed.");
                        }
                        else
                        {
                            foreach (var fileIntegrity in checkVersionIntegrity.Results.Files)
                            {
                                if (fileIntegrity.Status != FileIntegrityStatus.Ok)
                                {
                                    DebugLogger.Log(string.Format("File {0} is not consistent - {1}",
                                                                  fileIntegrity.FileName, fileIntegrity.Status));
                                }
                            }

                            DebugLogger.Log(
                                "Version is not consistent. Diff update is forbidden - using content strategy.");

                            return(new AppUpdaterContentStrategy(context));
                        }
                    }

                    var diffCost = GetDiffCost(context);
                    DebugLogger.LogVariable(diffCost, "diffCost");

                    DebugLogger.Log(string.Format("Cost of updating with diff equals {0}.", diffCost));

                    var contentCost = GetContentCost(context);
                    DebugLogger.LogVariable(contentCost, "contentCost");

                    DebugLogger.Log(string.Format("Cost of updating with content equals {0}.", contentCost));

                    if (diffCost < contentCost)
                    {
                        DebugLogger.Log("Cost of updating with diff is lower than cost of updating with content. Using diff strategy.");

                        return(new AppUpdaterDiffStrategy(context));
                    }

                    DebugLogger.Log("Cost of updating with content is lower than cost of updating with diff. Using content strategy.");
                }
                else
                {
                    DebugLogger.Log("Installed version is newer than the latest version. Using content strategy.");
                }
            }
            else
            {
                DebugLogger.Log("Application is not installed. Using content strategy.");
            }

            return(new AppUpdaterContentStrategy(context));
        }
        public StrategyType Resolve([NotNull] AppUpdaterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            try
            {
                _logger.LogDebug("Resolving best strategy for updating...");

                bool isInstallationBroken = context.App.IsInstallationBroken();

                if (context.App.IsFullyInstalled() || isInstallationBroken)
                {
                    int installedVersionId = context.App.GetInstalledVersionId();
                    _logger.LogTrace("installedVersionId = " + installedVersionId);

                    int latestVersionId = context.App.GetLatestVersionId();
                    _logger.LogTrace("latestVersionId = " + latestVersionId);

                    if (installedVersionId == latestVersionId)
                    {
                        _logger.LogDebug("Installed version is the same as the latest version. Using empty strategy.");

                        if (isInstallationBroken)
                        {
                            return(StrategyType.Repair);
                        }

                        return(StrategyType.Empty);
                    }

                    if (installedVersionId < latestVersionId)
                    {
                        _logger.LogDebug("Installed verison is older than latest version...");

#if PATCHKIT_DONT_USE_DIFF_UPDATES
                        _logger.LogDebug(
                            "Installed version is older than the latest version. " +
                            "Using content strategy due to define PATCHKIT_DONT_USE_DIFF_UPDATES");

                        return(StrategyType.Content);
#else
                        if (DoesVersionSupportDiffUpdates(context, installedVersionId))
                        {
                            _logger.LogDebug("Installed version does not support diff updates. Using content strategy");

                            return(StrategyType.Content);
                        }

                        _logger.LogDebug(
                            "Checking whether cost of updating with diff is lower than cost of updating with content...");

                        long contentSize = GetLatestVersionContentSize(context);
                        _logger.LogTrace("contentSize = " + contentSize);

                        long sumDiffSize = GetLatestVersionDiffSizeSum(context);
                        _logger.LogTrace("sumDiffSize = " + sumDiffSize);

                        if (sumDiffSize < contentSize)
                        {
                            _logger.LogDebug(
                                "Cost of updating with diff is lower than cost of updating with content.");

                            if (context.Configuration.CheckConsistencyBeforeDiffUpdate)
                            {
                                _logger.LogDebug("Checking consitency before allowing diff update...");

                                if (!IsVersionIntegral(contentSize, context))
                                {
                                    _logger.LogDebug("Installed version is broken. Using repair&diff strategy.");
                                    return(StrategyType.RepairAndDiff);
                                }

                                _logger.LogDebug("Installed verison is ready for diff updating.");
                            }

                            _logger.LogDebug("Using diff strategy.");

                            return(StrategyType.Diff);
                        }

                        _logger.LogDebug(
                            "Cost of updating with content is lower than cost of updating with diff. Using content strategy.");
#endif
                    }
                    else
                    {
                        _logger.LogDebug("Installed version is newer than the latest version. Using content strategy.");
                    }
                }
                else
                {
                    _logger.LogDebug("Application is not installed. Using content strategy.");
                }

                return(StrategyType.Content);
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to resolve best strategy for updating.", e);
                throw;
            }
        }
Exemplo n.º 3
0
        public StrategyType Resolve(AppUpdaterContext context)
        {
            Checks.ArgumentNotNull(context, "context");

            DebugLogger.Log("Resolving best strategy for updating...");

            if (context.App.IsInstalled())
            {
                int installedVersionId = context.App.GetInstalledVersionId();
                int latestVersionId    = context.App.GetLatestVersionId();

                if (installedVersionId == latestVersionId)
                {
                    DebugLogger.Log("Installed version is the same as the latest version. Using empty strategy.");

                    return(StrategyType.Empty);
                }

                if (installedVersionId < latestVersionId)
                {
#if PATCHKIT_DONT_USE_DIFF_UPDATES
                    DebugLogger.Log(
                        "Installed version is older than the latest version. " +
                        "Using content strategy due to define PATCHKIT_DONT_USE_DIFF_UPDATES");

                    return(StrategyType.Content);
#else
                    DebugLogger.Log(
                        "Installed version is older than the latest version. Checking whether cost of updating with diff is lower than cost of updating with content...");

                    if (context.Configuration.CheckConsistencyBeforeDiffUpdate)
                    {
                        DebugLogger.Log("Checking consitency before allowing diff update...");

                        if (!IsCheckIntegrityValid(context))
                        {
                            return(StrategyType.Content);
                        }
                    }

                    ulong contentCost = (ulong)GetContentCost(context); // bytes
                    ulong diffCost    = GetDiffCost(context);           // bytes
                    DebugLogger.LogVariable(contentCost, "contentCost");
                    DebugLogger.LogVariable(diffCost, "diffCost");
                    DebugLogger.Log(string.Format("Cost of updating with diff equals {0}.", diffCost));
                    DebugLogger.Log(string.Format("Cost of updating with content equals {0}.", contentCost));

                    if (diffCost < contentCost)
                    {
                        DebugLogger.Log(
                            "Cost of updating with diff is lower than cost of updating with content. Using diff strategy.");

                        return(StrategyType.Diff);
                    }

                    DebugLogger.Log(
                        "Cost of updating with content is lower than cost of updating with diff. Using content strategy.");
#endif
                }
                else
                {
                    DebugLogger.Log("Installed version is newer than the latest version. Using content strategy.");
                }
            }
            else
            {
                DebugLogger.Log("Application is not installed. Using content strategy.");
            }

            return(StrategyType.Content);
        }
        private static int GetLowestVersionWithDiffId(AppUpdaterContext context)
        {
            var appInfo = context.App.RemoteMetaData.GetAppInfo();

            return(appInfo.LowestVersionWithDiff);
        }