Skip to content

Commit

Permalink
New version
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Sep 27, 2023
2 parents 162e3ce + 3632f9b commit f1bee46
Show file tree
Hide file tree
Showing 15 changed files with 361 additions and 100 deletions.
4 changes: 2 additions & 2 deletions BytecodeApi.Cryptography/Encryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static byte[] Encrypt(byte[] data, byte[] iv, byte[] key)
Check.ArgumentNull(iv);
Check.Argument(iv.Length == 16, nameof(iv), "Array must be a 128-bit sized byte array (16 bytes).");
Check.ArgumentNull(key);
Check.Argument(key.Length == 16 || key.Length == 32, nameof(key), "Array must be a 128-bit or 256-bit sized byte array (16 or 32 bytes).");
Check.Argument(key.Length is 16 or 32, nameof(key), "Array must be a 128-bit or 256-bit sized byte array (16 or 32 bytes).");

using Aes aes = Aes.Create();
aes.IV = iv;
Expand Down Expand Up @@ -77,7 +77,7 @@ public static byte[] Decrypt(byte[] data, byte[] iv, byte[] key)
Check.ArgumentNull(iv);
Check.Argument(iv.Length == 16, nameof(iv), "Array must be a 128-bit sized byte array (16 bytes).");
Check.ArgumentNull(key);
Check.Argument(key.Length == 16 || key.Length == 32, nameof(key), "Array must be a 128-bit or 256-bit sized byte array (16 or 32 bytes).");
Check.Argument(key.Length is 16 or 32, nameof(key), "Array must be a 128-bit or 256-bit sized byte array (16 or 32 bytes).");

using Aes aes = Aes.Create();
aes.IV = iv;
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi.Lexer/BytecodeApi.Lexer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi.Lexer</Product>
Expand Down
8 changes: 4 additions & 4 deletions BytecodeApi.Lexer/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ internal sealed class Grammar<TTokenType> where TTokenType : struct, IConvertibl
private readonly Regex Regex;
public bool Ignore { get; private init; }
public TTokenType Type { get; private init; }
public Func<string, string>? PostProcessValue { get; private init; }
public Func<Match, string>? GetValue { get; private init; }

public Grammar(bool ignore, TTokenType type, string pattern, Func<string, string>? postProcessValue)
public Grammar(bool ignore, TTokenType type, string pattern, RegexOptions regexOptions, Func<Match, string>? getValue)
{
Regex = new(@"\G(" + pattern + ")", RegexOptions.Compiled);
Regex = new(@"\G(" + pattern + ")", RegexOptions.Compiled | regexOptions);
Ignore = ignore;
Type = type;
PostProcessValue = postProcessValue;
GetValue = getValue;
}

public Match Match(string code, int position)
Expand Down
59 changes: 48 additions & 11 deletions BytecodeApi.Lexer/Lexer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BytecodeApi.Extensions;
using BytecodeApi.Text;
using System.Text.RegularExpressions;

namespace BytecodeApi.Lexer;

Expand Down Expand Up @@ -42,10 +43,22 @@ internal Lexer()
/// A reference to this instance after the operation has completed.
/// </returns>
public Lexer<TTokenType> Ignore(string pattern)
{
return Ignore(pattern, RegexOptions.None);
}
/// <summary>
/// Specifies that tokens that match the given pattern are ignored.
/// </summary>
/// <param name="pattern">A <see cref="string" /> with a regular expression to match the token.</param>
/// <param name="regexOptions">The <see cref="RegexOptions" /> to be used to match the pattern.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public Lexer<TTokenType> Ignore(string pattern, RegexOptions regexOptions)
{
Check.ArgumentNull(pattern);

Grammar.Add(new(true, default, pattern, null));
Grammar.Add(new(true, default, pattern, regexOptions, null));
return this;
}
/// <summary>
Expand All @@ -58,22 +71,49 @@ public Lexer<TTokenType> Ignore(string pattern)
/// </returns>
public Lexer<TTokenType> Match(TTokenType type, string pattern)
{
return Match(type, pattern, null);
return Match(type, pattern, RegexOptions.None);
}
/// <summary>
/// Specifies that tokens of a specific type match the given pattern.
/// </summary>
/// <param name="type">The type of token that matches the given pattern.</param>
/// <param name="pattern">A <see cref="string" /> with a regular expression to match the token.</param>
/// <param name="regexOptions">The <see cref="RegexOptions" /> to be used to match the pattern.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public Lexer<TTokenType> Match(TTokenType type, string pattern, RegexOptions regexOptions)
{
return Match(type, pattern, regexOptions, null);
}
/// <summary>
/// Specifies that tokens of a specific type match the given pattern.
/// </summary>
/// <param name="type">The type of token that matches the given pattern.</param>
/// <param name="pattern">A <see cref="string" /> with a regular expression to match the token.</param>
/// <param name="postProcessValue">A conversion method that converts the parsed token - e.g., to remove quotes of a quoted string literal.</param>
/// <param name="getValue">A custom conversion method that converts the parsed <see cref="System.Text.RegularExpressions.Match" /> - e.g., to remove quotes of a quoted string literal.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public Lexer<TTokenType> Match(TTokenType type, string pattern, Func<string, string>? postProcessValue)
public Lexer<TTokenType> Match(TTokenType type, string pattern, Func<Match, string>? getValue)
{
return Match(type, pattern, RegexOptions.None, getValue);
}
/// <summary>
/// Specifies that tokens of a specific type match the given pattern.
/// </summary>
/// <param name="type">The type of token that matches the given pattern.</param>
/// <param name="pattern">A <see cref="string" /> with a regular expression to match the token.</param>
/// <param name="regexOptions">The <see cref="RegexOptions" /> to be used to match the pattern.</param>
/// <param name="getValue">A custom conversion method that converts the parsed <see cref="System.Text.RegularExpressions.Match" /> - e.g., to remove quotes of a quoted string literal.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public Lexer<TTokenType> Match(TTokenType type, string pattern, RegexOptions regexOptions, Func<Match, string>? getValue)
{
Check.ArgumentNull(pattern);

Grammar.Add(new(false, type, pattern, postProcessValue));
Grammar.Add(new(false, type, pattern, regexOptions, getValue));
return this;
}

