BsManagedComponent.cpp 8.6 KB

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