BsScriptObject.h 5.5 KB

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