|
| 1 | +/// <summary> |
| 2 | +/// Internals of the bindings between native and .NET code. |
| 3 | +/// Game code shouldn't go here. |
| 4 | +/// </summary> |
| 5 | +/// <author> |
| 6 | +/// Jackson Dunstan, 2017, http://JacksonDunstan.com |
| 7 | +/// </author> |
| 8 | +/// <license> |
| 9 | +/// MIT |
| 10 | +/// </license> |
| 11 | + |
| 12 | +// Type definitions |
| 13 | +#include "Bindings.h" |
| 14 | + |
| 15 | +// For assert() |
| 16 | +#include <assert.h> |
| 17 | + |
| 18 | +// For int32_t, etc. |
| 19 | +#include <stdint.h> |
| 20 | + |
| 21 | +// For malloc(), etc. |
| 22 | +#include <stdlib.h> |
| 23 | + |
| 24 | +// For std::forward |
| 25 | +#include <utility> |
| 26 | + |
| 27 | +// Macro to put before functions that need to be exposed to C# |
| 28 | +#ifdef _WIN32 |
| 29 | + #define DLLEXPORT extern "C" __declspec(dllexport) |
| 30 | +#else |
| 31 | + #define DLLEXPORT extern "C" |
| 32 | +#endif |
| 33 | + |
| 34 | +//////////////////////////////////////////////////////////////// |
| 35 | +// C# functions for C++ to call |
| 36 | +//////////////////////////////////////////////////////////////// |
| 37 | + |
| 38 | +namespace Plugin |
| 39 | +{ |
| 40 | + void (*ReleaseObject)(int32_t handle); |
| 41 | + |
| 42 | + int32_t (*StringNew)(const char* chars); |
| 43 | + |
| 44 | + /*BEGIN FUNCTION POINTERS*/ |
| 45 | + int32_t (*ObjectPropertyGetName)(int32_t thisHandle); |
| 46 | + |
| 47 | + void (*ObjectPropertySetName)(int32_t thisHandle, int32_t valueHandle); |
| 48 | + |
| 49 | + int32_t (*GameObjectConstructor)(); |
| 50 | + |
| 51 | + int32_t (*GameObjectPropertyGetTransform)(int32_t thisHandle); |
| 52 | + |
| 53 | + int32_t (*GameObjectMethodFindSystemString)(int32_t nameHandle); |
| 54 | + |
| 55 | + int32_t (*ComponentPropertyGetTransform)(int32_t thisHandle); |
| 56 | + |
| 57 | + UnityEngine::Vector3 (*TransformPropertyGetPosition)(int32_t thisHandle); |
| 58 | + |
| 59 | + void (*TransformPropertySetPosition)(int32_t thisHandle, UnityEngine::Vector3 value); |
| 60 | + |
| 61 | + void (*DebugMethodLogSystemObject)(int32_t messageHandle); |
| 62 | + |
| 63 | + System::Boolean (*AssertFieldGetRaiseExceptions)(); |
| 64 | + |
| 65 | + void (*AssertFieldSetRaiseExceptions)(System::Boolean value); |
| 66 | + /*END FUNCTION POINTERS*/ |
| 67 | +} |
| 68 | + |
| 69 | +//////////////////////////////////////////////////////////////// |
| 70 | +// Reference counting of managed objects |
| 71 | +//////////////////////////////////////////////////////////////// |
| 72 | + |
| 73 | +namespace Plugin |
| 74 | +{ |
| 75 | + int32_t managedObjectsRefCountLen; |
| 76 | + int32_t* managedObjectRefCounts; |
| 77 | + |
| 78 | + void ReferenceManagedObject(int32_t handle) |
| 79 | + { |
| 80 | + assert(handle >= 0 && handle < managedObjectsRefCountLen); |
| 81 | + if (handle != 0) |
| 82 | + { |
| 83 | + managedObjectRefCounts[handle]++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void DereferenceManagedObject(int32_t handle) |
| 88 | + { |
| 89 | + assert(handle >= 0 && handle < managedObjectsRefCountLen); |
| 90 | + if (handle != 0) |
| 91 | + { |
| 92 | + int32_t numRemain = --managedObjectRefCounts[handle]; |
| 93 | + if (numRemain == 0) |
| 94 | + { |
| 95 | + ReleaseObject(handle); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +//////////////////////////////////////////////////////////////// |
| 102 | +// Mirrors of C# types. These wrap the C# functions to present |
| 103 | +// a similiar API as in C#. |
| 104 | +//////////////////////////////////////////////////////////////// |
| 105 | + |
| 106 | +namespace System |
| 107 | +{ |
| 108 | + Object::Object(int32_t handle) |
| 109 | + { |
| 110 | + Handle = handle; |
| 111 | + Plugin::ReferenceManagedObject(handle); |
| 112 | + } |
| 113 | + |
| 114 | + Object::Object(const Object& other) |
| 115 | + { |
| 116 | + Handle = other.Handle; |
| 117 | + Plugin::ReferenceManagedObject(Handle); |
| 118 | + } |
| 119 | + |
| 120 | + Object::Object(Object&& other) |
| 121 | + { |
| 122 | + Handle = other.Handle; |
| 123 | + other.Handle = 0; |
| 124 | + } |
| 125 | + |
| 126 | + #define SYSTEM_OBJECT_LIFECYCLE_DEFINITION(ClassName, BaseClassName) \ |
| 127 | + ClassName::ClassName(int32_t handle) \ |
| 128 | + : BaseClassName(handle) \ |
| 129 | + { \ |
| 130 | + } \ |
| 131 | + \ |
| 132 | + ClassName::ClassName(const ClassName& other) \ |
| 133 | + : BaseClassName(other) \ |
| 134 | + { \ |
| 135 | + } \ |
| 136 | + \ |
| 137 | + ClassName::ClassName(ClassName&& other) \ |
| 138 | + : BaseClassName(std::forward<ClassName>(other)) \ |
| 139 | + { \ |
| 140 | + } \ |
| 141 | + \ |
| 142 | + ClassName::~ClassName() \ |
| 143 | + { \ |
| 144 | + Plugin::DereferenceManagedObject(Handle); \ |
| 145 | + } \ |
| 146 | + \ |
| 147 | + ClassName& ClassName::operator=(const ClassName& other) \ |
| 148 | + { \ |
| 149 | + Plugin::DereferenceManagedObject(Handle); \ |
| 150 | + Handle = other.Handle; \ |
| 151 | + Plugin::ReferenceManagedObject(Handle); \ |
| 152 | + return *this; \ |
| 153 | + } \ |
| 154 | + \ |
| 155 | + ClassName& ClassName::operator=(ClassName&& other) \ |
| 156 | + { \ |
| 157 | + Plugin::DereferenceManagedObject(Handle); \ |
| 158 | + Handle = other.Handle; \ |
| 159 | + other.Handle = 0; \ |
| 160 | + return *this; \ |
| 161 | + } |
| 162 | + |
| 163 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(String, System::Object) |
| 164 | + |
| 165 | + String::String(const char* chars) |
| 166 | + : String(Plugin::StringNew(chars)) |
| 167 | + { |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/*BEGIN METHOD DEFINITIONS*/ |
| 172 | +namespace UnityEngine |
| 173 | +{ |
| 174 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Object, System::Object) |
| 175 | + |
| 176 | + System::String Object::GetName() |
| 177 | + { |
| 178 | + return System::String(Plugin::ObjectPropertyGetName(Handle)); |
| 179 | + } |
| 180 | + |
| 181 | + void Object::SetName(System::String value) |
| 182 | + { |
| 183 | + Plugin::ObjectPropertySetName(Handle, value.Handle); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +namespace UnityEngine |
| 188 | +{ |
| 189 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(GameObject, UnityEngine::Object) |
| 190 | + |
| 191 | + GameObject::GameObject() |
| 192 | + : GameObject(GameObject(Plugin::GameObjectConstructor())) |
| 193 | + { |
| 194 | + } |
| 195 | + |
| 196 | + UnityEngine::Transform GameObject::GetTransform() |
| 197 | + { |
| 198 | + return UnityEngine::Transform(Plugin::GameObjectPropertyGetTransform(Handle)); |
| 199 | + } |
| 200 | + |
| 201 | + UnityEngine::GameObject GameObject::Find(System::String name) |
| 202 | + { |
| 203 | + return UnityEngine::GameObject(Plugin::GameObjectMethodFindSystemString(name.Handle)); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +namespace UnityEngine |
| 208 | +{ |
| 209 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Component, UnityEngine::Object) |
| 210 | + |
| 211 | + UnityEngine::Transform Component::GetTransform() |
| 212 | + { |
| 213 | + return UnityEngine::Transform(Plugin::ComponentPropertyGetTransform(Handle)); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +namespace UnityEngine |
| 218 | +{ |
| 219 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Transform, UnityEngine::Component) |
| 220 | + |
| 221 | + UnityEngine::Vector3 Transform::GetPosition() |
| 222 | + { |
| 223 | + return Plugin::TransformPropertyGetPosition(Handle); |
| 224 | + } |
| 225 | + |
| 226 | + void Transform::SetPosition(UnityEngine::Vector3 value) |
| 227 | + { |
| 228 | + Plugin::TransformPropertySetPosition(Handle, value); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +namespace UnityEngine |
| 233 | +{ |
| 234 | + SYSTEM_OBJECT_LIFECYCLE_DEFINITION(Debug, System::Object) |
| 235 | + |
| 236 | + void Debug::Log(System::Object message) |
| 237 | + { |
| 238 | + Plugin::DebugMethodLogSystemObject(message.Handle); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +namespace UnityEngine |
| 243 | +{ |
| 244 | + namespace Assertions |
| 245 | + { |
| 246 | + System::Boolean Assert::GetRaiseExceptions() |
| 247 | + { |
| 248 | + return Plugin::AssertFieldGetRaiseExceptions(); |
| 249 | + } |
| 250 | + |
| 251 | + void Assert::SetRaiseExceptions(System::Boolean value) |
| 252 | + { |
| 253 | + Plugin::AssertFieldSetRaiseExceptions(value); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
| 257 | +/*END METHOD DEFINITIONS*/ |
| 258 | + |
| 259 | +//////////////////////////////////////////////////////////////// |
| 260 | +// App-specific functions for this file to call |
| 261 | +//////////////////////////////////////////////////////////////// |
| 262 | + |
| 263 | +// Called when the plugin is initialized |
| 264 | +extern void PluginMain(); |
| 265 | + |
| 266 | +// Called for MonoBehaviour.Update |
| 267 | +extern void PluginUpdate(); |
| 268 | + |
| 269 | +//////////////////////////////////////////////////////////////// |
| 270 | +// C++ functions for C# to call |
| 271 | +//////////////////////////////////////////////////////////////// |
| 272 | + |
| 273 | +// Init the plugin |
| 274 | +DLLEXPORT void Init( |
| 275 | + int32_t maxManagedObjects, |
| 276 | + void (*releaseObject)( |
| 277 | + int32_t handle), |
| 278 | + int32_t (*stringNew)( |
| 279 | + const char* chars), |
| 280 | + /*BEGIN INIT PARAMS*/ |
| 281 | + int32_t (*objectPropertyGetName)(int32_t thisHandle), |
| 282 | + void (*objectPropertySetName)(int32_t thisHandle, int32_t valueHandle), |
| 283 | + int32_t (*gameObjectConstructor)(), |
| 284 | + int32_t (*gameObjectPropertyGetTransform)(int32_t thisHandle), |
| 285 | + int32_t (*gameObjectMethodFindSystemString)(int32_t nameHandle), |
| 286 | + int32_t (*componentPropertyGetTransform)(int32_t thisHandle), |
| 287 | + UnityEngine::Vector3 (*transformPropertyGetPosition)(int32_t thisHandle), |
| 288 | + void (*transformPropertySetPosition)(int32_t thisHandle, UnityEngine::Vector3 value), |
| 289 | + void (*debugMethodLogSystemObject)(int32_t messageHandle), |
| 290 | + System::Boolean (*assertFieldGetRaiseExceptions)(), |
| 291 | + void (*assertFieldSetRaiseExceptions)(System::Boolean value) |
| 292 | + /*END INIT PARAMS*/) |
| 293 | +{ |
| 294 | + using namespace Plugin; |
| 295 | + |
| 296 | + // Init managed object ref counting |
| 297 | + managedObjectsRefCountLen = maxManagedObjects; |
| 298 | + managedObjectRefCounts = (int32_t*)calloc( |
| 299 | + maxManagedObjects, |
| 300 | + sizeof(int32_t)); |
| 301 | + |
| 302 | + // Init pointers to C# functions |
| 303 | + StringNew = stringNew; |
| 304 | + ReleaseObject = releaseObject; |
| 305 | + /*BEGIN INIT BODY*/ |
| 306 | + ObjectPropertyGetName = objectPropertyGetName; |
| 307 | + ObjectPropertySetName = objectPropertySetName; |
| 308 | + GameObjectConstructor = gameObjectConstructor; |
| 309 | + GameObjectPropertyGetTransform = gameObjectPropertyGetTransform; |
| 310 | + GameObjectMethodFindSystemString = gameObjectMethodFindSystemString; |
| 311 | + ComponentPropertyGetTransform = componentPropertyGetTransform; |
| 312 | + TransformPropertyGetPosition = transformPropertyGetPosition; |
| 313 | + TransformPropertySetPosition = transformPropertySetPosition; |
| 314 | + DebugMethodLogSystemObject = debugMethodLogSystemObject; |
| 315 | + AssertFieldGetRaiseExceptions = assertFieldGetRaiseExceptions; |
| 316 | + AssertFieldSetRaiseExceptions = assertFieldSetRaiseExceptions; |
| 317 | + /*END INIT BODY*/ |
| 318 | + |
| 319 | + PluginMain(); |
| 320 | +} |
| 321 | + |
| 322 | +// Called by MonoBehaviour.Update |
| 323 | +DLLEXPORT void MonoBehaviourUpdate() |
| 324 | +{ |
| 325 | + PluginUpdate(); |
| 326 | +} |
0 commit comments