Example #1
0
        /// <summary>
        /// install mods as an asynchronous operation.
        /// </summary>
        /// <param name="skipOverlay">if set to <c>true</c> [skip overlay].</param>
        protected virtual async Task InstallModsAsync(bool skipOverlay = false)
        {
            var result = await modService.InstallModsAsync(InstalledMods.Mods);

            if (result != null)
            {
                if (result.Any(p => p.Installed == true))
                {
                    if (InstalledMods.IsActivated)
                    {
                        await InstalledMods.RefreshModsAsync(skipOverlay : skipOverlay);
                    }
                }
                if (result.Any(p => p.Invalid))
                {
                    await ShowInvalidModsNotificationAsync(result.Where(p => p.Invalid).ToList());
                }
            }
        }
Example #2
0
        /// <summary>
        /// Called when [activated].
        /// </summary>
        /// <param name="disposables">The disposables.</param>
        protected override void OnActivated(CompositeDisposable disposables)
        {
            Task.Run(() => EvalResumeAvailabilityLoopAsync().ConfigureAwait(false));

            ShowAdvancedFeatures = (gameService.GetSelected()?.AdvancedFeaturesSupported).GetValueOrDefault();
            AnalyzeClass         = string.Empty;

            var allowModSelectionEnabled = this.WhenAnyValue(v => v.AllowModSelection);
            var applyEnabled             = Observable.Merge(this.WhenAnyValue(v => v.ApplyingCollection, v => !v), allowModSelectionEnabled);

            this.WhenAnyValue(v => v.CollectionMods.SelectedModCollection).Subscribe(s =>
            {
                if (s != null)
                {
                    AllowModSelection = true;
                    InstalledMods.AllowModSelection  = true;
                    CollectionMods.AllowModSelection = true;
                }
                else
                {
                    AllowModSelection = false;
                    InstalledMods.AllowModSelection  = false;
                    CollectionMods.AllowModSelection = false;
                }
                InstallModsAsync().ConfigureAwait(true);
            }).DisposeWith(disposables);

            this.WhenAnyValue(v => v.InstalledMods.Mods).Subscribe(v =>
            {
                CollectionMods.SetMods(v, InstalledMods.ActiveGame);
            });

            this.WhenAnyValue(v => v.InstalledMods.RefreshingMods).Subscribe(s =>
            {
                CollectionMods.HandleModRefresh(s, InstalledMods.Mods, InstalledMods.ActiveGame);
            }).DisposeWith(disposables);

            this.WhenAnyValue(v => v.CollectionMods.NeedsModListRefresh).Where(x => x).Subscribe(async s =>
            {
                await InstalledMods.RefreshModsAsync();
            }).DisposeWith(disposables);

            this.WhenAnyValue(p => p.InstalledMods.PerformingEnableAll).Subscribe(s =>
            {
                CollectionMods.HandleEnableAllToggled(s, InstalledMods.AllModsEnabled, InstalledMods.FilteredMods);
            }).DisposeWith(disposables);

            ApplyCommand = ReactiveCommand.Create(() =>
            {
                ApplyCollectionAsync(idGenerator.GetNextId()).ConfigureAwait(true);
            }, applyEnabled).DisposeWith(disposables);

            AnalyzeCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var game = gameService.GetSelected();
                if (game != null && CollectionMods.SelectedMods?.Count > 0 && CollectionMods.SelectedModCollection != null)
                {
                    var messageState = promptNotificationsService.Get();
                    if (!messageState.ConflictSolverPromptShown)
                    {
                        var title   = localizationManager.GetResource(LocalizationResources.Mod_Actions.ConflictSolver.FirstUsePrompt.Title);
                        var message = localizationManager.GetResource(LocalizationResources.Mod_Actions.ConflictSolver.FirstUsePrompt.Message);
                        await notificationAction.ShowPromptAsync(title, title, message, NotificationType.Info, PromptType.OK);
                        messageState.ConflictSolverPromptShown = true;
                        promptNotificationsService.Save(messageState);
                    }
                    var id = idGenerator.GetNextId();
                    await TriggerOverlayAsync(id, true, localizationManager.GetResource(LocalizationResources.App.WaitBackgroundOperationMessage));
                    await shutDownState.WaitUntilFreeAsync();
                    modPatchCollectionService.ResetPatchStateCache();
                    var mode    = await modPatchCollectionService.GetPatchStateModeAsync(CollectionMods.SelectedModCollection.Name);
                    var version = gameService.GetVersion(game);
                    if (mode == PatchStateMode.None)
                    {
                        await TriggerOverlayAsync(id, false);
                        await Task.Delay(50);
                        IsModeOpen = true;
                    }
                    else
                    {
                        var hasGameDefinitions      = await modPatchCollectionService.PatchHasGameDefinitionsAsync(CollectionMods.SelectedModCollection.Name);
                        var shouldAnalyzePatchState = !string.IsNullOrEmpty(version);
                        var proceed = true;
                        if (hasGameDefinitions && !shouldAnalyzePatchState)
                        {
                            var title   = localizationManager.GetResource(LocalizationResources.Mod_Actions.ConflictSolver.GameExecutableNotSetPrompt.Title);
                            var message = localizationManager.GetResource(LocalizationResources.Mod_Actions.ConflictSolver.GameExecutableNotSetPrompt.Message);
                            proceed     = await notificationAction.ShowPromptAsync(title, title, message, NotificationType.Info, PromptType.YesNo);
                        }
                        if (proceed)
                        {
                            await AnalyzeModsAsync(id, mode, version);
                        }
                        else
                        {
                            await TriggerOverlayAsync(id, false);
                        }
                    }
                }
            }, allowModSelectionEnabled).DisposeWith(disposables);

            async Task <bool> ensureSteamIsRunning(IGameSettings args)
            {
                if (gameService.IsSteamGame(args))
                {
                    // Check if process is running
                    var processes = Process.GetProcesses();
                    if (!processes.Any(p => p.ProcessName.Equals(SteamProcess, StringComparison.OrdinalIgnoreCase)))
                    {
                        await appAction.OpenAsync(SteamLaunch);

                        var attempts = 0;
                        while (!processes.Any(p => p.ProcessName.Equals(SteamProcess, StringComparison.OrdinalIgnoreCase)))
                        {
                            if (attempts > 3)
                            {
                                break;
                            }
                            await Task.Delay(3000);

                            processes = Process.GetProcesses();
                            attempts++;
                        }
                    }
                }
                return(true);
            }

            async Task launchGame(bool continueGame)
            {
                var game = gameService.GetSelected();

                if (game != null)
                {
                    var args = gameService.GetLaunchSettings(game, continueGame);
                    if (!string.IsNullOrWhiteSpace(args.ExecutableLocation))
                    {
                        var id = idGenerator.GetNextId();
                        await TriggerOverlayAsync(id, true, localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.Overlay));

                        if (game.RefreshDescriptors)
                        {
                            await modService.DeleteDescriptorsAsync(InstalledMods.Mods);

                            await modService.InstallModsAsync(InstalledMods.Mods);
                        }
                        await ApplyCollectionAsync(id, false);

                        await MessageBus.PublishAsync(new LaunchingGameEvent(game.Type));

                        if (gameService.IsSteamLaunchPath(args))
                        {
                            if (await appAction.OpenAsync(args.ExecutableLocation))
                            {
                                if (game.CloseAppAfterGameLaunch)
                                {
                                    await appAction.ExitAppAsync();
                                }
                            }
                            else
                            {
                                notificationAction.ShowNotification(localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.LaunchError.Title),
                                                                    localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.LaunchError.Message), NotificationType.Error, 10);
                                await TriggerOverlayAsync(id, false);
                            }
                        }
                        else
                        {
                            await ensureSteamIsRunning(args);

                            if (await appAction.RunAsync(args.ExecutableLocation, args.LaunchArguments))
                            {
                                if (game.CloseAppAfterGameLaunch)
                                {
                                    await appAction.ExitAppAsync();
                                }
                                else
                                {
                                    await TriggerOverlayAsync(id, false);
                                }
                            }
                            else
                            {
                                notificationAction.ShowNotification(localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.LaunchError.Title),
                                                                    localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.LaunchError.Message), NotificationType.Error, 10);
                                await TriggerOverlayAsync(id, false);
                            }
                        }
                    }
                    else
                    {
                        notificationAction.ShowNotification(localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.NotSet.Title),
                                                            localizationManager.GetResource(LocalizationResources.Mod_Actions.LaunchGame.NotSet.Message), NotificationType.Warning, 10);
                    }
                }
            }

            LaunchGameCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                await launchGame(false);
            }, allowModSelectionEnabled).DisposeWith(disposables);

            ResumeGameCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                await launchGame(true);
            }, allowModSelectionEnabled).DisposeWith(disposables);

            AdvancedModeCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var game    = gameService.GetSelected();
                var version = gameService.GetVersion(game);
                await AnalyzeModsAsync(idGenerator.GetNextId(), PatchStateMode.Advanced, version);
            }).DisposeWith(disposables);

            DefaultModeCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var game    = gameService.GetSelected();
                var version = gameService.GetVersion(game);
                await AnalyzeModsAsync(idGenerator.GetNextId(), PatchStateMode.Default, version);
            }).DisposeWith(disposables);

            CloseModeCommand = ReactiveCommand.Create(() =>
            {
                ForceClosePopups();
            }).DisposeWith(disposables);

            var previousCollectionNotification = string.Empty;

            CollectionMods.ConflictSolverStateChanged += (collectionName, state) =>
            {
                AnalyzeClass = !state ? InvalidConflictSolverClass : string.Empty;
                if (!state && previousCollectionNotification != collectionName)
                {
                    notificationAction.ShowNotification(localizationManager.GetResource(LocalizationResources.Notifications.ConflictSolverUpdate.Title),
                                                        localizationManager.GetResource(LocalizationResources.Notifications.ConflictSolverUpdate.Message), NotificationType.Warning, 30);
                    previousCollectionNotification = collectionName;
                }
            };

            gameDirectoryChangedHandler.Subscribe(async s =>
            {
                if (s.CustomDirectoryChanged)
                {
                    CollectionMods.Reset(true);
                }
                await InstalledMods.RefreshModsAsync();
                EvalResumeAvailability(s.Game);
            }).DisposeWith(disposables);

            this.WhenAnyValue(v => v.InstalledMods.ModFilePopulationCompleted).Subscribe(s =>
            {
                CollectionMods.CanExportModHashReport = s;
            }).DisposeWith(disposables);

            modListInstallRefreshRequestHandler.Subscribe(async m =>
            {
                await InstallModsAsync(m.SkipOverlay);
            }).DisposeWith(disposables);

            base.OnActivated(disposables);
        }