Skip to content

Commit

Permalink
Unity: fix log interop by using alternative interop function
Browse files Browse the repository at this point in the history
on android arm64/il2cpp
and on iOS

ref https://code.videolan.org/videolan/vlc-unity/-/issues/243
  • Loading branch information
mfkl committed Feb 4, 2025
1 parent 49a32fa commit 32d45e5
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions src/LibVLCSharp/Helpers/MarshalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ internal readonly struct Native
[DllImport(Constants.LibSystem, EntryPoint = "vasprintf", CallingConvention = CallingConvention.Cdecl)]
public static extern int vasprintf_apple(ref IntPtr buffer, IntPtr format, IntPtr args);

[DllImport(Constants.Libc, EntryPoint = "vasprintf", CallingConvention = CallingConvention.Cdecl)]
public static extern int vasprintf_unix(ref IntPtr buffer, IntPtr format, IntPtr args);

[DllImport(Constants.Libc, EntryPoint = "vsprintf", CallingConvention = CallingConvention.Cdecl)]
public static extern int vsprintf_linux(IntPtr buffer, IntPtr format, IntPtr args);

Expand All @@ -69,33 +72,63 @@ internal readonly struct Native

internal static string GetLogMessage(IntPtr format, IntPtr args)
{
if(PlatformHelper.IsMac)
return AppleLogCallback(format, args);
#if APPLE
return AppleLogCallback(format, args);
#else
if(PlatformHelper.IsMac)
return AppleLogCallback(format, args);

// special marshalling is needed on Linux desktop 64 bits.
if (PlatformHelper.IsLinuxDesktop && PlatformHelper.IsX64BitProcess)
{
return LinuxX64LogCallback(format, args);
}

var byteLength = vsnprintf(IntPtr.Zero, UIntPtr.Zero, format, args) + 1;
if (PlatformHelper.IsWindows)
{
return WindowsLogCallback(format, args);
}

return UnixLogCallback(format, args);
#endif
}

static string WindowsLogCallback(IntPtr format, IntPtr args)
{
var byteLength = Native.vsnprintf_windows(IntPtr.Zero, UIntPtr.Zero, format, args) + 1;
if (byteLength <= 1)
return string.Empty;

var buffer = IntPtr.Zero;
try
{
buffer = Marshal.AllocHGlobal(byteLength);
vsprintf(buffer, format, args);
if (Native.vsprintf_windows(buffer, format, args) < 0)
return string.Empty;
return buffer.FromUtf8()!;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
#endif
}

static string UnixLogCallback(IntPtr format, IntPtr args)
{
var buffer = IntPtr.Zero;
try
{
var count = Native.vasprintf_unix(ref buffer, format, args);

if (count == -1)
return string.Empty;

return buffer.FromUtf8() ?? string.Empty;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}

static string AppleLogCallback(IntPtr format, IntPtr args)
Expand Down Expand Up @@ -173,35 +206,7 @@ static void UseStructurePointer<T>(T structure, Action<IntPtr> action) where T :
}
}

#pragma warning disable IDE1006 // Naming Styles
static int vsnprintf(IntPtr buffer, UIntPtr size, IntPtr format, IntPtr args)
{
#if ANDROID
return Native.vsnprintf_linux(buffer, size, format, args);
#else
if (PlatformHelper.IsWindows)
return Native.vsnprintf_windows(buffer, size, format, args);
else if (PlatformHelper.IsLinux)
return Native.vsnprintf_linux(buffer, size, format, args);
return -1;
#endif
}

static int vsprintf(IntPtr buffer, IntPtr format, IntPtr args)
{
#if ANDROID
return Native.vsprintf_linux(buffer, format, args);
#else
if (PlatformHelper.IsWindows)
return Native.vsprintf_windows(buffer, format, args);
else if (PlatformHelper.IsLinux)
return Native.vsprintf_linux(buffer, format, args);
return -1;
#endif
}

#endregion
#pragma warning restore IDE1006 // Naming Styles
#endregion

/// <summary>
/// Helper for libvlc_new
Expand Down

0 comments on commit 32d45e5

Please sign in to comment.