Skip to content

Commit

Permalink
Fix Guid on non Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 26, 2023
1 parent e75f967 commit f332dce
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/NPlug/Interop/LibVst.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See license.txt file in the project root for full license information.

using System;
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -30,6 +32,24 @@ private static void CopyStringToUTF16(string text, char* dest, int maxLength)
}
}

private static Guid ConvertToPlatform(this in Guid guid)
{
// GUID in VST are identical to the Windows Version
if (OperatingSystem.IsWindows() || !BitConverter.IsLittleEndian) return guid;

// But on non COM Compatible OS, the leading u32, u16, u16, u8[8]
// are big endian instead of little endian, so we need to reverse them
var newGuid = guid;
var pInt0 = (int*)&newGuid;
*pInt0 = BinaryPrimitives.ReverseEndianness(*pInt0);
var pShort1 = (short*)pInt0 + 2;
*pShort1 = BinaryPrimitives.ReverseEndianness(*pShort1);
var pShort2 = (short*)pInt0 + 4;
*pShort2 = BinaryPrimitives.ReverseEndianness(*pShort2);

return newGuid;
}

private static void CopyStringToUTF16(string text, ref String128 str128)
{
str128.CopyFrom(text);
Expand Down
2 changes: 1 addition & 1 deletion src/NPlug/Interop/LibVst.IComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial struct IComponent

private static partial ComResult getControllerClassId_ToManaged(IComponent* self, Guid* classId)
{
*classId = Get(self).ControllerClassId;
*classId = Get(self).ControllerClassId.ConvertToPlatform();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/NPlug/Interop/LibVst.IPluginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static partial int countClasses_ToManaged(IPluginFactory* self)
private static partial ComResult getClassInfo_ToManaged(IPluginFactory* self, int index, PClassInfo* info)
{
var pluginClassInfo = Get(self).GetPluginClassInfo(index);
info->cid = pluginClassInfo.ClassId;
info->cid = pluginClassInfo.ClassId.ConvertToPlatform();
info->cardinality = pluginClassInfo.Cardinality;
CopyStringToUTF8(AudioEffectCategory, info->category, 32);
CopyStringToUTF8(pluginClassInfo.Name, info->name, 64);
Expand All @@ -59,7 +59,7 @@ private static partial ComResult createInstance_ToManaged(IPluginFactory* self,
{
var comResult = false;
*obj = null;
var pluginComponent = Get(self).CreateInstance(*(Guid*)cid.Value);
var pluginComponent = Get(self).CreateInstance((*(Guid*)cid.Value).ConvertToPlatform());
if (pluginComponent != null)
{
var comObject = ComObjectManager.Instance.GetOrCreateComObject(pluginComponent);
Expand Down
2 changes: 1 addition & 1 deletion src/NPlug/Interop/LibVst.IPluginFactory2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial struct IPluginFactory2
private static partial ComResult getClassInfo2_ToManaged(IPluginFactory2* self, int index, PClassInfo2* info)
{
var pluginClassInfo = Get(self).GetPluginClassInfo(index);
info->cid = pluginClassInfo.ClassId;
info->cid = pluginClassInfo.ClassId.ConvertToPlatform();
info->cardinality = pluginClassInfo.Cardinality;
//public fixed byte category[32];
CopyStringToUTF8(GetPluginCategory(pluginClassInfo), info->category, 32);
Expand Down
2 changes: 1 addition & 1 deletion src/NPlug/Interop/LibVst.IPluginFactory3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial struct IPluginFactory3
private static partial ComResult getClassInfoUnicode_ToManaged(IPluginFactory3* self, int index, PClassInfoW* info)
{
var pluginClassInfo = Get(self).GetPluginClassInfo(index);
info->cid = pluginClassInfo.ClassId;
info->cid = pluginClassInfo.ClassId.ConvertToPlatform();
info->cardinality = pluginClassInfo.Cardinality;
//public fixed byte category[32];
CopyStringToUTF8(GetPluginCategory(pluginClassInfo), info->category, 32);
Expand Down
2 changes: 1 addition & 1 deletion src/NPlug/Interop/LibVst.ITestPlugProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static partial ComResult getSubCategories_ToManaged(ITestPlugProvider* s

private static partial ComResult getComponentUID_ToManaged(ITestPlugProvider* self, LibVst.FUID* uid)
{
*((Guid*)uid) = Get(self).GetAudioProcessorClassId();
*((Guid*)uid) = Get(self).GetAudioProcessorClassId().ConvertToPlatform();
return true;
}
}
Expand Down

0 comments on commit f332dce

Please sign in to comment.