Expand Down Expand Up @@ -108,12 +148,9 @@ public TokenCollection<TTokenType> Parse(string code)
{
if (!match.Grammar.Ignore)
{
string value = match.Match.Value;

if (match.Grammar.PostProcessValue != null)
{
value = match.Grammar.PostProcessValue(value);
}
string value = match.Grammar.GetValue != null
? match.Grammar.GetValue(match.Match)
: match.Match.Value;

tokens.Add(new(line, match.Grammar.Type, value));
}
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi.Win32/BytecodeApi.Win32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi.Win32</Product>
Expand Down
21 changes: 16 additions & 5 deletions BytecodeApi.Win32/SystemInfo/HardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class HardwareInfo
{
private static string[]? _ProcessorNames;
private static string[]? _VideoControllerNames;
private static long? _Memory;
private static long? _TotalMemory;
/// <summary>
/// Gets the names of all installed processors.
/// </summary>
Expand Down Expand Up @@ -65,17 +65,28 @@ public static string[] VideoControllerNames
/// <summary>
/// Gets the total amount of installed physical memory.
/// </summary>
public static long Memory
public static long TotalMemory
{
get
{
if (_Memory == null)
if (_TotalMemory == null)
{
Native.MemoryStatusEx memoryStatus = new();
_Memory = Native.GlobalMemoryStatusEx(memoryStatus) ? (long)memoryStatus.TotalPhys : throw Throw.Win32();
_TotalMemory = Native.GlobalMemoryStatusEx(memoryStatus) ? (long)memoryStatus.TotalPhys : throw Throw.Win32();
}

return _Memory.Value;
return _TotalMemory.Value;
}
}
/// <summary>
/// Gets the total amount of available physical memory.
/// </summary>
public static long AvailableMemory
{
get
{
Native.MemoryStatusEx memoryStatus = new();
return Native.GlobalMemoryStatusEx(memoryStatus) ? (long)memoryStatus.AvailPhys : throw Throw.Win32();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi.Wpf/BytecodeApi.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi.Wpf</Product>
Expand Down
124 changes: 124 additions & 0 deletions BytecodeApi.Wpf/Extensions/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,130 @@ namespace BytecodeApi.Wpf.Extensions;
/// </summary>
public static class ApplicationExtensions
{
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
public static void Dispatch(this Application application, Action callback)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

application.Dispatcher.Invoke(callback);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
public static void Dispatch(this Application application, Action callback, DispatcherPriority priority)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

application.Dispatcher.Invoke(callback, priority);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
public static void Dispatch(this Application application, Action callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

application.Dispatcher.Invoke(callback, priority, cancellationToken);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <param name="timeout">The minimum amount of time to wait for the operation to start.</param>
public static void Dispatch(this Application application, Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

application.Dispatcher.Invoke(callback, priority, cancellationToken, timeout);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public static T Dispatch<T>(this Application application, Func<T> callback)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

return application.Dispatcher.Invoke(callback);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public static T Dispatch<T>(this Application application, Func<T> callback, DispatcherPriority priority)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

return application.Dispatcher.Invoke(callback, priority);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public static T Dispatch<T>(this Application application, Func<T> callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

return application.Dispatcher.Invoke(callback, priority, cancellationToken);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="application">The <see cref="Application" /> to invoke the dispatcher in.</param>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <param name="timeout">The minimum amount of time to wait for the operation to start.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public static T Dispatch<T>(this Application application, Func<T> callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
Check.ArgumentNull(application);
Check.ArgumentNull(callback);

return application.Dispatcher.Invoke(callback, priority, cancellationToken, timeout);
}
/// <summary>
/// Searches for a user interface (UI) resource, such as a <see cref="Style" /> or <see cref="Brush" />, with the specified key, and throws an exception, if the requested resource is not found (see XAML Resources).
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi/BytecodeApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
<Version>3.0.2</Version>
<AssemblyVersion>3.0.2</AssemblyVersion>
<FileVersion>3.0.2</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi</Product>
Expand Down
Loading

0 comments on commit f1bee46

Please sign in to comment.