示例#1
0
        public void PlayGame(Game game)
        {
            if (!game.IsInstalled)
            {
                InstallGame(game);
                return;
            }

            logger.Info($"Starting {game.GetIdentifierInfo()}");
            var dbGame = Database.Games.Get(game.Id);

            if (dbGame == null)
            {
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartErrorNoGame"), game.Name),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
                UpdateJumpList();
                return;
            }

            IGameController controller = null;

            try
            {
                if (game.IsRunning)
                {
                    logger.Warn("Failed to start the game, game is already running.");
                    return;
                }

                if (game.PlayAction.IsHandledByPlugin)
                {
                    logger.Info("Using library plugin to start the game.");
                    controller = controllers.GetGameBasedController(game, Extensions);
                }
                else
                {
                    logger.Info("Using generic controller start the game.");
                    controller = controllers.GetGenericGameController(game);
                }

                if (controller == null)
                {
                    Dialogs.ShowErrorMessage(
                        resources.GetString("LOCErrorLibraryPluginNotFound"),
                        resources.GetString("LOCGameError"));
                    return;
                }

                controllers.RemoveController(game.Id);
                controllers.AddController(controller);
                UpdateGameState(game.Id, null, null, null, null, true);
                controller.Play();
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                if (controller != null)
                {
                    controllers.RemoveController(game.Id);
                    UpdateGameState(game.Id, null, null, null, null, false);
                }

                logger.Error(exc, "Cannot start game: ");
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartError"), exc.Message),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            try
            {
                UpdateJumpList();
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to set jump list data: ");
            }
        }
示例#2
0
        public void PlayGame(Game game)
        {
            if (!game.IsInstalled)
            {
                InstallGame(game);
                return;
            }

            logger.Info($"Starting {game.GetIdentifierInfo()}");
            var dbGame = Database.Games.Get(game.Id);

            if (dbGame == null)
            {
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartErrorNoGame"), game.Name),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
                UpdateJumpList();
                return;
            }

            IGameController controller = null;

            try
            {
                if (game.IsRunning || game.IsLaunching)
                {
                    logger.Warn("Failed to start the game, game is already running/launching.");
                    return;
                }

                if (game.PlayAction == null)
                {
                    Dialogs.ShowErrorMessage(
                        resources.GetString("LOCErrorNoPlayAction"),
                        resources.GetString("LOCGameError"));
                    return;
                }

                if (game.PlayAction.IsHandledByPlugin)
                {
                    logger.Info("Using library plugin to start the game.");
                    controller = controllers.GetGameBasedController(game, Extensions);
                }
                else
                {
                    logger.Info("Using generic controller start the game.");
                    controller = controllers.GetGenericGameController(game);
                }

                if (controller == null)
                {
                    Dialogs.ShowErrorMessage(
                        resources.GetString("LOCErrorLibraryPluginNotFound"),
                        resources.GetString("LOCGameError"));
                    return;
                }

                controllers.RemoveController(game.Id);
                controllers.AddController(controller);
                UpdateGameState(game.Id, null, null, null, null, true);

                if (!game.IsCustomGame && shutdownJobs.TryGetValue(game.PluginId, out var existingJob))
                {
                    logger.Debug($"Starting game with existing client shutdown job, canceling job {game.PluginId}.");
                    existingJob.CancelToken.Cancel();
                    shutdownJobs.TryRemove(game.PluginId, out var _);
                }

                if (!AppSettings.PreScript.IsNullOrWhiteSpace())
                {
                    try
                    {
                        var expanded = game.ExpandVariables(AppSettings.PreScript);
                        ExecuteScriptAction(AppSettings.ActionsScriptLanguage, expanded, game);
                    }
                    catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        var message = exc.Message;

                        if (exc is ScriptRuntimeException err)
                        {
                            message = err.Message + "\n\n" + err.ScriptStackTrace;
                        }

                        logger.Error(exc, "Failed to execute global pre-script action.");
                        logger.Error(AppSettings.PreScript);
                        Dialogs.ShowMessage(
                            message,
                            resources.GetString("LOCErrorGlobalScriptAction"),
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        controllers.RemoveController(game.Id);
                        UpdateGameState(game.Id, null, null, null, null, false);
                        return;
                    }
                }

                if (!game.PreScript.IsNullOrWhiteSpace())
                {
                    try
                    {
                        var expanded = game.ExpandVariables(game.PreScript);
                        ExecuteScriptAction(game.ActionsScriptLanguage, expanded, game);
                    }
                    catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        var message = exc.Message;

                        if (exc is ScriptRuntimeException err)
                        {
                            message = err.Message + "\n\n" + err.ScriptStackTrace;
                        }

                        logger.Error(exc, "Failed to execute game's pre-script action.");
                        logger.Error(game.PreScript);
                        Dialogs.ShowMessage(
                            message,
                            resources.GetString("LOCErrorGameScriptAction"),
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        controllers.RemoveController(game.Id);
                        UpdateGameState(game.Id, null, null, null, null, false);
                        return;
                    }
                }

                controller.Play();
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Cannot start game: ");
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartError"), exc.Message),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);

                if (controller != null)
                {
                    controllers.RemoveController(game.Id);
                    UpdateGameState(game.Id, null, null, null, null, false);
                }

                return;
            }

            UpdateJumpList();
        }
示例#3
0
        public void PlayGame(Game game)
        {
            if (!game.IsInstalled)
            {
                InstallGame(game);
                return;
            }

            logger.Info($"Starting {game.GetIdentifierInfo()}");
            var dbGame = Database.Games.Get(game.Id);

            if (dbGame == null)
            {
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartErrorNoGame"), game.Name),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
                UpdateJumpList();
                return;
            }

            IGameController controller = null;

            try
            {
                if (game.IsRunning || game.IsLaunching)
                {
                    logger.Warn("Failed to start the game, game is already running/launching.");
                    return;
                }

                if (game.PlayAction == null)
                {
                    Dialogs.ShowErrorMessage(
                        resources.GetString("LOCErrorNoPlayAction"),
                        resources.GetString("LOCGameError"));
                    return;
                }

                if (game.PlayAction.IsHandledByPlugin)
                {
                    logger.Info("Using library plugin to start the game.");
                    controller = controllers.GetGameBasedController(game, Extensions);
                }
                else
                {
                    logger.Info("Using generic controller start the game.");
                    controller = controllers.GetGenericGameController(game);
                }

                if (controller == null)
                {
                    Dialogs.ShowErrorMessage(
                        resources.GetString("LOCErrorLibraryPluginNotFound"),
                        resources.GetString("LOCGameError"));
                    return;
                }

                controllers.RemoveController(game.Id);
                controllers.AddController(controller);
                UpdateGameState(game.Id, null, null, null, null, true);

                if (!AppSettings.PreScript.IsNullOrWhiteSpace())
                {
                    try
                    {
                        ExecuteScriptAction(AppSettings.ActionsScriptLanguage, AppSettings.PreScript, game);
                    }
                    catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(exc, "Failed to execute global pre-script action.");
                        logger.Error(AppSettings.PreScript);
                        Dialogs.ShowMessage(
                            string.Format(resources.GetString("LOCErrorGlobalScriptAction"), exc.Message),
                            resources.GetString("LOCGameError"),
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        controllers.RemoveController(game.Id);
                        UpdateGameState(game.Id, null, null, null, null, false);
                        return;
                    }
                }

                if (!game.PreScript.IsNullOrWhiteSpace())
                {
                    try
                    {
                        ExecuteScriptAction(game.ActionsScriptLanguage, game.PreScript, game);
                    }
                    catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(exc, "Failed to execute game's pre-script action.");
                        logger.Error(game.PreScript);
                        Dialogs.ShowMessage(
                            string.Format(resources.GetString("LOCErrorGameScriptAction"), exc.Message),
                            resources.GetString("LOCGameError"),
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        controllers.RemoveController(game.Id);
                        UpdateGameState(game.Id, null, null, null, null, false);
                        return;
                    }
                }

                controller.Play();
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                if (controller != null)
                {
                    controllers.RemoveController(game.Id);
                    UpdateGameState(game.Id, null, null, null, null, false);
                }

                logger.Error(exc, "Cannot start game: ");
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameStartError"), exc.Message),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            UpdateJumpList();
        }