mirror of
https://github.com/ZuneDev/MicrosoftIris.git
synced 2026-07-27 13:13:29 -07:00
Sanity helpers when using DllResource (especially on non-Windows targets)
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
using Microsoft.Iris.OS;
|
using Microsoft.Iris.OS;
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.Iris.Debug;
|
||||||
|
|
||||||
namespace Microsoft.Iris.Data
|
namespace Microsoft.Iris.Data
|
||||||
{
|
{
|
||||||
@@ -109,13 +110,12 @@ namespace Microsoft.Iris.Data
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
_status = ResourceStatus.Error;
|
_status = ResourceStatus.Error;
|
||||||
if (errorDetails == null)
|
_errorDetails = errorDetails ?? $"Failed to acquire resource '{Identifier}'";
|
||||||
errorDetails = string.Format("Failed to acquire resource '{0}'", Identifier);
|
|
||||||
_errorDetails = errorDetails;
|
Trace.WriteLine(TraceCategory.Resource, _errorDetails);
|
||||||
}
|
}
|
||||||
if (_completeHandlers == null)
|
|
||||||
return;
|
_completeHandlers?.Invoke(this);
|
||||||
_completeHandlers(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static IntPtr AllocNativeBuffer(uint length) => NativeApi.MemAlloc(length, false);
|
protected static IntPtr AllocNativeBuffer(uint length) => NativeApi.MemAlloc(length, false);
|
||||||
|
|||||||
@@ -6,13 +6,14 @@
|
|||||||
|
|
||||||
using Microsoft.Iris.Data;
|
using Microsoft.Iris.Data;
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.Iris.Debug;
|
||||||
|
|
||||||
namespace Microsoft.Iris.OS
|
namespace Microsoft.Iris.OS
|
||||||
{
|
{
|
||||||
internal class DllResource : Resource
|
internal class DllResource : Resource
|
||||||
{
|
{
|
||||||
private string _dll;
|
private readonly string _dll;
|
||||||
private string _identifier;
|
private readonly string _identifier;
|
||||||
private IntPtr _buffer;
|
private IntPtr _buffer;
|
||||||
private uint _length;
|
private uint _length;
|
||||||
|
|
||||||
@@ -28,8 +29,21 @@ namespace Microsoft.Iris.OS
|
|||||||
protected override void StartAcquisition(bool forceSynchronous)
|
protected override void StartAcquisition(bool forceSynchronous)
|
||||||
{
|
{
|
||||||
string errorDetails = null;
|
string errorDetails = null;
|
||||||
if (_buffer == IntPtr.Zero && !NativeApi.SpLoadBinaryResource(_dll, _identifier, !DllResources.StaticDllResourcesOnly, out _buffer, out _length))
|
if (_buffer == IntPtr.Zero)
|
||||||
errorDetails = string.Format("Resource not found: res://{0}!{1}", _dll, _identifier);
|
{
|
||||||
|
var (error, buffer, length) = LoadBinaryResource(_dll, _identifier,
|
||||||
|
!DllResources.StaticDllResourcesOnly);
|
||||||
|
if (error is not null)
|
||||||
|
{
|
||||||
|
errorDetails = error;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_buffer = buffer;
|
||||||
|
_length = length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NotifyAcquisitionComplete(_buffer, _length, false, errorDetails);
|
NotifyAcquisitionComplete(_buffer, _length, false, errorDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +51,24 @@ namespace Microsoft.Iris.OS
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => _dll + "|" + _identifier.ToLowerInvariant();
|
public override string ToString() => $"{_dll}|{_identifier.ToLowerInvariant()}";
|
||||||
|
|
||||||
|
private static (string error, nint buffer, uint length) LoadBinaryResource(string dllPath, string resourceName, bool allowDynamicResources)
|
||||||
|
{
|
||||||
|
#if WINDOWS
|
||||||
|
|
||||||
|
if (NativeApi.SpLoadBinaryResource(dllPath, resourceName, allowDynamicResources, out var buffer, out var length))
|
||||||
|
return (null, buffer, length);
|
||||||
|
|
||||||
|
return ($"Resource not found: res://{dllPath}!{resourceName}", 0, 0);
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
var error = $"Cannot load Win32 resource res://{dllPath}!{resourceName} on non-Windows platforms";
|
||||||
|
Trace.WriteLine(TraceCategory.Resource, error);
|
||||||
|
return (error, 0, 0);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user