Inheritance: FirstFloor.ModernUI.Presentation.NotifyPropertyChanged
Beispiel #1
0
        private static async Task <Game.Result> StartAsync_Ui(Game.StartProperties properties, GameMode mode)
        {
            using (var ui = _uiFactory.Create()) {
                Logging.Write($"Starting game: {properties.GetDescription()}");
                ui.Show(properties, mode);

                CancellationTokenSource linked = null;
                IsInGame = true;

                try {
                    Game.Result result;
                    using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                        using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                            Started?.Invoke(null, new GameStartedArgs(properties, mode));

                            if (mode == GameMode.Race)
                            {
                                properties.SetAdditional(new RaceCommandExecutor(properties));
                                properties.SetAdditional(new DBoxIntegration());
                                if (SettingsHolder.Drive.ContinueOnEscape)
                                {
                                    properties.SetAdditional(new ContinueRaceHelper());
                                }
                            }
                            else if (mode == GameMode.Replay)
                            {
                                properties.SetAdditional(new ReplayCommandExecutor(properties));
                            }

                            var cancellationToken = ui.CancellationToken;
                            if (SettingsHolder.Drive.ImmediateCancel)
                            {
                                var cancelHelper = new ImmediateCancelHelper();
                                linked            = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelHelper.GetCancellationToken());
                                cancellationToken = linked.Token;
                                properties.SetAdditional(cancelHelper);
                                properties.SetKeyboardListener = true;
                            }

                            if (mode == GameMode.Replay)
                            {
                                await PrepareReplay(properties, ui, cancellationToken);
                            }

                            ui.OnProgress("Loading data for Custom Shaders Patch…");
                            var trackId = string.IsNullOrWhiteSpace(properties.BasicProperties?.TrackConfigurationId)
                                ? properties.BasicProperties?.TrackId
                                : properties.BasicProperties?.TrackId + @"/" + properties.BasicProperties?.TrackConfigurationId;
                            await PatchTracksDataUpdater.Instance.TriggerAutoLoadAsync(trackId);

                            await PatchTracksVaoDataUpdater.Instance.TriggerAutoLoadAsync(trackId);

                            await PatchBackgroundDataUpdater.Instance.TriggerAutoLoadAsync(trackId);

                            await PatchCarsDataUpdater.Instance.TriggerAutoLoadAsync(properties.BasicProperties?.CarId);

                            result = await Game.StartAsync(CreateStarter(properties), properties, new ProgressHandler(ui), cancellationToken);
                        }

                    Logging.Write($"Result: {result?.GetDescription() ?? @"<NULL>"}");
                    if (ui.CancellationToken.IsCancellationRequested)
                    {
                        ui.OnError(new UserCancelledException());
                        return(null);
                    }

                    var whatsGoingOn = mode != GameMode.Race || result == null?AcLogHelper.TryToDetermineWhatsGoingOn() : null;

                    if (whatsGoingOn != null)
                    {
                        properties.SetAdditional(whatsGoingOn);
                    }

                    if (mode == GameMode.Race)
                    {
                        var param = new GameEndedArgs(properties, result);
                        Ended?.Invoke(null, param);
                        /* TODO: should set result to null if param.Cancel is true? */

                        var replayHelper = new ReplayHelper(properties, result);
                        (result == null || param.Cancel ? Cancelled : Finished)?.Invoke(null, new GameFinishedArgs(properties, result));

                        ui.OnResult(result, replayHelper);
                    }
                    else
                    {
                        ui.OnResult(null, null);
                    }

                    return(result);
                } catch (Exception e) when(e.IsCancelled())
                {
                    // ui.OnError(new UserCancelledException());
                    ui.OnResult(null, null);
                    return(null);
                } catch (Exception e) {
                    Logging.Warning(e);
                    ui.OnError(e);
                    return(null);
                } finally {
                    linked?.Dispose();
                    IsInGame = false;
                }
            }
        }
