public void WatchesForFileExistence(
            Utility.Return r,
            DirectoryPath folder,
            [Frozen] IFileSystem fs,
            [Frozen] IProfileIdentifier ident,
            [Frozen] IGameDirectoryLookup lookup,
            Func <ProfileDataFolderVm> sutF)
        {
            lookup.TryGet(ident.Release, out Arg.Any <DirectoryPath>())
            .Returns(x =>
            {
                x[1] = folder;
                return(true);
            });
            var dataFolder = Path.Combine(folder.Path, "Data");

            fs.Directory.Exists(dataFolder).Returns(r);
            var sut = sutF();

            sut.DataFolderResult.Succeeded.Should().Be(r == Utility.Return.True);
            if (r == Utility.Return.True)
            {
                sut.DataFolderResult.Value.Path.Should().Be(dataFolder);
                sut.Path.Path.Should().NotBeNullOrWhiteSpace();
            }
            else
            {
                sut.Path.Path.Should().BeNullOrWhiteSpace();
            }
        }
예제 #2
0
        public ProfileDataFolderVm(
            ILogger logger,
            ISchedulerProvider schedulerProvider,
            IWatchDirectory watchDirectory,
            IFileSystem fileSystem,
            IGameDirectoryLookup gameLocator,
            IProfileIdentifier ident)
        {
            FileSystem        = fileSystem;
            GameLocator       = gameLocator;
            _dataFolderResult = this.WhenAnyValue(x => x.DataPathOverride)
                                .Select(path =>
            {
                if (path != null)
                {
                    return(Observable.Return(GetResponse <DirectoryPath> .Succeed(path)));
                }
                logger.Information("Starting to locate data folder");
                return(Observable.Return(ident.Release)
                       .ObserveOn(schedulerProvider.TaskPool)
                       .Select(x =>
                {
                    try
                    {
                        if (!gameLocator.TryGet(x, out var gameFolder))
                        {
                            return GetResponse <DirectoryPath> .Fail(
                                "Could not automatically locate Data folder.  Run Steam/GoG/etc once to properly register things.");
                        }

                        return GetResponse <DirectoryPath> .Succeed(System.IO.Path.Combine(gameFolder, "Data"));
                    }
                    catch (Exception ex)
                    {
                        return GetResponse <DirectoryPath> .Fail(string.Empty, ex);
                    }
                }));
            })
                                .Switch()
                                // Watch folder for existence
                                .Select(x =>
            {
                if (x.Failed)
                {
                    return(Observable.Return(x));
                }
                return(watchDirectory.Watch(x.Value)
                       .StartWith(Unit.Default)
                       .Select(_ =>
                {
                    try
                    {
                        if (fileSystem.Directory.Exists(x.Value))
                        {
                            return x;
                        }
                        return GetResponse <DirectoryPath> .Fail($"Data folder did not exist: {x.Value}");
                    }
                    catch (Exception ex)
                    {
                        return GetResponse <DirectoryPath> .Fail(string.Empty, ex);
                    }
                }));
            })
                                .Switch()
                                .StartWith(GetResponse <DirectoryPath> .Fail("Data folder uninitialized"))
                                .Do(d =>
            {
                if (d.Failed)
                {
                    logger.Error("Could not locate data folder: {Reason}", d.Reason);
                }
                else
                {
                    logger.Information("Data Folder: {DataFolderPath}", d.Value);
                }
            })
                                .ToGuiProperty(this, nameof(DataFolderResult), deferSubscription: true);

            _path = this.WhenAnyValue(x => x.DataFolderResult)
                    .Select(x => x.Value)
                    .ToGuiProperty(this, nameof(Path), deferSubscription: true);
        }