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
}
}