Beispiel #2
0
        private static async Task <Game.Result> StartAsync_Ui(Game.StartProperties properties, GameMode mode)
        {
            using (var ui = _uiFactory.Create()) {
                Logging.Write($"Starting game: {properties.GetDescription()}");
                ui.Show(properties, mode);

                CancellationTokenSource linked = null;
                IsInGame = true;

                try {
                    FileUtils.TryToDelete(AcPaths.GetLogFilename());

                    Game.Result result;
                    using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                        using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                            Started?.Invoke(null, new GameStartedArgs(properties, mode));

                            if (mode == GameMode.Race)
                            {
                                properties.SetAdditional(new RaceCommandExecutor(properties));
                                properties.SetAdditional(new DBoxIntegration());
                                if (SettingsHolder.Drive.ContinueOnEscape)
                                {
                                    properties.SetAdditional(new ContinueRaceHelper());
                                }
                            }
                            else if (mode == GameMode.Replay)
                            {
                                properties.SetAdditional(new ReplayCommandExecutor(properties));
                            }

                            var cancellationToken = ui.CancellationToken;
                            if (SettingsHolder.Drive.ImmediateCancel)
                            {
                                var cancelHelper = new ImmediateCancelHelper();
                                linked            = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelHelper.GetCancellationToken());
                                cancellationToken = linked.Token;
                                properties.SetAdditional(cancelHelper);
                                properties.SetKeyboardListener = true;
                            }

                            if (mode == GameMode.Replay)
                            {
                                await PrepareReplay(properties, ui, cancellationToken);
                            }

                            if (SettingsHolder.Drive.LoadPatchDataAutomatically && PatchHelper.IsActive())
                            {
                                var trackId = string.IsNullOrWhiteSpace(properties.BasicProperties?.TrackConfigurationId)
                                    ? properties.BasicProperties?.TrackId
                                    : properties.BasicProperties?.TrackId + @"/" + properties.BasicProperties?.TrackConfigurationId;
                                using (var cancellation = new CancellationTokenSource()) {
                                    ui.OnProgress("Loading data for Custom Shaders Patch…", AsyncProgressEntry.Indetermitate, () => { cancellation.Cancel(); });
                                    var carName   = properties.BasicProperties?.CarId == null ? null : CarsManager.Instance.GetById(properties.BasicProperties?.CarId);
                                    var trackName = trackId == null ? null : TracksManager.Instance.GetById(trackId)?.Name ?? trackId;
                                    await PatchTracksDataUpdater.Instance.TriggerAutoLoadAsync(trackId,
                                                                                               PatchSubProgress($"Config for track {trackName}"), cancellation.Token);

                                    await PatchTracksVaoDataUpdater.Instance.TriggerAutoLoadAsync(trackId,
                                                                                                  PatchSubProgress($"Ambient occlusion patch for track {trackName}"), cancellation.Token);

                                    await PatchBackgroundDataUpdater.Instance.TriggerAutoLoadAsync(trackId,
                                                                                                   PatchSubProgress($"Backgrounds for track {trackName}"), cancellation.Token);

                                    await PatchCarsDataUpdater.Instance.TriggerAutoLoadAsync(properties.BasicProperties?.CarId,
                                                                                             PatchSubProgress($"Config for car {carName}"), cancellation.Token);

                                    await PatchCarsVaoDataUpdater.Instance.TriggerAutoLoadAsync(properties.BasicProperties?.CarId,
                                                                                                PatchSubProgress($"Ambient occlusion patch for car {carName}"), cancellation.Token);

                                    ui.OnProgress("Final preparations…");

                                    IProgress <AsyncProgressEntry> PatchSubProgress(string target)
                                    {
                                        return(new Progress <AsyncProgressEntry>(p => ui.OnProgress("Loading data for Custom Shaders Patch…",
                                                                                                    new AsyncProgressEntry($"{target}\n{p.Message ?? @"…"}", p.IsReady || p.Progress == null ? 0d : p.Progress),
                                                                                                    () => cancellation.Cancel())));
                                    }
                                }
                            }

                            result = await Game.StartAsync(CreateStarter(properties), properties, new ProgressHandler(ui), cancellationToken);
                        }

                    Logging.Write($"Result: {result?.GetDescription() ?? @"<NULL>"}");
                    if (ui.CancellationToken.IsCancellationRequested)
                    {
                        ui.OnError(new UserCancelledException());
                        return(null);
                    }

                    var whatsGoingOn = mode != GameMode.Race || result == null?AcLogHelper.TryToDetermineWhatsGoingOn() : null;

                    if (whatsGoingOn != null)
                    {
                        properties.SetAdditional(whatsGoingOn);
                    }

                    if (mode == GameMode.Race)
                    {
                        var param = new GameEndedArgs(properties, result);
                        Ended?.Invoke(null, param);
                        /* TODO: should set result to null if param.Cancel is true? */

                        var replayHelper = new ReplayHelper(properties, result);
                        (result == null || param.Cancel ? Cancelled : Finished)?.Invoke(null, new GameFinishedArgs(properties, result));

                        ui.OnResult(result, replayHelper);
                    }
                    else
                    {
                        ui.OnResult(null, null);
                    }

                    return(result);
                } catch (Exception e) when(e.IsCancelled())
                {
                    // ui.OnError(new UserCancelledException());
                    ui.OnResult(null, null);
                    return(null);
                } catch (Exception e) {
                    Logging.Warning(e);
                    ui.OnError(e);
                    return(null);
                } finally {
                    linked?.Dispose();
                    IsInGame = false;
                }
            }
        }
