BsManagedComponent.cpp 8.4 KB

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