Beispiel #1
0
        private static async Task <Game.Result> StartAsync_Ui(Game.StartProperties properties, GameMode mode)
        {
            //return null;


            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));
                        Logging.Debug("Showing Results ?");
                        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;
                }
            }
        }