BsManagedComponent.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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)
  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)
  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. }
  81. return backupData;
  82. }
  83. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  84. {
  85. initialize(instance);
  86. if (instance != nullptr && data.data != nullptr)
  87. {
  88. MemorySerializer ms;
  89. GameObjectManager::instance().startDeserialization();
  90. ManagedSerializableObjectPtr serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  91. GameObjectManager::instance().endDeserialization();
  92. if (!missingType)
  93. serializableObject->deserialize();
  94. else
  95. mMissingTypeObjectData = serializableObject;
  96. }
  97. if (!missingType)
  98. mMissingTypeObjectData = nullptr;
  99. mMissingType = missingType;
  100. mRequiresReset = true;
  101. }
  102. void ManagedComponent::initialize(MonoObject* object)
  103. {
  104. mFullTypeName = mNamespace + "." + mTypeName;
  105. mManagedInstance = object;
  106. MonoClass* managedClass = nullptr;
  107. if (mManagedInstance != nullptr)
  108. {
  109. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  110. ::MonoClass* monoClass = mono_object_get_class(object);
  111. MonoType* monoType = mono_class_get_type(monoClass);
  112. mRuntimeType = mono_type_get_object(MonoManager::instance().getDomain(), monoType);
  113. managedClass = MonoManager::instance().findClass(monoClass);
  114. }
  115. if (managedClass != nullptr)
  116. {
  117. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  118. if (onInitializedMethod != nullptr)
  119. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  120. MonoMethod* updateMethod = managedClass->getMethod("Update", 0);
  121. if (updateMethod != nullptr)
  122. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  123. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  124. if (onResetMethod != nullptr)
  125. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  126. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  127. if (onDestroyMethod != nullptr)
  128. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  129. }
  130. }
  131. void ManagedComponent::update()
  132. {
  133. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  134. {
  135. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  136. // for some extra speed.
  137. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  138. }
  139. }
  140. void ManagedComponent::triggerOnReset()
  141. {
  142. if (mRequiresReset && mOnResetThunk != nullptr && mManagedInstance != nullptr)
  143. {
  144. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  145. // for some extra speed.
  146. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  147. }
  148. mRequiresReset = false;
  149. }
  150. void ManagedComponent::onInitialized()
  151. {
  152. assert(mManagedInstance != nullptr);
  153. // Find handle to self
  154. HManagedComponent componentHandle;
  155. if (mParent != nullptr)
  156. {
  157. const Vector<HComponent>& components = mParent->getComponents();
  158. for (auto& component : components)
  159. {
  160. if (component.get() == this)
  161. {
  162. componentHandle = component;
  163. break;
  164. }
  165. }
  166. }
  167. assert(componentHandle != nullptr);
  168. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  169. if (mOnInitializedThunk != nullptr)
  170. {
  171. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  172. // for some extra speed.
  173. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  174. }
  175. triggerOnReset();
  176. }
  177. void ManagedComponent::onDestroyed()
  178. {
  179. assert(mManagedInstance != nullptr);
  180. if (mOnDestroyThunk != nullptr)
  181. {
  182. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  183. // for some extra speed.
  184. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  185. }
  186. if (mManagedInstance != nullptr)
  187. {
  188. mManagedInstance = nullptr;
  189. mono_gchandle_free(mManagedHandle);
  190. }
  191. }
  192. RTTITypeBase* ManagedComponent::getRTTIStatic()
  193. {
  194. return ManagedComponentRTTI::instance();
  195. }
  196. RTTITypeBase* ManagedComponent::getRTTI() const
  197. {
  198. return ManagedComponent::getRTTIStatic();
  199. }
  200. }