-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0a1a1e
commit f4e0f66
Showing
18 changed files
with
984 additions
and
701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
|
||
using TerraFX.Interop.Windows; | ||
|
||
namespace Dalamud.ImGuiScene.Helpers; | ||
|
||
/// <summary> | ||
/// Base for implementing COM. The implementor must have this struct at the beginning of the struct layout. | ||
/// </summary> | ||
/// <remarks> | ||
/// Would use generics as UnknownComBase{T}, but doing so makes the types containing this fail to initialize. | ||
/// </remarks> | ||
[StructLayout(LayoutKind.Sequential)] | ||
[SuppressMessage( | ||
"StyleCop.CSharp.SpacingRules", | ||
"SA1023:Dereference and access of symbols should be spaced correctly", | ||
Justification = "Wtf")] | ||
internal unsafe struct UnknownComBase | ||
{ | ||
private static readonly nint[] Vtbl; | ||
|
||
private void* pVtbl; | ||
private void* pObject; | ||
private uint* pRefCount; | ||
private delegate*<void*, in Guid, void*> pfnDynamicCast; | ||
private delegate*<void*, void> pfnFinalRelease; | ||
|
||
static UnknownComBase() | ||
{ | ||
Vtbl = GC.AllocateArray<nint>(3, true); | ||
Vtbl[0] = (nint)(delegate*<UnknownComBase*, Guid*, void**, HRESULT>)&StaticQueryInterface; | ||
Vtbl[1] = (nint)(delegate*<UnknownComBase*, uint>)&StaticAddRef; | ||
Vtbl[2] = (nint)(delegate*<UnknownComBase*, uint>)&StaticRelease; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UnknownComBase"/> struct. | ||
/// </summary> | ||
/// <param name="pObject">A pointer to the parent struct.</param> | ||
/// <param name="pRefCount">A pointer to the reference counter.</param> | ||
/// <param name="pfnDynamicCast">Dynamic cast function. Return null to <see cref="E.E_NOINTERFACE"/>. Do not change refcount.</param> | ||
/// <param name="pfnFinalRelease">Final release function.</param> | ||
public UnknownComBase( | ||
void* pObject, | ||
uint* pRefCount, | ||
delegate*<void*, in Guid, void*> pfnDynamicCast, | ||
delegate*<void*, void> pfnFinalRelease = null) | ||
{ | ||
this.pVtbl = Unsafe.AsPointer(ref Vtbl[0]); | ||
this.pObject = pObject; | ||
this.pRefCount = pRefCount; | ||
this.pfnDynamicCast = pfnDynamicCast; | ||
this.pfnFinalRelease = pfnFinalRelease; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the address of this struct as a pointer to <see cref="IUnknown"/>. | ||
/// </summary> | ||
public IUnknown* AsPunk => (IUnknown*)Unsafe.AsPointer(ref this); | ||
|
||
private static HRESULT StaticQueryInterface(UnknownComBase* self, Guid* piid, void** ppvObject) | ||
{ | ||
if (ppvObject == null) | ||
return E.E_POINTER; | ||
|
||
*ppvObject = null; | ||
if (piid == null) | ||
return E.E_INVALIDARG; | ||
|
||
if (*piid == IID.IID_IUnknown) | ||
{ | ||
StaticAddRef(self); | ||
*ppvObject = self; | ||
return S.S_OK; | ||
} | ||
|
||
if ((*ppvObject = self->pfnDynamicCast(self->pObject, *piid)) is null) | ||
return E.E_NOINTERFACE; | ||
|
||
StaticAddRef(self); | ||
return S.S_OK; | ||
} | ||
|
||
private static uint StaticAddRef(UnknownComBase* self) => Interlocked.Increment(ref *self->pRefCount); | ||
|
||
private static uint StaticRelease(UnknownComBase* self) | ||
{ | ||
var r = Interlocked.Decrement(ref *self->pRefCount); | ||
if (r > 0) | ||
return r; | ||
if (self->pfnFinalRelease is not null) | ||
self->pfnFinalRelease(self->pObject); | ||
Marshal.FreeHGlobal((nint)self->pObject); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Dalamud.ImGuiScene; | ||
|
||
/// <summary> | ||
/// Represents a handle to an immutable texture pipeline. | ||
/// </summary> | ||
public interface ITexturePipelineWrap : ICloneable, IDisposable | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether this instance of <see cref="ITexturePipelineWrap"/> has been disposed. | ||
/// </summary> | ||
bool IsDisposed { get; } | ||
|
||
/// <inheritdoc cref="ICloneable.Clone"/> | ||
new ITexturePipelineWrap Clone(); | ||
|
||
/// <inheritdoc cref="ICloneable.Clone"/> | ||
object ICloneable.Clone() => this.Clone(); | ||
} |
Oops, something went wrong.