Add D2D1EffectRegistrationData.V1.Load API
このコミットが含まれているのは:
1 親
57bebc1aef
コミット
f2b49cd6b8
2ファイル変更
+122
-19
検索条件に一致するファイルはありません
@@ -8,6 +8,18 @@ namespace ComputeSharp.D2D1.Helpers;
|
||||
/// </summary>
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The argument name.</param>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentException">Thrown with the specified parameters.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentException(string? name, string? message)
|
||||
{
|
||||
throw new ArgumentException(message, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
#if !NET6_0_OR_GREATER
|
||||
using ComputeSharp.D2D1.NetStandard.System.Text;
|
||||
#endif
|
||||
@@ -79,6 +80,54 @@ public static class D2D1EffectRegistrationData
|
||||
/// </summary>
|
||||
public void* EffectFactory { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="V1"/> instance from the input binary blob.
|
||||
/// </summary>
|
||||
/// <param name="blob">The input binary blob to deserialize.</param>
|
||||
/// <returns>The resulting <see cref="V1"/> instance.</returns>
|
||||
/// <exception cref="ArgumentException">Thrown if the loading failed.</exception>
|
||||
/// <remarks>
|
||||
/// The input blob should have been created by a call to any of the overloads
|
||||
/// of <see cref="D2D1PixelShaderEffect.GetRegistrationBlob{T}(out Guid)"/>.
|
||||
/// </remarks>
|
||||
public static V1 Load(ReadOnlyMemory<byte> blob)
|
||||
{
|
||||
switch (TryLoadAndGetResult(blob, out V1 data))
|
||||
{
|
||||
case LoadingResult.Success:
|
||||
return data;
|
||||
case LoadingResult.EmptyBuffer:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is empty.");
|
||||
break;
|
||||
case LoadingResult.BufferTooShort:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too short.");
|
||||
break;
|
||||
case LoadingResult.BufferTooLong:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too long.");
|
||||
break;
|
||||
case LoadingResult.MismatchedBlobVersionId:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The blob version id from the input blob does not match.");
|
||||
break;
|
||||
case LoadingResult.InvalidNumberOfInputs:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid number of inputs.");
|
||||
break;
|
||||
case LoadingResult.InvalidPropertyXml:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The property XML value in the input blob is invalid.");
|
||||
break;
|
||||
case LoadingResult.InvalidNumberOfBindings:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob declares an invalid number of bindings.");
|
||||
break;
|
||||
case LoadingResult.InvalidBindingPropertyName:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid property name for one of its property bindings.");
|
||||
break;
|
||||
default:
|
||||
ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob failed to be loaded for an unknown reason.");
|
||||
break;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to load a <see cref="V1"/> instance from the input binary blob.
|
||||
/// </summary>
|
||||
@@ -89,22 +138,38 @@ public static class D2D1EffectRegistrationData
|
||||
/// The input blob should have been created by a call to any of the overloads
|
||||
/// of <see cref="D2D1PixelShaderEffect.GetRegistrationBlob{T}(out Guid)"/>.
|
||||
/// </remarks>
|
||||
public static unsafe bool TryLoad(ReadOnlyMemory<byte> blob, out V1 data)
|
||||
public static bool TryLoad(ReadOnlyMemory<byte> blob, out V1 data)
|
||||
{
|
||||
return TryLoadAndGetResult(blob, out data) is LoadingResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to load a <see cref="V1"/> instance from the input binary blob and returns a result.
|
||||
/// </summary>
|
||||
/// <param name="blob">The input binary blob to deserialize.</param>
|
||||
/// <param name="data">The resulting <see cref="V1"/> instance, if successful.</param>
|
||||
/// <returns>The result of the loading operation.</returns>
|
||||
private static LoadingResult TryLoadAndGetResult(ReadOnlyMemory<byte> blob, out V1 data)
|
||||
{
|
||||
data = default;
|
||||
|
||||
if (blob.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.EmptyBuffer;
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> span = blob.Span;
|
||||
|
||||
// Blob id
|
||||
if (!MemoryMarshal.TryRead(span, out Guid blobId) ||
|
||||
blobId != BlobId)
|
||||
if (!MemoryMarshal.TryRead(span, out Guid blobId))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
// Validate the blob id
|
||||
if (blobId != BlobId)
|
||||
{
|
||||
return LoadingResult.MismatchedBlobVersionId;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(Guid));
|
||||
@@ -112,16 +177,21 @@ public static class D2D1EffectRegistrationData
|
||||
// Effect class id
|
||||
if (!MemoryMarshal.TryRead(span, out Guid effectId))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(Guid));
|
||||
|
||||
// Number of inputs
|
||||
if (!MemoryMarshal.TryRead(span, out int numberOfInputs) ||
|
||||
numberOfInputs < 0)
|
||||
if (!MemoryMarshal.TryRead(span, out int numberOfInputs))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
// Validate the number of inputs
|
||||
if (numberOfInputs < 0)
|
||||
{
|
||||
return LoadingResult.InvalidNumberOfInputs;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(int));
|
||||
@@ -131,7 +201,7 @@ public static class D2D1EffectRegistrationData
|
||||
|
||||
if (lengthOfXml == -1)
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.InvalidPropertyXml;
|
||||
}
|
||||
|
||||
string xml = Encoding.UTF8.GetString(span.Slice(0, lengthOfXml));
|
||||
@@ -139,10 +209,15 @@ public static class D2D1EffectRegistrationData
|
||||
span = span.Slice(lengthOfXml + 1);
|
||||
|
||||
// Number of bindings
|
||||
if (!MemoryMarshal.TryRead(span, out int numberOfBindings) ||
|
||||
numberOfBindings < 0)
|
||||
if (!MemoryMarshal.TryRead(span, out int numberOfBindings))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
// Validate the number of bindings
|
||||
if (numberOfBindings < 0)
|
||||
{
|
||||
return LoadingResult.InvalidNumberOfBindings;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(int));
|
||||
@@ -157,7 +232,7 @@ public static class D2D1EffectRegistrationData
|
||||
|
||||
if (lengthOfName == -1)
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.InvalidBindingPropertyName;
|
||||
}
|
||||
|
||||
string name = Encoding.UTF8.GetString(span.Slice(0, lengthOfName));
|
||||
@@ -167,7 +242,7 @@ public static class D2D1EffectRegistrationData
|
||||
// Property get function
|
||||
if (!MemoryMarshal.TryRead(span, out nint getFunction))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
@@ -175,7 +250,7 @@ public static class D2D1EffectRegistrationData
|
||||
// Property set function
|
||||
if (!MemoryMarshal.TryRead(span, out nint setFunction))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
@@ -186,7 +261,7 @@ public static class D2D1EffectRegistrationData
|
||||
// Effect factory
|
||||
if (!MemoryMarshal.TryRead(span, out nint effectFactory))
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooShort;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
@@ -194,7 +269,7 @@ public static class D2D1EffectRegistrationData
|
||||
// If the buffer is bigger than expected, also consider it malformed
|
||||
if (span.Length > 0)
|
||||
{
|
||||
return false;
|
||||
return LoadingResult.BufferTooLong;
|
||||
}
|
||||
|
||||
data = new V1(
|
||||
@@ -204,7 +279,23 @@ public static class D2D1EffectRegistrationData
|
||||
propertyBindings,
|
||||
effectFactory);
|
||||
|
||||
return true;
|
||||
return LoadingResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the result of a blob loading operation.
|
||||
/// </summary>
|
||||
private enum LoadingResult
|
||||
{
|
||||
Success,
|
||||
EmptyBuffer,
|
||||
BufferTooShort,
|
||||
BufferTooLong,
|
||||
MismatchedBlobVersionId,
|
||||
InvalidNumberOfInputs,
|
||||
InvalidPropertyXml,
|
||||
InvalidNumberOfBindings,
|
||||
InvalidBindingPropertyName
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする