BsScriptObject.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsScriptEnginePrerequisites.h"
  5. #include "BsScriptMeta.h"
  6. #include "BsException.h"
  7. #include "BsMonoManager.h"
  8. #include "BsMonoField.h"
  9. #include "BsMonoClass.h"
  10. #include <mono/jit/jit.h>
  11. namespace BansheeEngine
  12. {
  13. /** @addtogroup SBansheeEngine
  14. * @{
  15. */
  16. /** Contains backed up interop object data. */
  17. struct ScriptObjectBackup
  18. {
  19. Any data;
  20. };
  21. /** Helper class to initialize all script interop objects as soon as the library is loaded. */
  22. template <class Type, class Base>
  23. struct InitScriptObjectOnStart
  24. {
  25. public:
  26. InitScriptObjectOnStart()
  27. {
  28. ScriptObject<Type, Base>::_initMetaData();
  29. }
  30. };
  31. /**
  32. * Base class for all script interop objects. Interop objects form a connection between C++ and CLR classes and methods.
  33. */
  34. class BS_SCR_BE_EXPORT ScriptObjectBase
  35. {
  36. public:
  37. ScriptObjectBase(MonoObject* instance);
  38. virtual ~ScriptObjectBase();
  39. /** Gets the managed object this interop object represents. */
  40. MonoObject* getManagedInstance() const { return mManagedInstance; }
  41. /**
  42. * Should the interop object persist through assembly reload. If false then the interop object will be destroyed on
  43. * reload.
  44. */
  45. virtual bool isPersistent() const { return false; }
  46. /**
  47. * Clears any managed instance references from the interop object. Normally called right after the assemblies are
  48. * unloaded.
  49. */
  50. virtual void _clearManagedInstance() { }
  51. /** Allows persistent objects to restore their managed instances after assembly reload. */
  52. virtual void _restoreManagedInstance() { }
  53. /** Called when the managed instance gets finalized by the CLR. */
  54. virtual void _onManagedInstanceDeleted();
  55. /** Called before assembly reload starts to give the object a chance to back up its data. */
  56. virtual ScriptObjectBackup beginRefresh();
  57. /**
  58. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  59. * beginRefresh() call.
  60. */
  61. virtual void endRefresh(const ScriptObjectBackup& data);
  62. protected:
  63. MonoObject* mManagedInstance;
  64. };
  65. /** Base class for all persistent interop objects. Persistent objects persist through assembly reload. */
  66. class BS_SCR_BE_EXPORT PersistentScriptObjectBase : public ScriptObjectBase
  67. {
  68. public:
  69. PersistentScriptObjectBase(MonoObject* instance);
  70. virtual ~PersistentScriptObjectBase();
  71. /** @copydoc ScriptObjectBase::isPersistent */
  72. virtual bool isPersistent() const override { return true; }
  73. };
  74. /** Template version of ScriptObjectBase populates the object meta-data on library load. */
  75. template <class Type, class Base = ScriptObjectBase>
  76. class ScriptObject : public Base
  77. {
  78. public:
  79. ScriptObject(MonoObject* instance)
  80. :Base(instance)
  81. {
  82. Type* param = (Type*)(Base*)this; // Needed due to multiple inheritance. Safe since Type must point to an class derived from this one.
  83. if(metaData.thisPtrField != nullptr)
  84. metaData.thisPtrField->setValue(instance, &param);
  85. }
  86. virtual ~ScriptObject()
  87. { }
  88. /**
  89. * Clears any managed instance references from the interop object. Normally called right after the assemblies are
  90. * unloaded.
  91. */
  92. void _clearManagedInstance()
  93. {
  94. if (metaData.thisPtrField != nullptr && this->mManagedInstance != nullptr)
  95. metaData.thisPtrField->setValue(this->mManagedInstance, nullptr);
  96. this->mManagedInstance = nullptr;
  97. }
  98. /** Allows persistent objects to restore their managed instances after assembly reload. */
  99. void _restoreManagedInstance()
  100. {
  101. this->mManagedInstance = _createManagedInstance(true);
  102. Type* param = (Type*)(Base*)this; // Needed due to multiple inheritance. Safe since Type must point to an class derived from this one.
  103. if (metaData.thisPtrField != nullptr && this->mManagedInstance != nullptr)
  104. metaData.thisPtrField->setValue(this->mManagedInstance, &param);
  105. }
  106. /** Creates a new managed instance of the type wrapped by this interop object. */
  107. virtual MonoObject* _createManagedInstance(bool construct)
  108. {
  109. return metaData.scriptClass->createInstance(construct);
  110. }
  111. /**
  112. * Converts a managed instance into a specific interop object. Caller must ensure the managed instance is of the
  113. * proper type.
  114. */
  115. static Type* toNative(MonoObject* managedInstance)
  116. {
  117. Type* nativeInstance = nullptr;
  118. if (metaData.thisPtrField != nullptr && managedInstance != nullptr)
  119. metaData.thisPtrField->getValue(managedInstance, &nativeInstance);
  120. return nativeInstance;
  121. }
  122. /** Returns the meta-data containing class and method information for the managed type. */
  123. static const ScriptMeta* getMetaData() { return &metaData; }
  124. /**
  125. * Initializes the meta-data containing class and method information for the managed type. Called on library load
  126. * and on assembly reload.
  127. */
  128. static void _initMetaData()
  129. {
  130. metaData = ScriptMeta(Type::getAssemblyName(), Type::getNamespace(), Type::getTypeName(), &Type::initRuntimeData);
  131. MonoManager::registerScriptType(&metaData);
  132. }
  133. protected:
  134. static ScriptMeta metaData;
  135. private:
  136. static volatile InitScriptObjectOnStart<Type, Base> initOnStart;
  137. };
  138. template <typename Type, typename Base>
  139. volatile InitScriptObjectOnStart<Type, Base> ScriptObject<Type, Base>::initOnStart;
  140. template <typename Type, typename Base>
  141. ScriptMeta ScriptObject<Type, Base>::metaData;
  142. /** Helper macro to use with script interop objects that form a link between C++ and CLR. */
  143. #define SCRIPT_OBJ(assembly, namespace, name) \
  144. static String getAssemblyName() { return assembly; } \
  145. static String getNamespace() { return namespace; } \
  146. static String getTypeName() { return name; } \
  147. static void initRuntimeData();
  148. /** @} */
  149. }