public RegistrySavesMatches MatchLocalFilesToRegistry(SavesMap saves, Registry registry) { var flattened = registry.FlattenFiles(RegistryPackageType.Scripts).Where(IsScript).ToList(); var scripts = new List <LocalScriptFile>(saves.Scripts); var withHash = flattened .Where(svf => svf.File.Hash?.Value != null) // NOTE: This will match the first of two packages sharing the same file .GroupBy(svf => svf.File.Hash.Value) .ToDictionary(svf => svf.Key, svf => svf.ToList()); var byHash = MatchByHash(scripts, withHash).ToArray(); var matchedByHash = new HashSet <LocalScriptFile>(byHash.Select(m => m.Local)); scripts = scripts.Where(s => !matchedByHash.Contains(s)).ToList(); var withFilename = flattened .Where(remote => remote.File.Filename != null) .GroupBy(remote => Path.GetFileName(remote.File.Filename)) .Where(g => g.Count() == 1) .ToDictionary(g => g.Key, g => g.ToList()); var byFilename = MatchByFilename(scripts, withFilename).ToArray(); var matchedByFilename = new HashSet <LocalScriptFile>(byFilename.Concat(byHash).Select(m => m.Local)); return(new RegistrySavesMatches { HashMatches = byHash, FilenameMatches = byFilename, NoMatches = scripts.Where(s => !matchedByFilename.Contains(s)).ToArray() }); }
public RegistrySavesMatches Match(SavesMap saves, Registry registry) { var flattened = registry.FlattenFiles().ToList(); var scripts = new List <Script>(saves.Scripts); var withHash = flattened .Where(svf => svf.file.Hash?.Value != null) .GroupBy(svf => svf.file.Hash.Value) .ToDictionary(svf => svf.Key, svf => svf.First()); var byHash = MatchByHash(scripts, withHash).ToArray(); var matchedByHash = new HashSet <Script>(byHash.Select(m => m.Local)); scripts = scripts.Where(s => !matchedByHash.Contains(s)).ToList(); var withFilename = flattened .Where(svf => svf.file.Filename != null) .GroupBy(svf => Path.GetFileName(svf.file.Filename)) .ToDictionary(g => g.Key, g => g.FirstOrDefault()); var byFilename = MatchByFilename(scripts, withFilename).ToArray(); var matchedByFilename = new HashSet <Script>(byFilename.Concat(byHash).Select(m => m.Local)); return(new RegistrySavesMatches { HashMatches = byHash, FilenameMatches = byFilename, NoMatches = scripts.Where(s => !matchedByFilename.Contains(s)).ToArray() }); }
public IEnumerable <SearchResult> Search(Registry registry, SavesMap saves, string query) { if (registry is null) { throw new ArgumentNullException(nameof(registry)); } if (registry?.Packages is null) { throw new ArgumentException("registry does not have any scripts", nameof(registry)); } foreach (var package in registry.Packages) { if (!string.IsNullOrEmpty(query)) { if (!MatchesQuery(package, query)) { continue; } } var trusted = package.Versions? .SelectMany(v => v.Files) .Where(f => f.Url != null && !f.Ignore) .All(f => _trustedDomains.Any(t => f.Url.StartsWith(t))) ?? false; Script[] scripts = null; Scene[] scenes = null; if (saves != null && package.Versions != null) { // TODO: We should consider all files from a specific version of plugin together var allFilesFromAllVersions = package.Versions .SelectMany(v => v.Files ?? new SortedSet <RegistryFile>()); scripts = allFilesFromAllVersions .Where(regFile => regFile.Hash?.Value != null) .SelectMany(regFile => saves.Scripts.Where(localScript => localScript.Hash == regFile.Hash.Value)) .Distinct() .ToArray(); scenes = scripts .SelectMany(s => s.Scenes) .Distinct() .ToArray(); } yield return(new SearchResult { Package = package, Trusted = trusted, Scripts = scripts, Scenes = scenes }); } }
private void AssertNoErrors(SavesMap result) { Assert.That(result.Scenes.SelectMany(s => s.Errors ?? new List <LocalFileError>()), Is.Empty, result.Scenes.FirstOrDefault()?.Errors?.FirstOrDefault()?.Error); Assert.That(result.Scripts.SelectMany(s => s.Errors ?? new List <LocalFileError>()), Is.Empty, result.Scripts.FirstOrDefault()?.Errors?.FirstOrDefault().Error); }
public IEnumerable <SearchResult> Search(Registry registry, SavesMap saves, string query) { return(new SearchHandler(_config.Registry.TrustedDomains) .Search(registry, saves, query)); }
public RegistrySavesMatches MatchSavesToRegistry(SavesMap saves, Registry registry) { return(new RegistrySavesMatchHandler() .Match(saves, registry)); }
protected void PrintScanErrors(bool details, SavesMap map) { PrintScanErrors(details, map.Scripts.Cast <LocalFile>().Concat(map.Scenes).ToArray()); }