forked from jacksondunstan/UnityNativeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindings.cpp
More file actions
411 lines (352 loc) · 10.9 KB
/
Bindings.cpp
File metadata and controls
411 lines (352 loc) · 10.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/// <summary>
/// Internals of the bindings between native and .NET code.
/// Game code shouldn't go here.
/// </summary>
/// <author>
/// Jackson Dunstan, 2017, http://JacksonDunstan.com
/// </author>
/// <license>
/// MIT
/// </license>
// Type definitions
#include "Bindings.h"
// For assert()
#include <assert.h>
// For int32_t, etc.
#include <stdint.h>
// For malloc(), etc.
#include <stdlib.h>
// For std::forward
#include <utility>
// Macro to put before functions that need to be exposed to C#
#ifdef _WIN32
#define DLLEXPORT extern "C" __declspec(dllexport)
#else
#define DLLEXPORT extern "C"
#endif
////////////////////////////////////////////////////////////////
// C# functions for C++ to call
////////////////////////////////////////////////////////////////
namespace Plugin
{
void (*ReleaseObject)(int32_t handle);
int32_t (*StringNew)(const char* chars);
/*BEGIN FUNCTION POINTERS*/
int32_t (*StopwatchConstructor)();
int64_t (*StopwatchPropertyGetElapsedMilliseconds)(int32_t thisHandle);
void (*StopwatchMethodStart)(int32_t thisHandle);
void (*StopwatchMethodReset)(int32_t thisHandle);
int32_t (*ObjectPropertyGetName)(int32_t thisHandle);
void (*ObjectPropertySetName)(int32_t thisHandle, int32_t valueHandle);
int32_t (*GameObjectConstructor)();
int32_t (*GameObjectConstructorSystemString)(int32_t nameHandle);
int32_t (*GameObjectPropertyGetTransform)(int32_t thisHandle);
int32_t (*GameObjectMethodFindSystemString)(int32_t nameHandle);
int32_t (*GameObjectMethodAddComponentMyGameMonoBehavioursTestScript)(int32_t thisHandle);
int32_t (*ComponentPropertyGetTransform)(int32_t thisHandle);
UnityEngine::Vector3 (*TransformPropertyGetPosition)(int32_t thisHandle);
void (*TransformPropertySetPosition)(int32_t thisHandle, UnityEngine::Vector3 value);
void (*DebugMethodLogSystemObject)(int32_t messageHandle);
System::Boolean (*AssertFieldGetRaiseExceptions)();
void (*AssertFieldSetRaiseExceptions)(System::Boolean value);
/*END FUNCTION POINTERS*/
}
////////////////////////////////////////////////////////////////
// Reference counting of managed objects
////////////////////////////////////////////////////////////////
namespace Plugin
{
int32_t managedObjectsRefCountLen;
int32_t* managedObjectRefCounts;
void ReferenceManagedObject(int32_t handle)
{
assert(handle >= 0 && handle < managedObjectsRefCountLen);
if (handle != 0)
{
managedObjectRefCounts[handle]++;
}
}
void DereferenceManagedObject(int32_t handle)
{
assert(handle >= 0 && handle < managedObjectsRefCountLen);
if (handle != 0)
{
int32_t numRemain = --managedObjectRefCounts[handle];
if (numRemain == 0)
{
ReleaseObject(handle);
}
}
}
}
////////////////////////////////////////////////////////////////
// Mirrors of C# types. These wrap the C# functions to present
// a similiar API as in C#.
////////////////////////////////////////////////////////////////
namespace System
{
Object::Object(int32_t handle)
{
Handle = handle;
Plugin::ReferenceManagedObject(handle);
}
Object::Object(const Object& other)
{
Handle = other.Handle;
Plugin::ReferenceManagedObject(Handle);
}
Object::Object(Object&& other)
{
Handle = other.Handle;
other.Handle = 0;
}
#define SYSTEM_OBJECT_LIFECYCLE_DEFINITION(ClassName, BaseClassName) \
ClassName::ClassName(int32_t handle) \
: BaseClassName(handle) \
{ \
} \
\
ClassName::ClassName(const ClassName& other) \
: BaseClassName(other) \
{ \
} \
\
ClassName::ClassName(ClassName&& other) \
: BaseClassName(std::forward<ClassName>(other)) \
{ \
} \
\
ClassName::~ClassName() \
{ \
Plugin::DereferenceManagedObject(Handle); \
} \
\
ClassName& ClassName::operator=(const ClassName& other) \
{ \
Plugin::DereferenceManagedObject(Handle); \
Handle = other.Handle; \
Plugin::ReferenceManagedObject(Handle); \
return *this; \
} \
\
ClassName& ClassName::operator=(ClassName&& other) \
{ \
Plugin::DereferenceManagedObject(Handle); \
Handle = other.Handle; \
other.Handle = 0; \
return *this; \
}
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(String, System::Object)
String::String(const char* chars)
: String(Plugin::StringNew(chars))
{
}
}
/*BEGIN METHOD DEFINITIONS*/
namespace System
{
namespace Diagnostics
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Stopwatch, System::Object)
Stopwatch::Stopwatch()
: Stopwatch(Stopwatch(Plugin::StopwatchConstructor()))
{
}
int64_t Stopwatch::GetElapsedMilliseconds()
{
return Plugin::StopwatchPropertyGetElapsedMilliseconds(Handle);
}
void Stopwatch::Start()
{
Plugin::StopwatchMethodStart(Handle);
}
void Stopwatch::Reset()
{
Plugin::StopwatchMethodReset(Handle);
}
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Object, System::Object)
System::String Object::GetName()
{
return System::String(Plugin::ObjectPropertyGetName(Handle));
}
void Object::SetName(System::String value)
{
Plugin::ObjectPropertySetName(Handle, value.Handle);
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(GameObject, UnityEngine::Object)
GameObject::GameObject()
: GameObject(GameObject(Plugin::GameObjectConstructor()))
{
}
GameObject::GameObject(System::String name)
: GameObject(GameObject(Plugin::GameObjectConstructorSystemString(name.Handle)))
{
}
UnityEngine::Transform GameObject::GetTransform()
{
return UnityEngine::Transform(Plugin::GameObjectPropertyGetTransform(Handle));
}
UnityEngine::GameObject GameObject::Find(System::String name)
{
return UnityEngine::GameObject(Plugin::GameObjectMethodFindSystemString(name.Handle));
}
template<> MyGame::MonoBehaviours::TestScript GameObject::AddComponent<MyGame::MonoBehaviours::TestScript>()
{
return MyGame::MonoBehaviours::TestScript(Plugin::GameObjectMethodAddComponentMyGameMonoBehavioursTestScript(Handle));
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Component, UnityEngine::Object)
UnityEngine::Transform Component::GetTransform()
{
return UnityEngine::Transform(Plugin::ComponentPropertyGetTransform(Handle));
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Transform, UnityEngine::Component)
UnityEngine::Vector3 Transform::GetPosition()
{
return Plugin::TransformPropertyGetPosition(Handle);
}
void Transform::SetPosition(UnityEngine::Vector3 value)
{
Plugin::TransformPropertySetPosition(Handle, value);
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Debug, System::Object)
void Debug::Log(System::Object message)
{
Plugin::DebugMethodLogSystemObject(message.Handle);
}
}
namespace UnityEngine
{
namespace Assertions
{
System::Boolean Assert::GetRaiseExceptions()
{
return Plugin::AssertFieldGetRaiseExceptions();
}
void Assert::SetRaiseExceptions(System::Boolean value)
{
Plugin::AssertFieldSetRaiseExceptions(value);
}
}
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Collision, System::Object)
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Behaviour, UnityEngine::Component)
}
namespace UnityEngine
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(MonoBehaviour, UnityEngine::Behaviour)
}
namespace MyGame
{
namespace MonoBehaviours
{
SYSTEM_OBJECT_LIFECYCLE_DEFINITION(TestScript, UnityEngine::MonoBehaviour)
}
}
/*END METHOD DEFINITIONS*/
////////////////////////////////////////////////////////////////
// App-specific functions for this file to call
////////////////////////////////////////////////////////////////
// Called when the plugin is initialized
extern void PluginMain();
////////////////////////////////////////////////////////////////
// C++ functions for C# to call
////////////////////////////////////////////////////////////////
// Init the plugin
DLLEXPORT void Init(
int32_t maxManagedObjects,
void (*releaseObject)(int32_t handle),
int32_t (*stringNew)(const char* chars),
/*BEGIN INIT PARAMS*/
int32_t (*stopwatchConstructor)(),
int64_t (*stopwatchPropertyGetElapsedMilliseconds)(int32_t thisHandle),
void (*stopwatchMethodStart)(int32_t thisHandle),
void (*stopwatchMethodReset)(int32_t thisHandle),
int32_t (*objectPropertyGetName)(int32_t thisHandle),
void (*objectPropertySetName)(int32_t thisHandle, int32_t valueHandle),
int32_t (*gameObjectConstructor)(),
int32_t (*gameObjectConstructorSystemString)(int32_t nameHandle),
int32_t (*gameObjectPropertyGetTransform)(int32_t thisHandle),
int32_t (*gameObjectMethodFindSystemString)(int32_t nameHandle),
int32_t (*gameObjectMethodAddComponentMyGameMonoBehavioursTestScript)(int32_t thisHandle),
int32_t (*componentPropertyGetTransform)(int32_t thisHandle),
UnityEngine::Vector3 (*transformPropertyGetPosition)(int32_t thisHandle),
void (*transformPropertySetPosition)(int32_t thisHandle, UnityEngine::Vector3 value),
void (*debugMethodLogSystemObject)(int32_t messageHandle),
System::Boolean (*assertFieldGetRaiseExceptions)(),
void (*assertFieldSetRaiseExceptions)(System::Boolean value)
/*END INIT PARAMS*/)
{
using namespace Plugin;
// Init managed object ref counting
managedObjectsRefCountLen = maxManagedObjects;
managedObjectRefCounts = (int32_t*)calloc(
maxManagedObjects,
sizeof(int32_t));
// Init pointers to C# functions
StringNew = stringNew;
ReleaseObject = releaseObject;
/*BEGIN INIT BODY*/
StopwatchConstructor = stopwatchConstructor;
StopwatchPropertyGetElapsedMilliseconds = stopwatchPropertyGetElapsedMilliseconds;
StopwatchMethodStart = stopwatchMethodStart;
StopwatchMethodReset = stopwatchMethodReset;
ObjectPropertyGetName = objectPropertyGetName;
ObjectPropertySetName = objectPropertySetName;
GameObjectConstructor = gameObjectConstructor;
GameObjectConstructorSystemString = gameObjectConstructorSystemString;
GameObjectPropertyGetTransform = gameObjectPropertyGetTransform;
GameObjectMethodFindSystemString = gameObjectMethodFindSystemString;
GameObjectMethodAddComponentMyGameMonoBehavioursTestScript = gameObjectMethodAddComponentMyGameMonoBehavioursTestScript;
ComponentPropertyGetTransform = componentPropertyGetTransform;
TransformPropertyGetPosition = transformPropertyGetPosition;
TransformPropertySetPosition = transformPropertySetPosition;
DebugMethodLogSystemObject = debugMethodLogSystemObject;
AssertFieldGetRaiseExceptions = assertFieldGetRaiseExceptions;
AssertFieldSetRaiseExceptions = assertFieldSetRaiseExceptions;
/*END INIT BODY*/
PluginMain();
}
/*BEGIN MONOBEHAVIOUR MESSAGES*/
DLLEXPORT void TestScriptAwake(int32_t thisHandle)
{
MyGame::MonoBehaviours::TestScript thiz(thisHandle);
thiz.Awake();
}
DLLEXPORT void TestScriptOnAnimatorIK(int32_t thisHandle, int32_t param0)
{
MyGame::MonoBehaviours::TestScript thiz(thisHandle);
thiz.OnAnimatorIK(param0);
}
DLLEXPORT void TestScriptOnCollisionEnter(int32_t thisHandle, int32_t param0Handle)
{
MyGame::MonoBehaviours::TestScript thiz(thisHandle);
UnityEngine::Collision param0(param0Handle);
thiz.OnCollisionEnter(param0);
}
DLLEXPORT void TestScriptUpdate(int32_t thisHandle)
{
MyGame::MonoBehaviours::TestScript thiz(thisHandle);
thiz.Update();
}
/*END MONOBEHAVIOUR MESSAGES*/