Skip to content

Commit 6d9126c

Browse files
committed
refactor: Cleanup
1 parent 7034663 commit 6d9126c

File tree

5 files changed

+76
-16
lines changed

5 files changed

+76
-16
lines changed

src/Moss.NET.Sdk/Core/InternalFunctions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33

44
namespace Moss.NET.Sdk.Core;
55

6-
public class InternalFunctions
6+
public static class InternalFunctions
77
{
88
[DllImport(Functions.DLL, EntryPoint = "moss_em_export_statistical_data")]
99
public static extern void ExportStatisticalData();
10+
11+
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_export")]
12+
private static extern void ExportDocument(ulong documentUuidPtr);
13+
14+
public static void ExportDocument(string documentUuid)
15+
{
16+
ExportDocument(documentUuid.GetPointer());
17+
}
1018
}

src/Moss.NET.Sdk/Core/RunOnce.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Moss.NET.Sdk.Core;
4+
5+
using System;
6+
using System.Collections.Generic;
7+
8+
public static class RunOnce
9+
{
10+
private static readonly Dictionary<string, bool> actionsRun = new Dictionary<string, bool>();
11+
private static readonly object _lockObj = new();
12+
13+
public static void Execute(string actionKey, Action action)
14+
{
15+
lock (_lockObj)
16+
{
17+
if (actionsRun.ContainsKey(actionKey) && actionsRun[actionKey])
18+
return;
19+
20+
action();
21+
actionsRun[actionKey] = true;
22+
}
23+
}
24+
25+
public static unsafe void Execute(Action action)
26+
{
27+
Execute(new IntPtr(Unsafe.AsPointer(ref action)).ToString(), action);
28+
}
29+
}

src/Moss.NET.Sdk/Storage.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Runtime.InteropServices;
22
using Extism;
33
using Moss.NET.Sdk.API;
4+
using Moss.NET.Sdk.Core;
45
using Moss.NET.Sdk.FFI;
56
using Moss.NET.Sdk.FFI.Dto;
67

@@ -10,7 +11,7 @@ namespace Moss.NET.Sdk;
1011
public static class Storage
1112
{
1213
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_metadata_get_all")]
13-
private static extern ulong GetDocumentMetadata(ulong uuidPtr);
14+
private static extern ulong GetApiDocumentMetadata(ulong uuidPtr);
1415

1516
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_duplicate")]
1617
private static extern ulong DuplicateDocument(ulong uuidPtr); // -> string
@@ -30,15 +31,21 @@ public static class Storage
3031
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_unload_files")]
3132
private static extern void UnloadFiles(ulong uuidPtr);
3233

34+
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_ensure_download")]
35+
private static extern void EnsureDownload(ulong uuidPtr);
36+
37+
[DllImport(Functions.DLL, EntryPoint = "moss_api_document_load_files_from_cache")]
38+
private static extern void LoadFilesFromCache(ulong uuidPtr);
39+
3340
/// <summary>
3441
/// Retrieves the metadata of a document given its UUID.
3542
/// </summary>
3643
/// <param name="uuid">The UUID of the document.</param>
3744
/// <returns>The metadata of the document.</returns>
38-
public static Metadata GetDocumentMetadata(string uuid)
45+
public static Metadata GetApiDocumentMetadata(string uuid)
3946
{
4047
var uuidPtr = Pdk.Allocate(uuid).Offset;
41-
var resultPtr = GetDocumentMetadata(uuidPtr);
48+
var resultPtr = GetApiDocumentMetadata(uuidPtr);
4249

4350
return Utils.Deserialize(resultPtr, JsonContext.Default.Metadata);
4451
}
@@ -187,4 +194,22 @@ public static void UnloadFiles(string uuid)
187194
var ptr = Pdk.Allocate(uuid).Offset;
188195
UnloadFiles(ptr);
189196
}
197+
198+
/// <summary>
199+
/// Ensures that the document is downloaded.
200+
/// </summary>
201+
/// <param name="uuid"></param>
202+
public static void EnsureDownload(string uuid)
203+
{
204+
EnsureDownload(uuid.GetPointer());
205+
}
206+
207+
/// <summary>
208+
/// This function loads all the files enforcing cache usage only. If the cache misses a file, it will not be downloaded.
209+
/// </summary>
210+
/// <param name="uuid"></param>
211+
public static void LoadFilesFromCache(string uuid)
212+
{
213+
LoadFilesFromCache(uuid.GetPointer());
214+
}
190215
}

