Skip to content

Commit d096d84

Browse files
committed
also uh CancellationTokens nice
1 parent 5dd6b44 commit d096d84

File tree

4 files changed

+136
-43
lines changed

4 files changed

+136
-43
lines changed

WriterSharp.PluginApi/FileSystem/IFileSystem.cs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
35

46

57
namespace WriterSharp.PluginApi.FileSystem
@@ -12,28 +14,26 @@ namespace WriterSharp.PluginApi.FileSystem
1214
public interface IFileSystem
1315
{
1416

15-
#region Reading
16-
1717
/// <summary>
1818
/// Reads all text from a file.
1919
/// </summary>
2020
/// <param name="filepath">The path to the file</param>
2121
/// <returns>The contents of the file</returns>
22-
public string ReadAllText(string filepath);
22+
public Task<string> ReadAllTextAsync(string filepath, CancellationToken cancellationToken = default);
2323

2424
/// <summary>
2525
/// Reads all text from a file, as a list of lines.
2626
/// </summary>
2727
/// <param name="filepath">The path to the file</param>
2828
/// <returns>The contents of the file</returns>
29-
public string[] ReadAllLines(string filepath);
29+
public Task<string[]> ReadAllLinesAsync(string filepath, CancellationToken cancellationToken = default);
3030

3131
/// <summary>
3232
/// Reads the very first line of a file.
3333
/// </summary>
3434
/// <param name="filepath">The path to the file</param>
3535
/// <returns>The first line of the file</returns>
36-
public string ReadLine(string filepath);
36+
public Task<string> ReadLineAsync(string filepath, CancellationToken cancellationToken = default);
3737

3838
/// <summary>
3939
/// Reads a specific amount of characters from a file buffer.
@@ -42,49 +42,37 @@ public interface IFileSystem
4242
/// <param name="amount">The amount of characters to read</param>
4343
/// <param name="offset">The index from which to start reading the characters</param>
4444
/// <returns>A span of characters</returns>
45-
public Span<char> ReadCharacters(string filepath, ulong amount, long offset = 0);
46-
47-
#endregion
48-
49-
#region Writing
45+
public Task<nint> ReadCharactersAsync(string filepath, ulong amount, long offset = 0, CancellationToken cancellationToken = default);
5046

5147
/// <summary>
5248
/// Writes text to a file, creating it if necessary. If the file exists,
5349
/// it will be overwritten.
5450
/// </summary>
5551
/// <param name="filepath">The path to the file</param>
5652
/// <param name="data">The text to write</param>
57-
public void WriteAllText(string filepath, string data);
53+
public Task WriteAllTextAsync(string filepath, string data, CancellationToken cancellationToken = default);
5854

5955
/// <summary>
6056
/// Writes lines of text to a file, creating it if necessary.
6157
/// If the file exists, it will be overwritten.
6258
/// </summary>
6359
/// <param name="filepath">The path to the file</param>
6460
/// <param name="data">The lines of text to write</param>
65-
public void WriteAllLines(string filepath, string[] data);
66-
67-
#endregion
68-
69-
#region Appending
61+
public Task WriteAllLinesAsync(string filepath, string[] data, CancellationToken cancellationToken = default);
7062

7163
/// <summary>
7264
/// Appends all the text to the end of a file.
7365
/// </summary>
7466
/// <param name="filepath">The path to the file</param>
7567
/// <param name="data">The text to append</param>
76-
public void AppendAllText(string filepath, string data);
68+
public Task AppendAllTextAsync(string filepath, string data, CancellationToken cancellationToken = default);
7769

7870
/// <summary>
7971
/// Appends all the specified lines of text to the end of a file.
8072
/// </summary>
8173
/// <param name="filepath">The path to the file</param>
8274
/// <param name="data">The lines to append</param>
83-
public void AppendAllLines(string filepath, string[] data);
84-
85-
#endregion
86-
87-
#region Sharding Toolkit
75+
public Task AppendAllLinesAsync(string filepath, string[] data, CancellationToken cancellationToken = default);
8876

8977
/// <summary>
9078
/// Checks if a file is in use by another plugin.
@@ -95,21 +83,17 @@ public interface IFileSystem
9583
/// <returns><c>true</c> if in use by another plugin</returns>
9684
public bool InUse(string filepath);
9785

98-
#endregion
99-
100-
#region Locking
101-
10286
/// <summary>
10387
/// Locks a file, to prevent it from being accessed by other plugins.
10488
/// </summary>
10589
/// <param name="filepath">The path to the file to lock</param>
106-
public void Lock(string filepath);
90+
public Task LockAsync(string filepath, CancellationToken cancellationToken = default);
10791

10892
/// <summary>
10993
/// Unlocks a previously locked file.
11094
/// </summary>
11195
/// <param name="filepath">The path to the file</param>
112-
public void Unlock(string filepath);
96+
public Task UnlockAsync(string filepath, CancellationToken cancellationToken = default);
11397

11498
/// <summary>
11599
/// Checks if a file is locked.
@@ -134,23 +118,23 @@ public interface IFileSystem
134118
/// <returns><c>true</c> if it can be locked</returns>
135119
public bool IsLockable(string filepath);
136120

137-
#endregion
138-
139-
#region Stream
140-
141-
/// <inheritdoc cref="File.OpenRead(String)" />
142-
public FileStream OpenRead(string filepath);
143-
144-
/// <inheritdoc cref="File.OpenWrite(String)" />
145-
public FileStream OpenWrite(string filepath);
121+
/// <inheritdoc cref="System.IO.File.OpenRead(String)" />
122+
public Task<FileStream> OpenReadAsync(string filepath, CancellationToken cancellationToken = default);
146123

147-
/// <inheritdoc cref="File.OpenText(String)" />
148-
public FileStream OpenText(string filepath);
124+
/// <inheritdoc cref="System.IO.File.OpenWrite(String)" />
125+
public Task<FileStream> OpenWriteAsync(string filepath, CancellationToken cancellationToken = default);
149126

150-
/// <inheritdoc cref="File.Open(String, FileMode, FileAccess, FileShare)" />
151-
public FileStream Open(string filepath, FileMode mode, FileAccess access, FileShare share);
127+
/// <inheritdoc cref="System.IO.File.OpenText(String)" />
128+
public Task<FileStream> OpenTextAsync(string filepath, CancellationToken cancellationToken = default);
152129

153-
#endregion
130+
/// <inheritdoc cref="System.IO.File.Open(String, FileMode, FileAccess, FileShare)" />
131+
public Task<FileStream> OpenAsync(
132+
string filepath,
133+
FileMode mode,
134+
FileAccess access,
135+
FileShare share,
136+
CancellationToken cancellationToken = default
137+
);
154138

155139
}
156140

