add migration to file extensions for mare storage
This commit is contained in:
		| @@ -105,9 +105,9 @@ public sealed class FileCacheManager : IDisposable | |||||||
|  |  | ||||||
|     public List<FileCacheEntity> GetAllFileCaches() => _fileCaches.Values.SelectMany(v => v).ToList(); |     public List<FileCacheEntity> GetAllFileCaches() => _fileCaches.Values.SelectMany(v => v).ToList(); | ||||||
|  |  | ||||||
|     public string GetCacheFilePath(string hash, bool isTemporaryFile) |     public string GetCacheFilePath(string hash, string extension, bool isTemporaryFile) | ||||||
|     { |     { | ||||||
|         return Path.Combine(_configService.Current.CacheFolder, hash + (isTemporaryFile ? ".tmp" : string.Empty)); |         return Path.Combine(_configService.Current.CacheFolder, hash + "." + extension + (isTemporaryFile ? ".tmp" : string.Empty)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public FileCacheEntity? GetFileCacheByHash(string hash) |     public FileCacheEntity? GetFileCacheByHash(string hash) | ||||||
| @@ -181,12 +181,13 @@ public sealed class FileCacheManager : IDisposable | |||||||
|         { |         { | ||||||
|             sb.AppendLine(entry.CsvEntry); |             sb.AppendLine(entry.CsvEntry); | ||||||
|         } |         } | ||||||
|         if (File.Exists(_csvPath)) |  | ||||||
|         { |  | ||||||
|             File.Copy(_csvPath, CsvBakPath, overwrite: true); |  | ||||||
|         } |  | ||||||
|         lock (_fileWriteLock) |         lock (_fileWriteLock) | ||||||
|         { |         { | ||||||
|  |             if (File.Exists(_csvPath)) | ||||||
|  |             { | ||||||
|  |                 File.Copy(_csvPath, CsvBakPath, overwrite: true); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|                 File.WriteAllText(_csvPath, sb.ToString()); |                 File.WriteAllText(_csvPath, sb.ToString()); | ||||||
| @@ -199,6 +200,31 @@ public sealed class FileCacheManager : IDisposable | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     internal FileCacheEntity MigrateFileHashToExtension(FileCacheEntity fileCache, string ext) | ||||||
|  |     { | ||||||
|  |         try | ||||||
|  |         { | ||||||
|  |             RemoveHashedFile(fileCache); | ||||||
|  |             FileInfo oldCache = new(fileCache.ResolvedFilepath); | ||||||
|  |             var extensionPath = fileCache.ResolvedFilepath.ToUpper() + "." + ext; | ||||||
|  |             File.Move(fileCache.ResolvedFilepath, extensionPath, true); | ||||||
|  |             var newHashedEntity = new FileCacheEntity(fileCache.Hash, fileCache.PrefixedFilePath + "." + ext, DateTime.UtcNow.Ticks.ToString()); | ||||||
|  |             newHashedEntity.SetResolvedFilePath(extensionPath); | ||||||
|  |             FileInfo newCache = new FileInfo(extensionPath); | ||||||
|  |             newCache.LastAccessTime = oldCache.LastAccessTime; | ||||||
|  |             newCache.LastWriteTime = oldCache.LastWriteTime; | ||||||
|  |             AddHashedFile(newHashedEntity); | ||||||
|  |             _logger.LogDebug("Migrated from {oldPath} to {newPath}", fileCache.ResolvedFilepath, newHashedEntity.ResolvedFilepath); | ||||||
|  |             return newHashedEntity; | ||||||
|  |         } | ||||||
|  |         catch (Exception ex) | ||||||
|  |         { | ||||||
|  |             AddHashedFile(fileCache); | ||||||
|  |             _logger.LogWarning(ex, "Failed to migrate entity {entity}", fileCache.PrefixedFilePath); | ||||||
|  |             return fileCache; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private void AddHashedFile(FileCacheEntity fileCache) |     private void AddHashedFile(FileCacheEntity fileCache) | ||||||
|     { |     { | ||||||
|         if (!_fileCaches.TryGetValue(fileCache.Hash, out var entries)) |         if (!_fileCaches.TryGetValue(fileCache.Hash, out var entries)) | ||||||
|   | |||||||
| @@ -665,6 +665,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase | |||||||
|         List<FileReplacementData> missingFiles = new(); |         List<FileReplacementData> missingFiles = new(); | ||||||
|         moddedDictionary = new Dictionary<string, string>(StringComparer.Ordinal); |         moddedDictionary = new Dictionary<string, string>(StringComparer.Ordinal); | ||||||
|         ConcurrentDictionary<string, string> outputDict = new(StringComparer.Ordinal); |         ConcurrentDictionary<string, string> outputDict = new(StringComparer.Ordinal); | ||||||
|  |         bool hasMigrationChanges = false; | ||||||
|         try |         try | ||||||
|         { |         { | ||||||
|             var replacementList = charaData.FileReplacements.SelectMany(k => k.Value.Where(v => string.IsNullOrEmpty(v.FileSwapPath))).ToList(); |             var replacementList = charaData.FileReplacements.SelectMany(k => k.Value.Where(v => string.IsNullOrEmpty(v.FileSwapPath))).ToList(); | ||||||
| @@ -679,6 +680,12 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase | |||||||
|                 var fileCache = _fileDbManager.GetFileCacheByHash(item.Hash); |                 var fileCache = _fileDbManager.GetFileCacheByHash(item.Hash); | ||||||
|                 if (fileCache != null) |                 if (fileCache != null) | ||||||
|                 { |                 { | ||||||
|  |                     if (string.IsNullOrEmpty(new FileInfo(fileCache.ResolvedFilepath).Extension)) | ||||||
|  |                     { | ||||||
|  |                         hasMigrationChanges = true; | ||||||
|  |                         fileCache = _fileDbManager.MigrateFileHashToExtension(fileCache, item.GamePaths[0].Split(".").Last()); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|                     foreach (var gamePath in item.GamePaths) |                     foreach (var gamePath in item.GamePaths) | ||||||
|                     { |                     { | ||||||
|                         outputDict[gamePath] = fileCache.ResolvedFilepath; |                         outputDict[gamePath] = fileCache.ResolvedFilepath; | ||||||
| @@ -706,6 +713,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase | |||||||
|         { |         { | ||||||
|             PluginLog.Error(ex, "Something went wrong during calculation replacements"); |             PluginLog.Error(ex, "Something went wrong during calculation replacements"); | ||||||
|         } |         } | ||||||
|  |         if (hasMigrationChanges) _fileDbManager.WriteOutFullCsv(); | ||||||
|         st.Stop(); |         st.Stop(); | ||||||
|         Logger.LogDebug("ModdedPaths calculated in {time}ms, missing files: {count}, total files: {total}", st.ElapsedMilliseconds, missingFiles.Count, moddedDictionary.Keys.Count); |         Logger.LogDebug("ModdedPaths calculated in {time}ms, missing files: {count}, total files: {total}", st.ElapsedMilliseconds, missingFiles.Count, moddedDictionary.Keys.Count); | ||||||
|         return missingFiles; |         return missingFiles; | ||||||
|   | |||||||
| @@ -191,7 +191,8 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase | |||||||
|  |  | ||||||
|             foreach (var file in fileGroup) |             foreach (var file in fileGroup) | ||||||
|             { |             { | ||||||
|                 var tempPath = _fileDbManager.GetCacheFilePath(file.Hash, isTemporaryFile: true); |                 var ext = fileReplacement.First(f => string.Equals(f.Hash, file.Hash, StringComparison.OrdinalIgnoreCase)).GamePaths.First().Split(".").Last(); | ||||||
|  |                 var tempPath = _fileDbManager.GetCacheFilePath(file.Hash, ext, isTemporaryFile: true); | ||||||
|                 Progress<long> progress = new((bytesDownloaded) => |                 Progress<long> progress = new((bytesDownloaded) => | ||||||
|                 { |                 { | ||||||
|                     try |                     try | ||||||
| @@ -235,7 +236,7 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase | |||||||
|                 var tempFileData = await File.ReadAllBytesAsync(tempPath, token).ConfigureAwait(false); |                 var tempFileData = await File.ReadAllBytesAsync(tempPath, token).ConfigureAwait(false); | ||||||
|                 var extractedFile = LZ4Codec.Unwrap(tempFileData); |                 var extractedFile = LZ4Codec.Unwrap(tempFileData); | ||||||
|                 File.Delete(tempPath); |                 File.Delete(tempPath); | ||||||
|                 var filePath = _fileDbManager.GetCacheFilePath(file.Hash, isTemporaryFile: false); |                 var filePath = _fileDbManager.GetCacheFilePath(file.Hash, ext, isTemporaryFile: false); | ||||||
|                 await File.WriteAllBytesAsync(filePath, extractedFile, token).ConfigureAwait(false); |                 await File.WriteAllBytesAsync(filePath, extractedFile, token).ConfigureAwait(false); | ||||||
|                 var fi = new FileInfo(filePath); |                 var fi = new FileInfo(filePath); | ||||||
|                 Func<DateTime> RandomDayInThePast() |                 Func<DateTime> RandomDayInThePast() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 rootdarkarchon
					rootdarkarchon