コード例 #1
0
ファイル: GameWrapper.cs プロジェクト: killvxk/actools
        private static void PrepareRaceDriverName(Game.StartProperties properties)
        {
            if (properties.HasAdditional <SrsMark>())
            {
                return;
            }

            if (properties.BasicProperties?.DriverName != null)
            {
                properties.SetAdditional(new DriverName(properties.BasicProperties.DriverName, properties.BasicProperties.DriverNationality));
                return;
            }

            if (properties.ModeProperties is Game.OnlineProperties online && SettingsHolder.Live.SrsEnabled && SettingsHolder.Live.SrsAutoMode)
            {
                var filter = Filter.Create(new StringTester(), SettingsHolder.Live.SrsAutoMask, true);
                if (filter.Test(online.ServerName ?? ""))
                {
                    Logging.Write("Looks like this is a SRS server, let’s use SRS name");
                    properties.SetAdditional(new SrsMark {
                        Name        = SrsMark.GetName(),
                        Nationality = "",
                        Team        = ""
                    });
                    return;
                }
            }

            properties.SetAdditional(new DriverName());
        }
コード例 #2
0
ファイル: GameWrapper.cs プロジェクト: Abishai2007/actools
        private static async Task <Game.Result> StartAsync_NoUi(Game.StartProperties properties, GameMode mode)
        {
            using (ReplaysExtensionSetter.OnlyNewIfEnabled())
                using (ScreenshotsConverter.OnlyNewIfEnabled()) {
                    if (mode == GameMode.Race)
                    {
                        properties.SetAdditional(new RaceCommandExecutor(properties));
                    }
                    else if (mode == GameMode.Replay)
                    {
                        properties.SetAdditional(new ReplayCommandExecutor(properties));
                    }

                    return(await Game.StartAsync(AcsStarterFactory.Create(), properties, null, CancellationToken.None));
                }
        }
コード例 #3
0
ファイル: GameWrapper.cs プロジェクト: killvxk/actools
 private static void PrepareRaceModeImmediateStart(Game.StartProperties properties)
 {
     if (!SettingsHolder.Drive.ImmediateStart)
     {
         return;
     }
     properties.SetAdditional(new ImmediateStart());
 }
コード例 #4
0
        private static IAcsStarter CreateStarter(Game.StartProperties properties)
        {
            var starter = AcsStarterFactory.Create();

            if (SettingsHolder.Drive.PatchAcToDisableShadows && AcShadowsPatcher.IsSupposedToWork())
            {
                properties.SetAdditional(new AcShadowsPatcher(starter));
            }

            return(starter);
        }
コード例 #5
0
ファイル: GameWrapper.cs プロジェクト: Abishai2007/actools
        private static void StartAsync_Prepare(Game.StartProperties properties)
        {
            if (!_nationCodesProviderSet)
            {
                _nationCodesProviderSet = true;
                try {
                    Game.NationCodeProvider = NationCodeProvider.Instance;
                } catch (Exception e) {
                    Logging.Unexpected(e);
                }
            }

            AcSettingsHolder.Graphics.FixShadowMapBias();

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

            if (SettingsHolder.Common.FixResolutionAutomatically)
            {
                Logging.Debug("Trying to fix resolution just in case…");
                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);
            }

            if (SettingsHolder.Drive.RaceEssentialsIntegration && properties.BasicProperties?.CarId != null)
            {
                RaceEssentialsHelper.UpdateRaceEssentialsDatabase(properties.BasicProperties.CarId, false);
            }

            properties.SetAdditional(new WeatherSpecificVideoSettingsHelper());
            properties.SetAdditional(new ModeSpecificPresetsHelper());
            properties.SetAdditional(new CarSpecificControlsPresetHelper());
        }
コード例 #6
0
ファイル: GameWrapper.cs プロジェクト: killvxk/actools
        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;
                }
            }
        }
コード例 #7
0
ファイル: GameWrapper.cs プロジェクト: killvxk/actools
        private static void StartAsync_Prepare(Game.StartProperties properties)
        {
            if (!_nationCodesProviderSet)
            {
                _nationCodesProviderSet = true;
                try {
                    Game.NationCodeProvider = NationCodeProvider.Instance;
                } catch (Exception e) {
                    Logging.Unexpected(e);
                }
            }

            AcSettingsHolder.Graphics.FixShadowMapBias();
            CarCustomDataHelper.Revert();

            if (SettingsHolder.Drive.CheckAndFixControlsOrder)
            {
                try {
                    AcSettingsHolder.Controls.FixControllersOrder();
                } catch (Exception e) {
                    VisualCppTool.OnException(e, null);
                }
            }

            if (SettingsHolder.Common.FixResolutionAutomatically)
            {
                Logging.Debug("Trying to fix resolution just in case…");
                AcSettingsHolder.Video.EnsureResolutionIsCorrect();
            }

            properties.SetAdditional(new TrackDetails());
            properties.SetAdditional(
                new WeatherProceduralHelper(properties.ConditionProperties != null && properties.ConditionProperties.RoadTemperature == null));
            properties.SetAdditional(new WeatherSpecificLightingHelper());

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

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

            if (File.Exists(properties.GetAdditional <CustomTrackState>()?.Filename))
            {
                properties.SetAdditional(new CustomTrackPropertiesHelper(properties.GetAdditional <CustomTrackState>()?.Filename ?? string.Empty));
            }

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

            var carId = properties.BasicProperties?.CarId;

            if (carId != null)
            {
                if (SettingsHolder.Drive.CamberExtravaganzaIntegration)
                {
                    CamberExtravaganzaHelper.UpdateDatabase(carId);
                }

                if (SettingsHolder.Drive.SidekickIntegration)
                {
                    SidekickHelper.UpdateSidekickDatabase(carId);
                }

                if (SettingsHolder.Drive.SidekickOdometerExportValues)
                {
                    SidekickHelper.OdometerExport(carId);
                }

                if (SettingsHolder.Drive.RaceEssentialsIntegration)
                {
                    RaceEssentialsHelper.UpdateRaceEssentialsDatabase(carId, false);
                }

                if (SettingsHolder.Drive.StereoOdometerExportValues)
                {
                    StereoOdometerHelper.Export(carId);
                }

                if (PatchHelper.IsFeatureSupported(PatchHelper.FeatureNeedsOdometerValue))
                {
                    var car = CarsManager.Instance.GetById(carId);
                    if (car != null)
                    {
                        properties.SetAdditional(new DrivenDistance(car.TotalDrivenDistance));
                    }
                }
            }

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

            if (PatchHelper.GetInstalledVersion() != null)
            {
                properties.SetAdditional(new AcPatchTrackOutline());
            }

            properties.SetAdditional(new ExtraHotkeysRaceHelper());

            if (SettingsHolder.Drive.CopyFilterToSystemForOculus &&
                (AcSettingsHolder.Video.CameraMode.Id == "OCULUS" || AcSettingsHolder.Video.CameraMode.Id == "OPENVR"))
            {
                properties.SetAdditional(new CopyFilterToSystemForOculusHelper());
            }
        }
コード例 #8
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;
                }
            }
        }
コード例 #9
0
ファイル: GameWrapper.cs プロジェクト: WildGenie/actools
        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;
                }
            }
        }
コード例 #10
0
ファイル: GameWrapper.cs プロジェクト: Abishai2007/actools
        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;
                }
            }
        }