This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CSharpCallback.cs
180 lines (136 loc) · 6.22 KB
/
CSharpCallback.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using AOT;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class CSharpCallback
{
delegate int ArithmeticOperationDelegate(int a, int b);
[DllImport("__Internal")]
extern static int AddTwoNumbersInNative(int a, int b);
[MonoPInvokeCallback(typeof(ArithmeticOperationDelegate))]
static int SubtractTwoNumbersInManagedCode(int a, int b)
{
return a - b;
}
[DllImport("__Internal")]
extern static int SubtractTwoNumbersUsingInlineCallback(int a, int b, ArithmeticOperationDelegate callback);
[DllImport("__Internal")]
extern static int MultiplyTwoNumbersUsingExportedFunction(int a, int b );
[DllImport("__Internal")]
extern static ArithmeticOperationDelegate GetLateBoundDivison();
[DllImport("__Internal")]
extern static ArithmeticOperationDelegate GetLateBoundSubtraction();
[DllImport("__Internal")]
extern static IntPtr GetLateBoundSubtractionAddress();
[DllImport("__Internal")]
extern static void SetComparisonCallback (ArithmeticOperationDelegate callback);
[MonoPInvokeCallback(typeof(ArithmeticOperationDelegate))]
static int CompareManagedCallback(int a, int b)
{
return a - b;
}
[StructLayout(LayoutKind.Sequential, CharSet= CharSet.Unicode)]
struct MActivationParamsAsReturnValue
{
public bool isFirstActivation;
//Make this IntPtr so it is a blittable struct
public IntPtr commandLinePtr ;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct MActivationParamsAsParam
{
public bool isFirstActivation;
[MarshalAs(UnmanagedType.LPWStr)]
public string commandLine;
public int commandLineBufferSize;
};
[DllImport("__Internal", CallingConvention = CallingConvention.StdCall)]
extern static bool TryGetActivationParams( ref MActivationParamsAsParam p );
[DllImport("__Internal", CallingConvention = CallingConvention.StdCall)]
extern static MActivationParamsAsReturnValue GetActivationParams( );
public static void CallChainFromManaged ( int a, int b )
{
ArithmeticOperationDelegate latebound;
try
{
// Simplest: Just DllImport ...
Log(string.Format("{0} + {1} = {2}", a, b, AddTwoNumbersInNative (a, b)));
// not with a callback..
Log(string.Format("{0} - {1} = {2}", a, b, SubtractTwoNumbersUsingInlineCallback(a, b, SubtractTwoNumbersInManagedCode)));
// This one function is just a DllImport too.. (like Addition)
// but in this case the function can also be called from C++.
Log(string.Format("{0} * {1} = {2}", a, b, MultiplyTwoNumbersUsingExportedFunction(a, b)));
//now we use a latebound function.
// Gotcha here is that this does not do great error handling.. this might throw if the latebound method pointer is not set
latebound = GetLateBoundDivison();
Log(string.Format("{0} / {1} = {2}", a * b, b, latebound(a * b, b)));
MActivationParamsAsParam activationParams = new MActivationParamsAsParam();
activationParams.commandLineBufferSize = 1024;
activationParams.commandLine = new String('\0', activationParams.commandLineBufferSize);
if ( TryGetActivationParams ( ref activationParams ))
{
Log(string.Format("is First: {0}, params:{1}", activationParams.isFirstActivation, activationParams.commandLine));
}
MActivationParamsAsReturnValue activationParams2 = GetActivationParams();
if (activationParams2.commandLinePtr != IntPtr.Zero)
{
string commandLine = Marshal.PtrToStringUni(activationParams2.commandLinePtr);
int targetLevel = 1;
bool isRoomScale = false ;
if ( !TryParseCommandLine ( commandLine ))
{
Log("Failed to parse: " + commandLine );
}
}
else
{
Log("No commandline ");
}
}
catch ( EntryPointNotFoundException eeh )
{
Log(string.Format("{0} was not found. If you are trying to use it in UNITY_EDITOR, use a plugin for this", eeh.Message));
}
// Another approach to late binding.
// This is safer on checks if your function is not guaranteed to exist.
try
{
IntPtr lateboundSubtractionPtr = GetLateBoundSubtractionAddress();
if ( lateboundSubtractionPtr != IntPtr.Zero )
{
ArithmeticOperationDelegate lateboundSubtraction = Marshal.GetDelegateForFunctionPointer(lateboundSubtractionPtr, typeof(ArithmeticOperationDelegate)) as ArithmeticOperationDelegate;
if ( lateboundSubtraction != null )
Log(string.Format("{0} - {1} = {2}", a, b, lateboundSubtraction(a, b)));
}
else
Log("Subtraction was not defined");
}
catch ( Exception ex )
{
Log( ex.Message );
}
SetComparisonCallback(CompareManagedCallback);
}
static bool TryParseCommandLine( string commandLine)
{
//TODO: do real parsing of Uri... here i am just illustrating calling the roomscale APIs ;)
string commandLineLowerCase = commandLine.ToLower();
bool isRoomScale = commandLineLowerCase.Contains("roomscale=true");
if ( isRoomScale )
{
UnityEngine.XR.XRDevice.SetTrackingSpaceType(UnityEngine.XR.TrackingSpaceType.RoomScale);
Log("Setting SpaceType to Roomscale");
}
else
{
UnityEngine.XR.XRDevice.SetTrackingSpaceType(UnityEngine.XR.TrackingSpaceType.Stationary);
}
return true;
}
static void Log ( string s )
{
Debug.Log("***" + s + "***");
}
}