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. mNamespace = mono_class_get_namespace(monoClass);
  26. mTypeName = mono_class_get_name(monoClass);
  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. if (mManagedInstance != nullptr)
  39. {
  40. mManagedInstance = nullptr;
  41. mono_gchandle_free(mManagedHandle);
  42. }
  43. }
  44. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  45. {
  46. ComponentBackupData backupData;
  47. // If type is not missing read data from actual managed instance, instead just
  48. // return the data we backed up before the type was lost
  49. if (!mMissingType)
  50. {
  51. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  52. // Serialize the object information and its fields. We cannot just serialize the entire object because
  53. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  54. if (serializableObject != nullptr)
  55. {
  56. ManagedSerializableObjectInfoPtr objectInfo = serializableObject->getObjectInfo();
  57. ManagedSerializableObjectDataPtr objectData = serializableObject->getObjectData();
  58. MemorySerializer ms;
  59. backupData.mTypeInfo.size = 0;
  60. backupData.mTypeInfo.data = ms.encode(objectInfo.get(), backupData.mTypeInfo.size);
  61. backupData.mObjectData.size = 0;
  62. backupData.mObjectData.data = ms.encode(objectData.get(), backupData.mObjectData.size);
  63. }
  64. else
  65. {
  66. backupData.mTypeInfo.size = 0;
  67. backupData.mTypeInfo.data = nullptr;
  68. backupData.mObjectData.size = 0;
  69. backupData.mObjectData.data = nullptr;
  70. }
  71. }
  72. else
  73. {
  74. MemorySerializer ms;
  75. backupData.mTypeInfo.size = 0;
  76. if (mMissingTypeObjectInfo != nullptr)
  77. backupData.mTypeInfo.data = ms.encode(mMissingTypeObjectInfo.get(), backupData.mTypeInfo.size);
  78. else
  79. backupData.mTypeInfo.data = nullptr;
  80. backupData.mObjectData.size = 0;
  81. if (mMissingTypeObjectData != nullptr)
  82. backupData.mObjectData.data = ms.encode(mMissingTypeObjectData.get(), backupData.mObjectData.size);
  83. else
  84. backupData.mObjectData.data = nullptr;
  85. }
  86. if (clearExisting)
  87. {
  88. if (mManagedInstance != nullptr)
  89. {
  90. mManagedInstance = nullptr;
  91. mono_gchandle_free(mManagedHandle);
  92. mManagedHandle = 0;
  93. }
  94. mRuntimeType = nullptr;
  95. mOnInitializedThunk = nullptr;
  96. mUpdateThunk = nullptr;
  97. mOnDestroyThunk = nullptr;
  98. }
  99. return backupData;
  100. }
  101. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  102. {
  103. initialize(instance);
  104. if (instance != nullptr && data.mTypeInfo.data != nullptr && data.mObjectData.data != nullptr)
  105. {
  106. MemorySerializer ms;
  107. ManagedSerializableObjectInfoPtr objectInfo = std::static_pointer_cast<ManagedSerializableObjectInfo>(ms.decode(data.mTypeInfo.data, data.mTypeInfo.size));
  108. ManagedSerializableObjectDataPtr objectData = std::static_pointer_cast<ManagedSerializableObjectData>(ms.decode(data.mObjectData.data, data.mObjectData.size));
  109. if (!missingType)
  110. {
  111. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(instance);
  112. serializableObject->setObjectData(objectData, objectInfo);
  113. }
  114. else
  115. {
  116. mMissingTypeObjectInfo = objectInfo;
  117. mMissingTypeObjectData = objectData;
  118. }
  119. }
  120. if (!missingType)
  121. {
  122. mMissingTypeObjectInfo = nullptr;
  123. mMissingTypeObjectData = nullptr;
  124. }
  125. mMissingType = missingType;
  126. }
  127. void ManagedComponent::initialize(MonoObject* object)
  128. {
  129. mFullTypeName = mNamespace + "." + mTypeName;
  130. mManagedInstance = object;
  131. MonoClass* managedClass = nullptr;
  132. if (mManagedInstance != nullptr)
  133. {
  134. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  135. ::MonoClass* monoClass = mono_object_get_class(object);
  136. MonoType* monoType = mono_class_get_type(monoClass);
  137. mRuntimeType = mono_type_get_object(MonoManager::instance().getDomain(), monoType);
  138. managedClass = MonoManager::instance().findClass(monoClass);
  139. }
  140. if (managedClass != nullptr)
  141. {
  142. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  143. if (onInitializedMethod != nullptr)
  144. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  145. MonoMethod* updateMethod = managedClass->getMethod("Update", 0);
  146. if (updateMethod != nullptr)
  147. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  148. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  149. if (onResetMethod != nullptr)
  150. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  151. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  152. if (onDestroyMethod != nullptr)
  153. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  154. }
  155. }
  156. void ManagedComponent::update()
  157. {
  158. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  159. {
  160. MonoException* exception = nullptr;
  161. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  162. // for some extra speed.
  163. mUpdateThunk(mManagedInstance, &exception);
  164. MonoUtil::throwIfException(exception);
  165. }
  166. }
  167. void ManagedComponent::triggerOnReset()
  168. {
  169. if (mOnResetThunk != nullptr && mManagedInstance != nullptr)
  170. {
  171. MonoException* exception = nullptr;
  172. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  173. // for some extra speed.
  174. mOnResetThunk(mManagedInstance, &exception);
  175. MonoUtil::throwIfException(exception);
  176. }
  177. }
  178. void ManagedComponent::onInitialized()
  179. {
  180. assert(mManagedInstance != nullptr);
  181. ScriptComponent* nativeInstance = ScriptComponent::toNative(mManagedInstance);
  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. ScriptGameObjectManager::instance().registerScriptComponent(nativeInstance, 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. }