From f4dbdbdd7a447d828ff8815d6f5eafecdb4f13d7 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Fri, 1 Jul 2022 15:44:24 +0200 Subject: [PATCH] [PTRun]Remove BinaryFormatter (#19036) --- .../Storage/Win32ProgramRepositoryTest.cs | 26 +-- .../Plugins/Microsoft.Plugin.Program/Main.cs | 6 +- .../Storage/IProgramRepository.cs | 4 - .../Storage/PackageRepository.cs | 17 +- .../Storage/Win32ProgramRepository.cs | 16 +- .../Wox.Infrastructure/Image/ImageCache.cs | 14 +- .../Wox.Infrastructure/Image/ImageLoader.cs | 9 +- .../Storage/BinaryStorage`1.cs | 161 ------------------ .../Wox.Infrastructure/Storage/IStorage`1.cs | 22 --- .../Storage/WoxJsonStorage`1.cs | 8 +- 10 files changed, 36 insertions(+), 247 deletions(-) delete mode 100644 src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs delete mode 100644 src/modules/launcher/Wox.Infrastructure/Storage/IStorage`1.cs diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Storage/Win32ProgramRepositoryTest.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Storage/Win32ProgramRepositoryTest.cs index b6c29050c6..5ae07c55ad 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Storage/Win32ProgramRepositoryTest.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Storage/Win32ProgramRepositoryTest.cs @@ -44,7 +44,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32RepositoryMustNotStoreDuplicatesWhileAddingItemsWithSameHashCode(string name, string exename, string fullPath, string description1, string description2) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); Win32Program item1 = new Win32Program { @@ -79,7 +79,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppCreatedForApprefAppsWhenCreatedEventIsRaised(string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Created, "directory", path); // Act @@ -95,7 +95,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppDeletedForApprefAppsWhenDeletedEventIsRaised(string directory, string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Deleted, directory, path); string fullPath = directory + "\\" + path; @@ -114,7 +114,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppRenamedForApprefAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); RenamedEventArgs e = new RenamedEventArgs(WatcherChangeTypes.Renamed, directory, newpath, oldpath); string oldFullPath = directory + "\\" + oldpath; @@ -138,7 +138,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppCreatedForExeAppsWhenCreatedEventIsRaised(string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Created, "directory", path); // FileVersionInfo must be mocked for exe applications @@ -159,7 +159,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppDeletedForExeAppsWhenDeletedEventIsRaised(string directory, string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Deleted, directory, path); // FileVersionInfo must be mocked for exe applications @@ -183,7 +183,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppRenamedForExeAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); RenamedEventArgs e = new RenamedEventArgs(WatcherChangeTypes.Renamed, directory, newpath, oldpath); string oldFullPath = directory + "\\" + oldpath; @@ -214,7 +214,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage // We are handing internet shortcut apps using the Changed event instead // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Created, "directory", path); // File.ReadAllLines must be mocked for url applications @@ -238,7 +238,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage // We are handing internet shortcut apps using the Changed event instead // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Changed, "directory", path); // FileVersionInfo must be mocked for exe applications @@ -263,7 +263,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppDeletedForUrlAppsWhenDeletedEventIsRaised(string directory, string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Deleted, directory, path); // File.ReadAllLines must be mocked for url applications @@ -287,7 +287,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppRenamedForUrlAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); RenamedEventArgs e = new RenamedEventArgs(WatcherChangeTypes.Renamed, directory, newpath, oldpath); // File.ReadAllLines must be mocked for url applications @@ -316,7 +316,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppDeletedForLnkAppsWhenDeletedEventIsRaised(string directory, string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Deleted, directory, path); // ShellLinkHelper must be mocked for lnk applications @@ -347,7 +347,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage public void Win32ProgramRepositoryMustCallOnAppRenamedForLnkAppsWhenRenamedEventIsRaised(string directory, string oldpath, string path) { // Arrange - Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage>("Win32"), _settings, _pathsToWatch); + Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch); RenamedEventArgs e = new RenamedEventArgs(WatcherChangeTypes.Renamed, directory, path, oldpath); string oldFullPath = directory + "\\" + oldpath; diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs index f86b763e99..bff1e1ec2a 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; @@ -14,7 +13,6 @@ using Microsoft.Plugin.Program.Programs; using Microsoft.Plugin.Program.Storage; using Wox.Infrastructure.Storage; using Wox.Plugin; -using Wox.Plugin.Logger; using Stopwatch = Wox.Infrastructure.Stopwatch; namespace Microsoft.Plugin.Program @@ -39,7 +37,7 @@ namespace Microsoft.Plugin.Program private static PluginInitContext _context; private readonly PluginJsonStorage _settingsStorage; private bool _disposed; - private PackageRepository _packageRepository = new PackageRepository(new PackageCatalogWrapper(), new BinaryStorage>("UWP")); + private PackageRepository _packageRepository = new PackageRepository(new PackageCatalogWrapper()); private static Win32ProgramFileSystemWatchers _win32ProgramRepositoryHelper; private static Win32ProgramRepository _win32ProgramRepository; @@ -52,7 +50,7 @@ namespace Microsoft.Plugin.Program _win32ProgramRepositoryHelper = new Win32ProgramFileSystemWatchers(); // Initialize the Win32ProgramRepository with the settings object - _win32ProgramRepository = new Win32ProgramRepository(_win32ProgramRepositoryHelper.FileSystemWatchers.Cast().ToList(), new BinaryStorage>("Win32"), Settings, _win32ProgramRepositoryHelper.PathsToWatch); + _win32ProgramRepository = new Win32ProgramRepository(_win32ProgramRepositoryHelper.FileSystemWatchers.Cast().ToList(), Settings, _win32ProgramRepositoryHelper.PathsToWatch); } public void Save() diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/IProgramRepository.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/IProgramRepository.cs index 5d65b03b66..afdeb27264 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/IProgramRepository.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/IProgramRepository.cs @@ -7,9 +7,5 @@ namespace Microsoft.Plugin.Program.Storage internal interface IProgramRepository { void IndexPrograms(); - - void Load(); - - void Save(); } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/PackageRepository.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/PackageRepository.cs index 1446987df1..07d393051f 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/PackageRepository.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/PackageRepository.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Linq; using Microsoft.Plugin.Program.Logger; using Microsoft.Plugin.Program.Programs; @@ -19,13 +18,10 @@ namespace Microsoft.Plugin.Program.Storage /// internal class PackageRepository : ListRepository, IProgramRepository { - private IStorage> _storage; - private IPackageCatalog _packageCatalog; - public PackageRepository(IPackageCatalog packageCatalog, IStorage> storage) + public PackageRepository(IPackageCatalog packageCatalog) { - _storage = storage ?? throw new ArgumentNullException(nameof(storage), "StorageRepository requires an initialized storage interface"); _packageCatalog = packageCatalog ?? throw new ArgumentNullException(nameof(packageCatalog), "PackageRepository expects an interface to be able to subscribe to package events"); _packageCatalog.PackageInstalling += OnPackageInstalling; _packageCatalog.PackageUninstalling += OnPackageUninstalling; @@ -84,16 +80,5 @@ namespace Microsoft.Plugin.Program.Storage Log.Info($"Indexed {applications.Length} packaged applications", GetType()); SetList(applications); } - - public void Save() - { - _storage.Save(Items); - } - - public void Load() - { - var items = _storage.TryLoad(Array.Empty()); - SetList(items); - } } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/Win32ProgramRepository.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/Win32ProgramRepository.cs index 2982b90d2e..d0360db3c3 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/Win32ProgramRepository.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/Win32ProgramRepository.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Globalization; using System.IO; using System.IO.Abstractions; using System.Threading.Tasks; @@ -24,7 +23,6 @@ namespace Microsoft.Plugin.Program.Storage private const string LnkExtension = ".lnk"; private const string UrlExtension = ".url"; - private IStorage> _storage; private ProgramPluginSettings _settings; private IList _fileSystemWatcherHelpers; private string[] _pathsToWatch; @@ -33,10 +31,9 @@ namespace Microsoft.Plugin.Program.Storage private static ConcurrentQueue commonEventHandlingQueue = new ConcurrentQueue(); - public Win32ProgramRepository(IList fileSystemWatcherHelpers, IStorage> storage, ProgramPluginSettings settings, string[] pathsToWatch) + public Win32ProgramRepository(IList fileSystemWatcherHelpers, ProgramPluginSettings settings, string[] pathsToWatch) { _fileSystemWatcherHelpers = fileSystemWatcherHelpers; - _storage = storage ?? throw new ArgumentNullException(nameof(storage), "Win32ProgramRepository requires an initialized storage interface"); _settings = settings ?? throw new ArgumentNullException(nameof(settings), "Win32ProgramRepository requires an initialized settings object"); _pathsToWatch = pathsToWatch; _numberOfPathsToWatch = pathsToWatch.Length; @@ -246,16 +243,5 @@ namespace Microsoft.Plugin.Program.Storage Log.Info($"Indexed {applications.Count} win32 applications", GetType()); SetList(applications); } - - public void Save() - { - _storage.Save(Items); - } - - public void Load() - { - var items = _storage.TryLoad(Array.Empty()); - SetList(items); - } } } diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs index 69d064207b..4daf97959f 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -7,6 +7,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Windows.Media; +using Wox.Infrastructure.Storage; using Wox.Plugin; namespace Wox.Infrastructure.Image @@ -18,9 +19,16 @@ namespace Wox.Infrastructure.Image private const int PermissibleFactor = 2; private readonly ConcurrentDictionary _data = new ConcurrentDictionary(); + private readonly WoxJsonStorage> _storage; public ConcurrentDictionary Usage { get; private set; } = new ConcurrentDictionary(); + public ImageCache() + { + _storage = new WoxJsonStorage>("ImageCache"); + Usage = _storage.Load(); + } + public ImageSource this[string path] { get @@ -87,9 +95,9 @@ namespace Wox.Infrastructure.Image return new Dictionary(Usage); } - public void SetUsageAsDictionary(Dictionary usage) + public void Save() { - Usage = new ConcurrentDictionary(usage); + _storage.Save(); } } } diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs index 5b8e3b6fc1..515d31081b 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs @@ -1,10 +1,9 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Globalization; using System.IO.Abstractions; using System.Linq; @@ -13,7 +12,6 @@ using System.Threading.Tasks; using System.Windows.Media; using System.Windows.Media.Imaging; using ManagedCommon; -using Wox.Infrastructure.Storage; using Wox.Plugin; using Wox.Plugin.Logger; @@ -29,7 +27,6 @@ namespace Wox.Infrastructure.Image private static readonly ImageCache ImageCache = new ImageCache(); private static readonly ConcurrentDictionary GuidToKey = new ConcurrentDictionary(); - private static BinaryStorage> _storage; private static IImageHashGenerator _hashGenerator; public static string ErrorIconPath { get; set; } @@ -49,9 +46,7 @@ namespace Wox.Infrastructure.Image public static void Initialize(Theme theme) { - _storage = new BinaryStorage>("Image"); _hashGenerator = new ImageHashGenerator(); - ImageCache.SetUsageAsDictionary(_storage.TryLoad(new Dictionary())); foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon, Constant.LightThemedDefaultIcon, Constant.LightThemedErrorIcon }) { @@ -78,7 +73,7 @@ namespace Wox.Infrastructure.Image public static void Save() { ImageCache.Cleanup(); - _storage.Save(ImageCache.GetUsageAsDictionary()); + ImageCache.Save(); } // Todo : Update it with icons specific to each theme. diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs b/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs deleted file mode 100644 index baf65079c7..0000000000 --- a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs +++ /dev/null @@ -1,161 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.IO; -using System.IO.Abstractions; -using System.Reflection; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters; -using System.Runtime.Serialization.Formatters.Binary; -using Wox.Plugin; -using Wox.Plugin.Logger; - -namespace Wox.Infrastructure.Storage -{ - /// - /// Storage object using binary data - /// Normally, it has better performance, but not readable - /// - public class BinaryStorage : IStorage - { - private readonly IFileSystem _fileSystem; - - // This storage helper returns whether or not to delete the binary storage items - private const int _binaryStorage = 0; - private StoragePowerToysVersionInfo _storageHelper; - - public BinaryStorage(string filename) - : this(filename, new FileSystem()) - { - } - - public BinaryStorage(string filename, IFileSystem fileSystem) - { - _fileSystem = fileSystem; - - const string directoryName = "Cache"; - var path = _fileSystem.Path; - var directoryPath = path.Combine(Constant.DataDirectory, directoryName); - Helper.ValidateDirectory(directoryPath); - - const string fileSuffix = ".cache"; - FilePath = path.Combine(directoryPath, $"{filename}{fileSuffix}"); - } - - public string FilePath { get; } - - public T TryLoad(T defaultData) - { - _storageHelper = new StoragePowerToysVersionInfo(FilePath, _binaryStorage); - - // Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible - if (_storageHelper.ClearCache) - { - if (_fileSystem.File.Exists(FilePath)) - { - _fileSystem.File.Delete(FilePath); - - Log.Info($"Deleting cached data at <{FilePath}>", GetType()); - } - } - - if (_fileSystem.File.Exists(FilePath)) - { - if (_fileSystem.FileInfo.FromFileName(FilePath).Length == 0) - { - Log.Error($"Zero length cache file <{FilePath}>", GetType()); - - Save(defaultData); - return defaultData; - } - - using (var stream = _fileSystem.FileStream.Create(FilePath, FileMode.Open)) - { - var d = Deserialize(stream, defaultData); - return d; - } - } - else - { - Log.Info("Cache file not exist, load default data", GetType()); - - Save(defaultData); - return defaultData; - } - } - - private T Deserialize(Stream stream, T defaultData) - { - // http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception - AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - BinaryFormatter binaryFormatter = new BinaryFormatter - { - AssemblyFormat = FormatterAssemblyStyle.Simple, - }; - - try - { -#pragma warning disable SYSLIB0011 // Type or member is obsolete - // See https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide to fix this - var t = ((T)binaryFormatter.Deserialize(stream)).NonNull(); -#pragma warning restore SYSLIB0011 // Type or member is obsolete - return t; - } - catch (System.Exception e) - { - Log.Exception($"Deserialize error for file <{FilePath}>", e, GetType()); - - return defaultData; - } - finally - { - AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; - } - } - - private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - Assembly ayResult = null; - string sShortAssemblyName = args.Name.Split(',')[0]; - Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies(); - foreach (Assembly ayAssembly in ayAssemblies) - { - if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) - { - ayResult = ayAssembly; - break; - } - } - - return ayResult; - } - - public void Save(T data) - { - using (var stream = new FileStream(FilePath, FileMode.Create)) - { - BinaryFormatter binaryFormatter = new BinaryFormatter - { - AssemblyFormat = FormatterAssemblyStyle.Simple, - }; - - try - { -#pragma warning disable SYSLIB0011 // Type or member is obsolete - // See https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide to fix this - binaryFormatter.Serialize(stream, data); -#pragma warning restore SYSLIB0011 // Type or member is obsolete - } - catch (SerializationException e) - { - Log.Exception($"Serialize error for file <{FilePath}>", e, GetType()); - } - } - - _storageHelper.Close(); - Log.Info($"Saving cached data at <{FilePath}>", GetType()); - } - } -} diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/IStorage`1.cs b/src/modules/launcher/Wox.Infrastructure/Storage/IStorage`1.cs deleted file mode 100644 index ec73368a87..0000000000 --- a/src/modules/launcher/Wox.Infrastructure/Storage/IStorage`1.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace Wox.Infrastructure.Storage -{ - public interface IStorage - { - /// - /// Saves the data - /// - /// data to be saved - void Save(T data); - - /// - /// Attempts to load data, otherwise it will return the default provided - /// - /// default data value - /// The loaded data or default - T TryLoad(T defaultData); - } -} diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/WoxJsonStorage`1.cs b/src/modules/launcher/Wox.Infrastructure/Storage/WoxJsonStorage`1.cs index 8a62c78015..91c4fb6d00 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/WoxJsonStorage`1.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/WoxJsonStorage`1.cs @@ -14,12 +14,16 @@ namespace Wox.Infrastructure.Storage private static readonly IPath Path = FileSystem.Path; public WoxJsonStorage() + : this(typeof(T).Name) + { + } + + public WoxJsonStorage(string fileName) { var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName); Helper.ValidateDirectory(directoryPath); - var filename = typeof(T).Name; - FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}"); + FilePath = Path.Combine(directoryPath, $"{fileName}{FileSuffix}"); } } }