BsManagedComponent.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "BsManagedComponent.h"
  2. #include "BsManagedComponentRTTI.h"
  3. #include "BsMonoManager.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoUtil.h"
  6. #include "BsMonoMethod.h"
  7. #include "BsMemorySerializer.h"
  8. #include "BsManagedSerializableObject.h"
  9. #include "BsScriptGameObjectManager.h"
  10. #include "BsDebug.h"
  11. namespace BansheeEngine
  12. {
  13. ManagedComponent::ManagedComponent()
  14. :mManagedInstance(nullptr), mUpdateThunk(nullptr), mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr),
  15. mOnResetThunk(nullptr), mMissingType(false), mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr)
  16. { }
  17. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  18. : Component(parent), mManagedInstance(nullptr), mRuntimeType(runtimeType), mUpdateThunk(nullptr),
  19. mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr), mOnResetThunk(nullptr), mMissingType(false),
  20. mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr)
  21. {
  22. MonoType* monoType = mono_reflection_type_get_type(mRuntimeType);
  23. ::MonoClass* monoClass = mono_type_get_class(monoType);
  24. MonoUtil::getClassName(monoClass, mNamespace, mTypeName);
  25. MonoClass* managedClass = MonoManager::instance().findClass(mNamespace, mTypeName);
  26. if(managedClass == nullptr)
  27. {
  28. LOGWRN("Cannot create managed component: " + mNamespace + "." + mTypeName + " because that type doesn't exist.");
  29. return;
  30. }
  31. setName(mTypeName);
  32. initialize(managedClass->createInstance());
  33. }
  34. ManagedComponent::~ManagedComponent()
  35. {
  36. }
  37. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  38. {
  39. ComponentBackupData backupData;
  40. // If type is not missing read data from actual managed instance, instead just
  41. // return the data we backed up before the type was lost
  42. if (!mMissingType)
  43. {
  44. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  45. // Serialize the object information and its fields. We cannot just serialize the entire object because
  46. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  47. if (serializableObject != nullptr)
  48. {
  49. MemorySerializer ms;
  50. backupData.size = 0;
  51. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  52. }
  53. else
  54. {
  55. backupData.size = 0;
  56. backupData.data = nullptr;
  57. }
  58. }
  59. else
  60. {
  61. MemorySerializer ms;
  62. backupData.size = 0;
  63. if (mMissingTypeObjectData != nullptr)
  64. backupData.data = ms.encode(mMissingTypeObjectData.get(), backupData.size);
  65. else
  66. backupData.data = nullptr;
  67. }
  68. if (clearExisting)
  69. {
  70. if (mManagedInstance != nullptr)
  71. {
  72. mManagedInstance = nullptr;
  73. mono_gchandle_free(mManagedHandle);
  74. mManagedHandle = 0;
  75. }
  76. mRuntimeType = nullptr;
  77. mOnInitializedThunk = nullptr;
  78. mUpdateThunk = nullptr;
  79. mOnDestroyThunk = nullptr;
  80. mOnEnabledThunk = nullptr;
  81. mOnDisabledThunk = nullptr;
  82. }
  83. return backupData;
  84. }
  85. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  86. {
  87. initialize(instance);
  88. if (instance != nullptr && data.data != nullptr)
  89. {
  90. MemorySerializer ms;
  91. GameObjectManager::instance().startDeserialization();
  92. ManagedSerializableObjectPtr serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  93. GameObjectManager::instance().endDeserialization();
  94. if (!missingType)
  95. serializableObject->deserialize();
  96. else
  97. mMissingTypeObjectData = serializableObject;
  98. }
  99. if (!missingType)
  100. mMissingTypeObjectData = nullptr;
  101. mMissingType = missingType;
  102. mRequiresReset = true;
  103. }
  104. void ManagedComponent::initialize(MonoObject* object)
  105. {
  106. mFullTypeName = mNamespace + "." + mTypeName;
  107. mManagedInstance = object;
  108. MonoClass* managedClass = nullptr;
  109. if (mManagedInstance != nullptr)
  110. {
  111. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  112. ::MonoClass* monoClass = mono_object_get_class(object);
  113. MonoType* monoType = mono_class_get_type(monoClass);
  114. mRuntimeType = mono_type_get_object(MonoManager::instance().getDomain(), monoType);
  115. managedClass = MonoManager::instance().findClass(monoClass);
  116. }
  117. if (managedClass != nullptr)
  118. {
  119. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  120. if (onInitializedMethod != nullptr)
  121. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  122. MonoMethod* updateMethod = managedClass->getMethod("Update", 0);
  123. if (updateMethod != nullptr)
  124. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  125. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  126. if (onResetMethod != nullptr)
  127. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  128. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  129. if (onDestroyMethod != nullptr)
  130. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  131. MonoMethod* onDisableMethod = managedClass->getMethod("OnDisable", 0);
  132. if (onDisableMethod != nullptr)
  133. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  134. MonoMethod* onEnableMethod = managedClass->getMethod("OnEnable", 0);
  135. if (onEnableMethod != nullptr)
  136. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  137. }
  138. }
  139. void ManagedComponent::update()
  140. {
  141. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  142. {
  143. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  144. // for some extra speed.
  145. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  146. }
  147. }
  148. void ManagedComponent::triggerOnReset()
  149. {
  150. if (mRequiresReset && mOnResetThunk != nullptr && mManagedInstance != nullptr)
  151. {
  152. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  153. // for some extra speed.
  154. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  155. }
  156. mRequiresReset = false;
  157. }
  158. void ManagedComponent::onInitialized()
  159. {
  160. assert(mManagedInstance != nullptr);
  161. // Find handle to self
  162. HManagedComponent componentHandle;
  163. if (mParent != nullptr)
  164. {
  165. const Vector<HComponent>& components = mParent->getComponents();
  166. for (auto& component : components)
  167. {
  168. if (component.get() == this)
  169. {
  170. componentHandle = component;
  171. break;
  172. }
  173. }
  174. }
  175. assert(componentHandle != nullptr);
  176. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  177. if (mOnInitializedThunk != nullptr)
  178. {
  179. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  180. // for some extra speed.
  181. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  182. }
  183. triggerOnReset();
  184. }
  185. void ManagedComponent::onDestroyed()
  186. {
  187. assert(mManagedInstance != nullptr);
  188. if (mOnDestroyThunk != nullptr)
  189. {
  190. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  191. // for some extra speed.
  192. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  193. }
  194. if (mManagedInstance != nullptr)
  195. {
  196. mManagedInstance = nullptr;
  197. mono_gchandle_free(mManagedHandle);
  198. }
  199. }
  200. void ManagedComponent::onEnabled()
  201. {
  202. assert(mManagedInstance != nullptr);
  203. if (mOnEnabledThunk != nullptr)
  204. {
  205. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  206. // for some extra speed.
  207. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  208. }
  209. }
  210. void ManagedComponent::onDisabled()
  211. {
  212. assert(mManagedInstance != nullptr);
  213. if (mOnDisabledThunk != nullptr)
  214. {
  215. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  216. // for some extra speed.
  217. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  218. }
  219. }
  220. RTTITypeBase* ManagedComponent::getRTTIStatic()
  221. {
  222. return ManagedComponentRTTI::instance();
  223. }
  224. RTTITypeBase* ManagedComponent::getRTTI() const
  225. {
  226. return ManagedComponent::getRTTIStatic();
  227. }
  228. }