Skip to content

Commit 5372999

Browse files
committed
Yup, done with #1
1 parent 142a1d9 commit 5372999

File tree

8 files changed

+222
-71
lines changed

8 files changed

+222
-71
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
4+
namespace WriterSharp.Exceptions.IO
5+
{
6+
7+
/// <summary>
8+
/// A generic base exception that all WriterSharp exceptions related to IO inherit from.
9+
/// </summary>
10+
public class WriterSharpIOException : WriterSharpException
11+
{
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="WriterSharpIOException" /> class.
15+
/// </summary>
16+
public WriterSharpIOException() : base() { }
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="WriterSharpIOException" />
20+
/// with a custom error message.
21+
/// </summary>
22+
/// <param name="message">The custom error message</param>
23+
public WriterSharpIOException(string message) : base(message) { }
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="WriterSharpIOException" />
27+
/// with a custom error message and a reference to the exception
28+
/// that caused this error.
29+
/// </summary>
30+
/// <param name="message"></param>
31+
/// <param name="innerException"></param>
32+
public WriterSharpIOException(string? message, Exception? innerException) : base(message, innerException) { }
33+
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
4+
namespace WriterSharp.Exceptions.Security
5+
{
6+
7+
/// <summary>
8+
/// Exceptions that is thrown when an authorized caller attempts to run a WriterSharp command.
9+
/// </summary>
10+
public class SecurityException : WriterSharpException
11+
{
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="SecurityException" /> class.
15+
/// </summary>
16+
public SecurityException() : base() { }
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="SecurityException" />
20+
/// with a custom error message.
21+
/// </summary>
22+
/// <param name="message">The custom error message</param>
23+
public SecurityException(string message) : base(message) { }
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="SecurityException" />
27+
/// with a custom error message and a reference to the exception
28+
/// that caused this error.
29+
/// </summary>
30+
/// <param name="message"></param>
31+
/// <param name="innerException"></param>
32+
public SecurityException(string? message, Exception? innerException) : base(message, innerException) { }
33+
34+
}
35+
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<Platforms>AnyCPU;x64;x86;ARM32;ARM64</Platforms>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
11+
<DebugType>none</DebugType>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
15+
<DebugType>none</DebugType>
16+
</PropertyGroup>
17+
18+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
19+
<DebugType>none</DebugType>
20+
</PropertyGroup>
21+
22+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM32'">
23+
<DebugType>none</DebugType>
24+
</PropertyGroup>
25+
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
27+
<DebugType>none</DebugType>
28+
</PropertyGroup>
29+
30+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
4+
namespace WriterSharp.Exceptions
5+
{
6+
7+
/// <summary>
8+
/// A generic base exception that all WriterSharp exceptions inherit from.
9+
/// </summary>
10+
public class WriterSharpException : Exception
11+
{
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="WriterSharpException" /> class.
15+
/// </summary>
16+
public WriterSharpException() : base() { }
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="WriterSharpException" />
20+
/// with a custom error message.
21+
/// </summary>
22+
/// <param name="message">The custom error message</param>
23+
public WriterSharpException(string message) : base(message) { }
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="WriterSharpException" />
27+
/// with a custom error message and a reference to the exception
28+
/// that caused this error.
29+
/// </summary>
30+
/// <param name="message"></param>
31+
/// <param name="innerException"></param>
32+
public WriterSharpException(string? message, Exception? innerException) : base(message, innerException) { }
33+
34+
}
35+
36+
}

WriterSharp.PluginApi/FileSystem/IFile.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ public interface IFile
1414
public string AbsoluteFilepath { get; }
1515

1616
/// <summary>
17-
/// The name of the file.
17+
/// The name (without the extension) of the file.
1818
/// </summary>
1919
public string Name { get; }
2020

21+
/// <summary>
22+
/// The extension (with the dot) of the file.
23+
/// </summary>
24+
public string Extension { get; }
25+
2126
}
2227

2328
}

WriterSharp.PluginApi/FileSystem/IFileSystem.cs

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,20 @@ public interface IFileSystem
2020
/// <returns>The contents of the file</returns>
2121
public string ReadAllText(string filepath);
2222

23-
/// <summary>
24-
/// Reads all text from a file.
25-
/// </summary>
26-
/// <param name="file">The file</param>
27-
/// <returns>The contents of the file</returns>
28-
public string ReadAllText(IFile file);
29-
3023
/// <summary>
3124
/// Reads all text from a file, as a list of lines.
3225
/// </summary>
3326
/// <param name="filepath">The path to the file</param>
3427
/// <returns>The contents of the file</returns>
3528
public string[] ReadAllLines(string filepath);
3629

37-
/// <summary>
38-
/// Reads all text from a file, as a list of lines.
39-
/// </summary>
40-
/// <param name="file">The file</param>
41-
/// <returns>The contents of the file</returns>
42-
public string[] ReadAllLines(IFile file);
43-
4430
/// <summary>
4531
/// Reads the very first line of a file.
4632
/// </summary>
4733
/// <param name="filepath">The path to the file</param>
4834
/// <returns>The first line of the file</returns>
4935
public string ReadLine(string filepath);
5036

51-
/// <summary>
52-
/// Reads the very first line of a file.
53-
/// </summary>
54-
/// <param name="file">The file</param>
55-
/// <returns>The first line of the file</returns>
56-
public string ReadLine(IFile file);
57-
5837
/// <summary>
5938
/// Reads a specific amount of characters from a file buffer.
6039
/// </summary>
@@ -64,15 +43,6 @@ public interface IFileSystem
6443
/// <returns>A span of characters</returns>
6544
public Span<char> ReadCharacters(string filepath, ulong amount, long offset = 0);
6645

67-
/// <summary>
68-
/// Reads a specific amount of characters from a file buffer.
69-
/// </summary>
70-
/// <param name="file">The file</param>
71-
/// <param name="amount">The amount of characters to read</param>
72-
/// <param name="offset">The index from which to start reading the characters</param>
73-
/// <returns>A span of characters</returns>
74-
public Span<char> ReadCharacters(IFile file, ulong amount, long offset = 0);
75-
7646
#endregion
7747

7848
#region Writing
@@ -85,14 +55,6 @@ public interface IFileSystem
8555
/// <param name="data">The text to write</param>
8656
public void WriteAllText(string filepath, string data);
8757

88-
/// <summary>
89-
/// Writes text to a file, creating it if necessary. If the file exists,
90-
/// it will be overwritten.
91-
/// </summary>
92-
/// <param name="file">The file</param>
93-
/// <param name="data">The text to write</param>
94-
public void WriteAllText(IFile file, string data);
95-
9658
/// <summary>
9759
/// Writes lines of text to a file, creating it if necessary.
9860
/// If the file exists, it will be overwritten.
@@ -101,14 +63,6 @@ public interface IFileSystem
10163
/// <param name="data">The lines of text to write</param>
10264
public void WriteAllLines(string filepath, string[] data);
10365

104-
/// <summary>
105-
/// Writes lines of text to a file, creating it if necessary.
106-
/// If the file exists, it will be overwritten.
107-
/// </summary>
108-
/// <param name="file">The file</param>
109-
/// <param name="data">The lines of text to write</param>
110-
public void WriteAllLines(IFile file, string[] data);
111-
11266
#endregion
11367

11468
#region Appending
@@ -120,27 +74,13 @@ public interface IFileSystem
12074
/// <param name="data">The text to append</param>
12175
public void AppendAllText(string filepath, string data);
12276

123-
/// <summary>
124-
/// Appends all the text to the end of a file.
125-
/// </summary>
126-
/// <param name="file">The file</param>
127-
/// <param name="data">The text to append</param>
128-
public void AppendAllText(IFile file, string data);
129-
13077
/// <summary>
13178
/// Appends all the specified lines of text to the end of a file.
13279
/// </summary>
13380
/// <param name="filepath">The path to the file</param>
13481
/// <param name="data">The lines to append</param>
13582
public void AppendAllLines(string filepath, string[] data);
13683

137-
/// <summary>
138-
/// Appends all the specified lines of text to the end of a file.
139-
/// </summary>
140-
/// <param name="file">The file</param>
141-
/// <param name="data">The lines to append</param>
142-
public void AppendAllLines(IFile file, string[] data);
143-
14484
#endregion
14585

14686
#region Sharding Toolkit
@@ -154,20 +94,44 @@ public interface IFileSystem
15494
/// <returns><c>true</c> if in use by another plugin</returns>
15595
public bool InUse(string filepath);
15696

97+
#endregion
98+
99+
#region Locking
100+
157101
/// <summary>
158-
/// Checks if a file is in use by another plugin.
159-
/// A result of <c>false</c> does not mean the file is strictly
160-
/// not in use - it only means no other plugin is using it.
102+
/// Locks a file, to prevent it from being accessed by other plugins.
161103
/// </summary>
162-
/// <param name="filepath">The file</param>
163-
/// <returns><c>true</c> if in use by another plugin</returns>
164-
public bool InUse(IFile filepath);
104+
/// <param name="filepath">The path to the file to lock</param>
105+
public void Lock(string filepath);
165106

166-
#endregion
107+
/// <summary>
108+
/// Unlocks a previously locked file.
109+
/// </summary>
110+
/// <param name="filepath">The path to the file</param>
111+
public void Unlock(string filepath);
167112

168-
#region Locking
113+
/// <summary>
114+
/// Checks if a file is locked.
115+
/// </summary>
116+
/// <param name="filepath">The path to the file</param>
117+
/// <param name="lockOwnerIsCaller"><c>true</c> if the caller is the owner of the lock placed on this file</param>
118+
/// <returns><c>true</c> if the file is locked, no matter who owns the lock</returns>
119+
public bool IsLocked(string filepath, out bool lockOwnerIsCaller);
120+
121+
/// <summary>
122+
/// Checks if a file is locked by the caller.
123+
/// </summary>
124+
/// <param name="filepath">The path to the file</param>
125+
/// <returns><c>true</c> if the file is locked and the lock owner is the caller</returns>
126+
public bool IsLockedByMe(string filepath) => IsLocked(filepath, out var byMe) && byMe;
169127

170-
// todo: this shit
128+
/// <summary>
129+
/// Checks if a file is considered "lockable" (allows locking)
130+
/// by WriterSharp.
131+
/// </summary>
132+
/// <param name="filepath">The path to the file</param>
133+
/// <returns><c>true</c> if it can be locked</returns>
134+
public bool IsLockable(string filepath);
171135

172136
#endregion
173137

WriterSharp.sln

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{089100B1-113F-4E66-888A-E83F3999EAFD}"
1515
ProjectSection(SolutionItems) = preProject
1616
.gitignore = .gitignore
17-
README.md = README.md
1817
LICENSE.md = LICENSE.md
18+
README.md = README.md
1919
EndProjectSection
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WriterSharp.Exceptions", "WriterSharp.Exceptions\WriterSharp.Exceptions.csproj", "{DFBA8B1C-4288-4120-8FE4-1443B52080AF}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -112,6 +114,26 @@ Global
112114
{2C08ED8C-BCD5-4660-93B1-CB5AA66897D8}.Release|x64.Build.0 = Release|Any CPU
113115
{2C08ED8C-BCD5-4660-93B1-CB5AA66897D8}.Release|x86.ActiveCfg = Release|Any CPU
114116
{2C08ED8C-BCD5-4660-93B1-CB5AA66897D8}.Release|x86.Build.0 = Release|Any CPU
117+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
118+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
119+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|ARM32.ActiveCfg = Debug|Any CPU
120+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|ARM32.Build.0 = Debug|Any CPU
121+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|ARM64.ActiveCfg = Debug|Any CPU
122+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|ARM64.Build.0 = Debug|Any CPU
123+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|x64.ActiveCfg = Debug|Any CPU
124+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|x64.Build.0 = Debug|Any CPU
125+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|x86.ActiveCfg = Debug|Any CPU
126+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Debug|x86.Build.0 = Debug|Any CPU
127+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
128+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|Any CPU.Build.0 = Release|Any CPU
129+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|ARM32.ActiveCfg = Release|Any CPU
130+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|ARM32.Build.0 = Release|Any CPU
131+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|ARM64.ActiveCfg = Release|Any CPU
132+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|ARM64.Build.0 = Release|Any CPU
133+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|x64.ActiveCfg = Release|Any CPU
134+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|x64.Build.0 = Release|Any CPU
135+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|x86.ActiveCfg = Release|Any CPU
136+
{DFBA8B1C-4288-4120-8FE4-1443B52080AF}.Release|x86.Build.0 = Release|Any CPU
115137
EndGlobalSection
116138
GlobalSection(SolutionProperties) = preSolution
117139
HideSolutionNode = FALSE

WriterSharp/WriterSharp.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@
66
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
77
<ApplicationManifest>app.manifest</ApplicationManifest>
88
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
9+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10+
<Platforms>AnyCPU;x64;x86;ARM32;ARM64</Platforms>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
14+
<DebugType>none</DebugType>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
18+
<DebugType>none</DebugType>
19+
</PropertyGroup>
20+
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
22+
<DebugType>none</DebugType>
23+
</PropertyGroup>
24+
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM32'">
26+
<DebugType>none</DebugType>
27+
</PropertyGroup>
28+
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
30+
<DebugType>none</DebugType>
931
</PropertyGroup>
1032

1133
<ItemGroup>

0 commit comments

Comments
 (0)