diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage.cs b/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage.cs index 2382710abf..83a41cf040 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -14,6 +14,10 @@ namespace Wox.Infrastructure.Storage /// public class BinaryStorage { + // This storage helper returns whether or not to delete the binary storage items + private static readonly int BINARY_STORAGE = 0; + private StoragePowerToysVersionInfo _storageHelper; + public BinaryStorage(string filename) { const string directoryName = "Cache"; @@ -28,6 +32,17 @@ namespace Wox.Infrastructure.Storage public T TryLoad(T defaultData) { + _storageHelper = new StoragePowerToysVersionInfo(FilePath, BINARY_STORAGE); + // 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(File.Exists(FilePath)) + { + File.Delete(FilePath); + Log.Info($"|BinaryStorage.TryLoad|Deleting cached data| <{FilePath}>"); + } + } + if (File.Exists(FilePath)) { if (new FileInfo(FilePath).Length == 0) @@ -110,6 +125,8 @@ namespace Wox.Infrastructure.Storage Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", e); } } + _storageHelper.Close(); + Log.Info($"|BinaryStorage.Save|Saving cached data| <{FilePath}>"); } } } diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage.cs b/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage.cs index 305df48883..af973171fe 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage.cs @@ -19,6 +19,9 @@ namespace Wox.Infrastructure.Storage public string FilePath { get; set; } public string DirectoryPath { get; set; } + // This storage helper returns whether or not to delete the json storage items + private static readonly int JSON_STORAGE = 1; + private StoragePowerToysVersionInfo _storageHelper; internal JsonStorage() { @@ -33,6 +36,17 @@ namespace Wox.Infrastructure.Storage public T Load() { + _storageHelper = new StoragePowerToysVersionInfo(FilePath, JSON_STORAGE); + // 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(File.Exists(FilePath)) + { + File.Delete(FilePath); + Log.Info($"|JsonStorage.TryLoad|Deleting cached data|<{FilePath}>"); + } + } + if (File.Exists(FilePath)) { var serialized = File.ReadAllText(FilePath); @@ -96,6 +110,8 @@ namespace Wox.Infrastructure.Storage { string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented); File.WriteAllText(FilePath, serialized); + _storageHelper.Close(); + Log.Info($"|JsonStorage.Save|Saving cached data| <{FilePath}>"); } } } diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs b/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs new file mode 100644 index 0000000000..552c1d343d --- /dev/null +++ b/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.Windows.Markup; + +namespace Wox.Infrastructure.Storage +{ + public class StoragePowerToysVersionInfo + { + // This detail is accessed by the storage items and is used to decide if the cache must be deleted or not + public bool clearCache = false; + + + private String currentPowerToysVersion = String.Empty; + private String FilePath { get; set; } = String.Empty; + + // As of now this information is not pertinent but may be in the future + // There may be cases when we want to delete only the .cache files and not the .json storage files + private enum StorageType + { + BINARY_STORAGE = 0, + JSON_STORAGE = 1 + } + + // To compare the version numbers + public static bool Lessthan(string version1, string version2) + { + string version = "v"; + string period = "."; + const int versionLength = 3; + + // If there is some error in populating/retrieving the version numbers, then the cache must be deleted + // This case will not be hit, but is present as a fail safe + if(String.IsNullOrEmpty(version1) || String.IsNullOrEmpty(version2)) + { + return true; + } + + string[] split1 = version1.Split( new string[] { version, period }, StringSplitOptions.RemoveEmptyEntries); + string[] split2 = version2.Split( new string[] { version, period }, StringSplitOptions.RemoveEmptyEntries); + + for(int i=0; i