// --------------------------------------------------------------------------------------------- // Copyright (c) 2021-2023, Jiaqi Liu. All rights reserved. // See LICENSE file in the project root for license information. // --------------------------------------------------------------------------------------------- namespace Pal3.Core.FileSystem { using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using DataReader.Cpk; using Utilities; /// /// File system wrapper for CPack archives /// public sealed class CpkFileSystem : ICpkFileSystem { private readonly string _rootPath; private readonly ConcurrentDictionary _cpkArchives = new(); private readonly Crc32Hash _crcHash; public CpkFileSystem(string rootPath, Crc32Hash crcHash) { if (!rootPath.EndsWith(Path.DirectorySeparatorChar)) { rootPath += Path.DirectorySeparatorChar; } if (!Directory.Exists(rootPath)) { throw new DirectoryNotFoundException(rootPath); } _rootPath = rootPath; _crcHash = crcHash; } public string GetRootPath() { return _rootPath; } /// /// Mount a Cpk archive to the file system. /// /// CPK file relative path /// Codepage CPK file uses for encoding text info public void Mount(string cpkFileRelativePath, int codepage) { string cpkFileName = CoreUtility.GetFileName(cpkFileRelativePath, Path.DirectorySeparatorChar).ToLower(); if (_cpkArchives.ContainsKey(cpkFileName)) { return; } string cpkFilePath = _rootPath + cpkFileRelativePath; if (!File.Exists(cpkFilePath)) { throw new FileNotFoundException(cpkFilePath); } var cpkArchive = new CpkArchive(cpkFilePath, _crcHash, codepage); cpkArchive.Init(); _cpkArchives[cpkFileName] = cpkArchive; } /// /// Check if file exists in any of the segmented archives using virtual path. /// /// File virtual path {Cpk file name}\{File relative path inside archive} /// Name of the segmented archive if exists /// True if file exists in segmented archive public bool FileExistsInSegmentedArchive(string fileVirtualPath, out string segmentedArchiveName) { ParseFileVirtualPath(fileVirtualPath, out var cpkFileName, out var relativeVirtualPath); if (_cpkArchives.ContainsKey(cpkFileName)) { segmentedArchiveName = null; return false; } // Check if the file exists in any of the segmented cpk archives. foreach (var subCpkPath in _cpkArchives.Keys .Where(_ => _.StartsWith(cpkFileName[..^CpkConstants.FileExtension.Length] + '_', StringComparison.OrdinalIgnoreCase))) { var relativePathInSubCpk = relativeVirtualPath.Replace(cpkFileName, subCpkPath); if (_cpkArchives[subCpkPath].FileExists(relativePathInSubCpk)) { segmentedArchiveName = subCpkPath; return true; } } segmentedArchiveName = null; return false; } /// /// Check if file exists in the archive using virtual path. /// /// File virtual path {Cpk file name}\{File relative path inside archive} /// Example: music.cpk\music\PI01.mp3 /// True if file exists public bool FileExists(string fileVirtualPath) { ParseFileVirtualPath(fileVirtualPath, out var cpkFileName, out var relativeVirtualPath); if (_cpkArchives.TryGetValue(cpkFileName, out CpkArchive archive)) { return archive.FileExists(relativeVirtualPath); } // Check if the file exists in any of the segmented cpk archives. foreach (var subCpkPath in _cpkArchives.Keys .Where(_ => _.StartsWith(cpkFileName[..^CpkConstants.FileExtension.Length] + '_', StringComparison.OrdinalIgnoreCase))) { var relativePathInSubCpk = relativeVirtualPath.Replace(cpkFileName, subCpkPath); if (_cpkArchives[subCpkPath].FileExists(relativePathInSubCpk)) { return true; } } return false; } /// /// Read all bytes of the given file. /// /// File virtual path inside CPK archive /// Decompressed, ready-to-go file content in byte array public byte[] ReadAllBytes(string fileVirtualPath) { ParseFileVirtualPath(fileVirtualPath, out var cpkFileName, out var relativeVirtualPath); if (_cpkArchives.TryGetValue(cpkFileName, out CpkArchive archive)) { return archive.ReadAllBytes(relativeVirtualPath); } // Find and read the file content in segmented cpk archives. foreach (var subCpkPath in _cpkArchives.Keys .Where(_ => _.StartsWith(cpkFileName[..^CpkConstants.FileExtension.Length] + '_', StringComparison.OrdinalIgnoreCase))) { var relativePathInSubCpk = relativeVirtualPath.Replace(cpkFileName, subCpkPath); if (_cpkArchives[subCpkPath].FileExists(relativePathInSubCpk)) { return _cpkArchives[subCpkPath].ReadAllBytes(relativePathInSubCpk); } } throw new FileNotFoundException($"<{fileVirtualPath}> not found"); } /// /// Preload archive into memory for faster read performance. /// public void LoadArchiveIntoMemory(string cpkFileName) { if (_cpkArchives.ContainsKey(cpkFileName.ToLower())) { _cpkArchives[cpkFileName.ToLower()].LoadArchiveIntoMemory(); } else { throw new Exception($"<{cpkFileName}> archive not mounted yet"); } } /// /// Dispose in-memory archive data. /// public void DisposeInMemoryArchive(string cpkFileName) { if (_cpkArchives.ContainsKey(cpkFileName.ToLower())) { _cpkArchives[cpkFileName.ToLower()].DisposeInMemoryArchive(); } else { throw new Exception($"<{cpkFileName}> archive not mounted yet"); } } /// /// Dispose all in-memory archive data. /// public void DisposeAllInMemoryArchives() { foreach (CpkArchive cpkArchive in _cpkArchives.Values) { cpkArchive.DisposeInMemoryArchive(); } } /// /// Extract all archives to the specified destination /// public void ExtractTo(string outputFolder) { foreach ((string cpkFileName, CpkArchive cpkArchive) in _cpkArchives) { var outputDir = outputFolder + cpkFileName + Path.DirectorySeparatorChar; if (!Directory.Exists(outputDir)) { Directory.CreateDirectory(outputDir); } cpkArchive.ExtractTo(outputDir); } } /// /// Searches for files in the file system that match the specified keyword. /// /// The keyword to search for. If empty, all files will be returned. /// A list of file paths that match the specified keyword. public IList Search(string keyword = "") { var results = new ConcurrentBag>(); Parallel.ForEach(_cpkArchives, archive => { var rootNodes = archive.Value.GetRootEntries(); results.Add(from result in SearchInternal(rootNodes, keyword) select archive.Key + CpkConstants.DirectorySeparatorChar + result); }); var resultList = new List(); foreach (var result in results) { resultList.AddRange(result); } return resultList; } private IEnumerable SearchInternal(IEnumerable nodes, string keyword) { foreach (CpkEntry node in nodes) { if (node.IsDirectory) { foreach (var result in SearchInternal(node.Children, keyword)) { yield return result; } } else if (node.VirtualPath.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0) { yield return node.VirtualPath; } } } /// /// Searches for files in the CPK file system that match the specified keywords. /// /// The list of keywords to search for. /// A dictionary where the keys are the file paths of the matching files, /// and the values are the lines in the files that contain the keywords. public IDictionary> BatchSearch(IList keywords) { var results = new ConcurrentDictionary>(); foreach (string keyword in keywords) { results[keyword] = new ConcurrentBag(); } Parallel.ForEach(_cpkArchives, archive => { var rootNodes = archive.Value.GetRootEntries(); foreach (var match in SearchInternal(rootNodes, keywords)) { foreach (var keyword in keywords) { if (match.Value.Contains(keyword)) { var resultPath = archive.Key + CpkConstants.DirectorySeparatorChar + match.Key; results[keyword].Add(resultPath); } } } }); return results.ToDictionary>, string, IList>(result => result.Key, result => result.Value.ToList()); } private IDictionary> SearchInternal(IEnumerable nodes, IList keywords) { var results = new Dictionary>(); foreach (CpkEntry node in nodes) { if (node.IsDirectory) { foreach (var result in SearchInternal(node.Children, keywords)) { if (!results.ContainsKey(result.Key)) { results[result.Key] = new HashSet(); } foreach (var path in result.Value) { results[result.Key].Add(path); } } } else { foreach (var keyword in keywords) { if (!string.IsNullOrEmpty(node.VirtualPath) && node.VirtualPath.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0) { if (!results.ContainsKey(node.VirtualPath)) { results[node.VirtualPath] = new HashSet(); } results[node.VirtualPath].Add(keyword); } } } } return results; } /// /// Parse file virtual path into cpk file name and relative virtual path. /// /// File virtual path {Cpk file name}\{File relative path inside archive} /// {Cpk file name} /// {File relative path inside archive} private void ParseFileVirtualPath(string fullVirtualPath, out string cpkFileName, out string relativeVirtualPath) { if (!fullVirtualPath.Contains(CpkConstants.DirectorySeparatorChar)) { throw new ArgumentException($"File virtual path is invalid: {fullVirtualPath}"); } cpkFileName = fullVirtualPath[..fullVirtualPath.IndexOf(CpkConstants.DirectorySeparatorChar)].ToLower(); relativeVirtualPath = fullVirtualPath[(fullVirtualPath.IndexOf(CpkConstants.DirectorySeparatorChar) + 1)..]; } } }