Beispiel #3
0
        private static async Task <Game.Result> StartAsync_Ui(Game.StartProperties properties, GameMode mode)
        {
            using (var ui = _uiFactory.Create()) {
                Logging.Write($"Starting game: {properties.GetDescription()}");
                ui.Show(properties, mode);

                CancellationTokenSource linked = null;
                IsInGame = true;

                try {
                    Game.Result result;
                    using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                        using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                            if (mode == GameMode.Race)
                            {
                                properties.SetAdditional(new RaceCommandExecutor(properties));
                                Started?.Invoke(null, new GameStartedArgs(properties));

                                if (SettingsHolder.Drive.ContinueOnEscape)
                                {
                                    properties.SetAdditional(new ContinueRaceHelper());
                                }
                            }
                            else if (mode == GameMode.Replay)
                            {
                                properties.SetAdditional(new ReplayCommandExecutor(properties));
                            }

                            var cancellationToken = ui.CancellationToken;

                            if (SettingsHolder.Drive.ImmediateCancel)
                            {
                                var cancelHelper = new ImmediateCancelHelper();
                                linked            = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelHelper.GetCancellationToken());
                                cancellationToken = linked.Token;

                                properties.SetAdditional(cancelHelper);
                                properties.SetKeyboardListener = true;
                            }

                            result = await Game.StartAsync(AcsStarterFactory.Create(), properties, new ProgressHandler(ui), cancellationToken);
                        }

                    Logging.Write($"Result: {result?.GetDescription() ?? @"<NULL>"}");
                    if (ui.CancellationToken.IsCancellationRequested)
                    {
                        ui.OnError(new UserCancelledException());
                        return(null);
                    }

                    if (mode == GameMode.Race)
                    {
                        if (result == null)
                        {
                            var whatsGoingOn = AcLogHelper.TryToDetermineWhatsGoingOn();
                            if (whatsGoingOn != null)
                            {
                                properties.SetAdditional(whatsGoingOn);
                            }
                        }

                        var param = new GameEndedArgs(properties, result);
                        Ended?.Invoke(null, param);
                        /* TODO: should set result to null if param.Cancel is true? */

                        var replayHelper = new ReplayHelper(properties, result);
                        (result == null || param.Cancel ? Cancelled : Finished)?.Invoke(null, new GameFinishedArgs(properties, result));

                        ui.OnResult(result, replayHelper);
                    }
                    else
                    {
                        ui.OnResult(null, null);
                    }

                    return(result);
                } catch (Exception e) when(e.IsCanceled())
                {
                    // ui.OnError(new UserCancelledException());
                    ui.OnResult(null, null);
                    return(null);
                } catch (Exception e) {
                    Logging.Warning(e);
                    ui.OnError(e);
                    return(null);
                } finally {
                    linked?.Dispose();
                    IsInGame = false;
                }
            }
        }
