/
NativeInvoke.cs
77 lines (66 loc) · 2.44 KB
/
NativeInvoke.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
#if XENKO_PLATFORM_IOS
using ObjCRuntime;
#endif
namespace Xenko.Core.Native
{
public static class NativeInvoke
{
#if XENKO_PLATFORM_IOS
internal const string Library = "__Internal";
internal const string LibraryName = "libcore.so";
#else
internal const string Library = "libcore";
#if XENKO_PLATFORM_WINDOWS
internal const string LibraryName = "libcore.dll";
#else
internal const string LibraryName = "libcore.so";
#endif
#endif
static NativeInvoke()
{
NativeLibrary.PreloadLibrary(LibraryName, typeof(NativeInvoke).Assembly);
}
/// <summary>
/// Suspends current thread for <paramref name="ms"/> milliseconds.
/// </summary>
/// <param name="ms">Number of milliseconds to sleep.</param>
[SuppressUnmanagedCodeSecurity]
[DllImport(Library, EntryPoint = "cnSleep", CallingConvention = CallingConvention.Cdecl)]
public static extern void Sleep(int ms);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void ManagedLogDelegate(string log);
private static ManagedLogDelegate managedLogDelegateSingleton;
#if XENKO_PLATFORM_IOS
[MonoPInvokeCallback(typeof(ManagedLogDelegate))]
#endif
private static void ManagedLog(string log)
{
Debug.WriteLine(log);
}
public static void Setup()
{
managedLogDelegateSingleton = ManagedLog;
#if !XENKO_PLATFORM_IOS
var ptr = Marshal.GetFunctionPointerForDelegate(managedLogDelegateSingleton);
#else
var ptr = managedLogDelegateSingleton;
#endif
CoreNativeSetup(ptr);
}
#if !XENKO_PLATFORM_IOS
[SuppressUnmanagedCodeSecurity]
[DllImport(Library, EntryPoint = "cnSetup", CallingConvention = CallingConvention.Cdecl)]
private static extern void CoreNativeSetup(IntPtr logger);
#else
[SuppressUnmanagedCodeSecurity]
[DllImport(Library, EntryPoint = "cnSetup", CallingConvention = CallingConvention.Cdecl)]
private static extern void CoreNativeSetup(ManagedLogDelegate logger);
#endif
}
}