Esempio n. 1
0
        /// <summary>
        /// Gets all valid target versions for a specified version of Factorio.
        /// </summary>
        /// <param name="versionToUpdate">The version of Factorio that is to be updated.</param>
        /// <param name="updateSteps">The available update steps provided by the API.</param>
        /// <returns>Returns a list of valid update targets for the specified version of Factorio.</returns>
        public static List <UpdateTarget> GetUpdateTargets(FactorioVersion versionToUpdate, List <UpdateStep> updateSteps)
        {
            var targets = new List <UpdateTarget>();
            var groups  = updateSteps.GroupBy(step => new Version(step.To.Major, step.To.Minor));

            foreach (var group in groups)
            {
                UpdateStep        targetStep = group.MaxBy(step => step.To, new VersionComparer());
                List <UpdateStep> stepChain  = GetStepChain(updateSteps, versionToUpdate.Version, targetStep.To);
                UpdateTarget      target     = new UpdateTarget(stepChain, targetStep.To, targetStep.IsStable);
                targets.Add(target);

                if (!targetStep.IsStable)
                {
                    UpdateStep stableStep = group.Where(step => step.IsStable).MaxBy(step => step.To, new VersionComparer());
                    if (stableStep != null)
                    {
                        stepChain = GetStepChain(updateSteps, versionToUpdate.Version, stableStep.To);
                        target    = new UpdateTarget(stepChain, stableStep.To, true);
                        targets.Add(target);
                    }
                }
            }
            return(targets);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets all valid target versions for a specified version of Factorio.
        /// </summary>
        /// <param name="versionToUpdate">The version of Factorio that is to be updated.</param>
        /// <param name="installedVersions">A collection that contains all installed versions of Factorio.</param>
        /// <param name="updateSteps">The available update steps provided by the API.</param>
        /// <returns>Returns a list of valid update targets for the specified version of Factorio.</returns>
        public static List <UpdateTarget> GetUpdateTargets(FactorioVersion versionToUpdate, ICollection <FactorioVersion> installedVersions, List <UpdateStep> updateSteps)
        {
            var targets = new List <UpdateTarget>();
            var groups  = updateSteps.GroupBy(step => new Version(step.To.Major, step.To.Minor));

            foreach (var group in groups)
            {
                UpdateStep        targetStep = group.MaxBy(step => step.To, new VersionComparer());
                List <UpdateStep> stepChain  = GetStepChain(updateSteps, versionToUpdate.Version, targetStep.To);
                bool         isValid         = installedVersions.All(version => version.IsSpecialVersion || !version.IsFileSystemEditable || version.Version != targetStep.To);
                UpdateTarget target          = new UpdateTarget(stepChain, targetStep.To, targetStep.IsStable, isValid);
                targets.Add(target);

                if (!targetStep.IsStable)
                {
                    UpdateStep stableStep = group.FirstOrDefault(step => step.IsStable);
                    if (stableStep != null)
                    {
                        stepChain = GetStepChain(updateSteps, versionToUpdate.Version, stableStep.To);
                        isValid   = installedVersions.All(version => version.IsSpecialVersion || !version.IsFileSystemEditable || version.Version != stableStep.To);
                        target    = new UpdateTarget(stepChain, stableStep.To, true, isValid);
                        targets.Add(target);
                    }
                }
            }
            return(targets);
        }
Esempio n. 3
0
        private async Task UpdateSelectedVersionInternal(string token, UpdateTarget target)
        {
            var progressWindow = new ProgressWindow {
                Owner = Window
            };
            var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;

            progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingFactorioAction");

            var cancellationSource = new CancellationTokenSource();

            progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel();

            var progress    = new Progress <double>(value => progressViewModel.Progress = value);
            var canCancel   = new Progress <bool>(value => progressViewModel.CanCancel = value);
            var description = new Progress <string>(value => progressViewModel.ProgressDescription = value);

            try
            {
                Task closeWindowTask = null;
                try
                {
                    Task updateTask = ApplyUpdate(token, target, progress, canCancel, description, cancellationSource.Token);

                    closeWindowTask = updateTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));
                    progressWindow.ShowDialog();

                    await updateTask;
                }
                finally
                {
                    if (closeWindowTask != null)
                    {
                        await closeWindowTask;
                    }
                }
            }
            catch (HttpRequestException)
            {
                MessageBox.Show(Window,
                                App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error),
                                App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error),
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (CriticalUpdaterException)
            {
                MessageBox.Show(Window,
                                App.Instance.GetLocalizedMessage("FactorioUpdaterCritical", MessageType.Error),
                                App.Instance.GetLocalizedMessageTitle("FactorioUpdaterCritical", MessageType.Error),
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 4
0
        public static void UpdateTarget(Session session, UpdateTarget target, UpdateParameters parameters)
        {
            if (!characterInfo.TryGetValue(session.Character, out CharacterUpdateInfo info))
            {
                return;
            }

            info.Target = target.Target;

            IWritable update = BuildUpdate(target, session.Character.Sequence);

            BroadcastUpdate(session, update);
        }
Esempio n. 5
0
        private async Task ApplyUpdate(string token, UpdateTarget target, IProgress <double> progress, IProgress <bool> canCancel, IProgress <string> description, CancellationToken cancellationToken)
        {
            canCancel.Report(true);
            description.Report(App.Instance.GetLocalizedResourceString("UpdatingFactorioStage1Description"));
            var files = await FactorioUpdater.DownloadUpdatePackagesAsync(GlobalCredentials.Instance.Username, token, target, progress, cancellationToken);

            if (!cancellationToken.IsCancellationRequested)
            {
                progress.Report(0);
                canCancel.Report(false);
                description.Report(App.Instance.GetLocalizedResourceString("UpdatingFactorioStage2Description"));
                await SelectedVersion.UpdateAsync(files, progress);
            }
        }
Esempio n. 6
0
        private void CreatedWithTarget()
        {
            var target = new UpdateTarget("1234");

            var(_, storageMod, eventBus) = CreateStorageMod(target);

            eventBus.Work(100, () => storageMod.GetState() == StorageModStateEnum.CreatedWithUpdateTarget);

            Assert.Equal(StorageModStateEnum.CreatedWithUpdateTarget, storageMod.GetState());
            Assert.NotNull(storageMod.GetVersionHash());
            Assert.NotNull(storageMod.GetMatchHash());
            var versionHash = storageMod.GetVersionHash();

            Assert.True(versionHash.IsMatch(VersionHash.FromDigest("1234")));
        }
Esempio n. 7
0
        private void UpdateWithTarget()
        {
            var target = new UpdateTarget("1234");

            var(_, storageMod, eventBus) = CreateStorageMod(target);

            eventBus.Work(100, () => storageMod.GetState() == StorageModStateEnum.CreatedWithUpdateTarget);

            var repo = CreateRepoMod();

            storageMod.Update(repo, MatchHash.CreateEmpty(), VersionHash.CreateEmpty(), null, CancellationToken.None);

            Assert.Equal(StorageModStateEnum.Updating, storageMod.GetState());
            Assert.True((storageMod.GetMatchHash()).IsMatch(MatchHash.CreateEmpty()));
            Assert.True((storageMod.GetVersionHash()).IsMatch(VersionHash.CreateEmpty()));
        }
Esempio n. 8
0
        private bool ShowUpdateWindow(List <UpdateTarget> targets, out UpdateTarget target)
        {
            var updateListWindow = new UpdateListWindow()
            {
                Owner = Window
            };
            var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel;

            updateListViewModel.UpdateTargets = targets;
            bool?result = updateListWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                target = updateListViewModel.SelectedTarget;
                return(target != null);
            }
            else
            {
                target = null;
                return(false);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Downloads all required packages to update to a specified update target.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="token">The login token.</param>
        /// <param name="target">The update target.</param>
        /// <param name="progress">A progress object used to report the progress of the operation.</param>
        /// <param name="cancellationToken">A cancelation token that can be used to cancel the operation.</param>
        /// <returns>Returns a list of update package files.</returns>
        private static async Task <List <FileInfo> > DownloadUpdatePackagesAsync(string username, string token, UpdateTarget target, IProgress <double> progress, CancellationToken cancellationToken)
        {
            var packageFiles = new List <FileInfo>();

            try
            {
                int stepCount = target.Steps.Count;
                int counter   = 0;
                foreach (var step in target.Steps)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    var subProgress = new Progress <double>(value => progress.Report((1.0 / stepCount) * counter + (value / stepCount)));
                    var packageFile = await UpdateWebsite.DownloadUpdateStepAsync(username, token, step, subProgress, cancellationToken);

                    if (packageFile != null)
                    {
                        packageFiles.Add(packageFile);
                    }

                    counter++;
                }
                progress.Report(1);

                if (cancellationToken.IsCancellationRequested)
                {
                    foreach (var file in packageFiles)
                    {
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }

                    return(null);
                }

                return(packageFiles);
            }
            catch (Exception)
            {
                foreach (var file in packageFiles)
                {
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                }

                throw;
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Downloads and applies an update target to a specified version of Factorio.
        /// </summary>
        /// <param name="versionToUpdate">The version of Factorio that is going to be updated.</param>
        /// <param name="username">The username.</param>
        /// <param name="token">The login token.</param>
        /// <param name="target">The update target.</param>
        /// <param name="progress">A progress object used to report the progress of the operation.</param>
        /// <param name="stageProgress">A progress object used to report the stage of the operation.</param>
        /// <param name="cancellationToken">A cancelation token that can be used to cancel the operation.</param>
        public static async Task ApplyUpdateAsync(FactorioVersion versionToUpdate, string username, string token, UpdateTarget target,
                                                  IProgress <double> progress, IProgress <UpdaterStageInfo> stageProgress, CancellationToken cancellationToken)
        {
            stageProgress.Report(new UpdaterStageInfo(true, App.Instance.GetLocalizedResourceString("UpdatingFactorioStage1Description")));
            List <FileInfo> packageFiles = await DownloadUpdatePackagesAsync(username, token, target, progress, cancellationToken);

            try
            {
                if ((packageFiles != null) && !cancellationToken.IsCancellationRequested)
                {
                    progress.Report(0);
                    stageProgress.Report(new UpdaterStageInfo(false, App.Instance.GetLocalizedResourceString("UpdatingFactorioStage2Description")));

                    await ApplyUpdatePackagesAsync(versionToUpdate, packageFiles, progress);

                    versionToUpdate.UpdateVersion(target.TargetVersion);
                }
            }
            finally
            {
                if (packageFiles != null)
                {
                    foreach (var file in packageFiles)
                    {
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        private async Task UpdateSelectedVersion()
        {
            string token;

            if (GlobalCredentials.Instance.LogIn(Window, out token))
            {
                UpdateInfo        updateInfo  = UpdateWebsite.GetUpdateInfo(GlobalCredentials.Instance.Username, token);
                List <UpdateStep> updateSteps = updateInfo.Package.Where(step => step.From >= SelectedVersion.Version).ToList();

                if (updateSteps.Count > 0)
                {
                    List <UpdateTarget> targets = FactorioUpdater.GetUpdateTargets(SelectedVersion, FactorioVersions, updateSteps);

                    var updateListWindow = new UpdateListWindow()
                    {
                        Owner = Window
                    };
                    var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel;
                    updateListViewModel.UpdateTargets = targets;
                    bool?result = updateListWindow.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        UpdateTarget target = updateListViewModel.SelectedTarget;

                        var progressWindow = new ProgressWindow {
                            Owner = Window
                        };
                        var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;
                        progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingFactorioAction");

                        var cancellationSource = new CancellationTokenSource();
                        progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel();

                        var progress      = new Progress <double>(value => progressViewModel.Progress = value);
                        var stageProgress = new Progress <UpdaterStageInfo>(value =>
                        {
                            progressViewModel.CanCancel           = value.CanCancel;
                            progressViewModel.ProgressDescription = value.Description;
                        });

                        try
                        {
                            Task closeWindowTask = null;
                            try
                            {
                                Task updateTask = FactorioUpdater.ApplyUpdateAsync(SelectedVersion,
                                                                                   GlobalCredentials.Instance.Username, token, target,
                                                                                   progress, stageProgress, cancellationSource.Token);

                                closeWindowTask = updateTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));
                                progressWindow.ShowDialog();

                                await updateTask;
                            }
                            finally
                            {
                                if (closeWindowTask != null)
                                {
                                    await closeWindowTask;
                                }
                            }
                        }
                        catch (HttpRequestException)
                        {
                            MessageBox.Show(Window,
                                            App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error),
                                            App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error),
                                            MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        catch (CriticalUpdaterException)
                        {
                            MessageBox.Show(Window,
                                            App.Instance.GetLocalizedMessage("FactorioUpdaterCritical", MessageType.Error),
                                            App.Instance.GetLocalizedMessageTitle("FactorioUpdaterCritical", MessageType.Error),
                                            MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(Window,
                                    App.Instance.GetLocalizedMessage("NoFactorioUpdate", MessageType.Information),
                                    App.Instance.GetLocalizedMessageTitle("NoFactorioUpdate", MessageType.Information),
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }
Esempio n. 12
0
 public MappingExecutor(IParseTree <TokenType, ParserRuleType> mappingDefinition)
 {
     this.extract = mappingDefinition.SourceExtractor();
     this.update  = mappingDefinition.TargetUpdater();
 }