Beispiel #4
0
        private static async Task <Game.Result> StartAsync(Game.StartProperties properties, bool raceMode)
        {
            AcSettingsHolder.Graphics.FixShadowMapBias();

            if (SettingsHolder.Drive.CopyFilterToSystemForOculus && AcSettingsHolder.Video.CameraMode.Id == "OCULUS")
            {
                properties.SetAdditional(new CopyFilterToSystemForOculusHelper());
            }

            if (SettingsHolder.Common.FixResolutionAutomatically)
            {
                AcSettingsHolder.Video.EnsureResolutionIsCorrect();
            }

            if (SettingsHolder.Drive.WeatherSpecificClouds)
            {
                properties.SetAdditional(new WeatherSpecificCloudsHelper());
            }

            if (SettingsHolder.Drive.WeatherSpecificTyreSmoke)
            {
                properties.SetAdditional(new WeatherSpecificTyreSmokeHelper());
            }

            if (SettingsHolder.Live.RsrEnabled && SettingsHolder.Live.RsrDisableAppAutomatically)
            {
                PrepareRaceModeRsr(properties);
            }

            if (SettingsHolder.Drive.SidekickIntegration && properties.BasicProperties?.CarId != null)
            {
                SidekickHelper.UpdateSidekickDatabase(properties.BasicProperties.CarId);
            }

            properties.SetAdditional(new WeatherSpecificVideoSettingsHelper());
            properties.SetAdditional(new ModeSpecificPresetsHelper());
            properties.SetAdditional(new CarSpecificControlsPresetHelper());

            if (raceMode)
            {
                if (properties.AssistsProperties == null)
                {
                    properties.AssistsProperties = _defaultAssistsFactory?.Create();
                }

                PrepareRaceModeImmediateStart(properties);
                PrepareRaceDriverName(properties);

                Logging.Write("Assists: " + properties.AssistsProperties?.GetDescription());
            }

            if (_uiFactory == null)
            {
                using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                    using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                        if (raceMode)
                        {
                            properties.SetAdditional(new RaceCommandExecutor(properties));
                        }
                        else
                        {
                            properties.SetAdditional(new ReplayCommandExecutor(properties));
                        }

                        return(await Game.StartAsync(AcsStarterFactory.Create(), properties, null, CancellationToken.None));
                    }
            }

            using (var ui = _uiFactory.Create()) {
                Logging.Write($"Starting game: {properties.GetDescription()}");
                ui.Show(properties);

                CancellationTokenSource linked = null;
                IsInGame = true;

                try {
                    Game.Result result;
                    using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                        using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                            if (raceMode)
                            {
                                properties.SetAdditional(new RaceCommandExecutor(properties));
                                Started?.Invoke(null, new GameStartedArgs(properties));

                                if (SettingsHolder.Drive.ContinueOnEscape)
                                {
                                    properties.SetAdditional(new ContinueRaceHelper());
                                }
                            }
                            else
                            {
                                properties.SetAdditional(new ReplayCommandExecutor(properties));
                            }

                            var cancellationToken = ui.CancellationToken;

                            if (SettingsHolder.Drive.ImmediateCancel)
                            {
                                var cancelHelper = new ImmediateCancelHelper();
                                linked            = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelHelper.GetCancellationToken());
                                cancellationToken = linked.Token;

                                properties.SetAdditional(cancelHelper);
                                properties.SetKeyboardListener = true;
                            }

                            result = await Game.StartAsync(AcsStarterFactory.Create(), properties, new ProgressHandler(ui), cancellationToken);
                        }

                    Logging.Write($"Result: {result?.GetDescription() ?? @"<NULL>"}");
                    if (ui.CancellationToken.IsCancellationRequested)
                    {
                        ui.OnError(new UserCancelledException());
                        return(null);
                    }

                    if (raceMode)
                    {
                        if (result == null)
                        {
                            var whatsGoingOn = AcLogHelper.TryToDetermineWhatsGoingOn();
                            if (whatsGoingOn != null)
                            {
                                properties.SetAdditional(whatsGoingOn);
                            }
                        }

                        var param = new GameEndedArgs(properties, result);
                        Ended?.Invoke(null, param);
                        /* TODO: should set result to null if param.Cancel is true? */

                        var replayHelper = new ReplayHelper(properties, result);
                        (result == null || param.Cancel ? Cancelled : Finished)?.Invoke(null, new GameFinishedArgs(properties, result));

                        ui.OnResult(result, replayHelper);
                    }
                    else
                    {
                        ui.OnResult(null, null);
                    }

                    return(result);
                } catch (TaskCanceledException) {
                    // ui.OnError(new UserCancelledException());
                    ui.OnResult(null, null);
                    return(null);
                } catch (Exception e) {
                    Logging.Warning(e);
                    ui.OnError(e);
                    return(null);
                } finally {
                    linked?.Dispose();
                    IsInGame = false;
                }
            }
        }
