2025-02-24 07:15:02 +00:00
|
|
|
|
using System.IO;
|
2023-11-12 00:00:03 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
2025-04-02 12:17:32 +00:00
|
|
|
|
using Flow.Launcher.Plugin.SharedCommands;
|
2023-11-12 00:00:03 +00:00
|
|
|
|
using MemoryPack;
|
2014-12-15 14:58:49 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure.Storage
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Stroage object using binary data
|
|
|
|
|
|
/// Normally, it has better performance, but not readable
|
|
|
|
|
|
/// </summary>
|
2023-11-12 00:00:03 +00:00
|
|
|
|
/// <remarks>
|
2025-02-24 07:15:02 +00:00
|
|
|
|
/// It utilize MemoryPack, which means the object must be MemoryPackSerializable <see href="https://github.com/Cysharp/MemoryPack"/>
|
2023-11-12 00:00:03 +00:00
|
|
|
|
/// </remarks>
|
2017-02-07 00:21:39 +00:00
|
|
|
|
public class BinaryStorage<T>
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2025-02-24 07:15:02 +00:00
|
|
|
|
public const string FileSuffix = ".cache";
|
2023-11-12 00:00:03 +00:00
|
|
|
|
|
2025-02-24 07:15:02 +00:00
|
|
|
|
// Let the derived class to set the file path
|
|
|
|
|
|
public BinaryStorage(string filename, string directoryPath = null)
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2025-02-24 07:15:02 +00:00
|
|
|
|
directoryPath ??= DataLocation.CacheDirectory;
|
2025-04-02 12:17:32 +00:00
|
|
|
|
FilesFolders.ValidateDirectory(directoryPath);
|
2016-04-27 01:15:53 +00:00
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-07 00:21:39 +00:00
|
|
|
|
public string FilePath { get; }
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
public async ValueTask<T> TryLoadAsync(T defaultData)
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
if (File.Exists(FilePath))
|
|
|
|
|
|
{
|
2017-12-22 15:59:55 +00:00
|
|
|
|
if (new FileInfo(FilePath).Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
|
2023-11-12 00:00:03 +00:00
|
|
|
|
await SaveAsync(defaultData);
|
2017-12-22 15:59:55 +00:00
|
|
|
|
return defaultData;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
await using var stream = new FileStream(FilePath, FileMode.Open);
|
|
|
|
|
|
var d = await DeserializeAsync(stream, defaultData);
|
|
|
|
|
|
return d;
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
2016-04-21 00:53:21 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
|
2023-11-12 00:00:03 +00:00
|
|
|
|
await SaveAsync(defaultData);
|
2017-01-13 15:40:32 +00:00
|
|
|
|
return defaultData;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 07:15:02 +00:00
|
|
|
|
private static async ValueTask<T> DeserializeAsync(Stream stream, T defaultData)
|
2016-04-21 00:53:21 +00:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-11-12 00:00:03 +00:00
|
|
|
|
var t = await MemoryPackSerializer.DeserializeAsync<T>(stream);
|
2017-01-13 15:40:32 +00:00
|
|
|
|
return t;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2025-02-24 07:15:02 +00:00
|
|
|
|
catch (System.Exception)
|
2016-04-21 00:53:21 +00:00
|
|
|
|
{
|
2024-05-30 19:21:07 +00:00
|
|
|
|
// Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e);
|
2017-01-13 15:40:32 +00:00
|
|
|
|
return defaultData;
|
2015-01-05 10:18:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
public async ValueTask SaveAsync(T data)
|
2015-01-05 10:18:29 +00:00
|
|
|
|
{
|
2023-11-12 00:00:03 +00:00
|
|
|
|
await using var stream = new FileStream(FilePath, FileMode.Create);
|
|
|
|
|
|
await MemoryPackSerializer.SerializeAsync(stream, data);
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|