Пример #1
0
 internal SavesMapBuilder Referencing(LocalSceneFile scene, out LocalSceneFile outScene)
 {
     _scripts.Last().Scenes.Add(scene);
     _scenes.Add(scene);
     outScene = scene;
     return(this);
 }
Пример #2
0
        private async Task <bool> UpgradeScene(LocalSceneFile scene, IEnumerable <RegistrySavesMatch> matches, IDictionary <RegistryPackageVersionContext, LocalPackageInfo> upgraded)
        {
            Renderer.Write($"Updating scene ");
            Renderer.Write(Controller.GetDisplayPath(scene.FullPath), ConsoleColor.Blue);
            Renderer.Write($"... ");

            var changes = 0;

            foreach (var match in matches)
            {
                if (upgraded.TryGetValue(match.Remote, out var info))
                {
                    changes += await Controller.UpgradeSceneAsync(scene, match.Local, info);
                }
            }

            if (changes > 0)
            {
                Renderer.WriteLine("  Scene updated", ConsoleColor.Green);
            }
            else
            {
                Renderer.WriteLine("  Scene already up to date", ConsoleColor.DarkGray);
            }

            return(true);
        }
        public async Task CanFindAndReplaceAScript()
        {
            var folders = new Mock <IFoldersHelper>();

            folders
            .Setup(x => x.ToRelativeToVam(It.IsAny <string>()))
            .Returns((string path) => path.Replace(@"C:\VaM\", ""));
            var scene  = new LocalSceneFile(@"C:\VaM\Saves\My Scene.json");
            var script = new LocalScriptFile(@"C:\VaM\Custom\Scripts\My Script.cs", "SOMEHASH");
            var info   = new LocalPackageInfo
            {
                Files = new[]
                {
                    new InstalledFileInfo
                    {
                        RegistryFile = new RegistryFile
                        {
                            Hash = new RegistryHash
                            {
                                Value = "SOMEHASH"
                            }
                        },
                        FullPath = @"C:\VaM\Custom\Scripts\author\some-package\1.0.0\My Script.cs"
                    }
                }
            };
            var serializer = new Mock <ISceneSerializer>(MockBehavior.Strict);
            var updates    = new List <(string before, string after)> {
                (@"Custom/Scripts/My Script.cs", @"Custom/Scripts/author/some-package/1.0.0/My Script.cs"),
                (@"Saves/Scripts/My Script.cs", @"Custom/Scripts/author/some-package/1.0.0/My Script.cs"),
                (@"My Script.cs", @"Custom/Scripts/author/some-package/1.0.0/My Script.cs")
            };
            var effectiveUpdates = new List <(string before, string after)> {
                (@"Custom/Scripts/My Script.cs", @"Custom/Scripts/author/some-package/1.0.0/My Script.cs")
            };
            var json = new SceneJsonMock(new AtomJsonMock(new PluginJsonMock(@"Custom/Scripts/My Script.cs")));

            serializer
            .Setup(s => s.DeserializeAsync(@"C:\VaM\Saves\My Scene.json"))
            .ReturnsAsync(json);
            serializer
            .Setup(s => s.SerializeAsync(json, @"C:\VaM\Saves\My Scene.json"))
            .Returns(Task.CompletedTask);
            var handler = new UpgradeSceneHandler(serializer.Object, folders.Object);

            var result = await handler.UpgradeSceneAsync(scene, script, info);

            Assert.That(result, Is.EqualTo(1));
            Assert.That(json.Atoms.First().Plugins.First().Path, Is.EqualTo("Custom/Scripts/author/some-package/1.0.0/My Script.cs"));
        }
        private async Task <int> ApplyChanges(LocalSceneFile scene, Dictionary <string, string> changes)
        {
            var counter = 0;
            var json    = await _serializer.DeserializeAsync(scene.FullPath).ConfigureAwait(false);

            foreach (var plugins in json.Atoms.SelectMany(a => a.Plugins).GroupBy(p => p.Path))
            {
                if (changes.TryGetValue(plugins.Key, out var after))
                {
                    foreach (var plugin in plugins)
                    {
                        plugin.Path = after;
                        counter++;
                    }
                }
            }
            if (counter > 0)
            {
                await _serializer.SerializeAsync(json, scene.FullPath).ConfigureAwait(false);
            }
            return(counter);
        }
        public async Task <int> UpgradeSceneAsync(LocalSceneFile scene, LocalScriptFile local, LocalPackageInfo after)
        {
            if (scene is null)
            {
                throw new ArgumentNullException(nameof(scene));
            }
            if (local is null)
            {
                throw new ArgumentNullException(nameof(local));
            }
            if (after is null)
            {
                throw new ArgumentNullException(nameof(after));
            }

            var changes = DetermineChanges(local, after);

            if (changes.Count == 0)
            {
                return(0);
            }

            return(await ApplyChanges(scene, changes).ConfigureAwait(false));
        }
Пример #6
0
 internal SavesMapBuilder WithScene(LocalSceneFile scene)
 {
     _scenes.Add(scene);
     return(this);
 }
Пример #7
0
        private async Task <LocalSceneFile> LoadSceneAsync(ConcurrentDictionary <string, LocalScriptFile> scripts, string sceneFile, bool shouldTryLoadingReferences)
        {
            _logger.Verbose($"{sceneFile}: Loading");
            var scene = new LocalSceneFile(sceneFile);

            try
            {
                var scriptRefs = await _sceneSerializer.FindScriptsFastAsync(sceneFile).ConfigureAwait(false);

                foreach (var scriptRefRelativePath in scriptRefs.Distinct())
                {
                    string fullPath  = GetReferenceFullPath(sceneFile, scriptRefRelativePath);
                    string localPath = _fs.Path.GetFullPath(_fs.Path.GetFileName(fullPath), _fs.Path.GetDirectoryName(sceneFile));
                    _logger.Verbose($"{sceneFile}: Mapping reference {scriptRefRelativePath} to {fullPath}");

                    if (scripts.TryGetValue(localPath, out var localScriptRef))
                    {
                        scene.References(localScriptRef);
                        localScriptRef.ReferencedBy(scene);
                        _logger.Verbose($"{sceneFile}: Script {fullPath} was found in the same folder, and was already loaded by earlier scan");
                    }
                    else if (scripts.TryGetValue(fullPath, out var scriptRef))
                    {
                        scene.References(scriptRef);
                        scriptRef.ReferencedBy(scene);
                        _logger.Verbose($"{sceneFile}: Script {fullPath} was already loaded by earlier scan");
                    }
                    else if (_ignoredPaths.Any(p => fullPath.StartsWith(p)))
                    {
                        _logger.Verbose($"{sceneFile}: Script {fullPath} was in an ignored path");
                        continue;
                    }
                    else if (shouldTryLoadingReferences)
                    {
                        string path;
                        if (_fs.File.Exists(fullPath))
                        {
                            path = fullPath;
                        }
                        else if (_fs.File.Exists(localPath))
                        {
                            path = localPath;
                            _logger.Verbose($"{sceneFile}: Script {fullPath} was not found, but a local version was found at {localPath}");
                        }
                        else
                        {
                            scene.AddError($"Script does not exist: '{fullPath}'", LocalFileErrorLevel.Warning);
                            _logger.Warning($"{sceneFile}: Script {fullPath} does not exist");
                            continue;
                        }

                        if (_fs.Path.GetExtension(scriptRefRelativePath) == ".cslist")
                        {
                            scriptRef = await LoadScriptListAsync(scripts, fullPath, true).ConfigureAwait(false);
                        }
                        else
                        {
                            scriptRef = await LoadScriptAsync(fullPath).ConfigureAwait(false);
                        }

                        scene.References(scriptRef);
                        scriptRef.ReferencedBy(scene);

                        if (scriptRef.Status > LocalFileErrorLevel.None)
                        {
                            scene.AddError($"Script is invalid: '{fullPath}'", LocalFileErrorLevel.Warning);
                            _logger.Warning($"{sceneFile}: Script {fullPath} have errors: {string.Join(", ", scriptRef.Errors)}.");
                        }
                        else
                        {
                            scripts.TryAdd(fullPath, scriptRef);
                            _logger.Verbose($"{sceneFile}: Script {fullPath} exists and has been loaded");
                        }
                    }
                    else
                    {
                        scene.AddError($"Script does not exist: '{fullPath}'", LocalFileErrorLevel.Warning);
                        _logger.Warning($"{sceneFile}: Script {fullPath} does not exist.");
                    }
                }
            }
            catch (Exception exc)
            {
                scene.AddError(exc.Message, LocalFileErrorLevel.Error);
                _logger.Error($"{sceneFile}: {exc.Message}");
            }
            return(scene);
        }