BsScriptObject.h 5.9 KB

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