Beispiel #5
0
        void IGameUi.OnResult(Game.Result result, ReplayHelper replayHelper) {
            if (result != null && result.NumberOfSessions == 1 && result.Sessions.Length == 1
                    && result.Sessions[0].Type == Game.SessionType.Practice && SettingsHolder.Drive.SkipPracticeResults
                    || _properties?.ReplayProperties != null || _properties?.BenchmarkProperties != null) {
                Close();
                return;
            }

            /* save replay button * /
            Func<string> buttonText = () => replayHelper?.IsReplayRenamed == true ?
                    AppStrings.RaceResult_UnsaveReplay : AppStrings.RaceResult_SaveReplay;

            var saveReplayButton = CreateExtraDialogButton(buttonText(), () => {
                if (replayHelper == null) {
                    Logging.Warning("ReplayHelper=<NULL>");
                    return;
                }

                replayHelper.IsReplayRenamed = !replayHelper.IsReplayRenamed;
            });

            if (replayHelper == null) {
                saveReplayButton.IsEnabled = false;
            } else {
                replayHelper.PropertyChanged += (sender, args) => {
                    if (args.PropertyName == nameof(ReplayHelper.IsReplayRenamed)) {
                        saveReplayButton.Content = buttonText();
                    }
                };
            }

            /* save replay alt button */
            ButtonWithComboBox saveReplayButton;
            if (replayHelper != null) {
                Func<string> buttonText = () => replayHelper.IsRenamed ?
                        AppStrings.RaceResult_UnsaveReplay : AppStrings.RaceResult_SaveReplay;
                Func<string> saveAsText = () => string.Format(replayHelper.IsRenamed ? "Saved as “{0}”" : "Save as “{0}”",
                        replayHelper.Name);

                saveReplayButton = new ButtonWithComboBox {
                    Margin = new Thickness(4, 0, 0, 0),
                    MinHeight = 21,
                    MinWidth = 65,
                    Content = ToolsStrings.Shared_Replay,
                    Command = new AsyncCommand(replayHelper.Play),
                    MenuItems = {
                        new MenuItem { Header = saveAsText(), Command = new DelegateCommand(() => {
                            var newName = Prompt.Show("Save replay as:", "Replay Name", replayHelper.Name, "?", required: true);
                            if (!string.IsNullOrWhiteSpace(newName)) {
                                replayHelper.Name = newName;
                            }

                            replayHelper.IsRenamed = true;
                        }) },
                        new MenuItem { Header = buttonText(), Command = new DelegateCommand(replayHelper.Rename) },
                        new Separator(),
                        new MenuItem {
                            Header = "Share Replay",
                            Command = new AsyncCommand(() => {
                                var car = _properties?.BasicProperties?.CarId == null ? null :
                                        CarsManager.Instance.GetById(_properties.BasicProperties.CarId);
                                var track = _properties?.BasicProperties?.TrackId == null ? null :
                                        TracksManager.Instance.GetById(_properties.BasicProperties.TrackId);
                                return SelectedReplayPage.ShareReplay(replayHelper.Name, replayHelper.Filename, car, track);
                            })
                        },
                    }
                };
                
                replayHelper.PropertyChanged += (sender, args) => {
                    if (args.PropertyName == nameof(ReplayHelper.IsRenamed)) {
                        ((MenuItem)saveReplayButton.MenuItems[0]).Header = saveAsText();
                        ((MenuItem)saveReplayButton.MenuItems[1]).Header = buttonText();
                    }
                };
            } else {
                saveReplayButton = null;
            }

            var tryAgainButton = CreateExtraDialogButton(AppStrings.RaceResult_TryAgain, () => {
                CloseWithResult(MessageBoxResult.None);
                GameWrapper.StartAsync(_properties).Forget();
            });

            Button fixButton = null;

            if (result == null || !result.IsNotCancelled) {
                Model.CurrentState = ViewModel.State.Cancelled;

                var whatsGoingOn = _properties?.GetAdditional<AcLogHelper.WhatsGoingOn>();
                fixButton = this.CreateFixItButton(whatsGoingOn?.Solution);
                Model.ErrorMessage = whatsGoingOn?.GetDescription();
            } else {
                try {
                    Model.CurrentState = ViewModel.State.Finished;
                    Model.FinishedData = GetFinishedData(_properties, result);
                } catch (Exception e) {
                    Logging.Warning(e);

                    Model.CurrentState = ViewModel.State.Error;
                    Model.ErrorMessage = AppStrings.RaceResult_ResultProcessingError;
                    Buttons = new[] { CloseButton };
                    return;
                }
            }

            Buttons = new[] {
                fixButton,
                fixButton == null ? saveReplayButton : null,
                fixButton == null ? tryAgainButton : null,
                CloseButton
            };
        }