Skip to content

Commit

Permalink
Partial refactor of RuriLib
Browse files Browse the repository at this point in the history
  • Loading branch information
openbullet committed Oct 15, 2024
1 parent eb7793f commit 7035548
Show file tree
Hide file tree
Showing 49 changed files with 5,052 additions and 4,856 deletions.
5 changes: 2 additions & 3 deletions RuriLib.Http/Helpers/ChunkedDecoderOptimized.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ private void ParseNewChunk(ref ReadOnlySequence<byte> buff)
}
}

private int GetChunkLength(ref ReadOnlySequence<byte> buff)
private static int GetChunkLength(ref ReadOnlySequence<byte> buff)
{
if (buff.IsSingleSegment)
{
var index = -1;
var span = buff.FirstSpan;
index = span.IndexOf(_crlfBytes);
var index = span.IndexOf(_crlfBytes);

if (index == -1)
{
Expand Down
2 changes: 1 addition & 1 deletion RuriLib.Parallelization.Tests/ParallelizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public async Task Run_DecreaseConcurrentThreads_CompleteSlower()
[Fact]
public async Task Run_PauseAndResume_CompleteAll()
{
var count = 10;
const int count = 10;
var parallelizer = ParallelizerFactory<int, bool>.Create(
type: _type,
workItems: Enumerable.Range(1, count),
Expand Down
11 changes: 7 additions & 4 deletions RuriLib.Proxies/Clients/HttpProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace RuriLib.Proxies.Clients;
/// <summary>
/// A client that provides proxies connections via HTTP proxies.
/// </summary>
public class HttpProxyClient : ProxyClient
public partial class HttpProxyClient : ProxyClient
{
/// <summary>
/// The HTTP version to send in the first line of the request to the proxy.
/// By default it's 1.1
/// By default, it's 1.1
/// </summary>
public string ProtocolVersion { get; set; } = "1.1";

Expand Down Expand Up @@ -59,7 +59,7 @@ protected override async Task CreateConnectionAsync(TcpClient client, string des
{
client.Close();

if (ex is IOException || ex is SocketException)
if (ex is IOException or SocketException)
{
throw new ProxyException("Error while working with proxy", ex);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private static async Task<HttpStatusCode> ReceiveResponseAsync(NetworkStream nSt
}

// Check if the response is a correct HTTP response
var match = Regex.Match(response, "HTTP/[0-9\\.]* ([0-9]{3})");
var match = HttpResponseRegex().Match(response);

if (!match.Success)
{
Expand All @@ -151,4 +151,7 @@ private static async Task<HttpStatusCode> ReceiveResponseAsync(NetworkStream nSt

return statusCode;
}

[GeneratedRegex("HTTP/[0-9\\.]* ([0-9]{3})")]
private static partial Regex HttpResponseRegex();
}
54 changes: 28 additions & 26 deletions RuriLib/Attributes/Block.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
using System;

namespace RuriLib.Attributes
namespace RuriLib.Attributes;

/// <summary>
/// Attribute used to decorate a method that can be turned into an auto block.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class Block : Attribute
{
/// <summary>
/// Attribute used to decorate a method that can be turned into an auto block.
/// The name of the block. If not specified, a name will be automatically
/// generated from the name of the method.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class Block : Attribute
{
/// <summary>
/// The name of the block. If not specified, a name will be automatically
/// generated from the name of the method.
/// </summary>
public string name = null;
// ReSharper disable once InconsistentNaming
public string? name = null;

/// <summary>
/// The description of what the block does.
/// </summary>
public string description = null;
/// <summary>
/// The description of what the block does.
/// </summary>
// ReSharper disable once InconsistentNaming
public string description;

/// <summary>
/// Any extra information that is too long to fit the short and concise description.
/// </summary>
public string extraInfo = null;
/// <summary>
/// Any extra information that is too long to fit the short and concise description.
/// </summary>
// ReSharper disable once InconsistentNaming
public string? extraInfo = null;

/// <summary>
/// Creates a <see cref="Block"/> attribute given the <paramref name="description"/> of what the block does.
/// The name of the block will be automatically generated unless explicitly set in the <see cref="name"/> field.
/// </summary>
public Block(string description)
{
this.description = description;
}
/// <summary>
/// Creates a <see cref="Block"/> attribute given the <paramref name="description"/> of what the block does.
/// The name of the block will be automatically generated unless explicitly set in the <see cref="name"/> field.
/// </summary>
public Block(string description)
{
this.description = description;
}
}
70 changes: 36 additions & 34 deletions RuriLib/Attributes/BlockAction.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
using System;

namespace RuriLib.Attributes
namespace RuriLib.Attributes;

/// <summary>
/// Attribute used to decorate a method that is a block action. The method should take only one
/// parameter of type <see cref="Models.Blocks.BlockInstance"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BlockAction : Attribute
{
/// <summary>
/// Attribute used to decorate a method that is a block action. The method should take only one
/// parameter of type <see cref="Models.Blocks.BlockInstance"/>.
/// The name of the action. If not specified, a name will automatically be
/// generated from the name of the method.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BlockAction : Attribute
{
/// <summary>
/// The name of the action. If not specified, a name will automatically be
/// generated from the name of the method.
/// </summary>
public string name = null;
// ReSharper disable once InconsistentNaming
public string? name;

/// <summary>
/// The description of what the action does.
/// </summary>
public string description = null;
/// <summary>
/// The description of what the action does.
/// </summary>
// ReSharper disable once InconsistentNaming
public string? description;

/// <summary>
/// The id of the block to which this action belongs to. Normally, the
/// id of a block is the name of the method.
/// </summary>
public string parentBlockId = null;
/// <summary>
/// The id of the block to which this action belongs to. Normally, the
/// id of a block is the name of the method.
/// </summary>
// ReSharper disable once InconsistentNaming
public string parentBlockId;

/// <summary>
/// Defines a block action.
/// </summary>
/// <param name="parentBlockId">The id of the block to which this action belongs to. Normally, the
/// id of a block is the name of the method.</param>
/// <param name="name">The name of the action. If not specified, a name will automatically be
/// generated from the name of the method.</param>
/// <param name="description">The description of what the action does.</param>
public BlockAction(string parentBlockId, string name = null, string description = null)
{
this.parentBlockId = parentBlockId;
this.name = name;
this.description = description;
}
/// <summary>
/// Defines a block action.
/// </summary>
/// <param name="parentBlockId">The id of the block to which this action belongs to. Normally, the
/// id of a block is the name of the method.</param>
/// <param name="name">The name of the action. If not specified, a name will automatically be
/// generated from the name of the method.</param>
/// <param name="description">The description of what the action does.</param>
public BlockAction(string parentBlockId, string? name = null, string? description = null)
{
this.parentBlockId = parentBlockId;
this.name = name;
this.description = description;
}
}
69 changes: 36 additions & 33 deletions RuriLib/Attributes/BlockCategory.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
using System;

namespace RuriLib.Attributes
namespace RuriLib.Attributes;

/// <summary>
/// Attribute used to decorate a class that contains methods decorated with the <see cref="Block"/> attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class BlockCategory : Attribute
{
/// <summary>
/// Attribute used to decorate a class that contains methods decorated with the <see cref="Block"/> attribute.
/// The name of the category.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class BlockCategory : Attribute
{
/// <summary>
/// The name of the category.
/// </summary>
public string name = null;
// ReSharper disable once InconsistentNaming
public string name;

/// <summary>
/// The common features of blocks that are grouped in this category.
/// </summary>
public string description;
/// <summary>
/// The common features of blocks that are grouped in this category.
/// </summary>
// ReSharper disable once InconsistentNaming
public string description;

/// <summary>
/// The background color of the category when displayed in a UI, as an HTML color string.
/// </summary>
public string backgroundColor;
/// <summary>
/// The background color of the category when displayed in a UI, as an HTML color string.
/// </summary>
// ReSharper disable once InconsistentNaming
public string backgroundColor;

/// <summary>
/// The foreground color of the category when displayed in a UI, as an HTML color string.
/// </summary>
public string foregroundColor;
/// <summary>
/// The foreground color of the category when displayed in a UI, as an HTML color string.
/// </summary>
// ReSharper disable once InconsistentNaming
public string foregroundColor;

/// <summary>
/// Creates a <see cref="BlockCategory"/> attribute given its <paramref name="name"/>,
/// <paramref name="description"/> and colors.
/// </summary>
public BlockCategory(string name, string description, string backgroundColor = "#fff",
string foregroundColor = "#000")
{
this.name = name;
this.description = description;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}
/// <summary>
/// Creates a <see cref="BlockCategory"/> attribute given its <paramref name="name"/>,
/// <paramref name="description"/> and colors.
/// </summary>
public BlockCategory(string name, string description, string backgroundColor = "#fff",
string foregroundColor = "#000")
{
this.name = name;
this.description = description;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}
}
50 changes: 26 additions & 24 deletions RuriLib/Attributes/BlockImage.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
using System;

namespace RuriLib.Attributes
namespace RuriLib.Attributes;

/// <summary>
/// Attribute used to decorate a block that can display images.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class BlockImage : Attribute
{
/// <summary>
/// Attribute used to decorate a block that can display images.
/// The unique id of the image.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class BlockImage : Attribute
{
/// <summary>
/// The unique id of the image.
/// </summary>
public string id = null;
// ReSharper disable once InconsistentNaming
public string id;

/// <summary>
/// The max width in pixels.
/// </summary>
public int maxWidth = 300;
/// <summary>
/// The max width in pixels.
/// </summary>
// ReSharper disable once InconsistentNaming
public int maxWidth = 300;

/// <summary>
/// The max height in pixels.
/// </summary>
public int maxHeight = 300;
/// <summary>
/// The max height in pixels.
/// </summary>
// ReSharper disable once InconsistentNaming
public int maxHeight = 300;

/// <summary>
/// Defines a block image with a given <paramref name="id"/>.
/// </summary>
public BlockImage(string id)
{
this.id = id;
}
/// <summary>
/// Defines a block image with a given <paramref name="id"/>.
/// </summary>
public BlockImage(string id)
{
this.id = id;
}
}
Loading

0 comments on commit 7035548

Please sign in to comment.