Skip to content

Commit ca221d7

Browse files
committed
Add binary storage api functions
1 parent 2432753 commit ca221d7

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

Flow.Launcher.Core/Plugin/JsonRPCV2Models/JsonRPCPublicAPI.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,10 @@ public void StopLoadingBar()
185185
{
186186
_api.StopLoadingBar();
187187
}
188+
189+
public void SavePluginCaches()
190+
{
191+
_api.SavePluginCaches();
192+
}
188193
}
189194
}

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void Save()
6666
}
6767

6868
API.SavePluginSettings();
69+
API.SavePluginCaches();
6970
}
7071

7172
public static async ValueTask DisposePluginsAsync()
@@ -587,11 +588,13 @@ internal static async Task UninstallPluginAsync(PluginMetadata plugin, bool remo
587588

588589
if (removePluginSettings)
589590
{
590-
// For dotnet plugins, we need to remove their PluginJsonStorage instance
591+
// For dotnet plugins, we need to remove their PluginJsonStorage and PluginBinaryStorage instances
591592
if (AllowedLanguage.IsDotNet(plugin.Language))
592593
{
593594
var method = API.GetType().GetMethod("RemovePluginSettings");
594595
method?.Invoke(API, new object[] { plugin.AssemblyName });
596+
var method1 = API.GetType().GetMethod("RemovePluginCache");
597+
method1?.Invoke(API, new object[] { plugin.PluginCacheDirectoryPath });
595598
}
596599

597600
try

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,38 @@ public interface IPublicAPI
344344
/// Stop the loading bar in main window
345345
/// </summary>
346346
public void StopLoadingBar();
347+
348+
/// <summary>
349+
/// Save all Flow's plugins caches
350+
/// </summary>
351+
void SavePluginCaches();
352+
353+
/// <summary>
354+
/// Load BinaryStorage for current plugin's cache. This is the method used to load cache from binary in Flow.
355+
/// When the file is not exist, it will create a new instance for the specific type.
356+
/// </summary>
357+
/// <typeparam name="T">Type for deserialization</typeparam>
358+
/// <param name="cacheName">Cache file name</param>
359+
/// <param name="cacheDirectory">Cache directory from plugin metadata</param>
360+
/// <param name="defaultData">Default data to return</param>
361+
/// <returns></returns>
362+
/// <remarks>
363+
/// BinaryStorage utilize MemoryPack, which means the object must be MemoryPackSerializable <see href="https://github.com/Cysharp/MemoryPack"/>
364+
/// </remarks>
365+
Task<T> LoadCacheBinaryStorageAsync<T>(string cacheName, string cacheDirectory, T defaultData) where T : new();
366+
367+
/// <summary>
368+
/// Save BinaryStorage for current plugin's cache. This is the method used to save cache to binary in Flow.Launcher
369+
/// This method will save the original instance loaded with LoadCacheBinaryStorageAsync.
370+
/// This API call is for manually Save. Flow will automatically save all cache type that has called LoadCacheBinaryStorageAsync or SaveCacheBinaryStorageAsync previously.
371+
/// </summary>
372+
/// <typeparam name="T">Type for Serialization</typeparam>
373+
/// <param name="cacheName">Cache file name</param>
374+
/// <param name="cacheDirectory">Cache directory from plugin metadata</param>
375+
/// <returns></returns>
376+
/// <remarks>
377+
/// BinaryStorage utilize MemoryPack, which means the object must be MemoryPackSerializable <see href="https://github.com/Cysharp/MemoryPack"/>
378+
/// </remarks>
379+
Task SaveCacheBinaryStorageAsync<T>(string cacheName, string cacheDirectory) where T : new();
347380
}
348381
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
namespace Flow.Launcher.Plugin
1+
namespace Flow.Launcher.Plugin
22
{
33
/// <summary>
44
/// Inherit this interface if additional data e.g. cache needs to be saved.
55
/// </summary>
66
/// <remarks>
77
/// For storing plugin settings, prefer <see cref="IPublicAPI.LoadSettingJsonStorage{T}"/>
8-
/// or <see cref="IPublicAPI.SaveSettingJsonStorage{T}"/>.
8+
/// or <see cref="IPublicAPI.SaveSettingJsonStorage{T}"/>.
9+
/// or <see cref="IPublicAPI.SaveCacheBinaryStorageAsync{T}(string, string)"/>.
910
/// Once called, your settings will be automatically saved by Flow.
1011
/// </remarks>
1112
public interface ISavable : IFeatures
@@ -15,4 +16,4 @@ public interface ISavable : IFeatures
1516
/// </summary>
1617
void Save();
1718
}
18-
}
19+
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,6 @@ public void SavePluginSettings()
236236
((PluginJsonStorage<T>)_pluginJsonStorages[type]).Save();
237237
}
238238

239-
public void SaveJsonStorage<T>(T settings) where T : new()
240-
{
241-
var type = typeof(T);
242-
_pluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
243-
244-
((PluginJsonStorage<T>)_pluginJsonStorages[type]).Save();
245-
}
246-
247239
public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null)
248240
{
249241
using var explorer = new Process();
@@ -342,6 +334,51 @@ public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", M
342334

343335
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action cancelProgress = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, cancelProgress);
344336

337+
private readonly ConcurrentDictionary<(string, string, Type), object> _pluginBinaryStorages = new();
338+
339+
public void RemovePluginCache(string cacheDirectory)
340+
{
341+
foreach (var keyValuePair in _pluginBinaryStorages)
342+
{
343+
var key = keyValuePair.Key;
344+
var currentCacheDirectory = key.Item2;
345+
if (cacheDirectory == currentCacheDirectory)
346+
{
347+
_pluginBinaryStorages.Remove(key, out var _);
348+
}
349+
}
350+
}
351+
352+
/// <summary>
353+
/// Save plugin caches.
354+
/// </summary>
355+
public void SavePluginCaches()
356+
{
357+
foreach (var value in _pluginBinaryStorages.Values)
358+
{
359+
var method = value.GetType().GetMethod("Save");
360+
method?.Invoke(value, null);
361+
}
362+
}
363+
364+
public async Task<T> LoadCacheBinaryStorageAsync<T>(string cacheName, string cacheDirectory, T defaultData) where T : new()
365+
{
366+
var type = typeof(T);
367+
if (!_pluginBinaryStorages.ContainsKey((cacheName, cacheDirectory, type)))
368+
_pluginBinaryStorages[(cacheName, cacheDirectory, type)] = new PluginBinaryStorage<T>(cacheName, cacheDirectory);
369+
370+
return await ((PluginBinaryStorage<T>)_pluginBinaryStorages[(cacheName, cacheDirectory, type)]).TryLoadAsync(defaultData);
371+
}
372+
373+
public async Task SaveCacheBinaryStorageAsync<T>(string cacheName, string cacheDirectory) where T : new()
374+
{
375+
var type = typeof(T);
376+
if (!_pluginBinaryStorages.ContainsKey((cacheName, cacheDirectory, type)))
377+
_pluginBinaryStorages[(cacheName, cacheDirectory, type)] = new PluginBinaryStorage<T>(cacheName, cacheDirectory);
378+
379+
await ((PluginBinaryStorage<T>)_pluginBinaryStorages[(cacheName, cacheDirectory, type)]).SaveAsync();
380+
}
381+
345382
#endregion
346383

347384
#region Private Methods

0 commit comments

Comments
 (0)