src/Moss.NET.Sdk/UI/ScreenManager.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public static class ScreenManager
1717
private static extern void RegisterScreen(ulong keyPtr);
1818

1919
[DllImport(Functions.DLL, EntryPoint = "_moss_pe_open_screen")]
20-
private static extern ulong
21-
OpenScreen(ulong keyPtr,
22-
ulong initial_valuesPtr); // initial_valuesPtr = dict of values, could be any object that is non primitive
20+
private static extern ulong OpenScreen(ulong keyPtr, ulong initial_valuesPtr); // initial_valuesPtr = dict of values, could be any object that is non primitive
2321

2422
[DllImport(Functions.DLL, EntryPoint = "moss_pe_get_screen_value")]
2523
private static extern ulong GetScreenValue(ulong keyPtr); //-> ConfigGet
@@ -61,10 +59,9 @@ public static void Register<T>()
6159
var instance = Activator.CreateInstance<T>();
6260
var screen = new FFI.Dto.Screen(instance.Name, "ext_event_screen_loop", "ext_event_screen_preloop",
6361
"ext_event_screen_postloop");
64-
var screenPtr = Utils.Serialize(screen, JsonContext.Default.Screen);
65-
Screens.TryAdd(instance.Name, instance);
6662

67-
RegisterScreen(screenPtr);
63+
Screens.TryAdd(instance.Name, instance);
64+
RegisterScreen(screen.GetPointer());
6865
}
6966

7067
public static void Open<T>(Dictionary<string, object> values)

src/SamplePlugin/SampleExtension.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,25 @@ public static void Main()
2424

2525
public override ExtensionInfo Register(MossState state)
2626
{
27-
Pdk.Log(LogLevel.Info, "registered sample extension");
28-
2927
Config.Set("theme", "dark");
3028
Defaults.SetDefaultValue("OUTLINE_COLOR", Color.Blue);
3129
Theme.Apply(new DarkTheme());
3230

3331
Assets.Add("Assets/swap.svg");
3432

35-
var md = Storage.GetDocumentMetadata("0ba3df9c-8ca0-4347-8d7c-07471101baad");
33+
var md = Storage.GetApiDocumentMetadata("0ba3df9c-8ca0-4347-8d7c-07471101baad");
3634
Pdk.Log(LogLevel.Info, $"Metadata: {md.VisibleName} with {md.Hash}");
3735

3836
var nid = Storage.DuplicateDocument("0ba3df9c-8ca0-4347-8d7c-07471101baad");
3937

4038
//var testUuid = Storage.NewNotebook("test notebook");
4139
//Storage.NewPdf("test pdf", Array.Empty<byte>());
42-
// Storage.NewEpub("test ebook", Array.Empty<byte>());
43-
40+
//Storage.NewEpub("test ebook", Array.Empty<byte>());
41+
InternalFunctions.ExportDocument("0ba3df9c-8ca0-4347-8d7c-07471101baad");
4442
Storage.UnloadFiles("0ba3df9c-8ca0-4347-8d7c-07471101baad");
43+
Storage.EnsureDownload("0ba3df9c-8ca0-4347-8d7c-07471101baad");
44+
Storage.LoadFilesFromCache("0ba3df9c-8ca0-4347-8d7c-07471101baad");
45+
4546
Storage.RandomizeUUIDs("0ba3df9c-8ca0-4347-8d7c-07471101baad");
4647

4748
Moss.NET.Sdk.Moss.RegisterExtensionButton(
@@ -74,7 +75,7 @@ public override void ExtensionLoop(MossState state)
7475

7576
GUI.InvertIcon("swap", "swap_inverted");
7677

77-
InternalFunctions.ExportStatisticalData();
78+
RunOnce.Execute(InternalFunctions.ExportStatisticalData);
7879
}
7980

8081
public override void Unregister()

0 commit comments

Comments
 (0)