2014-12-15 14:58:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2015-01-05 10:18:29 +00:00
|
|
|
|
using System.Reflection;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
using System.Runtime.Serialization;
|
2015-01-05 14:41:17 +00:00
|
|
|
|
using System.Runtime.Serialization.Formatters;
|
2014-12-15 14:58:49 +00:00
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
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;
|
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>
|
|
|
|
|
|
/// It utilize MemoryPack, which means the object must be MemoryPackSerializable
|
|
|
|
|
|
/// https://github.com/Cysharp/MemoryPack
|
|
|
|
|
|
/// </remarks>
|
2017-02-07 00:21:39 +00:00
|
|
|
|
public class BinaryStorage<T>
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2023-11-12 00:00:03 +00:00
|
|
|
|
const string DirectoryName = "Cache";
|
|
|
|
|
|
|
|
|
|
|
|
const string FileSuffix = ".cache";
|
|
|
|
|
|
|
2017-01-13 15:40:32 +00:00
|
|
|
|
public BinaryStorage(string filename)
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2023-11-12 00:00:03 +00:00
|
|
|
|
var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
|
2017-02-07 00:21:39 +00:00
|
|
|
|
Helper.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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 00:00:03 +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);
|
2017-01-13 15:40:32 +00:00
|
|
|
|
return t;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2016-08-20 00:02:20 +00:00
|
|
|
|
catch (System.Exception e)
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|