From b65122537d10e59c7da163bae5e567b1a5689181 Mon Sep 17 00:00:00 2001 From: uduki Date: Tue, 20 Oct 2026 11:29:53 +0900 Subject: [PATCH] =?UTF-8?q?=E6=A9=9F=E8=83=BD=E8=BF=BD=E5=8A=A0:=20?= =?UTF-8?q?=E3=83=A1=E3=83=A2=E3=83=AA=E4=B8=8A=E3=81=A7=E3=81=AE=E3=83=87?= =?UTF-8?q?=E3=82=A3=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA=E3=83=BB=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E9=AB=98=E9=80=9F=E5=88=A4=E5=AE=9A?= =?UTF-8?q?=E7=94=A8=E3=82=B9=E3=83=8A=E3=83=83=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Everything や NTFS MFT から取得したファイルツリーをインメモリで保持し、ディスクI/Oを行わずにファイル存在確認や一覧取得を可能にする FastDirectorySnapshot クラスを実装しました。 --- Services/FastDirectorySnapshot.cs | 170 ++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Services/FastDirectorySnapshot.cs diff --git a/Services/FastDirectorySnapshot.cs b/Services/FastDirectorySnapshot.cs new file mode 100644 index 0000000..f54a604 --- /dev/null +++ b/Services/FastDirectorySnapshot.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; + +namespace GameLauncher.Services +{ + /// + /// メモリ上に展開されたディレクトリおよびファイル構成の高速インメモリスナップショット + /// + public class FastDirectorySnapshot + { + #region □プロパティ(Public) + /// + /// ディレクトリパス -> 直下のファイル名(ファイル名のみ)のハッシュセット + /// + public Dictionary> FilesByDir { get; } = new(StringComparer.OrdinalIgnoreCase); + + /// + /// ディレクトリパス -> 直下のサブディレクトリのフルパスリスト + /// + public Dictionary> SubDirsByDir { get; } = new(StringComparer.OrdinalIgnoreCase); + + /// + /// 登録されている総ディレクトリ数 + /// + public int TotalDirectoryCount => this.SubDirsByDir.Count; + + /// + /// 登録されている総ファイル数 + /// + public int TotalFileCount => this.FilesByDir.Values.Sum(set => set.Count); + #endregion + + #region □メソッド(Public) + /// + /// ファイルをスナップショットに登録します。 + /// + public void AddFile(string dirPath, string fileName) + { + var normalizedDir = NormalizePath(dirPath); + if (!this.FilesByDir.TryGetValue(normalizedDir, out var files)) + { + files = new HashSet(StringComparer.OrdinalIgnoreCase); + this.FilesByDir[normalizedDir] = files; + } + files.Add(fileName); + + if (!this.SubDirsByDir.ContainsKey(normalizedDir)) + { + this.SubDirsByDir[normalizedDir] = new List(); + } + } + + /// + /// サブディレクトリを親ディレクトリのスナップショットに登録します。 + /// + public void AddDirectory(string parentDirPath, string childDirPath) + { + var normalizedParent = NormalizePath(parentDirPath); + var normalizedChild = NormalizePath(childDirPath); + + if (!this.SubDirsByDir.TryGetValue(normalizedParent, out var children)) + { + children = new List(); + this.SubDirsByDir[normalizedParent] = children; + } + + if (!children.Contains(normalizedChild, StringComparer.OrdinalIgnoreCase)) + { + children.Add(normalizedChild); + } + + if (!this.SubDirsByDir.ContainsKey(normalizedChild)) + { + this.SubDirsByDir[normalizedChild] = new List(); + } + } + + /// + /// 指定されたディレクトリが存在するかどうかをインメモリで判定します。 + /// + public bool DirectoryExists(string dirPath) + { + var normalized = NormalizePath(dirPath); + return this.SubDirsByDir.ContainsKey(normalized) || this.FilesByDir.ContainsKey(normalized); + } + + /// + /// 指定されたファイルが存在するかどうかをインメモリで判定します。 + /// + public bool FileExists(string filePath) + { + var dir = NormalizePath(Path.GetDirectoryName(filePath) ?? string.Empty); + var name = Path.GetFileName(filePath); + if (this.FilesByDir.TryGetValue(dir, out var files)) + { + return files.Contains(name); + } + return false; + } + + /// + /// 指定されたディレクトリ直下のサブディレクトリ一覧を取得します。 + /// + public string[] GetDirectories(string dirPath) + { + var normalized = NormalizePath(dirPath); + if (this.SubDirsByDir.TryGetValue(normalized, out var subDirs)) + { + return subDirs.ToArray(); + } + return Array.Empty(); + } + + /// + /// 指定されたディレクトリ直下のファイルフルパス一覧を取得します。 + /// + public string[] GetFiles(string dirPath, string searchPattern = "*") + { + var normalized = NormalizePath(dirPath); + if (!this.FilesByDir.TryGetValue(normalized, out var files)) + { + return Array.Empty(); + } + + if (string.IsNullOrEmpty(searchPattern) || searchPattern == "*") + { + return files.Select(f => Path.Combine(normalized, f)).ToArray(); + } + + if (searchPattern.StartsWith("*.") && searchPattern.IndexOf('*', 1) < 0 && searchPattern.IndexOf('?', 1) < 0) + { + string ext = searchPattern.Substring(1); + return files + .Where(f => f.EndsWith(ext, StringComparison.OrdinalIgnoreCase)) + .Select(f => Path.Combine(normalized, f)) + .ToArray(); + } + + var regex = WildcardToRegex(searchPattern); + return files + .Where(f => regex.IsMatch(f)) + .Select(f => Path.Combine(normalized, f)) + .ToArray(); + } + #endregion + + #region □メソッド(Private) + /// + /// パス末尾の区切り文字を除去し、正規化します。 + /// + private static string NormalizePath(string path) + { + if (string.IsNullOrEmpty(path)) return string.Empty; + return path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + + /// + /// ワイルドカードパターンを正規表現に変換します。 + /// + private static Regex WildcardToRegex(string pattern) + { + string escaped = Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", "."); + return new Regex("^" + escaped + "$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + } + #endregion + } +}