WriterSharp.PluginApi/IPluginContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace WriterSharp.PluginApi
1+
using WriterSharp.PluginApi.FileSystem;
2+
using WriterSharp.PluginApi.Settings;
3+
4+
5+
namespace WriterSharp.PluginApi
26
{
37

48
/// <summary>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
6+
namespace WriterSharp.PluginApi.Settings
7+
{
8+
9+
/// <summary>
10+
/// Manages WriterSharp's plugin's settings globally.
11+
/// This does not mean plugins can't have their own individual
12+
/// settings files, but it's recommended to use this structure of plugin design.
13+
/// </summary>
14+
public interface ISettingsManager
15+
{
16+
17+
/// <summary>
18+
/// Checks if the settings file is in use by another plugin.
19+
/// </summary>
20+
/// <returns><c>true</c> if in use</returns>
21+
public bool InUse();
22+
23+
/// <summary>
24+
/// Gets the plugin's section.
25+
/// </summary>
26+
/// <param name="cancellationToken">A cancellation token to stop WriterSharp from getting the section</param>
27+
/// <returns>The section, as a dictionary</returns>
28+
/// <remarks>Useful if you're going to be editing the settings a lot.</remarks>
29+
public Task<Dictionary<string, string>> GetSectionAsync(CancellationToken cancellationToken = default);
30+
31+
/// <summary>
32+
/// Update's the plugin's section.
33+
/// </summary>
34+
/// <param name="section">The new version of the section</param>
35+
/// <param name="cancellationToken">A cancellation token</param>
36+
public void UpdateSectionAsync(Dictionary<string, string> section, CancellationToken cancellationToken = default);
37+
38+
/// <summary>
39+
/// Retrieves a value from the plugin's section given a key. If the key is missing, an exception is thrown.
40+
/// </summary>
41+
/// <param name="key">The key that matches the value</param>
42+
/// <param name="cancellationToken">A cancellation token</param>
43+
/// <returns>A string or <c>null</c> if the value was <c>nil</c>.</returns>
44+
public Task<string?> GetValueAsync(string key, CancellationToken cancellationToken = default);
45+
46+
/// <summary>
47+
/// Sets an existing key to match a value or adds a new one if necessary.
48+
/// </summary>
49+
/// <param name="key">The key's name</param>
50+
/// <param name="value">The value</param>
51+
/// <param name="cancellationToken">A cancellation token</param>
52+
public Task SetValueAsync(string key, string value, CancellationToken cancellationToken = default);
53+
54+
/// <summary>
55+
/// Sets an existing key to match a value or adds a new one if necessary.
56+
/// </summary>
57+
/// <param name="key">The key's name</param>
58+
/// <param name="value">The value. It is converted to string</param>
59+
/// <param name="cancellationToken">A cancellation token</param>
60+
public Task SetValueAsync(string key, int value, CancellationToken cancellationToken = default);
61+
62+
/// <summary>
63+
/// Sets an existing key to match a value or adds a new one if necessary.
64+
/// </summary>
65+
/// <param name="key">The key's name</param>
66+
/// <param name="value">The value. It is converted to string</param>
67+
/// <param name="cancellationToken">A cancellation token</param>
68+
public Task SetValueAsync(string key, bool value, CancellationToken cancellationToken = default);
69+
70+
/// <summary>
71+
/// Sets an existing key to match a value or adds a new one if necessary.
72+
/// </summary>
73+
/// <param name="key">The key's name</param>
74+
/// <param name="value">The value</param>
75+
/// <param name="cancellationToken">A cancellation token</param>
76+
public Task SetValueAsync(string key, object? value, CancellationToken cancellationToken = default);
77+
78+
/// <summary>
79+
/// Sets an existing key to match a value or adds a new one if necessary.
80+
/// </summary>
81+
/// <param name="pair">A key-value pair containing a key string and a value string</param>
82+
/// <param name="cancellationToken">A cancellation token</param>
83+
public Task SetValueAsync(KeyValuePair<string, string?> pair, CancellationToken cancellationToken = default);
84+
85+
/// <summary>
86+
/// Sets an existing key to match a value or adds a new one if necessary.
87+
/// </summary>
88+
/// <param name="pair">A key-value pair containing a key string and a value integer</param>
89+
/// <param name="cancellationToken">A cancellation token</param>
90+
public Task SetValueAsync(KeyValuePair<string, int> pair, CancellationToken cancellationToken = default);
91+
92+
/// <summary>
93+
/// Sets an existing key to match a value or adds a new one if necessary.
94+
/// </summary>
95+
/// <param name="pair">A key-value pair containing a key string and a value boolean</param>
96+
/// <param name="cancellationToken">A cancellation token</param>
97+
public Task SetValueAsync(KeyValuePair<string, bool> pair, CancellationToken cancellationToken = default);
98+
99+
}
100+
101+
}

WriterSharp.PluginApi/WriterSharp.PluginApi.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
<DebugType>none</DebugType>
2929
</PropertyGroup>
3030

31+
<ItemGroup>
32+
<PackageReference Include="ini-parser" Version="2.5.2" />
33+
</ItemGroup>
34+
3135
</Project>

0 commit comments

Comments
 (0)