BsScriptObject.h 5.8 KB

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