| |
| |
|
|
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Collections.ObjectModel; |
| using System.Globalization; |
| using System.IO; |
| using System.Text.RegularExpressions; |
|
|
| namespace System.Management.Automation |
| { |
| internal sealed class MUIFileSearcher |
| { |
| |
| |
| |
| |
| |
| |
| |
| private MUIFileSearcher(string target, Collection<string> searchPaths, SearchMode searchMode) |
| { |
| Target = target; |
| SearchPaths = searchPaths; |
| SearchMode = searchMode; |
| } |
|
|
| |
| |
| |
| |
| |
| private MUIFileSearcher(string target, Collection<string> searchPaths) |
| : this(target, searchPaths, SearchMode.Unique) |
| { |
| } |
|
|
| #region Basic Properties |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal string Target { get; } = null; |
|
|
| |
| |
| |
| internal Collection<string> SearchPaths { get; } = null; |
|
|
| |
| |
| |
| internal SearchMode SearchMode { get; } = SearchMode.Unique; |
|
|
| private static readonly System.IO.EnumerationOptions _enumerationOptions = new() |
| { |
| IgnoreInaccessible = false, |
| AttributesToSkip = 0, |
| MatchType = MatchType.Win32, |
| MatchCasing = MatchCasing.CaseInsensitive, |
| }; |
|
|
| private Collection<string> _result = null; |
|
|
| |
| |
| |
| internal Collection<string> Result |
| { |
| get |
| { |
| if (_result == null) |
| { |
| _result = new Collection<string>(); |
|
|
| |
| SearchForFiles(); |
| } |
|
|
| return _result; |
| } |
| } |
|
|
| #endregion |
|
|
| #region File Search |
|
|
| |
| |
| |
| |
| private readonly Hashtable _uniqueMatches = new Hashtable(StringComparer.OrdinalIgnoreCase); |
|
|
| |
| |
| |
| private void SearchForFiles() |
| { |
| if (string.IsNullOrEmpty(this.Target)) |
| return; |
|
|
| string pattern = Path.GetFileName(this.Target); |
| if (string.IsNullOrEmpty(pattern)) |
| return; |
|
|
| Collection<string> normalizedSearchPaths = NormalizeSearchPaths(this.Target, this.SearchPaths); |
|
|
| foreach (string directory in normalizedSearchPaths) |
| { |
| SearchForFiles(pattern, directory); |
|
|
| if (this.SearchMode == SearchMode.First && this.Result.Count > 0) |
| { |
| return; |
| } |
| } |
| } |
|
|
| private void AddFiles(string muiDirectory, string directory, string pattern) |
| { |
| if (Directory.Exists(muiDirectory)) |
| { |
| foreach (string file in Directory.EnumerateFiles(muiDirectory, pattern, _enumerationOptions)) |
| { |
| string path = Path.Combine(muiDirectory, file); |
|
|
| switch (this.SearchMode) |
| { |
| case SearchMode.All: |
| _result.Add(path); |
| break; |
|
|
| case SearchMode.Unique: |
| |
| |
| |
| |
| |
| string leafFileName = Path.GetFileName(file); |
| string uniqueToDirectory = Path.Combine(directory, leafFileName); |
|
|
| if (!_result.Contains(path) && !_uniqueMatches.Contains(uniqueToDirectory)) |
| { |
| _result.Add(path); |
| _uniqueMatches[uniqueToDirectory] = true; |
| } |
|
|
| break; |
|
|
| case SearchMode.First: |
| _result.Add(path); |
| return; |
|
|
| default: |
| break; |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private void SearchForFiles(string pattern, string directory) |
| { |
| List<string> cultureNameList = new List<string>(); |
| CultureInfo culture = CultureInfo.CurrentUICulture; |
|
|
| while (culture != null && !string.IsNullOrEmpty(culture.Name)) |
| { |
| cultureNameList.Add(culture.Name); |
| culture = culture.Parent; |
| } |
|
|
| cultureNameList.Add(string.Empty); |
|
|
| |
| if (!cultureNameList.Contains("en-US")) |
| { |
| cultureNameList.Add("en-US"); |
| } |
|
|
| if (!cultureNameList.Contains("en")) |
| { |
| cultureNameList.Add("en"); |
| } |
|
|
| foreach (string name in cultureNameList) |
| { |
| string muiDirectory = Path.Combine(directory, name); |
|
|
| AddFiles(muiDirectory, directory, pattern); |
|
|
| if (this.SearchMode == SearchMode.First && this.Result.Count > 0) |
| { |
| return; |
| } |
| } |
|
|
| return; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private static Collection<string> NormalizeSearchPaths(string target, Collection<string> searchPaths) |
| { |
| Collection<string> result = new Collection<string>(); |
|
|
| |
| |
| if (!string.IsNullOrEmpty(target) && !string.IsNullOrEmpty(Path.GetDirectoryName(target))) |
| { |
| string directory = Path.GetDirectoryName(target); |
|
|
| if (Directory.Exists(directory)) |
| { |
| result.Add(Path.GetFullPath(directory)); |
| } |
|
|
| |
| |
| return result; |
| } |
|
|
| |
| if (searchPaths != null) |
| { |
| foreach (string directory in searchPaths) |
| { |
| if (!result.Contains(directory) && Directory.Exists(directory)) |
| { |
| result.Add(directory); |
| } |
| } |
| } |
|
|
| |
| string defaultPSPath = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID); |
| if (defaultPSPath != null && |
| !result.Contains(defaultPSPath) && |
| Directory.Exists(defaultPSPath)) |
| { |
| result.Add(defaultPSPath); |
| } |
|
|
| return result; |
| } |
|
|
| #endregion |
|
|
| #region Static API's |
|
|
| |
| |
| |
| |
| |
| internal static Collection<string> SearchFiles(string pattern) |
| { |
| return SearchFiles(pattern, new Collection<string>()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static Collection<string> SearchFiles(string pattern, Collection<string> searchPaths) |
| { |
| MUIFileSearcher searcher = new MUIFileSearcher(pattern, searchPaths); |
|
|
| return searcher.Result; |
| } |
|
|
| |
| |
| |
| |
| |
| internal static string LocateFile(string file) |
| { |
| return LocateFile(file, new Collection<string>()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string LocateFile(string file, Collection<string> searchPaths) |
| { |
| MUIFileSearcher searcher = new MUIFileSearcher(file, searchPaths, SearchMode.First); |
|
|
| if (searcher.Result == null || searcher.Result.Count == 0) |
| return null; |
|
|
| return searcher.Result[0]; |
| } |
|
|
| #endregion |
| } |
|
|
| |
| |
| |
| internal enum SearchMode |
| { |
| |
| First, |
|
|
| |
| All, |
|
|
| |
| Unique |
| } |
| } |
|
|