2025-04-11 10:40:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
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-05 03:19:01 +00:00
|
|
|
|
using Flow.Launcher.Plugin;
|
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
|
|
|
|
|
2025-04-01 06:16:32 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure.Storage
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-08-13 06:19:52 +00:00
|
|
|
|
/// Storage object using binary data
|
2014-12-15 14:58:49 +00:00
|
|
|
|
/// Normally, it has better performance, but not readable
|
|
|
|
|
|
/// </summary>
|
2023-11-12 00:00:03 +00:00
|
|
|
|
/// <remarks>
|
2025-04-08 11:42:59 +00:00
|
|
|
|
/// It utilizes MemoryPack, which means the object must be MemoryPackSerializable <see href="https://github.com/Cysharp/MemoryPack"/>
|
2023-11-12 00:00:03 +00:00
|
|
|
|
/// </remarks>
|
2025-04-05 03:19:01 +00:00
|
|
|
|
public class BinaryStorage<T> : ISavable
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
private static readonly string ClassName = "BinaryStorage";
|
|
|
|
|
|
|
2025-04-01 06:16:32 +00:00
|
|
|
|
protected T? Data;
|
|
|
|
|
|
|
2025-02-24 07:15:02 +00:00
|
|
|
|
public const string FileSuffix = ".cache";
|
2023-11-12 00:00:03 +00:00
|
|
|
|
|
2025-04-01 06:16:32 +00:00
|
|
|
|
protected string FilePath { get; init; } = null!;
|
|
|
|
|
|
|
|
|
|
|
|
protected string DirectoryPath { get; init; } = null!;
|
|
|
|
|
|
|
2025-02-24 07:15:02 +00:00
|
|
|
|
// Let the derived class to set the file path
|
2025-04-01 06:16:32 +00:00
|
|
|
|
protected BinaryStorage()
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-01 06:16:32 +00:00
|
|
|
|
public BinaryStorage(string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
DirectoryPath = DataLocation.CacheDirectory;
|
2025-04-04 08:41:08 +00:00
|
|
|
|
FilesFolders.ValidateDirectory(DirectoryPath);
|
2025-04-01 06:16:32 +00:00
|
|
|
|
|
|
|
|
|
|
FilePath = Path.Combine(DirectoryPath, $"{filename}{FileSuffix}");
|
|
|
|
|
|
}
|
2017-02-07 00:21:39 +00:00
|
|
|
|
|
2025-04-11 10:40:03 +00:00
|
|
|
|
// Let the old Program plugin get this constructor
|
|
|
|
|
|
[Obsolete("This constructor is obsolete. Use BinaryStorage(string filename) instead.")]
|
|
|
|
|
|
public BinaryStorage(string filename, string directoryPath = null!)
|
|
|
|
|
|
{
|
2025-04-11 14:13:58 +00:00
|
|
|
|
DirectoryPath = directoryPath ?? DataLocation.CacheDirectory;
|
|
|
|
|
|
FilesFolders.ValidateDirectory(DirectoryPath);
|
2025-04-11 10:40:03 +00:00
|
|
|
|
|
2025-04-11 14:13:58 +00:00
|
|
|
|
FilePath = Path.Combine(DirectoryPath, $"{filename}{FileSuffix}");
|
2025-04-11 10:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 05:58:02 +00:00
|
|
|
|
public T TryLoad(T defaultData)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Data != null) return Data;
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(FilePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (new FileInfo(FilePath).Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Error(ClassName, $"Zero length cache file <{FilePath}>");
|
|
|
|
|
|
Data = defaultData;
|
|
|
|
|
|
Save();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var bytes = File.ReadAllBytes(FilePath);
|
|
|
|
|
|
Data = Deserialize(bytes, defaultData);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Info(ClassName, "Cache file not exist, load default data");
|
|
|
|
|
|
Data = defaultData;
|
|
|
|
|
|
Save();
|
|
|
|
|
|
}
|
|
|
|
|
|
return Data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private T Deserialize(ReadOnlySpan<byte> bytes, T defaultData)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var t = MemoryPackSerializer.Deserialize<T>(bytes);
|
|
|
|
|
|
return t ?? defaultData;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Exception(ClassName, $"Deserialize error for file <{FilePath}>", e);
|
|
|
|
|
|
return defaultData;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
public async ValueTask<T> TryLoadAsync(T defaultData)
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2025-04-08 09:20:57 +00:00
|
|
|
|
if (Data != null) return Data;
|
2025-04-01 06:16:32 +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)
|
|
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Error(ClassName, $"Zero length cache file <{FilePath}>");
|
2025-04-01 06:16:32 +00:00
|
|
|
|
Data = defaultData;
|
|
|
|
|
|
await SaveAsync();
|
2017-12-22 15:59:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
await using var stream = new FileStream(FilePath, FileMode.Open);
|
2025-04-08 09:20:57 +00:00
|
|
|
|
Data = await DeserializeAsync(stream, defaultData);
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
2016-04-21 00:53:21 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Info(ClassName, "Cache file not exist, load default data");
|
2025-04-01 06:16:32 +00:00
|
|
|
|
Data = defaultData;
|
|
|
|
|
|
await SaveAsync();
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2025-04-01 06:16:32 +00:00
|
|
|
|
|
|
|
|
|
|
return Data;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 05:58:02 +00:00
|
|
|
|
private 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);
|
2025-04-01 06:16:32 +00:00
|
|
|
|
return t ?? defaultData;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2025-08-13 05:58:02 +00:00
|
|
|
|
catch (System.Exception e)
|
2016-04-21 00:53:21 +00:00
|
|
|
|
{
|
2025-08-13 05:58:02 +00:00
|
|
|
|
Log.Exception(ClassName, $"Deserialize error for file <{FilePath}>", e);
|
2017-01-13 15:40:32 +00:00
|
|
|
|
return defaultData;
|
2015-01-05 10:18:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-01 06:16:32 +00:00
|
|
|
|
public void Save()
|
2025-08-13 05:58:02 +00:00
|
|
|
|
{
|
|
|
|
|
|
Save(Data.NonNull());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Save(T data)
|
2025-04-01 06:16:32 +00:00
|
|
|
|
{
|
2025-04-11 08:48:02 +00:00
|
|
|
|
// User may delete the directory, so we need to check it
|
|
|
|
|
|
FilesFolders.ValidateDirectory(DirectoryPath);
|
|
|
|
|
|
|
2025-08-13 05:58:02 +00:00
|
|
|
|
var serialized = MemoryPackSerializer.Serialize(data);
|
2025-04-01 06:16:32 +00:00
|
|
|
|
File.WriteAllBytes(FilePath, serialized);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-05 03:11:14 +00:00
|
|
|
|
public async ValueTask SaveAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await SaveAsync(Data.NonNull());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +00:00
|
|
|
|
public async ValueTask SaveAsync(T data)
|
2015-01-05 10:18:29 +00:00
|
|
|
|
{
|
2025-04-11 08:48:02 +00:00
|
|
|
|
// User may delete the directory, so we need to check it
|
|
|
|
|
|
FilesFolders.ValidateDirectory(DirectoryPath);
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2025-08-13 05:58:02 +00:00
|
|
|
|
|
|
|
|
|
|
// ImageCache need to convert data into concurrent dictionary for usage,
|
|
|
|
|
|
// so we would better to clear the data
|
|
|
|
|
|
public void ClearData()
|
|
|
|
|
|
{
|
|
|
|
|
|
Data = default;
|
|
|
|
|
|
}
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|